summaryrefslogtreecommitdiff
path: root/core/src/fxcodec/jbig2/JBig2_ArithDecoder.h
blob: 7acfe9cd3681af3824fc7112b6058d03c5325b62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com

#ifndef _JBIG2_ARITHMETIC_DECODER_H_
#define _JBIG2_ARITHMETIC_DECODER_H_
#include "JBig2_Define.h"
#include "JBig2_BitStream.h"
#include "JBig2_ArithQe.h"

struct JBig2ArithCtx {
  unsigned int MPS;
  unsigned int I;
};

class CJBig2_ArithDecoder {
 public:
  explicit CJBig2_ArithDecoder(CJBig2_BitStream* pStream);

  ~CJBig2_ArithDecoder();

  int DECODE(JBig2ArithCtx* pCX);

 private:
  void INITDEC();
  void BYTEIN();

  unsigned char B;
  unsigned int C;
  unsigned int A;
  unsigned int CT;
  CJBig2_BitStream* m_pStream;
};

inline CJBig2_ArithDecoder::CJBig2_ArithDecoder(CJBig2_BitStream* pStream) {
  m_pStream = pStream;
  INITDEC();
}
inline CJBig2_ArithDecoder::~CJBig2_ArithDecoder() {}
inline void CJBig2_ArithDecoder::INITDEC() {
  B = m_pStream->getCurByte_arith();
  C = (B ^ 0xff) << 16;
  ;
  BYTEIN();
  C = C << 7;
  CT = CT - 7;
  A = 0x8000;
}
inline void CJBig2_ArithDecoder::BYTEIN() {
  unsigned char B1;
  if (B == 0xff) {
    B1 = m_pStream->getNextByte_arith();
    if (B1 > 0x8f) {
      CT = 8;
    } else {
      m_pStream->incByteIdx();
      B = B1;
      C = C + 0xfe00 - (B << 9);
      CT = 7;
    }
  } else {
    m_pStream->incByteIdx();
    B = m_pStream->getCurByte_arith();
    C = C + 0xff00 - (B << 8);
    CT = 8;
  }
}
inline int CJBig2_ArithDecoder::DECODE(JBig2ArithCtx* pCX) {
  if (!pCX || pCX->I >= JBIG2_QE_NUM) {
    return 0;
  }

  int D;
  const JBig2ArithQe* qe = &QeTable[pCX->I];
  A = A - qe->Qe;
  if ((C >> 16) < A) {
    if (A & 0x8000) {
      D = pCX->MPS;
    } else {
      if (A < qe->Qe) {
        D = 1 - pCX->MPS;
        if (qe->nSwitch == 1) {
          pCX->MPS = 1 - pCX->MPS;
        }
        pCX->I = qe->NLPS;
      } else {
        D = pCX->MPS;
        pCX->I = qe->NMPS;
      }
      do {
        if (CT == 0) {
          BYTEIN();
        }
        A <<= 1;
        C <<= 1;
        CT--;
      } while ((A & 0x8000) == 0);
    }
  } else {
    C -= A << 16;
    if (A < qe->Qe) {
      A = qe->Qe;
      D = pCX->MPS;
      pCX->I = qe->NMPS;
    } else {
      A = qe->Qe;
      D = 1 - pCX->MPS;
      if (qe->nSwitch == 1) {
        pCX->MPS = 1 - pCX->MPS;
      }
      pCX->I = qe->NLPS;
    }
    do {
      if (CT == 0) {
        BYTEIN();
      }
      A <<= 1;
      C <<= 1;
      CT--;
    } while ((A & 0x8000) == 0);
  }
  return D;
}
#endif