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_GENERAL_DECODER_H_
8#define _JBIG2_GENERAL_DECODER_H_
9#include "../../../include/fxcodec/fx_codec_def.h"
10#include "../../../include/fxcrt/fx_basic.h"
11#include "JBig2_Define.h"
12#include "JBig2_SymbolDict.h"
13#include "JBig2_ArithDecoder.h"
14#include "JBig2_ArithIntDecoder.h"
15#include "../../../include/fxcrt/fx_coordinates.h"
16class CJBig2_HuffmanTable;
17class CJBig2_Image;
18class CJBig2_PatternDict;
19typedef enum {
20    JBIG2_CORNER_BOTTOMLEFT = 0,
21    JBIG2_CORNER_TOPLEFT	= 1,
22    JBIG2_CORNER_BOTTOMRIGHT = 2,
23    JBIG2_CORNER_TOPRIGHT	= 3
24} JBig2Corner;
25class CJBig2_GRDProc : public CJBig2_Object
26{
27public:
28    CJBig2_GRDProc()
29    {
30        m_loopIndex = 0;
31        m_pLine = NULL;
32        m_pPause = NULL;
33        m_DecodeType = 0;
34        LTP = 0;
35        m_ReplaceRect.left = 0;
36        m_ReplaceRect.bottom = 0;
37        m_ReplaceRect.top = 0;
38        m_ReplaceRect.right = 0;
39    }
40
41    CJBig2_Image *decode_Arith(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
42
43    CJBig2_Image *decode_Arith_V2(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
44
45    CJBig2_Image *decode_Arith_V1(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
46
47    CJBig2_Image *decode_MMR(CJBig2_BitStream *pStream);
48    FXCODEC_STATUS Start_decode_Arith(CJBig2_Image** pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause = NULL);
49    FXCODEC_STATUS Start_decode_Arith_V2(CJBig2_Image** pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause = NULL);
50    FXCODEC_STATUS Start_decode_Arith_V1(CJBig2_Image** pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause = NULL);
51    FXCODEC_STATUS Start_decode_MMR(CJBig2_Image** pImage, CJBig2_BitStream *pStream, IFX_Pause* pPause = NULL);
52    FXCODEC_STATUS Continue_decode(IFX_Pause* pPause);
53    FX_RECT		   GetReplaceRect()
54    {
55        return m_ReplaceRect;
56    };
57private:
58    FXCODEC_STATUS decode_Arith(IFX_Pause* pPause);
59    FXCODEC_STATUS decode_Arith_V2(IFX_Pause* pPause);
60    FXCODEC_STATUS decode_Arith_V1(IFX_Pause* pPause);
61    FXCODEC_STATUS decode_MMR();
62    FXCODEC_STATUS decode_Arith_Template0_opt3(CJBig2_Image*pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause);
63    FXCODEC_STATUS decode_Arith_Template0_unopt(CJBig2_Image *pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause);
64    FXCODEC_STATUS decode_Arith_Template1_opt3(CJBig2_Image *pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause);
65    FXCODEC_STATUS decode_Arith_Template1_unopt(CJBig2_Image * pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause);
66    FXCODEC_STATUS decode_Arith_Template2_opt3(CJBig2_Image *pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause);
67    FXCODEC_STATUS decode_Arith_Template2_unopt(CJBig2_Image * pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause);
68    FXCODEC_STATUS decode_Arith_Template3_opt3(CJBig2_Image *pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause);
69    FXCODEC_STATUS decode_Arith_Template3_unopt(CJBig2_Image * pImage, CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause);
70    FX_DWORD	m_loopIndex;
71    FX_BYTE *	m_pLine;
72    IFX_Pause*	m_pPause;
73    FXCODEC_STATUS	m_ProssiveStatus;
74    CJBig2_Image** m_pImage;
75    CJBig2_ArithDecoder *m_pArithDecoder;
76    JBig2ArithCtx *m_gbContext;
77    FX_WORD		m_DecodeType;
78    FX_BOOL LTP;
79    FX_RECT m_ReplaceRect;
80private:
81
82    CJBig2_Image *decode_Arith_Template0_opt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
83
84    CJBig2_Image *decode_Arith_Template0_opt2(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
85
86    CJBig2_Image *decode_Arith_Template0_opt3(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
87
88    CJBig2_Image *decode_Arith_Template0_unopt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
89
90    CJBig2_Image *decode_Arith_Template1_opt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
91
92    CJBig2_Image *decode_Arith_Template1_opt2(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
93
94    CJBig2_Image *decode_Arith_Template1_opt3(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
95
96    CJBig2_Image *decode_Arith_Template1_unopt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
97
98    CJBig2_Image *decode_Arith_Template2_opt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
99
100    CJBig2_Image *decode_Arith_Template2_opt2(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
101
102    CJBig2_Image *decode_Arith_Template2_opt3(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
103
104    CJBig2_Image *decode_Arith_Template2_unopt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
105
106    CJBig2_Image *decode_Arith_Template3_opt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
107
108    CJBig2_Image *decode_Arith_Template3_opt2(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
109
110    CJBig2_Image *decode_Arith_Template3_opt3(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
111
112    CJBig2_Image *decode_Arith_Template3_unopt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext);
113public:
114    FX_BOOL MMR;
115    FX_DWORD GBW;
116    FX_DWORD GBH;
117    FX_BYTE GBTEMPLATE;
118    FX_BOOL TPGDON;
119    FX_BOOL USESKIP;
120    CJBig2_Image * SKIP;
121    signed char GBAT[8];
122};
123class CJBig2_GRRDProc : public CJBig2_Object
124{
125public:
126
127    CJBig2_Image *decode(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext);
128
129    CJBig2_Image *decode_Template0_unopt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext);
130
131    CJBig2_Image *decode_Template0_opt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext);
132
133    CJBig2_Image *decode_Template1_unopt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext);
134
135    CJBig2_Image *decode_Template1_opt(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext);
136
137    CJBig2_Image *decode_V1(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext);
138public:
139    FX_DWORD GRW;
140    FX_DWORD GRH;
141    FX_BOOL GRTEMPLATE;
142    CJBig2_Image *GRREFERENCE;
143    FX_INT32 GRREFERENCEDX;
144    FX_INT32 GRREFERENCEDY;
145    FX_BOOL TPGRON;
146    signed char	GRAT[4];
147};
148typedef struct {
149    CJBig2_ArithIntDecoder *IADT,
150                           *IAFS,
151                           *IADS,
152                           *IAIT,
153                           *IARI,
154                           *IARDW,
155                           *IARDH,
156                           *IARDX,
157                           *IARDY;
158    CJBig2_ArithIaidDecoder *IAID;
159} JBig2IntDecoderState;
160class CJBig2_TRDProc : public CJBig2_Object
161{
162public:
163
164    CJBig2_Image *decode_Huffman(CJBig2_BitStream *pStream, JBig2ArithCtx *grContext);
165
166    CJBig2_Image *decode_Arith(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *grContext,
167                               JBig2IntDecoderState *pIDS = NULL);
168public:
169    FX_BOOL SBHUFF;
170    FX_BOOL SBREFINE;
171    FX_DWORD SBW;
172    FX_DWORD SBH;
173    FX_DWORD SBNUMINSTANCES;
174    FX_DWORD SBSTRIPS;
175    FX_DWORD SBNUMSYMS;
176
177    JBig2HuffmanCode *SBSYMCODES;
178    FX_BYTE SBSYMCODELEN;
179
180    CJBig2_Image **SBSYMS;
181    FX_BOOL SBDEFPIXEL;
182
183    JBig2ComposeOp SBCOMBOP;
184    FX_BOOL TRANSPOSED;
185
186    JBig2Corner REFCORNER;
187    signed char SBDSOFFSET;
188    CJBig2_HuffmanTable *SBHUFFFS,
189                        *SBHUFFDS,
190                        *SBHUFFDT,
191                        *SBHUFFRDW,
192                        *SBHUFFRDH,
193                        *SBHUFFRDX,
194                        *SBHUFFRDY,
195                        *SBHUFFRSIZE;
196    FX_BOOL SBRTEMPLATE;
197    signed char SBRAT[4];
198};
199class CJBig2_SDDProc : public CJBig2_Object
200{
201public:
202
203    CJBig2_SymbolDict *decode_Arith(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, JBig2ArithCtx *grContext);
204
205    CJBig2_SymbolDict *decode_Huffman(CJBig2_BitStream *pStream, JBig2ArithCtx *gbContext, JBig2ArithCtx *grContext, IFX_Pause* pPause);
206public:
207    FX_BOOL SDHUFF;
208    FX_BOOL SDREFAGG;
209    FX_DWORD SDNUMINSYMS;
210    CJBig2_Image ** SDINSYMS;
211    FX_DWORD SDNUMNEWSYMS;
212    FX_DWORD SDNUMEXSYMS;
213    CJBig2_HuffmanTable *SDHUFFDH,
214                        *SDHUFFDW,
215                        *SDHUFFBMSIZE,
216                        *SDHUFFAGGINST;
217    FX_BYTE SDTEMPLATE;
218    signed char SDAT[8];
219    FX_BOOL SDRTEMPLATE;
220    signed char SDRAT[4];
221};
222class CJBig2_HTRDProc : public CJBig2_Object
223{
224public:
225
226    CJBig2_Image *decode_Arith(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause);
227
228    CJBig2_Image *decode_MMR(CJBig2_BitStream *pStream, IFX_Pause* pPause);
229public:
230    FX_DWORD HBW,
231             HBH;
232    FX_BOOL HMMR;
233    FX_BYTE HTEMPLATE;
234    FX_DWORD HNUMPATS;
235    CJBig2_Image **HPATS;
236    FX_BOOL HDEFPIXEL;
237    JBig2ComposeOp HCOMBOP;
238    FX_BOOL HENABLESKIP;
239    FX_DWORD HGW,
240             HGH;
241    FX_INT32 HGX,
242             HGY;
243    FX_WORD HRX,
244            HRY;
245    FX_BYTE HPW,
246            HPH;
247};
248class CJBig2_PDDProc : public CJBig2_Object
249{
250public:
251
252    CJBig2_PatternDict *decode_Arith(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause);
253
254    CJBig2_PatternDict *decode_MMR(CJBig2_BitStream *pStream, IFX_Pause* pPause);
255public:
256    FX_BOOL HDMMR;
257    FX_BYTE HDPW,
258            HDPH;
259    FX_DWORD GRAYMAX;
260    FX_BYTE HDTEMPLATE;
261};
262class CJBig2_GSIDProc : public CJBig2_Object
263{
264public:
265
266    FX_DWORD *decode_Arith(CJBig2_ArithDecoder *pArithDecoder, JBig2ArithCtx *gbContext, IFX_Pause* pPause);
267
268    FX_DWORD *decode_MMR(CJBig2_BitStream *pStream, IFX_Pause* pPause);
269public:
270    FX_BOOL GSMMR;
271    FX_BOOL GSUSESKIP;
272    FX_BYTE GSBPP;
273    FX_DWORD GSW,
274             GSH;
275    FX_BYTE GSTEMPLATE;
276    CJBig2_Image *GSKIP;
277};
278#endif
279