4fcef810f60b30e2c5a477cb6f3f1bcb34a49efa
[pdfium.git] / core / src / fxcodec / jbig2 / JBig2_Context.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_JBIG2_JBIG2_CONTEXT_H_
8 #define CORE_SRC_FXCODEC_JBIG2_JBIG2_CONTEXT_H_
9
10 #include <list>
11 #include <utility>
12
13 #include "../../../../third_party/base/nonstd_unique_ptr.h"
14 #include "../../../include/fxcodec/fx_codec_def.h"
15 #include "JBig2_List.h"
16 #include "JBig2_Page.h"
17 #include "JBig2_Segment.h"
18
19 class CJBig2_ArithDecoder;
20 class CJBig2_GRDProc;
21 class IFX_Pause;
22
23 using CJBig2_CachePair = std::pair<const uint8_t*, CJBig2_SymbolDict*>;
24
25 #define JBIG2_SUCCESS 0
26 #define JBIG2_FAILED -1
27 #define JBIG2_ERROR_TOO_SHORT -2
28 #define JBIG2_ERROR_FATAL -3
29 #define JBIG2_END_OF_PAGE 2
30 #define JBIG2_END_OF_FILE 3
31 #define JBIG2_ERROR_FILE_FORMAT -4
32 #define JBIG2_ERROR_STREAM_TYPE -5
33 #define JBIG2_ERROR_LIMIT -6
34 #define JBIG2_MIN_SEGMENT_SIZE 11
35
36 class CJBig2_Context {
37  public:
38   static CJBig2_Context* CreateContext(
39       const uint8_t* pGlobalData,
40       FX_DWORD dwGlobalLength,
41       const uint8_t* pData,
42       FX_DWORD dwLength,
43       std::list<CJBig2_CachePair>* pSymbolDictCache,
44       IFX_Pause* pPause = NULL);
45
46   static void DestroyContext(CJBig2_Context* pContext);
47
48   int32_t getFirstPage(uint8_t* pBuf,
49                        int32_t width,
50                        int32_t height,
51                        int32_t stride,
52                        IFX_Pause* pPause);
53
54   int32_t Continue(IFX_Pause* pPause);
55   FXCODEC_STATUS GetProcessingStatus() { return m_ProcessingStatus; }
56
57  private:
58   CJBig2_Context(const uint8_t* pGlobalData,
59                  FX_DWORD dwGlobalLength,
60                  const uint8_t* pData,
61                  FX_DWORD dwLength,
62                  std::list<CJBig2_CachePair>* pSymbolDictCache,
63                  IFX_Pause* pPause);
64
65   ~CJBig2_Context();
66
67   int32_t decode_SquentialOrgnazation(IFX_Pause* pPause);
68
69   int32_t decode_EmbedOrgnazation(IFX_Pause* pPause);
70
71   int32_t decode_RandomOrgnazation_FirstPage(IFX_Pause* pPause);
72
73   int32_t decode_RandomOrgnazation(IFX_Pause* pPause);
74
75   CJBig2_Segment* findSegmentByNumber(FX_DWORD dwNumber);
76
77   CJBig2_Segment* findReferredSegmentByTypeAndIndex(CJBig2_Segment* pSegment,
78                                                     uint8_t cType,
79                                                     int32_t nIndex);
80
81   int32_t parseSegmentHeader(CJBig2_Segment* pSegment);
82
83   int32_t parseSegmentData(CJBig2_Segment* pSegment, IFX_Pause* pPause);
84   int32_t ProcessingParseSegmentData(CJBig2_Segment* pSegment,
85                                      IFX_Pause* pPause);
86
87   int32_t parseSymbolDict(CJBig2_Segment* pSegment, IFX_Pause* pPause);
88
89   int32_t parseTextRegion(CJBig2_Segment* pSegment);
90
91   int32_t parsePatternDict(CJBig2_Segment* pSegment, IFX_Pause* pPause);
92
93   int32_t parseHalftoneRegion(CJBig2_Segment* pSegment, IFX_Pause* pPause);
94
95   int32_t parseGenericRegion(CJBig2_Segment* pSegment, IFX_Pause* pPause);
96
97   int32_t parseGenericRefinementRegion(CJBig2_Segment* pSegment);
98
99   int32_t parseTable(CJBig2_Segment* pSegment);
100
101   int32_t parseRegionInfo(JBig2RegionInfo* pRI);
102
103   JBig2HuffmanCode* decodeSymbolIDHuffmanTable(CJBig2_BitStream* pStream,
104                                                FX_DWORD SBNUMSYMS);
105
106   void huffman_assign_code(int* CODES, int* PREFLEN, int NTEMP);
107
108   void huffman_assign_code(JBig2HuffmanCode* SBSYMCODES, int NTEMP);
109
110  private:
111   CJBig2_Context* m_pGlobalContext;
112   nonstd::unique_ptr<CJBig2_BitStream> m_pStream;
113   CJBig2_List<CJBig2_Segment> m_SegmentList;
114   CJBig2_List<JBig2PageInfo> m_PageInfoList;
115   nonstd::unique_ptr<CJBig2_Image> m_pPage;
116   size_t m_nSegmentDecoded;
117   bool m_bInPage;
118   bool m_bBufSpecified;
119   int32_t m_PauseStep;
120   IFX_Pause* m_pPause;
121   FXCODEC_STATUS m_ProcessingStatus;
122   nonstd::unique_ptr<CJBig2_ArithDecoder> m_pArithDecoder;
123   nonstd::unique_ptr<CJBig2_GRDProc> m_pGRD;
124   JBig2ArithCtx* m_gbContext;
125   nonstd::unique_ptr<CJBig2_Segment> m_pSegment;
126   FX_DWORD m_dwOffset;
127   JBig2RegionInfo m_ri;
128   std::list<CJBig2_CachePair>* const m_pSymbolDictCache;
129 };
130
131 #endif  // CORE_SRC_FXCODEC_JBIG2_JBIG2_CONTEXT_H_