Cleanup some numeric 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/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() : m_pExtProvider(NULL) {}
162   void SetPovider(IFX_JpegProvider* pJP) { m_pExtProvider = pJP; }
163   ICodec_ScanlineDecoder* CreateDecoder(const uint8_t* src_buf,
164                                         FX_DWORD src_size,
165                                         int width,
166                                         int height,
167                                         int nComps,
168                                         FX_BOOL ColorTransform);
169   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,
177                    FX_DWORD* icc_length);
178   FX_BOOL Encode(const CFX_DIBSource* pSource,
179                  uint8_t*& dest_buf,
180                  FX_STRSIZE& dest_size,
181                  int quality,
182                  const uint8_t* icc_buf,
183                  FX_DWORD icc_length);
184   virtual void* Start();
185   virtual void Finish(void* pContext);
186   virtual void Input(void* pContext, const uint8_t* src_buf, FX_DWORD src_size);
187   virtual int ReadHeader(void* pContext, int* width, int* height, int* nComps);
188   virtual int StartScanline(void* pContext, int down_scale);
189   virtual FX_BOOL ReadScanline(void* pContext, uint8_t* dest_buf);
190   virtual FX_DWORD GetAvailInput(void* pContext, uint8_t** avail_buf_ptr);
191
192  protected:
193   IFX_JpegProvider* m_pExtProvider;
194 };
195 class CCodec_IccModule : public ICodec_IccModule {
196  public:
197   virtual IccCS GetProfileCS(const uint8_t* pProfileData,
198                              unsigned int dwProfileSize);
199   virtual IccCS GetProfileCS(IFX_FileRead* pFile);
200   virtual void* CreateTransform(
201       ICodec_IccModule::IccParam* pInputParam,
202       ICodec_IccModule::IccParam* pOutputParam,
203       ICodec_IccModule::IccParam* pProofParam = NULL,
204       FX_DWORD dwIntent = Icc_INTENT_PERCEPTUAL,
205       FX_DWORD dwFlag = Icc_FLAGS_DEFAULT,
206       FX_DWORD dwPrfIntent = Icc_INTENT_ABSOLUTE_COLORIMETRIC,
207       FX_DWORD dwPrfFlag = Icc_FLAGS_SOFTPROOFING);
208   virtual void* CreateTransform_sRGB(const uint8_t* pProfileData,
209                                      FX_DWORD dwProfileSize,
210                                      int32_t& nComponents,
211                                      int32_t intent = 0,
212                                      FX_DWORD dwSrcFormat = Icc_FORMAT_DEFAULT);
213   virtual void* CreateTransform_CMYK(const uint8_t* pSrcProfileData,
214                                      FX_DWORD dwSrcProfileSize,
215                                      int32_t& nSrcComponents,
216                                      const uint8_t* pDstProfileData,
217                                      FX_DWORD dwDstProfileSize,
218                                      int32_t intent = 0,
219                                      FX_DWORD dwSrcFormat = Icc_FORMAT_DEFAULT,
220                                      FX_DWORD dwDstFormat = Icc_FORMAT_DEFAULT);
221   virtual void DestroyTransform(void* pTransform);
222   virtual void Translate(void* pTransform,
223                          FX_FLOAT* pSrcValues,
224                          FX_FLOAT* pDestValues);
225   virtual void TranslateScanline(void* pTransform,
226                                  uint8_t* pDest,
227                                  const uint8_t* pSrc,
228                                  int pixels);
229   virtual void SetComponents(FX_DWORD nComponents) {
230     m_nComponents = nComponents;
231   }
232   virtual ~CCodec_IccModule();
233
234  protected:
235   enum Icc_CLASS {
236     Icc_CLASS_INPUT = 0,
237     Icc_CLASS_OUTPUT,
238     Icc_CLASS_PROOF,
239     Icc_CLASS_MAX
240   };
241   void* CreateProfile(ICodec_IccModule::IccParam* pIccParam,
242                       Icc_CLASS ic,
243                       CFX_BinaryBuf* pTransformKey);
244
245   FX_DWORD m_nComponents;
246   std::map<CFX_ByteString, CFX_IccTransformCache*> m_MapTranform;
247   std::map<CFX_ByteString, CFX_IccProfileCache*> m_MapProfile;
248 };
249
250 class CCodec_JpxModule : public ICodec_JpxModule {
251  public:
252   CCodec_JpxModule();
253   ~CCodec_JpxModule() override;
254
255   // ICodec_JpxModule:
256   CJPX_Decoder* CreateDecoder(const uint8_t* src_buf,
257                               FX_DWORD src_size,
258                               bool use_colorspace) override;
259   void GetImageInfo(CJPX_Decoder* pDecoder,
260                     FX_DWORD* width,
261                     FX_DWORD* height,
262                     FX_DWORD* components) override;
263   bool Decode(CJPX_Decoder* pDecoder,
264               uint8_t* dest_data,
265               int pitch,
266               const std::vector<uint8_t>& offsets) override;
267   void DestroyDecoder(CJPX_Decoder* pDecoder) override;
268 };
269
270 class CCodec_Jbig2Context {
271  public:
272   CCodec_Jbig2Context();
273   ~CCodec_Jbig2Context() {}
274
275   FX_DWORD m_width;
276   FX_DWORD m_height;
277   CPDF_StreamAcc* m_pGlobalStream;
278   CPDF_StreamAcc* m_pSrcStream;
279   uint8_t* m_dest_buf;
280   FX_DWORD m_dest_pitch;
281   IFX_Pause* m_pPause;
282   CJBig2_Context* m_pContext;
283   CJBig2_Image* m_dest_image;
284 };
285 class CCodec_Jbig2Module : public ICodec_Jbig2Module {
286  public:
287   CCodec_Jbig2Module() {}
288   ~CCodec_Jbig2Module() override;
289
290   // ICodec_Jbig2Module
291   void* CreateJbig2Context() override;
292   FXCODEC_STATUS StartDecode(void* pJbig2Context,
293                              CFX_PrivateData* pPrivateData,
294                              FX_DWORD width,
295                              FX_DWORD height,
296                              CPDF_StreamAcc* src_stream,
297                              CPDF_StreamAcc* global_stream,
298                              uint8_t* dest_buf,
299                              FX_DWORD dest_pitch,
300                              IFX_Pause* pPause) override;
301   FXCODEC_STATUS ContinueDecode(void* pJbig2Context,
302                                 IFX_Pause* pPause) override;
303   void DestroyJbig2Context(void* pJbig2Context) override;
304 };
305
306 struct DecodeData {
307  public:
308   DecodeData(unsigned char* src_data, OPJ_SIZE_T src_size)
309       : src_data(src_data), src_size(src_size), offset(0) {}
310   unsigned char* src_data;
311   OPJ_SIZE_T src_size;
312   OPJ_SIZE_T offset;
313 };
314
315 void sycc420_to_rgb(opj_image_t* img);
316
317 /* Wrappers for C-style callbacks. */
318 OPJ_SIZE_T opj_read_from_memory(void* p_buffer,
319                                 OPJ_SIZE_T nb_bytes,
320                                 void* p_user_data);
321 OPJ_SIZE_T opj_write_from_memory(void* p_buffer,
322                                  OPJ_SIZE_T nb_bytes,
323                                  void* p_user_data);
324 OPJ_OFF_T opj_skip_from_memory(OPJ_OFF_T nb_bytes, void* p_user_data);
325 OPJ_BOOL opj_seek_from_memory(OPJ_OFF_T nb_bytes, void* p_user_data);
326
327 #endif  // CORE_SRC_FXCODEC_CODEC_CODEC_INT_H_