45 void bitmap::reader::rle_bitmap_output_buffer<false>::fill(
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,
70 bitmap::reader::rle_bitmap_output_buffer<true>::fill(
unsigned int n,
71 unsigned char pattern)
73 assert(m_x + n <= m_image.width());
75 for(
unsigned int i = 0; i != n / 2; ++i, m_x += 2)
77 m_image[m_y][m_x] = m_palette[(pattern & 0xF0) >> 4];
78 m_image[m_y][m_x + 1] = m_palette[pattern & 0x0F];
83 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);
137 void bitmap::reader::rle_bitmap_output_buffer<true>::copy(
138 unsigned int n, file_input_buffer& buffer)
140 assert(m_x + n <= m_image.width());
143 unsigned int bytes_needed = n / 2 + n % 2;
148 if(buffer.remaining() < bytes_needed)
149 buffer.read_more(bytes_needed);
151 assert(buffer.remaining() >= bytes_needed);
153 const unsigned char* p
154 =
reinterpret_cast<const unsigned char*
>(buffer.get_buffer());
155 const unsigned char* last = p + n / 2;
157 for(; p != last; ++p, m_x += 2)
159 m_image[m_y][m_x] = m_palette[(*p & 0xF0) >> 4];
160 m_image[m_y][m_x + 1] = m_palette[*p & 0x0F];
165 m_image[m_y][m_x] = m_palette[(*p & 0xF0) >> 4];
169 buffer.move(bytes_needed);
181void claw::graphic::bitmap::reader::pixel1_to_pixel32::operator()(
182 scanline& dest,
const char* src,
const color_palette_type& palette)
const
184 assert(palette.size() == 2);
186 scanline::iterator it(dest.begin());
187 const unsigned int n = dest.size();
188 const unsigned int byte_size = 8;
189 const unsigned int upper_bound = n / byte_size;
191 for(
unsigned int i = 0; i != upper_bound; ++i)
192 for(
unsigned int j = 0; j != byte_size; ++j, ++it)
193 if(src[i] & (0x80 >> j))
198 for(
unsigned int j = 0; j != (n % byte_size); ++j, ++it)
199 if(src[upper_bound] & (0x80 >> j))
212void claw::graphic::bitmap::reader::pixel4_to_pixel32::operator()(
213 scanline& dest,
const char* src,
const color_palette_type& palette)
const
215 assert(palette.size() == 16);
217 scanline::iterator it(dest.begin());
218 const unsigned int upper_bound = dest.size() / 2;
220 for(
unsigned int i = 0; i != upper_bound; ++i, ++src)
222 *it = palette[(*src & 0xF0) >> 4];
224 *it = palette[*src & 0x0F];
229 *it = palette[(*src & 0xF0) >> 4];
239void claw::graphic::bitmap::reader::pixel8_to_pixel32::operator()(
240 scanline& dest,
const char* src,
const color_palette_type& palette)
const
242 assert(palette.size() == 256);
244 const unsigned char* s =
reinterpret_cast<const unsigned char*
>(src);
246 std::transform(s, s + dest.size(), dest.begin(), palette);
255void claw::graphic::bitmap::reader::pixel24_to_pixel32::operator()(
256 scanline& dest,
const char* src,
const color_palette_type& palette)
const
258 scanline::iterator it(dest.begin());
259 const unsigned int upper_bound = 3 * dest.size();
261 for(
unsigned int i = 0; i != upper_bound; i += 3)
263 it->components.alpha = 255;
264 it->components.blue = src[i];
265 it->components.green = src[i + 1];
266 it->components.red = src[i + 2];
300 std::istream::pos_type init_pos = f.tellg();
306 f.read(
reinterpret_cast<char*
>(&h),
sizeof(
header));
308 if((h.
id[0] ==
'B') && (h.
id[1] ==
'M')
309 && (f.rdstate() == std::ios_base::goodbit))
330 "bitmap::bitmap: unsupported color depth.");
339 f.seekg(init_pos, std::ios_base::beg);
351void claw::graphic::bitmap::reader::load_palette(
352 const header& h, std::istream& f, color_palette_type& palette)
const
359 assert(palette.size() == 2);
362 assert(palette.size() == 16);
365 assert(palette.size() == 256);
369 const unsigned int sizeof_color =
sizeof(color_palette_type::color_type);
370 const unsigned int buffer_size = sizeof_color * palette.size();
371 char* buffer =
new char[buffer_size];
373 f.read(buffer, buffer_size);
375 for(
unsigned int i = 0, j = 0; i != buffer_size; i += sizeof_color, ++j)
377 palette[j].components.alpha = 255;
378 palette[j].components.blue = buffer[i];
379 palette[j].components.green = buffer[i + 1];
380 palette[j].components.red = buffer[i + 2];
392void claw::graphic::bitmap::reader::load_1bpp(
const header& h, std::istream& f)
397 color_palette_type palette(2);
398 unsigned int buffer_size = m_image.width() / (
sizeof(char) * 8);
400 if(m_image.width() % (
sizeof(
char) * 8))
403 load_palette(h, f, palette);
404 f.seekg(h.data_offset);
406 load_rgb_data(f, buffer_size, palette, pixel1_to_pixel32());
415void claw::graphic::bitmap::reader::load_4bpp(
const header& h, std::istream& f)
418 assert((h.compression == BMP_COMPRESSION_RGB)
419 || (h.compression == BMP_COMPRESSION_RLE4));
421 color_palette_type palette(16);
422 load_palette(h, f, palette);
424 if(h.compression == BMP_COMPRESSION_RLE4)
425 load_4bpp_rle(h, f, palette);
427 load_4bpp_rgb(h, f, palette);
436void claw::graphic::bitmap::reader::load_8bpp(
const header& h, std::istream& f)
439 assert((h.compression == BMP_COMPRESSION_RGB)
440 || (h.compression == BMP_COMPRESSION_RLE8));
442 color_palette_type palette(256);
443 load_palette(h, f, palette);
445 if(h.compression == BMP_COMPRESSION_RLE8)
446 load_8bpp_rle(h, f, palette);
448 load_8bpp_rgb(h, f, palette);
457void claw::graphic::bitmap::reader::load_24bpp(
const header& h,
462 unsigned int buffer_size = m_image.width() * 3;
463 color_palette_type palette(0);
465 f.seekg(h.data_offset);
467 load_rgb_data(f, buffer_size, palette, pixel24_to_pixel32());
478void claw::graphic::bitmap::reader::load_4bpp_rle(
479 const header& h, std::istream& f,
const color_palette_type& palette)
482 assert(h.compression == BMP_COMPRESSION_RLE4);
483 assert(palette.size() == 16);
485 f.seekg(h.data_offset);
487 rle4_decoder decoder;
488 rle4_decoder::output_buffer_type output_buffer(palette, m_image);
489 file_input_buffer input_buffer(f);
491 decoder.decode(input_buffer, output_buffer);
502void claw::graphic::bitmap::reader::load_4bpp_rgb(
503 const header& h, std::istream& f,
const color_palette_type& palette)
506 assert(h.compression == BMP_COMPRESSION_RGB);
507 assert(palette.size() == 16);
509 unsigned int buffer_size = m_image.width() / 2 + m_image.width() % 2;
511 f.seekg(h.data_offset);
513 load_rgb_data(f, buffer_size, palette, pixel4_to_pixel32());
524void claw::graphic::bitmap::reader::load_8bpp_rle(
525 const header& h, std::istream& f,
const color_palette_type& palette)
528 assert(h.compression == BMP_COMPRESSION_RLE8);
529 assert(palette.size() == 256);
531 f.seekg(h.data_offset);
533 rle8_decoder decoder;
534 rle8_decoder::output_buffer_type output_buffer(palette, m_image);
535 file_input_buffer input_buffer(f);
537 decoder.decode(input_buffer, output_buffer);
548void claw::graphic::bitmap::reader::load_8bpp_rgb(
549 const header& h, std::istream& f,
const color_palette_type& palette)
552 assert(h.compression == BMP_COMPRESSION_RGB);
553 assert(palette.size() == 256);
555 unsigned int buffer_size = m_image.width();
557 f.seekg(h.data_offset);
559 load_rgb_data(f, buffer_size, palette, pixel8_to_pixel32());
#define CLAW_PRECOND(b)
Abort the program if a precondition is not true.
A class for bitmap pictures.
reader(image &img)
Constructor.
void load(std::istream &f)
Load the image data from a stream.
image()
Constructor. Creates an image without datas.
Everything about image structures and processing.
This is the main namespace.