Make a bunch of JBig2 classes independent of CJBig2_Object.
[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 _JBIG2_CONTEXT_H_
8 #define _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_GeneralDecoder.h"
16 #include "JBig2_List.h"
17 #include "JBig2_Module.h"
18 #include "JBig2_Page.h"
19 #include "JBig2_Segment.h"
20
21 using CJBig2_CachePair = std::pair<uint8_t*, CJBig2_SymbolDict*>;
22
23 #define JBIG2_SUCCESS 0
24 #define JBIG2_FAILED -1
25 #define JBIG2_ERROR_TOO_SHORT -2
26 #define JBIG2_ERROR_FATAL -3
27 #define JBIG2_END_OF_PAGE 2
28 #define JBIG2_END_OF_FILE 3
29 #define JBIG2_ERROR_FILE_FORMAT -4
30 #define JBIG2_ERROR_STREAM_TYPE -5
31 #define JBIG2_ERROR_LIMIT -6
32 #define JBIG2_FILE_STREAM 0
33 #define JBIG2_SQUENTIAL_STREAM 1
34 #define JBIG2_RANDOM_STREAM 2
35 #define JBIG2_EMBED_STREAM 3
36 #define JBIG2_MIN_SEGMENT_SIZE 11
37
38 class CJBig2_Context : public CJBig2_Object {
39  public:
40   static CJBig2_Context* CreateContext(
41       CJBig2_Module* pModule,
42       uint8_t* pGlobalData,
43       FX_DWORD dwGlobalLength,
44       uint8_t* pData,
45       FX_DWORD dwLength,
46       int32_t nStreamType,
47       std::list<CJBig2_CachePair>* pSymbolDictCache,
48       IFX_Pause* pPause = NULL);
49
50   static void DestroyContext(CJBig2_Context* pContext);
51
52   int32_t getFirstPage(uint8_t* pBuf,
53                        int32_t width,
54                        int32_t height,
55                        int32_t stride,
56                        IFX_Pause* pPause);
57
58   int32_t getFirstPage(CJBig2_Image** image, IFX_Pause* pPause);
59
60   int32_t Continue(IFX_Pause* pPause);
61   FXCODEC_STATUS GetProcessiveStatus() { return m_ProcessiveStatus; }
62
63  private:
64   enum JBig2State {
65     JBIG2_OUT_OF_PAGE = 0,
66     JBIG2_IN_PAGE,
67   };
68
69   CJBig2_Context(uint8_t* pGlobalData,
70                  FX_DWORD dwGlobalLength,
71                  uint8_t* pData,
72                  FX_DWORD dwLength,
73                  int32_t nStreamType,
74                  std::list<CJBig2_CachePair>* pSymbolDictCache,
75                  IFX_Pause* pPause);
76
77   ~CJBig2_Context();
78
79   int32_t decodeFile(IFX_Pause* pPause);
80
81   int32_t decode_SquentialOrgnazation(IFX_Pause* pPause);
82
83   int32_t decode_EmbedOrgnazation(IFX_Pause* pPause);
84
85   int32_t decode_RandomOrgnazation_FirstPage(IFX_Pause* pPause);
86
87   int32_t decode_RandomOrgnazation(IFX_Pause* pPause);
88
89   CJBig2_Segment* findSegmentByNumber(FX_DWORD dwNumber);
90
91   CJBig2_Segment* findReferredSegmentByTypeAndIndex(CJBig2_Segment* pSegment,
92                                                     uint8_t cType,
93                                                     int32_t nIndex);
94
95   int32_t parseSegmentHeader(CJBig2_Segment* pSegment);
96
97   int32_t parseSegmentData(CJBig2_Segment* pSegment, IFX_Pause* pPause);
98   int32_t ProcessiveParseSegmentData(CJBig2_Segment* pSegment,
99                                      IFX_Pause* pPause);
100
101   int32_t parseSymbolDict(CJBig2_Segment* pSegment, IFX_Pause* pPause);
102
103   int32_t parseTextRegion(CJBig2_Segment* pSegment);
104
105   int32_t parsePatternDict(CJBig2_Segment* pSegment, IFX_Pause* pPause);
106
107   int32_t parseHalftoneRegion(CJBig2_Segment* pSegment, IFX_Pause* pPause);
108
109   int32_t parseGenericRegion(CJBig2_Segment* pSegment, IFX_Pause* pPause);
110
111   int32_t parseGenericRefinementRegion(CJBig2_Segment* pSegment);
112
113   int32_t parseTable(CJBig2_Segment* pSegment);
114
115   int32_t parseRegionInfo(JBig2RegionInfo* pRI);
116
117   JBig2HuffmanCode* decodeSymbolIDHuffmanTable(CJBig2_BitStream* pStream,
118                                                FX_DWORD SBNUMSYMS);
119
120   void huffman_assign_code(int* CODES, int* PREFLEN, int NTEMP);
121
122   void huffman_assign_code(JBig2HuffmanCode* SBSYMCODES, int NTEMP);
123
124  private:
125   CJBig2_Context* m_pGlobalContext;
126   int32_t m_nStreamType;
127   CJBig2_BitStream* m_pStream;
128   int32_t m_nState;
129   CJBig2_List<CJBig2_Segment> m_SegmentList;
130   CJBig2_List<JBig2PageInfo> m_PageInfoList;
131   CJBig2_Image* m_pPage;
132   FX_BOOL m_bBufSpecified;
133   size_t m_nSegmentDecoded;
134   IFX_Pause* m_pPause;
135   int32_t m_PauseStep;
136   FXCODEC_STATUS m_ProcessiveStatus;
137   FX_BOOL m_bFirstPage;
138   CJBig2_ArithDecoder* m_pArithDecoder;
139   CJBig2_GRDProc* m_pGRD;
140   JBig2ArithCtx* m_gbContext;
141   nonstd::unique_ptr<CJBig2_Segment> m_pSegment;
142   FX_DWORD m_dwOffset;
143   JBig2RegionInfo m_ri;
144   std::list<CJBig2_CachePair>* m_pSymbolDictCache;
145 };
146 #endif