png++  0.2.9
generator.hpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2007,2008   Alex Shulgin
00003  *
00004  * This file is part of png++ the C++ wrapper for libpng.  PNG++ is free
00005  * software; the exact copying conditions are as follows:
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions are met:
00009  *
00010  * 1. Redistributions of source code must retain the above copyright notice,
00011  * this list of conditions and the following disclaimer.
00012  *
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  * notice, this list of conditions and the following disclaimer in the
00015  * documentation and/or other materials provided with the distribution.
00016  *
00017  * 3. The name of the author may not be used to endorse or promote products
00018  * derived from this software without specific prior written permission.
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00021  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00022  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00023  * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00024  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00025  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00026  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00027  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00028  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00029  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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: recompile with PNG_WRITE_SWAP_SUPPORTED.");
00142 #endif
00143             }
00144 #endif
00145 
00146             size_t pass_count;
00147             if (this->get_info().get_interlace_type() != interlace_none)
00148             {
00149 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
00150                 if (interlacing_supported)
00151                 {
00152                     pass_count = wr.set_interlace_handling();
00153                 }
00154                 else
00155                 {
00156                     throw std::logic_error("Cannot write interlaced image: generator does not support it.");
00157                 }
00158 #else
00159                 throw error("Cannot write interlaced image: interlace handling disabled.");
00160 #endif
00161             }
00162             else
00163             {
00164                 pass_count = 1;
00165             }
00166             pixgen* pixel_gen = static_cast< pixgen* >(this);
00167             for (size_t pass = 0; pass < pass_count; ++pass)
00168             {
00169                 pixel_gen->reset(pass);
00170 
00171                 for (uint_32 pos = 0; pos < this->get_info().get_height(); ++pos)
00172                 {
00173                     wr.write_row(pixel_gen->get_next_row(pos));
00174                 }
00175             }
00176 
00177             wr.write_end_info();
00178         }
00179 
00180     protected:
00181         typedef streaming_base< pixel, info_holder > base;
00182 
00187         explicit generator(image_info& info)
00188             : base(info)
00189         {
00190         }
00191 
00196         generator(size_t width, size_t height)
00197             : base(width, height)
00198         {
00199         }
00200     };
00201 
00202 } // namespace png
00203 
00204 #endif // PNGPP_GENERATOR_HPP_INCLUDED