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__
claw::graphic::pcx::pcx
pcx(unsigned int w, unsigned int h)
Constructor. Creates an empty image.
Definition: pcx.cpp:39
claw::graphic::pcx::reader
This class read data from a pcx file and store it in an image.
Definition: pcx.hpp:158
claw::graphic::image::scanline
One line in the image.
Definition: image.hpp:59
claw::unsigned_integer_of_size
Define the type of an unsigned integer stored with a given number of bits. Template parameters...
Definition: types.hpp:127
claw::graphic::pcx
A class for pcx pictures.
Definition: pcx.hpp:49
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
types.hpp
Some classes for the raw manipulation of the base types.
claw::graphic::pcx::writer::file_output_buffer::pattern_type
u_int_8 pattern_type
The typ of the output patterns.
Definition: pcx.hpp:315
color_palette.hpp
A palette of color, for palettized images.
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::pcx::writer::file_output_buffer
The type of the output buffer associated with the file when encoding RLE data.
Definition: pcx.hpp:311
claw::first
Fuction object to get the first element of a std::pair.
Definition: functional.hpp:44
claw::buffered_istream< std::istream >
claw::graphic::image::load
void load(std::istream &f)
Read the image from a stream.
Definition: image.cpp:381
claw::rle_decoder
A class to help decoding run-length encoded (RLE) streams.
Definition: rle_decoder.hpp:54
claw::graphic::pcx::writer
This class write an image in a pcx file.
Definition: pcx.hpp:303
rle_encoder.hpp
A class to help run-length encoding (RLE) streams.
claw::graphic::pcx::save
void save(std::ostream &os) const
Save the content of the image in a stream.
Definition: pcx.cpp:71
claw::graphic::color_palette
A palette of colors, for palettized images.
Definition: color_palette.hpp:44
rle_decoder.hpp
A class to help decoding run-length encoded (RLE) streams.
buffered_istream.hpp
This class is made to help reading istreams with a buffer.
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::rgb_pixel
RGB pixel.
Definition: pixel.hpp:44
claw::graphic::image
A class to deal with images.
Definition: image.hpp:49
claw::rle_encoder
A class to help run-length encoding (RLE) streams.
Definition: rle_encoder.hpp:58
claw
This is the main namespace.
Definition: algorithm.hpp:33
image.hpp
A class to deal with images.