• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 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_BMP_CFX_BMPDECOMPRESSOR_H_
8 #define CORE_FXCODEC_BMP_CFX_BMPDECOMPRESSOR_H_
9 
10 #include <vector>
11 
12 #include "core/fxcodec/bmp/bmpmodule.h"
13 #include "core/fxcodec/bmp/fx_bmp.h"
14 #include "core/fxcrt/retain_ptr.h"
15 #include "core/fxcrt/unowned_ptr.h"
16 
17 class CFX_CodecMemory;
18 
19 namespace fxcodec {
20 
21 class CFX_BmpContext;
22 
23 class CFX_BmpDecompressor {
24  public:
25   explicit CFX_BmpDecompressor(CFX_BmpContext* context);
26   ~CFX_BmpDecompressor();
27 
28   BmpModule::Status DecodeImage();
29   BmpModule::Status ReadHeader();
30   void SetInputBuffer(RetainPtr<CFX_CodecMemory> codec_memory);
31   FX_FILESIZE GetAvailInput() const;
32 
palette()33   const std::vector<uint32_t>* palette() const { return &palette_; }
width()34   uint32_t width() const { return width_; }
height()35   uint32_t height() const { return height_; }
components()36   int32_t components() const { return components_; }
img_tb_flag()37   bool img_tb_flag() const { return img_tb_flag_; }
pal_num()38   int32_t pal_num() const { return pal_num_; }
dpi_x()39   int32_t dpi_x() const { return dpi_x_; }
dpi_y()40   int32_t dpi_y() const { return dpi_y_; }
41 
42  private:
43   enum class DecodeStatus : uint8_t {
44     kHeader,
45     kPal,
46     kDataPre,
47     kData,
48     kTail,
49   };
50 
51   BmpModule::Status ReadBmpHeader();
52   BmpModule::Status ReadBmpHeaderIfh();
53   BmpModule::Status ReadBmpHeaderDimensions();
54   BmpModule::Status ReadBmpBitfields();
55   BmpModule::Status ReadBmpPalette();
56   bool GetDataPosition(uint32_t cur_pos);
57   void ReadNextScanline();
58   BmpModule::Status DecodeRGB();
59   BmpModule::Status DecodeRLE8();
60   BmpModule::Status DecodeRLE4();
61   bool ReadData(uint8_t* destination, uint32_t size);
62   void SaveDecodingStatus(DecodeStatus status);
63   bool ValidateColorIndex(uint8_t val) const;
64   bool ValidateFlag() const;
65   bool SetHeight(int32_t signed_height);
66 
67   UnownedPtr<CFX_BmpContext> const context_;
68   std::vector<uint8_t> out_row_buffer_;
69   std::vector<uint32_t> palette_;
70   uint32_t header_offset_ = 0;
71   uint32_t width_ = 0;
72   uint32_t height_ = 0;
73   uint32_t compress_flag_ = 0;
74   int32_t components_ = 0;
75   size_t src_row_bytes_ = 0;
76   size_t out_row_bytes_ = 0;
77   bool img_tb_flag_ = false;
78   uint16_t bit_counts_ = 0;
79   uint32_t color_used_ = 0;
80   int32_t pal_num_ = 0;
81   int32_t pal_type_ = 0;
82   uint32_t data_size_ = 0;
83   uint32_t img_ifh_size_ = 0;
84   uint32_t row_num_ = 0;
85   uint32_t col_num_ = 0;
86   int32_t dpi_x_ = 0;
87   int32_t dpi_y_ = 0;
88   uint32_t mask_red_ = 0;
89   uint32_t mask_green_ = 0;
90   uint32_t mask_blue_ = 0;
91   DecodeStatus decode_status_ = DecodeStatus::kHeader;
92   RetainPtr<CFX_CodecMemory> input_buffer_;
93 };
94 
95 }  // namespace fxcodec
96 
97 #endif  // CORE_FXCODEC_BMP_CFX_BMPDECOMPRESSOR_H_
98