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

#include <targa.hpp>

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

claw::graphic::targa::file_structure

List of all members.


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.


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< pixel32 > > 
rle32_decoder
 RLE decoder for 32 bpp targa images.
typedef rle_targa_decoder
< file_input_buffer< pixel24 > > 
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.

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...

Member Typedef Documentation

RLE decoder for 32 bpp targa images.

Definition at line 344 of file targa.hpp.

RLE decoder for 24 bpp targa images.

Definition at line 348 of file targa.hpp.

RLE decoder for 16 bpp targa images.

Definition at line 352 of file targa.hpp.

RLE decoder for color mapped 8 bpp targa images.

Definition at line 357 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 192 of file targa_reader.cpp.

00193   : m_image( img )
00194 {
00195 
00196 } // 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 205 of file targa_reader.cpp.

References load().

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


Member Function Documentation

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

Load an image from a targa file.

Parameters:
f Targa file.

Definition at line 216 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().

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

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 262 of file targa_reader.cpp.

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

Referenced by load().

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

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 287 of file targa_reader.cpp.

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

Referenced by load_color_mapped(), and load_rle_color_mapped().

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

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 310 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::color_mapped, claw::graphic::targa::file_structure::header::id_length, claw::graphic::targa::file_structure::header::image_specification, claw::graphic::targa::file_structure::header::image_type, claw::graphic::targa::file_structure::header::length, and load_palette().

Referenced by load().

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

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 336 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, claw::graphic::targa::file_structure::header::length, load_palette(), and claw::graphic::targa::file_structure::rle_color_mapped.

Referenced by load().

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

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 362 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, claw::graphic::targa::file_structure::header::image_type, and claw::graphic::targa::file_structure::true_color.

Referenced by load().

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

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 387 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, claw::graphic::targa::file_structure::header::image_type, and claw::graphic::targa::file_structure::rle_true_color.

Referenced by load().

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

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()

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 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()

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()

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()


Member Data Documentation

The image in which we store the data we read.

Definition at line 396 of file targa.hpp.

Referenced by decompress_rle_color_mapped(), decompress_rle_true_color(), load(), load_color_mapped_raw(), and load_true_color_raw().


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

Generated on Thu May 22 21:07:39 2008 for CLAW Library (a C++ Library Absolutely Wonderful) by  doxygen 1.5.5