OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #include "JBig2_ArithIntDecoder.h" | 7 #include "JBig2_ArithIntDecoder.h" |
8 | 8 |
9 #include "../../../include/fxcrt/fx_memory.h" | 9 #include "../../../include/fxcrt/fx_basic.h" |
10 #include "JBig2_Define.h" | 10 |
| 11 namespace { |
| 12 |
| 13 int ShiftOr(int val, int bitwise_or_val) { |
| 14 return (val << 1) | bitwise_or_val; |
| 15 } |
| 16 |
| 17 const struct ArithIntDecodeData { |
| 18 int nNeedBits; |
| 19 int nValue; |
| 20 } g_ArithIntDecodeData[] = { |
| 21 {2, 0}, |
| 22 {4, 4}, |
| 23 {6, 20}, |
| 24 {8, 84}, |
| 25 {12, 340}, |
| 26 {32, 4436}, |
| 27 }; |
| 28 |
| 29 size_t RecursiveDecode(CJBig2_ArithDecoder* decoder, |
| 30 std::vector<JBig2ArithCtx>* context, |
| 31 int* prev, |
| 32 size_t depth) { |
| 33 static const size_t kDepthEnd = FX_ArraySize(g_ArithIntDecodeData) - 1; |
| 34 if (depth == kDepthEnd) |
| 35 return kDepthEnd; |
| 36 |
| 37 JBig2ArithCtx* pCX = &(*context)[*prev]; |
| 38 int D = decoder->DECODE(pCX); |
| 39 *prev = ShiftOr(*prev, D); |
| 40 if (!D) |
| 41 return depth; |
| 42 return RecursiveDecode(decoder, context, prev, depth + 1); |
| 43 } |
| 44 |
| 45 } // namespace |
11 | 46 |
12 CJBig2_ArithIntDecoder::CJBig2_ArithIntDecoder() { | 47 CJBig2_ArithIntDecoder::CJBig2_ArithIntDecoder() { |
13 IAx = FX_Alloc(JBig2ArithCtx, 512); | 48 m_IAx.resize(512); |
14 JBIG2_memset(IAx, 0, sizeof(JBig2ArithCtx) * 512); | |
15 } | 49 } |
| 50 |
16 CJBig2_ArithIntDecoder::~CJBig2_ArithIntDecoder() { | 51 CJBig2_ArithIntDecoder::~CJBig2_ArithIntDecoder() { |
17 FX_Free(IAx); | |
18 } | 52 } |
19 int CJBig2_ArithIntDecoder::decode(CJBig2_ArithDecoder* pArithDecoder, | 53 |
20 int* nResult) { | 54 bool CJBig2_ArithIntDecoder::decode(CJBig2_ArithDecoder* pArithDecoder, |
21 int PREV, V; | 55 int* nResult) { |
22 int S, D; | 56 int PREV = 1; |
23 int nNeedBits, nTemp, i; | 57 const int S = pArithDecoder->DECODE(&m_IAx[PREV]); |
24 PREV = 1; | 58 PREV = ShiftOr(PREV, S); |
25 S = pArithDecoder->DECODE(IAx + PREV); | 59 |
26 PREV = (PREV << 1) | S; | 60 const size_t nDecodeDataIndex = |
27 D = pArithDecoder->DECODE(IAx + PREV); | 61 RecursiveDecode(pArithDecoder, &m_IAx, &PREV, 0); |
28 PREV = (PREV << 1) | D; | 62 |
29 if (D) { | 63 int nTemp = 0; |
30 D = pArithDecoder->DECODE(IAx + PREV); | 64 for (int i = 0; i < g_ArithIntDecodeData[nDecodeDataIndex].nNeedBits; ++i) { |
31 PREV = (PREV << 1) | D; | 65 int D = pArithDecoder->DECODE(&m_IAx[PREV]); |
32 if (D) { | 66 PREV = ShiftOr(PREV, D); |
33 D = pArithDecoder->DECODE(IAx + PREV); | 67 if (PREV >= 256) |
34 PREV = (PREV << 1) | D; | 68 PREV = (PREV & 511) | 256; |
35 if (D) { | 69 nTemp = ShiftOr(nTemp, D); |
36 D = pArithDecoder->DECODE(IAx + PREV); | |
37 PREV = (PREV << 1) | D; | |
38 if (D) { | |
39 D = pArithDecoder->DECODE(IAx + PREV); | |
40 PREV = (PREV << 1) | D; | |
41 if (D) { | |
42 nNeedBits = 32; | |
43 V = 4436; | |
44 } else { | |
45 nNeedBits = 12; | |
46 V = 340; | |
47 } | |
48 } else { | |
49 nNeedBits = 8; | |
50 V = 84; | |
51 } | |
52 } else { | |
53 nNeedBits = 6; | |
54 V = 20; | |
55 } | |
56 } else { | |
57 nNeedBits = 4; | |
58 V = 4; | |
59 } | |
60 } else { | |
61 nNeedBits = 2; | |
62 V = 0; | |
63 } | 70 } |
64 nTemp = 0; | 71 int nValue = g_ArithIntDecodeData[nDecodeDataIndex].nValue; |
65 for (i = 0; i < nNeedBits; i++) { | 72 nValue += nTemp; |
66 D = pArithDecoder->DECODE(IAx + PREV); | 73 if (S == 1 && nValue > 0) |
67 if (PREV < 256) { | 74 nValue = -nValue; |
68 PREV = (PREV << 1) | D; | 75 |
69 } else { | 76 *nResult = nValue; |
70 PREV = (((PREV << 1) | D) & 511) | 256; | 77 return S != 1 || nValue != 0; |
71 } | 78 } |
72 nTemp = (nTemp << 1) | D; | 79 |
| 80 CJBig2_ArithIaidDecoder::CJBig2_ArithIaidDecoder(unsigned char SBSYMCODELENA) |
| 81 : SBSYMCODELEN(SBSYMCODELENA) { |
| 82 m_IAID.resize(1 << SBSYMCODELEN); |
| 83 } |
| 84 |
| 85 CJBig2_ArithIaidDecoder::~CJBig2_ArithIaidDecoder() { |
| 86 } |
| 87 |
| 88 void CJBig2_ArithIaidDecoder::decode(CJBig2_ArithDecoder* pArithDecoder, |
| 89 FX_DWORD* nResult) { |
| 90 int PREV = 1; |
| 91 for (unsigned char i = 0; i < SBSYMCODELEN; ++i) { |
| 92 JBig2ArithCtx* pCX = &m_IAID[PREV]; |
| 93 int D = pArithDecoder->DECODE(pCX); |
| 94 PREV = ShiftOr(PREV, D); |
73 } | 95 } |
74 V += nTemp; | 96 *nResult = PREV - (1 << SBSYMCODELEN); |
75 if (S == 1 && V > 0) { | |
76 V = -V; | |
77 } | |
78 *nResult = V; | |
79 if (S == 1 && V == 0) { | |
80 return JBIG2_OOB; | |
81 } | |
82 return 0; | |
83 } | 97 } |
84 CJBig2_ArithIaidDecoder::CJBig2_ArithIaidDecoder(unsigned char SBSYMCODELENA) { | |
85 SBSYMCODELEN = SBSYMCODELENA; | |
86 IAID = FX_Alloc(JBig2ArithCtx, 1 << SBSYMCODELEN); | |
87 JBIG2_memset(IAID, 0, sizeof(JBig2ArithCtx) * (int)(1 << SBSYMCODELEN)); | |
88 } | |
89 CJBig2_ArithIaidDecoder::~CJBig2_ArithIaidDecoder() { | |
90 FX_Free(IAID); | |
91 } | |
92 int CJBig2_ArithIaidDecoder::decode(CJBig2_ArithDecoder* pArithDecoder, | |
93 int* nResult) { | |
94 int PREV; | |
95 int D; | |
96 int i; | |
97 PREV = 1; | |
98 for (i = 0; i < SBSYMCODELEN; i++) { | |
99 D = pArithDecoder->DECODE(IAID + PREV); | |
100 PREV = (PREV << 1) | D; | |
101 } | |
102 PREV = PREV - (1 << SBSYMCODELEN); | |
103 *nResult = PREV; | |
104 return 0; | |
105 } | |
OLD | NEW |