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