png++: generator.hpp Source File

png++ 0.2.10

generator.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_GENERATOR_HPP_INCLUDED
32#define PNGPP_GENERATOR_HPP_INCLUDED
33
34#include <cassert>
35#include <stdexcept>
36#include <iostream>
37#include <ostream>
38
39#include "config.hpp"
40#include "error.hpp"
41#include "streaming_base.hpp"
42#include "writer.hpp"
43
44namespace png
45{
46
112 template< typename pixel,
113 class pixgen,
114 class info_holder = def_image_info_holder,
115 bool interlacing_supported = false >
116 class generator
117 : public streaming_base< pixel, info_holder >
118 {
119 public:
128 template< typename ostream >
129 void write(ostream& stream)
130 {
131 writer< ostream > wr(stream);
132 wr.set_image_info(this->get_info());
133 wr.write_info();
134
135#if __BYTE_ORDER == __LITTLE_ENDIAN
136 if (pixel_traits< pixel >::get_bit_depth() == 16)
137 {
138#ifdef PNG_WRITE_SWAP_SUPPORTED
139 wr.set_swap();
140#else
141 throw error("Cannot write 16-bit image: recompile with PNG_WRITE_SWAP_SUPPORTED.");
142#endif
143 }
144#endif
145
146 size_t pass_count;
147 if (this->get_info().get_interlace_type() != interlace_none)
148 {
149#ifdef PNG_WRITE_INTERLACING_SUPPORTED
150 if (interlacing_supported)
151 {
152 pass_count = wr.set_interlace_handling();
153 }
154 else
155 {
156 throw std::logic_error("Cannot write interlaced image: generator does not support it.");
157 }
158#else
159 throw error("Cannot write interlaced image: interlace handling disabled.");
160#endif
161 }
162 else
163 {
164 pass_count = 1;
165 }
166 pixgen* pixel_gen = static_cast< pixgen* >(this);
167 for (size_t pass = 0; pass < pass_count; ++pass)
168 {
169 pixel_gen->reset(pass);
170
171 for (uint_32 pos = 0; pos < this->get_info().get_height(); ++pos)
172 {
173 wr.write_row(pixel_gen->get_next_row(pos));
174 }
175 }
176
177 wr.write_end_info();
178 }
179
180 protected:
181 typedef streaming_base< pixel, info_holder > base;
182
187 explicit generator(image_info& info)
188 : base(info)
189 {
190 }
191
196 generator(size_t width, size_t height)
197 : base(width, height)
198 {
199 }
200 };
201
202} // namespace png
203
204#endif // PNGPP_GENERATOR_HPP_INCLUDED
png::def_image_info_holder
The default image_info holder class. Stores image_info member object.
Definition streaming_base.hpp:46
png::error
Exception class to represent runtime errors related to png++ operation.
Definition error.hpp:59
png::generator::generator
generator(size_t width, size_t height)
Constructs a generator object prepared to generate an image of specified width and height.
Definition generator.hpp:196
png::generator::generator
generator(image_info &info)
Constructs a generator object using passed image_info object to store image information.
Definition generator.hpp:187
png::generator::write
void write(ostream &stream)
Writes an image to the stream.
Definition generator.hpp:129
png::generator::base
streaming_base< pixel, info_holder > base
Definition generator.hpp:181
png::image_info
Holds information about PNG image.
Definition image_info.hpp:48
png::image_info::get_height
uint_32 get_height() const
Definition image_info.hpp:77
png::info
Holds information about PNG image. Adapter class for IO image operations.
Definition info.hpp:48
png::io_base::set_interlace_handling
int set_interlace_handling() const
Definition io_base.hpp:376
png::io_base::set_image_info
void set_image_info(image_info const &info)
Definition io_base.hpp:102
png::io_base::set_swap
void set_swap() const
Definition io_base.hpp:312
png::streaming_base< pixel, def_image_info_holder >::get_info
image_info const & get_info() const
Definition streaming_base.hpp:107
png::streaming_base< pixel, def_image_info_holder >::streaming_base
streaming_base(image_info &info)
Definition streaming_base.hpp:95
png::writer
PNG writer class template. This is the low-level writing interface–use image class or generator class...
Definition writer.hpp:68
png::writer::write_info
void write_info() const
Write info about PNG image.
Definition writer.hpp:104
png::writer::write_end_info
void write_end_info() const
Reads ending info about PNG image.
Definition writer.hpp:128
png::writer::write_row
void write_row(byte *bytes)
Writes a row of image data at a time.
Definition writer.hpp:116
config.hpp
error.hpp
png
Definition color.hpp:37
png::interlace_none
@ interlace_none
Definition types.hpp:81
png::uint_32
png_uint_32 uint_32
Definition types.hpp:41
streaming_base.hpp
png::pixel_traits
Pixel traits class template.
Definition pixel_traits.hpp:48
writer.hpp