_FETAL => _FATAL.
[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 "JBig2_Module.h"
14 #include "JBig2_List.h"
15 #include "JBig2_Segment.h"
16 #include "JBig2_Page.h"
17 #include "JBig2_GeneralDecoder.h"
18 #include "../../../include/fxcodec/fx_codec_def.h"
19
20 typedef std::pair<uint8_t*, CJBig2_SymbolDict*> CJBig2_CachePair;
21 typedef enum {
22     JBIG2_OUT_OF_PAGE = 0,
23     JBIG2_IN_PAGE,
24 } JBig2State;
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_FILE_STREAM                       0
35 #define JBIG2_SQUENTIAL_STREAM          1
36 #define JBIG2_RANDOM_STREAM                     2
37 #define JBIG2_EMBED_STREAM                      3
38 #define JBIG2_MIN_SEGMENT_SIZE                                  11
39 class CJBig2_Context : public CJBig2_Object
40 {
41 public:
42
43     static CJBig2_Context *CreateContext(CJBig2_Module *pModule, uint8_t *pGlobalData, FX_DWORD dwGlobalLength,
44                                          uint8_t *pData, FX_DWORD dwLength, int32_t nStreamType, std::list<CJBig2_CachePair>* pSymbolDictCache, IFX_Pause* pPause = NULL);
45
46     static void DestroyContext(CJBig2_Context *pContext);
47
48     int32_t getFirstPage(uint8_t *pBuf, int32_t width, int32_t height, int32_t stride, IFX_Pause* pPause);
49
50     int32_t getNextPage(uint8_t *pBuf, int32_t width, int32_t height, int32_t stride, IFX_Pause* pPause);
51
52     int32_t getFirstPage(CJBig2_Image **image, IFX_Pause* pPause);
53
54     int32_t getNextPage(CJBig2_Image **image, IFX_Pause* pPause);
55     int32_t Continue(IFX_Pause* pPause);
56     FXCODEC_STATUS GetProcessiveStatus()
57     {
58         return m_ProcessiveStatus;
59     };
60 private:
61
62     CJBig2_Context(uint8_t *pGlobalData, FX_DWORD dwGlobalLength,
63                    uint8_t *pData, FX_DWORD dwLength, int32_t nStreamType, std::list<CJBig2_CachePair>* pSymbolDictCache, IFX_Pause* pPause);
64
65     ~CJBig2_Context();
66
67     int32_t decodeFile(IFX_Pause* pPause);
68
69     int32_t decode_SquentialOrgnazation(IFX_Pause* pPause);
70
71     int32_t decode_EmbedOrgnazation(IFX_Pause* pPause);
72
73     int32_t decode_RandomOrgnazation_FirstPage(IFX_Pause* pPause);
74
75     int32_t decode_RandomOrgnazation(IFX_Pause* pPause);
76
77     CJBig2_Segment *findSegmentByNumber(FX_DWORD dwNumber);
78
79     CJBig2_Segment *findReferredSegmentByTypeAndIndex(CJBig2_Segment *pSegment, uint8_t cType, 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 ProcessiveParseSegmentData(CJBig2_Segment *pSegment, IFX_Pause* pPause);
85
86     int32_t parseSymbolDict(CJBig2_Segment *pSegment, IFX_Pause* pPause);
87
88     int32_t parseTextRegion(CJBig2_Segment *pSegment);
89
90     int32_t parsePatternDict(CJBig2_Segment *pSegment, IFX_Pause* pPause);
91
92     int32_t parseHalftoneRegion(CJBig2_Segment *pSegment, IFX_Pause* pPause);
93
94     int32_t parseGenericRegion(CJBig2_Segment *pSegment, IFX_Pause* pPause);
95
96     int32_t parseGenericRefinementRegion(CJBig2_Segment *pSegment);
97
98     int32_t parseTable(CJBig2_Segment *pSegment);
99
100     int32_t parseRegionInfo(JBig2RegionInfo *pRI);
101
102
103
104     JBig2HuffmanCode *decodeSymbolIDHuffmanTable(CJBig2_BitStream *pStream, 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
112     CJBig2_Context *m_pGlobalContext;
113
114     int32_t m_nStreamType;
115
116     CJBig2_BitStream *m_pStream;
117
118     int32_t m_nState;
119
120     CJBig2_List<CJBig2_Segment> *m_pSegmentList;
121
122     CJBig2_List<JBig2PageInfo> *m_pPageInfoList;
123
124     CJBig2_Image *m_pPage;
125
126     FX_BOOL m_bBufSpecified;
127
128     int32_t m_nSegmentDecoded;
129     IFX_Pause*  m_pPause;
130     int32_t     m_PauseStep;
131     FXCODEC_STATUS m_ProcessiveStatus;
132     FX_BOOL     m_bFirstPage;
133     CJBig2_ArithDecoder *m_pArithDecoder;
134     CJBig2_GRDProc *m_pGRD;
135     JBig2ArithCtx *m_gbContext;
136     CJBig2_Segment *m_pSegment;
137     FX_DWORD m_dwOffset;
138     JBig2RegionInfo m_ri;
139     std::list<CJBig2_CachePair>* m_pSymbolDictCache;
140 };
141 #endif