OLD | NEW |
| (Empty) |
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 #include "core/src/fxcodec/jbig2/JBig2_ArithIntDecoder.h" | |
8 | |
9 #include <vector> | |
10 | |
11 #include "core/include/fxcrt/fx_basic.h" | |
12 | |
13 namespace { | |
14 | |
15 int ShiftOr(int val, int bitwise_or_val) { | |
16 return (val << 1) | bitwise_or_val; | |
17 } | |
18 | |
19 const struct ArithIntDecodeData { | |
20 int nNeedBits; | |
21 int nValue; | |
22 } g_ArithIntDecodeData[] = { | |
23 {2, 0}, | |
24 {4, 4}, | |
25 {6, 20}, | |
26 {8, 84}, | |
27 {12, 340}, | |
28 {32, 4436}, | |
29 }; | |
30 | |
31 size_t RecursiveDecode(CJBig2_ArithDecoder* decoder, | |
32 std::vector<JBig2ArithCtx>* context, | |
33 int* prev, | |
34 size_t depth) { | |
35 static const size_t kDepthEnd = FX_ArraySize(g_ArithIntDecodeData) - 1; | |
36 if (depth == kDepthEnd) | |
37 return kDepthEnd; | |
38 | |
39 JBig2ArithCtx* pCX = &(*context)[*prev]; | |
40 int D = decoder->DECODE(pCX); | |
41 *prev = ShiftOr(*prev, D); | |
42 if (!D) | |
43 return depth; | |
44 return RecursiveDecode(decoder, context, prev, depth + 1); | |
45 } | |
46 | |
47 } // namespace | |
48 | |
49 CJBig2_ArithIntDecoder::CJBig2_ArithIntDecoder() { | |
50 m_IAx.resize(512); | |
51 } | |
52 | |
53 CJBig2_ArithIntDecoder::~CJBig2_ArithIntDecoder() { | |
54 } | |
55 | |
56 bool CJBig2_ArithIntDecoder::decode(CJBig2_ArithDecoder* pArithDecoder, | |
57 int* nResult) { | |
58 int PREV = 1; | |
59 const int S = pArithDecoder->DECODE(&m_IAx[PREV]); | |
60 PREV = ShiftOr(PREV, S); | |
61 | |
62 const size_t nDecodeDataIndex = | |
63 RecursiveDecode(pArithDecoder, &m_IAx, &PREV, 0); | |
64 | |
65 int nTemp = 0; | |
66 for (int i = 0; i < g_ArithIntDecodeData[nDecodeDataIndex].nNeedBits; ++i) { | |
67 int D = pArithDecoder->DECODE(&m_IAx[PREV]); | |
68 PREV = ShiftOr(PREV, D); | |
69 if (PREV >= 256) | |
70 PREV = (PREV & 511) | 256; | |
71 nTemp = ShiftOr(nTemp, D); | |
72 } | |
73 int nValue = g_ArithIntDecodeData[nDecodeDataIndex].nValue; | |
74 nValue += nTemp; | |
75 if (S == 1 && nValue > 0) | |
76 nValue = -nValue; | |
77 | |
78 *nResult = nValue; | |
79 return S != 1 || nValue != 0; | |
80 } | |
81 | |
82 CJBig2_ArithIaidDecoder::CJBig2_ArithIaidDecoder(unsigned char SBSYMCODELENA) | |
83 : SBSYMCODELEN(SBSYMCODELENA) { | |
84 m_IAID.resize(1 << SBSYMCODELEN); | |
85 } | |
86 | |
87 CJBig2_ArithIaidDecoder::~CJBig2_ArithIaidDecoder() { | |
88 } | |
89 | |
90 void CJBig2_ArithIaidDecoder::decode(CJBig2_ArithDecoder* pArithDecoder, | |
91 FX_DWORD* nResult) { | |
92 int PREV = 1; | |
93 for (unsigned char i = 0; i < SBSYMCODELEN; ++i) { | |
94 JBig2ArithCtx* pCX = &m_IAID[PREV]; | |
95 int D = pArithDecoder->DECODE(pCX); | |
96 PREV = ShiftOr(PREV, D); | |
97 } | |
98 *nResult = PREV - (1 << SBSYMCODELEN); | |
99 } | |
OLD | NEW |