9fe77a44f718e1201370ffe831047999be5b0f96
[pdfium.git] / core / src / fxcodec / codec / codec_int.h
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_SRC_FXCODEC_CODEC_CODEC_INT_H_
8 #define CORE_SRC_FXCODEC_CODEC_CODEC_INT_H_
9
10 #include <limits.h>
11 #include <list>
12
13 #include "../../../../third_party/libopenjpeg20/openjpeg.h"  // For OPJ_SIZE_T.
14 #include "../../../include/fxcodec/fx_codec.h"
15 #include "../jbig2/JBig2_Context.h"
16
17 class CCodec_BasicModule : public ICodec_BasicModule {
18  public:
19   virtual FX_BOOL RunLengthEncode(const uint8_t* src_buf,
20                                   FX_DWORD src_size,
21                                   uint8_t*& dest_buf,
22                                   FX_DWORD& dest_size);
23   virtual FX_BOOL A85Encode(const uint8_t* src_buf,
24                             FX_DWORD src_size,
25                             uint8_t*& dest_buf,
26                             FX_DWORD& dest_size);
27   virtual ICodec_ScanlineDecoder* CreateRunLengthDecoder(const uint8_t* src_buf,
28                                                          FX_DWORD src_size,
29                                                          int width,
30                                                          int height,
31                                                          int nComps,
32                                                          int bpc);
33 };
34
35 struct CCodec_ImageDataCache {
36   int m_Width, m_Height;
37   int m_nCachedLines;
38   uint8_t m_Data;
39 };
40
41 class CCodec_ScanlineDecoder : public ICodec_ScanlineDecoder {
42  public:
43   CCodec_ScanlineDecoder();
44   ~CCodec_ScanlineDecoder() override;
45
46   // ICodec_ScanlineDecoder
47   FX_DWORD GetSrcOffset() override { return -1; }
48   void DownScale(int dest_width, int dest_height) override;
49   uint8_t* GetScanline(int line) override;
50   FX_BOOL SkipToScanline(int line, IFX_Pause* pPause) override;
51   int GetWidth() override { return m_OutputWidth; }
52   int GetHeight() override { return m_OutputHeight; }
53   int CountComps() override { return m_nComps; }
54   int GetBPC() override { return m_bpc; }
55   FX_BOOL IsColorTransformed() override { return m_bColorTransformed; }
56   void ClearImageData() override {
57     if (m_pDataCache) {
58       FX_Free(m_pDataCache);
59     }
60     m_pDataCache = NULL;
61   }
62
63  protected:
64   int m_OrigWidth;
65
66   int m_OrigHeight;
67
68   int m_DownScale;
69
70   int m_OutputWidth;
71
72   int m_OutputHeight;
73
74   int m_nComps;
75
76   int m_bpc;
77
78   int m_Pitch;
79
80   FX_BOOL m_bColorTransformed;
81
82   uint8_t* ReadNextLine();
83
84   virtual FX_BOOL v_Rewind() = 0;
85
86   virtual uint8_t* v_GetNextLine() = 0;
87
88   virtual void v_DownScale(int dest_width, int dest_height) = 0;
89
90   int m_NextLine;
91
92   uint8_t* m_pLastScanline;
93
94   CCodec_ImageDataCache* m_pDataCache;
95 };
96
97 class CCodec_FaxModule : public ICodec_FaxModule {
98  public:
99   virtual ICodec_ScanlineDecoder* CreateDecoder(const uint8_t* src_buf,
100                                                 FX_DWORD src_size,
101                                                 int width,
102                                                 int height,
103                                                 int K,
104                                                 FX_BOOL EndOfLine,
105                                                 FX_BOOL EncodedByteAlign,
106                                                 FX_BOOL BlackIs1,
107                                                 int Columns,
108                                                 int Rows);
109   FX_BOOL Encode(const uint8_t* src_buf,
110                  int width,
111                  int height,
112                  int pitch,
113                  uint8_t*& dest_buf,
114                  FX_DWORD& dest_size);
115 };
116
117 class CCodec_FlateModule : public ICodec_FlateModule {
118  public:
119   virtual ICodec_ScanlineDecoder* CreateDecoder(const uint8_t* src_buf,
120                                                 FX_DWORD src_size,
121                                                 int width,
122                                                 int height,
123                                                 int nComps,
124                                                 int bpc,
125                                                 int predictor,
126                                                 int Colors,
127                                                 int BitsPerComponent,
128                                                 int Columns);
129   virtual FX_DWORD FlateOrLZWDecode(FX_BOOL bLZW,
130                                     const uint8_t* src_buf,
131                                     FX_DWORD src_size,
132                                     FX_BOOL bEarlyChange,
133                                     int predictor,
134                                     int Colors,
135                                     int BitsPerComponent,
136                                     int Columns,
137                                     FX_DWORD estimated_size,
138                                     uint8_t*& dest_buf,
139                                     FX_DWORD& dest_size);
140   virtual FX_BOOL Encode(const uint8_t* src_buf,
141                          FX_DWORD src_size,
142                          int predictor,
143                          int Colors,
144                          int BitsPerComponent,
145                          int Columns,
146                          uint8_t*& dest_buf,
147                          FX_DWORD& dest_size);
148   virtual FX_BOOL Encode(const uint8_t* src_buf,
149                          FX_DWORD src_size,
150                          uint8_t*& dest_buf,
151                          FX_DWORD& dest_size);
152 };
153
154 class CCodec_JpegModule : public ICodec_JpegModule {
155  public:
156   CCodec_JpegModule() : m_pExtProvider(NULL) {}
157   void SetPovider(IFX_JpegProvider* pJP) { m_pExtProvider = pJP; }
158   ICodec_ScanlineDecoder* CreateDecoder(const uint8_t* src_buf,
159                                         FX_DWORD src_size,
160                                         int width,
161                                         int height,
162                                         int nComps,
163                                         FX_BOOL ColorTransform);
164   FX_BOOL LoadInfo(const uint8_t* src_buf,
165                    FX_DWORD src_size,
166                    int& width,
167                    int& height,
168                    int& num_components,
169                    int& bits_per_components,
170                    FX_BOOL& color_transform,
171                    uint8_t** icc_buf_ptr,
172                    FX_DWORD* icc_length);
173   FX_BOOL Encode(const CFX_DIBSource* pSource,
174                  uint8_t*& dest_buf,
175                  FX_STRSIZE& dest_size,
176                  int quality,
177                  const uint8_t* icc_buf,
178                  FX_DWORD icc_length);
179   virtual void* Start();
180   virtual void Finish(void* pContext);
181   virtual void Input(void* pContext, const uint8_t* src_buf, FX_DWORD src_size);
182   virtual int ReadHeader(void* pContext, int* width, int* height, int* nComps);
183   virtual int StartScanline(void* pContext, int down_scale);
184   virtual FX_BOOL ReadScanline(void* pContext, uint8_t* dest_buf);
185   virtual FX_DWORD GetAvailInput(void* pContext, uint8_t** avail_buf_ptr);
186
187  protected:
188   IFX_JpegProvider* m_pExtProvider;
189 };
190 class CCodec_IccModule : public ICodec_IccModule {
191  public:
192   virtual IccCS GetProfileCS(const uint8_t* pProfileData,
193                              unsigned int dwProfileSize);
194   virtual IccCS GetProfileCS(IFX_FileRead* pFile);
195   virtual void* CreateTransform(
196       ICodec_IccModule::IccParam* pInputParam,
197       ICodec_IccModule::IccParam* pOutputParam,
198       ICodec_IccModule::IccParam* pProofParam = NULL,
199       FX_DWORD dwIntent = Icc_INTENT_PERCEPTUAL,
200       FX_DWORD dwFlag = Icc_FLAGS_DEFAULT,
201       FX_DWORD dwPrfIntent = Icc_INTENT_ABSOLUTE_COLORIMETRIC,
202       FX_DWORD dwPrfFlag = Icc_FLAGS_SOFTPROOFING);
203   virtual void* CreateTransform_sRGB(const uint8_t* pProfileData,
204                                      FX_DWORD dwProfileSize,
205                                      int32_t& nComponents,
206                                      int32_t intent = 0,
207                                      FX_DWORD dwSrcFormat = Icc_FORMAT_DEFAULT);
208   virtual void* CreateTransform_CMYK(const uint8_t* pSrcProfileData,
209                                      FX_DWORD dwSrcProfileSize,
210                                      int32_t& nSrcComponents,
211                                      const uint8_t* pDstProfileData,
212                                      FX_DWORD dwDstProfileSize,
213                                      int32_t intent = 0,
214                                      FX_DWORD dwSrcFormat = Icc_FORMAT_DEFAULT,
215                                      FX_DWORD dwDstFormat = Icc_FORMAT_DEFAULT);
216   virtual void DestroyTransform(void* pTransform);
217   virtual void Translate(void* pTransform,
218                          FX_FLOAT* pSrcValues,
219                          FX_FLOAT* pDestValues);
220   virtual void TranslateScanline(void* pTransform,
221                                  uint8_t* pDest,
222                                  const uint8_t* pSrc,
223                                  int pixels);
224   virtual void SetComponents(FX_DWORD nComponents) {
225     m_nComponents = nComponents;
226   }
227   virtual ~CCodec_IccModule();
228
229  protected:
230   CFX_MapByteStringToPtr m_MapTranform;
231   CFX_MapByteStringToPtr m_MapProfile;
232   FX_DWORD m_nComponents;
233   typedef enum {
234     Icc_CLASS_INPUT = 0,
235     Icc_CLASS_OUTPUT,
236     Icc_CLASS_PROOF,
237     Icc_CLASS_MAX
238   } Icc_CLASS;
239   void* CreateProfile(ICodec_IccModule::IccParam* pIccParam,
240                       Icc_CLASS ic,
241                       CFX_BinaryBuf* pTransformKey);
242 };
243
244 class CCodec_JpxModule : public ICodec_JpxModule {
245  public:
246   CCodec_JpxModule();
247   void* CreateDecoder(const uint8_t* src_buf,
248                       FX_DWORD src_size,
249                       FX_BOOL useColorSpace = FALSE);
250   void GetImageInfo(void* ctx,
251                     FX_DWORD& width,
252                     FX_DWORD& height,
253                     FX_DWORD& codestream_nComps,
254                     FX_DWORD& output_nComps);
255   FX_BOOL Decode(void* ctx,
256                  uint8_t* dest_data,
257                  int pitch,
258                  FX_BOOL bTranslateColor,
259                  uint8_t* offsets);
260   void DestroyDecoder(void* ctx);
261 };
262
263 class CPDF_Jbig2Interface : public CJBig2_Module {
264  public:
265   virtual void* JBig2_Malloc(FX_DWORD dwSize) {
266     return FX_Alloc(uint8_t, dwSize);
267   }
268   virtual void* JBig2_Malloc2(FX_DWORD num, FX_DWORD dwSize) {
269     if (dwSize && num >= UINT_MAX / dwSize) {
270       return NULL;
271     }
272     return FX_Alloc(uint8_t, num * dwSize);
273   }
274   virtual void* JBig2_Malloc3(FX_DWORD num, FX_DWORD dwSize, FX_DWORD dwSize2) {
275     if (dwSize2 && dwSize >= UINT_MAX / dwSize2) {
276       return NULL;
277     }
278     FX_DWORD size = dwSize2 * dwSize;
279     if (size && num >= UINT_MAX / size) {
280       return NULL;
281     }
282     return FX_Alloc(uint8_t, num * size);
283   }
284   virtual void* JBig2_Realloc(void* pMem, FX_DWORD dwSize) {
285     return FX_Realloc(uint8_t, pMem, dwSize);
286   }
287   virtual void JBig2_Free(void* pMem) { FX_Free(pMem); }
288 };
289
290 class CCodec_Jbig2Context {
291  public:
292   CCodec_Jbig2Context();
293   ~CCodec_Jbig2Context(){};
294
295   FX_DWORD m_width;
296   FX_DWORD m_height;
297   uint8_t* m_src_buf;
298   FX_DWORD m_src_size;
299   const uint8_t* m_global_data;
300   FX_DWORD m_global_size;
301   uint8_t* m_dest_buf;
302   FX_DWORD m_dest_pitch;
303   FX_BOOL m_bFileReader;
304   IFX_Pause* m_pPause;
305   CJBig2_Context* m_pContext;
306   CJBig2_Image* m_dest_image;
307 };
308 class CCodec_Jbig2Module : public ICodec_Jbig2Module {
309  public:
310   CCodec_Jbig2Module(){};
311   ~CCodec_Jbig2Module() override;
312
313   // ICodec_Jbig2Module
314   FX_BOOL Decode(FX_DWORD width,
315                  FX_DWORD height,
316                  const uint8_t* src_buf,
317                  FX_DWORD src_size,
318                  const uint8_t* global_data,
319                  FX_DWORD global_size,
320                  uint8_t* dest_buf,
321                  FX_DWORD dest_pitch) override;
322   FX_BOOL Decode(IFX_FileRead* file_ptr,
323                  FX_DWORD& width,
324                  FX_DWORD& height,
325                  FX_DWORD& pitch,
326                  uint8_t*& dest_buf) override;
327   void* CreateJbig2Context() override;
328   FXCODEC_STATUS StartDecode(void* pJbig2Context,
329                              FX_DWORD width,
330                              FX_DWORD height,
331                              const uint8_t* src_buf,
332                              FX_DWORD src_size,
333                              const uint8_t* global_data,
334                              FX_DWORD global_size,
335                              uint8_t* dest_buf,
336                              FX_DWORD dest_pitch,
337                              IFX_Pause* pPause) override;
338   FXCODEC_STATUS StartDecode(void* pJbig2Context,
339                              IFX_FileRead* file_ptr,
340                              FX_DWORD& width,
341                              FX_DWORD& height,
342                              FX_DWORD& pitch,
343                              uint8_t*& dest_buf,
344                              IFX_Pause* pPause) override;
345   FXCODEC_STATUS ContinueDecode(void* pJbig2Context,
346                                 IFX_Pause* pPause) override;
347   void DestroyJbig2Context(void* pJbig2Context) override;
348
349  private:
350   CPDF_Jbig2Interface m_Module;
351   std::list<CJBig2_CachePair> m_SymbolDictCache;
352 };
353
354 struct DecodeData {
355  public:
356   DecodeData(unsigned char* src_data, OPJ_SIZE_T src_size)
357       : src_data(src_data), src_size(src_size), offset(0) {}
358   unsigned char* src_data;
359   OPJ_SIZE_T src_size;
360   OPJ_SIZE_T offset;
361 };
362
363 /* Wrappers for C-style callbacks. */
364 OPJ_SIZE_T opj_read_from_memory(void* p_buffer,
365                                 OPJ_SIZE_T nb_bytes,
366                                 void* p_user_data);
367 OPJ_SIZE_T opj_write_from_memory(void* p_buffer,
368                                  OPJ_SIZE_T nb_bytes,
369                                  void* p_user_data);
370 OPJ_OFF_T opj_skip_from_memory(OPJ_OFF_T nb_bytes, void* p_user_data);
371 OPJ_BOOL opj_seek_from_memory(OPJ_OFF_T nb_bytes, void* p_user_data);
372
373 #endif  // CORE_SRC_FXCODEC_CODEC_CODEC_INT_H_