Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
JBig2_ArithIntDecoder.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_ArithIntDecoder.h"
8
9
#
include
<
array
>
10
#
include
<
vector
>
11
12
#
include
"core/fxcrt/fx_safe_types.h"
13
#
include
"core/fxcrt/stl_util.h"
14
15
namespace
{
16
17
int
ShiftOr(
int
val,
int
bitwise_or_val) {
18
return
(val << 1) | bitwise_or_val;
19
}
20
21
struct
ArithIntDecodeData {
22
int
nNeedBits;
23
int
nValue;
24
};
25
26
constexpr
auto
kArithIntDecodeData =
fxcrt
::ToArray<ArithIntDecodeData>({
27
{2, 0},
28
{4, 4},
29
{6, 20},
30
{8, 84},
31
{12, 340},
32
{32, 4436},
33
});
34
35
size_t RecursiveDecode(
CJBig2_ArithDecoder
* decoder,
36
std::vector<JBig2ArithCtx>* context,
37
int
* prev,
38
size_t depth) {
39
static
const
size_t kDepthEnd =
std
::size(kArithIntDecodeData) - 1;
40
if
(depth == kDepthEnd)
41
return
kDepthEnd;
42
43
JBig2ArithCtx
* pCX = &(*context)[*prev];
44
int
D = decoder
->
Decode
(
pCX
)
;
45
*prev = ShiftOr(*prev, D);
46
if
(!D)
47
return
depth;
48
return
RecursiveDecode(decoder, context, prev, depth + 1);
49
}
50
51
}
// namespace
52
53
CJBig2_ArithIntDecoder
::
CJBig2_ArithIntDecoder
() {
54
m_IAx.resize(512);
55
}
56
57
CJBig2_ArithIntDecoder
::~
CJBig2_ArithIntDecoder
() =
default
;
58
59
bool
CJBig2_ArithIntDecoder
::
Decode
(
CJBig2_ArithDecoder
* pArithDecoder,
60
int
* nResult) {
61
// This decoding algorithm is explained in "Annex A - Arithmetic Integer
62
// Decoding Procedure" on page 113 of the JBIG2 specification (ISO/IEC FCD
63
// 14492).
64
int
PREV = 1;
65
const
int
S = pArithDecoder
->
Decode
(&m_IAx[PREV]);
66
PREV = ShiftOr(PREV, S);
67
68
const
size_t nDecodeDataIndex =
69
RecursiveDecode(pArithDecoder, &m_IAx, &PREV, 0);
70
71
int
nTemp = 0;
72
for
(
int
i = 0; i < kArithIntDecodeData[nDecodeDataIndex].nNeedBits; ++i) {
73
int
D = pArithDecoder
->
Decode
(&m_IAx[PREV]);
74
PREV = ShiftOr(PREV, D);
75
if
(PREV >= 256)
76
PREV = (PREV & 511) | 256;
77
nTemp = ShiftOr(nTemp, D);
78
}
79
FX_SAFE_INT32
safeValue = kArithIntDecodeData[nDecodeDataIndex].nValue;
80
safeValue += nTemp;
81
82
// Value does not fit in int.
83
if
(!safeValue.IsValid()) {
84
*nResult = 0;
85
return
false
;
86
}
87
88
int
nValue = safeValue.ValueOrDie();
89
if
(S == 1 && nValue > 0)
90
nValue = -nValue;
91
92
*nResult = nValue;
93
return
S != 1 || nValue != 0;
94
}
95
96
CJBig2_ArithIaidDecoder
::
CJBig2_ArithIaidDecoder
(
unsigned
char
SBSYMCODELENA)
97
: SBSYMCODELEN(SBSYMCODELENA) {
98
m_IAID.resize(
static_cast
<size_t>(1) << SBSYMCODELEN);
99
}
100
101
CJBig2_ArithIaidDecoder
::~
CJBig2_ArithIaidDecoder
() =
default
;
102
103
void
CJBig2_ArithIaidDecoder
::
Decode
(
CJBig2_ArithDecoder
* pArithDecoder,
104
uint32_t* nResult) {
105
int
PREV = 1;
106
for
(
unsigned
char
i = 0; i < SBSYMCODELEN; ++i) {
107
JBig2ArithCtx
* pCX = &m_IAID[PREV];
108
int
D = pArithDecoder
->
Decode
(
pCX
)
;
109
PREV = ShiftOr(PREV, D);
110
}
111
*nResult = PREV - (1 << SBSYMCODELEN);
112
}
CJBig2_ArithDecoder
Definition
JBig2_ArithDecoder.h:41
CJBig2_ArithDecoder::Decode
int Decode(JBig2ArithCtx *pCX)
Definition
JBig2_ArithDecoder.cpp:70
CJBig2_ArithIaidDecoder
Definition
JBig2_ArithIntDecoder.h:29
CJBig2_ArithIaidDecoder::Decode
void Decode(CJBig2_ArithDecoder *pArithDecoder, uint32_t *nResult)
Definition
JBig2_ArithIntDecoder.cpp:103
CJBig2_ArithIaidDecoder::CJBig2_ArithIaidDecoder
CJBig2_ArithIaidDecoder(unsigned char SBSYMCODELENA)
Definition
JBig2_ArithIntDecoder.cpp:96
CJBig2_ArithIaidDecoder::~CJBig2_ArithIaidDecoder
~CJBig2_ArithIaidDecoder()
CJBig2_ArithIntDecoder
Definition
JBig2_ArithIntDecoder.h:16
CJBig2_ArithIntDecoder::Decode
bool Decode(CJBig2_ArithDecoder *pArithDecoder, int *nResult)
Definition
JBig2_ArithIntDecoder.cpp:59
CJBig2_ArithIntDecoder::CJBig2_ArithIntDecoder
CJBig2_ArithIntDecoder()
Definition
JBig2_ArithIntDecoder.cpp:53
CJBig2_ArithIntDecoder::~CJBig2_ArithIntDecoder
~CJBig2_ArithIntDecoder()
JBig2ArithCtx
Definition
JBig2_ArithDecoder.h:18
FX_SAFE_INT32
pdfium::CheckedNumeric< int32_t > FX_SAFE_INT32
Definition
fx_safe_types.h:15
fxcrt
Definition
stl_util.h:17
std
[33]
Definition
src_corelib_tools_qhash.cpp:421
qtwebengine
src
3rdparty
chromium
third_party
pdfium
core
fxcodec
jbig2
JBig2_ArithIntDecoder.cpp
Generated on
for Qt by
1.18.0