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