diff options
author | 2015-08-18 13:30:25 -0700 | |
---|---|---|
committer | 2015-08-18 13:30:25 -0700 | |
commit | 48305e835351291ef72a7084c6c1af7b185d89c0 (patch) | |
tree | 56cf2a20e842163148bb212f9ab606deede363d4 /src/pdf | |
parent | a44919ea27acd9808a7581362018484355c0f026 (diff) |
SkPDF/Deflate: clean up old SkFlate code
Factor out some of https://crrev.com/1227913008
BUG=skia:3030
Review URL: https://codereview.chromium.org/1298243002
Diffstat (limited to 'src/pdf')
-rw-r--r-- | src/pdf/SkDeflate.cpp | 109 | ||||
-rw-r--r-- | src/pdf/SkDeflate.h | 30 | ||||
-rw-r--r-- | src/pdf/SkPDFStream.cpp | 6 |
3 files changed, 5 insertions, 140 deletions
diff --git a/src/pdf/SkDeflate.cpp b/src/pdf/SkDeflate.cpp index 0953ef27fb..2ca516c0e3 100644 --- a/src/pdf/SkDeflate.cpp +++ b/src/pdf/SkDeflate.cpp @@ -19,120 +19,15 @@ namespace { #include "zlib.h" #endif -// static -const size_t kBufferSize = 1024; - // Different zlib implementations use different T. // We've seen size_t and unsigned. template <typename T> void* skia_alloc_func(void*, T items, T size) { return sk_calloc_throw(SkToSizeT(items) * SkToSizeT(size)); } -static void skia_free_func(void*, void* address) { sk_free(address); } - -bool doFlate(bool compress, SkStream* src, SkWStream* dst) { - uint8_t inputBuffer[kBufferSize]; - uint8_t outputBuffer[kBufferSize]; - z_stream flateData; - flateData.zalloc = &skia_alloc_func; - flateData.zfree = &skia_free_func; - flateData.opaque = NULL; - flateData.next_in = NULL; - flateData.avail_in = 0; - flateData.next_out = outputBuffer; - flateData.avail_out = kBufferSize; - int rc; - if (compress) - rc = deflateInit(&flateData, Z_DEFAULT_COMPRESSION); - else - rc = inflateInit(&flateData); - if (rc != Z_OK) - return false; - - uint8_t* input = (uint8_t*)src->getMemoryBase(); - size_t inputLength = src->getLength(); - if (input == NULL || inputLength == 0) { - input = NULL; - flateData.next_in = inputBuffer; - flateData.avail_in = 0; - } else { - flateData.next_in = input; - flateData.avail_in = SkToUInt(inputLength); - } - - rc = Z_OK; - while (true) { - if (flateData.avail_out < kBufferSize) { - if (!dst->write(outputBuffer, kBufferSize - flateData.avail_out)) { - rc = Z_BUF_ERROR; - break; - } - flateData.next_out = outputBuffer; - flateData.avail_out = kBufferSize; - } - if (rc != Z_OK) - break; - if (flateData.avail_in == 0) { - if (input != NULL) - break; - size_t read = src->read(&inputBuffer, kBufferSize); - if (read == 0) - break; - flateData.next_in = inputBuffer; - flateData.avail_in = SkToUInt(read); - } - if (compress) - rc = deflate(&flateData, Z_NO_FLUSH); - else - rc = inflate(&flateData, Z_NO_FLUSH); - } - while (rc == Z_OK) { - if (compress) - rc = deflate(&flateData, Z_FINISH); - else - rc = inflate(&flateData, Z_FINISH); - if (flateData.avail_out < kBufferSize) { - if (!dst->write(outputBuffer, kBufferSize - flateData.avail_out)) - return false; - flateData.next_out = outputBuffer; - flateData.avail_out = kBufferSize; - } - } - - if (compress) - deflateEnd(&flateData); - else - inflateEnd(&flateData); - if (rc == Z_STREAM_END) - return true; - return false; -} - -} - -// static -bool SkFlate::Deflate(SkStream* src, SkWStream* dst) { - return doFlate(true, src, dst); -} - -bool SkFlate::Deflate(const void* ptr, size_t len, SkWStream* dst) { - SkMemoryStream stream(ptr, len); - return doFlate(true, &stream, dst); -} - -bool SkFlate::Deflate(const SkData* data, SkWStream* dst) { - if (data) { - SkMemoryStream stream(data->data(), data->size()); - return doFlate(true, &stream, dst); - } - return false; -} - -// static -bool SkFlate::Inflate(SkStream* src, SkWStream* dst) { - return doFlate(false, src, dst); -} +void skia_free_func(void*, void* address) { sk_free(address); } +} // namespace #define SKDEFLATEWSTREAM_INPUT_BUFFER_SIZE 4096 #define SKDEFLATEWSTREAM_OUTPUT_BUFFER_SIZE 4224 // 4096 + 128, usually big diff --git a/src/pdf/SkDeflate.h b/src/pdf/SkDeflate.h index 0104c4514d..d7323789a8 100644 --- a/src/pdf/SkDeflate.h +++ b/src/pdf/SkDeflate.h @@ -13,36 +13,6 @@ #include "SkTypes.h" #include "SkStream.h" -class SkData; - -/** \class SkFlate - A class to provide access to the flate compression algorithm. -*/ -class SkFlate { -public: - /** - * Use the flate compression algorithm to compress the data in src, - * putting the result into dst. Returns false if an error occurs. - */ - static bool Deflate(SkStream* src, SkWStream* dst); - - /** - * Use the flate compression algorithm to compress the data in src, - * putting the result into dst. Returns false if an error occurs. - */ - static bool Deflate(const void* src, size_t len, SkWStream* dst); - - /** - * Use the flate compression algorithm to compress the data, - * putting the result into dst. Returns false if an error occurs. - */ - static bool Deflate(const SkData*, SkWStream* dst); - - /** Use the flate compression algorithm to decompress the data in src, - putting the result into dst. Returns false if an error occurs. - */ - static bool Inflate(SkStream* src, SkWStream* dst); -}; /** * Wrap a stream in this class to compress the information written to diff --git a/src/pdf/SkPDFStream.cpp b/src/pdf/SkPDFStream.cpp index e709357e60..d21205cd38 100644 --- a/src/pdf/SkPDFStream.cpp +++ b/src/pdf/SkPDFStream.cpp @@ -29,9 +29,9 @@ void SkPDFStream::emitObject(SkWStream* stream, if (fState == kUnused_State) { fState = kNoCompression_State; SkDynamicMemoryWStream compressedData; - - SkAssertResult( - SkFlate::Deflate(fDataStream.get(), &compressedData)); + SkDeflateWStream deflateWStream(&compressedData); + SkAssertResult(SkStreamCopy(&deflateWStream, fDataStream.get())); + deflateWStream.finalize(); SkAssertResult(fDataStream->rewind()); if (compressedData.getOffset() < this->dataSize()) { SkAutoTDelete<SkStream> compressed( |