• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <memory>
13 #include <vector>
14 
15 #include "core/fxcodec/codec/ccodec_bmpmodule.h"
16 
17 #define BMP_WIDTHBYTES(width, bitCount) ((width * bitCount) + 31) / 32 * 4
18 #define BMP_PAL_ENCODE(a, r, g, b) \
19   (((uint32_t)(a) << 24) | ((r) << 16) | ((g) << 8) | (b))
20 #define BMP_D_STATUS_HEADER 0x01
21 #define BMP_D_STATUS_PAL 0x02
22 #define BMP_D_STATUS_DATA_PRE 0x03
23 #define BMP_D_STATUS_DATA 0x04
24 #define BMP_D_STATUS_TAIL 0x00
25 #define BMP_SIGNATURE 0x4D42
26 #define BMP_PAL_OLD 1
27 #define RLE_MARKER 0
28 #define RLE_EOL 0
29 #define RLE_EOI 1
30 #define RLE_DELTA 2
31 #define BMP_RGB 0L
32 #define BMP_RLE8 1L
33 #define BMP_RLE4 2L
34 #define BMP_BITFIELDS 3L
35 // Limit width to (MAXINT32 - 31) / 32
36 #define BMP_MAX_WIDTH 67108863
37 #pragma pack(1)
38 typedef struct tagBmpFileHeader {
39   uint16_t bfType;
40   uint32_t bfSize;
41   uint16_t bfReserved1;
42   uint16_t bfReserved2;
43   uint32_t bfOffBits;
44 } BmpFileHeader;
45 typedef struct tagBmpCoreHeader {
46   uint32_t bcSize;
47   uint16_t bcWidth;
48   uint16_t bcHeight;
49   uint16_t bcPlanes;
50   uint16_t bcBitCount;
51 } BmpCoreHeader;
52 typedef struct tagBmpInfoHeader {
53   uint32_t biSize;
54   int32_t biWidth;
55   int32_t biHeight;
56   uint16_t biPlanes;
57   uint16_t biBitCount;
58   uint32_t biCompression;
59   uint32_t biSizeImage;
60   int32_t biXPelsPerMeter;
61   int32_t biYPelsPerMeter;
62   uint32_t biClrUsed;
63   uint32_t biClrImportant;
64 } BmpInfoHeader;
65 #pragma pack()
66 
67 class BMPDecompressor {
68  public:
69   BMPDecompressor();
70   ~BMPDecompressor();
71 
72   void Error();
73   int32_t DecodeImage();
74   int32_t ReadHeader();
75   void SetInputBuffer(uint8_t* src_buf, uint32_t src_size);
76   uint32_t GetAvailInput(uint8_t** avail_buf);
77 
78   jmp_buf jmpbuf;
79 
80   void* context_ptr;
81 
82   std::vector<uint8_t> out_row_buffer;
83   std::vector<uint32_t> palette;
84   uint8_t* next_in;
85 
86   uint32_t header_offset;
87   uint32_t width;
88   uint32_t height;
89   uint32_t compress_flag;
90   int32_t components;
91   size_t src_row_bytes;
92   size_t out_row_bytes;
93   uint16_t bitCounts;
94   uint32_t color_used;
95   bool imgTB_flag;
96   int32_t pal_num;
97   int32_t pal_type;
98   uint32_t data_size;
99   uint32_t img_data_offset;
100   uint32_t img_ifh_size;
101   size_t row_num;
102   size_t col_num;
103   int32_t dpi_x;
104   int32_t dpi_y;
105   uint32_t mask_red;
106   uint32_t mask_green;
107   uint32_t mask_blue;
108 
109   uint32_t avail_in;
110   uint32_t skip_size;
111   int32_t decode_status;
112 
113  private:
114   bool GetDataPosition(uint32_t cur_pos);
115   void ReadScanline(uint32_t row_num, const std::vector<uint8_t>& row_buf);
116   int32_t DecodeRGB();
117   int32_t DecodeRLE8();
118   int32_t DecodeRLE4();
119   uint8_t* ReadData(uint8_t** des_buf, uint32_t data_size);
120   void SaveDecodingStatus(int32_t status);
121   bool ValidateColorIndex(uint8_t val);
122   bool ValidateFlag() const;
123   void SetHeight(int32_t signed_height);
124 };
125 
126 class CBmpContext : public CCodec_BmpModule::Context {
127  public:
128   CBmpContext(CCodec_BmpModule* pModule, CCodec_BmpModule::Delegate* pDelegate);
129   ~CBmpContext() override;
130 
131   BMPDecompressor m_Bmp;
132   UnownedPtr<CCodec_BmpModule> const m_pModule;
133   UnownedPtr<CCodec_BmpModule::Delegate> const m_pDelegate;
134 };
135 
136 #endif  // CORE_FXCODEC_LBMP_FX_BMP_H_
137