pcx.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_PCX_HPP__
#define __CLAW_PCX_HPP__
#include <iostream>
#include <claw/image.hpp>
#include <claw/rle_decoder.hpp>
#include <claw/rle_encoder.hpp>
#include <claw/color_palette.hpp>
#include <claw/types.hpp>
#include <claw/buffered_istream.hpp>
namespace claw
{
namespace graphic
{
class pcx : public image
{
private:
/* Some convenient renaming. */
typedef unsigned_integer_of_size<8>::type u_int_8;
typedef unsigned_integer_of_size<16>::type u_int_16;
typedef integer_of_size<8>::type int_8;
typedef integer_of_size<16>::type int_16;
enum format_version
{
v_2_5 = 0,
v_2_8_with_palette = 2,
v_2_8_without_palette = 3,
v_win = 4,
v_3_0 = 5
}; // enum format_version
# pragma pack (push,1)
/*--------------------------------------------------------------------*/
class header
{
public:
u_int_8 manufacturer;
u_int_8 version;
u_int_8 encoded;
u_int_8 bpp;
struct
{
u_int_16 x_min;
u_int_16 y_min;
u_int_16 x_max;
u_int_16 y_max;
} window;
u_int_16 horizontal_dpi;
u_int_16 vertical_dpi;
rgb_pixel_8 color_map[16];
u_int_8 reserved;
u_int_8 color_planes;
u_int_16 bytes_per_line;
u_int_16 palette_info;
struct
{
u_int_16 horizontal;
u_int_16 vertical;
} screen_size;
u_int_8 filler[54];
}; // struct header
# pragma pack (pop)
/*----------------------------------------------------------------------*/
typedef color_palette<rgba_pixel_8> color_palette32;
typedef std::vector<u_int_8> color_plane_type;
public:
/*----------------------------------------------------------------------*/
class reader
{
private:
/*--------------------------------------------------------------------*/
typedef buffered_istream<std::istream> rle_pcx_input_buffer;
/*--------------------------------------------------------------------*/
class rle_pcx_output_buffer
{
public:
rle_pcx_output_buffer( color_plane_type& result );
void fill( unsigned int n, u_int_8 pattern );
void copy( unsigned int n, rle_pcx_input_buffer& buffer );
bool completed() const;
private:
color_plane_type& m_result;
unsigned int m_position;
}; // class rle_pcx_output_buffer
/*--------------------------------------------------------------------*/
class rle_pcx_decoder
: public rle_decoder< u_int_8,
rle_pcx_input_buffer,
rle_pcx_output_buffer >
{
private:
virtual void
read_mode( input_buffer_type& input, output_buffer_type& output );
}; // class rle_pcx_decoder
/*--------------------------------------------------------------------*/
class converter_mono
{
public:
void operator()
( const std::vector<color_plane_type>& scanline, image& img,
unsigned int y ) const;
}; // class converter_mono
/*--------------------------------------------------------------------*/
class converter_16
{
public:
converter_16( const header& h );
void operator()
( const std::vector<color_plane_type>& scanline, image& img,
unsigned int y ) const;
private:
const header& m_header;
}; // class converter_16
/*--------------------------------------------------------------------*/
class converter_256
{
public:
converter_256( const color_palette32& palette );
void operator()
( const std::vector<color_plane_type>& scanline, image& img,
unsigned int y ) const;
private:
const color_palette32& m_palette;
}; // class converter_256
/*--------------------------------------------------------------------*/
class converter_true_color
{
public:
void operator()
( const std::vector<color_plane_type>& scanline, image& img,
unsigned int y ) const;
}; // class converter_true_color
public:
reader( image& img );
reader( image& img, std::istream& f );
void load( std::istream& f );
private:
void check_if_pcx( const header& h ) const;
void load_mono( const header& h, std::istream& f );
void load_16_color_mapped( const header& h, std::istream& f );
void load_true_color( const header& h, std::istream& f );
void load_256_color_mapped( const header& h, std::istream& f );
void
decompress_line( std::istream& f, color_plane_type& scanline ) const;
template<typename Converter>
void decompress
( const header& h, std::istream& f, const Converter& convert );
private:
image& m_image;
}; // class reader
/*----------------------------------------------------------------------*/
class writer
{
public:
/*--------------------------------------------------------------------*/
class file_output_buffer
{
public:
typedef u_int_8 pattern_type;
public:
file_output_buffer( std::ostream& os );
void encode( unsigned int n, pattern_type pattern );
template<typename Iterator>
void raw( Iterator first, Iterator last );
unsigned int min_interesting() const;
unsigned int max_encodable() const;
private:
std::ostream& m_stream;
}; // class file_output_buffer
/*--------------------------------------------------------------------*/
typedef rle_encoder<file_output_buffer> rle_pcx_encoder;
public:
writer( const image& img );
writer( const image& img, std::ostream& f );
void save( std::ostream& os ) const;
private:
void write_header
( std::ostream& os, unsigned int bytes_per_line ) const;
void save_rle_true_color
( std::ostream& os, unsigned int bytes_per_line ) const;
private:
const image& m_image;
}; // class writer
public:
pcx( unsigned int w, unsigned int h );
pcx( const image& that );
pcx( std::istream& f );
void save( std::ostream& os ) const;
}; // class pcx
} // namespace graphic
} // namespace claw
#include <claw/impl/pcx_writer.tpp>
#include <claw/impl/pcx_reader.tpp>
#endif // __CLAW_PCX_HPP__
buffered_istream.hpp
This class is made to help reading istreams with a buffer.
claw::buffered_istream
This class is made to help reading istreams with a buffer.
Definition buffered_istream.hpp:43
claw::first
Fuction object to get the first element of a std::pair.
Definition functional.hpp:44
claw::graphic::color_palette
A palette of colors, for palettized images.
Definition color_palette.hpp:45
claw::graphic::image::scanline
One line in the image.
Definition image.hpp:61
claw::graphic::image::fill
void fill(const math::rectangle< int > r, const pixel_type &c)
Fill an area of the image with a given color.
Definition image.cpp:314
claw::graphic::image::image
image()
Constructor. Creates an image without datas.
Definition image.cpp:106
claw::graphic::pcx::reader::load
void load(std::istream &f)
Load an image from a pcx file.
Definition pcx_reader.cpp:294
claw::graphic::pcx::reader::reader
reader(image &img)
Constructor.
Definition pcx_reader.cpp:270
claw::graphic::pcx::writer::file_output_buffer::max_encodable
unsigned int max_encodable() const
Get the maximum number of pixel a code can encode.
Definition pcx_writer.cpp:77
claw::graphic::pcx::writer::file_output_buffer::encode
void encode(unsigned int n, pattern_type pattern)
Encode a pixel data.
Definition pcx_writer.cpp:51
claw::graphic::pcx::writer::file_output_buffer::min_interesting
unsigned int min_interesting() const
Get the minimum number of pixels needed for encoding.
Definition pcx_writer.cpp:67
claw::graphic::pcx::writer::file_output_buffer::file_output_buffer
file_output_buffer(std::ostream &os)
Constructor.
Definition pcx_writer.cpp:38
claw::graphic::pcx::writer::file_output_buffer::pattern_type
u_int_8 pattern_type
The typ of the output patterns.
Definition pcx.hpp:315
claw::graphic::pcx::writer::save
void save(std::ostream &os) const
Save the content of the image in a stream.
Definition pcx_writer.cpp:113
claw::graphic::pcx::writer::writer
writer(const image &img)
Constructor.
Definition pcx_writer.cpp:90
claw::graphic::pcx::writer::rle_pcx_encoder
rle_encoder< file_output_buffer > rle_pcx_encoder
RLE encoder for pcx format.
Definition pcx.hpp:338
claw::graphic::pcx::pcx
pcx(unsigned int w, unsigned int h)
Constructor. Creates an empty image.
Definition pcx.cpp:39
claw::graphic::pcx::save
void save(std::ostream &os) const
Save the content of the image in a stream.
Definition pcx.cpp:71
claw::rle_decoder
A class to help decoding run-length encoded (RLE) streams.
Definition rle_decoder.hpp:55
claw::rle_decoder< u_int_8, rle_pcx_input_buffer, rle_pcx_output_buffer >::output_buffer_type
rle_pcx_output_buffer output_buffer_type
Definition rle_decoder.hpp:64
claw::rle_decoder< u_int_8, rle_pcx_input_buffer, rle_pcx_output_buffer >::input_buffer_type
rle_pcx_input_buffer input_buffer_type
Definition rle_decoder.hpp:61
claw::rle_encoder
A class to help run-length encoding (RLE) streams.
Definition rle_encoder.hpp:59
color_palette.hpp
A palette of color, for palettized images.
image.hpp
A class to deal with images.
claw::graphic
Everything about image structures and processing.
Definition bitmap.hpp:43
claw::graphic::rgb_pixel_8
rgb_pixel rgb_pixel_8
A color with 8 bits per component.
Definition pixel.hpp:123
claw::pattern
Here are the design patterns.
Definition basic_singleton.hpp:38
claw
This is the main namespace.
Definition algorithm.hpp:34
rle_decoder.hpp
A class to help decoding run-length encoded (RLE) streams.
rle_encoder.hpp
A class to help run-length encoding (RLE) streams.
claw::integer_of_size::type
find_type_by_size< Size, signed_integers >::type type
The integer type that matches the given size.
Definition types.hpp:116
claw::unsigned_integer_of_size::type
find_type_by_size< Size, unsigned_integers >::type type
The integer type that matches the given size.
Definition types.hpp:130
types.hpp
Some classes for the raw manipulation of the base types.