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 _FX_CODEC_PROGRESS_H_
8 #define _FX_CODEC_PROGRESS_H_
9 #define FXCODEC_BLOCK_SIZE 4096
10 #define FXCODEC_PNG_GAMMA 2.2
11 #if _FX_OS_ == _FX_MACOSX_ || _FX_OS_ == _FX_IOS_
12 #undef FXCODEC_PNG_GAMMA
13 #define FXCODEC_PNG_GAMMA 1.7
14 #endif
15 struct PixelWeight {
16   int m_SrcStart;
17   int m_SrcEnd;
18   int m_Weights[1];
19 };
20 class CFXCODEC_WeightTable {
21  public:
CFXCODEC_WeightTable()22   CFXCODEC_WeightTable() { m_pWeightTables = NULL; }
~CFXCODEC_WeightTable()23   ~CFXCODEC_WeightTable() {
24     if (m_pWeightTables != NULL) {
25       FX_Free(m_pWeightTables);
26     }
27   }
28 
29   void Calc(int dest_len,
30             int dest_min,
31             int dest_max,
32             int src_len,
33             int src_min,
34             int src_max,
35             FX_BOOL bInterpol);
GetPixelWeight(int pixel)36   PixelWeight* GetPixelWeight(int pixel) {
37     return (PixelWeight*)(m_pWeightTables + (pixel - m_DestMin) * m_ItemSize);
38   }
39 
40   int m_DestMin, m_ItemSize;
41   uint8_t* m_pWeightTables;
42 };
43 class CFXCODEC_HorzTable {
44  public:
CFXCODEC_HorzTable()45   CFXCODEC_HorzTable() { m_pWeightTables = NULL; }
~CFXCODEC_HorzTable()46   ~CFXCODEC_HorzTable() {
47     if (m_pWeightTables != NULL) {
48       FX_Free(m_pWeightTables);
49     }
50   }
51 
52   void Calc(int dest_len, int src_len, FX_BOOL bInterpol);
GetPixelWeight(int pixel)53   PixelWeight* GetPixelWeight(int pixel) {
54     return (PixelWeight*)(m_pWeightTables + pixel * m_ItemSize);
55   }
56 
57   int m_ItemSize;
58   uint8_t* m_pWeightTables;
59 };
60 class CFXCODEC_VertTable {
61  public:
CFXCODEC_VertTable()62   CFXCODEC_VertTable() { m_pWeightTables = NULL; }
~CFXCODEC_VertTable()63   ~CFXCODEC_VertTable() {
64     if (m_pWeightTables != NULL) {
65       FX_Free(m_pWeightTables);
66     }
67   }
68   void Calc(int dest_len, int src_len);
GetPixelWeight(int pixel)69   PixelWeight* GetPixelWeight(int pixel) {
70     return (PixelWeight*)(m_pWeightTables + pixel * m_ItemSize);
71   }
72   int m_ItemSize;
73   uint8_t* m_pWeightTables;
74 };
75 enum FXCodec_Format {
76   FXCodec_Invalid = 0,
77   FXCodec_1bppGray = 0x101,
78   FXCodec_1bppRgb = 0x001,
79   FXCodec_8bppGray = 0x108,
80   FXCodec_8bppRgb = 0x008,
81   FXCodec_Rgb = 0x018,
82   FXCodec_Rgb32 = 0x020,
83   FXCodec_Argb = 0x220,
84   FXCodec_Cmyk = 0x120
85 };
86 class CCodec_ProgressiveDecoder : public ICodec_ProgressiveDecoder {
87  public:
88   CCodec_ProgressiveDecoder(CCodec_ModuleMgr* pCodecMgr);
89   ~CCodec_ProgressiveDecoder() override;
90 
91   FXCODEC_STATUS LoadImageInfo(IFX_FileRead* pFile,
92                                FXCODEC_IMAGE_TYPE imageType,
93                                CFX_DIBAttribute* pAttribute) override;
94 
GetType()95   FXCODEC_IMAGE_TYPE GetType() const override { return m_imagType; }
GetWidth()96   int32_t GetWidth() const override { return m_SrcWidth; }
GetHeight()97   int32_t GetHeight() const override { return m_SrcHeight; }
GetNumComponents()98   int32_t GetNumComponents() const override { return m_SrcComponents; }
GetBPC()99   int32_t GetBPC() const override { return m_SrcBPC; }
100   void SetClipBox(FX_RECT* clip) override;
101 
102   FXCODEC_STATUS GetFrames(int32_t& frames, IFX_Pause* pPause) override;
103   FXCODEC_STATUS StartDecode(CFX_DIBitmap* pDIBitmap,
104                              int start_x,
105                              int start_y,
106                              int size_x,
107                              int size_y,
108                              int32_t frames,
109                              FX_BOOL bInterpol) override;
110 
111   FXCODEC_STATUS ContinueDecode(IFX_Pause* pPause) override;
112 
113  protected:
114   static FX_BOOL PngReadHeaderFunc(void* pModule,
115                                    int width,
116                                    int height,
117                                    int bpc,
118                                    int pass,
119                                    int* color_type,
120                                    double* gamma);
121   static FX_BOOL PngAskScanlineBufFunc(void* pModule,
122                                        int line,
123                                        uint8_t*& src_buf);
124   static void PngFillScanlineBufCompletedFunc(void* pModule,
125                                               int pass,
126                                               int line);
127   static void GifRecordCurrentPositionCallback(void* pModule,
128                                                FX_DWORD& cur_pos);
129   static uint8_t* GifAskLocalPaletteBufCallback(void* pModule,
130                                                 int32_t frame_num,
131                                                 int32_t pal_size);
132   static FX_BOOL GifInputRecordPositionBufCallback(void* pModule,
133                                                    FX_DWORD rcd_pos,
134                                                    const FX_RECT& img_rc,
135                                                    int32_t pal_num,
136                                                    void* pal_ptr,
137                                                    int32_t delay_time,
138                                                    FX_BOOL user_input,
139                                                    int32_t trans_index,
140                                                    int32_t disposal_method,
141                                                    FX_BOOL interlace);
142   static void GifReadScanlineCallback(void* pModule,
143                                       int32_t row_num,
144                                       uint8_t* row_buf);
145   static FX_BOOL BmpInputImagePositionBufCallback(void* pModule,
146                                                   FX_DWORD rcd_pos);
147   static void BmpReadScanlineCallback(void* pModule,
148                                       int32_t row_num,
149                                       uint8_t* row_buf);
150 
151   FX_BOOL DetectImageType(FXCODEC_IMAGE_TYPE imageType,
152                           CFX_DIBAttribute* pAttribute);
153   void GetDownScale(int& down_scale);
154   void GetTransMethod(FXDIB_Format des_format, FXCodec_Format src_format);
155   void ReSampleScanline(CFX_DIBitmap* pDeviceBitmap,
156                         int32_t des_line,
157                         uint8_t* src_scan,
158                         FXCodec_Format src_format);
159   void Resample(CFX_DIBitmap* pDeviceBitmap,
160                 int32_t src_line,
161                 uint8_t* src_scan,
162                 FXCodec_Format src_format);
163   void ResampleVert(CFX_DIBitmap* pDeviceBitmap, double scale_y, int des_row);
164   FX_BOOL JpegReadMoreData(ICodec_JpegModule* pJpegModule,
165                            FXCODEC_STATUS& err_status);
166   void PngOneOneMapResampleHorz(CFX_DIBitmap* pDeviceBitmap,
167                                 int32_t des_line,
168                                 uint8_t* src_scan,
169                                 FXCodec_Format src_format);
170   FX_BOOL GifReadMoreData(ICodec_GifModule* pGifModule,
171                           FXCODEC_STATUS& err_status);
172   void GifDoubleLineResampleVert(CFX_DIBitmap* pDeviceBitmap,
173                                  double scale_y,
174                                  int des_row);
175   FX_BOOL BmpReadMoreData(ICodec_BmpModule* pBmpModule,
176                           FXCODEC_STATUS& err_status);
177   void ResampleVertBT(CFX_DIBitmap* pDeviceBitmap, double scale_y, int des_row);
178 
179  public:
180   IFX_FileRead* m_pFile;
181   CCodec_ModuleMgr* m_pCodecMgr;
182   void* m_pJpegContext;
183   void* m_pPngContext;
184   void* m_pGifContext;
185   void* m_pBmpContext;
186   void* m_pTiffContext;
187   FXCODEC_IMAGE_TYPE m_imagType;
188   FX_DWORD m_offSet;
189   uint8_t* m_pSrcBuf;
190   FX_DWORD m_SrcSize;
191   uint8_t* m_pDecodeBuf;
192   int m_ScanlineSize;
193   CFX_DIBitmap* m_pDeviceBitmap;
194   FX_BOOL m_bInterpol;
195   CFXCODEC_WeightTable m_WeightHorz;
196   CFXCODEC_VertTable m_WeightVert;
197   CFXCODEC_HorzTable m_WeightHorzOO;
198   int m_SrcWidth;
199   int m_SrcHeight;
200   int m_SrcComponents;
201   int m_SrcBPC;
202   FX_RECT m_clipBox;
203   int m_startX;
204   int m_startY;
205   int m_sizeX;
206   int m_sizeY;
207   int m_TransMethod;
208   FX_ARGB* m_pSrcPalette;
209   int m_SrcPaletteNumber;
210   int m_SrcRow;
211   FXCodec_Format m_SrcFormat;
212   int m_SrcPassNumber;
213   int m_FrameNumber;
214   int m_FrameCur;
215   int m_GifBgIndex;
216   uint8_t* m_pGifPalette;
217   int32_t m_GifPltNumber;
218   int m_GifTransIndex;
219   FX_RECT m_GifFrameRect;
220   FX_BOOL m_BmpIsTopBottom;
221   FXCODEC_STATUS m_status;
222 };
223 #endif
224