1 //========================================================================
2 //
3 // JBIG2Stream.h
4 //
5 // Copyright 2002-2003 Glyph & Cog, LLC
6 //
7 //========================================================================
8 
9 //========================================================================
10 //
11 // Modified under the Poppler project - http://poppler.freedesktop.org
12 //
13 // All changes made under the Poppler project to this file are licensed
14 // under GPL version 2 or later
15 //
16 // Copyright (C) 2009 David Benjamin <davidben@mit.edu>
17 // Copyright (C) 2013 Adrian Johnson <ajohnson@redneon.com>
18 // Copyright (C) 2015 Suzuki Toshiya <mpsuzuki@hiroshima-u.ac.jp>
19 //
20 // To see a description of the changes please see the Changelog file that
21 // came with your tarball or type make ChangeLog if you are building from git
22 //
23 //========================================================================
24 
25 #ifndef JBIG2STREAM_H
26 #define JBIG2STREAM_H
27 
28 #ifdef USE_GCC_PRAGMAS
29 #pragma interface
30 #endif
31 
32 #include "goo/gtypes.h"
33 #include "Object.h"
34 #include "Stream.h"
35 
36 class GooList;
37 class JBIG2Segment;
38 class JBIG2Bitmap;
39 class JArithmeticDecoder;
40 class JArithmeticDecoderStats;
41 class JBIG2HuffmanDecoder;
42 struct JBIG2HuffmanTable;
43 class JBIG2MMRDecoder;
44 
45 //------------------------------------------------------------------------
46 
47 class JBIG2Stream: public FilterStream {
48 public:
49 
50   JBIG2Stream(Stream *strA, Object *globalsStreamA, Object *globalsStreamRefA);
51   virtual ~JBIG2Stream();
getKind()52   virtual StreamKind getKind() { return strJBIG2; }
53   virtual void reset();
54   virtual void close();
55   virtual Goffset getPos();
56   virtual int getChar();
57   virtual int lookChar();
58   virtual GooString *getPSFilter(int psLevel, const char *indent);
59   virtual GBool isBinary(GBool last = gTrue);
getGlobalsStream()60   virtual Object *getGlobalsStream() { return &globalsStream; }
getGlobalsStreamRef()61   virtual Ref getGlobalsStreamRef() { return globalsStreamRef; }
62 
63 private:
hasGetChars()64   virtual GBool hasGetChars() { return true; }
65   virtual int getChars(int nChars, Guchar *buffer);
66 
67   void readSegments();
68   GBool readSymbolDictSeg(Guint segNum, Guint length,
69 			  Guint *refSegs, Guint nRefSegs);
70   void readTextRegionSeg(Guint segNum, GBool imm,
71 			 GBool lossless, Guint length,
72 			 Guint *refSegs, Guint nRefSegs);
73   JBIG2Bitmap *readTextRegion(GBool huff, GBool refine,
74 			      int w, int h,
75 			      Guint numInstances,
76 			      Guint logStrips,
77 			      int numSyms,
78 			      JBIG2HuffmanTable *symCodeTab,
79 			      Guint symCodeLen,
80 			      JBIG2Bitmap **syms,
81 			      Guint defPixel, Guint combOp,
82 			      Guint transposed, Guint refCorner,
83 			      int sOffset,
84 			      JBIG2HuffmanTable *huffFSTable,
85 			      JBIG2HuffmanTable *huffDSTable,
86 			      JBIG2HuffmanTable *huffDTTable,
87 			      JBIG2HuffmanTable *huffRDWTable,
88 			      JBIG2HuffmanTable *huffRDHTable,
89 			      JBIG2HuffmanTable *huffRDXTable,
90 			      JBIG2HuffmanTable *huffRDYTable,
91 			      JBIG2HuffmanTable *huffRSizeTable,
92 			      Guint templ,
93 			      int *atx, int *aty);
94   void readPatternDictSeg(Guint segNum, Guint length);
95   void readHalftoneRegionSeg(Guint segNum, GBool imm,
96 			     GBool lossless, Guint length,
97 			     Guint *refSegs, Guint nRefSegs);
98   void readGenericRegionSeg(Guint segNum, GBool imm,
99 			    GBool lossless, Guint length);
100   void mmrAddPixels(int a1, int blackPixels,
101 		    int *codingLine, int *a0i, int w);
102   void mmrAddPixelsNeg(int a1, int blackPixels,
103 		       int *codingLine, int *a0i, int w);
104   JBIG2Bitmap *readGenericBitmap(GBool mmr, int w, int h,
105 				 int templ, GBool tpgdOn,
106 				 GBool useSkip, JBIG2Bitmap *skip,
107 				 int *atx, int *aty,
108 				 int mmrDataLength);
109   void readGenericRefinementRegionSeg(Guint segNum, GBool imm,
110 				      GBool lossless, Guint length,
111 				      Guint *refSegs,
112 				      Guint nRefSegs);
113   JBIG2Bitmap *readGenericRefinementRegion(int w, int h,
114 					   int templ, GBool tpgrOn,
115 					   JBIG2Bitmap *refBitmap,
116 					   int refDX, int refDY,
117 					   int *atx, int *aty);
118   void readPageInfoSeg(Guint length);
119   void readEndOfStripeSeg(Guint length);
120   void readProfilesSeg(Guint length);
121   void readCodeTableSeg(Guint segNum, Guint length);
122   void readExtensionSeg(Guint length);
123   JBIG2Segment *findSegment(Guint segNum);
124   void discardSegment(Guint segNum);
125   void resetGenericStats(Guint templ,
126 			 JArithmeticDecoderStats *prevStats);
127   void resetRefinementStats(Guint templ,
128 			    JArithmeticDecoderStats *prevStats);
129   void resetIntStats(int symCodeLen);
130   GBool readUByte(Guint *x);
131   GBool readByte(int *x);
132   GBool readUWord(Guint *x);
133   GBool readULong(Guint *x);
134   GBool readLong(int *x);
135 
136   Object globalsStream;
137   Ref globalsStreamRef;
138   Guint pageW, pageH, curPageH;
139   Guint pageDefPixel;
140   JBIG2Bitmap *pageBitmap;
141   Guint defCombOp;
142   GooList *segments;		// [JBIG2Segment]
143   GooList *globalSegments;	// [JBIG2Segment]
144   Stream *curStr;
145   Guchar *dataPtr;
146   Guchar *dataEnd;
147 
148   JArithmeticDecoder *arithDecoder;
149   JArithmeticDecoderStats *genericRegionStats;
150   JArithmeticDecoderStats *refinementRegionStats;
151   JArithmeticDecoderStats *iadhStats;
152   JArithmeticDecoderStats *iadwStats;
153   JArithmeticDecoderStats *iaexStats;
154   JArithmeticDecoderStats *iaaiStats;
155   JArithmeticDecoderStats *iadtStats;
156   JArithmeticDecoderStats *iaitStats;
157   JArithmeticDecoderStats *iafsStats;
158   JArithmeticDecoderStats *iadsStats;
159   JArithmeticDecoderStats *iardxStats;
160   JArithmeticDecoderStats *iardyStats;
161   JArithmeticDecoderStats *iardwStats;
162   JArithmeticDecoderStats *iardhStats;
163   JArithmeticDecoderStats *iariStats;
164   JArithmeticDecoderStats *iaidStats;
165   JBIG2HuffmanDecoder *huffDecoder;
166   JBIG2MMRDecoder *mmrDecoder;
167 };
168 
169 #endif
170