claw::graphic::targa::reader Class Reference

This class read data from a targa file and store it in an image. More...

#include <targa.hpp>

Inheritance diagram for claw::graphic::targa::reader:
claw::graphic::targa::file_structure

List of all members.

Classes

class  file_input_buffer
 The type of the input buffer associated with the file when decoding RLE files. More...
class  mapped_file_input_buffer
 The type of the input buffer associated with the file when decoding RLE files using a color palette. More...
class  rle_targa_decoder
 RLE decoder for targa RLE format. More...
class  rle_targa_output_buffer
 The output buffer for the RLE decoder. More...

Public Member Functions

 reader (image &img)
 Constructor.
 reader (image &img, std::istream &f)
 Constructor.
void load (std::istream &f)
 Load an image from a targa file.

Private Types

typedef rle_targa_decoder
< file_input_buffer
< rgba_pixel_8 > > 
rle32_decoder
 RLE decoder for 32 bpp targa images.
typedef rle_targa_decoder
< file_input_buffer
< rgb_pixel_8 > > 
rle24_decoder
 RLE decoder for 24 bpp targa images.
typedef rle_targa_decoder
< file_input_buffer< pixel16 > > 
rle16_decoder
 RLE decoder for 16 bpp targa images.
typedef rle_targa_decoder
< mapped_file_input_buffer
< pixel8 > > 
rle8_decoder
 RLE decoder for color mapped 8 bpp targa images.

Private Member Functions

void check_if_targa (std::istream &f) const
 Check if a stream contains targa data.
void load_palette (const header &h, std::istream &f, color_palette32 &palette) const
 Load the color palette.
void load_color_mapped (const header &h, std::istream &f)
 Load a not compressed color mapped targa file.
void load_rle_color_mapped (const header &h, std::istream &f)
 Load a RLE color mapped targa file.
void load_true_color (const header &h, std::istream &f)
 Load a not compressed true color targa file.
void load_rle_true_color (const header &h, std::istream &f)
 Load a RLE true color targa file.
template<typename Pixel >
void load_color_mapped_raw (const header &h, std::istream &f, const color_palette32 &palette)
 Load an uncompressed true color targa file.
template<typename Decoder >
void decompress_rle_color_mapped (const header &h, std::istream &f, const color_palette32 &palette)
 Load a RLE color mapped targa file.
template<typename Pixel >
void load_true_color_raw (const header &h, std::istream &f)
 Load an uncompressed true color targa file.
template<typename Decoder >
void decompress_rle_true_color (const header &h, std::istream &f)
 Load a true color RLE targa file.
template<typename Pixel >
void load_palette_content (std::istream &f, color_palette32 &palette) const
 Load the content of the color palette.

Private Attributes

imagem_image
 The image in which we store the data we read.

Detailed Description

This class read data from a targa file and store it in an image.

Author:
Julien Jorge

Definition at line 213 of file targa.hpp.


Member Typedef Documentation

RLE decoder for 16 bpp targa images.

Definition at line 354 of file targa.hpp.

RLE decoder for 24 bpp targa images.

Definition at line 350 of file targa.hpp.

RLE decoder for 32 bpp targa images.

Definition at line 345 of file targa.hpp.

RLE decoder for color mapped 8 bpp targa images.

Definition at line 359 of file targa.hpp.


Constructor & Destructor Documentation

claw::graphic::targa::reader::reader ( image img  ) 

Constructor.

Parameters:
img The image in which the data will be stored.

Definition at line 193 of file targa_reader.cpp.

00194   : m_image( img )
00195 {
00196 
00197 } // targa::reader::reader()

claw::graphic::targa::reader::reader ( image img,
std::istream &  f 
)

Constructor.

Parameters:
img The image in which the data will be stored.
f The file from which we read the data.
Postcondition:
img contains the data from f.

Definition at line 206 of file targa_reader.cpp.

References load().

00207   : m_image( img )
00208 {
00209   load(f);
00210 } // targa::reader::reader()


Member Function Documentation

void claw::graphic::targa::reader::check_if_targa ( std::istream &  f  )  const [private]

Check if a stream contains targa data.

Parameters:
f The stream to check.

Definition at line 263 of file targa_reader.cpp.

References CLAW_EXCEPTION, CLAW_PRECOND, and claw::graphic::targa::file_structure::footer::is_valid().

Referenced by load().

00264 {
00265   CLAW_PRECOND( !!f );
00266 
00267   std::istream::pos_type init_pos = f.tellg();
00268 
00269   footer foot;
00270 
00271   f.seekg( -(std::istream::off_type)sizeof(footer), std::ios::end );
00272   f.read( reinterpret_cast<char*>(&foot), sizeof(footer) );
00273   f.seekg( init_pos , std::ios::beg );
00274   
00275   if ( !foot.is_valid() )
00276     throw CLAW_EXCEPTION( "Not a Targa file." );
00277 } // targa::reader::check_if_targa()

template<typename Decoder >
void claw::graphic::targa::reader::decompress_rle_color_mapped ( const header h,
std::istream &  f,
const color_palette32 palette 
) [inline, private]

Load a RLE color mapped targa file.

Parameters:
h File's header, must have been read before call.
f Targa file.
palette The color palette of the image.
Precondition:
f.is_open()

Definition at line 264 of file targa_reader.tpp.

References claw::graphic::targa::file_structure::header::image_specification, claw::graphic::targa::file_structure::header::specification::left_right_oriented(), m_image, and claw::graphic::targa::file_structure::header::specification::up_down_oriented().

00265 {
00266   Decoder decoder;
00267   typename Decoder::output_buffer_type output_buffer
00268     (m_image, h.image_specification.up_down_oriented(),
00269      h.image_specification.left_right_oriented() );
00270   typename Decoder::input_buffer_type input_buffer(f, palette);
00271   
00272   decoder.decode(input_buffer, output_buffer);
00273 } // targa::reader::decompress_rle_color_mapped()

template<typename Decoder >
void claw::graphic::targa::reader::decompress_rle_true_color ( const header h,
std::istream &  f 
) [inline, private]

Load a true color RLE targa file.

Parameters:
h File's header, must have been read before call.
f Targa file.
Precondition:
f.is_open() && !h.color_map

Definition at line 311 of file targa_reader.tpp.

References claw::graphic::targa::file_structure::header::color_map, claw::graphic::targa::file_structure::header::image_specification, claw::graphic::targa::file_structure::header::specification::left_right_oriented(), m_image, and claw::graphic::targa::file_structure::header::specification::up_down_oriented().

00312 {
00313   assert(!h.color_map);
00314 
00315   Decoder decoder;
00316   typename Decoder::output_buffer_type output_buffer
00317     (m_image, h.image_specification.up_down_oriented(),
00318      h.image_specification.left_right_oriented() );
00319   typename Decoder::input_buffer_type input_buffer(f);
00320   
00321   decoder.decode(input_buffer, output_buffer);
00322 } // targa::reader::decompress_rle_true_color()

void claw::graphic::targa::reader::load ( std::istream &  f  ) 

Load an image from a targa file.

Parameters:
f Targa file.

Definition at line 217 of file targa_reader.cpp.

References check_if_targa(), CLAW_PRECOND, claw::graphic::targa::file_structure::color_mapped, claw::graphic::targa::file_structure::header::specification::height, claw::graphic::targa::file_structure::header::image_specification, claw::graphic::targa::file_structure::header::image_type, load_color_mapped(), load_rle_color_mapped(), load_rle_true_color(), load_true_color(), m_image, claw::graphic::targa::file_structure::rle_color_mapped, claw::graphic::targa::file_structure::rle_true_color, claw::graphic::image::set_size(), claw::graphic::targa::file_structure::true_color, and claw::graphic::targa::file_structure::header::specification::width.

Referenced by reader().

00218 {
00219   CLAW_PRECOND( !!f );
00220   std::istream::pos_type init_pos = f.tellg();
00221 
00222   try
00223     {
00224       check_if_targa(f);
00225 
00226       header h;
00227 
00228       f.read( reinterpret_cast<char*>(&h), sizeof(header) );
00229       
00230       if ( f.rdstate() == std::ios_base::goodbit )
00231         {
00232           m_image.set_size( h.image_specification.width,
00233                             h.image_specification.height );
00234           
00235           switch(h.image_type)
00236             {
00237             case color_mapped: load_color_mapped(h, f); break;
00238             case rle_color_mapped: load_rle_color_mapped(h, f); break;
00239             case true_color: load_true_color(h, f); break;
00240             case rle_true_color: load_rle_true_color(h, f); break;
00241             default :
00242               throw claw::bad_format
00243                 ( "targa::reader::targa: unsupported image type" );
00244             }
00245         }
00246       else
00247         throw claw::bad_format
00248           ( "claw::targa::reader::targa: can't read header" );
00249     }
00250   catch(...)
00251     {
00252       f.clear();
00253       f.seekg( init_pos, std::ios_base::beg );
00254       throw;
00255     }
00256 } // targa::reader::load()

void claw::graphic::targa::reader::load_color_mapped ( const header h,
std::istream &  f 
) [private]

Load a not compressed color mapped targa file.

Parameters:
h File's header, must have been read before call.
f Targa file.
Precondition:
h.image_type == color_mapped

Definition at line 311 of file targa_reader.cpp.

References claw::graphic::targa::file_structure::header::specification::bpp, claw::graphic::targa::file_structure::header::color_map_specification, claw::graphic::targa::file_structure::header::id_length, claw::graphic::targa::file_structure::header::image_specification, claw::graphic::targa::file_structure::header::image_type, and claw::graphic::targa::file_structure::header::length.

Referenced by load().

00312 {
00313   assert(h.image_type == color_mapped);
00314 
00315   f.seekg( h.id_length, std::ios_base::cur );
00316 
00317   color_palette32 palette( h.color_map_specification.length );
00318   load_palette( h, f, palette );
00319 
00320   switch(h.image_specification.bpp)
00321     {
00322     case 8: load_color_mapped_raw<pixel8>(h, f, palette); break;
00323     default: 
00324       throw claw::bad_format
00325         ( "targa::reader::load_color_mapped: unsupported color depth" );
00326     }
00327 } // targa::reader::load_color_mapped()

template<typename Pixel >
void claw::graphic::targa::reader::load_color_mapped_raw ( const header h,
std::istream &  f,
const color_palette32 palette 
) [inline, private]

Load an uncompressed true color targa file.

Parameters:
h File's header, must have been read before call.
f Targa file.
palette The color palette of the image.
Precondition:
f.is_open()

Definition at line 238 of file targa_reader.tpp.

References claw::graphic::image::height(), claw::graphic::targa::file_structure::header::image_specification, claw::graphic::targa::file_structure::header::specification::left_right_oriented(), m_image, claw::graphic::targa::file_structure::header::specification::up_down_oriented(), and claw::graphic::image::width().

00239 {
00240   /* We use a part of the rle framework but there isn't any compressed data
00241      here. We only use the direct copy of the rle algorithm. */
00242 
00243   typedef mapped_file_input_buffer<Pixel> input_buffer_type;
00244 
00245   rle_targa_output_buffer<input_buffer_type> output
00246     ( m_image, h.image_specification.up_down_oriented(),
00247       h.image_specification.left_right_oriented() );
00248   input_buffer_type input(f, palette);
00249   
00250   for ( unsigned int i=0; i!=m_image.height(); ++i )
00251     output.copy( m_image.width(), input );
00252 } // targa::reader::load_true_color_raw()

void claw::graphic::targa::reader::load_palette ( const header h,
std::istream &  f,
color_palette32 palette 
) const [private]

Load the color palette.

Parameters:
h File's header, must have been read before call.
f Targa file.
palette (out) The color palette.
Precondition:
(h.image_type == color_mapped) || (h.image_type == rle_color_mapped)

Definition at line 288 of file targa_reader.cpp.

References claw::graphic::targa::file_structure::header::color_map_specification, claw::graphic::targa::file_structure::header::entry_size, and claw::graphic::targa::file_structure::header::image_type.

00289 {
00290   assert((h.image_type == color_mapped) || (h.image_type == rle_color_mapped));
00291 
00292   switch( h.color_map_specification.entry_size )
00293     {
00294     case 16: load_palette_content<pixel16>(f, palette); break;
00295     case 24: load_palette_content<rgb_pixel_8>(f, palette); break;
00296     case 32: load_palette_content<rgba_pixel_8>(f, palette); break;
00297     default: 
00298       throw claw::bad_format
00299         ( "targa::reader::load_palette: unsupported entry size" );
00300     }
00301 } // targa::reader::load_palette()

template<typename Pixel >
void claw::graphic::targa::reader::load_palette_content ( std::istream &  f,
color_palette32 palette 
) const [inline, private]

Load the content of the color palette.

Parameters:
f Targa file.
palette (out) The color palette.

Definition at line 332 of file targa_reader.tpp.

References claw::graphic::targa::reader::file_input_buffer< Pixel >::get_pixel(), and claw::graphic::color_palette< Color >::size().

00333 {
00334   file_input_buffer<Pixel> input(f);
00335 
00336   for (unsigned int i=0; i!=palette.size(); ++i)
00337     palette[i] = input.get_pixel();
00338 } // targa::reader::load_palette_content()

void claw::graphic::targa::reader::load_rle_color_mapped ( const header h,
std::istream &  f 
) [private]

Load a RLE color mapped targa file.

Parameters:
h File's header, must have been read before call.
f Targa file.
Precondition:
h.image_type == color_mapped

Definition at line 337 of file targa_reader.cpp.

References claw::graphic::targa::file_structure::header::specification::bpp, claw::graphic::targa::file_structure::header::color_map_specification, claw::graphic::targa::file_structure::header::id_length, claw::graphic::targa::file_structure::header::image_specification, claw::graphic::targa::file_structure::header::image_type, and claw::graphic::targa::file_structure::header::length.

Referenced by load().

00338 {
00339   assert(h.image_type == rle_color_mapped);
00340 
00341   f.seekg( h.id_length, std::ios_base::cur );
00342 
00343   color_palette32 palette( h.color_map_specification.length );
00344   load_palette( h, f, palette );
00345 
00346   switch(h.image_specification.bpp)
00347     {
00348     case 8: decompress_rle_color_mapped<rle8_decoder>(h, f, palette); break;
00349     default: 
00350       throw claw::bad_format
00351         ( "targa::reader::load_rle_color_mapped: unsupported color depth" );
00352     }
00353 } // targa::reader::load_color_mapped()

void claw::graphic::targa::reader::load_rle_true_color ( const header h,
std::istream &  f 
) [private]

Load a RLE true color targa file.

Parameters:
h File's header, must have been read before call.
f Targa file.
Precondition:
h.image_type == rle_true_color

Definition at line 388 of file targa_reader.cpp.

References claw::graphic::targa::file_structure::header::specification::bpp, claw::graphic::targa::file_structure::header::id_length, claw::graphic::targa::file_structure::header::image_specification, and claw::graphic::targa::file_structure::header::image_type.

Referenced by load().

00389 {
00390   assert(h.image_type == rle_true_color);
00391 
00392   f.seekg( h.id_length, std::ios_base::cur );
00393 
00394   switch(h.image_specification.bpp)
00395     {
00396     case 16 : decompress_rle_true_color<rle16_decoder>(h, f); break;
00397     case 24 : decompress_rle_true_color<rle24_decoder>(h, f); break;
00398     case 32 : decompress_rle_true_color<rle32_decoder>(h, f); break;
00399     default : 
00400       throw claw::bad_format
00401         ( "targa::reader::load_rle_true_color: unsupported color depth" );
00402     }
00403 } // targa::reader::load_rle_true_color()

void claw::graphic::targa::reader::load_true_color ( const header h,
std::istream &  f 
) [private]

Load a not compressed true color targa file.

Parameters:
h File's header, must have been read before call.
f Targa file.
Precondition:
h.image_type == true_color

Definition at line 363 of file targa_reader.cpp.

References claw::graphic::targa::file_structure::header::specification::bpp, claw::graphic::targa::file_structure::header::id_length, claw::graphic::targa::file_structure::header::image_specification, and claw::graphic::targa::file_structure::header::image_type.

Referenced by load().

00364 {
00365   assert(h.image_type == true_color);
00366 
00367   f.seekg( h.id_length, std::ios_base::cur );
00368 
00369   switch(h.image_specification.bpp)
00370     {
00371     case 16 : load_true_color_raw<pixel16>(h, f); break;
00372     case 24 : load_true_color_raw<rgb_pixel_8>(h, f); break;
00373     case 32 : load_true_color_raw<rgba_pixel_8>(h, f); break;
00374     default : 
00375       throw claw::bad_format
00376         ( "targa::reader::load_true_color: unsupported color depth" );
00377     }
00378 } // targa::reader::load_true_color()

template<typename Pixel >
void claw::graphic::targa::reader::load_true_color_raw ( const header h,
std::istream &  f 
) [inline, private]

Load an uncompressed true color targa file.

Parameters:
h File's header, must have been read before call.
f Targa file.
Precondition:
f.is_open() && !h.color_map

Definition at line 284 of file targa_reader.tpp.

References claw::graphic::targa::file_structure::header::color_map, claw::graphic::image::height(), claw::graphic::targa::file_structure::header::image_specification, claw::graphic::targa::file_structure::header::specification::left_right_oriented(), m_image, claw::graphic::targa::file_structure::header::specification::up_down_oriented(), and claw::graphic::image::width().

00285 {
00286   assert(!h.color_map);
00287 
00288   /* We use a part of the rle framework but there isn't any compressed data
00289      here. We only use the direct copy of the rle algorithm. */
00290 
00291   typedef file_input_buffer<Pixel> input_buffer_type;
00292 
00293   rle_targa_output_buffer<input_buffer_type> output
00294     ( m_image, h.image_specification.up_down_oriented(),
00295       h.image_specification.left_right_oriented() );
00296   input_buffer_type input(f);
00297   
00298   for ( unsigned int i=0; i!=m_image.height(); ++i )
00299     output.copy( m_image.width(), input );
00300 } // targa::reader::load_true_color_raw()


Member Data Documentation


The documentation for this class was generated from the following files:

Generated on 9 Nov 2009 for CLAW Library (a C++ Library Absolutely Wonderful) by  doxygen 1.6.1