~ubuntu-branches/ubuntu/wily/luatex/wily

« back to all changes in this revision

Viewing changes to source/libs/xpdf/xpdf-3.02/xpdf/Stream.h

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Preining
  • Date: 2010-04-29 00:47:19 UTC
  • mfrom: (1.1.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20100429004719-o42etkqe90n97b9e
Tags: 0.60.1-1
* new upstream release, adapt build-script patch
* disable patch: upstream-epstopdf_cc_no_xpdf_patching, included upstream
* disable patch: libpoppler-0.12, not needed anymore

Show diffs side-by-side

added added

removed removed

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