a348017d02c4ad8e187a98e9d0a2aee675cb5b72
[swftools.git] / pdf2swf / xpdf / JArithmeticDecoder.h
1 //========================================================================
2 //
3 // JArithmeticDecoder.h
4 //
5 // Arithmetic decoder used by the JBIG2 and JPEG2000 decoders.
6 //
7 // Copyright 2002-2004 Glyph & Cog, LLC
8 //
9 //========================================================================
10
11 #ifndef JARITHMETICDECODER_H
12 #define JARITHMETICDECODER_H
13
14 #include <aconf.h>
15
16 #ifdef USE_GCC_PRAGMAS
17 #pragma interface
18 #endif
19
20 #include "gtypes.h"
21
22 class Stream;
23
24 //------------------------------------------------------------------------
25 // JArithmeticDecoderStats
26 //------------------------------------------------------------------------
27
28 class JArithmeticDecoderStats {
29 public:
30
31   JArithmeticDecoderStats(int contextSizeA);
32   ~JArithmeticDecoderStats();
33   JArithmeticDecoderStats *copy();
34   void reset();
35   int getContextSize() { return contextSize; }
36   void copyFrom(JArithmeticDecoderStats *stats);
37   void setEntry(Guint cx, int i, int mps);
38
39 private:
40
41   Guchar *cxTab;                // cxTab[cx] = (i[cx] << 1) + mps[cx]
42   int contextSize;
43
44   friend class JArithmeticDecoder;
45 };
46
47 //------------------------------------------------------------------------
48 // JArithmeticDecoder
49 //------------------------------------------------------------------------
50
51 class JArithmeticDecoder {
52 public:
53
54   JArithmeticDecoder();
55   ~JArithmeticDecoder();
56   void setStream(Stream *strA)
57     { str = strA; dataLen = -1; }
58   void setStream(Stream *strA, int dataLenA)
59     { str = strA; dataLen = dataLenA; }
60   void start();
61   int decodeBit(Guint context, JArithmeticDecoderStats *stats);
62   int decodeByte(Guint context, JArithmeticDecoderStats *stats);
63
64   // Returns false for OOB, otherwise sets *<x> and returns true.
65   GBool decodeInt(int *x, JArithmeticDecoderStats *stats);
66
67   Guint decodeIAID(Guint codeLen,
68                    JArithmeticDecoderStats *stats);
69
70 private:
71
72   Guint readByte();
73   int decodeIntBit(JArithmeticDecoderStats *stats);
74   void byteIn();
75
76   static Guint qeTab[47];
77   static int nmpsTab[47];
78   static int nlpsTab[47];
79   static int switchTab[47];
80
81   Guint buf0, buf1;
82   Guint c, a;
83   int ct;
84
85   Guint prev;                   // for the integer decoder
86
87   Stream *str;
88   int dataLen;
89 };
90
91 #endif