Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(83)

Side by Side Diff: src/core/SkFlate.cpp

Issue 957323003: miniz support in SkFlate / PDF (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: class comment Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/core/SkFlate.h ('k') | src/pdf/SkDeflateWStream.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* 2 /*
3 * Copyright 2010 The Android Open Source Project 3 * Copyright 2010 The Android Open Source Project
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 8
9 9
10 #include "SkData.h" 10 #include "SkData.h"
11 #include "SkFlate.h" 11 #include "SkFlate.h"
12 #include "SkStream.h" 12 #include "SkStream.h"
13 13
14 #ifndef SK_NO_FLATE 14 #ifndef SK_NO_FLATE
15 15
16 namespace { 16 namespace {
17 17
18 #include "zlib.h" 18 #ifdef ZLIB_INCLUDE
19 #include ZLIB_INCLUDE
20 #else
21 #include "zlib.h"
22 #endif
19 23
20 // static 24 // static
21 const size_t kBufferSize = 1024; 25 const size_t kBufferSize = 1024;
22 26
23 bool doFlate(bool compress, SkStream* src, SkWStream* dst) { 27 bool doFlate(bool compress, SkStream* src, SkWStream* dst) {
24 uint8_t inputBuffer[kBufferSize]; 28 uint8_t inputBuffer[kBufferSize];
25 uint8_t outputBuffer[kBufferSize]; 29 uint8_t outputBuffer[kBufferSize];
26 z_stream flateData; 30 z_stream flateData;
27 flateData.zalloc = NULL; 31 flateData.zalloc = NULL;
28 flateData.zfree = NULL; 32 flateData.zfree = NULL;
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 return doFlate(true, &stream, dst); 119 return doFlate(true, &stream, dst);
116 } 120 }
117 return false; 121 return false;
118 } 122 }
119 123
120 // static 124 // static
121 bool SkFlate::Inflate(SkStream* src, SkWStream* dst) { 125 bool SkFlate::Inflate(SkStream* src, SkWStream* dst) {
122 return doFlate(false, src, dst); 126 return doFlate(false, src, dst);
123 } 127 }
124 128
129
130 #define SKDEFLATEWSTREAM_INPUT_BUFFER_SIZE 4096
131 #define SKDEFLATEWSTREAM_OUTPUT_BUFFER_SIZE 4224 // 4096 + 128, usually big
132 // enough to always do a
133 // single loop.
134
135 // called by both write() and finalize()
136 static void do_deflate(int flush,
137 z_stream* zStream,
138 SkWStream* out,
139 unsigned char* inBuffer,
140 size_t inBufferSize) {
141 zStream->next_in = inBuffer;
142 zStream->avail_in = SkToInt(inBufferSize);
143 unsigned char outBuffer[SKDEFLATEWSTREAM_OUTPUT_BUFFER_SIZE];
144 SkDEBUGCODE(int returnValue;)
145 do {
146 zStream->next_out = outBuffer;
147 zStream->avail_out = sizeof(outBuffer);
148 SkDEBUGCODE(returnValue =) deflate(zStream, flush);
149 SkASSERT(!zStream->msg);
150
151 out->write(outBuffer, sizeof(outBuffer) - zStream->avail_out);
152 } while (zStream->avail_in || !zStream->avail_out);
153 SkASSERT(flush == Z_FINISH
154 ? returnValue == Z_STREAM_END
155 : returnValue == Z_OK);
156 }
157
158 // Hide all zlib impl details.
159 struct SkDeflateWStream::Impl {
160 SkWStream* fOut;
161 unsigned char fInBuffer[SKDEFLATEWSTREAM_INPUT_BUFFER_SIZE];
162 size_t fInBufferIndex;
163 z_stream fZStream;
164 };
165
166 SkDeflateWStream::SkDeflateWStream(SkWStream* out)
167 : fImpl(SkNEW(SkDeflateWStream::Impl)) {
168 fImpl->fOut = out;
169 fImpl->fInBufferIndex = 0;
170 if (!fImpl->fOut) {
171 return;
172 }
173 fImpl->fZStream.zalloc = Z_NULL;
174 fImpl->fZStream.zfree = Z_NULL;
175 fImpl->fZStream.opaque = Z_NULL;
176 SkDEBUGCODE(int r =) deflateInit(&fImpl->fZStream, Z_DEFAULT_COMPRESSION);
177 SkASSERT(Z_OK == r);
178 }
179
180 SkDeflateWStream::~SkDeflateWStream() { this->finalize(); }
181
182 void SkDeflateWStream::finalize() {
183 if (!fImpl->fOut) {
184 return;
185 }
186 do_deflate(Z_FINISH, &fImpl->fZStream, fImpl->fOut, fImpl->fInBuffer,
187 fImpl->fInBufferIndex);
188 (void)deflateEnd(&fImpl->fZStream);
189 fImpl->fOut = NULL;
190 }
191
192 bool SkDeflateWStream::write(const void* void_buffer, size_t len) {
193 if (!fImpl->fOut) {
194 return false;
195 }
196 const char* buffer = (const char*)void_buffer;
197 while (len > 0) {
198 size_t tocopy =
199 SkTMin(len, sizeof(fImpl->fInBuffer) - fImpl->fInBufferIndex);
200 memcpy(fImpl->fInBuffer + fImpl->fInBufferIndex, buffer, tocopy);
201 len -= tocopy;
202 buffer += tocopy;
203 fImpl->fInBufferIndex += tocopy;
204 SkASSERT(fImpl->fInBufferIndex <= sizeof(fImpl->fInBuffer));
205
206 // if the buffer isn't filled, don't call into zlib yet.
207 if (sizeof(fImpl->fInBuffer) == fImpl->fInBufferIndex) {
208 do_deflate(Z_NO_FLUSH, &fImpl->fZStream, fImpl->fOut,
209 fImpl->fInBuffer, fImpl->fInBufferIndex);
210 fImpl->fInBufferIndex = 0;
211 }
212 }
213 return true;
214 }
215
216 size_t SkDeflateWStream::bytesWritten() const {
217 return fImpl->fZStream.total_in + fImpl->fInBufferIndex;
218 }
219
220
125 #endif // SK_NO_FLATE 221 #endif // SK_NO_FLATE
126 222
OLDNEW
« no previous file with comments | « src/core/SkFlate.h ('k') | src/pdf/SkDeflateWStream.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698