png++  0.2.1
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
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:"
142  " recompile with PNG_WRITE_SWAP_SUPPORTED.");
143 #endif
144  }
145 #endif
146 
147  size_t pass_count;
148  if (this->get_info().get_interlace_type() != interlace_none)
149  {
150 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
151  if (interlacing_supported)
152  {
153  pass_count = wr.set_interlace_handling();
154  }
155  else
156  {
157  throw std::logic_error("Cannot write interlaced image:"
158  " generator does not support it.");
159  }
160 #else
161  throw error("Cannot write interlaced image:"
162  " interlace handling disabled.");
163 #endif
164  }
165  else
166  {
167  pass_count = 1;
168  }
169  pixgen* pixel_gen = static_cast< pixgen* >(this);
170  for (size_t pass = 0; pass < pass_count; ++pass)
171  {
172  pixel_gen->reset(pass);
173 
174  for (size_t pos = 0; pos < this->get_info().get_height(); ++pos)
175  {
176  wr.write_row(pixel_gen->get_next_row(pos));
177  }
178  }
179 
180  wr.write_end_info();
181  }
182 
183  protected:
185 
191  : base(info)
192  {
193  }
194 
199  generator(size_t width, size_t height)
200  : base(width, height)
201  {
202  }
203  };
204 
205 } // namespace png
206 
207 #endif // PNGPP_GENERATOR_HPP_INCLUDED