1 //========================================================================
5 // Copyright (C) 2005, Jeff Muizelaar <jeff@infidigm.net>
6 // Copyright (C) 2010, Albert Astals Cid <aacid@kde.org>
8 // This file is under the GPLv2 or later license
10 //========================================================================
11 #include "FlateStream.h"
12 FlateStream::FlateStream(Stream
*strA
, int predictor
, int columns
, int colors
, int bits
) :
16 pred
= new StreamPredictor(this, predictor
, columns
, colors
, bits
);
21 memset(&d_stream
, 0, sizeof(d_stream
));
22 inflateInit(&d_stream
);
25 FlateStream::~FlateStream() {
26 inflateEnd(&d_stream
);
31 void FlateStream::reset() {
32 //FIXME: what are the semantics of reset?
33 //i.e. how much intialization has to happen in the constructor?
35 /* reinitialize zlib */
36 inflateEnd(&d_stream
);
37 memset(&d_stream
, 0, sizeof(d_stream
));
38 inflateInit(&d_stream
);
41 d_stream
.avail_in
= 0;
47 int FlateStream::getRawChar() {
48 return doGetRawChar();
51 void FlateStream::getRawChars(int nChars
, int *buffer
) {
52 for (int i
= 0; i
< nChars
; ++i
)
53 buffer
[i
] = doGetRawChar();
56 int FlateStream::getChar() {
58 return pred
->getChar();
63 int FlateStream::lookChar() {
65 return pred
->lookChar();
70 return out_buf
[out_pos
];
73 int FlateStream::fill_buffer() {
74 /* only fill the buffer if it has all been used */
75 if (out_pos
>= out_buf_len
) {
76 /* check if the flatestream has been exhausted */
77 if (status
== Z_STREAM_END
) {
81 /* set to the begining of out_buf */
82 d_stream
.avail_out
= sizeof(out_buf
);
83 d_stream
.next_out
= out_buf
;
87 /* buffer is empty so we need to fill it */
88 if (d_stream
.avail_in
== 0) {
90 /* read from the source stream */
91 while (d_stream
.avail_in
< sizeof(in_buf
) && (c
= str
->getChar()) != EOF
) {
92 in_buf
[d_stream
.avail_in
++] = c
;
94 d_stream
.next_in
= in_buf
;
97 /* keep decompressing until we can't anymore */
98 if (d_stream
.avail_out
== 0 || d_stream
.avail_in
== 0 || (status
!= Z_OK
&& status
!= Z_BUF_ERROR
))
100 status
= inflate(&d_stream
, Z_SYNC_FLUSH
);
103 out_buf_len
= sizeof(out_buf
) - d_stream
.avail_out
;
104 if (status
!= Z_OK
&& status
!= Z_STREAM_END
)
113 GooString
*FlateStream::getPSFilter(int psLevel
, const char *indent
) {
116 if (psLevel
< 3 || pred
) {
119 if (!(s
= str
->getPSFilter(psLevel
, indent
))) {
122 s
->append(indent
)->append("<< >> /FlateDecode filter\n");
126 GBool
FlateStream::isBinary(GBool last
) {
127 return str
->isBinary(gTrue
);