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