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 #ifndef CORE_FXCODEC_JBIG2_JBIG2_ARITHDECODER_H_
8 #define CORE_FXCODEC_JBIG2_JBIG2_ARITHDECODER_H_
9 
10 #include <stdint.h>
11 
12 #include "core/fxcrt/unowned_ptr.h"
13 
14 class CJBig2_BitStream;
15 struct JBig2ArithQe;
16 
17 class JBig2ArithCtx {
18  public:
19   struct JBig2ArithQe {
20     uint16_t Qe;
21     uint8_t NMPS;
22     uint8_t NLPS;
23     bool bSwitch;
24   };
25 
26   JBig2ArithCtx();
27 
28   int DecodeNLPS(const JBig2ArithQe& qe);
29   int DecodeNMPS(const JBig2ArithQe& qe);
30 
MPS()31   unsigned int MPS() const { return m_MPS ? 1 : 0; }
I()32   unsigned int I() const { return m_I; }
33 
34  private:
35   bool m_MPS = 0;
36   unsigned int m_I = 0;
37 };
38 
39 class CJBig2_ArithDecoder {
40  public:
41   explicit CJBig2_ArithDecoder(CJBig2_BitStream* pStream);
42   ~CJBig2_ArithDecoder();
43 
44   int Decode(JBig2ArithCtx* pCX);
45 
IsComplete()46   bool IsComplete() const { return m_Complete; }
47 
48  private:
49   enum class StreamState : uint8_t {
50     kDataAvailable,
51     kDecodingFinished,
52     kLooping,
53   };
54 
55   void BYTEIN();
56   void ReadValueA();
57 
58   bool m_Complete = false;
59   StreamState m_State = StreamState::kDataAvailable;
60   uint8_t m_B;
61   unsigned int m_C;
62   unsigned int m_A;
63   unsigned int m_CT;
64   UnownedPtr<CJBig2_BitStream> const m_pStream;
65 };
66 
67 #endif  // CORE_FXCODEC_JBIG2_JBIG2_ARITHDECODER_H_
68