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();
getContextSize()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 
setStream(Stream * strA)57   void setStream(Stream *strA)
58     { str = strA; dataLen = 0; limitStream = gFalse; }
setStream(Stream * strA,int dataLenA)59   void setStream(Stream *strA, int dataLenA)
60     { str = strA; dataLen = dataLenA; limitStream = gTrue; }
61 
62   // Start decoding on a new stream.  This fills the byte buffers and
63   // runs INITDEC.
64   void start();
65 
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
69   // packets/layers.)
70   void restart(int dataLenA);
71 
72   // Read any leftover data in the stream.
73   void cleanup();
74 
75   // Decode one bit.
76   int decodeBit(Guint context, JArithmeticDecoderStats *stats);
77 
78   // Decode eight bits.
79   int decodeByte(Guint context, JArithmeticDecoderStats *stats);
80 
81   // Returns false for OOB, otherwise sets *<x> and returns true.
82   GBool decodeInt(int *x, JArithmeticDecoderStats *stats);
83 
84   Guint decodeIAID(Guint codeLen,
85 		   JArithmeticDecoderStats *stats);
86 
87 private:
88 
89   Guint readByte();
90   int decodeIntBit(JArithmeticDecoderStats *stats);
91   void byteIn();
92 
93   static Guint qeTab[47];
94   static int nmpsTab[47];
95   static int nlpsTab[47];
96   static int switchTab[47];
97 
98   Guint buf0, buf1;
99   Guint c, a;
100   int ct;
101 
102   Guint prev;			// for the integer decoder
103 
104   Stream *str;
105   int dataLen;
106   GBool limitStream;
107 };
108 
109 #endif
110