png++ 0.2.10

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
79#ifdef PNG_tRNS_SUPPORTED
80 if (png_get_valid(m_png, m_info, chunk_tRNS) == chunk_tRNS)
81 {
82 if (m_color_type == color_type_palette)
83 {
84 int count;
85 byte* values;
86 if (png_get_tRNS(m_png, m_info, & values, & count, NULL)
87 != PNG_INFO_tRNS)
88 {
89 throw error("png_get_tRNS() failed");
90 }
91 m_tRNS.assign(values, values + count);
92 }
93 }
94#endif
95
96#ifdef PNG_gAMA_SUPPORTED
97 if (png_get_valid(m_png, m_info, chunk_gAMA) == chunk_gAMA)
98 {
99#ifdef PNG_FLOATING_POINT_SUPPORTED
100 if (png_get_gAMA(m_png, m_info, &m_gamma) != PNG_INFO_gAMA)
101 {
102 throw error("png_get_gAMA() failed");
103 }
104#else
105 png_fixed_point gamma = 0;
106 if (png_get_gAMA_fixed(m_png, m_info, &gamma) != PNG_INFO_gAMA)
107 {
108 throw error("png_get_gAMA_fixed() failed");
109 }
110 m_gamma = gamma / 100000.0;
111#endif
112 }
113#endif
114 }
115
116 void write() const
117 {
118 assert(m_png);
119 assert(m_info);
120
121 sync_ihdr();
122 if (m_color_type == color_type_palette)
123 {
124 if (! m_palette.empty())
125 {
126 png_set_PLTE(m_png, m_info,
127 const_cast< color* >(& m_palette[0]),
128 (int) m_palette.size());
129 }
130 if (! m_tRNS.empty())
131 {
132#ifdef PNG_tRNS_SUPPORTED
133 png_set_tRNS(m_png, m_info,
134 const_cast< byte* >(& m_tRNS[0]),
135 m_tRNS.size(),
136 NULL);
137#else
138 throw error("attempted to write tRNS chunk; recompile with PNG_tRNS_SUPPORTED");
139#endif
140 }
141 }
142
143 if (m_gamma > 0)
144 {
145#ifdef PNG_gAMA_SUPPORTED
146#ifdef PNG_FLOATING_POINT_SUPPORTED
147 png_set_gAMA(m_png, m_info, m_gamma);
148#else
149 png_set_gAMA_fixed(m_png, m_info,
150 (png_fixed_point)(m_gamma * 100000));
151#endif
152#else
153 throw error("attempted to write gAMA chunk; recompile with PNG_gAMA_SUPPORTED");
154#endif
155 }
156
157 png_write_info(m_png, m_info);
158 }
159
160 void update()
161 {
162 assert(m_png);
163 assert(m_info);
164
165 sync_ihdr();
166 png_read_update_info(m_png, m_info);
167 }
168
169 protected:
170 void sync_ihdr(void) const
171 {
172 png_set_IHDR(m_png,
173 m_info,
174 m_width,
175 m_height,
176 m_bit_depth,
177 m_color_type,
178 m_interlace_type,
179 m_compression_type,
180 m_filter_type);
181 }
182 };
183
184} // namespace png
185
186#endif // PNGPP_INFO_HPP_INCLUDED
png::error
Exception class to represent runtime errors related to png++ operation.
Definition error.hpp:59
png::image_info::m_color_type
color_type m_color_type
Definition image_info.hpp:189
png::image_info::m_palette
palette m_palette
Definition image_info.hpp:193
png::image_info::m_width
uint_32 m_width
Definition image_info.hpp:186
png::image_info::m_bit_depth
int m_bit_depth
Definition image_info.hpp:188
png::image_info::m_gamma
double m_gamma
Definition image_info.hpp:195
png::image_info::m_interlace_type
interlace_type m_interlace_type
Definition image_info.hpp:190
png::image_info::image_info
image_info()
Constructs the image_info object with default values for color_type, interlace_type,...
Definition image_info.hpp:55
png::image_info::m_filter_type
filter_type m_filter_type
Definition image_info.hpp:192
png::image_info::m_compression_type
compression_type m_compression_type
Definition image_info.hpp:191
png::image_info::m_height
uint_32 m_height
Definition image_info.hpp:187
png::image_info::m_tRNS
tRNS m_tRNS
Definition image_info.hpp:194
png::info_base::m_info
png_info * m_info
Definition info_base.hpp:72
png::info_base::m_png
png_struct * m_png
Definition info_base.hpp:71
png::info::write
void write() const
Definition info.hpp:116
png::info::update
void update()
Definition info.hpp:160
png::info::info
info(io_base &io, png_struct *png)
Definition info.hpp:50
png::info::read
void read()
Definition info.hpp:55
png::info::sync_ihdr
void sync_ihdr(void) const
Definition info.hpp:170
png::io_base
Base class for PNG reader/writer classes.
Definition io_base.hpp:63
image_info.hpp
info_base.hpp
png
Definition color.hpp:37
png::color_type_palette
@ color_type_palette
Definition types.hpp:50
png::chunk_gAMA
@ chunk_gAMA
Definition types.hpp:100
png::chunk_PLTE
@ chunk_PLTE
Definition types.hpp:103
png::chunk_tRNS
@ chunk_tRNS
Definition types.hpp:104
png::color
PNG color struct extension. Adds constructors.
Definition color.hpp:44