source: trunk/poppler/mypoppler/poppler/FlateStream.cc @ 134

Last change on this file since 134 was 134, checked in by Eugene Romanenko, 15 years ago

poppler updated to version 0.5.4

File size: 2.7 KB
Line 
1//========================================================================
2//
3// FlateStream.cc
4//
5// Copyright (C) 2005, Jeff Muizelaar
6//
7//========================================================================
8#include "FlateStream.h"
9FlateStream::FlateStream(Stream *strA, int predictor, int columns, int colors, int bits) :
10  FilterStream(strA)
11{
12  if (predictor != 1) {
13    pred = new StreamPredictor(this, predictor, columns, colors, bits);
14  } else {
15    pred = NULL;
16  }
17  out_pos = 0;
18  memset(&d_stream, 0, sizeof(d_stream));
19  inflateInit(&d_stream);
20}
21
22FlateStream::~FlateStream() {
23  inflateEnd(&d_stream);
24  delete pred;
25  delete str;
26}
27
28void FlateStream::reset() {
29  //FIXME: what are the semantics of reset?
30  //i.e. how much intialization has to happen in the constructor?
31
32  /* reinitialize zlib */
33  inflateEnd(&d_stream);
34  memset(&d_stream, 0, sizeof(d_stream));
35  inflateInit(&d_stream);
36
37  str->reset();
38  d_stream.avail_in = 0;
39  status = Z_OK;
40  out_pos = 0;
41  out_buf_len = 0;
42}
43
44int FlateStream::getRawChar() {
45  if (fill_buffer())
46    return EOF;
47
48  return out_buf[out_pos++];
49}
50
51int FlateStream::getChar() {
52  if (pred)
53    return pred->getChar();
54  else
55    return getRawChar();
56}
57
58int FlateStream::lookChar() {
59  if (pred)
60    return pred->lookChar();
61
62  if (fill_buffer())
63    return EOF;
64
65  return out_buf[out_pos];
66}
67
68int FlateStream::fill_buffer() {
69  /* only fill the buffer if it has all been used */
70  if (out_pos >= out_buf_len) {
71    /* check if the flatestream has been exhausted */
72    if (status == Z_STREAM_END) {
73      return -1;
74    }
75
76    /* set to the begining of out_buf */
77    d_stream.avail_out = sizeof(out_buf);
78    d_stream.next_out = out_buf;
79    out_pos = 0;
80
81    while (1) {
82      /* buffer is empty so we need to fill it */
83      if (d_stream.avail_in == 0) {
84        int c;
85        /* read from the source stream */
86        while (d_stream.avail_in < sizeof(in_buf) && (c = str->getChar()) != EOF) {
87          in_buf[d_stream.avail_in++] = c;
88        }
89        d_stream.next_in = in_buf;
90      }
91
92      /* keep decompressing until we can't anymore */
93      if (d_stream.avail_out == 0 || d_stream.avail_in == 0 || (status != Z_OK && status != Z_BUF_ERROR))
94        break;
95      status = inflate(&d_stream, Z_SYNC_FLUSH);
96    }
97
98    out_buf_len = sizeof(out_buf) - d_stream.avail_out;
99    if (status != Z_OK && status != Z_STREAM_END)
100      return -1;
101    if (!out_buf_len)
102      return -1;
103  }
104
105  return 0;
106}
107
108GooString *FlateStream::getPSFilter(int psLevel, char *indent) {
109  GooString *s;
110
111  if (psLevel < 3 || pred) {
112    return NULL;
113  }
114  if (!(s = str->getPSFilter(psLevel, indent))) {
115    return NULL;
116  }
117  s->append(indent)->append("<< >> /FlateDecode filter\n");
118  return s;
119}
120
121GBool FlateStream::isBinary(GBool last) {
122  return str->isBinary(gTrue);
123}
Note: See TracBrowser for help on using the repository browser.