Classes | Public Member Functions | Private Types | Private Member Functions | Private Attributes

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:
imgThe image in which the data will be stored.

Definition at line 193 of file targa_reader.cpp.

  : m_image( img )
{

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

Constructor.

Parameters:
imgThe image in which the data will be stored.
fThe 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().

  : m_image( img )
{
  load(f);
} // 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:
fThe 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().

{
  CLAW_PRECOND( !!f );

  std::istream::pos_type init_pos = f.tellg();

  footer foot;

  f.seekg( -(std::istream::off_type)sizeof(footer), std::ios::end );
  f.read( reinterpret_cast<char*>(&foot), sizeof(footer) );
  f.seekg( init_pos , std::ios::beg );
  
  if ( !foot.is_valid() )
    throw CLAW_EXCEPTION( "Not a Targa file." );
} // 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 
) [private]

Load a RLE color mapped targa file.

Parameters:
hFile's header, must have been read before call.
fTarga file.
paletteThe 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().

{
  Decoder decoder;
  typename Decoder::output_buffer_type output_buffer
    (m_image, h.image_specification.up_down_oriented(),
     h.image_specification.left_right_oriented() );
  typename Decoder::input_buffer_type input_buffer(f, palette);
  
  decoder.decode(input_buffer, output_buffer);
} // targa::reader::decompress_rle_color_mapped()
template<typename Decoder >
void claw::graphic::targa::reader::decompress_rle_true_color ( const header h,
std::istream &  f 
) [private]

Load a true color RLE targa file.

Parameters:
hFile's header, must have been read before call.
fTarga 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().

{
  assert(!h.color_map);

  Decoder decoder;
  typename Decoder::output_buffer_type output_buffer
    (m_image, h.image_specification.up_down_oriented(),
     h.image_specification.left_right_oriented() );
  typename Decoder::input_buffer_type input_buffer(f);
  
  decoder.decode(input_buffer, output_buffer);
} // targa::reader::decompress_rle_true_color()
void claw::graphic::targa::reader::load ( std::istream &  f )

Load an image from a targa file.

Parameters:
fTarga file.

Definition at line 217 of file targa_reader.cpp.

References CLAW_PRECOND, claw::graphic::targa::file_structure::header::specification::height, claw::graphic::targa::file_structure::header::image_specification, claw::graphic::targa::file_structure::header::image_type, and claw::graphic::targa::file_structure::header::specification::width.

Referenced by reader().

{
  CLAW_PRECOND( !!f );
  std::istream::pos_type init_pos = f.tellg();

  try
    {
      check_if_targa(f);

      header h;

      f.read( reinterpret_cast<char*>(&h), sizeof(header) );
      
      if ( f.rdstate() == std::ios_base::goodbit )
        {
          m_image.set_size( h.image_specification.width,
                            h.image_specification.height );
          
          switch(h.image_type)
            {
            case color_mapped: load_color_mapped(h, f); break;
            case rle_color_mapped: load_rle_color_mapped(h, f); break;
            case true_color: load_true_color(h, f); break;
            case rle_true_color: load_rle_true_color(h, f); break;
            default :
              throw claw::bad_format
                ( "targa::reader::targa: unsupported image type" );
            }
        }
      else
        throw claw::bad_format
          ( "claw::targa::reader::targa: can't read header" );
    }
  catch(...)
    {
      f.clear();
      f.seekg( init_pos, std::ios_base::beg );
      throw;
    }
} // 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:
hFile's header, must have been read before call.
fTarga 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.

{
  assert(h.image_type == color_mapped);

  f.seekg( h.id_length, std::ios_base::cur );

  color_palette32 palette( h.color_map_specification.length );
  load_palette( h, f, palette );

  switch(h.image_specification.bpp)
    {
    case 8: load_color_mapped_raw<pixel8>(h, f, palette); break;
    default: 
      throw claw::bad_format
        ( "targa::reader::load_color_mapped: unsupported color depth" );
    }
} // 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 
) [private]

Load an uncompressed true color targa file.

Parameters:
hFile's header, must have been read before call.
fTarga file.
paletteThe 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().

{
  /* We use a part of the rle framework but there isn't any compressed data
     here. We only use the direct copy of the rle algorithm. */

  typedef mapped_file_input_buffer<Pixel> input_buffer_type;

  rle_targa_output_buffer<input_buffer_type> output
    ( m_image, h.image_specification.up_down_oriented(),
      h.image_specification.left_right_oriented() );
  input_buffer_type input(f, palette);
  
  for ( unsigned int i=0; i!=m_image.height(); ++i )
    output.copy( m_image.width(), input );
} // 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:
hFile's header, must have been read before call.
fTarga 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.

{
  assert((h.image_type == color_mapped) || (h.image_type == rle_color_mapped));

  switch( h.color_map_specification.entry_size )
    {
    case 16: load_palette_content<pixel16>(f, palette); break;
    case 24: load_palette_content<rgb_pixel_8>(f, palette); break;
    case 32: load_palette_content<rgba_pixel_8>(f, palette); break;
    default: 
      throw claw::bad_format
        ( "targa::reader::load_palette: unsupported entry size" );
    }
} // targa::reader::load_palette()
template<typename Pixel >
void claw::graphic::targa::reader::load_palette_content ( std::istream &  f,
color_palette32 palette 
) const [private]

Load the content of the color palette.

Parameters:
fTarga 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().

{
  file_input_buffer<Pixel> input(f);

  for (unsigned int i=0; i!=palette.size(); ++i)
    palette[i] = input.get_pixel();
} // 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:
hFile's header, must have been read before call.
fTarga 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.

{
  assert(h.image_type == rle_color_mapped);

  f.seekg( h.id_length, std::ios_base::cur );

  color_palette32 palette( h.color_map_specification.length );
  load_palette( h, f, palette );

  switch(h.image_specification.bpp)
    {
    case 8: decompress_rle_color_mapped<rle8_decoder>(h, f, palette); break;
    default: 
      throw claw::bad_format
        ( "targa::reader::load_rle_color_mapped: unsupported color depth" );
    }
} // 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:
hFile's header, must have been read before call.
fTarga 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.

{
  assert(h.image_type == rle_true_color);

  f.seekg( h.id_length, std::ios_base::cur );

  switch(h.image_specification.bpp)
    {
    case 16 : decompress_rle_true_color<rle16_decoder>(h, f); break;
    case 24 : decompress_rle_true_color<rle24_decoder>(h, f); break;
    case 32 : decompress_rle_true_color<rle32_decoder>(h, f); break;
    default : 
      throw claw::bad_format
        ( "targa::reader::load_rle_true_color: unsupported color depth" );
    }
} // 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:
hFile's header, must have been read before call.
fTarga 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.

{
  assert(h.image_type == true_color);

  f.seekg( h.id_length, std::ios_base::cur );

  switch(h.image_specification.bpp)
    {
    case 16 : load_true_color_raw<pixel16>(h, f); break;
    case 24 : load_true_color_raw<rgb_pixel_8>(h, f); break;
    case 32 : load_true_color_raw<rgba_pixel_8>(h, f); break;
    default : 
      throw claw::bad_format
        ( "targa::reader::load_true_color: unsupported color depth" );
    }
} // targa::reader::load_true_color()
template<typename Pixel >
void claw::graphic::targa::reader::load_true_color_raw ( const header h,
std::istream &  f 
) [private]

Load an uncompressed true color targa file.

Parameters:
hFile's header, must have been read before call.
fTarga 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().

{
  assert(!h.color_map);

  /* We use a part of the rle framework but there isn't any compressed data
     here. We only use the direct copy of the rle algorithm. */

  typedef file_input_buffer<Pixel> input_buffer_type;

  rle_targa_output_buffer<input_buffer_type> output
    ( m_image, h.image_specification.up_down_oriented(),
      h.image_specification.left_right_oriented() );
  input_buffer_type input(f);
  
  for ( unsigned int i=0; i!=m_image.height(); ++i )
    output.copy( m_image.width(), input );
} // targa::reader::load_true_color_raw()

Member Data Documentation


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