Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
JBig2_ArithDecoder.cpp
Go to the documentation of this file.
1// Copyright 2014 The PDFium Authors
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 "core/fxcodec/jbig2/JBig2_ArithDecoder.h"
8
9#include <iterator>
10
11#include "core/fxcodec/jbig2/JBig2_BitStream.h"
12#include "third_party/base/check_op.h"
13
14namespace {
15
16const JBig2ArithCtx::JBig2ArithQe kQeTable[] = {
17 // Stupid hack to keep clang-format from reformatting this badly.
18 {0x5601, 1, 1, true}, {0x3401, 2, 6, false}, {0x1801, 3, 9, false},
19 {0x0AC1, 4, 12, false}, {0x0521, 5, 29, false}, {0x0221, 38, 33, false},
20 {0x5601, 7, 6, true}, {0x5401, 8, 14, false}, {0x4801, 9, 14, false},
21 {0x3801, 10, 14, false}, {0x3001, 11, 17, false}, {0x2401, 12, 18, false},
22 {0x1C01, 13, 20, false}, {0x1601, 29, 21, false}, {0x5601, 15, 14, true},
23 {0x5401, 16, 14, false}, {0x5101, 17, 15, false}, {0x4801, 18, 16, false},
24 {0x3801, 19, 17, false}, {0x3401, 20, 18, false}, {0x3001, 21, 19, false},
25 {0x2801, 22, 19, false}, {0x2401, 23, 20, false}, {0x2201, 24, 21, false},
26 {0x1C01, 25, 22, false}, {0x1801, 26, 23, false}, {0x1601, 27, 24, false},
27 {0x1401, 28, 25, false}, {0x1201, 29, 26, false}, {0x1101, 30, 27, false},
28 {0x0AC1, 31, 28, false}, {0x09C1, 32, 29, false}, {0x08A1, 33, 30, false},
29 {0x0521, 34, 31, false}, {0x0441, 35, 32, false}, {0x02A1, 36, 33, false},
30 {0x0221, 37, 34, false}, {0x0141, 38, 35, false}, {0x0111, 39, 36, false},
31 {0x0085, 40, 37, false}, {0x0049, 41, 38, false}, {0x0025, 42, 39, false},
32 {0x0015, 43, 40, false}, {0x0009, 44, 41, false}, {0x0005, 45, 42, false},
33 {0x0001, 45, 43, false}, {0x5601, 46, 46, false}};
34
35const unsigned int kDefaultAValue = 0x8000;
36
37} // namespace
38
39JBig2ArithCtx::JBig2ArithCtx() = default;
40
42 bool D = !m_MPS;
43 if (qe.bSwitch)
44 m_MPS = !m_MPS;
45 m_I = qe.NLPS;
46 DCHECK_LT(m_I, std::size(kQeTable));
47 return D;
48}
49
51 m_I = qe.NMPS;
52 DCHECK_LT(m_I, std::size(kQeTable));
53 return MPS();
54}
55
58 m_B = m_pStream->getCurByte_arith();
59 m_C = (m_B ^ 0xff) << 16;
60 BYTEIN();
61 m_C = m_C << 7;
62 m_CT = m_CT - 7;
63 m_A = kDefaultAValue;
64}
65
67
69 CHECK_LT(pCX->I(), std::size(kQeTable));
70
71 const JBig2ArithCtx::JBig2ArithQe& qe = kQeTable[pCX->I()];
72 m_A -= qe.Qe;
73 if ((m_C >> 16) < m_A) {
74 if (m_A & kDefaultAValue)
75 return pCX->MPS();
76
77 const int D = m_A < qe.Qe ? pCX->DecodeNLPS(qe) : pCX->DecodeNMPS(qe);
78 ReadValueA();
79 return D;
80 }
81
82 m_C -= m_A << 16;
83 const int D = m_A < qe.Qe ? pCX->DecodeNMPS(qe) : pCX->DecodeNLPS(qe);
84 m_A = qe.Qe;
85 ReadValueA();
86 return D;
87}
88
89void CJBig2_ArithDecoder::BYTEIN() {
90 if (m_B == 0xff) {
91 unsigned char B1 = m_pStream->getNextByte_arith();
92 if (B1 > 0x8f) {
93 m_CT = 8;
94
95 switch (m_State) {
96 case StreamState::kDataAvailable:
97 // Finished decoding data (see JBIG2 spec, Section E.3.4).
98 m_State = StreamState::kDecodingFinished;
99 break;
100 case StreamState::kDecodingFinished:
101 // Allow one more call in the finished state. https://crbug.com/947622
102 m_State = StreamState::kLooping;
103 break;
104 case StreamState::kLooping:
105 // Looping state detected. Mark decoding as complete to bail out.
106 // https://crbug.com/767156
107 m_Complete = true;
108 break;
109 }
110 } else {
111 m_pStream->incByteIdx();
112 m_B = B1;
113 m_C = m_C + 0xfe00 - (m_B << 9);
114 m_CT = 7;
115 }
116 } else {
117 m_pStream->incByteIdx();
118 m_B = m_pStream->getCurByte_arith();
119 m_C = m_C + 0xff00 - (m_B << 8);
120 m_CT = 8;
121 }
122
123 if (!m_pStream->IsInBounds())
124 m_Complete = true;
125}
126
127void CJBig2_ArithDecoder::ReadValueA() {
128 do {
129 if (m_CT == 0)
130 BYTEIN();
131 m_A <<= 1;
132 m_C <<= 1;
133 --m_CT;
134 } while ((m_A & kDefaultAValue) == 0);
135}
CJBig2_ArithDecoder(CJBig2_BitStream *pStream)
int Decode(JBig2ArithCtx *pCX)
int DecodeNLPS(const JBig2ArithQe &qe)
unsigned int I() const
int DecodeNMPS(const JBig2ArithQe &qe)
unsigned int MPS() const