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_CORE_LIB_IO_ZLIB_COMPRESSION_OPTIONS_H_
17 #define TENSORFLOW_CORE_LIB_IO_ZLIB_COMPRESSION_OPTIONS_H_
18
19 #include "tensorflow/core/platform/types.h"
20
21 namespace tensorflow {
22 namespace io {
23
24 class ZlibCompressionOptions {
25 public:
26 ZlibCompressionOptions();
27
28 static ZlibCompressionOptions DEFAULT();
29 static ZlibCompressionOptions RAW();
30 static ZlibCompressionOptions GZIP();
31
32 // Defaults to Z_NO_FLUSH
33 int8 flush_mode;
34
35 // Size of the buffer used for caching the data read from source file.
36 int64 input_buffer_size = 256 << 10;
37
38 // Size of the sink buffer where the compressed/decompressed data produced by
39 // zlib is cached.
40 int64 output_buffer_size = 256 << 10;
41
42 // The window_bits parameter is the base two logarithm of the window size
43 // (the size of the history buffer). Larger values of buffer size result in
44 // better compression at the expense of memory usage.
45 //
46 // Accepted values:
47 //
48 // 8..15:
49 // Normal deflate with zlib header and checksum.
50 //
51 // -8..-15:
52 // Negative values can be used for raw deflate/inflate. In this case,
53 // -window_bits determines the window size. deflate() will then generate raw
54 // deflate data with no zlib header or trailer, and will not compute an
55 // adler32 check value. inflate() will then process raw deflate data, not
56 // looking for a zlib or gzip header, not generating a check value, and not
57 // looking for any check values for comparison at the end of the stream.
58 //
59 // 16 + [8..15]:
60 // window_bits can also be greater than 15 for optional gzip encoding. Add 16
61 // to window_bits to write a simple gzip header and trailer around the
62 // compressed data instead of a zlib wrapper. The gzip header will have no
63 // file name, no extra data, no comment, no modification time (set to zero),
64 // no header crc, and the operating system will be set to 255 (unknown). If a
65 // gzip stream is being written, strm->adler is a crc32 instead of an adler32.
66 //
67 // 0:
68 // window_bits can also be zero to request that inflate use the window size
69 // in the zlib header of the compressed stream.
70 //
71 // While inflating, window_bits must be greater than or equal to the
72 // window_bits value provided used while compressing. If a compressed stream
73 // with a larger window size is given as input, inflate() will return with the
74 // error code Z_DATA_ERROR instead of trying to allocate a larger window.
75 //
76 // Defaults to MAX_WBITS
77 int8 window_bits;
78
79 // From the zlib manual (http://www.zlib.net/manual.html):
80 // The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9:
81 // 1 gives best speed, 9 gives best compression, 0 gives no compression at all
82 // (the input data is simply copied a block at a time). Z_DEFAULT_COMPRESSION
83 // requests a default compromise between speed and compression (currently
84 // equivalent to level 6).
85 int8 compression_level;
86
87 // Only Z_DEFLATED is supported at this time.
88 int8 compression_method;
89
90 // From the zlib manual (http://www.zlib.net/manual.html):
91 // The mem_level parameter specifies how much memory should be allocated for
92 // the internal compression state. mem_level=1 uses minimum memory but is slow
93 // and reduces compression ratio; mem_level=9 uses maximum memory for optimal
94 // speed. The default value is 8.
95 int8 mem_level = 9;
96
97 // From the zlib manual (http://www.zlib.net/manual.html):
98 // The strategy parameter is used to tune the compression algorithm. Use the
99 // value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by
100 // a filter (or predictor), Z_HUFFMAN_ONLY to force Huffman encoding only
101 // (no string match), or Z_RLE to limit match distances to one
102 // (run-length encoding). Filtered data consists mostly of small values with
103 // a somewhat random distribution. In this case, the compression algorithm is
104 // tuned to compress them better. The effect of Z_FILTERED is to force more
105 // Huffman coding and less string matching; it is somewhat intermediate
106 // between Z_DEFAULT_STRATEGY and Z_HUFFMAN_ONLY. Z_RLE is designed to be
107 // almost as fast as Z_HUFFMAN_ONLY, but give better compression for
108 // PNG image data. The strategy parameter only affects the compression ratio
109 // but not the correctness of the compressed output even if it is not set
110 // appropriately. Z_FIXED prevents the use of dynamic Huffman codes, allowing
111 // for a simpler decoder for special applications.
112 int8 compression_strategy;
113
114 // When this is set to true and we are unable to find the header to correctly
115 // decompress a file, we return an error when `ReadNBytes` is called instead
116 // of CHECK-failing. Defaults to false (i.e. CHECK-failing).
117 //
118 // This option is ignored for `ZlibOutputBuffer`.
119 bool soft_fail_on_error = false; // NOLINT
120 };
121
DEFAULT()122 inline ZlibCompressionOptions ZlibCompressionOptions::DEFAULT() {
123 return ZlibCompressionOptions();
124 }
125
RAW()126 inline ZlibCompressionOptions ZlibCompressionOptions::RAW() {
127 ZlibCompressionOptions options = ZlibCompressionOptions();
128 options.window_bits = -options.window_bits;
129 return options;
130 }
131
GZIP()132 inline ZlibCompressionOptions ZlibCompressionOptions::GZIP() {
133 ZlibCompressionOptions options = ZlibCompressionOptions();
134 options.window_bits = options.window_bits + 16;
135 return options;
136 }
137
138 } // namespace io
139 } // namespace tensorflow
140
141 #endif // TENSORFLOW_CORE_LIB_IO_ZLIB_COMPRESSION_OPTIONS_H_
142