png++  0.2.1
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;"
119  " recompile with PNG_tRNS_SUPPORTED");
120 #endif
121  }
122  }
123  png_write_info(m_png, m_info);
124  }
125 
126  void update()
127  {
128  assert(m_png);
129  assert(m_info);
130 
131  sync_ihdr();
132  png_read_update_info(m_png, m_info);
133  }
134 
135  protected:
136  void sync_ihdr(void) const
137  {
138  png_set_IHDR(m_png,
139  m_info,
140  m_width,
141  m_height,
142  m_bit_depth,
143  m_color_type,
146  m_filter_type);
147  }
148  };
149 
150 } // namespace png
151 
152 #endif // PNGPP_INFO_HPP_INCLUDED