OLD | NEW |
(Empty) | |
| 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 #include "JBig2_ArithDecoder.h" |
| 8 |
| 9 #include "../../../include/fxcrt/fx_basic.h" |
| 10 #include "JBig2_BitStream.h" |
| 11 #include "JBig2_Define.h" |
| 12 |
| 13 namespace { |
| 14 |
| 15 struct JBig2ArithQe { |
| 16 unsigned int Qe; |
| 17 unsigned int NMPS; |
| 18 unsigned int NLPS; |
| 19 unsigned int nSwitch; |
| 20 }; |
| 21 |
| 22 const JBig2ArithQe kQeTable[] = { |
| 23 // Stupid hack to keep clang-format from reformatting this badly. |
| 24 {0x5601, 1, 1, 1}, |
| 25 {0x3401, 2, 6, 0}, |
| 26 {0x1801, 3, 9, 0}, |
| 27 {0x0AC1, 4, 12, 0}, |
| 28 {0x0521, 5, 29, 0}, |
| 29 {0x0221, 38, 33, 0}, |
| 30 {0x5601, 7, 6, 1}, |
| 31 {0x5401, 8, 14, 0}, |
| 32 {0x4801, 9, 14, 0}, |
| 33 {0x3801, 10, 14, 0}, |
| 34 {0x3001, 11, 17, 0}, |
| 35 {0x2401, 12, 18, 0}, |
| 36 {0x1C01, 13, 20, 0}, |
| 37 {0x1601, 29, 21, 0}, |
| 38 {0x5601, 15, 14, 1}, |
| 39 {0x5401, 16, 14, 0}, |
| 40 {0x5101, 17, 15, 0}, |
| 41 {0x4801, 18, 16, 0}, |
| 42 {0x3801, 19, 17, 0}, |
| 43 {0x3401, 20, 18, 0}, |
| 44 {0x3001, 21, 19, 0}, |
| 45 {0x2801, 22, 19, 0}, |
| 46 {0x2401, 23, 20, 0}, |
| 47 {0x2201, 24, 21, 0}, |
| 48 {0x1C01, 25, 22, 0}, |
| 49 {0x1801, 26, 23, 0}, |
| 50 {0x1601, 27, 24, 0}, |
| 51 {0x1401, 28, 25, 0}, |
| 52 {0x1201, 29, 26, 0}, |
| 53 {0x1101, 30, 27, 0}, |
| 54 {0x0AC1, 31, 28, 0}, |
| 55 {0x09C1, 32, 29, 0}, |
| 56 {0x08A1, 33, 30, 0}, |
| 57 {0x0521, 34, 31, 0}, |
| 58 {0x0441, 35, 32, 0}, |
| 59 {0x02A1, 36, 33, 0}, |
| 60 {0x0221, 37, 34, 0}, |
| 61 {0x0141, 38, 35, 0}, |
| 62 {0x0111, 39, 36, 0}, |
| 63 {0x0085, 40, 37, 0}, |
| 64 {0x0049, 41, 38, 0}, |
| 65 {0x0025, 42, 39, 0}, |
| 66 {0x0015, 43, 40, 0}, |
| 67 {0x0009, 44, 41, 0}, |
| 68 {0x0005, 45, 42, 0}, |
| 69 {0x0001, 45, 43, 0}, |
| 70 {0x5601, 46, 46, 0}}; |
| 71 |
| 72 } // namespace |
| 73 |
| 74 CJBig2_ArithDecoder::CJBig2_ArithDecoder(CJBig2_BitStream* pStream) { |
| 75 m_pStream = pStream; |
| 76 INITDEC(); |
| 77 } |
| 78 |
| 79 CJBig2_ArithDecoder::~CJBig2_ArithDecoder() { |
| 80 } |
| 81 |
| 82 int CJBig2_ArithDecoder::DECODE(JBig2ArithCtx* pCX) { |
| 83 if (!pCX || pCX->I >= FX_ArraySize(kQeTable)) { |
| 84 return 0; |
| 85 } |
| 86 |
| 87 int D; |
| 88 const JBig2ArithQe* qe = &kQeTable[pCX->I]; |
| 89 A = A - qe->Qe; |
| 90 if ((C >> 16) < A) { |
| 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; |
| 137 } |
| 138 |
| 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() { |
| 149 unsigned char B1; |
| 150 if (B == 0xff) { |
| 151 B1 = m_pStream->getNextByte_arith(); |
| 152 if (B1 > 0x8f) { |
| 153 CT = 8; |
| 154 } else { |
| 155 m_pStream->incByteIdx(); |
| 156 B = B1; |
| 157 C = C + 0xfe00 - (B << 9); |
| 158 CT = 7; |
| 159 } |
| 160 } else { |
| 161 m_pStream->incByteIdx(); |
| 162 B = m_pStream->getCurByte_arith(); |
| 163 C = C + 0xff00 - (B << 8); |
| 164 CT = 8; |
| 165 } |
| 166 } |
OLD | NEW |