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