OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #ifndef _JBIG2_ARITHMETIC_DECODER_H_ | 7 #ifndef _JBIG2_ARITHMETIC_DECODER_H_ |
8 #define _JBIG2_ARITHMETIC_DECODER_H_ | 8 #define _JBIG2_ARITHMETIC_DECODER_H_ |
9 #include "JBig2_Define.h" | 9 #include "JBig2_Define.h" |
10 #include "JBig2_BitStream.h" | 10 #include "JBig2_BitStream.h" |
11 #include "JBig2_ArithQe.h" | 11 #include "JBig2_ArithQe.h" |
12 typedef struct { | 12 typedef struct { |
13 unsigned int MPS; | 13 unsigned int MPS; |
14 unsigned int I; | 14 unsigned int I; |
15 } JBig2ArithCtx; | 15 } JBig2ArithCtx; |
16 class CJBig2_ArithDecoder : public CJBig2_Object | 16 class CJBig2_ArithDecoder : public CJBig2_Object { |
17 { | 17 public: |
18 public: | 18 CJBig2_ArithDecoder(CJBig2_BitStream* pStream); |
19 | 19 |
20 CJBig2_ArithDecoder(CJBig2_BitStream *pStream); | 20 ~CJBig2_ArithDecoder(); |
21 | 21 |
22 ~CJBig2_ArithDecoder(); | 22 int DECODE(JBig2ArithCtx* pCX); |
23 | 23 |
24 int DECODE(JBig2ArithCtx *pCX); | 24 private: |
25 private: | 25 void INITDEC(); |
26 | 26 |
27 void INITDEC(); | 27 void BYTEIN(); |
| 28 unsigned char B; |
| 29 unsigned int C; |
| 30 unsigned int A; |
| 31 unsigned int CT; |
| 32 CJBig2_BitStream* m_pStream; |
| 33 }; |
| 34 inline CJBig2_ArithDecoder::CJBig2_ArithDecoder(CJBig2_BitStream* pStream) { |
| 35 m_pStream = pStream; |
| 36 INITDEC(); |
| 37 } |
| 38 inline CJBig2_ArithDecoder::~CJBig2_ArithDecoder() {} |
| 39 inline void CJBig2_ArithDecoder::INITDEC() { |
| 40 B = m_pStream->getCurByte_arith(); |
| 41 C = (B ^ 0xff) << 16; |
| 42 ; |
| 43 BYTEIN(); |
| 44 C = C << 7; |
| 45 CT = CT - 7; |
| 46 A = 0x8000; |
| 47 } |
| 48 inline void CJBig2_ArithDecoder::BYTEIN() { |
| 49 unsigned char B1; |
| 50 if (B == 0xff) { |
| 51 B1 = m_pStream->getNextByte_arith(); |
| 52 if (B1 > 0x8f) { |
| 53 CT = 8; |
| 54 } else { |
| 55 m_pStream->incByteIdx(); |
| 56 B = B1; |
| 57 C = C + 0xfe00 - (B << 9); |
| 58 CT = 7; |
| 59 } |
| 60 } else { |
| 61 m_pStream->incByteIdx(); |
| 62 B = m_pStream->getCurByte_arith(); |
| 63 C = C + 0xff00 - (B << 8); |
| 64 CT = 8; |
| 65 } |
| 66 } |
| 67 inline int CJBig2_ArithDecoder::DECODE(JBig2ArithCtx* pCX) { |
| 68 if (!pCX || pCX->I >= JBIG2_QE_NUM) { |
| 69 return 0; |
| 70 } |
28 | 71 |
29 void BYTEIN(); | 72 int D; |
30 unsigned char B; | 73 const JBig2ArithQe* qe = &QeTable[pCX->I]; |
31 unsigned int C; | 74 A = A - qe->Qe; |
32 unsigned int A; | 75 if ((C >> 16) < A) { |
33 unsigned int CT; | 76 if (A & 0x8000) { |
34 CJBig2_BitStream *m_pStream; | 77 D = pCX->MPS; |
35 }; | 78 } else { |
36 inline CJBig2_ArithDecoder::CJBig2_ArithDecoder(CJBig2_BitStream *pStream) | 79 if (A < qe->Qe) { |
37 { | 80 D = 1 - pCX->MPS; |
38 m_pStream = pStream; | 81 if (qe->nSwitch == 1) { |
39 INITDEC(); | 82 pCX->MPS = 1 - pCX->MPS; |
40 } | |
41 inline CJBig2_ArithDecoder::~CJBig2_ArithDecoder() | |
42 { | |
43 } | |
44 inline void CJBig2_ArithDecoder::INITDEC() | |
45 { | |
46 B = m_pStream->getCurByte_arith(); | |
47 C = (B ^ 0xff) << 16;; | |
48 BYTEIN(); | |
49 C = C << 7; | |
50 CT = CT - 7; | |
51 A = 0x8000; | |
52 } | |
53 inline void CJBig2_ArithDecoder::BYTEIN() | |
54 { | |
55 unsigned char B1; | |
56 if(B == 0xff) { | |
57 B1 = m_pStream->getNextByte_arith(); | |
58 if(B1 > 0x8f) { | |
59 CT = 8; | |
60 } else { | |
61 m_pStream->incByteIdx(); | |
62 B = B1; | |
63 C = C + 0xfe00 - (B << 9); | |
64 CT = 7; | |
65 } | 83 } |
| 84 pCX->I = qe->NLPS; |
| 85 } else { |
| 86 D = pCX->MPS; |
| 87 pCX->I = qe->NMPS; |
| 88 } |
| 89 do { |
| 90 if (CT == 0) { |
| 91 BYTEIN(); |
| 92 } |
| 93 A <<= 1; |
| 94 C <<= 1; |
| 95 CT--; |
| 96 } while ((A & 0x8000) == 0); |
| 97 } |
| 98 } else { |
| 99 C -= A << 16; |
| 100 if (A < qe->Qe) { |
| 101 A = qe->Qe; |
| 102 D = pCX->MPS; |
| 103 pCX->I = qe->NMPS; |
66 } else { | 104 } else { |
67 m_pStream->incByteIdx(); | 105 A = qe->Qe; |
68 B = m_pStream->getCurByte_arith(); | 106 D = 1 - pCX->MPS; |
69 C = C + 0xff00 - (B << 8); | 107 if (qe->nSwitch == 1) { |
70 CT = 8; | 108 pCX->MPS = 1 - pCX->MPS; |
| 109 } |
| 110 pCX->I = qe->NLPS; |
71 } | 111 } |
72 } | 112 do { |
73 inline int CJBig2_ArithDecoder::DECODE(JBig2ArithCtx *pCX) | 113 if (CT == 0) { |
74 { | 114 BYTEIN(); |
75 if (!pCX || pCX->I >= JBIG2_QE_NUM) { | 115 } |
76 return 0; | 116 A <<= 1; |
77 } | 117 C <<= 1; |
78 | 118 CT--; |
79 int D; | 119 } while ((A & 0x8000) == 0); |
80 const JBig2ArithQe * qe = &QeTable[pCX->I]; | 120 } |
81 A = A - qe->Qe; | 121 return D; |
82 if((C >> 16) < A) { | |
83 if(A & 0x8000) { | |
84 D = pCX->MPS; | |
85 } else { | |
86 if(A < qe->Qe) { | |
87 D = 1 - pCX->MPS; | |
88 if(qe->nSwitch == 1) { | |
89 pCX->MPS = 1 - pCX->MPS; | |
90 } | |
91 pCX->I = qe->NLPS; | |
92 } else { | |
93 D = pCX->MPS; | |
94 pCX->I = qe->NMPS; | |
95 } | |
96 do { | |
97 if (CT == 0) { | |
98 BYTEIN(); | |
99 } | |
100 A <<= 1; | |
101 C <<= 1; | |
102 CT--; | |
103 } while ((A & 0x8000) == 0); | |
104 } | |
105 } else { | |
106 C -= A << 16; | |
107 if(A < qe->Qe) { | |
108 A = qe->Qe; | |
109 D = pCX->MPS; | |
110 pCX->I = qe->NMPS; | |
111 } else { | |
112 A = qe->Qe; | |
113 D = 1 - pCX->MPS; | |
114 if(qe->nSwitch == 1) { | |
115 pCX->MPS = 1 - pCX->MPS; | |
116 } | |
117 pCX->I = qe->NLPS; | |
118 } | |
119 do { | |
120 if (CT == 0) { | |
121 BYTEIN(); | |
122 } | |
123 A <<= 1; | |
124 C <<= 1; | |
125 CT--; | |
126 } while ((A & 0x8000) == 0); | |
127 } | |
128 return D; | |
129 } | 122 } |
130 #endif | 123 #endif |
OLD | NEW |