image.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_IMAGE_HPP_INCLUDED
#define PNGPP_IMAGE_HPP_INCLUDED
#include <fstream>
#include "pixel_buffer.hpp"
#include "generator.hpp"
#include "consumer.hpp"
#include "convert_color_space.hpp"
namespace png
{
template< typename pixel, typename pixel_buffer_type = pixel_buffer< pixel > >
class image
{
public:
typedef pixel_traits< pixel > traits;
typedef pixel_buffer_type pixbuf;
typedef typename pixbuf::row_type row_type;
typedef typename pixbuf::row_access row_access;
typedef typename pixbuf::row_const_access row_const_access;
typedef convert_color_space< pixel > transform_convert;
struct transform_identity
{
void operator()(io_base&) const {}
};
image()
: m_info(make_image_info< pixel >())
{
}
image(uint_32 width, uint_32 height)
: m_info(make_image_info< pixel >())
{
resize(width, height);
}
explicit image(std::string const& filename)
{
read(filename, transform_convert());
}
template< class transformation >
image(std::string const& filename,
transformation const& transform)
{
read(filename.c_str(), transform);
}
explicit image(char const* filename)
{
read(filename, transform_convert());
}
template< class transformation >
image(char const* filename, transformation const& transform)
{
read(filename, transform);
}
explicit image(std::istream& stream)
{
read_stream(stream, transform_convert());
}
template< class transformation >
image(std::istream& stream, transformation const& transform)
{
read_stream(stream, transform);
}
void read(std::string const& filename)
{
read(filename, transform_convert());
}
template< class transformation >
void read(std::string const& filename, transformation const& transform)
{
read(filename.c_str(), transform);
}
void read(char const* filename)
{
read(filename, transform_convert());
}
template< class transformation >
void read(char const* filename, transformation const& transform)
{
std::ifstream stream(filename, std::ios::binary);
if (!stream.is_open())
{
throw std_error(filename);
}
stream.exceptions(std::ios::badbit);
read_stream(stream, transform);
}
void read(std::istream& stream)
{
read_stream(stream, transform_convert());
}
template< class transformation >
void read(std::istream& stream, transformation const& transform)
{
read_stream(stream, transform);
}
template< class istream >
void read_stream(istream& stream)
{
read_stream(stream, transform_convert());
}
template< class istream, class transformation >
void read_stream(istream& stream, transformation const& transform)
{
pixel_consumer pixcon(m_info, m_pixbuf);
pixcon.read(stream, transform);
}
void write(std::string const& filename)
{
write(filename.c_str());
}
void write(char const* filename)
{
std::ofstream stream(filename, std::ios::binary);
if (!stream.is_open())
{
throw std_error(filename);
}
stream.exceptions(std::ios::badbit);
write_stream(stream);
}
template< class ostream >
void write_stream(ostream& stream)
{
pixel_generator pixgen(m_info, m_pixbuf);
pixgen.write(stream);
}
pixbuf& get_pixbuf()
{
return m_pixbuf;
}
pixbuf const& get_pixbuf() const
{
return m_pixbuf;
}
void set_pixbuf(pixbuf const& buffer)
{
m_pixbuf = buffer;
}
uint_32 get_width() const
{
return m_pixbuf.get_width();
}
uint_32 get_height() const
{
return m_pixbuf.get_height();
}
void resize(uint_32 width, uint_32 height)
{
m_pixbuf.resize(width, height);
m_info.set_width(width);
m_info.set_height(height);
}
row_access get_row(size_t index)
{
return m_pixbuf.get_row(index);
}
row_const_access get_row(size_t index) const
{
return m_pixbuf.get_row(index);
}
row_access operator[](size_t index)
{
return m_pixbuf[index];
}
row_const_access operator[](size_t index) const
{
return m_pixbuf[index];
}
pixel get_pixel(size_t x, size_t y) const
{
return m_pixbuf.get_pixel(x, y);
}
void set_pixel(size_t x, size_t y, pixel p)
{
m_pixbuf.set_pixel(x, y, p);
}
interlace_type get_interlace_type() const
{
return m_info.get_interlace_type();
}
void set_interlace_type(interlace_type interlace)
{
m_info.set_interlace_type(interlace);
}
compression_type get_compression_type() const
{
return m_info.get_compression_type();
}
void set_compression_type(compression_type compression)
{
m_info.set_compression_type(compression);
}
filter_type get_filter_type() const
{
return m_info.get_filter_type();
}
void set_filter_type(filter_type filter)
{
m_info.set_filter_type(filter);
}
palette& get_palette()
{
return m_info.get_palette();
}
palette const& get_palette() const
{
return m_info.get_palette();
}
void set_palette(palette const& plte)
{
m_info.set_palette(plte);
}
tRNS const& get_tRNS() const
{
return m_info.get_tRNS();
}
tRNS& get_tRNS()
{
return m_info.get_tRNS();
}
void set_tRNS(tRNS const& trns)
{
m_info.set_tRNS(trns);
}
double get_gamma() const
{
return m_info.get_gamma();
}
void set_gamma(double gamma)
{
m_info.set_gamma(gamma);
}
protected:
template< typename base_impl >
class streaming_impl
: public base_impl
{
public:
streaming_impl(image_info& info, pixbuf& pixels)
: base_impl(info),
m_pixbuf(pixels)
{
}
byte* get_next_row(size_t pos)
{
typedef typename pixbuf::row_traits row_traits;
return reinterpret_cast< byte* >
(row_traits::get_data(m_pixbuf.get_row(pos)));
}
protected:
pixbuf& m_pixbuf;
};
class pixel_consumer
: public streaming_impl< consumer< pixel,
pixel_consumer,
image_info_ref_holder,
/* interlacing = */ true > >
{
public:
pixel_consumer(image_info& info, pixbuf& pixels)
: streaming_impl< consumer< pixel,
pixel_consumer,
image_info_ref_holder,
true > >(info, pixels)
{
}
void reset(size_t pass)
{
if (pass == 0)
{
this->m_pixbuf.resize(this->get_info().get_width(),
this->get_info().get_height());
}
}
};
class pixel_generator
: public streaming_impl< generator< pixel,
pixel_generator,
image_info_ref_holder,
/* interlacing = */ true > >
{
public:
pixel_generator(image_info& info, pixbuf& pixels)
: streaming_impl< generator< pixel,
pixel_generator,
image_info_ref_holder,
true > >(info, pixels)
{
}
};
image_info m_info;
pixbuf m_pixbuf;
};
} // namespace png
#endif // PNGPP_IMAGE_HPP_INCLUDED
png::image::get_filter_type
filter_type get_filter_type() const
Definition: image.hpp:407
png::image::read_stream
void read_stream(istream &stream, transformation const &transform)
Reads an image from a stream using custom transformation.
Definition: image.hpp:249
png::image::transform_convert
convert_color_space< pixel > transform_convert
A transformation functor to convert any image to appropriate color space.
Definition: image.hpp:86
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::generator::write
void write(ostream &stream)
Writes an image to the stream.
Definition: generator.hpp:129
png::image::row_type
pixbuf::row_type row_type
Represents a row of image pixel data.
Definition: image.hpp:78
png::image::get_pixel
pixel get_pixel(size_t x, size_t y) const
Returns a pixel at (x,y) position.
Definition: image.hpp:374
png::image::image
image(uint_32 width, uint_32 height)
Constructs an empty image of specified width and height.
Definition: image.hpp:107
png::image::get_tRNS
tRNS const & get_tRNS() const
Definition: image.hpp:441
png::image::pixel_generator::pixel_generator
pixel_generator(image_info &info, pixbuf &pixels)
Definition: image.hpp:535
png::image::row_const_access
pixbuf::row_const_access row_const_access
Definition: image.hpp:80
png::image::streaming_impl::get_next_row
byte * get_next_row(size_t pos)
Returns the starting address of a pos-th row in the image&#39;s pixel buffer.
Definition: image.hpp:486
png::image::streaming_impl::m_pixbuf
pixbuf & m_pixbuf
Definition: image.hpp:494
png::image::get_pixbuf
pixbuf const & get_pixbuf() const
Returns a const reference to image pixel buffer.
Definition: image.hpp:298
png::interlace_type
interlace_type
Definition: types.hpp:79
png::image::image
image()
Constructs an empty image.
Definition: image.hpp:99
png::tRNS
std::vector< byte > tRNS
The palette transparency map type. Currently implemented as std::vector of png::byte.
Definition: tRNS.hpp:44
png::image::row_access
pixbuf::row_access row_access
Definition: image.hpp:79
png::io_base
Base class for PNG reader/writer classes.
Definition: io_base.hpp:62
png::image_info::get_compression_type
compression_type get_compression_type() const
Definition: image_info.hpp:117
png::image_info::set_palette
void set_palette(palette const &plte)
Definition: image_info.hpp:147
png::image::read
void read(char const *filename)
Reads an image from specified file using default converting transform.
Definition: image.hpp:194
png::image::set_tRNS
void set_tRNS(tRNS const &trns)
Definition: image.hpp:451
png::image_info::get_interlace_type
interlace_type get_interlace_type() const
Definition: image_info.hpp:107
png::image_info::set_filter_type
void set_filter_type(filter_type filter)
Definition: image_info.hpp:132
png::image::image
image(std::istream &stream, transformation const &transform)
Constructs an image reading data from a stream using custom transformation.
Definition: image.hpp:166
png::filter_type
filter_type
Definition: types.hpp:91
png::image_info::set_interlace_type
void set_interlace_type(interlace_type interlace)
Definition: image_info.hpp:112
pixel_buffer.hpp
generator.hpp
png::image::set_palette
void set_palette(palette const &plte)
Replaces the image palette.
Definition: image.hpp:436
png::palette
std::vector< color > palette
The palette type. Currently implemented as std::vector of png::color.
Definition: palette.hpp:44
png::image::write
void write(std::string const &filename)
Writes an image to specified file.
Definition: image.hpp:258
png::pixel_traits
Pixel traits class template.
Definition: pixel_traits.hpp:48
png::image::read
void read(char const *filename, transformation const &transform)
Reads an image from specified file using custom transformaton.
Definition: image.hpp:204
png::image::read
void read(std::istream &stream)
Reads an image from a stream using default converting transform.
Definition: image.hpp:219
png::image::read
void read(std::string const &filename, transformation const &transform)
Reads an image from specified file using custom transformaton.
Definition: image.hpp:185
convert_color_space.hpp
png::image_info::get_gamma
double get_gamma() const
Definition: image_info.hpp:175
png::image_info
Holds information about PNG image.
Definition: image_info.hpp:47
png::image::pixel_consumer::reset
void reset(size_t pass)
Definition: image.hpp:515
png::image::write
void write(char const *filename)
Writes an image to specified file.
Definition: image.hpp:266
png::image::resize
void resize(uint_32 width, uint_32 height)
Resizes the image pixel buffer.
Definition: image.hpp:326
png::image::operator[]
row_const_access operator[](size_t index) const
The non-checking version of get_row() method.
Definition: image.hpp:366
png::image_info::get_palette
palette const & get_palette() const
Definition: image_info.hpp:137
png::image::set_gamma
void set_gamma(double gamma)
Definition: image.hpp:461
png::image::image
image(std::istream &stream)
Constructs an image reading data from a stream using default converting transform.
Definition: image.hpp:156
png::image::image
image(std::string const &filename, transformation const &transform)
Constructs an image reading data from specified file using custom transformaton.
Definition: image.hpp:127
png::image::transform_identity
The default io transformation: does nothing.
Definition: image.hpp:91
png::image::get_width
uint_32 get_width() const
Definition: image.hpp:313
png::image::get_interlace_type
interlace_type get_interlace_type() const
Definition: image.hpp:387
png::compression_type
compression_type
Definition: types.hpp:85
png::image_info::set_width
void set_width(uint_32 width)
Definition: image_info.hpp:72
png::image_info_ref_holder
An image_info holder class. Stores a reference to the image_info object. The image_info object itself...
Definition: streaming_base.hpp:67
png::uint_32
png_uint_32 uint_32
Definition: types.hpp:41
png::image
Class template to represent PNG image.
Definition: image.hpp:62
png::image::streaming_impl
A common base class template for pixel_consumer and pixel_generator classes.
Definition: image.hpp:472
png::byte
png_byte byte
Definition: types.hpp:39
png::image::image
image(std::string const &filename)
Constructs an image reading data from specified file using default converting transform.
Definition: image.hpp:117
png::image_info::get_filter_type
filter_type get_filter_type() const
Definition: image_info.hpp:127
png::consumer
Pixel consumer class template.
Definition: consumer.hpp:125
png::image::m_pixbuf
pixbuf m_pixbuf
Definition: image.hpp:545
png::image::transform_identity::operator()
void operator()(io_base &) const
Definition: image.hpp:93
png::image::image
image(char const *filename, transformation const &transform)
Constructs an image reading data from specified file using custom transformaton.
Definition: image.hpp:147
png::image_info::set_compression_type
void set_compression_type(compression_type compression)
Definition: image_info.hpp:122
png::image::set_pixbuf
void set_pixbuf(pixbuf const &buffer)
Replaces the image pixel buffer.
Definition: image.hpp:308
png::image::get_palette
palette const & get_palette() const
Returns a const reference to the image palette.
Definition: image.hpp:428
png::image::get_compression_type
compression_type get_compression_type() const
Definition: image.hpp:397
png::image::image
image(char const *filename)
Constructs an image reading data from specified file using default converting transform.
Definition: image.hpp:137
png::image::set_compression_type
void set_compression_type(compression_type compression)
Definition: image.hpp:402
png::image_info::set_height
void set_height(uint_32 height)
Definition: image_info.hpp:82
png::image::traits
pixel_traits< pixel > traits
The pixel traits type for pixel.
Definition: image.hpp:68
png::image::get_row
row_const_access get_row(size_t index) const
Returns a const reference to the row of image data at specified index.
Definition: image.hpp:350
png::image::get_row
row_access get_row(size_t index)
Returns a reference to the row of image data at specified index.
Definition: image.hpp:339
png::image::get_gamma
double get_gamma() const
Definition: image.hpp:456
png::image::get_tRNS
tRNS & get_tRNS()
Definition: image.hpp:446
png::image::operator[]
row_access operator[](size_t index)
The non-checking version of get_row() method.
Definition: image.hpp:358
png::image::get_palette
palette & get_palette()
Returns a reference to the image palette.
Definition: image.hpp:420
consumer.hpp
png::info
Holds information about PNG image. Adapter class for IO image operations.
Definition: info.hpp:45
png::image_info::set_tRNS
void set_tRNS(tRNS const &trns)
Definition: image_info.hpp:170
png::image_info::get_tRNS
tRNS const & get_tRNS() const
Definition: image_info.hpp:160
png::image::get_pixbuf
pixbuf & get_pixbuf()
Returns a reference to image pixel buffer.
Definition: image.hpp:290
png::image::set_filter_type
void set_filter_type(filter_type filter)
Definition: image.hpp:412
png::image::write_stream
void write_stream(ostream &stream)
Writes an image to a stream.
Definition: image.hpp:281
png::make_image_info
image_info make_image_info()
Returns an image_info object with color_type and bit_depth fields setup appropriate for the pixel typ...
Definition: image_info.hpp:204
png::image::streaming_impl::streaming_impl
streaming_impl(image_info &info, pixbuf &pixels)
Definition: image.hpp:476
png::std_error
Exception class to represent standard library errors (generally IO).
Definition: error.hpp:76
png::convert_color_space
IO transformation class template. Converts image color space.
Definition: convert_color_space.hpp:256
png::image::pixbuf
pixel_buffer_type pixbuf
The pixel buffer type for pixel.
Definition: image.hpp:73
png::image::read_stream
void read_stream(istream &stream)
Reads an image from a stream using default converting transform.
Definition: image.hpp:239
png
Definition: color.hpp:36
png::image::get_height
uint_32 get_height() const
Definition: image.hpp:318
png::consumer::read
void read(istream &stream)
Reads an image from the stream using default io transformation.
Definition: consumer.hpp:144
png::image::set_pixel
void set_pixel(size_t x, size_t y, pixel p)
Replaces a pixel at (x,y) position.
Definition: image.hpp:382
png::image::set_interlace_type
void set_interlace_type(interlace_type interlace)
Definition: image.hpp:392
png::image_info::set_gamma
void set_gamma(double gamma)
Definition: image_info.hpp:180
png::image::pixel_consumer::pixel_consumer
pixel_consumer(image_info &info, pixbuf &pixels)
Definition: image.hpp:507
png::image::read
void read(std::string const &filename)
Reads an image from specified file using default converting transform.
Definition: image.hpp:175
png::image::m_info
image_info m_info
Definition: image.hpp:544
png::generator
Pixel generator class template.
Definition: generator.hpp:116
png::image::pixel_consumer
The pixel buffer adapter for reading pixel data.
Definition: image.hpp:500
png::image::read
void read(std::istream &stream, transformation const &transform)
Reads an image from a stream using custom transformation.
Definition: image.hpp:229
png::image::pixel_generator
The pixel buffer adapter for writing pixel data.
Definition: image.hpp:528