targa.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_TARGA_HPP__
#define __CLAW_TARGA_HPP__
#include <iostream>
#include <claw/image.hpp>
#include <claw/rle_decoder.hpp>
#include <claw/rle_encoder.hpp>
#include <claw/color_palette.hpp>
#include <claw/buffered_istream.hpp>
namespace claw
{
namespace graphic
{
class targa : public image
{
private:
/*----------------------------------------------------------------------*/
class file_structure
{
public:
enum image_coding
{
color_mapped = 1,
true_color = 2,
black_and_white = 3,
rle_color_mapped = 9,
rle_true_color = 10,
rle_black_and_white = 11
}; // enum image_coding
# pragma pack (push,1)
/*--------------------------------------------------------------------*/
class header
{
public:
header();
header( unsigned int w, unsigned int h );
public:
char id_length;
char color_map;
char image_type;
struct
{
unsigned short first_entry_index;
unsigned short length;
unsigned char entry_size;
} color_map_specification;
struct specification
{
unsigned short x_origin;
unsigned short y_origin;
unsigned short width;
unsigned short height;
unsigned char bpp;
unsigned char descriptor;
bool up_down_oriented() const ;
bool left_right_oriented() const ;
unsigned char alpha() const;
}; // struct specification
specification image_specification;
}; // struct header
/*--------------------------------------------------------------------*/
struct developer_item
{
unsigned short tag;
unsigned int offset;
unsigned int size;
}; // struct developer_item
/*--------------------------------------------------------------------*/
struct extension
{
}; // struct extension
/*--------------------------------------------------------------------*/
class footer
{
public:
footer();
bool is_valid() const;
public:
unsigned int extension_offset;
unsigned int developer_offset;
char signature[18];
private:
static const std::string s_signature;
}; // struct footer
# pragma pack (pop)
}; // class file_structure
/*----------------------------------------------------------------------*/
struct pixel16
{
}; // struct pixel16
/*----------------------------------------------------------------------*/
struct pixel8
{
}; // struct pixel8
/*----------------------------------------------------------------------*/
typedef color_palette<rgba_pixel_8> color_palette32;
public:
/*----------------------------------------------------------------------*/
class reader : private file_structure
{
private:
/*--------------------------------------------------------------------*/
template<typename Pixel>
class file_input_buffer : public buffered_istream<std::istream>
{
private:
typedef Pixel pixel_type;
public:
file_input_buffer( std::istream& f );
rgba_pixel_8 get_pixel();
}; // class file_input_buffer
/*--------------------------------------------------------------------*/
template<typename Pixel>
class mapped_file_input_buffer:
public buffered_istream<std::istream>
{
private:
typedef Pixel pixel_type;
public:
mapped_file_input_buffer( std::istream& f, const color_palette32& p );
rgba_pixel_8 get_pixel();
private:
const color_palette32& m_palette;
}; // class mapped_file_input_buffer
/*--------------------------------------------------------------------*/
template< typename InputBuffer >
class rle_targa_output_buffer
{
private:
typedef rgba_pixel_8 pixel_type;
typedef InputBuffer input_buffer_type;
public:
rle_targa_output_buffer( image& img, bool up_down, bool left_right );
void fill( unsigned int n, rgba_pixel_8 pattern );
void copy( unsigned int n, input_buffer_type& buffer );
bool completed() const;
private:
void adjust_position(int x);
private:
image& m_image;
unsigned int m_x;
unsigned int m_y;
const int m_x_inc;
const int m_y_inc;
}; // class rle_targa_output_buffer
/*--------------------------------------------------------------------*/
template< typename InputBuffer,
typename OutputBuffer = rle_targa_output_buffer<InputBuffer> >
class rle_targa_decoder
: public rle_decoder< rgba_pixel_8, InputBuffer, OutputBuffer >
{
public:
typedef InputBuffer input_buffer_type;
typedef OutputBuffer output_buffer_type;
private:
virtual void
read_mode( input_buffer_type& input, output_buffer_type& output );
}; // class rle_targa_decoder
/*--------------------------------------------------------------------*/
typedef
rle_targa_decoder< file_input_buffer<rgba_pixel_8> > rle32_decoder;
/*--------------------------------------------------------------------*/
typedef
rle_targa_decoder< file_input_buffer<rgb_pixel_8> > rle24_decoder;
/*--------------------------------------------------------------------*/
typedef rle_targa_decoder< file_input_buffer<pixel16> > rle16_decoder;
/*--------------------------------------------------------------------*/
typedef rle_targa_decoder< mapped_file_input_buffer<pixel8> >
rle8_decoder;
public:
reader( image& img );
reader( image& img, std::istream& f );
void load( std::istream& f );
private:
void check_if_targa( std::istream& f ) const;
void load_palette
( const header& h, std::istream& f, color_palette32& palette ) const;
void load_color_mapped( const header& h, std::istream& f );
void load_rle_color_mapped( const header& h, std::istream& f );
void load_true_color( const header& h, std::istream& f );
void load_rle_true_color( const header& h, std::istream& f );
template<typename Pixel>
void load_color_mapped_raw
( const header& h, std::istream& f, const color_palette32& palette );
template<typename Decoder>
void decompress_rle_color_mapped
( const header& h, std::istream& f, const color_palette32& palette );
template<typename Pixel>
void load_true_color_raw( const header& h, std::istream& f );
template<typename Decoder>
void decompress_rle_true_color( const header& h, std::istream& f );
template<typename Pixel>
void
load_palette_content( std::istream& f, color_palette32& palette ) const;
private:
image& m_image;
}; // class reader
/*----------------------------------------------------------------------*/
class writer : private file_structure
{
public:
/*--------------------------------------------------------------------*/
template<typename Pixel>
class file_output_buffer
{
public:
typedef Pixel pixel_type;
typedef pixel_type 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;
void order_pixel_bytes( const pixel_type& p );
private:
std::ostream& m_stream;
}; // class file_output_buffer
/*--------------------------------------------------------------------*/
template<typename Pixel>
class rle_targa_encoder
: public rle_encoder< file_output_buffer<Pixel> >
{
public:
typedef file_output_buffer<Pixel> output_buffer_type;
}; // class rle_targa_encoder
/*--------------------------------------------------------------------*/
typedef rle_targa_encoder<rgba_pixel_8> rle32_encoder;
public:
writer( const image& img );
writer( const image& img, std::ostream& f, bool rle );
void save( std::ostream& f, bool rle ) const;
private:
void save_true_color( std::ostream& os ) const;
void save_rle_true_color( std::ostream& os ) const;
private:
const image& m_image;
}; // class writer
public:
targa( unsigned int w, unsigned int h );
targa( const image& that );
targa( std::istream& f );
void save( std::ostream& os, bool rle ) const;
}; // class targa
} // namespace graphic
} // namespace claw
#include <claw/impl/targa_writer.tpp>
#include <claw/impl/targa_reader.tpp>
#endif // __CLAW_TARGA_HPP__
claw::graphic::targa::save
void save(std::ostream &os, bool rle) const
Save the content of the image in a stream.
Definition: targa.cpp:72
claw::graphic::targa
A class for targa pictures.
Definition: targa.hpp:48
claw::graphic::targa::file_structure::header
Header of a targa file.
Definition: targa.hpp:76
claw::graphic::targa::file_structure::header::header
header()
Default constructor.
Definition: targa_file_structure.cpp:41
claw::graphic::targa::file_structure::header::color_map
char color_map
1 if there is a color map, 0 otherwise.
Definition: targa.hpp:86
claw::graphic::image::pixel_type
rgba_pixel pixel_type
The type representing the colors of the pixels in the image.
Definition: image.hpp:53
claw::graphic::targa::file_structure::header::entry_size
unsigned char entry_size
Number of bits per enrty.
Definition: targa.hpp:98
claw::graphic::targa::file_structure::developer_item::tag
unsigned short tag
Item identifier.
Definition: targa.hpp:133
claw::graphic::targa::file_structure::developer_item::size
unsigned int size
Fielf size.
Definition: targa.hpp:137
claw::graphic::targa::writer::rle_targa_encoder::output_buffer_type
file_output_buffer< Pixel > output_buffer_type
Type of the output buffer.
Definition: targa.hpp:469
claw::graphic::targa::file_structure::header::specification::width
unsigned short width
Image width.
Definition: targa.hpp:109
claw::graphic::targa::file_structure::header::first_entry_index
unsigned short first_entry_index
Index of the first color map entry.
Definition: targa.hpp:94
claw::graphic::targa::reader
This class read data from a targa file and store it in an image.
Definition: targa.hpp:216
claw::graphic::targa::writer::rle32_encoder
rle_targa_encoder< rgba_pixel_8 > rle32_encoder
RLE encoder for 32 bpp targa images.
Definition: targa.hpp:475
claw::graphic::rgba_pixel
RGBA pixel.
Definition: pixel.hpp:79
claw::graphic::targa::file_structure::header::specification::bpp
unsigned char bpp
Bits per pixel.
Definition: targa.hpp:113
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::targa::writer::rle_targa_encoder
RLE encoder for targa format.
Definition: targa.hpp:464
claw::graphic::targa::file_structure::header::specification
Image specification.
Definition: targa.hpp:102
claw::first
Fuction object to get the first element of a std::pair.
Definition: functional.hpp:44
claw::graphic::targa::file_structure::developer_item
Item in the developper directory.
Definition: targa.hpp:130
claw::graphic::targa::file_structure::header::id_length
char id_length
Image identifier length.
Definition: targa.hpp:84
claw::buffered_istream
This class is made to help reading istreams with a buffer.
Definition: buffered_istream.hpp:42
claw::graphic::targa::file_structure::extension
Extension area.
Definition: targa.hpp:145
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
rle_encoder.hpp
A class to help run-length encoding (RLE) streams.
claw::graphic::targa::file_structure::header::image_type
char image_type
Image type.
Definition: targa.hpp:88
claw::graphic::color_palette
A palette of colors, for palettized images.
Definition: color_palette.hpp:44
claw::graphic::targa::file_structure::header::specification::x_origin
unsigned short x_origin
Lower left corner X-origin.
Definition: targa.hpp:105
rle_decoder.hpp
A class to help decoding run-length encoded (RLE) streams.
claw::graphic::targa::file_structure::developer_item::offset
unsigned int offset
Offset in the file.
Definition: targa.hpp:135
claw::graphic::targa::writer::file_output_buffer::pattern_type
pixel_type pattern_type
The type of the patterns to encode.
Definition: targa.hpp:429
claw::graphic::targa::targa
targa(unsigned int w, unsigned int h)
Constructor. Creates an empty image.
Definition: targa.cpp:39
buffered_istream.hpp
This class is made to help reading istreams with a buffer.
claw::graphic::targa::writer::file_output_buffer::pixel_type
Pixel pixel_type
The type of the pixels in the input buffer.
Definition: targa.hpp:426
claw::graphic::targa::file_structure::header::specification::alpha
unsigned char alpha() const
Number of bits per pixel assigned to alpha chanel.
Definition: targa_file_structure.cpp:110
claw::graphic::targa::file_structure::header::color_map_specification
struct claw::graphic::targa::file_structure::header::@18 color_map_specification
Color map specification.
claw::graphic::targa::file_structure::header::length
unsigned short length
Total number of color map entries included.
Definition: targa.hpp:96
claw::graphic::targa::file_structure::header::specification::descriptor
unsigned char descriptor
descriptor.
Definition: targa.hpp:115
claw::graphic::image
A class to deal with images.
Definition: image.hpp:49
claw::graphic::targa::file_structure::header::specification::height
unsigned short height
Image height.
Definition: targa.hpp:111
claw::rle_encoder
A class to help run-length encoding (RLE) streams.
Definition: rle_encoder.hpp:58
claw::graphic::targa::writer
This class write an image in a targa file.
Definition: targa.hpp:410
claw
This is the main namespace.
Definition: algorithm.hpp:33
claw::graphic::targa::file_structure::header::image_specification
specification image_specification
The specification of the image.
Definition: targa.hpp:123
claw::graphic::targa::file_structure::header::specification::up_down_oriented
bool up_down_oriented() const
Is image stored up to down ?
Definition: targa_file_structure.cpp:89
claw::graphic::targa::file_structure::header::specification::y_origin
unsigned short y_origin
Lower left corner Y-origin.
Definition: targa.hpp:107
claw::graphic::targa::writer::file_output_buffer
The type of the output buffer associated with the file when encoding RLE data.
Definition: targa.hpp:422
image.hpp
A class to deal with images.
claw::graphic::targa::file_structure::header::specification::left_right_oriented
bool left_right_oriented() const
Is image stored left to right ?
Definition: targa_file_structure.cpp:100