Various changes to JBig2 cache:
[pdfium.git] / core / include / fxcodec / fx_codec.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_INCLUDE_FXCODEC_FX_CODEC_H_
8 #define CORE_INCLUDE_FXCODEC_FX_CODEC_H_
9
10 #include <vector>
11
12 #include "../../../third_party/base/nonstd_unique_ptr.h"
13 #include "../fxcrt/fx_basic.h"
14 #include "fx_codec_def.h"
15 #include "fx_codec_provider.h"
16
17 class CFX_DIBSource;
18 class CJPX_Decoder;
19 class CPDF_PrivateData;
20 class CPDF_StreamAcc;
21 class ICodec_ScanlineDecoder;
22 class ICodec_BasicModule;
23 class ICodec_FaxModule;
24 class ICodec_JpegModule;
25 class ICodec_JpxModule;
26 class ICodec_Jbig2Module;
27 class ICodec_IccModule;
28 class ICodec_FlateModule;
29 class ICodec_Jbig2Encoder;
30 class ICodec_ScanlineDecoder;
31
32 class CCodec_ModuleMgr {
33  public:
34   CCodec_ModuleMgr();
35   ICodec_Jbig2Encoder* CreateJbig2Encoder();
36   ICodec_BasicModule* GetBasicModule() const { return m_pBasicModule.get(); }
37   ICodec_FaxModule* GetFaxModule() const { return m_pFaxModule.get(); }
38   ICodec_JpegModule* GetJpegModule() const { return m_pJpegModule.get(); }
39   ICodec_JpxModule* GetJpxModule() const { return m_pJpxModule.get(); }
40   ICodec_Jbig2Module* GetJbig2Module() const { return m_pJbig2Module.get(); }
41   ICodec_IccModule* GetIccModule() const { return m_pIccModule.get(); }
42   ICodec_FlateModule* GetFlateModule() const { return m_pFlateModule.get(); }
43
44  protected:
45   nonstd::unique_ptr<ICodec_BasicModule> m_pBasicModule;
46   nonstd::unique_ptr<ICodec_FaxModule> m_pFaxModule;
47   nonstd::unique_ptr<ICodec_JpegModule> m_pJpegModule;
48   nonstd::unique_ptr<ICodec_JpxModule> m_pJpxModule;
49   nonstd::unique_ptr<ICodec_Jbig2Module> m_pJbig2Module;
50   nonstd::unique_ptr<ICodec_IccModule> m_pIccModule;
51   nonstd::unique_ptr<ICodec_FlateModule> m_pFlateModule;
52 };
53 class ICodec_BasicModule {
54  public:
55   virtual ~ICodec_BasicModule() {}
56   virtual FX_BOOL RunLengthEncode(const uint8_t* src_buf,
57                                   FX_DWORD src_size,
58                                   uint8_t*& dest_buf,
59                                   FX_DWORD& dest_size) = 0;
60   virtual FX_BOOL A85Encode(const uint8_t* src_buf,
61                             FX_DWORD src_size,
62                             uint8_t*& dest_buf,
63                             FX_DWORD& dest_size) = 0;
64   virtual ICodec_ScanlineDecoder* CreateRunLengthDecoder(const uint8_t* src_buf,
65                                                          FX_DWORD src_size,
66                                                          int width,
67                                                          int height,
68                                                          int nComps,
69                                                          int bpc) = 0;
70 };
71
72 class ICodec_ScanlineDecoder {
73  public:
74   virtual ~ICodec_ScanlineDecoder() {}
75
76   virtual FX_DWORD GetSrcOffset() = 0;
77
78   virtual void DownScale(int dest_width, int dest_height) = 0;
79
80   virtual const uint8_t* GetScanline(int line) = 0;
81
82   virtual FX_BOOL SkipToScanline(int line, IFX_Pause* pPause) = 0;
83
84   virtual int GetWidth() = 0;
85
86   virtual int GetHeight() = 0;
87
88   virtual int CountComps() = 0;
89
90   virtual int GetBPC() = 0;
91
92   virtual FX_BOOL IsColorTransformed() = 0;
93
94   virtual void ClearImageData() = 0;
95 };
96
97 class ICodec_FlateModule {
98  public:
99   virtual ~ICodec_FlateModule() {}
100   virtual ICodec_ScanlineDecoder* CreateDecoder(const uint8_t* src_buf,
101                                                 FX_DWORD src_size,
102                                                 int width,
103                                                 int height,
104                                                 int nComps,
105                                                 int bpc,
106                                                 int predictor,
107                                                 int Colors,
108                                                 int BitsPerComponent,
109                                                 int Columns) = 0;
110   virtual FX_DWORD FlateOrLZWDecode(FX_BOOL bLZW,
111                                     const uint8_t* src_buf,
112                                     FX_DWORD src_size,
113                                     FX_BOOL bEarlyChange,
114                                     int predictor,
115                                     int Colors,
116                                     int BitsPerComponent,
117                                     int Columns,
118                                     FX_DWORD estimated_size,
119                                     uint8_t*& dest_buf,
120                                     FX_DWORD& dest_size) = 0;
121   virtual FX_BOOL Encode(const uint8_t* src_buf,
122                          FX_DWORD src_size,
123                          int predictor,
124                          int Colors,
125                          int BitsPerComponent,
126                          int Columns,
127                          uint8_t*& dest_buf,
128                          FX_DWORD& dest_size) = 0;
129   virtual FX_BOOL Encode(const uint8_t* src_buf,
130                          FX_DWORD src_size,
131                          uint8_t*& dest_buf,
132                          FX_DWORD& dest_size) = 0;
133 };
134 class ICodec_FaxModule {
135  public:
136   virtual ~ICodec_FaxModule() {}
137
138   virtual ICodec_ScanlineDecoder* CreateDecoder(const uint8_t* src_buf,
139                                                 FX_DWORD src_size,
140                                                 int width,
141                                                 int height,
142                                                 int K,
143                                                 FX_BOOL EndOfLine,
144                                                 FX_BOOL EncodedByteAlign,
145                                                 FX_BOOL BlackIs1,
146                                                 int Columns,
147                                                 int Rows) = 0;
148
149   virtual FX_BOOL Encode(const uint8_t* src_buf,
150                          int width,
151                          int height,
152                          int pitch,
153                          uint8_t*& dest_buf,
154                          FX_DWORD& dest_size) = 0;
155 };
156 class ICodec_JpegModule {
157  public:
158   virtual ~ICodec_JpegModule() {}
159
160   virtual void SetPovider(IFX_JpegProvider* pJP) = 0;
161
162   virtual ICodec_ScanlineDecoder* CreateDecoder(const uint8_t* src_buf,
163                                                 FX_DWORD src_size,
164                                                 int width,
165                                                 int height,
166                                                 int nComps,
167                                                 FX_BOOL ColorTransform) = 0;
168
169   virtual FX_BOOL LoadInfo(const uint8_t* src_buf,
170                            FX_DWORD src_size,
171                            int& width,
172                            int& height,
173                            int& num_components,
174                            int& bits_per_components,
175                            FX_BOOL& color_transform,
176                            uint8_t** icc_buf_ptr = NULL,
177                            FX_DWORD* icc_length = NULL) = 0;
178
179   virtual FX_BOOL Encode(const class CFX_DIBSource* pSource,
180                          uint8_t*& dest_buf,
181                          FX_STRSIZE& dest_size,
182                          int quality = 75,
183                          const uint8_t* icc_buf = NULL,
184                          FX_DWORD icc_length = 0) = 0;
185
186   virtual void* Start() = 0;
187
188   virtual void Finish(void* pContext) = 0;
189
190   virtual void Input(void* pContext,
191                      const uint8_t* src_buf,
192                      FX_DWORD src_size) = 0;
193
194   virtual int ReadHeader(void* pContext,
195                          int* width,
196                          int* height,
197                          int* nComps) = 0;
198
199   virtual int StartScanline(void* pContext, int down_scale) = 0;
200
201   virtual FX_BOOL ReadScanline(void* pContext, uint8_t* dest_buf) = 0;
202
203   virtual FX_DWORD GetAvailInput(void* pContext,
204                                  uint8_t** avail_buf_ptr = NULL) = 0;
205 };
206
207 class ICodec_JpxModule {
208  public:
209   virtual ~ICodec_JpxModule() {}
210
211   virtual CJPX_Decoder* CreateDecoder(const uint8_t* src_buf,
212                                       FX_DWORD src_size,
213                                       bool use_colorspace) = 0;
214
215   virtual void GetImageInfo(CJPX_Decoder* pDecoder,
216                             FX_DWORD* width,
217                             FX_DWORD* height,
218                             FX_DWORD* components) = 0;
219
220   virtual bool Decode(CJPX_Decoder* pDecoder,
221                       uint8_t* dest_data,
222                       int pitch,
223                       const std::vector<uint8_t>& offsets) = 0;
224
225   virtual void DestroyDecoder(CJPX_Decoder* pDecoder) = 0;
226 };
227
228 class ICodec_Jbig2Module {
229  public:
230   virtual ~ICodec_Jbig2Module() {}
231
232   virtual void* CreateJbig2Context() = 0;
233
234   virtual FXCODEC_STATUS StartDecode(void* pJbig2Context,
235                                      CFX_PrivateData* pPrivateData,
236                                      FX_DWORD width,
237                                      FX_DWORD height,
238                                      CPDF_StreamAcc* src_stream,
239                                      CPDF_StreamAcc* global_stream,
240                                      uint8_t* dest_buf,
241                                      FX_DWORD dest_pitch,
242                                      IFX_Pause* pPause) = 0;
243
244   virtual FXCODEC_STATUS ContinueDecode(void* pJbig2Content,
245                                         IFX_Pause* pPause) = 0;
246   virtual void DestroyJbig2Context(void* pJbig2Content) = 0;
247 };
248 class ICodec_Jbig2Encoder {
249  public:
250   virtual ~ICodec_Jbig2Encoder() {}
251 };
252 class ICodec_IccModule {
253  public:
254   typedef enum {
255     IccCS_Unknown = 0,
256     IccCS_XYZ,
257     IccCS_Lab,
258     IccCS_Luv,
259     IccCS_YCbCr,
260     IccCS_Yxy,
261     IccCS_Hsv,
262     IccCS_Hls,
263     IccCS_Gray,
264     IccCS_Rgb,
265     IccCS_Cmyk,
266     IccCS_Cmy
267   } IccCS;
268   typedef struct _IccParam {
269     FX_DWORD Version;
270     IccCS ColorSpace;
271     FX_DWORD dwProfileType;
272     FX_DWORD dwFormat;
273     uint8_t* pProfileData;
274     FX_DWORD dwProfileSize;
275     double Gamma;
276   } IccParam;
277
278   virtual ~ICodec_IccModule() {}
279
280   virtual IccCS GetProfileCS(const uint8_t* pProfileData,
281                              unsigned int dwProfileSize) = 0;
282
283   virtual IccCS GetProfileCS(IFX_FileRead* pFile) = 0;
284
285   virtual void* CreateTransform(
286       ICodec_IccModule::IccParam* pInputParam,
287       ICodec_IccModule::IccParam* pOutputParam,
288       ICodec_IccModule::IccParam* pProofParam = NULL,
289       FX_DWORD dwIntent = Icc_INTENT_PERCEPTUAL,
290       FX_DWORD dwFlag = Icc_FLAGS_DEFAULT,
291       FX_DWORD dwPrfIntent = Icc_INTENT_ABSOLUTE_COLORIMETRIC,
292       FX_DWORD dwPrfFlag = Icc_FLAGS_SOFTPROOFING) = 0;
293
294   virtual void* CreateTransform_sRGB(
295       const uint8_t* pProfileData,
296       FX_DWORD dwProfileSize,
297       int32_t& nComponents,
298       int32_t intent = 0,
299       FX_DWORD dwSrcFormat = Icc_FORMAT_DEFAULT) = 0;
300
301   virtual void* CreateTransform_CMYK(
302       const uint8_t* pSrcProfileData,
303       FX_DWORD dwSrcProfileSize,
304       int32_t& nSrcComponents,
305       const uint8_t* pDstProfileData,
306       FX_DWORD dwDstProfileSize,
307       int32_t intent = 0,
308       FX_DWORD dwSrcFormat = Icc_FORMAT_DEFAULT,
309       FX_DWORD dwDstFormat = Icc_FORMAT_DEFAULT) = 0;
310
311   virtual void DestroyTransform(void* pTransform) = 0;
312
313   virtual void Translate(void* pTransform,
314                          FX_FLOAT* pSrcValues,
315                          FX_FLOAT* pDestValues) = 0;
316
317   virtual void TranslateScanline(void* pTransform,
318                                  uint8_t* pDest,
319                                  const uint8_t* pSrc,
320                                  int pixels) = 0;
321   virtual void SetComponents(FX_DWORD nComponents) = 0;
322 };
323 void AdobeCMYK_to_sRGB(FX_FLOAT c,
324                        FX_FLOAT m,
325                        FX_FLOAT y,
326                        FX_FLOAT k,
327                        FX_FLOAT& R,
328                        FX_FLOAT& G,
329                        FX_FLOAT& B);
330 void AdobeCMYK_to_sRGB1(uint8_t c,
331                         uint8_t m,
332                         uint8_t y,
333                         uint8_t k,
334                         uint8_t& R,
335                         uint8_t& G,
336                         uint8_t& B);
337 FX_BOOL MD5ComputeID(const void* buf, FX_DWORD dwSize, uint8_t ID[16]);
338
339 void FaxG4Decode(const uint8_t* src_buf,
340                  FX_DWORD src_size,
341                  int* pbitpos,
342                  uint8_t* dest_buf,
343                  int width,
344                  int height,
345                  int pitch);
346
347 #endif  // CORE_INCLUDE_FXCODEC_FX_CODEC_H_