consumer.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_CONSUMER_HPP_INCLUDED
#define PNGPP_CONSUMER_HPP_INCLUDED
#include <cassert>
#include <stdexcept>
#include <iostream>
#include <istream>
#include "config.hpp"
#include "error.hpp"
#include "streaming_base.hpp"
#include "reader.hpp"
#include "pixel_buffer.hpp"
namespace png
{
template< typename pixel,
class pixcon,
class info_holder = def_image_info_holder,
bool interlacing_supported = false >
class consumer
: public streaming_base< pixel, info_holder >
{
public:
typedef pixel_traits< pixel > traits;
struct transform_identity
{
void operator()(io_base&) const {}
};
template< typename istream >
void read(istream& stream)
{
read(stream, transform_identity());
}
template< typename istream, class transformation >
void read(istream& stream, transformation const& transform)
{
reader< istream > rd(stream);
rd.read_info();
transform(rd);
#if __BYTE_ORDER == __LITTLE_ENDIAN
if (pixel_traits< pixel >::get_bit_depth() == 16)
{
#ifdef PNG_READ_SWAP_SUPPORTED
rd.set_swap();
#else
throw error("Cannot read 16-bit image: recompile with PNG_READ_SWAP_SUPPORTED.");
#endif
}
#endif
// interlace handling _must_ be set up prior to info update
size_t pass_count;
if (rd.get_interlace_type() != interlace_none)
{
#ifdef PNG_READ_INTERLACING_SUPPORTED
pass_count = rd.set_interlace_handling();
#else
throw error("Cannot read interlaced image: interlace handling disabled.");
#endif
}
else
{
pass_count = 1;
}
rd.update_info();
if (rd.get_color_type() != traits::get_color_type()
|| rd.get_bit_depth() != traits::get_bit_depth())
{
throw std::logic_error("color type and/or bit depth mismatch"
" in png::consumer::read()");
}
this->get_info() = rd.get_image_info();
pixcon* pixel_con = static_cast< pixcon* >(this);
if (pass_count > 1 && !interlacing_supported)
{
skip_interlaced_rows(rd, pass_count);
pass_count = 1;
}
read_rows(rd, pass_count, pixel_con);
rd.read_end_info();
}
protected:
typedef streaming_base< pixel, info_holder > base;
explicit consumer(image_info& info)
: base(info)
{
}
private:
template< typename istream >
void skip_interlaced_rows(reader< istream >& rd, size_t pass_count)
{
typedef std::vector< pixel > row;
typedef row_traits< row > row_traits_type;
row dummy_row(this->get_info().get_width());
for (size_t pass = 1; pass < pass_count; ++pass)
{
rd.read_row(reinterpret_cast< byte* >
(row_traits_type::get_data(dummy_row)));
}
}
template< typename istream >
void read_rows(reader< istream >& rd, size_t pass_count,
pixcon* pixel_con)
{
for (size_t pass = 0; pass < pass_count; ++pass)
{
pixel_con->reset(pass);
for (uint_32 pos = 0; pos < this->get_info().get_height(); ++pos)
{
rd.read_row(pixel_con->get_next_row(pos));
}
}
}
};
} // namespace png
#endif // PNGPP_CONSUMER_HPP_INCLUDED
streaming_base.hpp
png::row_traits
The pixel row traits class template. Provides a common way to get starting address of the row for pac...
Definition: pixel_buffer.hpp:53
png::io_base::get_bit_depth
int get_bit_depth() const
Definition: io_base.hpp:150
png::io_base
Base class for PNG reader/writer classes.
Definition: io_base.hpp:62
png::reader::read_info
void read_info()
Reads info about PNG image.
Definition: reader.hpp:108
png::consumer::traits
pixel_traits< pixel > traits
Definition: consumer.hpp:129
png::io_base::set_interlace_handling
int set_interlace_handling() const
Definition: io_base.hpp:376
pixel_buffer.hpp
config.hpp
png::reader
The PNG reader class template. This is the low-level reading interface–use image class or consumer c...
Definition: reader.hpp:65
png::pixel_traits
Pixel traits class template.
Definition: pixel_traits.hpp:48
png::io_base::get_color_type
color_type get_color_type() const
Definition: io_base.hpp:140
png::image_info
Holds information about PNG image.
Definition: image_info.hpp:47
png::reader::read_row
void read_row(byte *bytes)
Reads a row of image data at a time.
Definition: reader.hpp:120
png::reader::read_end_info
void read_end_info()
Reads ending info about PNG image.
Definition: reader.hpp:132
png::image_info::get_height
uint_32 get_height() const
Definition: image_info.hpp:77
png::io_base::set_swap
void set_swap() const
Definition: io_base.hpp:312
png::consumer::transform_identity::operator()
void operator()(io_base &) const
Definition: consumer.hpp:136
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::consumer
Pixel consumer class template.
Definition: consumer.hpp:125
png::consumer::read
void read(istream &stream, transformation const &transform)
Reads an image from the stream using custom io transformation.
Definition: consumer.hpp:158
png::reader::update_info
void update_info()
Definition: reader.hpp:141
png::interlace_none
Definition: types.hpp:81
reader.hpp
png::io_base::get_image_info
image_info const & get_image_info() const
Definition: io_base.hpp:97
error.hpp
png::streaming_base::get_info
image_info const & get_info() const
Definition: streaming_base.hpp:107
png::consumer::transform_identity
The default io transformation: does nothing.
Definition: consumer.hpp:134
png::info
Holds information about PNG image. Adapter class for IO image operations.
Definition: info.hpp:45
png::consumer::consumer
consumer(image_info &info)
Constructs a consumer object using passed image_info object to store image information.
Definition: consumer.hpp:218
png
Definition: color.hpp:36
png::consumer::read
void read(istream &stream)
Reads an image from the stream using default io transformation.
Definition: consumer.hpp:144
png::error
Exception class to represent runtime errors related to png++ operation.
Definition: error.hpp:57
png::consumer::base
streaming_base< pixel, info_holder > base
Definition: consumer.hpp:212
png::io_base::get_interlace_type
interlace_type get_interlace_type() const
Definition: io_base.hpp:160