maug
Quick and dirty C mini-augmentation library.
Loading...
Searching...
No Matches
mfmt.h
Go to the documentation of this file.
1
2#ifndef MFMT_H
3#define MFMT_H
4
5#include <mfile.h>
6
11
15
21
22#define MFMT_BMPINFO_OFS_WIDTH 4
23#define MFMT_BMPINFO_OFS_HEIGHT 8
24#define MFMT_BMPINFO_OFS_COLOR_PLANES 12
25#define MFMT_BMPINFO_OFS_BPP 14
26#define MFMT_BMPINFO_OFS_COMPRESSION 16
27#define MFMT_BMPINFO_OFS_SZ 20
28#define MFMT_BMPINFO_OFS_HRES 24
29#define MFMT_BMPINFO_OFS_VRES 28
30#define MFMT_BMPINFO_OFS_PAL_SZ 32
31#define MFMT_BMPINFO_OFS_IMP_COLORS 36
32
34#define MFMT_BMP_COMPRESSION_NONE (0)
36#define MFMT_BMP_COMPRESSION_RLE8 (1)
38#define MFMT_BMP_COMPRESSION_RLE4 (2)
39 /* maug_fmt_bmp */
41
42#define MFMT_DECOMP_FLAG_4BIT 0x01
43#define MFMT_DECOMP_FLAG_8BIT 0x02
44
45#define MFMT_PX_FLAG_INVERT_Y 0x01
46
52#define MFMT_PX_FLAG_NEW_LINE 0x02
53
54#ifndef MFMT_TRACE_BMP_LVL
55# define MFMT_TRACE_BMP_LVL 0
56#endif /* !MFMT_TRACE_BMP_LVL */
57
58#ifndef MFMT_TRACE_RLE_LVL
59# define MFMT_TRACE_RLE_LVL 0
60#endif /* !MFMT_TRACE_RLE_LVL */
61
71 uint32_t sz;
72};
73
78
79#define mfmt_bmp_check_header() \
80 debug_printf( MFMT_TRACE_BMP_LVL, \
81 "bmp file sz: " U32_FMT ", magic: %c%c", \
82 header->sz, (((char*)header)[0]), (((char*)header)[1]) ); \
83 if( 40 == header->sz ) { \
84 debug_printf( MFMT_TRACE_BMP_LVL, "bmp info header detected by sz" ); \
85 header_bmp_info = (struct MFMT_STRUCT_BMPINFO*)header; \
86 } else if( \
87 'B' == (((char*)header)[0]) && 'M' == (((char*)header)[1]) \
88 ) { \
89 debug_printf( MFMT_TRACE_BMP_LVL, "bmp file header detected by sig" ); \
90 header_bmp_file = (struct MFMT_STRUCT_BMPFILE*)header; \
91 header_bmp_info = &(header_bmp_file->info); \
92 } else { \
93 error_printf( "unable to select read header!" ); \
94 retval = MERROR_FILE; \
95 goto cleanup; \
96 }
97
101 uint32_t sz;
103 int32_t width;
105 int32_t height;
107 uint16_t color_planes;
109 uint16_t bpp;
111 uint32_t compression;
113 uint32_t img_sz;
115 uint32_t hres;
117 uint32_t vres;
121 uint32_t imp_colors;
122};
123
125 char magic[2];
126 uint32_t file_sz;
127 uint16_t reserved1;
128 uint16_t reserved2;
129 uint32_t px_offset;
130 struct MFMT_STRUCT_BMPINFO info;
131};
132
138 off_t byte_idx;
139 uint8_t SEG_FAR* px;
140 off_t px_sz;
141};
142 /* maug_fmt_bmp */
144
155 mfile_t* p_file_in, off_t file_offset, off_t file_sz, size_t line_w,
156 MAUG_MHANDLE buffer_out, off_t buffer_out_sz, uint8_t flags );
157
162 struct MFMT_STRUCT* header, mfile_t* p_file_in,
163 uint32_t file_offset, off_t file_sz, uint8_t* p_flags );
164
169 struct MFMT_STRUCT* header, uint32_t* palette, size_t palette_sz,
170 mfile_t* p_file_in, uint32_t file_offset, off_t file_sz,
171 uint8_t flags );
172
177 struct MFMT_STRUCT* header, uint8_t SEG_FAR* px, off_t px_sz,
178 mfile_t* p_file_in, uint32_t file_offset, off_t file_sz,
179 uint8_t flags );
180
186 void* data, uint8_t px, int32_t x, int32_t y,
187 void* header_info, uint8_t flags );
188
193 mfile_t* p_file_in, off_t file_offset, off_t file_sz, size_t line_w,
194 MAUG_MHANDLE buffer_out, off_t buffer_out_sz, uint8_t flags );
195
196MERROR_RETVAL mfmt_read_bmp_header(
197 struct MFMT_STRUCT* header, mfile_t* p_file_in,
198 off_t file_offset, off_t file_sz, uint8_t* p_flags );
199
200MERROR_RETVAL mfmt_read_bmp_palette(
201 struct MFMT_STRUCT* header, uint32_t* palette, size_t palette_sz,
202 mfile_t* p_file_in, uint32_t file_offset, off_t file_sz,
203 uint8_t flags );
204
210 struct MFMT_STRUCT_BMPINFO* header_bmp_info, mfile_t* p_bmp_in,
211 size_t px_offset, MAUG_MHANDLE* p_px_h );
212
217 struct MFMT_STRUCT* header,
218 mfile_t* p_file_in, uint32_t px_offset, off_t file_sz, uint8_t flags,
219 mfmt_read_1px_cb px_cb, void* px_cb_data );
220
225 struct MFMT_STRUCT* header, uint8_t SEG_FAR* px, off_t px_sz,
226 mfile_t* p_file_in, uint32_t file_offset, off_t file_sz,
227 uint8_t flags );
228
229#ifdef MFMT_C
230
232 mfile_t* p_file_in, off_t file_offset, off_t file_sz, size_t line_w,
233 MAUG_MHANDLE buffer_out_h, off_t buffer_out_sz, uint8_t flags
234) {
235 MERROR_RETVAL retval = MERROR_OK;
236 uint8_t* buffer_out = NULL;
237 off_t in_byte_cur = 0,
238 out_byte_cur = 0;
239 uint8_t out_mask_cur = 0xf0,
240 run_char = 0,
241 run_count = 0,
242 decode_state = 0,
243 unpadded_written = 0,
244 line_px_written = 0,
245 lines_out = 0,
246 byte_buffer = 0;
247
248 #define MFMT_RLE_DECODE_RUN 0
249 #define MFMT_RLE_DECODE_CHAR 1
250 #define MFMT_RLE_DECODE_ABS_RIGHT 2
251 #define MFMT_RLE_DECODE_ABS_DOWN 3
252 #define MFMT_RLE_DECODE_ESC 4
253 #define MFMT_RLE_DECODE_LITERAL 5
254 #define MFMT_RLE_DECODE_LITERAL_PAD 6
255
256 assert( flags == MFMT_DECOMP_FLAG_4BIT );
257
258 #define mfmt_decode_rle_state( new_state ) \
259 debug_printf( MFMT_TRACE_RLE_LVL, "new state: %s", #new_state ); \
260 decode_state = new_state;
261
262 #define mfmt_decode_rle_check_eol() \
263 if( line_px_written >= line_w ) { \
264 debug_printf( MFMT_TRACE_RLE_LVL, \
265 "EOL: %u px written (between runs)", \
266 line_px_written ); \
267 mfmt_decode_rle_reset_line(); \
268 }
269
270 /* Flip between odd/even nibble as advanced. */
271 #define mfmt_decode_rle_advance_mask() \
272 out_mask_cur >>= 4; \
273 if( 0 == out_mask_cur ) { \
274 out_byte_cur++; \
275 if( out_byte_cur > buffer_out_sz ) { \
276 error_printf( \
277 "out byte " OFF_T_FMT " outside of " OFF_T_FMT \
278 " pixel buffer!", out_byte_cur, buffer_out_sz ); \
279 retval = MERROR_OVERFLOW; \
280 goto cleanup; \
281 } else if( out_byte_cur < buffer_out_sz ) { \
282 /* We're not at the end of the file yet! */ \
283 buffer_out[out_byte_cur] = 0; \
284 } \
285 out_mask_cur = 0xf0; \
286 }
287
288 #define mfmt_decode_rle_reset_line() \
289 if( line_w != line_px_written ) { \
290 error_printf( \
291 "line written pixels %u does not match line width " SIZE_T_FMT, \
292 line_px_written, line_w ); \
293 retval = MERROR_OVERFLOW; \
294 goto cleanup; \
295 } \
296 line_px_written = 0; \
297 out_mask_cur = 0xf0; \
298 lines_out++; \
299 debug_printf( MFMT_TRACE_RLE_LVL, "now on line: %u", lines_out );
300
301 #define mfmt_decode_rle_inc_line_w( incr ) \
302 line_px_written += incr; \
303 if( line_w < line_px_written ) { \
304 error_printf( \
305 "line byte %u outside of " SIZE_T_FMT \
306 " line width!", line_px_written, line_w ); \
307 retval = MERROR_OVERFLOW; \
308 goto cleanup; \
309 }
310
311 debug_printf( 1, "decompressing RLE into temporary buffer..." );
312
313 maug_mlock( buffer_out_h, buffer_out );
314
315 maug_mzero( buffer_out, buffer_out_sz );
316
317 do {
318 retval = p_file_in->seek( p_file_in, file_offset + in_byte_cur++ );
319 maug_cleanup_if_not_ok();
320 retval = p_file_in->read_byte( p_file_in, &byte_buffer );
321 maug_cleanup_if_not_ok();
322
323 debug_printf( MFMT_TRACE_RLE_LVL, "in byte " OFF_T_FMT
324 ": 0x%02x, out byte " OFF_T_FMT ", line px: %u",
325 in_byte_cur, byte_buffer, out_byte_cur, line_px_written );
326
327 switch( byte_buffer ) {
328 case 0:
329 if( MFMT_RLE_DECODE_RUN == decode_state ) {
330 mfmt_decode_rle_state( MFMT_RLE_DECODE_ESC );
331 break;
332
333 } else if( MFMT_RLE_DECODE_LITERAL_PAD == decode_state ) {
334 /* This is just a padding byte to make sure literals are %16. */
335 assert( 0 == byte_buffer );
336 mfmt_decode_rle_state( MFMT_RLE_DECODE_RUN );
337 break;
338
339 } else if( MFMT_RLE_DECODE_ESC == decode_state ) {
340 /* This is an EOL marker. */
341 debug_printf( MFMT_TRACE_RLE_LVL,
342 "EOL: %u px written", line_px_written );
343 while( line_px_written < line_w ) {
344 /* Pad out the end of the line. */
345 assert( 0 == line_px_written % 2 );
346 buffer_out[out_byte_cur++] = 0x00;
347 mfmt_decode_rle_inc_line_w( 2 );
348 debug_printf( MFMT_TRACE_RLE_LVL,
349 "padded line (%u written)", line_px_written );
350 }
351 mfmt_decode_rle_reset_line();
352
353 /* Diversion over, go back to hunting for runs. */
354 mfmt_decode_rle_state( MFMT_RLE_DECODE_RUN );
355 break;
356 }
357
358 case 1:
359 if( MFMT_RLE_DECODE_ESC == decode_state ) {
360 debug_printf( MFMT_TRACE_RLE_LVL, "EOBM" );
361 /* End of bitmap, so pad the rest of the file. */
362
363 while( out_byte_cur < buffer_out_sz ) {
364 /* Pad out the end of the line. */
365 assert( 0 == line_px_written % 2 );
366 mfmt_decode_rle_check_eol();
367 buffer_out[out_byte_cur++] = 0x00;
368 mfmt_decode_rle_inc_line_w( 2 );
369 debug_printf( MFMT_TRACE_RLE_LVL,
370 "padded file (%u written)", line_px_written );
371 }
372
373 mfmt_decode_rle_state( MFMT_RLE_DECODE_RUN );
374 break;
375 }
376
377 case 2:
378 if( MFMT_RLE_DECODE_ESC == decode_state ) {
379 debug_printf( MFMT_TRACE_RLE_LVL, "absolute mode: right" );
380 /* TODO: Absolute mode. */
381 assert( 1 == 0 );
382 mfmt_decode_rle_state( MFMT_RLE_DECODE_ABS_RIGHT );
383 break;
384 }
385
386 default:
387 switch( decode_state ) {
388 case MFMT_RLE_DECODE_LITERAL:
389
390 mfmt_decode_rle_check_eol();
391
392 run_count -= 2;
393 unpadded_written += 2;
394 mfmt_decode_rle_inc_line_w( 2 );
395 debug_printf( MFMT_TRACE_RLE_LVL,
396 "writing literal: 0x%02x (%u left, unpadded run val: %u)",
397 byte_buffer, run_count, unpadded_written );
398 buffer_out[out_byte_cur++] = byte_buffer;
399
400 if( 0 == run_count ) {
401 if( 0 != unpadded_written % 4 ) {
402 /* Uneven number of literals copied. */
403 /* Ignore the byte, as it's a pad to word-size/16-bits. */
404 debug_printf( MFMT_TRACE_RLE_LVL,
405 "unpadded: %u, next is pad byte", unpadded_written );
406 /* assert( 0 == byte_buffer ); */
407 mfmt_decode_rle_state( MFMT_RLE_DECODE_LITERAL_PAD );
408 } else {
409
410 /* Diversion over, go back to hunting for runs. */
411 mfmt_decode_rle_state( MFMT_RLE_DECODE_RUN );
412 }
413 }
414 break;
415
416 case MFMT_RLE_DECODE_ESC:
417 run_count = byte_buffer;
418 unpadded_written = 0;
419 debug_printf( MFMT_TRACE_RLE_LVL,
420 "literal mode: %u nibbles", run_count );
421 assert( 0 == run_count % 2 );
422 mfmt_decode_rle_state( MFMT_RLE_DECODE_LITERAL );
423 break;
424
425 case MFMT_RLE_DECODE_ABS_RIGHT:
426 debug_printf( MFMT_TRACE_RLE_LVL, "absolute mode: up" );
427 /* TODO: Absolute mode. */
428 assert( 1 == 0 );
429 mfmt_decode_rle_state( MFMT_RLE_DECODE_ABS_DOWN );
430 break;
431
432 case MFMT_RLE_DECODE_RUN:
433
434 mfmt_decode_rle_check_eol();
435
436 run_count = byte_buffer;
437 debug_printf( MFMT_TRACE_RLE_LVL,
438 "starting run: %u nibbles", run_count );
439 mfmt_decode_rle_state( MFMT_RLE_DECODE_CHAR );
440 break;
441
442 case MFMT_RLE_DECODE_CHAR:
443 assert( 0 != run_count );
444 run_char = byte_buffer;
445 debug_printf( MFMT_TRACE_RLE_LVL,
446 "%u-long run of 0x%02x...", run_count, run_char );
447 do {
448 /* Expand the run into the prescribed number of nibbles. */
449 debug_printf( MFMT_TRACE_RLE_LVL,
450 "writing 0x%02x & 0x%02x #%u (line px #%u)...",
451 run_char, out_mask_cur, run_count, line_px_written );
452 buffer_out[out_byte_cur] |= (run_char & out_mask_cur);
453 mfmt_decode_rle_advance_mask();
454 mfmt_decode_rle_inc_line_w( 1 );
455 run_count--;
456
457 } while( 0 < run_count );
458
459 /* Diversion over, go back to hunting for runs. */
460 mfmt_decode_rle_state( MFMT_RLE_DECODE_RUN );
461 break;
462
463 }
464 break;
465 }
466 } while( in_byte_cur < file_sz );
467
468 debug_printf(
469 MFMT_TRACE_RLE_LVL, "wrote " OFF_T_FMT " bytes (%u lines)",
470 out_byte_cur, lines_out );
471
472cleanup:
473
474 if( NULL != buffer_out ) {
475 maug_munlock( buffer_out_h, buffer_out );
476 }
477
478 return retval;
479}
480
481/* === */
482
483MERROR_RETVAL mfmt_read_bmp_header(
484 struct MFMT_STRUCT* header, mfile_t* p_file_in,
485 off_t file_offset, off_t file_sz, uint8_t* p_flags
486) {
487 MERROR_RETVAL retval = MERROR_OK;
488 struct MFMT_STRUCT_BMPINFO* header_bmp_info = NULL;
489 struct MFMT_STRUCT_BMPFILE* header_bmp_file = NULL;
490 uint32_t file_hdr_sz = 0;
491 off_t header_offset = 0;
492
493 mfmt_bmp_check_header();
494
495 if( NULL != header_bmp_file ) {
496 header_offset = 14; /* Size of info header. */
497
498 debug_printf( MFMT_TRACE_BMP_LVL,
499 "reading bitmap file header at " OFF_T_FMT " bytes...",
500 file_offset + 2 );
501
502 /* Grab file header info. */
503 retval = p_file_in->seek( p_file_in, file_offset + 2 );
504 maug_cleanup_if_not_ok();
505 retval = p_file_in->read_int( p_file_in,
506 (uint8_t*)&(header_bmp_file->file_sz), 4, MFILE_READ_FLAG_LSBF );
507 maug_cleanup_if_not_ok();
508
509 retval = p_file_in->seek( p_file_in, file_offset + 10 );
510 maug_cleanup_if_not_ok();
511 retval = p_file_in->read_int( p_file_in,
512 (uint8_t*)&(header_bmp_file->px_offset), 4, MFILE_READ_FLAG_LSBF );
513 maug_cleanup_if_not_ok();
514
515 debug_printf( MFMT_TRACE_BMP_LVL,
516 "bitmap file " U32_FMT " bytes long, px at "
517 U32_FMT " bytes",
518 header_bmp_file->file_sz,
519 header_bmp_file->px_offset );
520 }
521
522 /* Read the bitmap image header. */
523 retval = p_file_in->seek( p_file_in, file_offset + header_offset );
524 maug_cleanup_if_not_ok();
525 retval = p_file_in->read_int( p_file_in,
526 (uint8_t*)&file_hdr_sz, 4, MFILE_READ_FLAG_LSBF );
527 maug_cleanup_if_not_ok();
528 if( 40 != file_hdr_sz ) { /* Windows BMP. */
529 error_printf( "invalid header size: " U32_FMT, file_hdr_sz );
530 retval = MERROR_FILE;
531 goto cleanup;
532 }
533 debug_printf(
534 MFMT_TRACE_BMP_LVL, "bitmap header is " U32_FMT " bytes",
535 file_hdr_sz );
536
537 if( 40 > file_sz - (file_offset + header_offset) ) {
538 error_printf(
539 "bitmap header overflow! (only " OFF_T_FMT " bytes remain!)",
540 file_sz - (file_offset + header_offset) );
541 retval = MERROR_OVERFLOW;
542 goto cleanup;
543 }
544
545 /* Read bitmap image dimensions. */
546 retval = p_file_in->seek( p_file_in,
547 file_offset + header_offset + MFMT_BMPINFO_OFS_WIDTH );
548 maug_cleanup_if_not_ok();
549 retval = p_file_in->read_int( p_file_in,
550 (uint8_t*)&(header_bmp_info->width), 4, MFILE_READ_FLAG_LSBF );
551 maug_cleanup_if_not_ok();
552
553 retval = p_file_in->seek( p_file_in,
554 file_offset + header_offset + MFMT_BMPINFO_OFS_HEIGHT );
555 maug_cleanup_if_not_ok();
556 retval = p_file_in->read_int( p_file_in,
557 (uint8_t*)&(header_bmp_info->height), 4, MFILE_READ_FLAG_LSBF );
558 maug_cleanup_if_not_ok();
559
560 if( 0 > header_bmp_info->height ) {
561 debug_printf(
562 MFMT_TRACE_BMP_LVL, "bitmap Y coordinate is inverted..." );
563 *p_flags |= MFMT_PX_FLAG_INVERT_Y;
564 }
565
566 retval = p_file_in->seek( p_file_in,
567 file_offset + header_offset + MFMT_BMPINFO_OFS_SZ );
568 maug_cleanup_if_not_ok();
569 retval = p_file_in->read_int( p_file_in,
570 (uint8_t*)&(header_bmp_info->img_sz), 4, MFILE_READ_FLAG_LSBF );
571 maug_cleanup_if_not_ok();
572
573 /* Check that we're a palettized image. */
574 retval = p_file_in->seek( p_file_in,
575 file_offset + header_offset + MFMT_BMPINFO_OFS_BPP );
576 maug_cleanup_if_not_ok();
577 retval = p_file_in->read_int( p_file_in,
578 (uint8_t*)&(header_bmp_info->bpp), 2, MFILE_READ_FLAG_LSBF );
579 maug_cleanup_if_not_ok();
580
581 if( 8 < header_bmp_info->bpp ) {
582 error_printf( "invalid bitmap bpp: %u",
583 header_bmp_info->bpp );
584 retval = MERROR_FILE;
585 goto cleanup;
586 }
587
588 /* Make sure there's no weird compression. */
589 retval = p_file_in->seek( p_file_in,
590 file_offset + header_offset + MFMT_BMPINFO_OFS_COMPRESSION );
591 maug_cleanup_if_not_ok();
592 retval = p_file_in->read_int( p_file_in,
593 (uint8_t*)&(header_bmp_info->compression), 4, MFILE_READ_FLAG_LSBF );
594 maug_cleanup_if_not_ok();
595
596 if(
598 header_bmp_info->compression &&
600 header_bmp_info->compression
601 ) {
602 error_printf( "invalid bitmap compression: " U32_FMT,
603 header_bmp_info->compression );
604 retval = MERROR_FILE;
605 goto cleanup;
606 }
607
608 /* Get the number of palette colors. */
609
610 retval = p_file_in->seek( p_file_in,
611 file_offset + header_offset + MFMT_BMPINFO_OFS_PAL_SZ );
612 maug_cleanup_if_not_ok();
613 retval = p_file_in->read_int( p_file_in,
614 (uint8_t*)&(header_bmp_info->palette_ncolors), 4, MFILE_READ_FLAG_LSBF );
615 maug_cleanup_if_not_ok();
616
617 debug_printf( 2, "bitmap is " S32_FMT " x " S32_FMT
618 ", %u bpp (palette has " U32_FMT " colors)",
619 header_bmp_info->width,
620 header_bmp_info->height,
621 header_bmp_info->bpp,
622 header_bmp_info->palette_ncolors );
623
624cleanup:
625
626 return retval;
627}
628
629/* === */
630
631MERROR_RETVAL mfmt_read_bmp_palette(
632 struct MFMT_STRUCT* header, uint32_t* palette, size_t palette_sz,
633 mfile_t* p_file_in, uint32_t file_offset, off_t file_sz, uint8_t flags
634) {
635 MERROR_RETVAL retval = MERROR_OK;
636 struct MFMT_STRUCT_BMPINFO* header_bmp_info = NULL;
637 struct MFMT_STRUCT_BMPFILE* header_bmp_file = NULL;
638 off_t i = 0;
639
640 mfmt_bmp_check_header();
641
642 retval = p_file_in->seek( p_file_in, file_offset );
643 maug_cleanup_if_not_ok();
644 for( i = 0 ; header_bmp_info->palette_ncolors > i ; i++ ) {
645 if( i * 4 > palette_sz ) {
646 error_printf( "palette overflow!" );
647 retval = MERROR_OVERFLOW;
648 goto cleanup;
649 }
650
651 retval = p_file_in->read_int( p_file_in,
652 (uint8_t*)&(palette[i]), 4, MFILE_READ_FLAG_LSBF );
653 maug_cleanup_if_not_ok();
654
655 debug_printf( MFMT_TRACE_BMP_LVL,
656 "set palette entry " OFF_T_FMT " to " X32_FMT,
657 i, palette[i] );
658 }
659
660cleanup:
661
662 return retval;
663}
664
665/* === */
666
668 struct MFMT_STRUCT_BMPINFO* header_bmp_info, mfile_t* p_bmp_in,
669 size_t px_offset, MAUG_MHANDLE* p_px_h
670) {
671 MERROR_RETVAL retval = MERROR_OK;
672
673 /* Check header for validation and info on how to decode pixels. */
674
675 if( 0 == header_bmp_info->height ) {
676 error_printf( "bitmap height is 0!" );
677 retval = MERROR_FILE;
678 goto cleanup;
679 }
680
681 if( 0 == header_bmp_info->width ) {
682 error_printf( "bitmap width is 0!" );
683 retval = MERROR_FILE;
684 goto cleanup;
685 }
686
687 if( 0 == header_bmp_info->bpp ) {
688 error_printf( "bitmap BPP is 0!" );
689 retval = MERROR_FILE;
690 goto cleanup;
691 }
692
693 if( 8 < header_bmp_info->bpp ) {
694 error_printf( ">8BPP bitmaps not supported!" );
695 retval = MERROR_FILE;
696 goto cleanup;
697 }
698
699 if(
700 MFMT_BMP_COMPRESSION_RLE4 == header_bmp_info->compression
701 ) {
702 debug_printf( 1, "allocating decompression buffer..." );
703
704 /* Create a temporary memory buffer and decompress into it. */
705 *p_px_h = maug_malloc(
706 header_bmp_info->width,
707 header_bmp_info->height );
708 maug_cleanup_if_null_alloc( MAUG_MHANDLE, *p_px_h );
709
710 retval = mfmt_decode_rle(
711 p_bmp_in, px_offset, header_bmp_info->img_sz,
712 header_bmp_info->width,
713 *p_px_h,
714 header_bmp_info->width * header_bmp_info->height,
715 MFMT_DECOMP_FLAG_4BIT );
716 maug_cleanup_if_not_ok();
717 }
718
719cleanup:
720
721 return retval;
722}
723
724/* === */
725
727 struct MFMT_STRUCT* header,
728 mfile_t* p_file_in, uint32_t px_offset, off_t file_sz, uint8_t flags,
729 mfmt_read_1px_cb px_cb, void* px_cb_data
730) {
731 MERROR_RETVAL retval = MERROR_OK;
732 struct MFMT_STRUCT_BMPINFO* header_bmp_info = NULL;
733 struct MFMT_STRUCT_BMPFILE* header_bmp_file = NULL;
734 int32_t x = 0,
735 y = 0,
736 width_mod_4 = 0;
737 off_t i = 0,
738 byte_in_idx = 0,
739 bit_idx = 0;
740 uint8_t byte_buffer = 0,
741 byte_mask = 0,
742 pixel_buffer = 0;
743 MAUG_MHANDLE decomp_buffer_h = (MAUG_MHANDLE)NULL;
744 mfile_t file_decomp;
745 mfile_t *p_file_bmp = p_file_in;
746
747 mfmt_bmp_check_header();
748
749 maug_mzero( &file_decomp, sizeof( mfile_t ) );
750
751 retval = mfmt_get_px_ptr(
752 header_bmp_info, p_file_in, px_offset, &decomp_buffer_h );
753 maug_cleanup_if_not_ok();
754
755 if( (MAUG_MHANDLE)NULL != decomp_buffer_h ) {
756 retval = mfile_lock_buffer(
757 decomp_buffer_h,
758 header_bmp_info->width * header_bmp_info->height,
759 &file_decomp );
760 maug_cleanup_if_not_ok();
761
762 /* Switch out the file used below for the decomp buffer mfile_t. */
763 p_file_bmp = &file_decomp;
764 }
765
766 /* TODO: Handle padding for non-conforming images. */
767 width_mod_4 = header_bmp_info->width % 4;
768 maug_cleanup_if_ne( (int32_t)0, width_mod_4, S32_FMT, MERROR_GUI );
769
770 y = header_bmp_info->height - 1;
771 retval = px_cb(
772 px_cb_data, 0, x, y, header_bmp_info, flags | MFMT_PX_FLAG_NEW_LINE );
773 maug_cleanup_if_not_ok();
774 if( p_file_bmp == p_file_in ) {
775 /* Only seek to offset if we're using the original (not translated from
776 * RLE or something.
777 */
778 p_file_bmp->seek( p_file_bmp, px_offset );
779 }
780 while( 0 <= y ) {
781 /* Each iteration is a single, fresh pixel. */
782 pixel_buffer = 0;
783
784 debug_printf( MFMT_TRACE_BMP_LVL,
785 "byte in: " OFF_T_FMT " (" OFF_T_FMT
786 "), bit " OFF_T_FMT ", y: " U32_FMT ", x: " U32_FMT ")",
787 byte_in_idx, file_sz, bit_idx, y, x );
788
789 /* Byte finished check. */
790 if( 0 == bit_idx ) {
791 if( byte_in_idx >= file_sz ) {
792 /* TODO: Figure out why ICO parser messes up size. */
793 error_printf(
794 "input bitmap has insufficient size " OFF_T_FMT " bytes)!",
795 file_sz );
796 /* retval = MERROR_OVERFLOW;
797 goto cleanup; */
798 }
799
800 /* Move on to a new byte. */
801 /* TODO: Bad cursor? */
802 retval = p_file_bmp->read_byte( p_file_bmp, &byte_buffer );
803 maug_cleanup_if_not_ok();
804 byte_in_idx++;
805
806 /* Start at 8 bits from the right (0 from the left). */
807 bit_idx = 8;
808
809 /* Build a bitwise mask based on the bitmap's BPP. */
810 byte_mask = 0;
811 for( i = 0 ; header_bmp_info->bpp > i ; i++ ) {
812 byte_mask >>= 1;
813 byte_mask |= 0x80;
814 }
815 }
816
817 /* Use the byte mask to place the bits for this pixel in the
818 * pixel buffer.
819 */
820 pixel_buffer |= byte_buffer & byte_mask;
821
822 /* Shift the pixel buffer so the index lines up at the first bit. */
823 pixel_buffer >>=
824 /* Index starts from the right, so the current bits from the left
825 * minus 1 * bpp.
826 */
827 (bit_idx - header_bmp_info->bpp);
828 debug_printf( MFMT_TRACE_BMP_LVL,
829 "byte_mask: 0x%02x, bit_idx: " OFF_T_FMT
830 ", pixel_buffer: 0x%02x",
831 byte_mask, bit_idx, pixel_buffer );
832
833 /* Place the pixel buffer at the X/Y in the grid. */
834 retval = px_cb( px_cb_data, pixel_buffer, x, y, header_bmp_info, flags );
835 maug_cleanup_if_not_ok();
836
837 /* Increment the bits position byte mask by the bpp so it's pointing
838 * to the next pixel in the bitmap for the next go around.
839 */
840 byte_mask >>= header_bmp_info->bpp;
841 bit_idx -= header_bmp_info->bpp;
842
843 /* Move to the next pixel. */
844 x++;
845 if( x >= header_bmp_info->width ) {
846 /* Move to the next row of the input. */
847 y--;
848 x = 0;
849 while( byte_in_idx % 4 != 0 ) {
850 byte_in_idx++;
851 p_file_bmp->seek( p_file_bmp, px_offset + byte_in_idx );
852 }
853
854 /* Move to the next row of the output. */
855 retval = px_cb(
856 px_cb_data, 0, x, y, header_bmp_info,
857 flags | MFMT_PX_FLAG_NEW_LINE );
858 maug_cleanup_if_not_ok();
859
860 /* TODO Get past the padding. */
861
862 }
863 }
864
865cleanup:
866
867 mfile_close( &file_decomp );
868 /* decomp_buffer_h = file_decomp.h.mem; */
869
870 if( (MAUG_MHANDLE)NULL != decomp_buffer_h ) {
871 debug_printf( 1, "freeing decomp buffer %p...", decomp_buffer_h );
872 maug_mfree( decomp_buffer_h );
873 }
874
875 return retval;
876}
877
878/* === */
879
880static MERROR_RETVAL _mfmt_read_bmp_px_cb_px(
881 void* data, uint8_t px, int32_t x, int32_t y,
882 void* header_info, uint8_t flags
883) {
884 MERROR_RETVAL retval = MERROR_OK;
885 struct MFMT_PX_OUT_STAT* p_out = (struct MFMT_PX_OUT_STAT*)data;
886 struct MFMT_STRUCT_BMPINFO* header_bmp_info =
887 (struct MFMT_STRUCT_BMPINFO*)header_info;
888
889 /* Buffer bounds check. */
890 if( p_out->px_sz < p_out->byte_idx ) {
891 error_printf(
892 "byte " OFF_T_FMT " outside of " OFF_T_FMT
893 " pixel buffer!", p_out->byte_idx, p_out->px_sz );
894 retval = MERROR_OVERFLOW;
895 goto cleanup;
896 }
897
899 /* Set the output index to a new line. */
900 debug_printf( MFMT_TRACE_BMP_LVL,
901 "new row starting at byte_out_idx: " OFF_T_FMT,
902 p_out->byte_idx );
903 p_out->byte_idx =
904 (MFMT_PX_FLAG_INVERT_Y == (MFMT_PX_FLAG_INVERT_Y & flags) ?
905 ((header_bmp_info->height - y - 1) *
906 header_bmp_info->width) :
907 ((y) * header_bmp_info->width));
908
909 } else {
910 debug_printf( MFMT_TRACE_BMP_LVL,
911 "writing byte " OFF_T_FMT " (x: " S32_FMT
912 ", y: " S32_FMT ")",
913 p_out->byte_idx, x, y );
914 p_out->px[p_out->byte_idx] = px;
915 p_out->byte_idx++;
916 }
917
918cleanup:
919
920 return retval;
921}
922
923/* === */
924
926 struct MFMT_STRUCT* header, uint8_t SEG_FAR* px, off_t px_sz,
927 mfile_t* p_file_in, uint32_t px_offset, off_t file_sz, uint8_t flags
928) {
929 MERROR_RETVAL retval = MERROR_OK;
930 struct MFMT_PX_OUT_STAT out;
931
932 maug_mzero( &out, sizeof( struct MFMT_PX_OUT_STAT ) );
933 out.px = px;
934 out.px_sz = px_sz;
935
936 retval = mfmt_read_bmp_px_cb(
937 header, p_file_in, px_offset, file_sz, flags,
938 _mfmt_read_bmp_px_cb_px, &out );
939
940 return retval;
941}
942
943#endif /* MFMT_C */
944 /* maug_fmt */
946
947#endif /* !MFMT_H */
948
int MERROR_RETVAL
Return type indicating function returns a value from this list.
Definition merror.h:19
#define MFMT_BMP_COMPRESSION_NONE
MFMT_STRUCT_BMPINFO::compression value indicating none.
Definition mfmt.h:34
#define MFMT_BMP_COMPRESSION_RLE4
MFMT_STRUCT_BMPINFO::compression value indicating 4-bit RLE.
Definition mfmt.h:38
MERROR_RETVAL(* mfmt_read_1px_cb)(void *data, uint8_t px, int32_t x, int32_t y, void *header_info, uint8_t flags)
Parameter for mfmt_read_px_cb() functions to process individual pixel data.
Definition mfmt.h:185
MERROR_RETVAL mfmt_read_bmp_px(struct MFMT_STRUCT *header, uint8_t SEG_FAR *px, off_t px_sz, mfile_t *p_file_in, uint32_t file_offset, off_t file_sz, uint8_t flags)
Read mfmt_bitmap pixels into an 8-bit memory bitmap.
MERROR_RETVAL mfmt_get_px_ptr(struct MFMT_STRUCT_BMPINFO *header_bmp_info, mfile_t *p_bmp_in, size_t px_offset, MAUG_MHANDLE *p_px_h)
Read mfmt_bitmap header and return a handle to the bitmap's decompressed pixel data if it is compress...
MERROR_RETVAL(* mfmt_read_header_cb)(struct MFMT_STRUCT *header, mfile_t *p_file_in, uint32_t file_offset, off_t file_sz, uint8_t *p_flags)
Callback to read image header and get properties.
Definition mfmt.h:161
MERROR_RETVAL mfmt_decode_rle(mfile_t *p_file_in, off_t file_offset, off_t file_sz, size_t line_w, MAUG_MHANDLE buffer_out, off_t buffer_out_sz, uint8_t flags)
Decode RLE-encoded data from an input file into a memory buffer.
MERROR_RETVAL(* mfmt_read_px_cb)(struct MFMT_STRUCT *header, uint8_t SEG_FAR *px, off_t px_sz, mfile_t *p_file_in, uint32_t file_offset, off_t file_sz, uint8_t flags)
Callback to read image pixels into 8-bit values.
Definition mfmt.h:176
MERROR_RETVAL(* mfmt_read_palette_cb)(struct MFMT_STRUCT *header, uint32_t *palette, size_t palette_sz, mfile_t *p_file_in, uint32_t file_offset, off_t file_sz, uint8_t flags)
Callback to read image palette into 24-bit RGB values.
Definition mfmt.h:168
MERROR_RETVAL(* mfmt_decode)(mfile_t *p_file_in, off_t file_offset, off_t file_sz, size_t line_w, MAUG_MHANDLE buffer_out, off_t buffer_out_sz, uint8_t flags)
Callback to decode compressed data.
Definition mfmt.h:154
MERROR_RETVAL mfmt_read_bmp_px_cb(struct MFMT_STRUCT *header, mfile_t *p_file_in, uint32_t px_offset, off_t file_sz, uint8_t flags, mfmt_read_1px_cb px_cb, void *px_cb_data)
Read mfmt_bitmap pixels and process them using a callback.
#define MFMT_PX_FLAG_NEW_LINE
Flag for mfmt_read_bmp_px_t() indicating a new line has started.
Definition mfmt.h:52
#define MFILE_READ_FLAG_LSBF
Flag for mfile_read_int_t() indicating integer should always be read least significant byte first.
Definition mfile.h:74
void mfile_close(mfile_t *p_file)
Close a file opened with mfile_open_read().
MERROR_RETVAL mfile_lock_buffer(MAUG_MHANDLE, off_t, mfile_t *p_file)
Lock a buffer and assign it to an mfile_t to read/write.
Utility struct for mfmt_read_bmp_px() to keep track out of output bitmap info.
Definition mfmt.h:137
Definition mfmt.h:124
BITMAPINFO struct that comes before Windows bitmap data.
Definition mfmt.h:99
uint32_t compression
Type of compression used.
Definition mfmt.h:111
uint32_t vres
Vertical resolution in pixels per inch (unsupported).
Definition mfmt.h:117
uint16_t bpp
Number of bits per pixel (only =<8 are supported).
Definition mfmt.h:109
uint16_t color_planes
Number of color planes (only 0 or 1 are supported).
Definition mfmt.h:107
uint32_t sz
Size of this struct in bytes (only 40 is supported).
Definition mfmt.h:101
uint32_t imp_colors
Definition mfmt.h:121
uint32_t img_sz
Size of pixel data in bytes.
Definition mfmt.h:113
uint32_t hres
Horizontal resolution in pixels per inch (unsupported).
Definition mfmt.h:115
uint32_t palette_ncolors
Number of palette colors in this bitmap (<256 supported).
Definition mfmt.h:119
int32_t width
Width of the bitmap in pixels.
Definition mfmt.h:103
int32_t height
Height of the bitmap in pixels.
Definition mfmt.h:105
Generic image description struct.
Definition mfmt.h:69
uint32_t sz
Size of this struct (use to tell apart).
Definition mfmt.h:71