source: trunk/poppler/mypoppler/poppler/Stream.h @ 257

Last change on this file since 257 was 257, checked in by Eugene Romanenko, 13 years ago

PDF plugin: Poppler library updated to version 0.10.0

File size: 28.2 KB
Line 
1//========================================================================
2//
3// Stream.h
4//
5// Copyright 1996-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) 2005 Jeff Muizelaar <jeff@infidigm.net>
17// Copyright (C) 2008 Julien Rebetez <julien@fhtagn.net>
18// Copyright (C) 2008 Albert Astals Cid <aacid@kde.org>
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 STREAM_H
26#define STREAM_H
27
28#ifdef USE_GCC_PRAGMAS
29#pragma interface
30#endif
31
32#include <stdio.h>
33#include "goo/gtypes.h"
34#include "Object.h"
35
36class BaseStream;
37
38//------------------------------------------------------------------------
39
40enum StreamKind {
41  strFile,
42  strASCIIHex,
43  strASCII85,
44  strLZW,
45  strRunLength,
46  strCCITTFax,
47  strDCT,
48  strFlate,
49  strJBIG2,
50  strJPX,
51  strWeird                      // internal-use stream types
52};
53
54enum StreamColorSpaceMode {
55  streamCSNone,
56  streamCSDeviceGray,
57  streamCSDeviceRGB,
58  streamCSDeviceCMYK
59};
60
61//------------------------------------------------------------------------
62
63// This is in Stream.h instead of Decrypt.h to avoid really annoying
64// include file dependency loops.
65enum CryptAlgorithm {
66  cryptRC4,
67  cryptAES
68};
69
70//------------------------------------------------------------------------
71// Stream (base class)
72//------------------------------------------------------------------------
73
74class Stream {
75public:
76
77  // Constructor.
78  Stream();
79
80  // Destructor.
81  virtual ~Stream();
82
83  // Reference counting.
84  int incRef() { return ++ref; }
85  int decRef() { return --ref; }
86
87  // Get kind of stream.
88  virtual StreamKind getKind() = 0;
89
90  // Reset stream to beginning.
91  virtual void reset() = 0;
92
93  // Close down the stream.
94  virtual void close();
95
96  // Get next char from stream.
97  virtual int getChar() = 0;
98
99  // Peek at next char in stream.
100  virtual int lookChar() = 0;
101
102  // Get next char from stream without using the predictor.
103  // This is only used by StreamPredictor.
104  virtual int getRawChar();
105
106  // Get next char directly from stream source, without filtering it
107  virtual int getUnfilteredChar () = 0;
108
109  // Resets the stream without reading anything (even not the headers)
110  // WARNING: Reading the stream with something else than getUnfilteredChar
111  // may lead to unexcepted behaviour until you call reset ()
112  virtual void unfilteredReset () = 0;
113
114  // Get next line from stream.
115  virtual char *getLine(char *buf, int size);
116
117  // Get current position in file.
118  virtual int getPos() = 0;
119
120  // Go to a position in the stream.  If <dir> is negative, the
121  // position is from the end of the file; otherwise the position is
122  // from the start of the file.
123  virtual void setPos(Guint pos, int dir = 0) = 0;
124
125  // Get PostScript command for the filter(s).
126  virtual GooString *getPSFilter(int psLevel, char *indent);
127
128  // Does this stream type potentially contain non-printable chars?
129  virtual GBool isBinary(GBool last = gTrue) = 0;
130
131  // Get the BaseStream of this stream.
132  virtual BaseStream *getBaseStream() = 0;
133
134  // Get the stream after the last decoder (this may be a BaseStream
135  // or a DecryptStream).
136  virtual Stream *getUndecodedStream() = 0;
137
138  // Get the dictionary associated with this stream.
139  virtual Dict *getDict() = 0;
140
141  // Is this an encoding filter?
142  virtual GBool isEncoder() { return gFalse; }
143
144  // Get image parameters which are defined by the stream contents.
145  virtual void getImageParams(int * /*bitsPerComponent*/,
146                              StreamColorSpaceMode * /*csMode*/) {}
147
148  // Return the next stream in the "stack".
149  virtual Stream *getNextStream() { return NULL; }
150
151  // Add filters to this stream according to the parameters in <dict>.
152  // Returns the new stream.
153  Stream *addFilters(Object *dict);
154
155private:
156
157  Stream *makeFilter(char *name, Stream *str, Object *params);
158
159  int ref;                      // reference count
160};
161
162
163 //------------------------------------------------------------------------
164// OutStream
165//
166// This is the base class for all streams that output to a file
167//------------------------------------------------------------------------
168class OutStream {
169public:
170  // Constructor.
171  OutStream ();
172
173  // Desctructor.
174  virtual ~OutStream ();
175
176  // Reference counting.
177  int incRef() { return ++ref; }
178  int decRef() { return --ref; }
179
180  // Close the stream
181  virtual void close() = 0;
182
183  // Return position in stream
184  virtual int getPos() = 0;
185
186  // Put a char in the stream
187  virtual void put (char c) = 0;
188
189  //FIXME
190  // Printf-like function                         2,3 because the first arg is class instance ?
191  virtual void printf (const char *format, ...) = 0 ; //__attribute__((format(printf, 2,3))) = 0;
192
193private:
194  int ref; // reference count
195   
196};
197
198//------------------------------------------------------------------------
199// FileOutStream
200//------------------------------------------------------------------------
201class FileOutStream : public OutStream {
202public:
203  FileOutStream (FILE* fa, Guint startA);
204
205  virtual ~FileOutStream ();
206
207  virtual void close();
208
209  virtual int getPos();
210
211  virtual void put (char c);
212
213  virtual void printf (const char *format, ...);
214private:
215  FILE *f;
216  Guint start;
217
218};
219
220
221//------------------------------------------------------------------------
222// BaseStream
223//
224// This is the base class for all streams that read directly from a file.
225//------------------------------------------------------------------------
226
227class BaseStream: public Stream {
228public:
229
230  BaseStream(Object *dictA);
231  virtual ~BaseStream();
232  virtual Stream *makeSubStream(Guint start, GBool limited,
233                                Guint length, Object *dict) = 0;
234  virtual void setPos(Guint pos, int dir = 0) = 0;
235  virtual GBool isBinary(GBool last = gTrue) { return last; }
236  virtual BaseStream *getBaseStream() { return this; }
237  virtual Stream *getUndecodedStream() { return this; }
238  virtual Dict *getDict() { return dict.getDict(); }
239  virtual GooString *getFileName() { return NULL; }
240
241  // Get/set position of first byte of stream within the file.
242  virtual Guint getStart() = 0;
243  virtual void moveStart(int delta) = 0;
244
245private:
246
247  Object dict;
248};
249
250//------------------------------------------------------------------------
251// FilterStream
252//
253// This is the base class for all streams that filter another stream.
254//------------------------------------------------------------------------
255
256class FilterStream: public Stream {
257public:
258
259  FilterStream(Stream *strA);
260  virtual ~FilterStream();
261  virtual void close();
262  virtual int getPos() { return str->getPos(); }
263  virtual void setPos(Guint pos, int dir = 0);
264  virtual BaseStream *getBaseStream() { return str->getBaseStream(); }
265  virtual Stream *getUndecodedStream() { return str->getUndecodedStream(); }
266  virtual Dict *getDict() { return str->getDict(); }
267  virtual Stream *getNextStream() { return str; }
268
269  virtual int getUnfilteredChar () { return str->getUnfilteredChar(); }
270  virtual void unfilteredReset () { str->unfilteredReset(); }
271
272protected:
273
274  Stream *str;
275};
276
277//------------------------------------------------------------------------
278// ImageStream
279//------------------------------------------------------------------------
280
281class ImageStream {
282public:
283
284  // Create an image stream object for an image with the specified
285  // parameters.  Note that these are the actual image parameters,
286  // which may be different from the predictor parameters.
287  ImageStream(Stream *strA, int widthA, int nCompsA, int nBitsA);
288
289  ~ImageStream();
290
291  // Reset the stream.
292  void reset();
293
294  // Gets the next pixel from the stream.  <pix> should be able to hold
295  // at least nComps elements.  Returns false at end of file.
296  GBool getPixel(Guchar *pix);
297
298  // Returns a pointer to the next line of pixels.  Returns NULL at
299  // end of file.
300  Guchar *getLine();
301
302  // Skip an entire line from the image.
303  void skipLine();
304
305private:
306
307  Stream *str;                  // base stream
308  int width;                    // pixels per line
309  int nComps;                   // components per pixel
310  int nBits;                    // bits per component
311  int nVals;                    // components per line
312  Guchar *imgLine;              // line buffer
313  int imgIdx;                   // current index in imgLine
314};
315
316//------------------------------------------------------------------------
317// StreamPredictor
318//------------------------------------------------------------------------
319
320class StreamPredictor {
321public:
322
323  // Create a predictor object.  Note that the parameters are for the
324  // predictor, and may not match the actual image parameters.
325  StreamPredictor(Stream *strA, int predictorA,
326                  int widthA, int nCompsA, int nBitsA);
327
328  ~StreamPredictor();
329
330  GBool isOk() { return ok; }
331
332  int lookChar();
333  int getChar();
334
335private:
336
337  GBool getNextLine();
338
339  Stream *str;                  // base stream
340  int predictor;                // predictor
341  int width;                    // pixels per line
342  int nComps;                   // components per pixel
343  int nBits;                    // bits per component
344  int nVals;                    // components per line
345  int pixBytes;                 // bytes per pixel
346  int rowBytes;                 // bytes per line
347  Guchar *predLine;             // line buffer
348  int predIdx;                  // current index in predLine
349  GBool ok;
350};
351
352//------------------------------------------------------------------------
353// FileStream
354//------------------------------------------------------------------------
355
356#define fileStreamBufSize 256
357
358class FileStream: public BaseStream {
359public:
360
361  FileStream(FILE *fA, Guint startA, GBool limitedA,
362             Guint lengthA, Object *dictA);
363  virtual ~FileStream();
364  virtual Stream *makeSubStream(Guint startA, GBool limitedA,
365                                Guint lengthA, Object *dictA);
366  virtual StreamKind getKind() { return strFile; }
367  virtual void reset();
368  virtual void close();
369  virtual int getChar()
370    { return (bufPtr >= bufEnd && !fillBuf()) ? EOF : (*bufPtr++ & 0xff); }
371  virtual int lookChar()
372    { return (bufPtr >= bufEnd && !fillBuf()) ? EOF : (*bufPtr & 0xff); }
373  virtual int getPos() { return bufPos + (bufPtr - buf); }
374  virtual void setPos(Guint pos, int dir = 0);
375  virtual Guint getStart() { return start; }
376  virtual void moveStart(int delta);
377
378  virtual int getUnfilteredChar () { return getChar(); }
379  virtual void unfilteredReset () { reset(); }
380
381private:
382
383  GBool fillBuf();
384
385  FILE *f;
386  Guint start;
387  GBool limited;
388  Guint length;
389  char buf[fileStreamBufSize];
390  char *bufPtr;
391  char *bufEnd;
392  Guint bufPos;
393  int savePos;
394  GBool saved;
395};
396
397//------------------------------------------------------------------------
398// MemStream
399//------------------------------------------------------------------------
400
401class MemStream: public BaseStream {
402public:
403
404  MemStream(char *bufA, Guint startA, Guint lengthA, Object *dictA);
405  virtual ~MemStream();
406  virtual Stream *makeSubStream(Guint start, GBool limited,
407                                Guint lengthA, Object *dictA);
408  virtual StreamKind getKind() { return strWeird; }
409  virtual void reset();
410  virtual void close();
411  virtual int getChar()
412    { return (bufPtr < bufEnd) ? (*bufPtr++ & 0xff) : EOF; }
413  virtual int lookChar()
414    { return (bufPtr < bufEnd) ? (*bufPtr & 0xff) : EOF; }
415  virtual int getPos() { return (int)(bufPtr - buf); }
416  virtual void setPos(Guint pos, int dir = 0);
417  virtual Guint getStart() { return start; }
418  virtual void moveStart(int delta);
419
420  //if needFree = true, the stream will delete buf when it is destroyed
421  //otherwise it will not touch it. Default value is false
422  virtual void setNeedFree (GBool val) { needFree = val; }
423
424  virtual int getUnfilteredChar () { return getChar(); }
425  virtual void unfilteredReset () { reset (); } 
426
427private:
428
429  char *buf;
430  Guint start;
431  Guint length;
432  char *bufEnd;
433  char *bufPtr;
434  GBool needFree;
435};
436
437//------------------------------------------------------------------------
438// EmbedStream
439//
440// This is a special stream type used for embedded streams (inline
441// images).  It reads directly from the base stream -- after the
442// EmbedStream is deleted, reads from the base stream will proceed where
443// the BaseStream left off.  Note that this is very different behavior
444// that creating a new FileStream (using makeSubStream).
445//------------------------------------------------------------------------
446
447class EmbedStream: public BaseStream {
448public:
449
450  EmbedStream(Stream *strA, Object *dictA, GBool limitedA, Guint lengthA);
451  virtual ~EmbedStream();
452  virtual Stream *makeSubStream(Guint start, GBool limitedA,
453                                Guint lengthA, Object *dictA);
454  virtual StreamKind getKind() { return str->getKind(); }
455  virtual void reset() {}
456  virtual int getChar();
457  virtual int lookChar();
458  virtual int getPos() { return str->getPos(); }
459  virtual void setPos(Guint pos, int dir = 0);
460  virtual Guint getStart();
461  virtual void moveStart(int delta);
462
463  virtual int getUnfilteredChar () { return str->getUnfilteredChar(); }
464  virtual void unfilteredReset () { str->unfilteredReset(); }
465
466
467private:
468
469  Stream *str;
470  GBool limited;
471  Guint length;
472};
473
474//------------------------------------------------------------------------
475// ASCIIHexStream
476//------------------------------------------------------------------------
477
478class ASCIIHexStream: public FilterStream {
479public:
480
481  ASCIIHexStream(Stream *strA);
482  virtual ~ASCIIHexStream();
483  virtual StreamKind getKind() { return strASCIIHex; }
484  virtual void reset();
485  virtual int getChar()
486    { int c = lookChar(); buf = EOF; return c; }
487  virtual int lookChar();
488  virtual GooString *getPSFilter(int psLevel, char *indent);
489  virtual GBool isBinary(GBool last = gTrue);
490
491private:
492
493  int buf;
494  GBool eof;
495};
496
497//------------------------------------------------------------------------
498// ASCII85Stream
499//------------------------------------------------------------------------
500
501class ASCII85Stream: public FilterStream {
502public:
503
504  ASCII85Stream(Stream *strA);
505  virtual ~ASCII85Stream();
506  virtual StreamKind getKind() { return strASCII85; }
507  virtual void reset();
508  virtual int getChar()
509    { int ch = lookChar(); ++index; return ch; }
510  virtual int lookChar();
511  virtual GooString *getPSFilter(int psLevel, char *indent);
512  virtual GBool isBinary(GBool last = gTrue);
513
514private:
515
516  int c[5];
517  int b[4];
518  int index, n;
519  GBool eof;
520};
521
522//------------------------------------------------------------------------
523// LZWStream
524//------------------------------------------------------------------------
525
526class LZWStream: public FilterStream {
527public:
528
529  LZWStream(Stream *strA, int predictor, int columns, int colors,
530            int bits, int earlyA);
531  virtual ~LZWStream();
532  virtual StreamKind getKind() { return strLZW; }
533  virtual void reset();
534  virtual int getChar();
535  virtual int lookChar();
536  virtual int getRawChar();
537  virtual GooString *getPSFilter(int psLevel, char *indent);
538  virtual GBool isBinary(GBool last = gTrue);
539
540private:
541
542  StreamPredictor *pred;        // predictor
543  int early;                    // early parameter
544  GBool eof;                    // true if at eof
545  int inputBuf;                 // input buffer
546  int inputBits;                // number of bits in input buffer
547  struct {                      // decoding table
548    int length;
549    int head;
550    Guchar tail;
551  } table[4097];
552  int nextCode;                 // next code to be used
553  int nextBits;                 // number of bits in next code word
554  int prevCode;                 // previous code used in stream
555  int newChar;                  // next char to be added to table
556  Guchar seqBuf[4097];          // buffer for current sequence
557  int seqLength;                // length of current sequence
558  int seqIndex;                 // index into current sequence
559  GBool first;                  // first code after a table clear
560
561  GBool processNextCode();
562  void clearTable();
563  int getCode();
564};
565
566//------------------------------------------------------------------------
567// RunLengthStream
568//------------------------------------------------------------------------
569
570class RunLengthStream: public FilterStream {
571public:
572
573  RunLengthStream(Stream *strA);
574  virtual ~RunLengthStream();
575  virtual StreamKind getKind() { return strRunLength; }
576  virtual void reset();
577  virtual int getChar()
578    { return (bufPtr >= bufEnd && !fillBuf()) ? EOF : (*bufPtr++ & 0xff); }
579  virtual int lookChar()
580    { return (bufPtr >= bufEnd && !fillBuf()) ? EOF : (*bufPtr & 0xff); }
581  virtual GooString *getPSFilter(int psLevel, char *indent);
582  virtual GBool isBinary(GBool last = gTrue);
583
584private:
585
586  char buf[128];                // buffer
587  char *bufPtr;                 // next char to read
588  char *bufEnd;                 // end of buffer
589  GBool eof;
590
591  GBool fillBuf();
592};
593
594//------------------------------------------------------------------------
595// CCITTFaxStream
596//------------------------------------------------------------------------
597
598struct CCITTCodeTable;
599
600class CCITTFaxStream: public FilterStream {
601public:
602
603  CCITTFaxStream(Stream *strA, int encodingA, GBool endOfLineA,
604                 GBool byteAlignA, int columnsA, int rowsA,
605                 GBool endOfBlockA, GBool blackA);
606  virtual ~CCITTFaxStream();
607  virtual StreamKind getKind() { return strCCITTFax; }
608  virtual void reset();
609  virtual int getChar()
610    { int c = lookChar(); buf = EOF; return c; }
611  virtual int lookChar();
612  virtual GooString *getPSFilter(int psLevel, char *indent);
613  virtual GBool isBinary(GBool last = gTrue);
614
615  virtual void unfilteredReset ();
616
617private:
618
619  int encoding;                 // 'K' parameter
620  GBool endOfLine;              // 'EndOfLine' parameter
621  GBool byteAlign;              // 'EncodedByteAlign' parameter
622  int columns;                  // 'Columns' parameter
623  int rows;                     // 'Rows' parameter
624  GBool endOfBlock;             // 'EndOfBlock' parameter
625  GBool black;                  // 'BlackIs1' parameter
626  GBool eof;                    // true if at eof
627  GBool nextLine2D;             // true if next line uses 2D encoding
628  int row;                      // current row
629  int inputBuf;                 // input buffer
630  int inputBits;                // number of bits in input buffer
631  int *codingLine;              // coding line changing elements
632  int *refLine;                 // reference line changing elements
633  int a0i;                      // index into codingLine
634  GBool err;                    // error on current line
635  int outputBits;               // remaining ouput bits
636  int buf;                      // character buffer
637
638  void addPixels(int a1, int black);
639  void addPixelsNeg(int a1, int black);
640  short getTwoDimCode();
641  short getWhiteCode();
642  short getBlackCode();
643  short lookBits(int n);
644  void eatBits(int n) { if ((inputBits -= n) < 0) inputBits = 0; }
645};
646
647#ifndef ENABLE_LIBJPEG
648//------------------------------------------------------------------------
649// DCTStream
650//------------------------------------------------------------------------
651
652// DCT component info
653struct DCTCompInfo {
654  int id;                       // component ID
655  int hSample, vSample;         // horiz/vert sampling resolutions
656  int quantTable;               // quantization table number
657  int prevDC;                   // DC coefficient accumulator
658};
659
660struct DCTScanInfo {
661  GBool comp[4];                // comp[i] is set if component i is
662                                //   included in this scan
663  int numComps;                 // number of components in the scan
664  int dcHuffTable[4];           // DC Huffman table numbers
665  int acHuffTable[4];           // AC Huffman table numbers
666  int firstCoeff, lastCoeff;    // first and last DCT coefficient
667  int ah, al;                   // successive approximation parameters
668};
669
670// DCT Huffman decoding table
671struct DCTHuffTable {
672  Guchar firstSym[17];          // first symbol for this bit length
673  Gushort firstCode[17];        // first code for this bit length
674  Gushort numCodes[17];         // number of codes of this bit length
675  Guchar sym[256];              // symbols
676};
677
678class DCTStream: public FilterStream {
679public:
680
681  DCTStream(Stream *strA, int colorXformA);
682  virtual ~DCTStream();
683  virtual StreamKind getKind() { return strDCT; }
684  virtual void reset();
685  virtual void close();
686  virtual int getChar();
687  virtual int lookChar();
688  virtual GooString *getPSFilter(int psLevel, char *indent);
689  virtual GBool isBinary(GBool last = gTrue);
690  Stream *getRawStream() { return str; }
691
692  virtual void unfilteredReset();
693
694private:
695
696  GBool progressive;            // set if in progressive mode
697  GBool interleaved;            // set if in interleaved mode
698  int width, height;            // image size
699  int mcuWidth, mcuHeight;      // size of min coding unit, in data units
700  int bufWidth, bufHeight;      // frameBuf size
701  DCTCompInfo compInfo[4];      // info for each component
702  DCTScanInfo scanInfo;         // info for the current scan
703  int numComps;                 // number of components in image
704  int colorXform;               // color transform: -1 = unspecified
705                                //                   0 = none
706                                //                   1 = YUV/YUVK -> RGB/CMYK
707  GBool gotJFIFMarker;          // set if APP0 JFIF marker was present
708  GBool gotAdobeMarker;         // set if APP14 Adobe marker was present
709  int restartInterval;          // restart interval, in MCUs
710  Gushort quantTables[4][64];   // quantization tables
711  int numQuantTables;           // number of quantization tables
712  DCTHuffTable dcHuffTables[4]; // DC Huffman tables
713  DCTHuffTable acHuffTables[4]; // AC Huffman tables
714  int numDCHuffTables;          // number of DC Huffman tables
715  int numACHuffTables;          // number of AC Huffman tables
716  Guchar *rowBuf[4][32];        // buffer for one MCU (non-progressive mode)
717  int *frameBuf[4];             // buffer for frame (progressive mode)
718  int comp, x, y, dy;           // current position within image/MCU
719  int restartCtr;               // MCUs left until restart
720  int restartMarker;            // next restart marker
721  int eobRun;                   // number of EOBs left in the current run
722  int inputBuf;                 // input buffer for variable length codes
723  int inputBits;                // number of valid bits in input buffer
724
725  void restart();
726  GBool readMCURow();
727  void readScan();
728  GBool readDataUnit(DCTHuffTable *dcHuffTable,
729                     DCTHuffTable *acHuffTable,
730                     int *prevDC, int data[64]);
731  GBool readProgressiveDataUnit(DCTHuffTable *dcHuffTable,
732                                DCTHuffTable *acHuffTable,
733                                int *prevDC, int data[64]);
734  void decodeImage();
735  void transformDataUnit(Gushort *quantTable,
736                         int dataIn[64], Guchar dataOut[64]);
737  int readHuffSym(DCTHuffTable *table);
738  int readAmp(int size);
739  int readBit();
740  GBool readHeader();
741  GBool readBaselineSOF();
742  GBool readProgressiveSOF();
743  GBool readScanInfo();
744  GBool readQuantTables();
745  GBool readHuffmanTables();
746  GBool readRestartInterval();
747  GBool readJFIFMarker();
748  GBool readAdobeMarker();
749  GBool readTrailer();
750  int readMarker();
751  int read16();
752};
753
754#endif
755
756#ifndef ENABLE_ZLIB
757//------------------------------------------------------------------------
758// FlateStream
759//------------------------------------------------------------------------
760
761#define flateWindow          32768    // buffer size
762#define flateMask            (flateWindow-1)
763#define flateMaxHuffman         15    // max Huffman code length
764#define flateMaxCodeLenCodes    19    // max # code length codes
765#define flateMaxLitCodes       288    // max # literal codes
766#define flateMaxDistCodes       30    // max # distance codes
767
768// Huffman code table entry
769struct FlateCode {
770  Gushort len;                  // code length, in bits
771  Gushort val;                  // value represented by this code
772};
773
774struct FlateHuffmanTab {
775  FlateCode *codes;
776  int maxLen;
777};
778
779// Decoding info for length and distance code words
780struct FlateDecode {
781  int bits;                     // # extra bits
782  int first;                    // first length/distance
783};
784
785class FlateStream: public FilterStream {
786public:
787
788  FlateStream(Stream *strA, int predictor, int columns,
789              int colors, int bits);
790  virtual ~FlateStream();
791  virtual StreamKind getKind() { return strFlate; }
792  virtual void reset();
793  virtual int getChar();
794  virtual int lookChar();
795  virtual int getRawChar();
796  virtual GooString *getPSFilter(int psLevel, char *indent);
797  virtual GBool isBinary(GBool last = gTrue);
798  virtual void unfilteredReset ();
799
800private:
801
802  StreamPredictor *pred;        // predictor
803  Guchar buf[flateWindow];      // output data buffer
804  int index;                    // current index into output buffer
805  int remain;                   // number valid bytes in output buffer
806  int codeBuf;                  // input buffer
807  int codeSize;                 // number of bits in input buffer
808  int                           // literal and distance code lengths
809    codeLengths[flateMaxLitCodes + flateMaxDistCodes];
810  FlateHuffmanTab litCodeTab;   // literal code table
811  FlateHuffmanTab distCodeTab;  // distance code table
812  GBool compressedBlock;        // set if reading a compressed block
813  int blockLen;                 // remaining length of uncompressed block
814  GBool endOfBlock;             // set when end of block is reached
815  GBool eof;                    // set when end of stream is reached
816
817  static int                    // code length code reordering
818    codeLenCodeMap[flateMaxCodeLenCodes];
819  static FlateDecode            // length decoding info
820    lengthDecode[flateMaxLitCodes-257];
821  static FlateDecode            // distance decoding info
822    distDecode[flateMaxDistCodes];
823  static FlateHuffmanTab        // fixed literal code table
824    fixedLitCodeTab;
825  static FlateHuffmanTab        // fixed distance code table
826    fixedDistCodeTab;
827
828  void readSome();
829  GBool startBlock();
830  void loadFixedCodes();
831  GBool readDynamicCodes();
832  void compHuffmanCodes(int *lengths, int n, FlateHuffmanTab *tab);
833  int getHuffmanCodeWord(FlateHuffmanTab *tab);
834  int getCodeWord(int bits);
835};
836#endif
837
838//------------------------------------------------------------------------
839// EOFStream
840//------------------------------------------------------------------------
841
842class EOFStream: public FilterStream {
843public:
844
845  EOFStream(Stream *strA);
846  virtual ~EOFStream();
847  virtual StreamKind getKind() { return strWeird; }
848  virtual void reset() {}
849  virtual int getChar() { return EOF; }
850  virtual int lookChar() { return EOF; }
851  virtual GooString *getPSFilter(int /*psLevel*/, char * /*indent*/)  { return NULL; }
852  virtual GBool isBinary(GBool /*last = gTrue*/) { return gFalse; }
853};
854
855//------------------------------------------------------------------------
856// FixedLengthEncoder
857//------------------------------------------------------------------------
858
859class FixedLengthEncoder: public FilterStream {
860public:
861
862  FixedLengthEncoder(Stream *strA, int lengthA);
863  ~FixedLengthEncoder();
864  virtual StreamKind getKind() { return strWeird; }
865  virtual void reset();
866  virtual int getChar();
867  virtual int lookChar();
868  virtual GooString *getPSFilter(int /*psLevel*/, char * /*indent*/) { return NULL; }
869  virtual GBool isBinary(GBool /*last = gTrue*/);
870  virtual GBool isEncoder() { return gTrue; }
871
872private:
873
874  int length;
875  int count;
876};
877
878//------------------------------------------------------------------------
879// ASCIIHexEncoder
880//------------------------------------------------------------------------
881
882class ASCIIHexEncoder: public FilterStream {
883public:
884
885  ASCIIHexEncoder(Stream *strA);
886  virtual ~ASCIIHexEncoder();
887  virtual StreamKind getKind() { return strWeird; }
888  virtual void reset();
889  virtual int getChar()
890    { return (bufPtr >= bufEnd && !fillBuf()) ? EOF : (*bufPtr++ & 0xff); }
891  virtual int lookChar()
892    { return (bufPtr >= bufEnd && !fillBuf()) ? EOF : (*bufPtr & 0xff); }
893  virtual GooString *getPSFilter(int /*psLevel*/, char * /*indent*/) { return NULL; }
894  virtual GBool isBinary(GBool /*last = gTrue*/) { return gFalse; }
895  virtual GBool isEncoder() { return gTrue; }
896
897private:
898
899  char buf[4];
900  char *bufPtr;
901  char *bufEnd;
902  int lineLen;
903  GBool eof;
904
905  GBool fillBuf();
906};
907
908//------------------------------------------------------------------------
909// ASCII85Encoder
910//------------------------------------------------------------------------
911
912class ASCII85Encoder: public FilterStream {
913public:
914
915  ASCII85Encoder(Stream *strA);
916  virtual ~ASCII85Encoder();
917  virtual StreamKind getKind() { return strWeird; }
918  virtual void reset();
919  virtual int getChar()
920    { return (bufPtr >= bufEnd && !fillBuf()) ? EOF : (*bufPtr++ & 0xff); }
921  virtual int lookChar()
922    { return (bufPtr >= bufEnd && !fillBuf()) ? EOF : (*bufPtr & 0xff); }
923  virtual GooString *getPSFilter(int /*psLevel*/, char * /*indent*/) { return NULL; }
924  virtual GBool isBinary(GBool /*last = gTrue*/) { return gFalse; }
925  virtual GBool isEncoder() { return gTrue; }
926
927private:
928
929  char buf[8];
930  char *bufPtr;
931  char *bufEnd;
932  int lineLen;
933  GBool eof;
934
935  GBool fillBuf();
936};
937
938//------------------------------------------------------------------------
939// RunLengthEncoder
940//------------------------------------------------------------------------
941
942class RunLengthEncoder: public FilterStream {
943public:
944
945  RunLengthEncoder(Stream *strA);
946  virtual ~RunLengthEncoder();
947  virtual StreamKind getKind() { return strWeird; }
948  virtual void reset();
949  virtual int getChar()
950    { return (bufPtr >= bufEnd && !fillBuf()) ? EOF : (*bufPtr++ & 0xff); }
951  virtual int lookChar()
952    { return (bufPtr >= bufEnd && !fillBuf()) ? EOF : (*bufPtr & 0xff); }
953  virtual GooString *getPSFilter(int /*psLevel*/, char * /*indent*/) { return NULL; }
954  virtual GBool isBinary(GBool /*last = gTrue*/) { return gTrue; }
955  virtual GBool isEncoder() { return gTrue; }
956
957private:
958
959  char buf[131];
960  char *bufPtr;
961  char *bufEnd;
962  char *nextEnd;
963  GBool eof;
964
965  GBool fillBuf();
966};
967
968#endif
Note: See TracBrowser for help on using the repository browser.