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

claw::graphic::bitmap::reader Class Reference

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

#include <bitmap.hpp>

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

List of all members.

Classes

class  pixel1_to_pixel32
 Functor converting a 1bpp buffer to a 32bpp buffer. More...
class  pixel24_to_pixel32
 Functor converting a 24bpp buffer to a 32bpp buffer. More...
class  pixel4_to_pixel32
 Functor converting a 4bpp buffer to a 32bpp buffer. More...
class  pixel8_to_pixel32
 Functor converting a 8bpp buffer to a 32bpp buffer. More...
class  rle_bitmap_decoder
 RLE decoder for bitmap RLE format. More...
class  rle_bitmap_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 the image data from a stream.

Private Types

typedef buffered_istream
< std::istream > 
file_input_buffer
 The type of the input buffer associated with the file when decoding RLE files.
typedef rle_bitmap_decoder
< rle_bitmap_output_buffer
< true > > 
rle4_decoder
 RLE decoder for 4 bpp bitmap images.
typedef rle_bitmap_decoder
< rle_bitmap_output_buffer
< false > > 
rle8_decoder
 RLE decoder for 8 bpp bitmap images.

Private Member Functions

void load_palette (const header &h, std::istream &f, color_palette_type &palette) const
 Load the palette of the image.
void load_1bpp (const header &h, std::istream &f)
 Load a monochrome bitmap file.
void load_4bpp (const header &h, std::istream &f)
 Loads a 4 bpp bitmap file.
void load_8bpp (const header &h, std::istream &f)
 Loads a 8 bpp bitmap file.
void load_24bpp (const header &h, std::istream &f)
 Loads a 24 bpp bitmap file.
void load_4bpp_rle (const header &h, std::istream &f, const color_palette_type &palette)
 Loads a 4 bpp RLE encoded bitmap file.
void load_4bpp_rgb (const header &h, std::istream &f, const color_palette_type &palette)
 Loads a 4 bpp RGB encoded bitmap file.
void load_8bpp_rle (const header &h, std::istream &f, const color_palette_type &palette)
 Loads a 8 bpp bitmap file.
void load_8bpp_rgb (const header &h, std::istream &f, const color_palette_type &palette)
 Loads a 8 bpp RGB encoded bitmap file.
template<typename Convert >
void load_rgb_data (std::istream &f, unsigned int buffer_size, const color_palette_type &palette, const Convert &pixel_convert)
 Load uncompressed data from the file.

Private Attributes

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

Detailed Description

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

Author:
Julien Jorge

Definition at line 134 of file bitmap.hpp.


Member Typedef Documentation

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

Definition at line 139 of file bitmap.hpp.

RLE decoder for 4 bpp bitmap images.

Definition at line 210 of file bitmap.hpp.

RLE decoder for 8 bpp bitmap images.

Definition at line 214 of file bitmap.hpp.


Constructor & Destructor Documentation

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

Constructor.

Parameters:
imgThe image in which the data will be stored.

Definition at line 287 of file bitmap_reader.cpp.

  : m_image( img )
{

} // bitmap::reader::reader()
claw::graphic::bitmap::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 300 of file bitmap_reader.cpp.

References load().

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

Member Function Documentation

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

Load the image data from a stream.

Parameters:
fThe file from which we read the data.
Postcondition:
The image passed to the constructor contains the data from f.

Definition at line 312 of file bitmap_reader.cpp.

References claw::graphic::bitmap::file_structure::header::bpp, CLAW_PRECOND, claw::graphic::bitmap::file_structure::header::height, claw::graphic::bitmap::file_structure::header::id, and claw::graphic::bitmap::file_structure::header::width.

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 ( (h.id[0] == 'B') && (h.id[1] == 'M')
           && (f.rdstate() == std::ios_base::goodbit) )
        {
          m_image.set_size(h.width, h.height);
      
          switch(h.bpp)
            {
            case 1 : load_1bpp(h, f); break;
            case 4 : load_4bpp(h, f); break;
            case 8 : load_8bpp(h, f); break;
              //case 16 : load_16bpp(h, f); break;
            case 24 : load_24bpp(h, f); break;
            default : 
              throw claw::bad_format
                ("bitmap::bitmap: unsupported color depth.");
            }
        }
      else
        throw claw::bad_format( "bitmap::bitmap: invalid header." );
    }
  catch(...)
    {
      f.clear();
      f.seekg( init_pos, std::ios_base::beg );
      throw;
    }
} // bitmap::reader::load()
void claw::graphic::bitmap::reader::load_1bpp ( const header h,
std::istream &  f 
) [private]

Load a monochrome bitmap file.

Parameters:
hFile's header, must have been read before call.
fBitmap file.
Precondition:
h.bpp == 1

Definition at line 396 of file bitmap_reader.cpp.

References claw::graphic::bitmap::file_structure::header::bpp, and claw::graphic::bitmap::file_structure::header::data_offset.

{
  assert(h.bpp == 1);
  //assert(h.compression == BMP_COMPRESSION_BITFIELDS);

  color_palette_type palette(2);
  unsigned int buffer_size = m_image.width() / (sizeof(char) * 8);
        
  if ( m_image.width() % (sizeof(char) * 8) )
    ++buffer_size;
    
  load_palette(h, f, palette);
  f.seekg(h.data_offset);

  load_rgb_data(f, buffer_size, palette, pixel1_to_pixel32());
} // bitmap::reader::load_1bpp()
void claw::graphic::bitmap::reader::load_24bpp ( const header h,
std::istream &  f 
) [private]

Loads a 24 bpp bitmap file.

Parameters:
hFile's header, must have been read before call.
fBitmap file.
Precondition:
(h.bpp == 24)

Definition at line 467 of file bitmap_reader.cpp.

References claw::graphic::bitmap::file_structure::header::bpp, and claw::graphic::bitmap::file_structure::header::data_offset.

{
  assert(h.bpp == 24);

  unsigned int buffer_size = m_image.width() * 3;
  color_palette_type palette(0);
        
  f.seekg(h.data_offset);

  load_rgb_data(f, buffer_size, palette, pixel24_to_pixel32());
} // bitmap::reader::load_24bpp()
void claw::graphic::bitmap::reader::load_4bpp ( const header h,
std::istream &  f 
) [private]

Loads a 4 bpp bitmap file.

Parameters:
hFile's header, must have been read before call.
fBitmap file.
Precondition:
(h.bpp == 4)

Definition at line 421 of file bitmap_reader.cpp.

References claw::graphic::bitmap::file_structure::header::bpp, and claw::graphic::bitmap::file_structure::header::compression.

{
  assert(h.bpp == 4);
  assert( (h.compression == BMP_COMPRESSION_RGB)
          || (h.compression == BMP_COMPRESSION_RLE4) );

  color_palette_type palette(16);
  load_palette(h, f, palette);

  if (h.compression == BMP_COMPRESSION_RLE4)
    load_4bpp_rle(h, f, palette);
  else
    load_4bpp_rgb(h, f, palette);
} // bitmap::reader::load_4bpp()
void claw::graphic::bitmap::reader::load_4bpp_rgb ( const header h,
std::istream &  f,
const color_palette_type palette 
) [private]

Loads a 4 bpp RGB encoded bitmap file.

Parameters:
hFile's header and palette, must have been read before call.
fBitmap file.
paletteThe color palette to use for converting colors.
Precondition:
(h.bpp == 4) && (h.compression = BMP_COMPRESSION_RGB) && (palette.size() == 16)

Definition at line 514 of file bitmap_reader.cpp.

References claw::graphic::bitmap::file_structure::header::bpp, claw::graphic::bitmap::file_structure::header::compression, claw::graphic::bitmap::file_structure::header::data_offset, and claw::graphic::color_palette< Color >::size().

{
  assert(h.bpp == 4);
  assert(h.compression == BMP_COMPRESSION_RGB);
  assert(palette.size() == 16);

  unsigned int buffer_size = m_image.width() / 2 + m_image.width() % 2;
    
  f.seekg(h.data_offset);

  load_rgb_data(f, buffer_size, palette, pixel4_to_pixel32());
} // bitmap::reader::load_4bpp_rgb()
void claw::graphic::bitmap::reader::load_4bpp_rle ( const header h,
std::istream &  f,
const color_palette_type palette 
) [private]

Loads a 4 bpp RLE encoded bitmap file.

Parameters:
hFile's header and palette, must have been read before call.
fBitmap file.
paletteThe color palette to use for converting colors.
Precondition:
(h.bpp == 4) && (h.compression = BMP_COMPRESSION_RLE4) && (palette.size() == 16)

Definition at line 489 of file bitmap_reader.cpp.

References claw::graphic::bitmap::file_structure::header::bpp, claw::graphic::bitmap::file_structure::header::compression, claw::graphic::bitmap::file_structure::header::data_offset, claw::rle_decoder< Pattern, InputBuffer, OutputBuffer >::decode(), and claw::graphic::color_palette< Color >::size().

{
  assert(h.bpp == 4);
  assert(h.compression == BMP_COMPRESSION_RLE4);
  assert(palette.size() == 16);

  f.seekg(h.data_offset);

  rle4_decoder decoder;
  rle4_decoder::output_buffer_type output_buffer( palette, m_image );
  file_input_buffer input_buffer(f);

  decoder.decode( input_buffer, output_buffer );
} // bitmap::reader::load_4bpp_rle()
void claw::graphic::bitmap::reader::load_8bpp ( const header h,
std::istream &  f 
) [private]

Loads a 8 bpp bitmap file.

Parameters:
hFile's header, must have been read before call.
fBitmap file.
Precondition:
(h.bpp == 8)

Definition at line 444 of file bitmap_reader.cpp.

References claw::graphic::bitmap::file_structure::header::bpp, and claw::graphic::bitmap::file_structure::header::compression.

{
  assert(h.bpp == 8);
  assert( (h.compression == BMP_COMPRESSION_RGB)
          || (h.compression == BMP_COMPRESSION_RLE8) );

  color_palette_type palette(256);
  load_palette(h, f, palette);

  if (h.compression == BMP_COMPRESSION_RLE8)
    load_8bpp_rle(h, f, palette);
  else
    load_8bpp_rgb(h, f, palette);
} // bitmap::reader::load_8bpp()
void claw::graphic::bitmap::reader::load_8bpp_rgb ( const header h,
std::istream &  f,
const color_palette_type palette 
) [private]

Loads a 8 bpp RGB encoded bitmap file.

Parameters:
hFile's header and palette, must have been read before call.
fBitmap file.
paletteThe color palette to use for converting colors.
Precondition:
(h.bpp == 8) && (h.compression = BMP_COMPRESSION_RGB) && (palette.size() == 256)

Definition at line 562 of file bitmap_reader.cpp.

References claw::graphic::bitmap::file_structure::header::bpp, claw::graphic::bitmap::file_structure::header::compression, claw::graphic::bitmap::file_structure::header::data_offset, and claw::graphic::color_palette< Color >::size().

{
  assert(h.bpp == 8);
  assert(h.compression == BMP_COMPRESSION_RGB);
  assert(palette.size() == 256);

  unsigned int buffer_size = m_image.width();
    
  f.seekg(h.data_offset);

  load_rgb_data(f, buffer_size, palette, pixel8_to_pixel32());
} // bitmap::reader::load_8bpp_rgb()
void claw::graphic::bitmap::reader::load_8bpp_rle ( const header h,
std::istream &  f,
const color_palette_type palette 
) [private]

Loads a 8 bpp bitmap file.

Parameters:
hFile's header and palette, must have been read before call.
fBitmap file.
paletteThe color palette to use for converting colors.
Precondition:
(h.bpp == 8) && (h.compression = BMP_COMPRESSION_RLE8) && (palette.size() == 256)

Definition at line 537 of file bitmap_reader.cpp.

References claw::graphic::bitmap::file_structure::header::bpp, claw::graphic::bitmap::file_structure::header::compression, claw::graphic::bitmap::file_structure::header::data_offset, claw::rle_decoder< Pattern, InputBuffer, OutputBuffer >::decode(), and claw::graphic::color_palette< Color >::size().

{
  assert(h.bpp == 8);
  assert(h.compression == BMP_COMPRESSION_RLE8);
  assert(palette.size() == 256);

  f.seekg(h.data_offset);

  rle8_decoder decoder;
  rle8_decoder::output_buffer_type output_buffer( palette, m_image );
  file_input_buffer input_buffer(f);

  decoder.decode( input_buffer, output_buffer );
} // bitmap::reader::load_8bpp_rle()
void claw::graphic::bitmap::reader::load_palette ( const header h,
std::istream &  f,
color_palette_type palette 
) const [private]

Load the palette of the image.

Parameters:
hFile's header, must have been read before call.
fBitmap file.
paletteThe loaded palette.
Precondition:
h.bpp <= 8

Definition at line 360 of file bitmap_reader.cpp.

References claw::graphic::bitmap::file_structure::header::bpp, and claw::graphic::color_palette< Color >::size().

{
  assert(h.bpp <= 8);

  switch(h.bpp)
    {
    case 1 : assert( palette.size() == 2 ); break;
    case 4 : assert( palette.size() == 16 ); break;
    case 8 : assert( palette.size() == 256 ); break;
    }

  const unsigned int sizeof_color = sizeof(color_palette_type::color_type);
  const unsigned int buffer_size = sizeof_color * palette.size();
  char* buffer = new char[buffer_size];

  f.read(buffer, buffer_size);

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

  delete[] buffer;
} // bitmap::reader::load_palette()
template<typename Convert >
void claw::graphic::bitmap::reader::load_rgb_data ( std::istream &  f,
unsigned int  buffer_size,
const color_palette_type palette,
const Convert &  pixel_convert 
) [private]

Load uncompressed data from the file.

Parameters:
fThe file from which we're loading the bitmap.
buffer_sizeNumber of bytes needed to store one line of pixels.
paletteColor palette.
pixel_convertA method to convert one line of pixels from the file to a line of the current bitmap.
Remarks:
The Convert type method must take this four parameters in this order: # scanline& destination line, # const char* input buffer (contains one line of the bitmap), # const color_palette_type& palette The color palette of the file,

Definition at line 161 of file bitmap_reader.tpp.

{
  unsigned int line;

  // lines are 4-bytes aligned, so adjust buffer's size.
  if (buffer_size % 4 != 0) 
    buffer_size += 4 - buffer_size % 4;

  char* buffer = new char[buffer_size];

  for (line = m_image.height(); (line>0) && !f.eof(); )
    {
      --line;
      f.read(buffer, buffer_size);
      pixel_convert( m_image[line], buffer, palette );
    }

  delete[] buffer;

  if ( f.rdstate() != std::ios_base::goodbit )
    throw claw::bad_format("bitmap::reader::load_data");
} // bitmap::reader::load_data()

Member Data Documentation

The image in which we store the data we read.

Definition at line 287 of file bitmap.hpp.


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