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

claw::graphic::pcx::reader Class Reference

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

#include <pcx.hpp>

List of all members.

Classes

class  converter_16
 Function object that converts a scanline of a 4bpp color mapped pcx into 32 bpp pixels. More...
class  converter_256
 Function object that converts a scanline of a 8bpp color mapped pcx into 32 bpp pixels. More...
class  converter_mono
 Function object that converts a scanline of a monochrome pcx into 32 bpp pixels. More...
class  converter_true_color
 Function object that converts a scanline of a 3 planes true color scanline into 32 bpp pixels. More...
class  rle_pcx_decoder
 RLE decoder for pcx RLE format. More...
class  rle_pcx_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 pcx file.

Private Types

typedef buffered_istream
< std::istream > 
rle_pcx_input_buffer
 The type of the input buffer associated with the file when decoding RLE files.

Private Member Functions

void check_if_pcx (const header &h) const
 Check if an header seems valid for a pcx file.
void load_mono (const header &h, std::istream &f)
 Load the monochrome pcx content.
void load_16_color_mapped (const header &h, std::istream &f)
 Load the 16 bpp color mapped pcx content.
void load_true_color (const header &h, std::istream &f)
 Load the true color (24 bpp) pcx content.
void load_256_color_mapped (const header &h, std::istream &f)
 Load the 8 bpp color mapped pcx content.
void decompress_line (std::istream &f, color_plane_type &scanline) const
 Load the 8 bpp color mapped pcx content.
template<typename Converter >
void decompress (const header &h, std::istream &f, const Converter &convert)
 Decompress the scan lines and convert their contents into pixels in the image.

Private Attributes

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

Detailed Description

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

Author:
Julien Jorge

Definition at line 158 of file pcx.hpp.


Member Typedef Documentation

The type of the input buffer associated with the file when decoding RLE files.

Definition at line 166 of file pcx.hpp.


Constructor & Destructor Documentation

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

Constructor.

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

Definition at line 270 of file pcx_reader.cpp.

  : m_image( img )
{

} // pcx::reader::reader()

claw::graphic::pcx::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 283 of file pcx_reader.cpp.

References load().

  : m_image( img )
{
  load(f);
} // pcx::reader::reader()


Member Function Documentation

void claw::graphic::pcx::reader::check_if_pcx ( const header h  )  const [private]

Check if an header seems valid for a pcx file.

Parameters:
h The header read in the file.

Definition at line 361 of file pcx_reader.cpp.

References CLAW_EXCEPTION, and claw::graphic::pcx::header::manufacturer.

Referenced by load().

{
  if ( h.manufacturer != 0x0A )
    throw CLAW_EXCEPTION( "Not a Pcx file." );
} // pcx::reader::check_if_pcx()

template<typename Converter >
void claw::graphic::pcx::reader::decompress ( const header h,
std::istream &  f,
const Converter &  convert 
) [private]

Decompress the scan lines and convert their contents into pixels in the image.

Parameters:
h The header read in the file.
f The file from which we read.
convert Converter to use to convert the data into pixels.

Definition at line 41 of file pcx_reader.tpp.

References claw::graphic::pcx::header::bytes_per_line, and claw::graphic::pcx::header::color_planes.

Referenced by load_mono(), and load_true_color().

{
  std::vector<color_plane_type> scanline
    ( h.color_planes, color_plane_type(h.bytes_per_line) );

  for ( unsigned int y=0; y!=m_image.height(); ++y)
    {
      for (unsigned int i=0; i!=h.color_planes; ++i)
        decompress_line(f, scanline[i]);

      convert( scanline, m_image, y );
    }
} // pcx::reader::decompress()

void claw::graphic::pcx::reader::decompress_line ( std::istream &  f,
color_plane_type scanline 
) const [private]

Load the 8 bpp color mapped pcx content.

Parameters:
f The file from which we read.
scanline (out) Uncompressed scan line.

Definition at line 460 of file pcx_reader.cpp.

References claw::rle_decoder< Pattern, InputBuffer, OutputBuffer >::decode().

{
  rle_pcx_input_buffer input(f);
  rle_pcx_output_buffer output(scanline);

  rle_pcx_decoder decoder;

  decoder.decode( input, output );
} // pcx::reader::decompress_line()

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

Load an image from a pcx file.

Parameters:
f Pcx file.

Definition at line 294 of file pcx_reader.cpp.

References claw::graphic::pcx::header::bpp, check_if_pcx(), CLAW_PRECOND, claw::graphic::pcx::header::color_planes, load_16_color_mapped(), load_256_color_mapped(), load_mono(), load_true_color(), m_image, claw::graphic::image::set_size(), claw::graphic::pcx::header::window, claw::graphic::pcx::header::x_max, claw::graphic::pcx::header::x_min, claw::graphic::pcx::header::y_max, and claw::graphic::pcx::header::y_min.

Referenced by reader().

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

  try
    {
      header h;

      f.read( reinterpret_cast<char*>(&h), sizeof(header) );
      
      if ( f.rdstate() == std::ios_base::goodbit )
        {
          check_if_pcx(h);

          m_image.set_size( h.window.x_max - h.window.x_min + 1,
                            h.window.y_max - h.window.y_min + 1 );

          bool supported_format = true;

          switch(h.color_planes)
            {
            case 1:
              if (h.bpp == 1)
                load_mono(h, f);
              else if (h.bpp == 8)
                load_256_color_mapped(h, f);
              else
                supported_format = false;
              break;
            case 3:
              if (h.bpp == 8)
                load_true_color(h, f);
              else
                supported_format = false;
              break;
            case 4:
              if (h.bpp == 1)
                load_16_color_mapped(h, f);
              else
                supported_format = false;
              break;
            default :
              supported_format = false;
            }

          if ( supported_format == false )
            throw claw::bad_format
              ( "pcx::reader::pcx: unsupported image type" );
        }
      else
        throw claw::bad_format
          ( "claw::pcx::reader::pcx: can't read header" );
    }
  catch(...)
    {
      f.clear();
      f.seekg( init_pos, std::ios_base::beg );
      throw;
    }
} // pcx::reader::load()

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

Load the 16 bpp color mapped pcx content.

Parameters:
h The header read in the file.
f The file from which we read.

Definition at line 388 of file pcx_reader.cpp.

References claw::graphic::pcx::header::color_planes.

Referenced by load().

{
  assert( h.color_planes == 4 );

  converter_16 convert(h);
  decompress( h, f, convert );
} // pcx::reader::load_16_color_mapped()

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

Load the 8 bpp color mapped pcx content.

Parameters:
h The header read in the file.
f The file from which we read.

Definition at line 418 of file pcx_reader.cpp.

References CLAW_EXCEPTION, claw::graphic::pcx::header::color_planes, and claw::buffered_istream< Stream >::read().

Referenced by load().

{
  assert( h.color_planes == 1 );

  // 256 RGB triplets
  const unsigned int palette_length = 256 * 3;

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

  // -1 for the check byte
  f.seekg( -(std::istream::off_type)palette_length - 1, std::ios_base::end ); 

  char check;
  f.read(&check, 1);
  
  if ( check != 12 )
    throw CLAW_EXCEPTION( "PCX: The color palette is missing." );

  char buffer[palette_length];
  f.read(buffer, palette_length);

  for (unsigned int i=0, j=0; i!=palette_length; i+=3, ++j)
    {
      palette[j].components.alpha = 255;
      palette[j].components.red   = buffer[i];
      palette[j].components.green = buffer[i+1];
      palette[j].components.blue  = buffer[i+2];
    }

  f.seekg( init_pos );
  converter_256 convert(palette);
  decompress( h, f, convert );
} // pcx::reader::load_256_color_mapped()

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

Load the monochrome pcx content.

Parameters:
h The header read in the file.
f The file from which we read.

Definition at line 373 of file pcx_reader.cpp.

References claw::graphic::pcx::header::color_planes, and decompress().

Referenced by load().

{
  assert( h.color_planes == 1 );

  converter_mono convert;
  decompress( h, f, convert );
} // pcx::reader::load_mono()

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

Load the true color (24 bpp) pcx content.

Parameters:
h The header read in the file.
f The file from which we read.

Definition at line 403 of file pcx_reader.cpp.

References claw::graphic::pcx::header::color_planes, and decompress().

Referenced by load().

{
  assert( h.color_planes == 3 );

  converter_true_color convert;
  decompress( h, f, convert );
} // pcx::reader::load_true_color()


Member Data Documentation

The image in which we store the data we read.

Definition at line 294 of file pcx.hpp.

Referenced by load().


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