| 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 "core/src/fxcodec/jbig2/JBig2_ArithDecoder.h" | 7 #include "core/fxcodec/jbig2/JBig2_ArithDecoder.h" |
| 8 | 8 |
| 9 #include "core/fxcodec/jbig2/JBig2_BitStream.h" |
| 9 #include "core/include/fxcrt/fx_basic.h" | 10 #include "core/include/fxcrt/fx_basic.h" |
| 10 #include "core/src/fxcodec/jbig2/JBig2_BitStream.h" | |
| 11 | 11 |
| 12 namespace { | 12 namespace { |
| 13 | 13 |
| 14 struct JBig2ArithQe { | 14 struct JBig2ArithQe { |
| 15 unsigned int Qe; | 15 unsigned int Qe; |
| 16 unsigned int NMPS; | 16 unsigned int NMPS; |
| 17 unsigned int NLPS; | 17 unsigned int NLPS; |
| 18 unsigned int nSwitch; | 18 unsigned int nSwitch; |
| 19 }; | 19 }; |
| 20 | 20 |
| 21 const JBig2ArithQe kQeTable[] = { | 21 const JBig2ArithQe kQeTable[] = { |
| 22 // Stupid hack to keep clang-format from reformatting this badly. | 22 // Stupid hack to keep clang-format from reformatting this badly. |
| 23 {0x5601, 1, 1, 1}, | 23 {0x5601, 1, 1, 1}, {0x3401, 2, 6, 0}, {0x1801, 3, 9, 0}, |
| 24 {0x3401, 2, 6, 0}, | 24 {0x0AC1, 4, 12, 0}, {0x0521, 5, 29, 0}, {0x0221, 38, 33, 0}, |
| 25 {0x1801, 3, 9, 0}, | 25 {0x5601, 7, 6, 1}, {0x5401, 8, 14, 0}, {0x4801, 9, 14, 0}, |
| 26 {0x0AC1, 4, 12, 0}, | 26 {0x3801, 10, 14, 0}, {0x3001, 11, 17, 0}, {0x2401, 12, 18, 0}, |
| 27 {0x0521, 5, 29, 0}, | 27 {0x1C01, 13, 20, 0}, {0x1601, 29, 21, 0}, {0x5601, 15, 14, 1}, |
| 28 {0x0221, 38, 33, 0}, | 28 {0x5401, 16, 14, 0}, {0x5101, 17, 15, 0}, {0x4801, 18, 16, 0}, |
| 29 {0x5601, 7, 6, 1}, | 29 {0x3801, 19, 17, 0}, {0x3401, 20, 18, 0}, {0x3001, 21, 19, 0}, |
| 30 {0x5401, 8, 14, 0}, | 30 {0x2801, 22, 19, 0}, {0x2401, 23, 20, 0}, {0x2201, 24, 21, 0}, |
| 31 {0x4801, 9, 14, 0}, | 31 {0x1C01, 25, 22, 0}, {0x1801, 26, 23, 0}, {0x1601, 27, 24, 0}, |
| 32 {0x3801, 10, 14, 0}, | 32 {0x1401, 28, 25, 0}, {0x1201, 29, 26, 0}, {0x1101, 30, 27, 0}, |
| 33 {0x3001, 11, 17, 0}, | 33 {0x0AC1, 31, 28, 0}, {0x09C1, 32, 29, 0}, {0x08A1, 33, 30, 0}, |
| 34 {0x2401, 12, 18, 0}, | 34 {0x0521, 34, 31, 0}, {0x0441, 35, 32, 0}, {0x02A1, 36, 33, 0}, |
| 35 {0x1C01, 13, 20, 0}, | 35 {0x0221, 37, 34, 0}, {0x0141, 38, 35, 0}, {0x0111, 39, 36, 0}, |
| 36 {0x1601, 29, 21, 0}, | 36 {0x0085, 40, 37, 0}, {0x0049, 41, 38, 0}, {0x0025, 42, 39, 0}, |
| 37 {0x5601, 15, 14, 1}, | 37 {0x0015, 43, 40, 0}, {0x0009, 44, 41, 0}, {0x0005, 45, 42, 0}, |
| 38 {0x5401, 16, 14, 0}, | 38 {0x0001, 45, 43, 0}, {0x5601, 46, 46, 0}}; |
| 39 {0x5101, 17, 15, 0}, | |
| 40 {0x4801, 18, 16, 0}, | |
| 41 {0x3801, 19, 17, 0}, | |
| 42 {0x3401, 20, 18, 0}, | |
| 43 {0x3001, 21, 19, 0}, | |
| 44 {0x2801, 22, 19, 0}, | |
| 45 {0x2401, 23, 20, 0}, | |
| 46 {0x2201, 24, 21, 0}, | |
| 47 {0x1C01, 25, 22, 0}, | |
| 48 {0x1801, 26, 23, 0}, | |
| 49 {0x1601, 27, 24, 0}, | |
| 50 {0x1401, 28, 25, 0}, | |
| 51 {0x1201, 29, 26, 0}, | |
| 52 {0x1101, 30, 27, 0}, | |
| 53 {0x0AC1, 31, 28, 0}, | |
| 54 {0x09C1, 32, 29, 0}, | |
| 55 {0x08A1, 33, 30, 0}, | |
| 56 {0x0521, 34, 31, 0}, | |
| 57 {0x0441, 35, 32, 0}, | |
| 58 {0x02A1, 36, 33, 0}, | |
| 59 {0x0221, 37, 34, 0}, | |
| 60 {0x0141, 38, 35, 0}, | |
| 61 {0x0111, 39, 36, 0}, | |
| 62 {0x0085, 40, 37, 0}, | |
| 63 {0x0049, 41, 38, 0}, | |
| 64 {0x0025, 42, 39, 0}, | |
| 65 {0x0015, 43, 40, 0}, | |
| 66 {0x0009, 44, 41, 0}, | |
| 67 {0x0005, 45, 42, 0}, | |
| 68 {0x0001, 45, 43, 0}, | |
| 69 {0x5601, 46, 46, 0}}; | |
| 70 | 39 |
| 71 const unsigned int kDefaultAValue = 0x8000; | 40 const unsigned int kDefaultAValue = 0x8000; |
| 72 | 41 |
| 73 int DecodeNMPS(JBig2ArithCtx* pCX, const JBig2ArithQe& qe) { | 42 int DecodeNMPS(JBig2ArithCtx* pCX, const JBig2ArithQe& qe) { |
| 74 pCX->I = qe.NMPS; | 43 pCX->I = qe.NMPS; |
| 75 return pCX->MPS; | 44 return pCX->MPS; |
| 76 } | 45 } |
| 77 | 46 |
| 78 int DecodeNLPS(JBig2ArithCtx* pCX, const JBig2ArithQe& qe) { | 47 int DecodeNLPS(JBig2ArithCtx* pCX, const JBig2ArithQe& qe) { |
| 79 // TODO(thestig): |D|, |MPS| and friends probably should be booleans. | 48 // TODO(thestig): |D|, |MPS| and friends probably should be booleans. |
| 80 int D = 1 - pCX->MPS; | 49 int D = 1 - pCX->MPS; |
| 81 if (qe.nSwitch == 1) | 50 if (qe.nSwitch == 1) |
| 82 pCX->MPS = 1 - pCX->MPS; | 51 pCX->MPS = 1 - pCX->MPS; |
| 83 pCX->I = qe.NLPS; | 52 pCX->I = qe.NLPS; |
| 84 return D; | 53 return D; |
| 85 } | 54 } |
| 86 | 55 |
| 87 } // namespace | 56 } // namespace |
| 88 | 57 |
| 89 CJBig2_ArithDecoder::CJBig2_ArithDecoder(CJBig2_BitStream* pStream) | 58 CJBig2_ArithDecoder::CJBig2_ArithDecoder(CJBig2_BitStream* pStream) |
| 90 : m_pStream(pStream) { | 59 : m_pStream(pStream) { |
| 91 m_B = m_pStream->getCurByte_arith(); | 60 m_B = m_pStream->getCurByte_arith(); |
| 92 m_C = (m_B ^ 0xff) << 16; | 61 m_C = (m_B ^ 0xff) << 16; |
| 93 BYTEIN(); | 62 BYTEIN(); |
| 94 m_C = m_C << 7; | 63 m_C = m_C << 7; |
| 95 m_CT = m_CT - 7; | 64 m_CT = m_CT - 7; |
| 96 m_A = kDefaultAValue; | 65 m_A = kDefaultAValue; |
| 97 } | 66 } |
| 98 | 67 |
| 99 CJBig2_ArithDecoder::~CJBig2_ArithDecoder() { | 68 CJBig2_ArithDecoder::~CJBig2_ArithDecoder() {} |
| 100 } | |
| 101 | 69 |
| 102 int CJBig2_ArithDecoder::DECODE(JBig2ArithCtx* pCX) { | 70 int CJBig2_ArithDecoder::DECODE(JBig2ArithCtx* pCX) { |
| 103 if (!pCX || pCX->I >= FX_ArraySize(kQeTable)) | 71 if (!pCX || pCX->I >= FX_ArraySize(kQeTable)) |
| 104 return 0; | 72 return 0; |
| 105 | 73 |
| 106 const JBig2ArithQe& qe = kQeTable[pCX->I]; | 74 const JBig2ArithQe& qe = kQeTable[pCX->I]; |
| 107 m_A -= qe.Qe; | 75 m_A -= qe.Qe; |
| 108 if ((m_C >> 16) < m_A) { | 76 if ((m_C >> 16) < m_A) { |
| 109 if (m_A & kDefaultAValue) | 77 if (m_A & kDefaultAValue) |
| 110 return pCX->MPS; | 78 return pCX->MPS; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 | 111 |
| 144 void CJBig2_ArithDecoder::ReadValueA() { | 112 void CJBig2_ArithDecoder::ReadValueA() { |
| 145 do { | 113 do { |
| 146 if (m_CT == 0) | 114 if (m_CT == 0) |
| 147 BYTEIN(); | 115 BYTEIN(); |
| 148 m_A <<= 1; | 116 m_A <<= 1; |
| 149 m_C <<= 1; | 117 m_C <<= 1; |
| 150 --m_CT; | 118 --m_CT; |
| 151 } while ((m_A & kDefaultAValue) == 0); | 119 } while ((m_A & kDefaultAValue) == 0); |
| 152 } | 120 } |
| OLD | NEW |