46 (
unsigned int n,
unsigned char pattern )
48 assert( m_x + n <= m_image.width() );
50 std::fill(&m_image[m_y][m_x], &m_image[m_y][m_x] + n, m_palette[pattern]);
70 (
unsigned int n,
unsigned char pattern )
72 assert( m_x + n <= m_image.width() );
74 for (
unsigned int i = 0; i != n / 2; ++i, m_x += 2)
76 m_image[m_y][m_x] = m_palette[ (pattern & 0xF0) >> 4 ];
77 m_image[m_y][m_x+1] = m_palette[ pattern & 0x0F ];
82 m_image[m_y][m_x] = m_palette[ (pattern & 0xF0) >> 4 ];
101 void bitmap::reader::rle_bitmap_output_buffer<false>::copy
102 (
unsigned int n, file_input_buffer& buffer )
104 assert( m_x + n <= m_image.width() );
107 const unsigned int bytes_needed = n + n % 2;
109 if ( buffer.remaining() < bytes_needed )
110 buffer.read_more(bytes_needed);
112 assert( buffer.remaining() >= bytes_needed );
114 const unsigned char* p =
115 reinterpret_cast<const unsigned char*
>(buffer.get_buffer());
117 std::transform( p, p + n, &m_image[m_y][m_x], m_palette );
121 buffer.move(bytes_needed);
138 void bitmap::reader::rle_bitmap_output_buffer<true>::copy
139 (
unsigned int n, file_input_buffer& buffer )
141 assert( m_x + n <= m_image.width() );
144 unsigned int bytes_needed = n / 2 + n % 2;
146 if ( bytes_needed % 2 )
149 if ( buffer.remaining() < bytes_needed )
150 buffer.read_more( bytes_needed );
152 assert( buffer.remaining() >= bytes_needed );
154 const unsigned char* p =
155 reinterpret_cast<const unsigned char*
>(buffer.get_buffer());
156 const unsigned char* last = p + n / 2;
158 for ( ; p != last; ++p, m_x += 2)
160 m_image[m_y][m_x] = m_palette[ (*p & 0xF0) >> 4 ];
161 m_image[m_y][m_x+1] = m_palette[ *p & 0x0F ];
166 m_image[m_y][m_x] = m_palette[ (*p & 0xF0) >> 4 ];
170 buffer.move( bytes_needed );
185 void claw::graphic::bitmap::reader::pixel1_to_pixel32::operator()
186 ( scanline& dest,
const char* src,
const color_palette_type& palette )
const
188 assert(palette.size() == 2);
190 scanline::iterator it( dest.begin() );
191 const unsigned int n = dest.size();
192 const unsigned int byte_size = 8;
193 const unsigned int upper_bound = n / byte_size;
195 for (
unsigned int i=0; i!=upper_bound; ++i)
196 for (
unsigned int j=0; j!=byte_size; ++j, ++it)
197 if ( src[i] & (0x80 >> j) )
202 for (
unsigned int j = 0; j != (n % byte_size); ++j, ++it)
203 if ( src[upper_bound] & (0x80 >> j) )
217 void claw::graphic::bitmap::reader::pixel4_to_pixel32::operator()
218 ( scanline& dest,
const char* src,
const color_palette_type& palette )
const
220 assert(palette.size() == 16);
222 scanline::iterator it( dest.begin() );
223 const unsigned int upper_bound = dest.size() / 2;
225 for (
unsigned int i=0; i!=upper_bound; ++i, ++src)
227 *it = palette[ (*src & 0xF0) >> 4 ];
229 *it = palette[ *src & 0x0F ];
234 *it = palette[ (*src & 0xF0) >> 4 ];
245 void claw::graphic::bitmap::reader::pixel8_to_pixel32::operator()
246 ( scanline& dest,
const char* src,
const color_palette_type& palette )
const
248 assert(palette.size() == 256);
250 const unsigned char* s =
reinterpret_cast<const unsigned char*
>(src);
252 std::transform(s, s + dest.size(), dest.begin(), palette);
262 void claw::graphic::bitmap::reader::pixel24_to_pixel32::operator()
263 ( scanline& dest,
const char* src,
const color_palette_type& palette )
const
265 scanline::iterator it( dest.begin() );
266 const unsigned int upper_bound = 3 * dest.size();
268 for (
unsigned int i=0; i!=upper_bound; i+=3)
270 it->components.alpha = 255;
271 it->components.blue = src[i];
272 it->components.green = src[i+1];
273 it->components.red = src[i+2];
315 std::istream::pos_type init_pos = f.tellg();
321 f.read( reinterpret_cast<char*>(&h),
sizeof(
header) );
323 if ( (h.
id[0] ==
'B') && (h.
id[1] ==
'M')
324 && (f.rdstate() == std::ios_base::goodbit) )
330 case 1 : load_1bpp(h, f);
break;
331 case 4 : load_4bpp(h, f);
break;
332 case 8 : load_8bpp(h, f);
break;
334 case 24 : load_24bpp(h, f);
break;
337 (
"bitmap::bitmap: unsupported color depth.");
346 f.seekg( init_pos, std::ios_base::beg );
359 void claw::graphic::bitmap::reader::load_palette
360 (
const header& h, std::istream& f, color_palette_type& palette )
const
366 case 1 : assert( palette.size() == 2 );
break;
367 case 4 : assert( palette.size() == 16 );
break;
368 case 8 : assert( palette.size() == 256 );
break;
371 const unsigned int sizeof_color =
sizeof(color_palette_type::color_type);
372 const unsigned int buffer_size = sizeof_color * palette.size();
373 char* buffer =
new char[buffer_size];
375 f.read(buffer, buffer_size);
377 for (
unsigned int i=0, j=0; i!=buffer_size; i+=sizeof_color, ++j)
379 palette[j].components.alpha = 255;
380 palette[j].components.blue = buffer[i];
381 palette[j].components.green = buffer[i+1];
382 palette[j].components.red = buffer[i+2];
396 claw::graphic::bitmap::reader::load_1bpp(
const header& h, std::istream& f )
401 color_palette_type palette(2);
402 unsigned int buffer_size = m_image.width() / (
sizeof(char) * 8);
404 if ( m_image.width() % (
sizeof(char) * 8) )
407 load_palette(h, f, palette);
408 f.seekg(h.data_offset);
410 load_rgb_data(f, buffer_size, palette, pixel1_to_pixel32());
421 claw::graphic::bitmap::reader::load_4bpp(
const header& h, std::istream& f )
424 assert( (h.compression == BMP_COMPRESSION_RGB)
425 || (h.compression == BMP_COMPRESSION_RLE4) );
427 color_palette_type palette(16);
428 load_palette(h, f, palette);
430 if (h.compression == BMP_COMPRESSION_RLE4)
431 load_4bpp_rle(h, f, palette);
433 load_4bpp_rgb(h, f, palette);
444 claw::graphic::bitmap::reader::load_8bpp(
const header& h, std::istream& f )
447 assert( (h.compression == BMP_COMPRESSION_RGB)
448 || (h.compression == BMP_COMPRESSION_RLE8) );
450 color_palette_type palette(256);
451 load_palette(h, f, palette);
453 if (h.compression == BMP_COMPRESSION_RLE8)
454 load_8bpp_rle(h, f, palette);
456 load_8bpp_rgb(h, f, palette);
467 claw::graphic::bitmap::reader::load_24bpp(
const header& h, std::istream& f )
471 unsigned int buffer_size = m_image.width() * 3;
472 color_palette_type palette(0);
474 f.seekg(h.data_offset);
476 load_rgb_data(f, buffer_size, palette, pixel24_to_pixel32());
488 void claw::graphic::bitmap::reader::load_4bpp_rle
489 (
const header& h, std::istream& f,
const color_palette_type& palette )
492 assert(h.compression == BMP_COMPRESSION_RLE4);
493 assert(palette.size() == 16);
495 f.seekg(h.data_offset);
497 rle4_decoder decoder;
498 rle4_decoder::output_buffer_type output_buffer( palette, m_image );
499 file_input_buffer input_buffer(f);
501 decoder.decode( input_buffer, output_buffer );
513 void claw::graphic::bitmap::reader::load_4bpp_rgb
514 (
const header& h, std::istream& f,
const color_palette_type& palette )
517 assert(h.compression == BMP_COMPRESSION_RGB);
518 assert(palette.size() == 16);
520 unsigned int buffer_size = m_image.width() / 2 + m_image.width() % 2;
522 f.seekg(h.data_offset);
524 load_rgb_data(f, buffer_size, palette, pixel4_to_pixel32());
536 void claw::graphic::bitmap::reader::load_8bpp_rle
537 (
const header& h, std::istream& f,
const color_palette_type& palette )
540 assert(h.compression == BMP_COMPRESSION_RLE8);
541 assert(palette.size() == 256);
543 f.seekg(h.data_offset);
545 rle8_decoder decoder;
546 rle8_decoder::output_buffer_type output_buffer( palette, m_image );
547 file_input_buffer input_buffer(f);
549 decoder.decode( input_buffer, output_buffer );
561 void claw::graphic::bitmap::reader::load_8bpp_rgb
562 (
const header& h, std::istream& f,
const color_palette_type& palette )
565 assert(h.compression == BMP_COMPRESSION_RGB);
566 assert(palette.size() == 256);
568 unsigned int buffer_size = m_image.width();
570 f.seekg(h.data_offset);
572 load_rgb_data(f, buffer_size, palette, pixel8_to_pixel32());