- Main Page
- Namespaces
- Classes
- Files

- File List

# 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:"
    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 } // namespace png
    00206
    00207 #endif // PNGPP_GENERATOR_HPP_INCLUDED