png.hpp
Go to the documentation of this file.
/*
CLAW - a C++ Library Absolutely Wonderful
CLAW is a free library without any particular aim but being useful to
anyone.
Copyright (C) 2005-2011 Julien Jorge
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
contact: julien.jorge@gamned.org
*/
#ifndef __CLAW_PNG_HPP__
#define __CLAW_PNG_HPP__
#include <claw/image.hpp>
#include <png.h>
#include <zlib.h>
#include <setjmp.h>
#include <iostream>
#include <string>
namespace claw
{
namespace graphic
{
class png : public image
{
public:
/*----------------------------------------------------------------------*/
class reader
{
// classes that need to be accessible from png callbacks.
public:
/*--------------------------------------------------------------------*/
struct source_manager
{
public:
source_manager( std::istream& is );
void read( png_bytep data, png_size_t length );
private:
std::istream& m_input;
}; // struct source_manager
public:
reader( image& img );
reader( image& img, std::istream& f );
void load( std::istream& f );
private:
void read_from_file( std::istream& f );
void check_if_png( png_structp png_ptr, std::istream& f ) const;
void read_image( png_structp png_ptr, png_infop info_ptr );
void read_sequential_image( png_structp png_ptr, png_infop info_ptr );
void read_interlaced_image( png_structp png_ptr, png_infop info_ptr,
unsigned int passes );
void copy_pixel_line
( png_byte color_type, png_bytep data, unsigned int y );
void create_read_structures( png_structp& png_ptr,
png_infop& info_ptr ) const;
private:
image& m_image;
static const unsigned int s_rgba_pixel_size;
}; // class reader
/*----------------------------------------------------------------------*/
class writer
{
public:
struct options
{
public:
enum compression_level
{
no_compression = Z_NO_COMPRESSION,
best_speed = Z_BEST_SPEED,
best_compression = Z_BEST_COMPRESSION,
default_compression = Z_DEFAULT_COMPRESSION
}; // enum compression_level
enum interlace_type
{
none = PNG_INTERLACE_NONE,
adam7 = PNG_INTERLACE_ADAM7
}; // enum interlace_type
public:
options();
options( compression_level compression_level_,
interlace_type interlace_ );
public:
compression_level compression;
interlace_type interlace;
}; // struct options
// classes that need to be accessible from png callbacks.
/*--------------------------------------------------------------------*/
struct target_manager
{
public:
target_manager( std::ostream& os );
void write( png_bytep data, png_size_t length );
void flush();
private:
std::ostream& m_output;
}; // struct target_manager
public:
writer( const image& img );
writer( const image& img, std::ostream& f,
const options& opt = options() );
void save( std::ostream& f, const options& opt = options() ) const;
private:
void set_options( png_structp png_ptr, png_infop info_ptr,
const options& opt ) const;
void save_image( png_structp png_ptr, png_infop info_ptr ) const;
void copy_pixel_line( png_bytep data, unsigned int y ) const;
void create_write_structures( png_structp& png_ptr,
png_infop& info_ptr ) const;
private:
const image& m_image;
static const unsigned int s_rgba_pixel_size;
}; // class writer
public:
png( unsigned int w, unsigned int h );
png( const image& that );
png( std::istream& f );
void save( std::ostream& os,
const writer::options& opt = writer::options() ) const;
}; // class png
} // namespace graphic
} // namespace claw
#endif // __CLAW_PNG_HPP__
claw::graphic::image::image
image()
Constructor. Creates an image without datas.
Definition image.cpp:106
claw::graphic::png::reader::load
void load(std::istream &f)
Load an image from a png file.
Definition png_reader.cpp:114
claw::graphic::png::reader::reader
reader(image &img)
Constructor.
Definition png_reader.cpp:90
claw::graphic::png::writer::save
void save(std::ostream &f, const options &opt=options()) const
Save the image in a PNG file.
Definition png_writer.cpp:163
claw::graphic::png::writer::writer
writer(const image &img)
Constructor.
Definition png_writer.cpp:136
claw::graphic::png::png
png(unsigned int w, unsigned int h)
Constructor. Creates an empty image.
Definition png.cpp:39
claw::graphic::png::save
void save(std::ostream &os, const writer::options &opt=writer::options()) const
Save the image.
Definition png.cpp:73
image.hpp
A class to deal with images.
claw::graphic
Everything about image structures and processing.
Definition bitmap.hpp:43
claw
This is the main namespace.
Definition algorithm.hpp:34
claw::graphic::png::reader::source_manager::read
void read(png_bytep data, png_size_t length)
Read data from the input stream.
Definition png_reader.cpp:74
claw::graphic::png::reader::source_manager::source_manager
source_manager(std::istream &is)
Constructor.
Definition png_reader.cpp:61
claw::graphic::png::writer::options
Parameters of the writing algorithm.
Definition png.hpp:122
claw::graphic::png::writer::options::compression
compression_level compression
Compression level to use in the saved stream.
Definition png.hpp:151
claw::graphic::png::writer::options::interlace
interlace_type interlace
Interlace method to apply to the saved image.
Definition png.hpp:154
claw::graphic::png::writer::options::interlace_type
interlace_type
The algorithm to use to interlace the saved image.
Definition png.hpp:135
claw::graphic::png::writer::options::none
@ none
Saved image won't be interaced.
Definition png.hpp:137
claw::graphic::png::writer::options::adam7
@ adam7
Saved image will be interlaced using the Adam7 algorithm.
Definition png.hpp:141
claw::graphic::png::writer::options::compression_level
compression_level
Compression level in the interlaced image.
Definition png.hpp:126
claw::graphic::png::writer::options::options
options()
Default constructor.
Definition png_writer.cpp:106
claw::graphic::png::writer::target_manager::flush
void flush()
Flush the output stream.
Definition png_writer.cpp:94
claw::graphic::png::writer::target_manager::write
void write(png_bytep data, png_size_t length)
Write data in the ouput stream.
Definition png_writer.cpp:85
claw::graphic::png::writer::target_manager::target_manager
target_manager(std::ostream &os)
Constructor.
Definition png_writer.cpp:72