1 //========================================================================
5 // Copyright (C) 2005, Jeff Muizelaar
7 //========================================================================
8 #include "FlateStream.h"
9 FlateStream::FlateStream(Stream
*strA
, int predictor
, int columns
, int colors
, int bits
) :
13 pred
= new StreamPredictor(this, predictor
, columns
, colors
, bits
);
18 memset(&d_stream
, 0, sizeof(d_stream
));
19 inflateInit(&d_stream
);
22 FlateStream::~FlateStream() {
23 inflateEnd(&d_stream
);
28 void FlateStream::reset() {
29 //FIXME: what are the semantics of reset?
30 //i.e. how much intialization has to happen in the constructor?
32 /* reinitialize zlib */
33 inflateEnd(&d_stream
);
34 memset(&d_stream
, 0, sizeof(d_stream
));
35 inflateInit(&d_stream
);
38 d_stream
.avail_in
= 0;
44 int FlateStream::getChar() {
46 return pred
->getChar();
51 int FlateStream::lookChar() {
53 return pred
->lookChar();
58 return out_buf
[out_pos
];
61 int FlateStream::fill_buffer() {
62 /* only fill the buffer if it has all been used */
63 if (out_pos
>= out_buf_len
) {
64 /* check if the flatestream has been exhausted */
65 if (status
== Z_STREAM_END
) {
69 /* set to the begining of out_buf */
70 d_stream
.avail_out
= sizeof(out_buf
);
71 d_stream
.next_out
= out_buf
;
75 /* buffer is empty so we need to fill it */
76 if (d_stream
.avail_in
== 0) {
78 /* read from the source stream */
79 while (d_stream
.avail_in
< sizeof(in_buf
) && (c
= str
->getChar()) != EOF
) {
80 in_buf
[d_stream
.avail_in
++] = c
;
82 d_stream
.next_in
= in_buf
;
85 /* keep decompressing until we can't anymore */
86 if (d_stream
.avail_out
== 0 || d_stream
.avail_in
== 0 || (status
!= Z_OK
&& status
!= Z_BUF_ERROR
))
88 status
= inflate(&d_stream
, Z_SYNC_FLUSH
);
91 out_buf_len
= sizeof(out_buf
) - d_stream
.avail_out
;
92 if (status
!= Z_OK
&& status
!= Z_STREAM_END
)
101 GooString
*FlateStream::getPSFilter(int psLevel
, char *indent
) {
104 if (psLevel
< 3 || pred
) {
107 if (!(s
= str
->getPSFilter(psLevel
, indent
))) {
110 s
->append(indent
)->append("<< >> /FlateDecode filter\n");
114 GBool
FlateStream::isBinary(GBool last
) {
115 return str
->isBinary(gTrue
);