summaryrefslogtreecommitdiff
path: root/core/src/fxcodec/codec/codec_int.h
blob: dad2953e12ace7e2ad2bb86a96b4846a126ee44c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com

#ifndef CORE_SRC_FXCODEC_CODEC_CODEC_INT_H_
#define CORE_SRC_FXCODEC_CODEC_CODEC_INT_H_

#include <limits.h>
#include <list>
#include <map>

#include "../../../../third_party/base/nonstd_unique_ptr.h"
#include "../../../../third_party/libopenjpeg20/openjpeg.h"  // For OPJ_SIZE_T.
#include "../../../include/fxcodec/fx_codec.h"
#include "../jbig2/JBig2_Context.h"

class CFX_IccProfileCache;
class CFX_IccTransformCache;

class CCodec_BasicModule : public ICodec_BasicModule {
 public:
  virtual FX_BOOL RunLengthEncode(const uint8_t* src_buf,
                                  FX_DWORD src_size,
                                  uint8_t*& dest_buf,
                                  FX_DWORD& dest_size);
  virtual FX_BOOL A85Encode(const uint8_t* src_buf,
                            FX_DWORD src_size,
                            uint8_t*& dest_buf,
                            FX_DWORD& dest_size);
  virtual ICodec_ScanlineDecoder* CreateRunLengthDecoder(const uint8_t* src_buf,
                                                         FX_DWORD src_size,
                                                         int width,
                                                         int height,
                                                         int nComps,
                                                         int bpc);
};

class CCodec_ScanlineDecoder : public ICodec_ScanlineDecoder {
 public:
  CCodec_ScanlineDecoder();
  ~CCodec_ScanlineDecoder() override;

  // ICodec_ScanlineDecoder
  FX_DWORD GetSrcOffset() override { return -1; }
  void DownScale(int dest_width, int dest_height) override;
  const uint8_t* GetScanline(int line) override;
  FX_BOOL SkipToScanline(int line, IFX_Pause* pPause) override;
  int GetWidth() override { return m_OutputWidth; }
  int GetHeight() override { return m_OutputHeight; }
  int CountComps() override { return m_nComps; }
  int GetBPC() override { return m_bpc; }
  FX_BOOL IsColorTransformed() override { return m_bColorTransformed; }
  void ClearImageData() override { m_pDataCache.reset(); }

 protected:
  class ImageDataCache {
   public:
    ImageDataCache(int width, int height, int pitch);
    ~ImageDataCache();

    bool AllocateCache();
    void AppendLine(const uint8_t* line);

    int NumLines() const { return m_nCachedLines; }
    const uint8_t* GetLine(int line) const;
    bool IsSameDimensions(int width, int height) const {
      return width == m_Width && height == m_Height;
    }

   private:
    bool IsValid() const { return m_Data.get() != nullptr; }

    const int m_Width;
    const int m_Height;
    const int m_Pitch;
    int m_nCachedLines;
    nonstd::unique_ptr<uint8_t, FxFreeDeleter> m_Data;
  };

  virtual FX_BOOL v_Rewind() = 0;
  virtual uint8_t* v_GetNextLine() = 0;
  virtual void v_DownScale(int dest_width, int dest_height) = 0;

  uint8_t* ReadNextLine();

  int m_OrigWidth;
  int m_OrigHeight;
  int m_DownScale;
  int m_OutputWidth;
  int m_OutputHeight;
  int m_nComps;
  int m_bpc;
  int m_Pitch;
  FX_BOOL m_bColorTransformed;
  int m_NextLine;
  uint8_t* m_pLastScanline;
  nonstd::unique_ptr<ImageDataCache> m_pDataCache;
};

class CCodec_FaxModule : public ICodec_FaxModule {
 public:
  virtual ICodec_ScanlineDecoder* CreateDecoder(const uint8_t* src_buf,
                                                FX_DWORD src_size,
                                                int width,
                                                int height,
                                                int K,
                                                FX_BOOL EndOfLine,
                                                FX_BOOL EncodedByteAlign,
                                                FX_BOOL BlackIs1,
                                                int Columns,
                                                int Rows);
  FX_BOOL Encode(const uint8_t* src_buf,
                 int width,
                 int height,
                 int pitch,
                 uint8_t*& dest_buf,
                 FX_DWORD& dest_size);
};

class CCodec_FlateModule : public ICodec_FlateModule {
 public:
  virtual ICodec_ScanlineDecoder* CreateDecoder(const uint8_t* src_buf,
                                                FX_DWORD src_size,
                                                int width,
                                                int height,
                                                int nComps,
                                                int bpc,
                                                int predictor,
                                                int Colors,
                                                int BitsPerComponent,
                                                int Columns);
  virtual FX_DWORD FlateOrLZWDecode(FX_BOOL bLZW,
                                    const uint8_t* src_buf,
                                    FX_DWORD src_size,
                                    FX_BOOL bEarlyChange,
                                    int predictor,
                                    int Colors,
                                    int BitsPerComponent,
                                    int Columns,
                                    FX_DWORD estimated_size,
                                    uint8_t*& dest_buf,
                                    FX_DWORD& dest_size);
  virtual FX_BOOL Encode(const uint8_t* src_buf,
                         FX_DWORD src_size,
                         int predictor,
                         int Colors,
                         int BitsPerComponent,
                         int Columns,
                         uint8_t*& dest_buf,
                         FX_DWORD& dest_size);
  virtual FX_BOOL Encode(const uint8_t* src_buf,
                         FX_DWORD src_size,
                         uint8_t*& dest_buf,
                         FX_DWORD& dest_size);
};

class CCodec_JpegModule : public ICodec_JpegModule {
 public:
  CCodec_JpegModule() : m_pExtProvider(NULL) {}
  void SetPovider(IFX_JpegProvider* pJP) { m_pExtProvider = pJP; }
  ICodec_ScanlineDecoder* CreateDecoder(const uint8_t* src_buf,
                                        FX_DWORD src_size,
                                        int width,
                                        int height,
                                        int nComps,
                                        FX_BOOL ColorTransform);
  FX_BOOL LoadInfo(const uint8_t* src_buf,
                   FX_DWORD src_size,
                   int& width,
                   int& height,
                   int& num_components,
                   int& bits_per_components,
                   FX_BOOL& color_transform,
                   uint8_t** icc_buf_ptr,
                   FX_DWORD* icc_length);
  FX_BOOL Encode(const CFX_DIBSource* pSource,
                 uint8_t*& dest_buf,
                 FX_STRSIZE& dest_size,
                 int quality,
                 const uint8_t* icc_buf,
                 FX_DWORD icc_length);
  virtual void* Start();
  virtual void Finish(void* pContext);
  virtual void Input(void* pContext, const uint8_t* src_buf, FX_DWORD src_size);
  virtual int ReadHeader(void* pContext,
                         int* width,
                         int* height,
                         int* nComps,
                         CFX_DIBAttribute* pAttribute = NULL);
  virtual int StartScanline(void* pContext, int down_scale);
  virtual FX_BOOL ReadScanline(void* pContext, uint8_t* dest_buf);
  virtual FX_DWORD GetAvailInput(void* pContext, uint8_t** avail_buf_ptr);

 protected:
  IFX_JpegProvider* m_pExtProvider;
};
#define PNG_ERROR_SIZE 256
class CCodec_PngModule : public ICodec_PngModule {
 public:
  CCodec_PngModule() { FXSYS_memset(m_szLastError, '\0', PNG_ERROR_SIZE); }

  virtual void* Start(void* pModule);
  virtual void Finish(void* pContext);
  virtual FX_BOOL Input(void* pContext,
                        const uint8_t* src_buf,
                        FX_DWORD src_size,
                        CFX_DIBAttribute* pAttribute);

 protected:
  FX_CHAR m_szLastError[PNG_ERROR_SIZE];
};
class CCodec_GifModule : public ICodec_GifModule {
 public:
  CCodec_GifModule() { FXSYS_memset(m_szLastError, '\0', 256); }
  virtual void* Start(void* pModule);
  virtual void Finish(void* pContext);
  virtual FX_DWORD GetAvailInput(void* pContext, uint8_t** avail_buf_ptr);
  virtual void Input(void* pContext, const uint8_t* src_buf, FX_DWORD src_size);

  virtual int32_t ReadHeader(void* pContext,
                             int* width,
                             int* height,
                             int* pal_num,
                             void** pal_pp,
                             int* bg_index,
                             CFX_DIBAttribute* pAttribute);

  virtual int32_t LoadFrameInfo(void* pContext, int* frame_num);

  virtual int32_t LoadFrame(void* pContext,
                            int frame_num,
                            CFX_DIBAttribute* pAttribute);

 protected:
  FX_CHAR m_szLastError[256];
};
class CCodec_BmpModule : public ICodec_BmpModule {
 public:
  CCodec_BmpModule() { FXSYS_memset(m_szLastError, '\0', 256); }
  virtual void* Start(void* pModule);
  virtual void Finish(void* pContext);
  virtual FX_DWORD GetAvailInput(void* pContext, uint8_t** avail_buf_ptr);
  virtual void Input(void* pContext, const uint8_t* src_buf, FX_DWORD src_size);
  virtual int32_t ReadHeader(void* pContext,
                             int32_t* width,
                             int32_t* height,
                             FX_BOOL* tb_flag,
                             int32_t* components,
                             int32_t* pal_num,
                             FX_DWORD** pal_pp,
                             CFX_DIBAttribute* pAttribute);
  virtual int32_t LoadImage(void* pContext);

 protected:
  FX_CHAR m_szLastError[256];
};
class CCodec_IccModule : public ICodec_IccModule {
 public:
  virtual IccCS GetProfileCS(const uint8_t* pProfileData,
                             unsigned int dwProfileSize);
  virtual IccCS GetProfileCS(IFX_FileRead* pFile);
  virtual void* CreateTransform(
      ICodec_IccModule::IccParam* pInputParam,
      ICodec_IccModule::IccParam* pOutputParam,
      ICodec_IccModule::IccParam* pProofParam = NULL,
      FX_DWORD dwIntent = Icc_INTENT_PERCEPTUAL,
      FX_DWORD dwFlag = Icc_FLAGS_DEFAULT,
      FX_DWORD dwPrfIntent = Icc_INTENT_ABSOLUTE_COLORIMETRIC,
      FX_DWORD dwPrfFlag = Icc_FLAGS_SOFTPROOFING);
  virtual void* CreateTransform_sRGB(const uint8_t* pProfileData,
                                     FX_DWORD dwProfileSize,
                                     int32_t& nComponents,
                                     int32_t intent = 0,
                                     FX_DWORD dwSrcFormat = Icc_FORMAT_DEFAULT);
  virtual void* CreateTransform_CMYK(const uint8_t* pSrcProfileData,
                                     FX_DWORD dwSrcProfileSize,
                                     int32_t& nSrcComponents,
                                     const uint8_t* pDstProfileData,
                                     FX_DWORD dwDstProfileSize,
                                     int32_t intent = 0,
                                     FX_DWORD dwSrcFormat = Icc_FORMAT_DEFAULT,
                                     FX_DWORD dwDstFormat = Icc_FORMAT_DEFAULT);
  virtual void DestroyTransform(void* pTransform);
  virtual void Translate(void* pTransform,
                         FX_FLOAT* pSrcValues,
                         FX_FLOAT* pDestValues);
  virtual void TranslateScanline(void* pTransform,
                                 uint8_t* pDest,
                                 const uint8_t* pSrc,
                                 int pixels);
  virtual void SetComponents(FX_DWORD nComponents) {
    m_nComponents = nComponents;
  }
  virtual ~CCodec_IccModule();

 protected:
  enum Icc_CLASS {
    Icc_CLASS_INPUT = 0,
    Icc_CLASS_OUTPUT,
    Icc_CLASS_PROOF,
    Icc_CLASS_MAX
  };
  void* CreateProfile(ICodec_IccModule::IccParam* pIccParam,
                      Icc_CLASS ic,
                      CFX_BinaryBuf* pTransformKey);

  FX_DWORD m_nComponents;
  std::map<CFX_ByteString, CFX_IccTransformCache*> m_MapTranform;
  std::map<CFX_ByteString, CFX_IccProfileCache*> m_MapProfile;
};

class CCodec_JpxModule : public ICodec_JpxModule {
 public:
  CCodec_JpxModule();
  ~CCodec_JpxModule() override;

  // ICodec_JpxModule:
  CJPX_Decoder* CreateDecoder(const uint8_t* src_buf,
                              FX_DWORD src_size,
                              bool use_colorspace) override;
  void GetImageInfo(CJPX_Decoder* pDecoder,
                    FX_DWORD* width,
                    FX_DWORD* height,
                    FX_DWORD* components) override;
  bool Decode(CJPX_Decoder* pDecoder,
              uint8_t* dest_data,
              int pitch,
              const std::vector<uint8_t>& offsets) override;
  void DestroyDecoder(CJPX_Decoder* pDecoder) override;
};

class CCodec_TiffModule : public ICodec_TiffModule {
 public:
  // ICodec_TiffModule
  void* CreateDecoder(IFX_FileRead* file_ptr) override;
  void GetFrames(void* ctx, int32_t& frames) override;
  FX_BOOL LoadFrameInfo(void* ctx,
                        int32_t frame,
                        FX_DWORD& width,
                        FX_DWORD& height,
                        FX_DWORD& comps,
                        FX_DWORD& bpc,
                        CFX_DIBAttribute* pAttribute = NULL) override;
  FX_BOOL Decode(void* ctx, class CFX_DIBitmap* pDIBitmap) override;
  void DestroyDecoder(void* ctx) override;

 protected:
  ~CCodec_TiffModule() override {}
};

class CCodec_Jbig2Context {
 public:
  CCodec_Jbig2Context();
  ~CCodec_Jbig2Context() {}

  FX_DWORD m_width;
  FX_DWORD m_height;
  uint8_t* m_src_buf;
  FX_DWORD m_src_size;
  const uint8_t* m_global_data;
  FX_DWORD m_global_size;
  uint8_t* m_dest_buf;
  FX_DWORD m_dest_pitch;
  FX_BOOL m_bFileReader;
  IFX_Pause* m_pPause;
  CJBig2_Context* m_pContext;
  CJBig2_Image* m_dest_image;
};
class CCodec_Jbig2Module : public ICodec_Jbig2Module {
 public:
  CCodec_Jbig2Module() {}
  ~CCodec_Jbig2Module() override;

  // ICodec_Jbig2Module
  void* CreateJbig2Context() override;
  FXCODEC_STATUS StartDecode(void* pJbig2Context,
                             FX_DWORD width,
                             FX_DWORD height,
                             const uint8_t* src_buf,
                             FX_DWORD src_size,
                             const uint8_t* global_data,
                             FX_DWORD global_size,
                             uint8_t* dest_buf,
                             FX_DWORD dest_pitch,
                             IFX_Pause* pPause) override;
  FXCODEC_STATUS ContinueDecode(void* pJbig2Context,
                                IFX_Pause* pPause) override;
  void DestroyJbig2Context(void* pJbig2Context) override;

 private:
  std::list<CJBig2_CachePair> m_SymbolDictCache;
};
class CFX_DIBAttributeExif : public IFX_DIBAttributeExif {
 public:
  CFX_DIBAttributeExif();
  ~CFX_DIBAttributeExif();
  virtual FX_BOOL GetInfo(FX_WORD tag, void* val);

  FX_BOOL ParseExif(CFX_MapPtrTemplate<FX_DWORD, uint8_t*>* pHead,
                    uint8_t* data,
                    FX_DWORD len,
                    CFX_MapPtrTemplate<FX_DWORD, uint8_t*>* pVal);

  typedef FX_WORD (*_Read2Bytes)(uint8_t* data);
  typedef FX_DWORD (*_Read4Bytes)(uint8_t* data);
  uint8_t* ParseExifIFH(uint8_t* data,
                        FX_DWORD len,
                        _Read2Bytes* pReadWord,
                        _Read4Bytes* pReadDword);
  FX_BOOL ParseExifIFD(CFX_MapPtrTemplate<FX_DWORD, uint8_t*>* pMap,
                       uint8_t* data,
                       FX_DWORD len);

  uint8_t* m_pExifData;

  FX_DWORD m_dwExifDataLen;

  void clear();
  _Read2Bytes m_readWord;
  _Read4Bytes m_readDword;
  CFX_MapPtrTemplate<FX_DWORD, uint8_t*> m_TagHead;
  CFX_MapPtrTemplate<FX_DWORD, uint8_t*> m_TagVal;
};

struct DecodeData {
 public:
  DecodeData(unsigned char* src_data, OPJ_SIZE_T src_size)
      : src_data(src_data), src_size(src_size), offset(0) {}
  unsigned char* src_data;
  OPJ_SIZE_T src_size;
  OPJ_SIZE_T offset;
};

/* Wrappers for C-style callbacks. */
OPJ_SIZE_T opj_read_from_memory(void* p_buffer,
                                OPJ_SIZE_T nb_bytes,
                                void* p_user_data);
OPJ_SIZE_T opj_write_from_memory(void* p_buffer,
                                 OPJ_SIZE_T nb_bytes,
                                 void* p_user_data);
OPJ_OFF_T opj_skip_from_memory(OPJ_OFF_T nb_bytes, void* p_user_data);
OPJ_BOOL opj_seek_from_memory(OPJ_OFF_T nb_bytes, void* p_user_data);

#endif  // CORE_SRC_FXCODEC_CODEC_CODEC_INT_H_