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

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: mutable bitmaps get copied 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/pdf/SkDeflateWStream.h ('k') | src/pdf/SkPDFBitmap.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #ifndef SK_NO_FLATE
12
13 #include "zlib.h"
14
15 #define SKDEFLATEWSTREAM_INPUT_BUFFER_SIZE 4096
16 #define SKDEFLATEWSTREAM_OUTPUT_BUFFER_SIZE 4224 // 4096 + 128, usually big
17 // enough to always do a
18 // single loop.
19
20 // called by both write() and finalize()
21 static void do_deflate(int flush,
22 z_stream* zStream,
23 SkWStream* out,
24 unsigned char* inBuffer,
25 size_t inBufferSize) {
26 zStream->next_in = inBuffer;
27 zStream->avail_in = inBufferSize;
28 unsigned char outBuffer[SKDEFLATEWSTREAM_OUTPUT_BUFFER_SIZE];
29 SkDEBUGCODE(int returnValue;)
30 do {
31 zStream->next_out = outBuffer;
32 zStream->avail_out = sizeof(outBuffer);
33 SkDEBUGCODE(returnValue =) deflate(zStream, flush);
34 SkASSERT(!zStream->msg);
35
36 out->write(outBuffer, sizeof(outBuffer) - zStream->avail_out);
37 } while (zStream->avail_in || !zStream->avail_out);
38 SkASSERT(flush == Z_FINISH
39 ? returnValue == Z_STREAM_END
40 : returnValue == Z_OK);
41 }
42
43 // Hide all zlib impl details.
44 struct SkDeflateWStream::Impl {
45 SkWStream* fOut;
46 unsigned char fInBuffer[SKDEFLATEWSTREAM_INPUT_BUFFER_SIZE];
47 size_t fInBufferIndex;
48 z_stream fZStream;
49 };
50
51 SkDeflateWStream::SkDeflateWStream(SkWStream* out)
52 : fImpl(SkNEW(SkDeflateWStream::Impl)) {
53 fImpl->fOut = out;
54 fImpl->fInBufferIndex = 0;
55 if (!fImpl->fOut) {
56 return;
57 }
58 fImpl->fZStream.zalloc = Z_NULL;
59 fImpl->fZStream.zfree = Z_NULL;
60 fImpl->fZStream.opaque = Z_NULL;
61 fImpl->fZStream.data_type = Z_BINARY;
62 SkDEBUGCODE(int r =) deflateInit(&fImpl->fZStream, Z_DEFAULT_COMPRESSION);
63 SkASSERT(Z_OK == r);
64 }
65
66 SkDeflateWStream::~SkDeflateWStream() { this->finalize(); }
67
68 void SkDeflateWStream::finalize() {
69 if (!fImpl->fOut) {
70 return;
71 }
72 do_deflate(Z_FINISH, &fImpl->fZStream, fImpl->fOut, fImpl->fInBuffer,
73 fImpl->fInBufferIndex);
74 (void)deflateEnd(&fImpl->fZStream);
75 fImpl->fOut = NULL;
76 }
77
78 bool SkDeflateWStream::write(const void* void_buffer, size_t len) {
79 if (!fImpl->fOut) {
80 return false;
81 }
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 into zlib yet.
93 if (sizeof(fImpl->fInBuffer) == fImpl->fInBufferIndex) {
94 do_deflate(Z_NO_FLUSH, &fImpl->fZStream, fImpl->fOut,
95 fImpl->fInBuffer, fImpl->fInBufferIndex);
96 fImpl->fInBufferIndex = 0;
97 }
98 }
99 return true;
100 }
101
102 size_t SkDeflateWStream::bytesWritten() const {
103 return fImpl->fZStream.total_in + fImpl->fInBufferIndex;
104 }
105
106 #endif // SK_NO_ZLIB
OLDNEW
« no previous file with comments | « src/pdf/SkDeflateWStream.h ('k') | src/pdf/SkPDFBitmap.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698