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
37namespace 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),
77 0))
78 {
79 png_set_read_fn(m_png, & stream, read_data);
80 }
81
83 {
84 png_destroy_read_struct(& m_png,
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,
101 /* transforms = */ 0,
102 /* params = */ 0);
103 }
104
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
133 {
134 if (setjmp(png_jmpbuf(m_png)))
135 {
136 throw error(m_error);
137 }
139 }
140
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
void read()
Definition: end_info.hpp:59
Exception class to represent runtime errors related to png++ operation.
Definition: error.hpp:59
png_info ** get_png_info_ptr()
Definition: info_base.hpp:64
png_info * get_png_info() const
Definition: info_base.hpp:59
void update()
Definition: info.hpp:160
void read()
Definition: info.hpp:55
Base class for PNG reader/writer classes.
Definition: io_base.hpp:63
std::string m_error
Definition: io_base.hpp:462
info m_info
Definition: io_base.hpp:460
png_struct * m_png
Definition: io_base.hpp:459
void set_error(char const *message)
Definition: io_base.hpp:424
end_info m_end_info
Definition: io_base.hpp:461
bool is_error() const
Definition: io_base.hpp:442
void raise_error()
Definition: io_base.hpp:447
void reset_error()
Definition: io_base.hpp:430
The PNG reader class template. This is the low-level reading interface–use image class or consumer cl...
Definition: reader.hpp:67
void read_row(byte *bytes)
Reads a row of image data at a time.
Definition: reader.hpp:120
void read_end_info()
Reads ending info about PNG image.
Definition: reader.hpp:132
void read_png()
Reads the whole PNG data stream into memory. Not particularly useful.
Definition: reader.hpp:93
void read_info()
Reads info about PNG image.
Definition: reader.hpp:108
~reader()
Definition: reader.hpp:82
reader(istream &stream)
Constructs a reader prepared to read PNG image from a stream.
Definition: reader.hpp:73
void update_info()
Definition: reader.hpp:141
Definition: color.hpp:37