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

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-12 (Thursday) 16:27:28 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 // https://skia.org/dev/contrib/style#no-define-before-sktypes
10
11 #ifdef SK_HAS_ZLIB
12
13 # ifdef SK_SYSTEM_ZLIB
14 # include <zlib.h>
15 # else
16 # include SK_ZLIB_INCLUDE
17 # endif
18
19 #define SKDEFLATEWSTREAM_INPUT_BUFFER_SIZE 4096
20 #define SKDEFLATEWSTREAM_OUTPUT_BUFFER_SIZE 4224 // 4096 + 128, usually big
21 // enough to always do a
22 // single loop.
23
24 // called by both write() and finalize()
25 static void do_deflate(int flush,
26 z_stream* zStream,
27 SkWStream* out,
28 unsigned char* inBuffer,
29 size_t inBufferSize) {
30 zStream->next_in = inBuffer;
31 zStream->avail_in = inBufferSize;
32 unsigned char outBuffer[SKDEFLATEWSTREAM_OUTPUT_BUFFER_SIZE];
33 SkDEBUGCODE(int returnValue;)
34 do {
35 zStream->next_out = outBuffer;
36 zStream->avail_out = sizeof(outBuffer);
37 SkDEBUGCODE(returnValue =) deflate(zStream, flush);
38 SkASSERT(!zStream->msg);
39
40 out->write(outBuffer, sizeof(outBuffer) - zStream->avail_out);
41 } while (zStream->avail_in || !zStream->avail_out);
42 SkASSERT(flush == Z_FINISH
43 ? returnValue == Z_STREAM_END
44 : returnValue == Z_OK);
45 }
46
47 // Hide all zlib impl details.
48 struct SkDeflateWStream::Impl {
49 SkWStream* fOut;
50 unsigned char fInBuffer[SKDEFLATEWSTREAM_INPUT_BUFFER_SIZE];
51 size_t fInBufferIndex;
52 z_stream fZStream;
53 };
54
55 SkDeflateWStream::SkDeflateWStream(SkWStream* out)
56 : fImpl(SkNEW(SkDeflateWStream::Impl)) {
57 fImpl->fOut = out;
58 fImpl->fInBufferIndex = 0;
59 if (!fImpl->fOut) {
60 return;
61 }
62 fImpl->fZStream.zalloc = Z_NULL;
63 fImpl->fZStream.zfree = Z_NULL;
64 fImpl->fZStream.opaque = Z_NULL;
65 fImpl->fZStream.data_type = Z_BINARY;
66 SkDEBUGCODE(int r =) deflateInit(&fImpl->fZStream, Z_DEFAULT_COMPRESSION);
67 SkASSERT(Z_OK == r);
68 }
69
70 SkDeflateWStream::~SkDeflateWStream() { this->finalize(); }
71
72 void SkDeflateWStream::finalize() {
73 if (!fImpl->fOut) {
74 return;
75 }
76 do_deflate(Z_FINISH, &fImpl->fZStream, fImpl->fOut, fImpl->fInBuffer,
77 fImpl->fInBufferIndex);
78 (void)deflateEnd(&fImpl->fZStream);
79 fImpl->fOut = NULL;
80 }
81
82 bool SkDeflateWStream::write(const void* void_buffer, size_t len) {
83 if (!fImpl->fOut) {
84 return false;
85 }
86 const char* buffer = (const char*)void_buffer;
87 while (len > 0) {
88 size_t tocopy =
89 SkTMin(len, sizeof(fImpl->fInBuffer) - fImpl->fInBufferIndex);
90 memcpy(fImpl->fInBuffer + fImpl->fInBufferIndex, buffer, tocopy);
91 len -= tocopy;
92 buffer += tocopy;
93 fImpl->fInBufferIndex += tocopy;
94 SkASSERT(fImpl->fInBufferIndex <= sizeof(fImpl->fInBuffer));
95
96 // if the buffer isn't filled, don't call into zlib yet.
97 if (sizeof(fImpl->fInBuffer) == fImpl->fInBufferIndex) {
98 do_deflate(Z_SYNC_FLUSH, &fImpl->fZStream, fImpl->fOut,
99 fImpl->fInBuffer, fImpl->fInBufferIndex);
100 fImpl->fInBufferIndex = 0;
101 }
102 }
103 return true;
104 }
105
106 void SkDeflateWStream::flush() {
107 if (!fImpl->fOut) {
108 return;
109 }
110 do_deflate(Z_FULL_FLUSH, &fImpl->fZStream, fImpl->fOut, fImpl->fInBuffer,
111 fImpl->fInBufferIndex);
112 fImpl->fInBufferIndex = 0;
113 }
114
115 size_t SkDeflateWStream::bytesWritten() const {
116 return fImpl->fZStream.total_in + fImpl->fInBufferIndex;
117 }
118
119 #endif // SK_HAS_ZLIB
OLDNEW
« no previous file with comments | « src/pdf/SkDeflateWStream.h ('k') | src/pdf/SkPDFBitmap.h » ('j') | src/pdf/SkPDFBitmap.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698