png++  0.2.9
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 
44 namespace 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
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:
182 
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
void write(ostream &stream)
Writes an image to the stream.
Definition: generator.hpp:129
generator(image_info &info)
Constructs a generator object using passed image_info object to store image information.
Definition: generator.hpp:187
int set_interlace_handling() const
Definition: io_base.hpp:376
void write_info() const
Write info about PNG image.
Definition: writer.hpp:104
Pixel traits class template.
Definition: pixel_traits.hpp:48
Holds information about PNG image.
Definition: image_info.hpp:47
uint_32 get_height() const
Definition: image_info.hpp:77
void set_swap() const
Definition: io_base.hpp:312
void set_image_info(image_info const &info)
Definition: io_base.hpp:102
png_uint_32 uint_32
Definition: types.hpp:41
A base class template for consumer and generator classes. Provides default reset() method implementat...
Definition: streaming_base.hpp:90
void write_end_info() const
Reads ending info about PNG image.
Definition: writer.hpp:128
PNG writer class template. This is the low-level writing interface–use image class or generator clas...
Definition: writer.hpp:66
Definition: types.hpp:81
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
image_info const & get_info() const
Definition: streaming_base.hpp:107
Holds information about PNG image. Adapter class for IO image operations.
Definition: info.hpp:45
streaming_base< pixel, info_holder > base
Definition: generator.hpp:181
Definition: color.hpp:36
Exception class to represent runtime errors related to png++ operation.
Definition: error.hpp:57
Pixel generator class template.
Definition: generator.hpp:116
void write_row(byte *bytes)
Writes a row of image data at a time.
Definition: writer.hpp:116