png++  0.2.7
info.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007,2008 Alex Shulgin
3  *
4  * This file is part of png++ the C++ wrapper for libpng. PNG++ is free
5  * software; the exact copying conditions are as follows:
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice,
11  * this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * 3. The name of the author may not be used to endorse or promote products
18  * derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
23  * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
25  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 #ifndef PNGPP_INFO_HPP_INCLUDED
32 #define PNGPP_INFO_HPP_INCLUDED
33 
34 #include <cassert>
35 #include "info_base.hpp"
36 #include "image_info.hpp"
37 
38 namespace png
39 {
40 
45  class info
46  : public info_base,
47  public image_info
48  {
49  public:
50  info(io_base& io, png_struct* png)
51  : info_base(io, png)
52  {
53  }
54 
55  void read()
56  {
57  assert(m_png);
58  assert(m_info);
59 
60  png_read_info(m_png, m_info);
61  png_get_IHDR(m_png,
62  m_info,
63  & m_width,
64  & m_height,
65  reinterpret_cast< int* >(& m_bit_depth),
66  reinterpret_cast< int* >(& m_color_type),
67  reinterpret_cast< int* >(& m_interlace_type),
68  reinterpret_cast< int* >(& m_compression_type),
69  reinterpret_cast< int* >(& m_filter_type));
70 
71  if (png_get_valid(m_png, m_info, chunk_PLTE) == chunk_PLTE)
72  {
73  png_color* colors = 0;
74  int count = 0;
75  png_get_PLTE(m_png, m_info, & colors, & count);
76  m_palette.assign(colors, colors + count);
77  }
78 #ifdef PNG_tRNS_SUPPORTED
79  if (png_get_valid(m_png, m_info, chunk_tRNS) == chunk_tRNS)
80  {
82  {
83  int count;
84  byte* values;
85  if (png_get_tRNS(m_png, m_info, & values, & count, NULL)
86  != PNG_INFO_tRNS)
87  {
88  throw error("png_get_tRNS() failed");
89  }
90  m_tRNS.assign(values, values + count);
91  }
92  }
93 #endif
94  }
95 
96  void write() const
97  {
98  assert(m_png);
99  assert(m_info);
100 
101  sync_ihdr();
103  {
104  if (! m_palette.empty())
105  {
106  png_set_PLTE(m_png, m_info,
107  const_cast< color* >(& m_palette[0]),
108  m_palette.size());
109  }
110  if (! m_tRNS.empty())
111  {
112 #ifdef PNG_tRNS_SUPPORTED
113  png_set_tRNS(m_png, m_info,
114  const_cast< byte* >(& m_tRNS[0]),
115  m_tRNS.size(),
116  NULL);
117 #else
118  throw error("attempted to write tRNS chunk; recompile with PNG_tRNS_SUPPORTED");
119 #endif
120  }
121  }
122  png_write_info(m_png, m_info);
123  }
124 
125  void update()
126  {
127  assert(m_png);
128  assert(m_info);
129 
130  sync_ihdr();
131  png_read_update_info(m_png, m_info);
132  }
133 
134  protected:
135  void sync_ihdr(void) const
136  {
137  png_set_IHDR(m_png,
138  m_info,
139  m_width,
140  m_height,
141  m_bit_depth,
142  m_color_type,
145  m_filter_type);
146  }
147  };
148 
149 } // namespace png
150 
151 #endif // PNGPP_INFO_HPP_INCLUDED
size_t m_bit_depth
Definition: image_info.hpp:177
png_struct * m_png
Definition: info_base.hpp:71
info(io_base &io, png_struct *png)
Definition: info.hpp:50
void write() const
Definition: info.hpp:96
Base class for PNG reader/writer classes.
Definition: io_base.hpp:62
Definition: types.hpp:104
void read()
Definition: info.hpp:55
Definition: types.hpp:103
filter_type m_filter_type
Definition: image_info.hpp:181
palette m_palette
Definition: image_info.hpp:182
void update()
Definition: info.hpp:125
void sync_ihdr(void) const
Definition: info.hpp:135
uint_32 m_height
Definition: image_info.hpp:176
Holds information about PNG image.
Definition: image_info.hpp:47
color_type m_color_type
Definition: image_info.hpp:178
uint_32 m_width
Definition: image_info.hpp:175
png_byte byte
Definition: types.hpp:39
Holds information about PNG image. Adapter class for IO image operations.
Definition: info.hpp:45
tRNS m_tRNS
Definition: image_info.hpp:183
Internal class to hold PNG info or end_info.
Definition: info_base.hpp:46
Definition: color.hpp:36
png_info * m_info
Definition: info_base.hpp:72
Definition: types.hpp:50
Exception class to represent runtime errors related to png++ operation.
Definition: error.hpp:47
compression_type m_compression_type
Definition: image_info.hpp:180
interlace_type m_interlace_type
Definition: image_info.hpp:179