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