Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifndef PNGPP_GENERATOR_HPP_INCLUDED
00032 #define PNGPP_GENERATOR_HPP_INCLUDED
00033
00034 #include <cassert>
00035 #include <stdexcept>
00036 #include <iostream>
00037 #include <ostream>
00038
00039 #include "config.hpp"
00040 #include "error.hpp"
00041 #include "streaming_base.hpp"
00042 #include "writer.hpp"
00043
00044 namespace png
00045 {
00046
00112 template< typename pixel,
00113 class pixgen,
00114 class info_holder = def_image_info_holder,
00115 bool interlacing_supported = false >
00116 class generator
00117 : public streaming_base< pixel, info_holder >
00118 {
00119 public:
00128 template< typename ostream >
00129 void write(ostream& stream)
00130 {
00131 writer< ostream > wr(stream);
00132 wr.set_image_info(this->get_info());
00133 wr.write_info();
00134
00135 #if __BYTE_ORDER == __LITTLE_ENDIAN
00136 if (pixel_traits< pixel >::get_bit_depth() == 16)
00137 {
00138 #ifdef PNG_WRITE_SWAP_SUPPORTED
00139 wr.set_swap();
00140 #else
00141 throw error("Cannot write 16-bit image:"
00142 " recompile with PNG_WRITE_SWAP_SUPPORTED.");
00143 #endif
00144 }
00145 #endif
00146
00147 size_t pass_count;
00148 if (this->get_info().get_interlace_type() != interlace_none)
00149 {
00150 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
00151 if (interlacing_supported)
00152 {
00153 pass_count = wr.set_interlace_handling();
00154 }
00155 else
00156 {
00157 throw std::logic_error("Cannot write interlaced image:"
00158 " generator does not support it.");
00159 }
00160 #else
00161 throw error("Cannot write interlaced image:"
00162 " interlace handling disabled.");
00163 #endif
00164 }
00165 else
00166 {
00167 pass_count = 1;
00168 }
00169 pixgen* pixel_gen = static_cast< pixgen* >(this);
00170 for (size_t pass = 0; pass < pass_count; ++pass)
00171 {
00172 pixel_gen->reset(pass);
00173
00174 for (size_t pos = 0; pos < this->get_info().get_height(); ++pos)
00175 {
00176 wr.write_row(pixel_gen->get_next_row(pos));
00177 }
00178 }
00179
00180 wr.write_end_info();
00181 }
00182
00183 protected:
00184 typedef streaming_base< pixel, info_holder > base;
00185
00190 explicit generator(image_info& info)
00191 : base(info)
00192 {
00193 }
00194
00199 generator(size_t width, size_t height)
00200 : base(width, height)
00201 {
00202 }
00203 };
00204
00205 }
00206
00207 #endif // PNGPP_GENERATOR_HPP_INCLUDED