1 /* Copyright 2016 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 #ifndef TENSORFLOW_LIB_IO_ZLIB_COMPRESSION_OPTIONS_H_ 17 #define TENSORFLOW_LIB_IO_ZLIB_COMPRESSION_OPTIONS_H_ 18 19 #include <zlib.h> 20 21 #include "tensorflow/core/platform/types.h" 22 23 namespace tensorflow { 24 namespace io { 25 26 class ZlibCompressionOptions { 27 public: 28 static ZlibCompressionOptions DEFAULT(); 29 static ZlibCompressionOptions RAW(); 30 static ZlibCompressionOptions GZIP(); 31 32 int8 flush_mode = Z_NO_FLUSH; 33 34 // Size of the buffer used for caching the data read from source file. 35 int64 input_buffer_size = 256 << 10; 36 37 // Size of the sink buffer where the compressed/decompressed data produced by 38 // zlib is cached. 39 int64 output_buffer_size = 256 << 10; 40 41 // The window_bits parameter is the base two logarithm of the window size 42 // (the size of the history buffer). Larger values of buffer size result in 43 // better compression at the expense of memory usage. 44 // 45 // Accepted values: 46 // 47 // 8..15: 48 // Normal deflate with zlib header and checksum. 49 // 50 // -8..-15: 51 // Negative values can be used for raw deflate/inflate. In this case, 52 // -window_bits determines the window size. deflate() will then generate raw 53 // deflate data with no zlib header or trailer, and will not compute an 54 // adler32 check value. inflate() will then process raw deflate data, not 55 // looking for a zlib or gzip header, not generating a check value, and not 56 // looking for any check values for comparison at the end of the stream. 57 // 58 // 16 + [8..15]: 59 // window_bits can also be greater than 15 for optional gzip encoding. Add 16 60 // to window_bits to write a simple gzip header and trailer around the 61 // compressed data instead of a zlib wrapper. The gzip header will have no 62 // file name, no extra data, no comment, no modification time (set to zero), 63 // no header crc, and the operating system will be set to 255 (unknown). If a 64 // gzip stream is being written, strm->adler is a crc32 instead of an adler32. 65 // 66 // 0: 67 // window_bits can also be zero to request that inflate use the window size 68 // in the zlib header of the compressed stream. 69 // 70 // While inflating, window_bits must be greater than or equal to the 71 // window_bits value provided used while compressing. If a compressed stream 72 // with a larger window size is given as input, inflate() will return with the 73 // error code Z_DATA_ERROR instead of trying to allocate a larger window. 74 int8 window_bits = MAX_WBITS; 75 76 // From the zlib manual (http://www.zlib.net/manual.html): 77 // The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9: 78 // 1 gives best speed, 9 gives best compression, 0 gives no compression at all 79 // (the input data is simply copied a block at a time). Z_DEFAULT_COMPRESSION 80 // requests a default compromise between speed and compression (currently 81 // equivalent to level 6). 82 int8 compression_level = Z_DEFAULT_COMPRESSION; 83 84 // The only one supported at this time. 85 int8 compression_method = Z_DEFLATED; 86 87 // From the zlib manual (http://www.zlib.net/manual.html): 88 // The mem_level parameter specifies how much memory should be allocated for 89 // the internal compression state. mem_level=1 uses minimum memory but is slow 90 // and reduces compression ratio; mem_level=9 uses maximum memory for optimal 91 // speed. The default value is 8. 92 int8 mem_level = 9; 93 94 // From the zlib manual (http://www.zlib.net/manual.html): 95 // The strategy parameter is used to tune the compression algorithm. Use the 96 // value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by 97 // a filter (or predictor), Z_HUFFMAN_ONLY to force Huffman encoding only 98 // (no string match), or Z_RLE to limit match distances to one 99 // (run-length encoding). Filtered data consists mostly of small values with 100 // a somewhat random distribution. In this case, the compression algorithm is 101 // tuned to compress them better. The effect of Z_FILTERED is to force more 102 // Huffman coding and less string matching; it is somewhat intermediate 103 // between Z_DEFAULT_STRATEGY and Z_HUFFMAN_ONLY. Z_RLE is designed to be 104 // almost as fast as Z_HUFFMAN_ONLY, but give better compression for 105 // PNG image data. The strategy parameter only affects the compression ratio 106 // but not the correctness of the compressed output even if it is not set 107 // appropriately. Z_FIXED prevents the use of dynamic Huffman codes, allowing 108 // for a simpler decoder for special applications. 109 int8 compression_strategy = Z_DEFAULT_STRATEGY; 110 }; 111 112 inline ZlibCompressionOptions ZlibCompressionOptions::DEFAULT() { 113 return ZlibCompressionOptions(); 114 } 115 116 inline ZlibCompressionOptions ZlibCompressionOptions::RAW() { 117 ZlibCompressionOptions options = ZlibCompressionOptions(); 118 options.window_bits = -options.window_bits; 119 return options; 120 } 121 122 inline ZlibCompressionOptions ZlibCompressionOptions::GZIP() { 123 ZlibCompressionOptions options = ZlibCompressionOptions(); 124 options.window_bits = options.window_bits + 16; 125 return options; 126 } 127 128 } // namespace io 129 } // namespace tensorflow 130 131 #endif // TENSORFLOW_LIB_IO_ZLIB_COMPRESSION_OPTIONS_H_ 132