1 // Copyright 2014 PDFium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 6 7 #include <setjmp.h> 8 9 #include "core/include/fxcrt/fx_basic.h" 10 11 #define BMP_SUPPORT_BITFIELD 12 #define BMP_WIDTHBYTES(width, bitCount) ((width * bitCount) + 31) / 32 * 4 13 #define BMP_PAL_ENCODE(a, r, g, b) \ 14 (((FX_DWORD)(a) << 24) | ((r) << 16) | ((g) << 8) | (b)) 15 #define BMP_D_STATUS_HEADER 0x01 16 #define BMP_D_STATUS_PAL 0x02 17 #define BMP_D_STATUS_DATA_PRE 0x03 18 #define BMP_D_STATUS_DATA 0x04 19 #define BMP_D_STATUS_TAIL 0x00 20 #define BMP_SIGNATURE 0x4D42 21 #define BMP_PAL_NEW 0 22 #define BMP_PAL_OLD 1 23 #define RLE_MARKER 0 24 #define RLE_EOL 0 25 #define RLE_EOI 1 26 #define RLE_DELTA 2 27 #define BMP_RGB 0L 28 #define BMP_RLE8 1L 29 #define BMP_RLE4 2L 30 #define BMP_BITFIELDS 3L 31 #define BMP_BIT_555 0 32 #define BMP_BIT_565 1 33 #define BMP_MAX_ERROR_SIZE 256 34 #pragma pack(1) 35 typedef struct tagBmpFileHeader { 36 FX_WORD bfType; 37 FX_DWORD bfSize; 38 FX_WORD bfReserved1; 39 FX_WORD bfReserved2; 40 FX_DWORD bfOffBits; 41 } BmpFileHeader, *BmpFileHeaderPtr; 42 typedef struct tagBmpCoreHeader { 43 FX_DWORD bcSize; 44 FX_WORD bcWidth; 45 FX_WORD bcHeight; 46 FX_WORD bcPlanes; 47 FX_WORD bcBitCount; 48 } BmpCoreHeader, *BmpCoreHeaderPtr; 49 typedef struct tagBmpInfoHeader { 50 FX_DWORD biSize; 51 int32_t biWidth; 52 int32_t biHeight; 53 FX_WORD biPlanes; 54 FX_WORD biBitCount; 55 FX_DWORD biCompression; 56 FX_DWORD biSizeImage; 57 int32_t biXPelsPerMeter; 58 int32_t biYPelsPerMeter; 59 FX_DWORD biClrUsed; 60 FX_DWORD biClrImportant; 61 } BmpInfoHeader, *BmpInfoHeaderPtr; 62 #pragma pack() 63 typedef struct tag_bmp_decompress_struct bmp_decompress_struct; 64 typedef bmp_decompress_struct* bmp_decompress_struct_p; 65 typedef bmp_decompress_struct_p* bmp_decompress_struct_pp; 66 struct tag_bmp_decompress_struct { 67 jmp_buf jmpbuf; 68 FX_CHAR* err_ptr; 69 void (*_bmp_error_fn)(bmp_decompress_struct_p gif_ptr, 70 const FX_CHAR* err_msg); 71 72 void* context_ptr; 73 74 BmpFileHeaderPtr bmp_header_ptr; 75 BmpInfoHeaderPtr bmp_infoheader_ptr; 76 int32_t width; 77 int32_t height; 78 FX_DWORD compress_flag; 79 int32_t components; 80 int32_t src_row_bytes; 81 int32_t out_row_bytes; 82 uint8_t* out_row_buffer; 83 FX_WORD bitCounts; 84 FX_DWORD color_used; 85 FX_BOOL imgTB_flag; 86 int32_t pal_num; 87 int32_t pal_type; 88 FX_DWORD* pal_ptr; 89 FX_DWORD data_size; 90 FX_DWORD img_data_offset; 91 FX_DWORD img_ifh_size; 92 int32_t row_num; 93 int32_t col_num; 94 int32_t dpi_x; 95 int32_t dpi_y; 96 #ifdef BMP_SUPPORT_BITFIELD 97 FX_DWORD mask_red; 98 FX_DWORD mask_green; 99 FX_DWORD mask_blue; 100 #endif 101 102 FX_BOOL (*_bmp_get_data_position_fn)(bmp_decompress_struct_p bmp_ptr, FX_DWORD cur_pos); 103 void (*_bmp_get_row_fn)(bmp_decompress_struct_p bmp_ptr, 104 int32_t row_num, 105 uint8_t* row_buf); 106 uint8_t* next_in; 107 FX_DWORD avail_in; 108 FX_DWORD skip_size; 109 int32_t decode_status; 110 }; 111 void _bmp_error(bmp_decompress_struct_p bmp_ptr, const FX_CHAR* err_msg); 112 bmp_decompress_struct_p _bmp_create_decompress(); 113 void _bmp_destroy_decompress(bmp_decompress_struct_pp bmp_ptr_ptr); 114 int32_t _bmp_read_header(bmp_decompress_struct_p bmp_ptr); 115 int32_t _bmp_decode_image(bmp_decompress_struct_p bmp_ptr); 116 int32_t _bmp_decode_rgb(bmp_decompress_struct_p bmp_ptr); 117 int32_t _bmp_decode_rle8(bmp_decompress_struct_p bmp_ptr); 118 int32_t _bmp_decode_rle4(bmp_decompress_struct_p bmp_ptr); 119 uint8_t* _bmp_read_data(bmp_decompress_struct_p bmp_ptr, 120 uint8_t** des_buf_pp, 121 FX_DWORD data_size); 122 void _bmp_save_decoding_status(bmp_decompress_struct_p bmp_ptr, int32_t status); 123 void _bmp_input_buffer(bmp_decompress_struct_p bmp_ptr, 124 uint8_t* src_buf, 125 FX_DWORD src_size); 126 FX_DWORD _bmp_get_avail_input(bmp_decompress_struct_p bmp_ptr, 127 uint8_t** avial_buf_ptr); 128 #define BMP_PTR_NOT_NULL(ptr, bmp_ptr) \ 129 if (ptr == NULL) { \ 130 _bmp_error(bmp_ptr, "Out Of Memory"); \ 131 return 0; \ 132 } 133 typedef struct tag_bmp_compress_struct bmp_compress_struct; 134 typedef bmp_compress_struct* bmp_compress_struct_p; 135 typedef bmp_compress_struct_p* bmp_compress_struct_pp; 136 struct tag_bmp_compress_struct { 137 BmpFileHeader file_header; 138 BmpInfoHeader info_header; 139 uint8_t* src_buf; 140 FX_DWORD src_pitch; 141 FX_DWORD src_row; 142 uint8_t src_bpp; 143 FX_DWORD src_width; 144 FX_BOOL src_free; 145 FX_DWORD* pal_ptr; 146 FX_WORD pal_num; 147 #ifdef BMP_SUPPORT_BITFIELD 148 uint8_t bit_type; 149 #endif 150 }; 151 bmp_compress_struct_p _bmp_create_compress(); 152 void _bmp_destroy_compress(bmp_compress_struct_p bmp_ptr); 153 FX_BOOL _bmp_encode_image(bmp_compress_struct_p bmp_ptr, 154 uint8_t*& dst_buf, 155 FX_DWORD& dst_size); 156