1 //========================================================================
3 // JArithmeticDecoder.cc
5 // Copyright 2002-2004 Glyph & Cog, LLC
7 //========================================================================
11 #ifdef USE_GCC_PRAGMAS
12 #pragma implementation
17 #include "JArithmeticDecoder.h"
19 //------------------------------------------------------------------------
20 // JArithmeticDecoderStates
21 //------------------------------------------------------------------------
23 JArithmeticDecoderStats::JArithmeticDecoderStats(int contextSizeA
) {
24 contextSize
= contextSizeA
;
25 cxTab
= (Guchar
*)gmallocn(contextSize
, sizeof(Guchar
));
29 JArithmeticDecoderStats::~JArithmeticDecoderStats() {
33 JArithmeticDecoderStats
*JArithmeticDecoderStats::copy() {
34 JArithmeticDecoderStats
*stats
;
36 stats
= new JArithmeticDecoderStats(contextSize
);
37 memcpy(stats
->cxTab
, cxTab
, contextSize
);
41 void JArithmeticDecoderStats::reset() {
42 memset(cxTab
, 0, contextSize
);
45 void JArithmeticDecoderStats::copyFrom(JArithmeticDecoderStats
*stats
) {
46 memcpy(cxTab
, stats
->cxTab
, contextSize
);
49 void JArithmeticDecoderStats::setEntry(Guint cx
, int i
, int mps
) {
50 cxTab
[cx
] = (i
<< 1) + mps
;
53 //------------------------------------------------------------------------
55 //------------------------------------------------------------------------
57 Guint
JArithmeticDecoder::qeTab
[47] = {
58 0x56010000, 0x34010000, 0x18010000, 0x0AC10000,
59 0x05210000, 0x02210000, 0x56010000, 0x54010000,
60 0x48010000, 0x38010000, 0x30010000, 0x24010000,
61 0x1C010000, 0x16010000, 0x56010000, 0x54010000,
62 0x51010000, 0x48010000, 0x38010000, 0x34010000,
63 0x30010000, 0x28010000, 0x24010000, 0x22010000,
64 0x1C010000, 0x18010000, 0x16010000, 0x14010000,
65 0x12010000, 0x11010000, 0x0AC10000, 0x09C10000,
66 0x08A10000, 0x05210000, 0x04410000, 0x02A10000,
67 0x02210000, 0x01410000, 0x01110000, 0x00850000,
68 0x00490000, 0x00250000, 0x00150000, 0x00090000,
69 0x00050000, 0x00010000, 0x56010000
72 int JArithmeticDecoder::nmpsTab
[47] = {
73 1, 2, 3, 4, 5, 38, 7, 8, 9, 10, 11, 12, 13, 29, 15, 16,
74 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
75 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 45, 46
78 int JArithmeticDecoder::nlpsTab
[47] = {
79 1, 6, 9, 12, 29, 33, 6, 14, 14, 14, 17, 18, 20, 21, 14, 14,
80 15, 16, 17, 18, 19, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
81 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 46
84 int JArithmeticDecoder::switchTab
[47] = {
85 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0,
86 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
87 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
90 JArithmeticDecoder::JArithmeticDecoder() {
97 inline Guint
JArithmeticDecoder::readByte() {
105 return (Guint
)str
->getChar() & 0xff;
108 JArithmeticDecoder::~JArithmeticDecoder() {
112 void JArithmeticDecoder::start() {
117 c
= (buf0
^ 0xff) << 16;
124 void JArithmeticDecoder::restart(int dataLenA
) {
131 } else if (dataLen
== -1) {
135 k
= (-dataLen
- 1) * 8 - ct
;
142 cAdd
+= 0xfe00 - (buf0
<< 9);
145 cAdd
+= 0xff00 - (buf0
<< 8);
148 prevFF
= buf0
== 0xff;
163 void JArithmeticDecoder::cleanup() {
165 while (dataLen
> 0) {
172 int JArithmeticDecoder::decodeBit(Guint context
,
173 JArithmeticDecoderStats
*stats
) {
178 iCX
= stats
->cxTab
[context
] >> 1;
179 mpsCX
= stats
->cxTab
[context
] & 1;
183 if (a
& 0x80000000) {
189 if (switchTab
[iCX
]) {
190 stats
->cxTab
[context
] = (nlpsTab
[iCX
] << 1) | (1 - mpsCX
);
192 stats
->cxTab
[context
] = (nlpsTab
[iCX
] << 1) | mpsCX
;
196 stats
->cxTab
[context
] = (nmpsTab
[iCX
] << 1) | mpsCX
;
206 } while (!(a
& 0x80000000));
213 stats
->cxTab
[context
] = (nmpsTab
[iCX
] << 1) | mpsCX
;
216 if (switchTab
[iCX
]) {
217 stats
->cxTab
[context
] = (nlpsTab
[iCX
] << 1) | (1 - mpsCX
);
219 stats
->cxTab
[context
] = (nlpsTab
[iCX
] << 1) | mpsCX
;
231 } while (!(a
& 0x80000000));
236 int JArithmeticDecoder::decodeByte(Guint context
,
237 JArithmeticDecoderStats
*stats
) {
242 for (i
= 0; i
< 8; ++i
) {
243 byte
= (byte
<< 1) | decodeBit(context
, stats
);
248 GBool
JArithmeticDecoder::decodeInt(int *x
, JArithmeticDecoderStats
*stats
) {
254 s
= decodeIntBit(stats
);
255 if (decodeIntBit(stats
)) {
256 if (decodeIntBit(stats
)) {
257 if (decodeIntBit(stats
)) {
258 if (decodeIntBit(stats
)) {
259 if (decodeIntBit(stats
)) {
261 for (i
= 0; i
< 32; ++i
) {
262 v
= (v
<< 1) | decodeIntBit(stats
);
267 for (i
= 0; i
< 12; ++i
) {
268 v
= (v
<< 1) | decodeIntBit(stats
);
274 for (i
= 0; i
< 8; ++i
) {
275 v
= (v
<< 1) | decodeIntBit(stats
);
281 for (i
= 0; i
< 6; ++i
) {
282 v
= (v
<< 1) | decodeIntBit(stats
);
287 v
= decodeIntBit(stats
);
288 v
= (v
<< 1) | decodeIntBit(stats
);
289 v
= (v
<< 1) | decodeIntBit(stats
);
290 v
= (v
<< 1) | decodeIntBit(stats
);
294 v
= decodeIntBit(stats
);
295 v
= (v
<< 1) | decodeIntBit(stats
);
309 int JArithmeticDecoder::decodeIntBit(JArithmeticDecoderStats
*stats
) {
312 bit
= decodeBit(prev
, stats
);
314 prev
= (prev
<< 1) | bit
;
316 prev
= (((prev
<< 1) | bit
) & 0x1ff) | 0x100;
321 Guint
JArithmeticDecoder::decodeIAID(Guint codeLen
,
322 JArithmeticDecoderStats
*stats
) {
327 for (i
= 0; i
< codeLen
; ++i
) {
328 bit
= decodeBit(prev
, stats
);
329 prev
= (prev
<< 1) | bit
;
331 return prev
- (1 << codeLen
);
334 void JArithmeticDecoder::byteIn() {
340 c
= c
+ 0xff00 - (buf0
<< 8);
346 c
= c
+ 0xfe00 - (buf0
<< 9);
352 c
= c
+ 0xff00 - (buf0
<< 8);