diff options
author | Kelvin Zhang <zhangkelvin@google.com> | 2021-11-30 10:45:32 -0800 |
---|---|---|
committer | Kelvin Zhang <zhangkelvin@google.com> | 2021-11-30 10:45:32 -0800 |
commit | 98e74d4125576e27c2bda80a7f79d584d16a9436 (patch) | |
tree | 1db6c794bfc6aea95743569b5e21b303948354eb | |
parent | 44c83ceeac916e8db73b3b12d52b63df9cb63c0d (diff) | |
download | puffin-98e74d4125576e27c2bda80a7f79d584d16a9436.tar.gz |
Allow user to specify quality when using brotli utils
Test: th
Change-Id: I7871399df17d4eb0e37626018214c9f23b3334a1
-rw-r--r-- | src/brotli_util.cc | 12 | ||||
-rw-r--r-- | src/include/puffin/brotli_util.h | 7 | ||||
-rw-r--r-- | src/include/puffin/common.h | 11 |
3 files changed, 26 insertions, 4 deletions
diff --git a/src/brotli_util.cc b/src/brotli_util.cc index e36e98d..587a844 100644 --- a/src/brotli_util.cc +++ b/src/brotli_util.cc @@ -20,14 +20,15 @@ constexpr auto kDefaultParamLgwin = 20; bool BrotliEncode(const uint8_t* input, size_t input_size, - UniqueStreamPtr output_stream) { + UniqueStreamPtr output_stream, + int quality) { std::unique_ptr<BrotliEncoderState, decltype(&BrotliEncoderDestroyInstance)> encoder(BrotliEncoderCreateInstance(nullptr, nullptr, nullptr), BrotliEncoderDestroyInstance); TEST_AND_RETURN_FALSE(encoder != nullptr); BrotliEncoderSetParameter(encoder.get(), BROTLI_PARAM_QUALITY, - kDefaultParamQuality); + quality); BrotliEncoderSetParameter(encoder.get(), BROTLI_PARAM_LGWIN, kDefaultParamLgwin); @@ -57,6 +58,13 @@ bool BrotliEncode(const uint8_t* input, bool BrotliEncode(const uint8_t* input, size_t input_size, + UniqueStreamPtr output_stream) { + return BrotliEncode(input, input_size, std::move(output_stream), + kDefaultParamQuality); +} + +bool BrotliEncode(const uint8_t* input, + size_t input_size, std::vector<uint8_t>* output) { TEST_AND_RETURN_FALSE(output != nullptr); return BrotliEncode(input, input_size, MemoryStream::CreateForWrite(output)); diff --git a/src/include/puffin/brotli_util.h b/src/include/puffin/brotli_util.h index 07240c3..cee84ba 100644 --- a/src/include/puffin/brotli_util.h +++ b/src/include/puffin/brotli_util.h @@ -23,6 +23,13 @@ bool BrotliEncode(const uint8_t* input, size_t input_size, std::vector<uint8_t>* output); +// Similar to the above, but quality controls how well the compression is +// |quality| should be between 0 and 11 +bool BrotliEncode(const uint8_t* input, + size_t input_size, + UniqueStreamPtr output_stream, + int quality); + // Decompress |input_size| bytes of data with brotli, and write the result to // |output_stream|. bool BrotliDecode(const uint8_t* input, diff --git a/src/include/puffin/common.h b/src/include/puffin/common.h index 07b8589..61e9eb2 100644 --- a/src/include/puffin/common.h +++ b/src/include/puffin/common.h @@ -31,13 +31,20 @@ using Buffer = std::vector<uint8_t>; // defined an extra class so the users of puffin do not have to include // puffin.pb.h and deal with its use. struct ByteExtent { - ByteExtent(uint64_t offset, uint64_t length) + constexpr ByteExtent(uint64_t offset, uint64_t length) : offset(offset), length(length) {} - bool operator==(const ByteExtent& other) const { + constexpr bool operator==(const ByteExtent& other) const { return this->length == other.length && this->offset == other.offset; } + constexpr bool operator<(const ByteExtent& other) const { + if (offset != other.offset) { + return offset < other.offset; + } + return length < other.length; + } + uint64_t offset; uint64_t length; }; |