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

#include <bitmap.hpp>

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

claw::graphic::bitmap::file_structure List of all members.

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.

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.

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


Member Typedef Documentation

typedef buffered_istream<std::istream> claw::graphic::bitmap::reader::file_input_buffer [private]

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

Definition at line 139 of file bitmap.hpp.

typedef rle_bitmap_decoder< rle_bitmap_output_buffer<true> > claw::graphic::bitmap::reader::rle4_decoder [private]

RLE decoder for 4 bpp bitmap images.

Definition at line 210 of file bitmap.hpp.

typedef rle_bitmap_decoder< rle_bitmap_output_buffer<false> > claw::graphic::bitmap::reader::rle8_decoder [private]

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

Definition at line 287 of file bitmap_reader.cpp.

00288   : m_image( img )
00289 {
00290 
00291 } // bitmap::reader::reader()

claw::graphic::bitmap::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 300 of file bitmap_reader.cpp.

References load().

00301   : m_image( img )
00302 {
00303   load(f);
00304 } // bitmap::reader::reader()


Member Function Documentation

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

Load the image data from a stream.

Parameters:
f The 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_PRECOND, load_1bpp(), load_24bpp(), load_4bpp(), load_8bpp(), m_image, and claw::graphic::image::set_size().

Referenced by reader().

00313 {
00314   CLAW_PRECOND( !!f );
00315   std::istream::pos_type init_pos = f.tellg();
00316 
00317   try
00318     {
00319       header h;
00320 
00321       f.read( reinterpret_cast<char*>(&h), sizeof(header) );
00322 
00323       if ( (h.id[0] == 'B') && (h.id[1] == 'M')
00324            && (f.rdstate() == std::ios_base::goodbit) )
00325         {
00326           m_image.set_size(h.width, h.height);
00327       
00328           switch(h.bpp)
00329             {
00330             case 1 : load_1bpp(h, f); break;
00331             case 4 : load_4bpp(h, f); break;
00332             case 8 : load_8bpp(h, f); break;
00333               //case 16 : load_16bpp(h, f); break;
00334             case 24 : load_24bpp(h, f); break;
00335             default : 
00336               throw claw::bad_format
00337                 ("bitmap::bitmap: unsupported color depth.");
00338             }
00339         }
00340       else
00341         throw claw::bad_format( "bitmap::bitmap: invalid header." );
00342     }
00343   catch(...)
00344     {
00345       f.clear();
00346       f.seekg( init_pos, std::ios_base::beg );
00347       throw;
00348     }
00349 } // bitmap::reader::load()

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

Load a monochrome bitmap file.

Parameters:
h File's header, must have been read before call.
f Bitmap file.
Precondition:
h.bpp == 1

Definition at line 396 of file bitmap_reader.cpp.

References load_palette(), load_rgb_data(), m_image, and claw::graphic::image::width().

Referenced by load().

00397 {
00398   assert(h.bpp == 1);
00399   //assert(h.compression == BMP_COMPRESSION_BITFIELDS);
00400 
00401   color_palette_type palette(2);
00402   unsigned int buffer_size = m_image.width() / (sizeof(char) * 8);
00403         
00404   if ( m_image.width() % (sizeof(char) * 8) )
00405     ++buffer_size;
00406     
00407   load_palette(h, f, palette);
00408   f.seekg(h.data_offset);
00409 
00410   load_rgb_data(f, buffer_size, palette, pixel1_to_pixel32());
00411 } // 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:
h File's header, must have been read before call.
f Bitmap file.
Precondition:
(h.bpp == 24)

Definition at line 467 of file bitmap_reader.cpp.

References load_rgb_data(), m_image, and claw::graphic::image::width().

Referenced by load().

00468 {
00469   assert(h.bpp == 24);
00470 
00471   unsigned int buffer_size = m_image.width() * 3;
00472   color_palette_type palette(0);
00473         
00474   f.seekg(h.data_offset);
00475 
00476   load_rgb_data(f, buffer_size, palette, pixel24_to_pixel32());
00477 } // 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:
h File's header, must have been read before call.
f Bitmap file.
Precondition:
(h.bpp == 4)

Definition at line 421 of file bitmap_reader.cpp.

References claw::graphic::bitmap::file_structure::BMP_COMPRESSION_RGB, claw::graphic::bitmap::file_structure::BMP_COMPRESSION_RLE4, load_4bpp_rgb(), load_4bpp_rle(), and load_palette().

Referenced by load().

00422 {
00423   assert(h.bpp == 4);
00424   assert( (h.compression == BMP_COMPRESSION_RGB)
00425           || (h.compression == BMP_COMPRESSION_RLE4) );
00426 
00427   color_palette_type palette(16);
00428   load_palette(h, f, palette);
00429 
00430   if (h.compression == BMP_COMPRESSION_RLE4)
00431     load_4bpp_rle(h, f, palette);
00432   else
00433     load_4bpp_rgb(h, f, palette);
00434 } // 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:
h File's header and palette, must have been read before call.
f Bitmap file.
palette The 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.

Referenced by load_4bpp().

00515 {
00516   assert(h.bpp == 4);
00517   assert(h.compression == BMP_COMPRESSION_RGB);
00518   assert(palette.size() == 16);
00519 
00520   unsigned int buffer_size = m_image.width() / 2 + m_image.width() % 2;
00521     
00522   f.seekg(h.data_offset);
00523 
00524   load_rgb_data(f, buffer_size, palette, pixel4_to_pixel32());
00525 } // 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:
h File's header and palette, must have been read before call.
f Bitmap file.
palette The 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::rle_decoder< Pattern, InputBuffer, OutputBuffer >::decode().

Referenced by load_4bpp().

00490 {
00491   assert(h.bpp == 4);
00492   assert(h.compression == BMP_COMPRESSION_RLE4);
00493   assert(palette.size() == 16);
00494 
00495   f.seekg(h.data_offset);
00496 
00497   rle4_decoder decoder;
00498   rle4_decoder::output_buffer_type output_buffer( palette, m_image );
00499   file_input_buffer input_buffer(f);
00500 
00501   decoder.decode( input_buffer, output_buffer );
00502 } // 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:
h File's header, must have been read before call.
f Bitmap file.
Precondition:
(h.bpp == 8)

Definition at line 444 of file bitmap_reader.cpp.

References claw::graphic::bitmap::file_structure::BMP_COMPRESSION_RGB, claw::graphic::bitmap::file_structure::BMP_COMPRESSION_RLE8, load_8bpp_rgb(), load_8bpp_rle(), and load_palette().

Referenced by load().

00445 {
00446   assert(h.bpp == 8);
00447   assert( (h.compression == BMP_COMPRESSION_RGB)
00448           || (h.compression == BMP_COMPRESSION_RLE8) );
00449 
00450   color_palette_type palette(256);
00451   load_palette(h, f, palette);
00452 
00453   if (h.compression == BMP_COMPRESSION_RLE8)
00454     load_8bpp_rle(h, f, palette);
00455   else
00456     load_8bpp_rgb(h, f, palette);
00457 } // 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:
h File's header and palette, must have been read before call.
f Bitmap file.
palette The 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.

Referenced by load_8bpp().

00563 {
00564   assert(h.bpp == 8);
00565   assert(h.compression == BMP_COMPRESSION_RGB);
00566   assert(palette.size() == 256);
00567 
00568   unsigned int buffer_size = m_image.width();
00569     
00570   f.seekg(h.data_offset);
00571 
00572   load_rgb_data(f, buffer_size, palette, pixel8_to_pixel32());
00573 } // 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:
h File's header and palette, must have been read before call.
f Bitmap file.
palette The 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::rle_decoder< Pattern, InputBuffer, OutputBuffer >::decode().

Referenced by load_8bpp().

00538 {
00539   assert(h.bpp == 8);
00540   assert(h.compression == BMP_COMPRESSION_RLE8);
00541   assert(palette.size() == 256);
00542 
00543   f.seekg(h.data_offset);
00544 
00545   rle8_decoder decoder;
00546   rle8_decoder::output_buffer_type output_buffer( palette, m_image );
00547   file_input_buffer input_buffer(f);
00548 
00549   decoder.decode( input_buffer, output_buffer );
00550 } // 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:
h File's header, must have been read before call.
f Bitmap file.
palette The loaded palette.
Precondition:
h.bpp <= 8

Definition at line 360 of file bitmap_reader.cpp.

Referenced by load_1bpp(), load_4bpp(), and load_8bpp().

00361 {
00362   assert(h.bpp <= 8);
00363 
00364   switch(h.bpp)
00365     {
00366     case 1 : assert( palette.size() == 2 ); break;
00367     case 4 : assert( palette.size() == 16 ); break;
00368     case 8 : assert( palette.size() == 256 ); break;
00369     }
00370 
00371   const unsigned int sizeof_color = sizeof(color_palette_type::color_type);
00372   const unsigned int buffer_size = sizeof_color * palette.size();
00373   char* buffer = new char[buffer_size];
00374 
00375   f.read(buffer, buffer_size);
00376 
00377   for (unsigned int i=0, j=0; i!=buffer_size; i+=sizeof_color, ++j)
00378     {
00379       palette[j].components.alpha = 255;
00380       palette[j].components.blue  = buffer[i];
00381       palette[j].components.green = buffer[i+1];
00382       palette[j].components.red   = buffer[i+2];
00383     }
00384 
00385   delete[] buffer;
00386 } // 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:
f The file from which we're loading the bitmap.
buffer_size Number of bytes needed to store one line of pixels.
palette Color palette.
pixel_convert A 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.

Referenced by load_1bpp(), and load_24bpp().

00163 {
00164   unsigned int line;
00165 
00166   // lines are 4-bytes aligned, so adjust buffer's size.
00167   if (buffer_size % 4 != 0) 
00168     buffer_size += 4 - buffer_size % 4;
00169 
00170   char* buffer = new char[buffer_size];
00171 
00172   for (line = m_image.height(); (line>0) && !f.eof(); )
00173     {
00174       --line;
00175       f.read(buffer, buffer_size);
00176       pixel_convert( m_image[line], buffer, palette );
00177     }
00178 
00179   delete[] buffer;
00180 
00181   if ( f.rdstate() != std::ios_base::goodbit )
00182     throw claw::bad_format("bitmap::reader::load_data");
00183 } // bitmap::reader::load_data()


Member Data Documentation

image& claw::graphic::bitmap::reader::m_image [private]

The image in which we store the data we read.

Definition at line 287 of file bitmap.hpp.

Referenced by load(), load_1bpp(), and load_24bpp().


The documentation for this class was generated from the following files:
Generated on Mon Nov 9 05:07:10 2009 for CLAW Library (a C++ Library Absolutely Wonderful) by  doxygen 1.4.7