| 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 // TODO(thestig): |D|, |MPS| and friends probably should be booleans. |
| 80 int D = 1 - pCX->MPS; |
| 81 if (qe.nSwitch == 1) |
| 82 pCX->MPS = 1 - pCX->MPS; |
| 83 pCX->I = qe.NLPS; |
| 84 return D; |
| 85 } |
| 86 |
| 72 } // namespace | 87 } // namespace |
| 73 | 88 |
| 74 CJBig2_ArithDecoder::CJBig2_ArithDecoder(CJBig2_BitStream* pStream) { | 89 CJBig2_ArithDecoder::CJBig2_ArithDecoder(CJBig2_BitStream* pStream) |
| 75 m_pStream = pStream; | 90 : m_pStream(pStream) { |
| 76 INITDEC(); | 91 m_B = m_pStream->getCurByte_arith(); |
| 92 m_C = (m_B ^ 0xff) << 16; |
| 93 BYTEIN(); |
| 94 m_C = m_C << 7; |
| 95 m_CT = m_CT - 7; |
| 96 m_A = kDefaultAValue; |
| 77 } | 97 } |
| 78 | 98 |
| 79 CJBig2_ArithDecoder::~CJBig2_ArithDecoder() { | 99 CJBig2_ArithDecoder::~CJBig2_ArithDecoder() { |
| 80 } | 100 } |
| 81 | 101 |
| 82 int CJBig2_ArithDecoder::DECODE(JBig2ArithCtx* pCX) { | 102 int CJBig2_ArithDecoder::DECODE(JBig2ArithCtx* pCX) { |
| 83 if (!pCX || pCX->I >= FX_ArraySize(kQeTable)) { | 103 if (!pCX || pCX->I >= FX_ArraySize(kQeTable)) |
| 84 return 0; | 104 return 0; |
| 105 |
| 106 const JBig2ArithQe& qe = kQeTable[pCX->I]; |
| 107 m_A -= qe.Qe; |
| 108 if ((m_C >> 16) < m_A) { |
| 109 if (m_A & kDefaultAValue) |
| 110 return pCX->MPS; |
| 111 |
| 112 const int D = m_A < qe.Qe ? DecodeNLPS(pCX, qe) : DecodeNMPS(pCX, qe); |
| 113 ReadValueA(); |
| 114 return D; |
| 85 } | 115 } |
| 86 | 116 |
| 87 int D; | 117 m_C -= m_A << 16; |
| 88 const JBig2ArithQe* qe = &kQeTable[pCX->I]; | 118 const int D = m_A < qe.Qe ? DecodeNMPS(pCX, qe) : DecodeNLPS(pCX, qe); |
| 89 A = A - qe->Qe; | 119 m_A = qe.Qe; |
| 90 if ((C >> 16) < A) { | 120 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; | 121 return D; |
| 137 } | 122 } |
| 138 | 123 |
| 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() { | 124 void CJBig2_ArithDecoder::BYTEIN() { |
| 149 unsigned char B1; | 125 unsigned char B1; |
| 150 if (B == 0xff) { | 126 if (m_B == 0xff) { |
| 151 B1 = m_pStream->getNextByte_arith(); | 127 B1 = m_pStream->getNextByte_arith(); |
| 152 if (B1 > 0x8f) { | 128 if (B1 > 0x8f) { |
| 153 CT = 8; | 129 m_CT = 8; |
| 154 } else { | 130 } else { |
| 155 m_pStream->incByteIdx(); | 131 m_pStream->incByteIdx(); |
| 156 B = B1; | 132 m_B = B1; |
| 157 C = C + 0xfe00 - (B << 9); | 133 m_C = m_C + 0xfe00 - (m_B << 9); |
| 158 CT = 7; | 134 m_CT = 7; |
| 159 } | 135 } |
| 160 } else { | 136 } else { |
| 161 m_pStream->incByteIdx(); | 137 m_pStream->incByteIdx(); |
| 162 B = m_pStream->getCurByte_arith(); | 138 m_B = m_pStream->getCurByte_arith(); |
| 163 C = C + 0xff00 - (B << 8); | 139 m_C = m_C + 0xff00 - (m_B << 8); |
| 164 CT = 8; | 140 m_CT = 8; |
| 165 } | 141 } |
| 166 } | 142 } |
| 143 |
| 144 void CJBig2_ArithDecoder::ReadValueA() { |
| 145 do { |
| 146 if (m_CT == 0) |
| 147 BYTEIN(); |
| 148 m_A <<= 1; |
| 149 m_C <<= 1; |
| 150 --m_CT; |
| 151 } while ((m_A & kDefaultAValue) == 0); |
| 152 } |
| OLD | NEW |