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