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 #ifndef CORE_FXCODEC_LBMP_FX_BMP_H_ 8 #define CORE_FXCODEC_LBMP_FX_BMP_H_ 9 10 #include <setjmp.h> 11 12 #include "core/fxcrt/fx_basic.h" 13 14 #define BMP_WIDTHBYTES(width, bitCount) ((width * bitCount) + 31) / 32 * 4 15 #define BMP_PAL_ENCODE(a, r, g, b) \ 16 (((uint32_t)(a) << 24) | ((r) << 16) | ((g) << 8) | (b)) 17 #define BMP_D_STATUS_HEADER 0x01 18 #define BMP_D_STATUS_PAL 0x02 19 #define BMP_D_STATUS_DATA_PRE 0x03 20 #define BMP_D_STATUS_DATA 0x04 21 #define BMP_D_STATUS_TAIL 0x00 22 #define BMP_SIGNATURE 0x4D42 23 #define BMP_PAL_NEW 0 24 #define BMP_PAL_OLD 1 25 #define RLE_MARKER 0 26 #define RLE_EOL 0 27 #define RLE_EOI 1 28 #define RLE_DELTA 2 29 #define BMP_RGB 0L 30 #define BMP_RLE8 1L 31 #define BMP_RLE4 2L 32 #define BMP_BITFIELDS 3L 33 #define BMP_BIT_555 0 34 #define BMP_BIT_565 1 35 #define BMP_MAX_ERROR_SIZE 256 36 #pragma pack(1) 37 typedef struct tagBmpFileHeader { 38 uint16_t bfType; 39 uint32_t bfSize; 40 uint16_t bfReserved1; 41 uint16_t bfReserved2; 42 uint32_t bfOffBits; 43 } BmpFileHeader, *BmpFileHeaderPtr; 44 typedef struct tagBmpCoreHeader { 45 uint32_t bcSize; 46 uint16_t bcWidth; 47 uint16_t bcHeight; 48 uint16_t bcPlanes; 49 uint16_t bcBitCount; 50 } BmpCoreHeader, *BmpCoreHeaderPtr; 51 typedef struct tagBmpInfoHeader { 52 uint32_t biSize; 53 int32_t biWidth; 54 int32_t biHeight; 55 uint16_t biPlanes; 56 uint16_t biBitCount; 57 uint32_t biCompression; 58 uint32_t biSizeImage; 59 int32_t biXPelsPerMeter; 60 int32_t biYPelsPerMeter; 61 uint32_t biClrUsed; 62 uint32_t biClrImportant; 63 } BmpInfoHeader, *BmpInfoHeaderPtr; 64 #pragma pack() 65 66 typedef struct tag_bmp_decompress_struct bmp_decompress_struct; 67 typedef bmp_decompress_struct* bmp_decompress_struct_p; 68 typedef bmp_decompress_struct_p* bmp_decompress_struct_pp; 69 struct tag_bmp_decompress_struct { 70 jmp_buf jmpbuf; 71 FX_CHAR* err_ptr; 72 void (*bmp_error_fn)(bmp_decompress_struct_p gif_ptr, const FX_CHAR* err_msg); 73 74 void* context_ptr; 75 76 BmpFileHeaderPtr bmp_header_ptr; 77 BmpInfoHeaderPtr bmp_infoheader_ptr; 78 int32_t width; 79 int32_t height; 80 uint32_t compress_flag; 81 int32_t components; 82 int32_t src_row_bytes; 83 int32_t out_row_bytes; 84 uint8_t* out_row_buffer; 85 uint16_t bitCounts; 86 uint32_t color_used; 87 bool imgTB_flag; 88 int32_t pal_num; 89 int32_t pal_type; 90 uint32_t* pal_ptr; 91 uint32_t data_size; 92 uint32_t img_data_offset; 93 uint32_t img_ifh_size; 94 int32_t row_num; 95 int32_t col_num; 96 int32_t dpi_x; 97 int32_t dpi_y; 98 uint32_t mask_red; 99 uint32_t mask_green; 100 uint32_t mask_blue; 101 102 bool (*bmp_get_data_position_fn)(bmp_decompress_struct_p bmp_ptr, 103 uint32_t cur_pos); 104 void (*bmp_get_row_fn)(bmp_decompress_struct_p bmp_ptr, 105 int32_t row_num, 106 uint8_t* row_buf); 107 uint8_t* next_in; 108 uint32_t avail_in; 109 uint32_t skip_size; 110 int32_t decode_status; 111 }; 112 void bmp_error(bmp_decompress_struct_p bmp_ptr, const FX_CHAR* err_msg); 113 bmp_decompress_struct_p bmp_create_decompress(); 114 void bmp_destroy_decompress(bmp_decompress_struct_pp bmp_ptr_ptr); 115 int32_t bmp_read_header(bmp_decompress_struct_p bmp_ptr); 116 int32_t bmp_decode_image(bmp_decompress_struct_p bmp_ptr); 117 int32_t bmp_decode_rgb(bmp_decompress_struct_p bmp_ptr); 118 int32_t bmp_decode_rle8(bmp_decompress_struct_p bmp_ptr); 119 int32_t bmp_decode_rle4(bmp_decompress_struct_p bmp_ptr); 120 uint8_t* bmp_read_data(bmp_decompress_struct_p bmp_ptr, 121 uint8_t** des_buf_pp, 122 uint32_t data_size); 123 void bmp_save_decoding_status(bmp_decompress_struct_p bmp_ptr, int32_t status); 124 void bmp_input_buffer(bmp_decompress_struct_p bmp_ptr, 125 uint8_t* src_buf, 126 uint32_t src_size); 127 uint32_t bmp_get_avail_input(bmp_decompress_struct_p bmp_ptr, 128 uint8_t** avail_buf_ptr); 129 typedef struct tag_bmp_compress_struct bmp_compress_struct; 130 typedef bmp_compress_struct* bmp_compress_struct_p; 131 typedef bmp_compress_struct_p* bmp_compress_struct_pp; 132 struct tag_bmp_compress_struct { 133 BmpFileHeader file_header; 134 BmpInfoHeader info_header; 135 uint8_t* src_buf; 136 uint32_t src_pitch; 137 uint32_t src_row; 138 uint8_t src_bpp; 139 uint32_t src_width; 140 bool src_free; 141 uint32_t* pal_ptr; 142 uint16_t pal_num; 143 uint8_t bit_type; 144 }; 145 146 bmp_compress_struct_p bmp_create_compress(); 147 void bmp_destroy_compress(bmp_compress_struct_p bmp_ptr); 148 bool bmp_encode_image(bmp_compress_struct_p bmp_ptr, 149 uint8_t*& dst_buf, 150 uint32_t& dst_size); 151 152 uint16_t GetWord_LSBFirst(uint8_t* p); 153 void SetWord_LSBFirst(uint8_t* p, uint16_t v); 154 155 #endif // CORE_FXCODEC_LBMP_FX_BMP_H_ 156