1 //========================================================================
3 // JArithmeticDecoder.h
5 // Arithmetic decoder used by the JBIG2 and JPEG2000 decoders.
7 // Copyright 2002-2004 Glyph & Cog, LLC
9 //========================================================================
11 #ifndef JARITHMETICDECODER_H
12 #define JARITHMETICDECODER_H
16 #ifdef USE_GCC_PRAGMAS
24 //------------------------------------------------------------------------
25 // JArithmeticDecoderStats
26 //------------------------------------------------------------------------
28 class JArithmeticDecoderStats {
31 JArithmeticDecoderStats(int contextSizeA);
32 ~JArithmeticDecoderStats();
33 JArithmeticDecoderStats *copy();
35 int getContextSize() { return contextSize; }
36 void copyFrom(JArithmeticDecoderStats *stats);
37 void setEntry(Guint cx, int i, int mps);
41 Guchar *cxTab; // cxTab[cx] = (i[cx] << 1) + mps[cx]
44 friend class JArithmeticDecoder;
47 //------------------------------------------------------------------------
49 //------------------------------------------------------------------------
51 class JArithmeticDecoder {
55 ~JArithmeticDecoder();
57 void setStream(Stream *strA)
58 { str = strA; dataLen = 0; limitStream = gFalse; }
59 void setStream(Stream *strA, int dataLenA)
60 { str = strA; dataLen = dataLenA; limitStream = gTrue; }
62 // Start decoding on a new stream. This fills the byte buffers and
66 // Restart decoding on an interrupted stream. This refills the
67 // buffers if needed, but does not run INITDEC. (This is used in
68 // JPEG 2000 streams when codeblock data is split across multiple
70 void restart(int dataLenA);
72 // Read any leftover data in the stream.
76 int decodeBit(Guint context, JArithmeticDecoderStats *stats);
79 int decodeByte(Guint context, JArithmeticDecoderStats *stats);
81 // Returns false for OOB, otherwise sets *<x> and returns true.
82 GBool decodeInt(int *x, JArithmeticDecoderStats *stats);
84 Guint decodeIAID(Guint codeLen,
85 JArithmeticDecoderStats *stats);
90 int decodeIntBit(JArithmeticDecoderStats *stats);
93 static Guint qeTab[47];
94 static int nmpsTab[47];
95 static int nlpsTab[47];
96 static int switchTab[47];
102 Guint prev; // for the integer decoder