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

Side by Side Diff: src/pdf/SkDeflateWStream.cpp

Issue 918813002: PDF: Add (low-memory) SkPDFBitmap class (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: 2015-02-11 (Wednesday) 18:49:13 EST 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
OLDNEW
(Empty)
1 /*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "SkDeflateWStream.h"
9
10 #ifndef SK_HAS_ZLIB
11
12 SkDeflateWStream::SkDeflateWStream(SkWStream*) { SkDEBUGFAIL("!SK_HAS_ZLIB"); }
13 SkDeflateWStream::~SkDeflateWStream() {}
14 bool SkDeflateWStream::write(const void*, size_t) { return false; }
15 void SkDeflateWStream::flush() {}
16 size_t SkDeflateWStream::bytesWritten() { return 0; }
17
mtklein 2015/02/12 00:21:45 Don't we need to struct SkDeflateWStream::Impl{
hal.canary 2015/02/12 21:28:03 Done.
18 #else // SK_HAS_ZLIB
19
20 # ifdef SK_SYSTEM_ZLIB
21 # include <zlib.h>
22 # else
23 # include SK_ZLIB_INCLUDE
24 # endif
25
26 #define SKDEFLATEWSTREAM_INPUT_BUFFER_SIZE 4096
27 #define SKDEFLATEWSTREAM_OUTPUT_BUFFER_SIZE 4024 // 4096 + 128, usually big
mtklein 2015/02/12 00:21:45 Um, 4024 != 4096 + 128.
hal.canary 2015/02/12 21:28:03 Done. I was testing out big and small values for
28 // enough to always do a
29 // single loop.
30
31 // called by both write() and ~SkDeflateWStream()
32 static void do_deflate(int flush,
33 z_stream* zStream,
34 SkWStream* out,
35 unsigned char* inBuffer,
36 size_t inBufferSize) {
37 zStream->next_in = inBuffer;
38 zStream->avail_in = inBufferSize;
39 unsigned char outBuffer[SKDEFLATEWSTREAM_OUTPUT_BUFFER_SIZE];
40 SkDEBUGCODE(int returnValue;)
41 do {
42 zStream->next_out = outBuffer;
43 zStream->avail_out = sizeof(outBuffer);
44 SkDEBUGCODE(returnValue =) deflate(zStream, flush);
45 SkASSERT(!zStream->msg);
46
47 out->write(outBuffer, sizeof(outBuffer) - zStream->avail_out);
48 } while (zStream->avail_in || !zStream->avail_out);
49 SkASSERT(flush == Z_FINISH
50 ? returnValue == Z_STREAM_END
51 : returnValue == Z_OK);
52 }
53
54 // Hide all zlib impl details.
55 struct SkDeflateWStream::Impl {
56 SkWStream* fOut;
57 unsigned char fInBuffer[SKDEFLATEWSTREAM_INPUT_BUFFER_SIZE];
58 size_t fInBufferIndex;
59 z_stream fZStream;
60 };
61
62 SkDeflateWStream::SkDeflateWStream(SkWStream* out)
63 : fImpl(new SkDeflateWStream::Impl) {
64 fImpl->fOut = out;
65 fImpl->fInBufferIndex = 0;
66 fImpl->fZStream.zalloc = Z_NULL;
67 fImpl->fZStream.zfree = Z_NULL;
68 fImpl->fZStream.opaque = Z_NULL;
69 fImpl->fZStream.data_type = Z_BINARY;
70 SkDEBUGCODE(int r =) deflateInit(&fImpl->fZStream, Z_DEFAULT_COMPRESSION);
71 SkASSERT(Z_OK == r);
72 }
73
74 SkDeflateWStream::~SkDeflateWStream() {
75 do_deflate(Z_FINISH, &fImpl->fZStream, fImpl->fOut, fImpl->fInBuffer,
76 fImpl->fInBufferIndex);
77 (void)deflateEnd(&fImpl->fZStream);
78 delete fImpl;
79 }
80
81 bool SkDeflateWStream::write(const void* void_buffer, size_t len) {
82 const char* buffer = (const char*)void_buffer;
83 while (len > 0) {
84 size_t tocopy =
85 SkTMin(len, sizeof(fImpl->fInBuffer) - fImpl->fInBufferIndex);
86 memcpy(fImpl->fInBuffer + fImpl->fInBufferIndex, buffer, tocopy);
87 len -= tocopy;
88 buffer += tocopy;
89 fImpl->fInBufferIndex += tocopy;
90 SkASSERT(fImpl->fInBufferIndex <= sizeof(fImpl->fInBuffer));
91
92 // if the buffer isn't filled, don't call intp zlib yet.
mtklein 2015/02/12 00:21:45 into
hal.canary 2015/02/12 21:28:03 Done.
93 if (sizeof(fImpl->fInBuffer) == fImpl->fInBufferIndex) {
94 do_deflate(Z_SYNC_FLUSH, &fImpl->fZStream, fImpl->fOut,
95 fImpl->fInBuffer, fImpl->fInBufferIndex);
96 fImpl->fInBufferIndex = 0;
97 }
98 }
99 return true;
100 }
101
102 void SkDeflateWStream::flush() {
103 do_deflate(Z_FULL_FLUSH, &fImpl->fZStream, fImpl->fOut, fImpl->fInBuffer,
104 fImpl->fInBufferIndex);
105 fImpl->fInBufferIndex = 0;
106 }
107
108 size_t SkDeflateWStream::bytesWritten() const {
109 return fImpl->fZStream.total_in + fImpl->fInBufferIndex;
110 }
111
112 #endif // SK_HAS_ZLIB
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698