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 #include "JBig2_ArithDecoder.h" | 7 #include "JBig2_ArithDecoder.h" |
8 | 8 |
9 #include "../../../include/fxcrt/fx_basic.h" | 9 #include "../../../include/fxcrt/fx_basic.h" |
10 #include "JBig2_BitStream.h" | 10 #include "JBig2_BitStream.h" |
11 #include "JBig2_Define.h" | |
12 | 11 |
13 namespace { | 12 namespace { |
14 | 13 |
15 struct JBig2ArithQe { | 14 struct JBig2ArithQe { |
16 unsigned int Qe; | 15 unsigned int Qe; |
17 unsigned int NMPS; | 16 unsigned int NMPS; |
18 unsigned int NLPS; | 17 unsigned int NLPS; |
19 unsigned int nSwitch; | 18 unsigned int nSwitch; |
20 }; | 19 }; |
21 | 20 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
62 {0x0111, 39, 36, 0}, | 61 {0x0111, 39, 36, 0}, |
63 {0x0085, 40, 37, 0}, | 62 {0x0085, 40, 37, 0}, |
64 {0x0049, 41, 38, 0}, | 63 {0x0049, 41, 38, 0}, |
65 {0x0025, 42, 39, 0}, | 64 {0x0025, 42, 39, 0}, |
66 {0x0015, 43, 40, 0}, | 65 {0x0015, 43, 40, 0}, |
67 {0x0009, 44, 41, 0}, | 66 {0x0009, 44, 41, 0}, |
68 {0x0005, 45, 42, 0}, | 67 {0x0005, 45, 42, 0}, |
69 {0x0001, 45, 43, 0}, | 68 {0x0001, 45, 43, 0}, |
70 {0x5601, 46, 46, 0}}; | 69 {0x5601, 46, 46, 0}}; |
71 | 70 |
71 const unsigned int kDefaultAValue = 0x8000; | |
72 | |
73 int DecodeNMPS(JBig2ArithCtx* pCX, const JBig2ArithQe& qe) { | |
74 pCX->I = qe.NMPS; | |
75 return pCX->MPS; | |
76 } | |
77 | |
78 int DecodeNLPS(JBig2ArithCtx* pCX, const JBig2ArithQe& qe) { | |
79 int D = 1 - pCX->MPS; | |
Tom Sepez
2015/09/29 17:56:50
Nit: not today, but I think D and pCX->MPS are rea
Lei Zhang
2015/09/30 04:14:44
Added a TODO.
| |
80 if (qe.nSwitch == 1) | |
81 pCX->MPS = 1 - pCX->MPS; | |
Tom Sepez
2015/09/29 17:56:50
nit: same here.
| |
82 pCX->I = qe.NLPS; | |
83 return D; | |
84 } | |
85 | |
72 } // namespace | 86 } // namespace |
73 | 87 |
74 CJBig2_ArithDecoder::CJBig2_ArithDecoder(CJBig2_BitStream* pStream) { | 88 CJBig2_ArithDecoder::CJBig2_ArithDecoder(CJBig2_BitStream* pStream) |
75 m_pStream = pStream; | 89 : m_pStream(pStream) { |
76 INITDEC(); | 90 m_B = m_pStream->getCurByte_arith(); |
91 m_C = (m_B ^ 0xff) << 16; | |
92 BYTEIN(); | |
93 m_C = m_C << 7; | |
94 m_CT = m_CT - 7; | |
95 m_A = kDefaultAValue; | |
77 } | 96 } |
78 | 97 |
79 CJBig2_ArithDecoder::~CJBig2_ArithDecoder() { | 98 CJBig2_ArithDecoder::~CJBig2_ArithDecoder() { |
80 } | 99 } |
81 | 100 |
82 int CJBig2_ArithDecoder::DECODE(JBig2ArithCtx* pCX) { | 101 int CJBig2_ArithDecoder::DECODE(JBig2ArithCtx* pCX) { |
83 if (!pCX || pCX->I >= FX_ArraySize(kQeTable)) { | 102 if (!pCX || pCX->I >= FX_ArraySize(kQeTable)) |
84 return 0; | 103 return 0; |
104 | |
105 const JBig2ArithQe& qe = kQeTable[pCX->I]; | |
106 m_A -= qe.Qe; | |
107 if ((m_C >> 16) < m_A) { | |
108 if (m_A & kDefaultAValue) | |
109 return pCX->MPS; | |
110 | |
111 const int D = m_A < qe.Qe ? DecodeNLPS(pCX, qe) : DecodeNMPS(pCX, qe); | |
112 ReadValueA(); | |
113 return D; | |
85 } | 114 } |
86 | 115 |
87 int D; | 116 m_C -= m_A << 16; |
88 const JBig2ArithQe* qe = &kQeTable[pCX->I]; | 117 const int D = m_A < qe.Qe ? DecodeNMPS(pCX, qe) : DecodeNLPS(pCX, qe); |
89 A = A - qe->Qe; | 118 m_A = qe.Qe; |
90 if ((C >> 16) < A) { | 119 ReadValueA(); |
91 if (A & 0x8000) { | |
92 D = pCX->MPS; | |
93 } else { | |
94 if (A < qe->Qe) { | |
95 D = 1 - pCX->MPS; | |
96 if (qe->nSwitch == 1) { | |
97 pCX->MPS = 1 - pCX->MPS; | |
98 } | |
99 pCX->I = qe->NLPS; | |
100 } else { | |
101 D = pCX->MPS; | |
102 pCX->I = qe->NMPS; | |
103 } | |
104 do { | |
105 if (CT == 0) { | |
106 BYTEIN(); | |
107 } | |
108 A <<= 1; | |
109 C <<= 1; | |
110 CT--; | |
111 } while ((A & 0x8000) == 0); | |
112 } | |
113 } else { | |
114 C -= A << 16; | |
115 if (A < qe->Qe) { | |
116 A = qe->Qe; | |
117 D = pCX->MPS; | |
118 pCX->I = qe->NMPS; | |
119 } else { | |
120 A = qe->Qe; | |
121 D = 1 - pCX->MPS; | |
122 if (qe->nSwitch == 1) { | |
123 pCX->MPS = 1 - pCX->MPS; | |
124 } | |
125 pCX->I = qe->NLPS; | |
126 } | |
127 do { | |
128 if (CT == 0) { | |
129 BYTEIN(); | |
130 } | |
131 A <<= 1; | |
132 C <<= 1; | |
133 CT--; | |
134 } while ((A & 0x8000) == 0); | |
135 } | |
136 return D; | 120 return D; |
137 } | 121 } |
138 | 122 |
139 void CJBig2_ArithDecoder::INITDEC() { | |
140 B = m_pStream->getCurByte_arith(); | |
141 C = (B ^ 0xff) << 16; | |
142 BYTEIN(); | |
143 C = C << 7; | |
144 CT = CT - 7; | |
145 A = 0x8000; | |
146 } | |
147 | |
148 void CJBig2_ArithDecoder::BYTEIN() { | 123 void CJBig2_ArithDecoder::BYTEIN() { |
149 unsigned char B1; | 124 unsigned char B1; |
150 if (B == 0xff) { | 125 if (m_B == 0xff) { |
151 B1 = m_pStream->getNextByte_arith(); | 126 B1 = m_pStream->getNextByte_arith(); |
152 if (B1 > 0x8f) { | 127 if (B1 > 0x8f) { |
153 CT = 8; | 128 m_CT = 8; |
154 } else { | 129 } else { |
155 m_pStream->incByteIdx(); | 130 m_pStream->incByteIdx(); |
156 B = B1; | 131 m_B = B1; |
157 C = C + 0xfe00 - (B << 9); | 132 m_C = m_C + 0xfe00 - (m_B << 9); |
158 CT = 7; | 133 m_CT = 7; |
159 } | 134 } |
160 } else { | 135 } else { |
161 m_pStream->incByteIdx(); | 136 m_pStream->incByteIdx(); |
162 B = m_pStream->getCurByte_arith(); | 137 m_B = m_pStream->getCurByte_arith(); |
163 C = C + 0xff00 - (B << 8); | 138 m_C = m_C + 0xff00 - (m_B << 8); |
164 CT = 8; | 139 m_CT = 8; |
165 } | 140 } |
166 } | 141 } |
142 | |
143 void CJBig2_ArithDecoder::ReadValueA() { | |
144 do { | |
145 if (m_CT == 0) | |
146 BYTEIN(); | |
147 m_A <<= 1; | |
148 m_C <<= 1; | |
149 --m_CT; | |
150 } while ((m_A & kDefaultAValue) == 0); | |
151 } | |
OLD | NEW |