generator.hpp
Go to the documentation of this file.
/*
* Copyright (C) 2007,2008 Alex Shulgin
*
* This file is part of png++ the C++ wrapper for libpng. PNG++ is free
* software; the exact copying conditions are as follows:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
* NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef PNGPP_GENERATOR_HPP_INCLUDED
#define PNGPP_GENERATOR_HPP_INCLUDED
#include <cassert>
#include <stdexcept>
#include <iostream>
#include <ostream>
#include "config.hpp"
#include "error.hpp"
#include "streaming_base.hpp"
#include "writer.hpp"
namespace png
{
template< typename pixel,
class pixgen,
class info_holder = def_image_info_holder,
bool interlacing_supported = false >
class generator
: public streaming_base< pixel, info_holder >
{
public:
template< typename ostream >
void write(ostream& stream)
{
writer< ostream > wr(stream);
wr.set_image_info(this->get_info());
wr.write_info();
#if __BYTE_ORDER == __LITTLE_ENDIAN
if (pixel_traits< pixel >::get_bit_depth() == 16)
{
#ifdef PNG_WRITE_SWAP_SUPPORTED
wr.set_swap();
#else
throw error("Cannot write 16-bit image: recompile with PNG_WRITE_SWAP_SUPPORTED.");
#endif
}
#endif
size_t pass_count;
if (this->get_info().get_interlace_type() != interlace_none)
{
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
if (interlacing_supported)
{
pass_count = wr.set_interlace_handling();
}
else
{
throw std::logic_error("Cannot write interlaced image: generator does not support it.");
}
#else
throw error("Cannot write interlaced image: interlace handling disabled.");
#endif
}
else
{
pass_count = 1;
}
pixgen* pixel_gen = static_cast< pixgen* >(this);
for (size_t pass = 0; pass < pass_count; ++pass)
{
pixel_gen->reset(pass);
for (uint_32 pos = 0; pos < this->get_info().get_height(); ++pos)
{
wr.write_row(pixel_gen->get_next_row(pos));
}
}
wr.write_end_info();
}
protected:
typedef streaming_base< pixel, info_holder > base;
explicit generator(image_info& info)
: base(info)
{
}
generator(size_t width, size_t height)
: base(width, height)
{
}
};
} // namespace png
#endif // PNGPP_GENERATOR_HPP_INCLUDED
streaming_base.hpp
png::generator::write
void write(ostream &stream)
Writes an image to the stream.
Definition: generator.hpp:129
png::generator::generator
generator(image_info &info)
Constructs a generator object using passed image_info object to store image information.
Definition: generator.hpp:187
config.hpp
png::pixel_traits
Pixel traits class template.
Definition: pixel_traits.hpp:48
png::streaming_base::get_info
image_info const & get_info() const
Definition: streaming_base.hpp:107
png::image_info::get_height
uint_32 get_height() const
Definition: image_info.hpp:77
png::image_info
Holds information about PNG image.
Definition: image_info.hpp:47
png::io_base::set_image_info
void set_image_info(image_info const &info)
Definition: io_base.hpp:102
png::uint_32
png_uint_32 uint_32
Definition: types.hpp:41
png::streaming_base
A base class template for consumer and generator classes. Provides default reset() method implementat...
Definition: streaming_base.hpp:90
png::io_base::set_interlace_handling
int set_interlace_handling() const
Definition: io_base.hpp:376
png::writer::write_end_info
void write_end_info() const
Reads ending info about PNG image.
Definition: writer.hpp:128
png::writer
PNG writer class template. This is the low-level writing interface–use image class or generator clas...
Definition: writer.hpp:66
png::interlace_none
Definition: types.hpp:81
png::writer::write_info
void write_info() const
Write info about PNG image.
Definition: writer.hpp:104
error.hpp
png::generator::generator
generator(size_t width, size_t height)
Constructs a generator object prepared to generate an image of specified width and height...
Definition: generator.hpp:196
png::info
Holds information about PNG image. Adapter class for IO image operations.
Definition: info.hpp:45
writer.hpp
png::generator::base
streaming_base< pixel, info_holder > base
Definition: generator.hpp:181
png
Definition: color.hpp:36
png::error
Exception class to represent runtime errors related to png++ operation.
Definition: error.hpp:57
png::generator
Pixel generator class template.
Definition: generator.hpp:116
png::writer::write_row
void write_row(byte *bytes)
Writes a row of image data at a time.
Definition: writer.hpp:116
png::io_base::set_swap
void set_swap() const
Definition: io_base.hpp:312