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 | 28 unsigned char B; |
29 void BYTEIN(); | 29 unsigned int C; |
30 unsigned char B; | 30 unsigned int A; |
31 unsigned int C; | 31 unsigned int CT; |
32 unsigned int A; | 32 CJBig2_BitStream* m_pStream; |
33 unsigned int CT; | |
34 CJBig2_BitStream *m_pStream; | |
35 }; | 33 }; |
36 inline CJBig2_ArithDecoder::CJBig2_ArithDecoder(CJBig2_BitStream *pStream) | 34 inline CJBig2_ArithDecoder::CJBig2_ArithDecoder(CJBig2_BitStream* pStream) { |
37 { | 35 m_pStream = pStream; |
38 m_pStream = pStream; | 36 INITDEC(); |
39 INITDEC(); | |
40 } | 37 } |
41 inline CJBig2_ArithDecoder::~CJBig2_ArithDecoder() | 38 inline CJBig2_ArithDecoder::~CJBig2_ArithDecoder() { |
42 { | |
43 } | 39 } |
44 inline void CJBig2_ArithDecoder::INITDEC() | 40 inline void CJBig2_ArithDecoder::INITDEC() { |
45 { | 41 B = m_pStream->getCurByte_arith(); |
| 42 C = (B ^ 0xff) << 16; |
| 43 ; |
| 44 BYTEIN(); |
| 45 C = C << 7; |
| 46 CT = CT - 7; |
| 47 A = 0x8000; |
| 48 } |
| 49 inline void CJBig2_ArithDecoder::BYTEIN() { |
| 50 unsigned char B1; |
| 51 if (B == 0xff) { |
| 52 B1 = m_pStream->getNextByte_arith(); |
| 53 if (B1 > 0x8f) { |
| 54 CT = 8; |
| 55 } else { |
| 56 m_pStream->incByteIdx(); |
| 57 B = B1; |
| 58 C = C + 0xfe00 - (B << 9); |
| 59 CT = 7; |
| 60 } |
| 61 } else { |
| 62 m_pStream->incByteIdx(); |
46 B = m_pStream->getCurByte_arith(); | 63 B = m_pStream->getCurByte_arith(); |
47 C = (B ^ 0xff) << 16;; | 64 C = C + 0xff00 - (B << 8); |
48 BYTEIN(); | 65 CT = 8; |
49 C = C << 7; | 66 } |
50 CT = CT - 7; | |
51 A = 0x8000; | |
52 } | 67 } |
53 inline void CJBig2_ArithDecoder::BYTEIN() | 68 inline int CJBig2_ArithDecoder::DECODE(JBig2ArithCtx* pCX) { |
54 { | 69 int D; |
55 unsigned char B1; | 70 const JBig2ArithQe* qe = &QeTable[pCX->I]; |
56 if(B == 0xff) { | 71 A = A - qe->Qe; |
57 B1 = m_pStream->getNextByte_arith(); | 72 if ((C >> 16) < A) { |
58 if(B1 > 0x8f) { | 73 if (A & 0x8000) { |
59 CT = 8; | 74 D = pCX->MPS; |
60 } else { | 75 } else { |
61 m_pStream->incByteIdx(); | 76 if (A < qe->Qe) { |
62 B = B1; | 77 D = 1 - pCX->MPS; |
63 C = C + 0xfe00 - (B << 9); | 78 if (qe->nSwitch == 1) { |
64 CT = 7; | 79 pCX->MPS = 1 - pCX->MPS; |
65 } | 80 } |
| 81 pCX->I = qe->NLPS; |
| 82 } else { |
| 83 D = pCX->MPS; |
| 84 pCX->I = qe->NMPS; |
| 85 } |
| 86 do { |
| 87 if (CT == 0) { |
| 88 BYTEIN(); |
| 89 } |
| 90 A <<= 1; |
| 91 C <<= 1; |
| 92 CT--; |
| 93 } while ((A & 0x8000) == 0); |
| 94 } |
| 95 } else { |
| 96 C -= A << 16; |
| 97 if (A < qe->Qe) { |
| 98 A = qe->Qe; |
| 99 D = pCX->MPS; |
| 100 pCX->I = qe->NMPS; |
66 } else { | 101 } else { |
67 m_pStream->incByteIdx(); | 102 A = qe->Qe; |
68 B = m_pStream->getCurByte_arith(); | 103 D = 1 - pCX->MPS; |
69 C = C + 0xff00 - (B << 8); | 104 if (qe->nSwitch == 1) { |
70 CT = 8; | 105 pCX->MPS = 1 - pCX->MPS; |
| 106 } |
| 107 pCX->I = qe->NLPS; |
71 } | 108 } |
72 } | 109 do { |
73 inline int CJBig2_ArithDecoder::DECODE(JBig2ArithCtx *pCX) | 110 if (CT == 0) { |
74 { | 111 BYTEIN(); |
75 int D; | 112 } |
76 const JBig2ArithQe * qe = &QeTable[pCX->I]; | 113 A <<= 1; |
77 A = A - qe->Qe; | 114 C <<= 1; |
78 if((C >> 16) < A) { | 115 CT--; |
79 if(A & 0x8000) { | 116 } while ((A & 0x8000) == 0); |
80 D = pCX->MPS; | 117 } |
81 } else { | 118 return D; |
82 if(A < qe->Qe) { | |
83 D = 1 - pCX->MPS; | |
84 if(qe->nSwitch == 1) { | |
85 pCX->MPS = 1 - pCX->MPS; | |
86 } | |
87 pCX->I = qe->NLPS; | |
88 } else { | |
89 D = pCX->MPS; | |
90 pCX->I = qe->NMPS; | |
91 } | |
92 do { | |
93 if (CT == 0) { | |
94 BYTEIN(); | |
95 } | |
96 A <<= 1; | |
97 C <<= 1; | |
98 CT--; | |
99 } while ((A & 0x8000) == 0); | |
100 } | |
101 } else { | |
102 C -= A << 16; | |
103 if(A < qe->Qe) { | |
104 A = qe->Qe; | |
105 D = pCX->MPS; | |
106 pCX->I = qe->NMPS; | |
107 } else { | |
108 A = qe->Qe; | |
109 D = 1 - pCX->MPS; | |
110 if(qe->nSwitch == 1) { | |
111 pCX->MPS = 1 - pCX->MPS; | |
112 } | |
113 pCX->I = qe->NLPS; | |
114 } | |
115 do { | |
116 if (CT == 0) { | |
117 BYTEIN(); | |
118 } | |
119 A <<= 1; | |
120 C <<= 1; | |
121 CT--; | |
122 } while ((A & 0x8000) == 0); | |
123 } | |
124 return D; | |
125 } | 119 } |
126 #endif | 120 #endif |
OLD | NEW |