generator.hpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2007   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 "error.hpp"
00037 #include "streaming_base.hpp"
00038 #include "writer.hpp"
00039 
00040 namespace png
00041 {
00042 
00108     template< typename pixel,
00109               class pixgen,
00110               class info_holder = def_image_info_holder,
00111               bool interlacing_supported = false >
00112     class generator
00113         : public streaming_base< pixel, info_holder >
00114     {
00115     public:
00124         void write(std::ostream& stream)
00125         {
00126             writer wr(stream);
00127             wr.set_image_info(this->get_info());
00128             wr.write_info();
00129 
00130             size_t pass_count;
00131             if (this->get_info().get_interlace_type() != interlace_none)
00132             {
00133 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
00134                 if (interlacing_supported)
00135                 {
00136                     pass_count = wr.set_interlace_handling();
00137                 }
00138                 else
00139                 {
00140                     throw std::logic_error("Cannot write interlaced image --"
00141                                            " generator does not support it.");
00142                 }
00143 #else
00144                 throw error("Cannot write interlaced image --"
00145                             " interlace handling disabled.");
00146 #endif
00147             }
00148             else
00149             {
00150                 pass_count = 1;
00151             }
00152             pixgen* pixel_gen = static_cast< pixgen* >(this);
00153             for (size_t pass = 0; pass < pass_count; ++pass)
00154             {
00155                 pixel_gen->reset(pass);
00156 
00157                 for (size_t pos = 0; pos < this->get_info().get_height(); ++pos)
00158                 {
00159                     wr.write_row(pixel_gen->get_next_row(pos));
00160                 }
00161             }
00162 
00163             wr.write_end_info();
00164         }
00165 
00166     protected:
00167         typedef streaming_base< pixel, info_holder > base;
00168 
00173         explicit generator(image_info& info)
00174             : base(info)
00175         {
00176         }
00177 
00182         generator(size_t width, size_t height)
00183             : base(width, height)
00184         {
00185         }
00186     };
00187 
00188 } // namespace png
00189 
00190 #endif // PNGPP_GENERATOR_HPP_INCLUDED

Generated on Sun Jul 8 19:33:00 2007 for png++ by  doxygen 1.5.2