png++: reader.hpp Source File

png++ 0.2.10

reader.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_READER_HPP_INCLUDED
32 #define PNGPP_READER_HPP_INCLUDED
33
34 #include <cassert>
35 #include "io_base.hpp"
36
37 namespace png
38 {
39
64 template< class istream >
65 class reader
66 : public io_base
67 {
68 public:
73 explicit reader(istream& stream)
74 : io_base(png_create_read_struct(PNG_LIBPNG_VER_STRING,
75 static_cast< io_base* >(this),
76 raise_error,
77 0))
78 {
79 png_set_read_fn(m_png, & stream, read_data);
80 }
81
82 ~reader()
83 {
84 png_destroy_read_struct(& m_png,
85 m_info.get_png_info_ptr(),
86 m_end_info.get_png_info_ptr());
87 }
88
93 void read_png()
94 {
95 if (setjmp(png_jmpbuf(m_png)))
96 {
97 throw error(m_error);
98 }
99 png_read_png(m_png,
100 m_info.get_png_info(),
101 /* transforms = */ 0,
102 /* params = */ 0);
103 }
104
108 void read_info()
109 {
110 if (setjmp(png_jmpbuf(m_png)))
111 {
112 throw error(m_error);
113 }
114 m_info.read();
115 }
116
120 void read_row(byte* bytes)
121 {
122 if (setjmp(png_jmpbuf(m_png)))
123 {
124 throw error(m_error);
125 }
126 png_read_row(m_png, bytes, 0);
127 }
128
132 void read_end_info()
133 {
134 if (setjmp(png_jmpbuf(m_png)))
135 {
136 throw error(m_error);
137 }
138 m_end_info.read();
139 }
140
141 void update_info()
142 {
143 m_info.update();
144 }
145
146 private:
147 static void read_data(png_struct* png, byte* data, png_size_t length)
148 {
149 io_base* io = static_cast< io_base* >(png_get_error_ptr(png));
150 reader* rd = static_cast< reader* >(io);
151 rd->reset_error();
152 istream* stream = reinterpret_cast< istream* >(png_get_io_ptr(png));
153 try
154 {
155 stream->read(reinterpret_cast< char* >(data), length);
156 if (!stream->good())
157 {
158 rd->set_error("istream::read() failed");
159 }
160 }
161 catch (std::exception const& error)
162 {
163 rd->set_error(error.what());
164 }
165 catch (...)
166 {
167 assert(!"read_data: caught something wrong");
168 rd->set_error("read_data: caught something wrong");
169 }
170 if (rd->is_error())
171 {
172 rd->raise_error();
173 }
174 }
175 };
176
177 } // namespace png
178
179 #endif // PNGPP_READER_HPP_INCLUDED
png::io_base::m_error
std::string m_error
Definition: io_base.hpp:462
png::info::read
void read()
Definition: info.hpp:55
png::io_base::reset_error
void reset_error()
Definition: io_base.hpp:430
png::reader::update_info
void update_info()
Definition: reader.hpp:141
png::io_base::m_png
png_struct * m_png
Definition: io_base.hpp:459
png::io_base::is_error
bool is_error() const
Definition: io_base.hpp:442
png::reader::~reader
~reader()
Definition: reader.hpp:82
png::reader::read_png
void read_png()
Reads the whole PNG data stream into memory. Not particularly useful.
Definition: reader.hpp:93
png::io_base
Base class for PNG reader/writer classes.
Definition: io_base.hpp:63
png::info::update
void update()
Definition: info.hpp:160
png::io_base::m_info
info m_info
Definition: io_base.hpp:460
png::info_base::get_png_info_ptr
png_info ** get_png_info_ptr()
Definition: info_base.hpp:64
png::reader
The PNG reader class template. This is the low-level reading interface–use image class or consumer cl...
Definition: reader.hpp:67
io_base.hpp
png::end_info::read
void read()
Definition: end_info.hpp:59
png::reader::read_info
void read_info()
Reads info about PNG image.
Definition: reader.hpp:108
png::error
Exception class to represent runtime errors related to png++ operation.
Definition: error.hpp:59
png::io_base::m_end_info
end_info m_end_info
Definition: io_base.hpp:461
png::io_base::set_error
void set_error(char const *message)
Definition: io_base.hpp:424
png::reader::read_row
void read_row(byte *bytes)
Reads a row of image data at a time.
Definition: reader.hpp:120
png::io_base::raise_error
void raise_error()
Definition: io_base.hpp:447
png::info_base::get_png_info
png_info * get_png_info() const
Definition: info_base.hpp:59
png::reader::read_end_info
void read_end_info()
Reads ending info about PNG image.
Definition: reader.hpp:132
png
Definition: color.hpp:37
png::reader::reader
reader(istream &stream)
Constructs a reader prepared to read PNG image from a stream.
Definition: reader.hpp:73