filters

Stream.h

00001 //========================================================================
00002 //
00003 // Stream.h
00004 //
00005 // Copyright 1996-2002 Glyph & Cog, LLC
00006 //
00007 //========================================================================
00008 
00009 #ifndef STREAM_H
00010 #define STREAM_H
00011 
00012 #include <aconf.h>
00013 
00014 #ifdef USE_GCC_PRAGMAS
00015 #pragma interface
00016 #endif
00017 
00018 #include <stdio.h>
00019 #include "gtypes.h"
00020 #include "Object.h"
00021 
00022 #ifndef NO_DECRYPTION
00023 class Decrypt;
00024 #endif
00025 class BaseStream;
00026 
00027 //------------------------------------------------------------------------
00028 
00029 enum StreamKind {
00030   strFile,
00031   strASCIIHex,
00032   strASCII85,
00033   strLZW,
00034   strRunLength,
00035   strCCITTFax,
00036   strDCT,
00037   strFlate,
00038   strJBIG2,
00039   strWeird          // internal-use stream types
00040 };
00041 
00042 //------------------------------------------------------------------------
00043 // Stream (base class)
00044 //------------------------------------------------------------------------
00045 
00046 class Stream {
00047 public:
00048 
00049   // Constructor.
00050   Stream();
00051 
00052   // Destructor.
00053   virtual ~Stream();
00054 
00055   // Reference counting.
00056   int incRef() { return ++ref; }
00057   int decRef() { return --ref; }
00058 
00059   // Get kind of stream.
00060   virtual StreamKind getKind() = 0;
00061 
00062   // Reset stream to beginning.
00063   virtual void reset() = 0;
00064 
00065   // Close down the stream.
00066   virtual void close();
00067 
00068   // Get next char from stream.
00069   virtual int getChar() = 0;
00070 
00071   // Peek at next char in stream.
00072   virtual int lookChar() = 0;
00073 
00074   // Get next char from stream without using the predictor.
00075   // This is only used by StreamPredictor.
00076   virtual int getRawChar();
00077 
00078   // Get next line from stream.
00079   virtual char *getLine(char *buf, int size);
00080 
00081   // Get current position in file.
00082   virtual int getPos() = 0;
00083 
00084   // Go to a position in the stream.  If <dir> is negative, the
00085   // position is from the end of the file; otherwise the position is
00086   // from the start of the file.
00087   virtual void setPos(Guint pos, int dir = 0) = 0;
00088 
00089   // Get PostScript command for the filter(s).
00090   virtual GString *getPSFilter(const char *indent);
00091 
00092   // Does this stream type potentially contain non-printable chars?
00093   virtual GBool isBinary(GBool last = gTrue) = 0;
00094 
00095   // Get the BaseStream or EmbedStream of this stream.
00096   virtual BaseStream *getBaseStream() = 0;
00097 
00098   // Get the dictionary associated with this stream.
00099   virtual Dict *getDict() = 0;
00100 
00101   // Is this an encoding filter?
00102   virtual GBool isEncoder() { return gFalse; }
00103 
00104   // Add filters to this stream according to the parameters in <dict>.
00105   // Returns the new stream.
00106   Stream *addFilters(Object *dict);
00107 
00108 private:
00109 
00110   Stream *makeFilter(const char *name, Stream *str, Object *params);
00111 
00112   int ref;          // reference count
00113 };
00114 
00115 //------------------------------------------------------------------------
00116 // BaseStream
00117 //
00118 // This is the base class for all streams that read directly from a file.
00119 //------------------------------------------------------------------------
00120 
00121 class BaseStream: public Stream {
00122 public:
00123 
00124   BaseStream(Object *dictA);
00125   virtual ~BaseStream();
00126   virtual Stream *makeSubStream(Guint start, GBool limited,
00127                 Guint length, Object *dict) = 0;
00128   virtual void setPos(Guint pos, int dir = 0) = 0;
00129   virtual BaseStream *getBaseStream() { return this; }
00130   virtual Dict *getDict() { return dict.getDict(); }
00131 
00132   // Get/set position of first byte of stream within the file.
00133   virtual Guint getStart() = 0;
00134   virtual void moveStart(int delta) = 0;
00135 
00136 #ifndef NO_DECRYPTION
00137   // Set decryption for this stream.
00138   virtual void doDecryption(Guchar *fileKey, int keyLength,
00139                 int objNum, int objGen);
00140 #endif
00141 
00142 #ifndef NO_DECRYPTION
00143 protected:
00144 
00145   Decrypt *decrypt;
00146 #endif
00147 
00148 private:
00149 
00150   Object dict;
00151 };
00152 
00153 //------------------------------------------------------------------------
00154 // FilterStream
00155 //
00156 // This is the base class for all streams that filter another stream.
00157 //------------------------------------------------------------------------
00158 
00159 class FilterStream: public Stream {
00160 public:
00161 
00162   FilterStream(Stream *strA);
00163   virtual ~FilterStream();
00164   virtual void close();
00165   virtual int getPos() { return str->getPos(); }
00166   virtual void setPos(Guint pos, int dir = 0);
00167   virtual BaseStream *getBaseStream() { return str->getBaseStream(); }
00168   virtual Dict *getDict() { return str->getDict(); }
00169 
00170 protected:
00171 
00172   Stream *str;
00173 };
00174 
00175 //------------------------------------------------------------------------
00176 // ImageStream
00177 //------------------------------------------------------------------------
00178 
00179 class ImageStream {
00180 public:
00181 
00182   // Create an image stream object for an image with the specified
00183   // parameters.  Note that these are the actual image parameters,
00184   // which may be different from the predictor parameters.
00185   ImageStream(Stream *strA, int widthA, int nCompsA, int nBitsA);
00186 
00187   ~ImageStream();
00188 
00189   // Reset the stream.
00190   void reset();
00191 
00192   // Gets the next pixel from the stream.  <pix> should be able to hold
00193   // at least nComps elements.  Returns false at end of file.
00194   GBool getPixel(Guchar *pix);
00195 
00196   // Returns a pointer to the next line of pixels.  Returns NULL at
00197   // end of file.
00198   Guchar *getLine();
00199 
00200   // Skip an entire line from the image.
00201   void skipLine();
00202 
00203 private:
00204 
00205   Stream *str;          // base stream
00206   int width;            // pixels per line
00207   int nComps;           // components per pixel
00208   int nBits;            // bits per component
00209   int nVals;            // components per line
00210   Guchar *imgLine;      // line buffer
00211   int imgIdx;           // current index in imgLine
00212 };
00213 
00214 //------------------------------------------------------------------------
00215 // StreamPredictor
00216 //------------------------------------------------------------------------
00217 
00218 class StreamPredictor {
00219 public:
00220 
00221   // Create a predictor object.  Note that the parameters are for the
00222   // predictor, and may not match the actual image parameters.
00223   StreamPredictor(Stream *strA, int predictorA,
00224           int widthA, int nCompsA, int nBitsA);
00225 
00226   ~StreamPredictor();
00227 
00228   int lookChar();
00229   int getChar();
00230   GBool isOk() { return ok; }
00231 
00232 private:
00233 
00234   GBool getNextLine();
00235 
00236   Stream *str;          // base stream
00237   int predictor;        // predictor
00238   int width;            // pixels per line
00239   int nComps;           // components per pixel
00240   int nBits;            // bits per component
00241   int nVals;            // components per line
00242   int pixBytes;         // bytes per pixel
00243   int rowBytes;         // bytes per line
00244   Guchar *predLine;     // line buffer
00245   int predIdx;          // current index in predLine
00246   GBool ok;
00247 };
00248 
00249 //------------------------------------------------------------------------
00250 // FileStream
00251 //------------------------------------------------------------------------
00252 
00253 #define fileStreamBufSize 256
00254 
00255 class FileStream: public BaseStream {
00256 public:
00257 
00258   FileStream(FILE *fA, Guint startA, GBool limitedA,
00259          Guint lengthA, Object *dictA);
00260   virtual ~FileStream();
00261   virtual Stream *makeSubStream(Guint startA, GBool limitedA,
00262                 Guint lengthA, Object *dictA);
00263   virtual StreamKind getKind() { return strFile; }
00264   virtual void reset();
00265   virtual void close();
00266   virtual int getChar()
00267     { return (bufPtr >= bufEnd && !fillBuf()) ? EOF : (*bufPtr++ & 0xff); }
00268   virtual int lookChar()
00269     { return (bufPtr >= bufEnd && !fillBuf()) ? EOF : (*bufPtr & 0xff); }
00270   virtual int getPos() { return bufPos + (bufPtr - buf); }
00271   virtual void setPos(Guint pos, int dir = 0);
00272   virtual GBool isBinary(GBool last = gTrue) { return last; }
00273   virtual Guint getStart() { return start; }
00274   virtual void moveStart(int delta);
00275 
00276 private:
00277 
00278   GBool fillBuf();
00279 
00280   FILE *f;
00281   Guint start;
00282   GBool limited;
00283   Guint length;
00284   char buf[fileStreamBufSize];
00285   char *bufPtr;
00286   char *bufEnd;
00287   Guint bufPos;
00288   int savePos;
00289   GBool saved;
00290 };
00291 
00292 //------------------------------------------------------------------------
00293 // MemStream
00294 //------------------------------------------------------------------------
00295 
00296 class MemStream: public BaseStream {
00297 public:
00298 
00299   MemStream(char *bufA, Guint lengthA, Object *dictA);
00300   virtual ~MemStream();
00301   virtual Stream *makeSubStream(Guint start, GBool limited,
00302                 Guint lengthA, Object *dictA);
00303   virtual StreamKind getKind() { return strWeird; }
00304   virtual void reset();
00305   virtual void close();
00306   virtual int getChar()
00307     { return (bufPtr < bufEnd) ? (*bufPtr++ & 0xff) : EOF; }
00308   virtual int lookChar()
00309     { return (bufPtr < bufEnd) ? (*bufPtr & 0xff) : EOF; }
00310   virtual int getPos() { return bufPtr - buf; }
00311   virtual void setPos(Guint pos, int dir = 0);
00312   virtual GBool isBinary(GBool last = gTrue) { return last; }
00313   virtual Guint getStart() { return 0; }
00314   virtual void moveStart(int delta);
00315 #ifndef NO_DECRYPTION
00316   virtual void doDecryption(Guchar *fileKey, int keyLength,
00317                 int objNum, int objGen);
00318 #endif
00319 
00320 private:
00321 
00322   char *buf;
00323   Guint length;
00324   GBool needFree;
00325   char *bufEnd;
00326   char *bufPtr;
00327 };
00328 
00329 //------------------------------------------------------------------------
00330 // EmbedStream
00331 //
00332 // This is a special stream type used for embedded streams (inline
00333 // images).  It reads directly from the base stream -- after the
00334 // EmbedStream is deleted, reads from the base stream will proceed where
00335 // the BaseStream left off.  Note that this is very different behavior
00336 // that creating a new FileStream (using makeSubStream).
00337 //------------------------------------------------------------------------
00338 
00339 class EmbedStream: public BaseStream {
00340 public:
00341 
00342   EmbedStream(Stream *strA, Object *dictA);
00343   virtual ~EmbedStream();
00344   virtual Stream *makeSubStream(Guint start, GBool limited,
00345                 Guint length, Object *dictA);
00346   virtual StreamKind getKind() { return str->getKind(); }
00347   virtual void reset() {}
00348   virtual int getChar() { return str->getChar(); }
00349   virtual int lookChar() { return str->lookChar(); }
00350   virtual int getPos() { return str->getPos(); }
00351   virtual void setPos(Guint pos, int dir = 0);
00352   virtual GBool isBinary(GBool last = gTrue) { return last; }
00353   virtual Guint getStart();
00354   virtual void moveStart(int delta);
00355 
00356 private:
00357 
00358   Stream *str;
00359 };
00360 
00361 //------------------------------------------------------------------------
00362 // ASCIIHexStream
00363 //------------------------------------------------------------------------
00364 
00365 class ASCIIHexStream: public FilterStream {
00366 public:
00367 
00368   ASCIIHexStream(Stream *strA);
00369   virtual ~ASCIIHexStream();
00370   virtual StreamKind getKind() { return strASCIIHex; }
00371   virtual void reset();
00372   virtual int getChar()
00373     { int c = lookChar(); buf = EOF; return c; }
00374   virtual int lookChar();
00375   virtual GString *getPSFilter(const char *indent);
00376   virtual GBool isBinary(GBool last = gTrue);
00377 
00378 private:
00379 
00380   int buf;
00381   GBool eof;
00382 };
00383 
00384 //------------------------------------------------------------------------
00385 // ASCII85Stream
00386 //------------------------------------------------------------------------
00387 
00388 class ASCII85Stream: public FilterStream {
00389 public:
00390 
00391   ASCII85Stream(Stream *strA);
00392   virtual ~ASCII85Stream();
00393   virtual StreamKind getKind() { return strASCII85; }
00394   virtual void reset();
00395   virtual int getChar()
00396     { int ch = lookChar(); ++index; return ch; }
00397   virtual int lookChar();
00398   virtual GString *getPSFilter(const char *indent);
00399   virtual GBool isBinary(GBool last = gTrue);
00400 
00401 private:
00402 
00403   int c[5];
00404   int b[4];
00405   int index, n;
00406   GBool eof;
00407 };
00408 
00409 //------------------------------------------------------------------------
00410 // LZWStream
00411 //------------------------------------------------------------------------
00412 
00413 class LZWStream: public FilterStream {
00414 public:
00415 
00416   LZWStream(Stream *strA, int predictor, int columns, int colors,
00417         int bits, int earlyA);
00418   virtual ~LZWStream();
00419   virtual StreamKind getKind() { return strLZW; }
00420   virtual void reset();
00421   virtual int getChar();
00422   virtual int lookChar();
00423   virtual int getRawChar();
00424   virtual GString *getPSFilter(const char *indent);
00425   virtual GBool isBinary(GBool last = gTrue);
00426 
00427 private:
00428 
00429   StreamPredictor *pred;    // predictor
00430   int early;            // early parameter
00431   GBool eof;            // true if at eof
00432   int inputBuf;         // input buffer
00433   int inputBits;        // number of bits in input buffer
00434   struct {          // decoding table
00435     int length;
00436     int head;
00437     Guchar tail;
00438   } table[4097];
00439   int nextCode;         // next code to be used
00440   int nextBits;         // number of bits in next code word
00441   int prevCode;         // previous code used in stream
00442   int newChar;          // next char to be added to table
00443   Guchar seqBuf[4097];      // buffer for current sequence
00444   int seqLength;        // length of current sequence
00445   int seqIndex;         // index into current sequence
00446   GBool first;          // first code after a table clear
00447 
00448   GBool processNextCode();
00449   void clearTable();
00450   int getCode();
00451 };
00452 
00453 //------------------------------------------------------------------------
00454 // RunLengthStream
00455 //------------------------------------------------------------------------
00456 
00457 class RunLengthStream: public FilterStream {
00458 public:
00459 
00460   RunLengthStream(Stream *strA);
00461   virtual ~RunLengthStream();
00462   virtual StreamKind getKind() { return strRunLength; }
00463   virtual void reset();
00464   virtual int getChar()
00465     { return (bufPtr >= bufEnd && !fillBuf()) ? EOF : (*bufPtr++ & 0xff); }
00466   virtual int lookChar()
00467     { return (bufPtr >= bufEnd && !fillBuf()) ? EOF : (*bufPtr & 0xff); }
00468   virtual GString *getPSFilter(const char *indent);
00469   virtual GBool isBinary(GBool last = gTrue);
00470 
00471 private:
00472 
00473   char buf[128];        // buffer
00474   char *bufPtr;         // next char to read
00475   char *bufEnd;         // end of buffer
00476   GBool eof;
00477 
00478   GBool fillBuf();
00479 };
00480 
00481 //------------------------------------------------------------------------
00482 // CCITTFaxStream
00483 //------------------------------------------------------------------------
00484 
00485 struct CCITTCodeTable;
00486 
00487 class CCITTFaxStream: public FilterStream {
00488 public:
00489 
00490   CCITTFaxStream(Stream *strA, int encodingA, GBool endOfLineA,
00491          GBool byteAlignA, int columnsA, int rowsA,
00492          GBool endOfBlockA, GBool blackA);
00493   virtual ~CCITTFaxStream();
00494   virtual StreamKind getKind() { return strCCITTFax; }
00495   virtual void reset();
00496   virtual int getChar()
00497     { int c = lookChar(); buf = EOF; return c; }
00498   virtual int lookChar();
00499   virtual GString *getPSFilter(const char *indent);
00500   virtual GBool isBinary(GBool last = gTrue);
00501 
00502 private:
00503 
00504   int encoding;         // 'K' parameter
00505   GBool endOfLine;      // 'EndOfLine' parameter
00506   GBool byteAlign;      // 'EncodedByteAlign' parameter
00507   int columns;          // 'Columns' parameter
00508   int rows;         // 'Rows' parameter
00509   GBool endOfBlock;     // 'EndOfBlock' parameter
00510   GBool black;          // 'BlackIs1' parameter
00511   GBool eof;            // true if at eof
00512   GBool nextLine2D;     // true if next line uses 2D encoding
00513   int row;          // current row
00514   int inputBuf;         // input buffer
00515   int inputBits;        // number of bits in input buffer
00516   int *codingLine;             // coding line changing elements
00517   int *refLine;                        // reference line changing elements
00518   int a0i;                     // index into codingLine
00519   GBool err;                   // error on current line
00520 
00521   int outputBits;       // remaining output bits
00522   int buf;          // character buffer
00523 
00524   void addPixels(int a1, int black);
00525   void addPixelsNeg(int a1, int black);
00526 
00527   short getTwoDimCode();
00528   short getWhiteCode();
00529   short getBlackCode();
00530   short lookBits(int n);
00531   void eatBits(int n) { if ((inputBits -= n) < 0) inputBits = 0; }
00532 };
00533 
00534 //------------------------------------------------------------------------
00535 // DCTStream
00536 //------------------------------------------------------------------------
00537 
00538 // DCT component info
00539 struct DCTCompInfo {
00540   int id;           // component ID
00541   int hSample, vSample;     // horiz/vert sampling resolutions
00542   int quantTable;       // quantization table number
00543   int prevDC;           // DC coefficient accumulator
00544 };
00545 
00546 struct DCTScanInfo {
00547   GBool comp[4];        // comp[i] is set if component i is
00548                 //   included in this scan
00549   int numComps;         // number of components in the scan
00550   int dcHuffTable[4];       // DC Huffman table numbers
00551   int acHuffTable[4];       // AC Huffman table numbers
00552   int firstCoeff, lastCoeff;    // first and last DCT coefficient
00553   int ah, al;           // successive approximation parameters
00554 };
00555 
00556 // DCT Huffman decoding table
00557 struct DCTHuffTable {
00558   Guchar firstSym[17];      // first symbol for this bit length
00559   Gushort firstCode[17];    // first code for this bit length
00560   Gushort numCodes[17];     // number of codes of this bit length
00561   Guchar sym[256];      // symbols
00562 };
00563 
00564 class DCTStream: public FilterStream {
00565 public:
00566 
00567   DCTStream(Stream *strA);
00568   virtual ~DCTStream();
00569   virtual StreamKind getKind() { return strDCT; }
00570   virtual void reset();
00571   virtual int getChar();
00572   virtual int lookChar();
00573   virtual GString *getPSFilter(const char *indent);
00574   virtual GBool isBinary(GBool last = gTrue);
00575   Stream *getRawStream() { return str; }
00576 
00577 private:
00578 
00579   GBool progressive;        // set if in progressive mode
00580   GBool interleaved;        // set if in interleaved mode
00581   int width, height;        // image size
00582   int mcuWidth, mcuHeight;  // size of min coding unit, in data units
00583   int bufWidth, bufHeight;  // frameBuf size
00584   DCTCompInfo compInfo[4];  // info for each component
00585   DCTScanInfo scanInfo;     // info for the current scan
00586   int numComps;         // number of components in image
00587   int colorXform;       // need YCbCr-to-RGB transform?
00588   GBool gotAdobeMarker;     // set if APP14 Adobe marker was present
00589   int restartInterval;      // restart interval, in MCUs
00590   Guchar quantTables[4][64];    // quantization tables
00591   int numQuantTables;       // number of quantization tables
00592   DCTHuffTable dcHuffTables[4]; // DC Huffman tables
00593   DCTHuffTable acHuffTables[4]; // AC Huffman tables
00594   int numDCHuffTables;      // number of DC Huffman tables
00595   int numACHuffTables;      // number of AC Huffman tables
00596   Guchar *rowBuf[4][32];    // buffer for one MCU (non-progressive mode)
00597   int *frameBuf[4];     // buffer for frame (progressive mode)
00598   int comp, x, y, dy;       // current position within image/MCU
00599   int restartCtr;       // MCUs left until restart
00600   int restartMarker;        // next restart marker
00601   int eobRun;           // number of EOBs left in the current run
00602   int inputBuf;         // input buffer for variable length codes
00603   int inputBits;        // number of valid bits in input buffer
00604 
00605   void restart();
00606   GBool readMCURow();
00607   void readScan();
00608   GBool readDataUnit(DCTHuffTable *dcHuffTable,
00609              DCTHuffTable *acHuffTable,
00610              int *prevDC, int data[64]);
00611   GBool readProgressiveDataUnit(DCTHuffTable *dcHuffTable,
00612                 DCTHuffTable *acHuffTable,
00613                 int *prevDC, int data[64]);
00614   void decodeImage();
00615   void transformDataUnit(Guchar *quantTable,
00616              int dataIn[64], Guchar dataOut[64]);
00617   int readHuffSym(DCTHuffTable *table);
00618   int readAmp(int size);
00619   int readBit();
00620   GBool readHeader();
00621   GBool readBaselineSOF();
00622   GBool readProgressiveSOF();
00623   GBool readScanInfo();
00624   GBool readQuantTables();
00625   GBool readHuffmanTables();
00626   GBool readRestartInterval();
00627   GBool readAdobeMarker();
00628   GBool readTrailer();
00629   int readMarker();
00630   int read16();
00631 };
00632 
00633 //------------------------------------------------------------------------
00634 // FlateStream
00635 //------------------------------------------------------------------------
00636 
00637 #define flateWindow          32768    // buffer size
00638 #define flateMask            (flateWindow-1)
00639 #define flateMaxHuffman         15    // max Huffman code length
00640 #define flateMaxCodeLenCodes    19    // max # code length codes
00641 #define flateMaxLitCodes       288    // max # literal codes
00642 #define flateMaxDistCodes       30    // max # distance codes
00643 
00644 // Huffman code table entry
00645 struct FlateCode {
00646   Gushort len;          // code length, in bits
00647   Gushort val;          // value represented by this code
00648 };
00649 
00650 struct FlateHuffmanTab {
00651   FlateCode *codes;
00652   int maxLen;
00653 };
00654 
00655 // Decoding info for length and distance code words
00656 struct FlateDecode {
00657   int bits;         // # extra bits
00658   int first;            // first length/distance
00659 };
00660 
00661 class FlateStream: public FilterStream {
00662 public:
00663 
00664   FlateStream(Stream *strA, int predictor, int columns,
00665           int colors, int bits);
00666   virtual ~FlateStream();
00667   virtual StreamKind getKind() { return strFlate; }
00668   virtual void reset();
00669   virtual int getChar();
00670   virtual int lookChar();
00671   virtual int getRawChar();
00672   virtual GString *getPSFilter(const char *indent);
00673   virtual GBool isBinary(GBool last = gTrue);
00674 
00675 private:
00676 
00677   StreamPredictor *pred;    // predictor
00678   Guchar buf[flateWindow];  // output data buffer
00679   int index;            // current index into output buffer
00680   int remain;           // number valid bytes in output buffer
00681   int codeBuf;          // input buffer
00682   int codeSize;         // number of bits in input buffer
00683   int               // literal and distance code lengths
00684     codeLengths[flateMaxLitCodes + flateMaxDistCodes];
00685   FlateHuffmanTab litCodeTab;   // literal code table
00686   FlateHuffmanTab distCodeTab;  // distance code table
00687   GBool compressedBlock;    // set if reading a compressed block
00688   int blockLen;         // remaining length of uncompressed block
00689   GBool endOfBlock;     // set when end of block is reached
00690   GBool eof;            // set when end of stream is reached
00691 
00692   static int            // code length code reordering
00693     codeLenCodeMap[flateMaxCodeLenCodes];
00694   static FlateDecode        // length decoding info
00695     lengthDecode[flateMaxLitCodes-257];
00696   static FlateDecode        // distance decoding info
00697     distDecode[flateMaxDistCodes];
00698 
00699   void readSome();
00700   GBool startBlock();
00701   void loadFixedCodes();
00702   GBool readDynamicCodes();
00703   void compHuffmanCodes(int *lengths, int n, FlateHuffmanTab *tab);
00704   int getHuffmanCodeWord(FlateHuffmanTab *tab);
00705   int getCodeWord(int bits);
00706 };
00707 
00708 //------------------------------------------------------------------------
00709 // EOFStream
00710 //------------------------------------------------------------------------
00711 
00712 class EOFStream: public FilterStream {
00713 public:
00714 
00715   EOFStream(Stream *strA);
00716   virtual ~EOFStream();
00717   virtual StreamKind getKind() { return strWeird; }
00718   virtual void reset() {}
00719   virtual int getChar() { return EOF; }
00720   virtual int lookChar() { return EOF; }
00721   virtual GString *getPSFilter(const char */*indent*/)  { return NULL; }
00722   virtual GBool isBinary(GBool /*last*/ = gTrue) { return gFalse; }
00723 };
00724 
00725 //------------------------------------------------------------------------
00726 // FixedLengthEncoder
00727 //------------------------------------------------------------------------
00728 
00729 class FixedLengthEncoder: public FilterStream {
00730 public:
00731 
00732   FixedLengthEncoder(Stream *strA, int lengthA);
00733   ~FixedLengthEncoder();
00734   virtual StreamKind getKind() { return strWeird; }
00735   virtual void reset();
00736   virtual void close();
00737   virtual int getChar();
00738   virtual int lookChar();
00739   virtual GString *getPSFilter(const char */*indent*/) { return NULL; }
00740   virtual GBool isBinary(GBool /*last*/ = gTrue) { return gFalse; }
00741   virtual GBool isEncoder() { return gTrue; }
00742 
00743 private:
00744 
00745   int length;
00746   int count;
00747 };
00748 
00749 //------------------------------------------------------------------------
00750 // ASCIIHexEncoder
00751 //------------------------------------------------------------------------
00752 
00753 class ASCIIHexEncoder: public FilterStream {
00754 public:
00755 
00756   ASCIIHexEncoder(Stream *strA);
00757   virtual ~ASCIIHexEncoder();
00758   virtual StreamKind getKind() { return strWeird; }
00759   virtual void reset();
00760   virtual void close();
00761   virtual int getChar()
00762     { return (bufPtr >= bufEnd && !fillBuf()) ? EOF : (*bufPtr++ & 0xff); }
00763   virtual int lookChar()
00764     { return (bufPtr >= bufEnd && !fillBuf()) ? EOF : (*bufPtr & 0xff); }
00765   virtual GString *getPSFilter(const char */*indent*/) { return NULL; }
00766   virtual GBool isBinary(GBool /*last*/ = gTrue) { return gFalse; }
00767   virtual GBool isEncoder() { return gTrue; }
00768 
00769 private:
00770 
00771   char buf[4];
00772   char *bufPtr;
00773   char *bufEnd;
00774   int lineLen;
00775   GBool eof;
00776 
00777   GBool fillBuf();
00778 };
00779 
00780 //------------------------------------------------------------------------
00781 // ASCII85Encoder
00782 //------------------------------------------------------------------------
00783 
00784 class ASCII85Encoder: public FilterStream {
00785 public:
00786 
00787   ASCII85Encoder(Stream *strA);
00788   virtual ~ASCII85Encoder();
00789   virtual StreamKind getKind() { return strWeird; }
00790   virtual void reset();
00791   virtual void close();
00792   virtual int getChar()
00793     { return (bufPtr >= bufEnd && !fillBuf()) ? EOF : (*bufPtr++ & 0xff); }
00794   virtual int lookChar()
00795     { return (bufPtr >= bufEnd && !fillBuf()) ? EOF : (*bufPtr & 0xff); }
00796   virtual GString *getPSFilter(const char */*indent*/) { return NULL; }
00797   virtual GBool isBinary(GBool /*last*/ = gTrue) { return gFalse; }
00798   virtual GBool isEncoder() { return gTrue; }
00799 
00800 private:
00801 
00802   char buf[8];
00803   char *bufPtr;
00804   char *bufEnd;
00805   int lineLen;
00806   GBool eof;
00807 
00808   GBool fillBuf();
00809 };
00810 
00811 //------------------------------------------------------------------------
00812 // RunLengthEncoder
00813 //------------------------------------------------------------------------
00814 
00815 class RunLengthEncoder: public FilterStream {
00816 public:
00817 
00818   RunLengthEncoder(Stream *strA);
00819   virtual ~RunLengthEncoder();
00820   virtual StreamKind getKind() { return strWeird; }
00821   virtual void reset();
00822   virtual void close();
00823   virtual int getChar()
00824     { return (bufPtr >= bufEnd && !fillBuf()) ? EOF : (*bufPtr++ & 0xff); }
00825   virtual int lookChar()
00826     { return (bufPtr >= bufEnd && !fillBuf()) ? EOF : (*bufPtr & 0xff); }
00827   virtual GString *getPSFilter(const char */*indent*/) { return NULL; }
00828   virtual GBool isBinary(GBool /*last*/ = gTrue) { return gFalse; }
00829   virtual GBool isEncoder() { return gTrue; }
00830 
00831 private:
00832 
00833   char buf[131];
00834   char *bufPtr;
00835   char *bufEnd;
00836   char *nextEnd;
00837   GBool eof;
00838 
00839   GBool fillBuf();
00840 };
00841 
00842 #endif
KDE Home | KDE Accessibility Home | Description of Access Keys