00001
00002
00003
00004
00005
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 strJPX,
00040 strWeird
00041 };
00042
00043
00044
00045
00046
00047 class Stream {
00048 public:
00049
00050
00051 Stream();
00052
00053
00054 virtual ~Stream();
00055
00056
00057 int incRef() { return ++ref; }
00058 int decRef() { return --ref; }
00059
00060
00061 virtual StreamKind getKind() = 0;
00062
00063
00064 virtual void reset() = 0;
00065
00066
00067 virtual void close();
00068
00069
00070 virtual int getChar() = 0;
00071
00072
00073 virtual int lookChar() = 0;
00074
00075
00076
00077 virtual int getRawChar();
00078
00079
00080 virtual char *getLine(char *buf, int size);
00081
00082
00083 virtual int getPos() = 0;
00084
00085
00086
00087
00088 virtual void setPos(Guint pos, int dir = 0) = 0;
00089
00090
00091 virtual GString *getPSFilter(int psLevel, char *indent);
00092
00093
00094 virtual GBool isBinary(GBool last = gTrue) = 0;
00095
00096
00097 virtual BaseStream *getBaseStream() = 0;
00098
00099
00100 virtual Dict *getDict() = 0;
00101
00102
00103 virtual GBool isEncoder() { return gFalse; }
00104
00105
00106
00107 Stream *addFilters(Object *dict);
00108
00109
00110
00111
00112 virtual void ignoreLength() {}
00113
00114 private:
00115
00116 Stream *makeFilter(char *name, Stream *str, Object *params);
00117
00118 int ref;
00119 };
00120
00121
00122
00123
00124
00125
00126
00127 class BaseStream: public Stream {
00128 public:
00129
00130 BaseStream(Object *dictA);
00131 virtual ~BaseStream();
00132 virtual Stream *makeSubStream(Guint start, GBool limited,
00133 Guint length, Object *dict) = 0;
00134 virtual void setPos(Guint pos, int dir = 0) = 0;
00135 virtual GBool isBinary(GBool last = gTrue) { return last; }
00136 virtual BaseStream *getBaseStream() { return this; }
00137 virtual Dict *getDict() { return dict.getDict(); }
00138
00139
00140 virtual Guint getStart() = 0;
00141 virtual void moveStart(int delta) = 0;
00142
00143 #ifndef NO_DECRYPTION
00144
00145 virtual void doDecryption(Guchar *fileKey, int keyLength,
00146 int objNum, int objGen);
00147 #endif
00148
00149 #ifndef NO_DECRYPTION
00150 protected:
00151
00152 Decrypt *decrypt;
00153 #endif
00154
00155 private:
00156
00157 Object dict;
00158 };
00159
00160
00161
00162
00163
00164
00165
00166 class FilterStream: public Stream {
00167 public:
00168
00169 FilterStream(Stream *strA);
00170 virtual ~FilterStream();
00171 virtual void close();
00172 virtual int getPos() { return str->getPos(); }
00173 virtual void setPos(Guint pos, int dir = 0);
00174 virtual BaseStream *getBaseStream() { return str->getBaseStream(); }
00175 virtual Dict *getDict() { return str->getDict(); }
00176 virtual void ignoreLength() { str->ignoreLength(); }
00177
00178 protected:
00179
00180 Stream *str;
00181 };
00182
00183
00184
00185
00186
00187 class ImageStream {
00188 public:
00189
00190
00191
00192
00193 ImageStream(Stream *strA, int widthA, int nCompsA, int nBitsA);
00194
00195 ~ImageStream();
00196
00197
00198 void reset();
00199
00200
00201
00202 GBool getPixel(Guchar *pix);
00203
00204
00205
00206 Guchar *getLine();
00207
00208
00209 void skipLine();
00210
00211 private:
00212
00213 Stream *str;
00214 int width;
00215 int nComps;
00216 int nBits;
00217 int nVals;
00218 Guchar *imgLine;
00219 int imgIdx;
00220 };
00221
00222
00223
00224
00225
00226 class StreamPredictor {
00227 public:
00228
00229
00230
00231 StreamPredictor(Stream *strA, int predictorA,
00232 int widthA, int nCompsA, int nBitsA);
00233
00234 ~StreamPredictor();
00235
00236 int lookChar();
00237 int getChar();
00238
00239 private:
00240
00241 GBool getNextLine();
00242
00243 Stream *str;
00244 int predictor;
00245 int width;
00246 int nComps;
00247 int nBits;
00248 int nVals;
00249 int pixBytes;
00250 int rowBytes;
00251 Guchar *predLine;
00252 int predIdx;
00253 };
00254
00255
00256
00257
00258
00259 #define fileStreamBufSize 256
00260
00261 class FileStream: public BaseStream {
00262 public:
00263
00264 FileStream(FILE *fA, Guint startA, GBool limitedA,
00265 Guint lengthA, Object *dictA);
00266 virtual ~FileStream();
00267 virtual Stream *makeSubStream(Guint startA, GBool limitedA,
00268 Guint lengthA, Object *dictA);
00269 virtual StreamKind getKind() { return strFile; }
00270 virtual void reset();
00271 virtual void close();
00272 virtual int getChar()
00273 { return (bufPtr >= bufEnd && !fillBuf()) ? EOF : (*bufPtr++ & 0xff); }
00274 virtual int lookChar()
00275 { return (bufPtr >= bufEnd && !fillBuf()) ? EOF : (*bufPtr & 0xff); }
00276 virtual int getPos() { return bufPos + (bufPtr - buf); }
00277 virtual void setPos(Guint pos, int dir = 0);
00278 virtual void ignoreLength() { limited = gFalse; }
00279 virtual Guint getStart() { return start; }
00280 virtual void moveStart(int delta);
00281
00282 private:
00283
00284 GBool fillBuf();
00285
00286 FILE *f;
00287 Guint start;
00288 GBool limited;
00289 Guint length;
00290 char buf[fileStreamBufSize];
00291 char *bufPtr;
00292 char *bufEnd;
00293 Guint bufPos;
00294 int savePos;
00295 GBool saved;
00296 };
00297
00298
00299
00300
00301
00302 class MemStream: public BaseStream {
00303 public:
00304
00305 MemStream(char *bufA, Guint startA, Guint lengthA, Object *dictA);
00306 virtual ~MemStream();
00307 virtual Stream *makeSubStream(Guint start, GBool limited,
00308 Guint lengthA, Object *dictA);
00309 virtual StreamKind getKind() { return strWeird; }
00310 virtual void reset();
00311 virtual void close();
00312 virtual int getChar()
00313 { return (bufPtr < bufEnd) ? (*bufPtr++ & 0xff) : EOF; }
00314 virtual int lookChar()
00315 { return (bufPtr < bufEnd) ? (*bufPtr & 0xff) : EOF; }
00316 virtual int getPos() { return (int)(bufPtr - buf); }
00317 virtual void setPos(Guint pos, int dir = 0);
00318 virtual Guint getStart() { return start; }
00319 virtual void moveStart(int delta);
00320 #ifndef NO_DECRYPTION
00321 virtual void doDecryption(Guchar *fileKey, int keyLength,
00322 int objNum, int objGen);
00323 #endif
00324
00325 private:
00326
00327 char *buf;
00328 Guint start;
00329 Guint length;
00330 char *bufEnd;
00331 char *bufPtr;
00332 GBool needFree;
00333 };
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345 class EmbedStream: public BaseStream {
00346 public:
00347
00348 EmbedStream(Stream *strA, Object *dictA, GBool limitedA, Guint lengthA);
00349 virtual ~EmbedStream();
00350 virtual Stream *makeSubStream(Guint start, GBool limitedA,
00351 Guint lengthA, Object *dictA);
00352 virtual StreamKind getKind() { return str->getKind(); }
00353 virtual void reset() {}
00354 virtual int getChar();
00355 virtual int lookChar();
00356 virtual int getPos() { return str->getPos(); }
00357 virtual void setPos(Guint pos, int dir = 0);
00358 virtual Guint getStart();
00359 virtual void moveStart(int delta);
00360
00361 private:
00362
00363 Stream *str;
00364 GBool limited;
00365 Guint length;
00366 };
00367
00368
00369
00370
00371
00372 class ASCIIHexStream: public FilterStream {
00373 public:
00374
00375 ASCIIHexStream(Stream *strA);
00376 virtual ~ASCIIHexStream();
00377 virtual StreamKind getKind() { return strASCIIHex; }
00378 virtual void reset();
00379 virtual int getChar()
00380 { int c = lookChar(); buf = EOF; return c; }
00381 virtual int lookChar();
00382 virtual GString *getPSFilter(int psLevel, char *indent);
00383 virtual GBool isBinary(GBool last = gTrue);
00384
00385 private:
00386
00387 int buf;
00388 GBool eof;
00389 };
00390
00391
00392
00393
00394
00395 class ASCII85Stream: public FilterStream {
00396 public:
00397
00398 ASCII85Stream(Stream *strA);
00399 virtual ~ASCII85Stream();
00400 virtual StreamKind getKind() { return strASCII85; }
00401 virtual void reset();
00402 virtual int getChar()
00403 { int ch = lookChar(); ++index; return ch; }
00404 virtual int lookChar();
00405 virtual GString *getPSFilter(int psLevel, char *indent);
00406 virtual GBool isBinary(GBool last = gTrue);
00407
00408 private:
00409
00410 int c[5];
00411 int b[4];
00412 int index, n;
00413 GBool eof;
00414 };
00415
00416
00417
00418
00419
00420 class LZWStream: public FilterStream {
00421 public:
00422
00423 LZWStream(Stream *strA, int predictor, int columns, int colors,
00424 int bits, int earlyA);
00425 virtual ~LZWStream();
00426 virtual StreamKind getKind() { return strLZW; }
00427 virtual void reset();
00428 virtual int getChar();
00429 virtual int lookChar();
00430 virtual int getRawChar();
00431 virtual GString *getPSFilter(int psLevel, char *indent);
00432 virtual GBool isBinary(GBool last = gTrue);
00433
00434 private:
00435
00436 StreamPredictor *pred;
00437 int early;
00438 GBool eof;
00439 int inputBuf;
00440 int inputBits;
00441 struct {
00442 int length;
00443 int head;
00444 Guchar tail;
00445 } table[4097];
00446 int nextCode;
00447 int nextBits;
00448 int prevCode;
00449 int newChar;
00450 Guchar seqBuf[4097];
00451 int seqLength;
00452 int seqIndex;
00453 GBool first;
00454
00455 GBool processNextCode();
00456 void clearTable();
00457 int getCode();
00458 };
00459
00460
00461
00462
00463
00464 class RunLengthStream: public FilterStream {
00465 public:
00466
00467 RunLengthStream(Stream *strA);
00468 virtual ~RunLengthStream();
00469 virtual StreamKind getKind() { return strRunLength; }
00470 virtual void reset();
00471 virtual int getChar()
00472 { return (bufPtr >= bufEnd && !fillBuf()) ? EOF : (*bufPtr++ & 0xff); }
00473 virtual int lookChar()
00474 { return (bufPtr >= bufEnd && !fillBuf()) ? EOF : (*bufPtr & 0xff); }
00475 virtual GString *getPSFilter(int psLevel, char *indent);
00476 virtual GBool isBinary(GBool last = gTrue);
00477
00478 private:
00479
00480 char buf[128];
00481 char *bufPtr;
00482 char *bufEnd;
00483 GBool eof;
00484
00485 GBool fillBuf();
00486 };
00487
00488
00489
00490
00491
00492 struct CCITTCodeTable;
00493
00494 class CCITTFaxStream: public FilterStream {
00495 public:
00496
00497 CCITTFaxStream(Stream *strA, int encodingA, GBool endOfLineA,
00498 GBool byteAlignA, int columnsA, int rowsA,
00499 GBool endOfBlockA, GBool blackA);
00500 virtual ~CCITTFaxStream();
00501 virtual StreamKind getKind() { return strCCITTFax; }
00502 virtual void reset();
00503 virtual int getChar()
00504 { int c = lookChar(); buf = EOF; return c; }
00505 virtual int lookChar();
00506 virtual GString *getPSFilter(int psLevel, char *indent);
00507 virtual GBool isBinary(GBool last = gTrue);
00508
00509 private:
00510
00511 int encoding;
00512 GBool endOfLine;
00513 GBool byteAlign;
00514 int columns;
00515 int rows;
00516 GBool endOfBlock;
00517 GBool black;
00518 GBool eof;
00519 GBool nextLine2D;
00520 int row;
00521 int inputBuf;
00522 int inputBits;
00523 short *refLine;
00524 int b1;
00525 short *codingLine;
00526 int a0;
00527 int outputBits;
00528 int buf;
00529
00530 short getTwoDimCode();
00531 short getWhiteCode();
00532 short getBlackCode();
00533 short lookBits(int n);
00534 void eatBits(int n) { inputBits -= n; }
00535 };
00536
00537
00538
00539
00540
00541
00542 struct DCTCompInfo {
00543 int id;
00544 int hSample, vSample;
00545 int quantTable;
00546 int prevDC;
00547 };
00548
00549 struct DCTScanInfo {
00550 GBool comp[4];
00551
00552 int numComps;
00553 int dcHuffTable[4];
00554 int acHuffTable[4];
00555 int firstCoeff, lastCoeff;
00556 int ah, al;
00557 };
00558
00559
00560 struct DCTHuffTable {
00561 Guchar firstSym[17];
00562 Gushort firstCode[17];
00563 Gushort numCodes[17];
00564 Guchar sym[256];
00565 };
00566
00567 class DCTStream: public FilterStream {
00568 public:
00569
00570 DCTStream(Stream *strA);
00571 virtual ~DCTStream();
00572 virtual StreamKind getKind() { return strDCT; }
00573 virtual void reset();
00574 virtual int getChar();
00575 virtual int lookChar();
00576 virtual GString *getPSFilter(int psLevel, char *indent);
00577 virtual GBool isBinary(GBool last = gTrue);
00578 Stream *getRawStream() { return str; }
00579
00580 private:
00581
00582 GBool progressive;
00583 GBool interleaved;
00584 int width, height;
00585 int mcuWidth, mcuHeight;
00586 int bufWidth, bufHeight;
00587 DCTCompInfo compInfo[4];
00588 DCTScanInfo scanInfo;
00589 int numComps;
00590 int colorXform;
00591 GBool gotJFIFMarker;
00592 GBool gotAdobeMarker;
00593 int restartInterval;
00594 Guchar quantTables[4][64];
00595 int numQuantTables;
00596 DCTHuffTable dcHuffTables[4];
00597 DCTHuffTable acHuffTables[4];
00598 int numDCHuffTables;
00599 int numACHuffTables;
00600 Guchar *rowBuf[4][32];
00601 int *frameBuf[4];
00602 int comp, x, y, dy;
00603 int restartCtr;
00604 int restartMarker;
00605 int eobRun;
00606 int inputBuf;
00607 int inputBits;
00608
00609 void restart();
00610 GBool readMCURow();
00611 void readScan();
00612 GBool readDataUnit(DCTHuffTable *dcHuffTable,
00613 DCTHuffTable *acHuffTable,
00614 int *prevDC, int data[64]);
00615 GBool readProgressiveDataUnit(DCTHuffTable *dcHuffTable,
00616 DCTHuffTable *acHuffTable,
00617 int *prevDC, int data[64]);
00618 void decodeImage();
00619 void transformDataUnit(Guchar *quantTable,
00620 int dataIn[64], Guchar dataOut[64]);
00621 int readHuffSym(DCTHuffTable *table);
00622 int readAmp(int size);
00623 int readBit();
00624 GBool readHeader();
00625 GBool readBaselineSOF();
00626 GBool readProgressiveSOF();
00627 GBool readScanInfo();
00628 GBool readQuantTables();
00629 GBool readHuffmanTables();
00630 GBool readRestartInterval();
00631 GBool readJFIFMarker();
00632 GBool readAdobeMarker();
00633 GBool readTrailer();
00634 int readMarker();
00635 int read16();
00636 };
00637
00638
00639
00640
00641
00642 #define flateWindow 32768 // buffer size
00643 #define flateMask (flateWindow-1)
00644 #define flateMaxHuffman 15 // max Huffman code length
00645 #define flateMaxCodeLenCodes 19 // max # code length codes
00646 #define flateMaxLitCodes 288 // max # literal codes
00647 #define flateMaxDistCodes 30 // max # distance codes
00648
00649
00650 struct FlateCode {
00651 Gushort len;
00652 Gushort val;
00653 };
00654
00655 struct FlateHuffmanTab {
00656 FlateCode *codes;
00657 int maxLen;
00658 };
00659
00660
00661 struct FlateDecode {
00662 int bits;
00663 int first;
00664 };
00665
00666 class FlateStream: public FilterStream {
00667 public:
00668
00669 FlateStream(Stream *strA, int predictor, int columns,
00670 int colors, int bits);
00671 virtual ~FlateStream();
00672 virtual StreamKind getKind() { return strFlate; }
00673 virtual void reset();
00674 virtual int getChar();
00675 virtual int lookChar();
00676 virtual int getRawChar();
00677 virtual GString *getPSFilter(int psLevel, char *indent);
00678 virtual GBool isBinary(GBool last = gTrue);
00679
00680 private:
00681
00682 StreamPredictor *pred;
00683 Guchar buf[flateWindow];
00684 int index;
00685 int remain;
00686 int codeBuf;
00687 int codeSize;
00688 int
00689 codeLengths[flateMaxLitCodes + flateMaxDistCodes];
00690 FlateHuffmanTab litCodeTab;
00691 FlateHuffmanTab distCodeTab;
00692 GBool compressedBlock;
00693 int blockLen;
00694 GBool endOfBlock;
00695 GBool eof;
00696
00697 static int
00698 codeLenCodeMap[flateMaxCodeLenCodes];
00699 static FlateDecode
00700 lengthDecode[flateMaxLitCodes-257];
00701 static FlateDecode
00702 distDecode[flateMaxDistCodes];
00703
00704 void readSome();
00705 GBool startBlock();
00706 void loadFixedCodes();
00707 GBool readDynamicCodes();
00708 void compHuffmanCodes(int *lengths, int n, FlateHuffmanTab *tab);
00709 int getHuffmanCodeWord(FlateHuffmanTab *tab);
00710 int getCodeWord(int bits);
00711 };
00712
00713
00714
00715
00716
00717 class EOFStream: public FilterStream {
00718 public:
00719
00720 EOFStream(Stream *strA);
00721 virtual ~EOFStream();
00722 virtual StreamKind getKind() { return strWeird; }
00723 virtual void reset() {}
00724 virtual int getChar() { return EOF; }
00725 virtual int lookChar() { return EOF; }
00726 virtual GString *getPSFilter(int psLevel, char *indent) { return NULL; }
00727 virtual GBool isBinary(GBool last = gTrue) { return gFalse; }
00728 };
00729
00730
00731
00732
00733
00734 class FixedLengthEncoder: public FilterStream {
00735 public:
00736
00737 FixedLengthEncoder(Stream *strA, int lengthA);
00738 ~FixedLengthEncoder();
00739 virtual StreamKind getKind() { return strWeird; }
00740 virtual void reset();
00741 virtual int getChar();
00742 virtual int lookChar();
00743 virtual GString *getPSFilter(int psLevel, char *indent) { return NULL; }
00744 virtual GBool isBinary(GBool last = gTrue);
00745 virtual GBool isEncoder() { return gTrue; }
00746
00747 private:
00748
00749 int length;
00750 int count;
00751 };
00752
00753
00754
00755
00756
00757 class ASCIIHexEncoder: public FilterStream {
00758 public:
00759
00760 ASCIIHexEncoder(Stream *strA);
00761 virtual ~ASCIIHexEncoder();
00762 virtual StreamKind getKind() { return strWeird; }
00763 virtual void reset();
00764 virtual int getChar()
00765 { return (bufPtr >= bufEnd && !fillBuf()) ? EOF : (*bufPtr++ & 0xff); }
00766 virtual int lookChar()
00767 { return (bufPtr >= bufEnd && !fillBuf()) ? EOF : (*bufPtr & 0xff); }
00768 virtual GString *getPSFilter(int psLevel, char *indent) { return NULL; }
00769 virtual GBool isBinary(GBool last = gTrue) { return gFalse; }
00770 virtual GBool isEncoder() { return gTrue; }
00771
00772 private:
00773
00774 char buf[4];
00775 char *bufPtr;
00776 char *bufEnd;
00777 int lineLen;
00778 GBool eof;
00779
00780 GBool fillBuf();
00781 };
00782
00783
00784
00785
00786
00787 class ASCII85Encoder: public FilterStream {
00788 public:
00789
00790 ASCII85Encoder(Stream *strA);
00791 virtual ~ASCII85Encoder();
00792 virtual StreamKind getKind() { return strWeird; }
00793 virtual void reset();
00794 virtual int getChar()
00795 { return (bufPtr >= bufEnd && !fillBuf()) ? EOF : (*bufPtr++ & 0xff); }
00796 virtual int lookChar()
00797 { return (bufPtr >= bufEnd && !fillBuf()) ? EOF : (*bufPtr & 0xff); }
00798 virtual GString *getPSFilter(int psLevel, char *indent) { return NULL; }
00799 virtual GBool isBinary(GBool last = gTrue) { return gFalse; }
00800 virtual GBool isEncoder() { return gTrue; }
00801
00802 private:
00803
00804 char buf[8];
00805 char *bufPtr;
00806 char *bufEnd;
00807 int lineLen;
00808 GBool eof;
00809
00810 GBool fillBuf();
00811 };
00812
00813
00814
00815
00816
00817 class RunLengthEncoder: public FilterStream {
00818 public:
00819
00820 RunLengthEncoder(Stream *strA);
00821 virtual ~RunLengthEncoder();
00822 virtual StreamKind getKind() { return strWeird; }
00823 virtual void reset();
00824 virtual int getChar()
00825 { return (bufPtr >= bufEnd && !fillBuf()) ? EOF : (*bufPtr++ & 0xff); }
00826 virtual int lookChar()
00827 { return (bufPtr >= bufEnd && !fillBuf()) ? EOF : (*bufPtr & 0xff); }
00828 virtual GString *getPSFilter(int psLevel, char *indent) { return NULL; }
00829 virtual GBool isBinary(GBool last = gTrue) { return gTrue; }
00830 virtual GBool isEncoder() { return gTrue; }
00831
00832 private:
00833
00834 char buf[131];
00835 char *bufPtr;
00836 char *bufEnd;
00837 char *nextEnd;
00838 GBool eof;
00839
00840 GBool fillBuf();
00841 };
00842
00843 #endif