CLAW Library (a C++ Library Absolutely Wonderful) 1.5.5
|
This class read data from a bitmap file and store it in an image. More...
#include <bitmap.hpp>
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 | |
image & | m_image |
The image in which we store the data we read. |
This class read data from a bitmap file and store it in an image.
Definition at line 134 of file bitmap.hpp.
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.
claw::graphic::bitmap::reader::reader | ( | image & | img | ) |
Constructor.
img | The 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.
img | The image in which the data will be stored. |
f | The file from which we read the data. |
Definition at line 300 of file bitmap_reader.cpp.
References load().
void claw::graphic::bitmap::reader::load | ( | std::istream & | f | ) |
Load the image data from a stream.
f | The file from which we read the data. |
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.
h | File's header, must have been read before call. |
f | Bitmap file. |
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.
h | File's header, must have been read before call. |
f | Bitmap file. |
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.
h | File's header, must have been read before call. |
f | Bitmap file. |
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.
h | File's header and palette, must have been read before call. |
f | Bitmap file. |
palette | The color palette to use for converting colors. |
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.
h | File's header and palette, must have been read before call. |
f | Bitmap file. |
palette | The color palette to use for converting colors. |
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.
h | File's header, must have been read before call. |
f | Bitmap file. |
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.
h | File's header and palette, must have been read before call. |
f | Bitmap file. |
palette | The color palette to use for converting colors. |
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.
h | File's header and palette, must have been read before call. |
f | Bitmap file. |
palette | The color palette to use for converting colors. |
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.
h | File's header, must have been read before call. |
f | Bitmap file. |
palette | The loaded palette. |
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()
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.
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. |
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()
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.