| 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 CORE_SRC_FXCODEC_JBIG2_JBIG2_ARITHDECODER_H_ |
| 8 #define _JBIG2_ARITHMETIC_DECODER_H_ | 8 #define CORE_SRC_FXCODEC_JBIG2_JBIG2_ARITHDECODER_H_ |
| 9 #include "JBig2_Define.h" | 9 |
| 10 #include "JBig2_BitStream.h" | 10 class CJBig2_BitStream; |
| 11 #include "JBig2_ArithQe.h" | |
| 12 | 11 |
| 13 struct JBig2ArithCtx { | 12 struct JBig2ArithCtx { |
| 14 unsigned int MPS; | 13 unsigned int MPS; |
| 15 unsigned int I; | 14 unsigned int I; |
| 16 }; | 15 }; |
| 17 | 16 |
| 18 class CJBig2_ArithDecoder { | 17 class CJBig2_ArithDecoder { |
| 19 public: | 18 public: |
| 20 explicit CJBig2_ArithDecoder(CJBig2_BitStream* pStream); | 19 explicit CJBig2_ArithDecoder(CJBig2_BitStream* pStream); |
| 21 | 20 |
| 22 ~CJBig2_ArithDecoder(); | 21 ~CJBig2_ArithDecoder(); |
| 23 | 22 |
| 24 int DECODE(JBig2ArithCtx* pCX); | 23 int DECODE(JBig2ArithCtx* pCX); |
| 25 | 24 |
| 26 private: | 25 private: |
| 27 void INITDEC(); | 26 void INITDEC(); |
| 28 void BYTEIN(); | 27 void BYTEIN(); |
| 29 | 28 |
| 30 unsigned char B; | 29 unsigned char B; |
| 31 unsigned int C; | 30 unsigned int C; |
| 32 unsigned int A; | 31 unsigned int A; |
| 33 unsigned int CT; | 32 unsigned int CT; |
| 34 CJBig2_BitStream* m_pStream; | 33 CJBig2_BitStream* m_pStream; |
| 35 }; | 34 }; |
| 36 | 35 |
| 37 inline CJBig2_ArithDecoder::CJBig2_ArithDecoder(CJBig2_BitStream* pStream) { | 36 #endif // CORE_SRC_FXCODEC_JBIG2_JBIG2_ARITHDECODER_H_ |
| 38 m_pStream = pStream; | |
| 39 INITDEC(); | |
| 40 } | |
| 41 inline CJBig2_ArithDecoder::~CJBig2_ArithDecoder() {} | |
| 42 inline void CJBig2_ArithDecoder::INITDEC() { | |
| 43 B = m_pStream->getCurByte_arith(); | |
| 44 C = (B ^ 0xff) << 16; | |
| 45 ; | |
| 46 BYTEIN(); | |
| 47 C = C << 7; | |
| 48 CT = CT - 7; | |
| 49 A = 0x8000; | |
| 50 } | |
| 51 inline void CJBig2_ArithDecoder::BYTEIN() { | |
| 52 unsigned char B1; | |
| 53 if (B == 0xff) { | |
| 54 B1 = m_pStream->getNextByte_arith(); | |
| 55 if (B1 > 0x8f) { | |
| 56 CT = 8; | |
| 57 } else { | |
| 58 m_pStream->incByteIdx(); | |
| 59 B = B1; | |
| 60 C = C + 0xfe00 - (B << 9); | |
| 61 CT = 7; | |
| 62 } | |
| 63 } else { | |
| 64 m_pStream->incByteIdx(); | |
| 65 B = m_pStream->getCurByte_arith(); | |
| 66 C = C + 0xff00 - (B << 8); | |
| 67 CT = 8; | |
| 68 } | |
| 69 } | |
| 70 inline int CJBig2_ArithDecoder::DECODE(JBig2ArithCtx* pCX) { | |
| 71 if (!pCX || pCX->I >= JBIG2_QE_NUM) { | |
| 72 return 0; | |
| 73 } | |
| 74 | |
| 75 int D; | |
| 76 const JBig2ArithQe* qe = &QeTable[pCX->I]; | |
| 77 A = A - qe->Qe; | |
| 78 if ((C >> 16) < A) { | |
| 79 if (A & 0x8000) { | |
| 80 D = pCX->MPS; | |
| 81 } else { | |
| 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 } | |
| 126 #endif | |
| OLD | NEW |