zopfli-bin 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.document +5 -0
- data/.gitmodules +3 -0
- data/.testguardrc +1 -0
- data/Gemfile +17 -0
- data/Gemfile.lock +111 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +19 -0
- data/Rakefile +39 -0
- data/VERSION +1 -0
- data/ext/Makefile +8 -0
- data/ext/extconf.rb +4 -0
- data/lib/zopfli-bin.rb +5 -0
- data/lib/zopfli/bin.rb +34 -0
- data/test/helper.rb +19 -0
- data/test/test_zopfli-bin.rb +33 -0
- data/vendor/zopfli/CONTRIBUTORS +7 -0
- data/vendor/zopfli/COPYING +201 -0
- data/vendor/zopfli/Makefile +37 -0
- data/vendor/zopfli/README +32 -0
- data/vendor/zopfli/README.zopflipng +35 -0
- data/vendor/zopfli/src/zopfli/blocksplitter.c +342 -0
- data/vendor/zopfli/src/zopfli/blocksplitter.h +77 -0
- data/vendor/zopfli/src/zopfli/cache.c +119 -0
- data/vendor/zopfli/src/zopfli/cache.h +66 -0
- data/vendor/zopfli/src/zopfli/deflate.c +866 -0
- data/vendor/zopfli/src/zopfli/deflate.h +86 -0
- data/vendor/zopfli/src/zopfli/gzip_container.c +117 -0
- data/vendor/zopfli/src/zopfli/gzip_container.h +50 -0
- data/vendor/zopfli/src/zopfli/hash.c +135 -0
- data/vendor/zopfli/src/zopfli/hash.h +70 -0
- data/vendor/zopfli/src/zopfli/katajainen.c +251 -0
- data/vendor/zopfli/src/zopfli/katajainen.h +42 -0
- data/vendor/zopfli/src/zopfli/lz77.c +482 -0
- data/vendor/zopfli/src/zopfli/lz77.h +129 -0
- data/vendor/zopfli/src/zopfli/squeeze.c +546 -0
- data/vendor/zopfli/src/zopfli/squeeze.h +60 -0
- data/vendor/zopfli/src/zopfli/tree.c +101 -0
- data/vendor/zopfli/src/zopfli/tree.h +51 -0
- data/vendor/zopfli/src/zopfli/util.c +213 -0
- data/vendor/zopfli/src/zopfli/util.h +175 -0
- data/vendor/zopfli/src/zopfli/zlib_container.c +79 -0
- data/vendor/zopfli/src/zopfli/zlib_container.h +50 -0
- data/vendor/zopfli/src/zopfli/zopfli.h +97 -0
- data/vendor/zopfli/src/zopfli/zopfli_bin.c +203 -0
- data/vendor/zopfli/src/zopfli/zopfli_lib.c +42 -0
- data/vendor/zopfli/src/zopflipng/lodepng/lodepng.cpp +6260 -0
- data/vendor/zopfli/src/zopflipng/lodepng/lodepng.h +1716 -0
- data/vendor/zopfli/src/zopflipng/lodepng/lodepng_util.cpp +656 -0
- data/vendor/zopfli/src/zopflipng/lodepng/lodepng_util.h +151 -0
- data/vendor/zopfli/src/zopflipng/zopflipng_bin.cc +407 -0
- data/vendor/zopfli/src/zopflipng/zopflipng_lib.cc +425 -0
- data/vendor/zopfli/src/zopflipng/zopflipng_lib.h +79 -0
- data/zopfli-bin.gemspec +119 -0
- metadata +225 -0
@@ -0,0 +1,60 @@
|
|
1
|
+
/*
|
2
|
+
Copyright 2011 Google Inc. All Rights Reserved.
|
3
|
+
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
you may not use this file except in compliance with the License.
|
6
|
+
You may obtain a copy of the License at
|
7
|
+
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
See the License for the specific language governing permissions and
|
14
|
+
limitations under the License.
|
15
|
+
|
16
|
+
Author: lode.vandevenne@gmail.com (Lode Vandevenne)
|
17
|
+
Author: jyrki.alakuijala@gmail.com (Jyrki Alakuijala)
|
18
|
+
*/
|
19
|
+
|
20
|
+
/*
|
21
|
+
The squeeze functions do enhanced LZ77 compression by optimal parsing with a
|
22
|
+
cost model, rather than greedily choosing the longest length or using a single
|
23
|
+
step of lazy matching like regular implementations.
|
24
|
+
|
25
|
+
Since the cost model is based on the Huffman tree that can only be calculated
|
26
|
+
after the LZ77 data is generated, there is a chicken and egg problem, and
|
27
|
+
multiple runs are done with updated cost models to converge to a better
|
28
|
+
solution.
|
29
|
+
*/
|
30
|
+
|
31
|
+
#ifndef ZOPFLI_SQUEEZE_H_
|
32
|
+
#define ZOPFLI_SQUEEZE_H_
|
33
|
+
|
34
|
+
#include "lz77.h"
|
35
|
+
|
36
|
+
/*
|
37
|
+
Calculates lit/len and dist pairs for given data.
|
38
|
+
If instart is larger than 0, it uses values before instart as starting
|
39
|
+
dictionary.
|
40
|
+
*/
|
41
|
+
void ZopfliLZ77Optimal(ZopfliBlockState *s,
|
42
|
+
const unsigned char* in, size_t instart, size_t inend,
|
43
|
+
ZopfliLZ77Store* store);
|
44
|
+
|
45
|
+
/*
|
46
|
+
Does the same as ZopfliLZ77Optimal, but optimized for the fixed tree of the
|
47
|
+
deflate standard.
|
48
|
+
The fixed tree never gives the best compression. But this gives the best
|
49
|
+
possible LZ77 encoding possible with the fixed tree.
|
50
|
+
This does not create or output any fixed tree, only LZ77 data optimized for
|
51
|
+
using with a fixed tree.
|
52
|
+
If instart is larger than 0, it uses values before instart as starting
|
53
|
+
dictionary.
|
54
|
+
*/
|
55
|
+
void ZopfliLZ77OptimalFixed(ZopfliBlockState *s,
|
56
|
+
const unsigned char* in,
|
57
|
+
size_t instart, size_t inend,
|
58
|
+
ZopfliLZ77Store* store);
|
59
|
+
|
60
|
+
#endif /* ZOPFLI_SQUEEZE_H_ */
|
@@ -0,0 +1,101 @@
|
|
1
|
+
/*
|
2
|
+
Copyright 2011 Google Inc. All Rights Reserved.
|
3
|
+
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
you may not use this file except in compliance with the License.
|
6
|
+
You may obtain a copy of the License at
|
7
|
+
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
See the License for the specific language governing permissions and
|
14
|
+
limitations under the License.
|
15
|
+
|
16
|
+
Author: lode.vandevenne@gmail.com (Lode Vandevenne)
|
17
|
+
Author: jyrki.alakuijala@gmail.com (Jyrki Alakuijala)
|
18
|
+
*/
|
19
|
+
|
20
|
+
#include "tree.h"
|
21
|
+
|
22
|
+
#include <assert.h>
|
23
|
+
#include <math.h>
|
24
|
+
#include <stdio.h>
|
25
|
+
#include <stdlib.h>
|
26
|
+
|
27
|
+
#include "katajainen.h"
|
28
|
+
#include "util.h"
|
29
|
+
|
30
|
+
void ZopfliLengthsToSymbols(const unsigned* lengths, size_t n, unsigned maxbits,
|
31
|
+
unsigned* symbols) {
|
32
|
+
size_t* bl_count = (size_t*)malloc(sizeof(size_t) * (maxbits + 1));
|
33
|
+
size_t* next_code = (size_t*)malloc(sizeof(size_t) * (maxbits + 1));
|
34
|
+
unsigned bits, i;
|
35
|
+
unsigned code;
|
36
|
+
|
37
|
+
for (i = 0; i < n; i++) {
|
38
|
+
symbols[i] = 0;
|
39
|
+
}
|
40
|
+
|
41
|
+
/* 1) Count the number of codes for each code length. Let bl_count[N] be the
|
42
|
+
number of codes of length N, N >= 1. */
|
43
|
+
for (bits = 0; bits <= maxbits; bits++) {
|
44
|
+
bl_count[bits] = 0;
|
45
|
+
}
|
46
|
+
for (i = 0; i < n; i++) {
|
47
|
+
assert(lengths[i] <= maxbits);
|
48
|
+
bl_count[lengths[i]]++;
|
49
|
+
}
|
50
|
+
/* 2) Find the numerical value of the smallest code for each code length. */
|
51
|
+
code = 0;
|
52
|
+
bl_count[0] = 0;
|
53
|
+
for (bits = 1; bits <= maxbits; bits++) {
|
54
|
+
code = (code + bl_count[bits-1]) << 1;
|
55
|
+
next_code[bits] = code;
|
56
|
+
}
|
57
|
+
/* 3) Assign numerical values to all codes, using consecutive values for all
|
58
|
+
codes of the same length with the base values determined at step 2. */
|
59
|
+
for (i = 0; i < n; i++) {
|
60
|
+
unsigned len = lengths[i];
|
61
|
+
if (len != 0) {
|
62
|
+
symbols[i] = next_code[len];
|
63
|
+
next_code[len]++;
|
64
|
+
}
|
65
|
+
}
|
66
|
+
|
67
|
+
free(bl_count);
|
68
|
+
free(next_code);
|
69
|
+
}
|
70
|
+
|
71
|
+
void ZopfliCalculateEntropy(const size_t* count, size_t n, double* bitlengths) {
|
72
|
+
static const double kInvLog2 = 1.4426950408889; /* 1.0 / log(2.0) */
|
73
|
+
unsigned sum = 0;
|
74
|
+
unsigned i;
|
75
|
+
double log2sum;
|
76
|
+
for (i = 0; i < n; ++i) {
|
77
|
+
sum += count[i];
|
78
|
+
}
|
79
|
+
log2sum = (sum == 0 ? log(n) : log(sum)) * kInvLog2;
|
80
|
+
for (i = 0; i < n; ++i) {
|
81
|
+
/* When the count of the symbol is 0, but its cost is requested anyway, it
|
82
|
+
means the symbol will appear at least once anyway, so give it the cost as if
|
83
|
+
its count is 1.*/
|
84
|
+
if (count[i] == 0) bitlengths[i] = log2sum;
|
85
|
+
else bitlengths[i] = log2sum - log(count[i]) * kInvLog2;
|
86
|
+
/* Depending on compiler and architecture, the above subtraction of two
|
87
|
+
floating point numbers may give a negative result very close to zero
|
88
|
+
instead of zero (e.g. -5.973954e-17 with gcc 4.1.2 on Ubuntu 11.4). Clamp
|
89
|
+
it to zero. These floating point imprecisions do not affect the cost model
|
90
|
+
significantly so this is ok. */
|
91
|
+
if (bitlengths[i] < 0 && bitlengths[i] > -1e-5) bitlengths[i] = 0;
|
92
|
+
assert(bitlengths[i] >= 0);
|
93
|
+
}
|
94
|
+
}
|
95
|
+
|
96
|
+
void ZopfliCalculateBitLengths(const size_t* count, size_t n, int maxbits,
|
97
|
+
unsigned* bitlengths) {
|
98
|
+
int error = ZopfliLengthLimitedCodeLengths(count, n, maxbits, bitlengths);
|
99
|
+
(void) error;
|
100
|
+
assert(!error);
|
101
|
+
}
|
@@ -0,0 +1,51 @@
|
|
1
|
+
/*
|
2
|
+
Copyright 2011 Google Inc. All Rights Reserved.
|
3
|
+
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
you may not use this file except in compliance with the License.
|
6
|
+
You may obtain a copy of the License at
|
7
|
+
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
See the License for the specific language governing permissions and
|
14
|
+
limitations under the License.
|
15
|
+
|
16
|
+
Author: lode.vandevenne@gmail.com (Lode Vandevenne)
|
17
|
+
Author: jyrki.alakuijala@gmail.com (Jyrki Alakuijala)
|
18
|
+
*/
|
19
|
+
|
20
|
+
/*
|
21
|
+
Utilities for creating and using Huffman trees.
|
22
|
+
*/
|
23
|
+
|
24
|
+
#ifndef ZOPFLI_TREE_H_
|
25
|
+
#define ZOPFLI_TREE_H_
|
26
|
+
|
27
|
+
#include <string.h>
|
28
|
+
|
29
|
+
/*
|
30
|
+
Calculates the bitlengths for the Huffman tree, based on the counts of each
|
31
|
+
symbol.
|
32
|
+
*/
|
33
|
+
void ZopfliCalculateBitLengths(const size_t* count, size_t n, int maxbits,
|
34
|
+
unsigned *bitlengths);
|
35
|
+
|
36
|
+
/*
|
37
|
+
Converts a series of Huffman tree bitlengths, to the bit values of the symbols.
|
38
|
+
*/
|
39
|
+
void ZopfliLengthsToSymbols(const unsigned* lengths, size_t n, unsigned maxbits,
|
40
|
+
unsigned* symbols);
|
41
|
+
|
42
|
+
/*
|
43
|
+
Calculates the entropy of each symbol, based on the counts of each symbol. The
|
44
|
+
result is similar to the result of ZopfliCalculateBitLengths, but with the
|
45
|
+
actual theoritical bit lengths according to the entropy. Since the resulting
|
46
|
+
values are fractional, they cannot be used to encode the tree specified by
|
47
|
+
DEFLATE.
|
48
|
+
*/
|
49
|
+
void ZopfliCalculateEntropy(const size_t* count, size_t n, double* bitlengths);
|
50
|
+
|
51
|
+
#endif /* ZOPFLI_TREE_H_ */
|
@@ -0,0 +1,213 @@
|
|
1
|
+
/*
|
2
|
+
Copyright 2011 Google Inc. All Rights Reserved.
|
3
|
+
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
you may not use this file except in compliance with the License.
|
6
|
+
You may obtain a copy of the License at
|
7
|
+
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
See the License for the specific language governing permissions and
|
14
|
+
limitations under the License.
|
15
|
+
|
16
|
+
Author: lode.vandevenne@gmail.com (Lode Vandevenne)
|
17
|
+
Author: jyrki.alakuijala@gmail.com (Jyrki Alakuijala)
|
18
|
+
*/
|
19
|
+
|
20
|
+
#include "util.h"
|
21
|
+
|
22
|
+
#include "zopfli.h"
|
23
|
+
|
24
|
+
#include <assert.h>
|
25
|
+
#include <stdio.h>
|
26
|
+
#include <stdlib.h>
|
27
|
+
|
28
|
+
int ZopfliGetDistExtraBits(int dist) {
|
29
|
+
#ifdef __GNUC__
|
30
|
+
if (dist < 5) return 0;
|
31
|
+
return (31 ^ __builtin_clz(dist - 1)) - 1; /* log2(dist - 1) - 1 */
|
32
|
+
#else
|
33
|
+
if (dist < 5) return 0;
|
34
|
+
else if (dist < 9) return 1;
|
35
|
+
else if (dist < 17) return 2;
|
36
|
+
else if (dist < 33) return 3;
|
37
|
+
else if (dist < 65) return 4;
|
38
|
+
else if (dist < 129) return 5;
|
39
|
+
else if (dist < 257) return 6;
|
40
|
+
else if (dist < 513) return 7;
|
41
|
+
else if (dist < 1025) return 8;
|
42
|
+
else if (dist < 2049) return 9;
|
43
|
+
else if (dist < 4097) return 10;
|
44
|
+
else if (dist < 8193) return 11;
|
45
|
+
else if (dist < 16385) return 12;
|
46
|
+
else return 13;
|
47
|
+
#endif
|
48
|
+
}
|
49
|
+
|
50
|
+
int ZopfliGetDistExtraBitsValue(int dist) {
|
51
|
+
#ifdef __GNUC__
|
52
|
+
if (dist < 5) {
|
53
|
+
return 0;
|
54
|
+
} else {
|
55
|
+
int l = 31 ^ __builtin_clz(dist - 1); /* log2(dist - 1) */
|
56
|
+
return (dist - (1 + (1 << l))) & ((1 << (l - 1)) - 1);
|
57
|
+
}
|
58
|
+
#else
|
59
|
+
if (dist < 5) return 0;
|
60
|
+
else if (dist < 9) return (dist - 5) & 1;
|
61
|
+
else if (dist < 17) return (dist - 9) & 3;
|
62
|
+
else if (dist < 33) return (dist - 17) & 7;
|
63
|
+
else if (dist < 65) return (dist - 33) & 15;
|
64
|
+
else if (dist < 129) return (dist - 65) & 31;
|
65
|
+
else if (dist < 257) return (dist - 129) & 63;
|
66
|
+
else if (dist < 513) return (dist - 257) & 127;
|
67
|
+
else if (dist < 1025) return (dist - 513) & 255;
|
68
|
+
else if (dist < 2049) return (dist - 1025) & 511;
|
69
|
+
else if (dist < 4097) return (dist - 2049) & 1023;
|
70
|
+
else if (dist < 8193) return (dist - 4097) & 2047;
|
71
|
+
else if (dist < 16385) return (dist - 8193) & 4095;
|
72
|
+
else return (dist - 16385) & 8191;
|
73
|
+
#endif
|
74
|
+
}
|
75
|
+
|
76
|
+
int ZopfliGetDistSymbol(int dist) {
|
77
|
+
#ifdef __GNUC__
|
78
|
+
if (dist < 5) {
|
79
|
+
return dist - 1;
|
80
|
+
} else {
|
81
|
+
int l = (31 ^ __builtin_clz(dist - 1)); /* log2(dist - 1) */
|
82
|
+
int r = ((dist - 1) >> (l - 1)) & 1;
|
83
|
+
return l * 2 + r;
|
84
|
+
}
|
85
|
+
#else
|
86
|
+
if (dist < 193) {
|
87
|
+
if (dist < 13) { /* dist 0..13. */
|
88
|
+
if (dist < 5) return dist - 1;
|
89
|
+
else if (dist < 7) return 4;
|
90
|
+
else if (dist < 9) return 5;
|
91
|
+
else return 6;
|
92
|
+
} else { /* dist 13..193. */
|
93
|
+
if (dist < 17) return 7;
|
94
|
+
else if (dist < 25) return 8;
|
95
|
+
else if (dist < 33) return 9;
|
96
|
+
else if (dist < 49) return 10;
|
97
|
+
else if (dist < 65) return 11;
|
98
|
+
else if (dist < 97) return 12;
|
99
|
+
else if (dist < 129) return 13;
|
100
|
+
else return 14;
|
101
|
+
}
|
102
|
+
} else {
|
103
|
+
if (dist < 2049) { /* dist 193..2049. */
|
104
|
+
if (dist < 257) return 15;
|
105
|
+
else if (dist < 385) return 16;
|
106
|
+
else if (dist < 513) return 17;
|
107
|
+
else if (dist < 769) return 18;
|
108
|
+
else if (dist < 1025) return 19;
|
109
|
+
else if (dist < 1537) return 20;
|
110
|
+
else return 21;
|
111
|
+
} else { /* dist 2049..32768. */
|
112
|
+
if (dist < 3073) return 22;
|
113
|
+
else if (dist < 4097) return 23;
|
114
|
+
else if (dist < 6145) return 24;
|
115
|
+
else if (dist < 8193) return 25;
|
116
|
+
else if (dist < 12289) return 26;
|
117
|
+
else if (dist < 16385) return 27;
|
118
|
+
else if (dist < 24577) return 28;
|
119
|
+
else return 29;
|
120
|
+
}
|
121
|
+
}
|
122
|
+
#endif
|
123
|
+
}
|
124
|
+
|
125
|
+
int ZopfliGetLengthExtraBits(int l) {
|
126
|
+
static const int table[259] = {
|
127
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
|
128
|
+
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
|
129
|
+
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
|
130
|
+
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
|
131
|
+
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
|
132
|
+
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
|
133
|
+
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
|
134
|
+
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
|
135
|
+
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
|
136
|
+
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
|
137
|
+
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
|
138
|
+
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
|
139
|
+
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
|
140
|
+
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
|
141
|
+
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
|
142
|
+
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0
|
143
|
+
};
|
144
|
+
return table[l];
|
145
|
+
}
|
146
|
+
|
147
|
+
int ZopfliGetLengthExtraBitsValue(int l) {
|
148
|
+
static const int table[259] = {
|
149
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 2, 3, 0,
|
150
|
+
1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5,
|
151
|
+
6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6,
|
152
|
+
7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
|
153
|
+
13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2,
|
154
|
+
3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
155
|
+
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28,
|
156
|
+
29, 30, 31, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
|
157
|
+
18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 0, 1, 2, 3, 4, 5, 6,
|
158
|
+
7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
|
159
|
+
27, 28, 29, 30, 31, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
|
160
|
+
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 0
|
161
|
+
};
|
162
|
+
return table[l];
|
163
|
+
}
|
164
|
+
|
165
|
+
/*
|
166
|
+
Returns symbol in range [257-285] (inclusive).
|
167
|
+
*/
|
168
|
+
int ZopfliGetLengthSymbol(int l) {
|
169
|
+
static const int table[259] = {
|
170
|
+
0, 0, 0, 257, 258, 259, 260, 261, 262, 263, 264,
|
171
|
+
265, 265, 266, 266, 267, 267, 268, 268,
|
172
|
+
269, 269, 269, 269, 270, 270, 270, 270,
|
173
|
+
271, 271, 271, 271, 272, 272, 272, 272,
|
174
|
+
273, 273, 273, 273, 273, 273, 273, 273,
|
175
|
+
274, 274, 274, 274, 274, 274, 274, 274,
|
176
|
+
275, 275, 275, 275, 275, 275, 275, 275,
|
177
|
+
276, 276, 276, 276, 276, 276, 276, 276,
|
178
|
+
277, 277, 277, 277, 277, 277, 277, 277,
|
179
|
+
277, 277, 277, 277, 277, 277, 277, 277,
|
180
|
+
278, 278, 278, 278, 278, 278, 278, 278,
|
181
|
+
278, 278, 278, 278, 278, 278, 278, 278,
|
182
|
+
279, 279, 279, 279, 279, 279, 279, 279,
|
183
|
+
279, 279, 279, 279, 279, 279, 279, 279,
|
184
|
+
280, 280, 280, 280, 280, 280, 280, 280,
|
185
|
+
280, 280, 280, 280, 280, 280, 280, 280,
|
186
|
+
281, 281, 281, 281, 281, 281, 281, 281,
|
187
|
+
281, 281, 281, 281, 281, 281, 281, 281,
|
188
|
+
281, 281, 281, 281, 281, 281, 281, 281,
|
189
|
+
281, 281, 281, 281, 281, 281, 281, 281,
|
190
|
+
282, 282, 282, 282, 282, 282, 282, 282,
|
191
|
+
282, 282, 282, 282, 282, 282, 282, 282,
|
192
|
+
282, 282, 282, 282, 282, 282, 282, 282,
|
193
|
+
282, 282, 282, 282, 282, 282, 282, 282,
|
194
|
+
283, 283, 283, 283, 283, 283, 283, 283,
|
195
|
+
283, 283, 283, 283, 283, 283, 283, 283,
|
196
|
+
283, 283, 283, 283, 283, 283, 283, 283,
|
197
|
+
283, 283, 283, 283, 283, 283, 283, 283,
|
198
|
+
284, 284, 284, 284, 284, 284, 284, 284,
|
199
|
+
284, 284, 284, 284, 284, 284, 284, 284,
|
200
|
+
284, 284, 284, 284, 284, 284, 284, 284,
|
201
|
+
284, 284, 284, 284, 284, 284, 284, 285
|
202
|
+
};
|
203
|
+
return table[l];
|
204
|
+
}
|
205
|
+
|
206
|
+
void ZopfliInitOptions(ZopfliOptions* options) {
|
207
|
+
options->verbose = 0;
|
208
|
+
options->verbose_more = 0;
|
209
|
+
options->numiterations = 15;
|
210
|
+
options->blocksplitting = 1;
|
211
|
+
options->blocksplittinglast = 0;
|
212
|
+
options->blocksplittingmax = 15;
|
213
|
+
}
|
@@ -0,0 +1,175 @@
|
|
1
|
+
/*
|
2
|
+
Copyright 2011 Google Inc. All Rights Reserved.
|
3
|
+
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
you may not use this file except in compliance with the License.
|
6
|
+
You may obtain a copy of the License at
|
7
|
+
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
See the License for the specific language governing permissions and
|
14
|
+
limitations under the License.
|
15
|
+
|
16
|
+
Author: lode.vandevenne@gmail.com (Lode Vandevenne)
|
17
|
+
Author: jyrki.alakuijala@gmail.com (Jyrki Alakuijala)
|
18
|
+
*/
|
19
|
+
|
20
|
+
/*
|
21
|
+
Several utilities, including: #defines to try different compression results,
|
22
|
+
basic deflate specification values and generic program options.
|
23
|
+
*/
|
24
|
+
|
25
|
+
#ifndef ZOPFLI_UTIL_H_
|
26
|
+
#define ZOPFLI_UTIL_H_
|
27
|
+
|
28
|
+
#include <string.h>
|
29
|
+
#include <stdlib.h>
|
30
|
+
|
31
|
+
/* Minimum and maximum length that can be encoded in deflate. */
|
32
|
+
#define ZOPFLI_MAX_MATCH 258
|
33
|
+
#define ZOPFLI_MIN_MATCH 3
|
34
|
+
|
35
|
+
/*
|
36
|
+
The window size for deflate. Must be a power of two. This should be 32768, the
|
37
|
+
maximum possible by the deflate spec. Anything less hurts compression more than
|
38
|
+
speed.
|
39
|
+
*/
|
40
|
+
#define ZOPFLI_WINDOW_SIZE 32768
|
41
|
+
|
42
|
+
/*
|
43
|
+
The window mask used to wrap indices into the window. This is why the
|
44
|
+
window size must be a power of two.
|
45
|
+
*/
|
46
|
+
#define ZOPFLI_WINDOW_MASK (ZOPFLI_WINDOW_SIZE - 1)
|
47
|
+
|
48
|
+
/*
|
49
|
+
A block structure of huge, non-smart, blocks to divide the input into, to allow
|
50
|
+
operating on huge files without exceeding memory, such as the 1GB wiki9 corpus.
|
51
|
+
The whole compression algorithm, including the smarter block splitting, will
|
52
|
+
be executed independently on each huge block.
|
53
|
+
Dividing into huge blocks hurts compression, but not much relative to the size.
|
54
|
+
Set this to, for example, 20MB (20000000). Set it to 0 to disable master blocks.
|
55
|
+
*/
|
56
|
+
#define ZOPFLI_MASTER_BLOCK_SIZE 20000000
|
57
|
+
|
58
|
+
/*
|
59
|
+
Used to initialize costs for example
|
60
|
+
*/
|
61
|
+
#define ZOPFLI_LARGE_FLOAT 1e30
|
62
|
+
|
63
|
+
/*
|
64
|
+
For longest match cache. max 256. Uses huge amounts of memory but makes it
|
65
|
+
faster. Uses this many times three bytes per single byte of the input data.
|
66
|
+
This is so because longest match finding has to find the exact distance
|
67
|
+
that belongs to each length for the best lz77 strategy.
|
68
|
+
Good values: e.g. 5, 8.
|
69
|
+
*/
|
70
|
+
#define ZOPFLI_CACHE_LENGTH 8
|
71
|
+
|
72
|
+
/*
|
73
|
+
limit the max hash chain hits for this hash value. This has an effect only
|
74
|
+
on files where the hash value is the same very often. On these files, this
|
75
|
+
gives worse compression (the value should ideally be 32768, which is the
|
76
|
+
ZOPFLI_WINDOW_SIZE, while zlib uses 4096 even for best level), but makes it
|
77
|
+
faster on some specific files.
|
78
|
+
Good value: e.g. 8192.
|
79
|
+
*/
|
80
|
+
#define ZOPFLI_MAX_CHAIN_HITS 8192
|
81
|
+
|
82
|
+
/*
|
83
|
+
Whether to use the longest match cache for ZopfliFindLongestMatch. This cache
|
84
|
+
consumes a lot of memory but speeds it up. No effect on compression size.
|
85
|
+
*/
|
86
|
+
#define ZOPFLI_LONGEST_MATCH_CACHE
|
87
|
+
|
88
|
+
/*
|
89
|
+
Enable to remember amount of successive identical bytes in the hash chain for
|
90
|
+
finding longest match
|
91
|
+
required for ZOPFLI_HASH_SAME_HASH and ZOPFLI_SHORTCUT_LONG_REPETITIONS
|
92
|
+
This has no effect on the compression result, and enabling it increases speed.
|
93
|
+
*/
|
94
|
+
#define ZOPFLI_HASH_SAME
|
95
|
+
|
96
|
+
/*
|
97
|
+
Switch to a faster hash based on the info from ZOPFLI_HASH_SAME once the
|
98
|
+
best length so far is long enough. This is way faster for files with lots of
|
99
|
+
identical bytes, on which the compressor is otherwise too slow. Regular files
|
100
|
+
are unaffected or maybe a tiny bit slower.
|
101
|
+
This has no effect on the compression result, only on speed.
|
102
|
+
*/
|
103
|
+
#define ZOPFLI_HASH_SAME_HASH
|
104
|
+
|
105
|
+
/*
|
106
|
+
Enable this, to avoid slowness for files which are a repetition of the same
|
107
|
+
character more than a multiple of ZOPFLI_MAX_MATCH times. This should not affect
|
108
|
+
the compression result.
|
109
|
+
*/
|
110
|
+
#define ZOPFLI_SHORTCUT_LONG_REPETITIONS
|
111
|
+
|
112
|
+
/*
|
113
|
+
Whether to use lazy matching in the greedy LZ77 implementation. This gives a
|
114
|
+
better result of ZopfliLZ77Greedy, but the effect this has on the optimal LZ77
|
115
|
+
varies from file to file.
|
116
|
+
*/
|
117
|
+
#define ZOPFLI_LAZY_MATCHING
|
118
|
+
|
119
|
+
/*
|
120
|
+
Gets the symbol for the given length, cfr. the DEFLATE spec.
|
121
|
+
Returns the symbol in the range [257-285] (inclusive)
|
122
|
+
*/
|
123
|
+
int ZopfliGetLengthSymbol(int l);
|
124
|
+
|
125
|
+
/* Gets the amount of extra bits for the given length, cfr. the DEFLATE spec. */
|
126
|
+
int ZopfliGetLengthExtraBits(int l);
|
127
|
+
|
128
|
+
/* Gets value of the extra bits for the given length, cfr. the DEFLATE spec. */
|
129
|
+
int ZopfliGetLengthExtraBitsValue(int l);
|
130
|
+
|
131
|
+
/* Gets the symbol for the given dist, cfr. the DEFLATE spec. */
|
132
|
+
int ZopfliGetDistSymbol(int dist);
|
133
|
+
|
134
|
+
/* Gets the amount of extra bits for the given dist, cfr. the DEFLATE spec. */
|
135
|
+
int ZopfliGetDistExtraBits(int dist);
|
136
|
+
|
137
|
+
/* Gets value of the extra bits for the given dist, cfr. the DEFLATE spec. */
|
138
|
+
int ZopfliGetDistExtraBitsValue(int dist);
|
139
|
+
|
140
|
+
/*
|
141
|
+
Appends value to dynamically allocated memory, doubling its allocation size
|
142
|
+
whenever needed.
|
143
|
+
|
144
|
+
value: the value to append, type T
|
145
|
+
data: pointer to the dynamic array to append to, type T**
|
146
|
+
size: pointer to the size of the array to append to, type size_t*. This is the
|
147
|
+
size that you consider the array to be, not the internal allocation size.
|
148
|
+
Precondition: allocated size of data is at least a power of two greater than or
|
149
|
+
equal than *size.
|
150
|
+
*/
|
151
|
+
#ifdef __cplusplus /* C++ cannot assign void* from malloc to *data */
|
152
|
+
#define ZOPFLI_APPEND_DATA(/* T */ value, /* T** */ data, /* size_t* */ size) {\
|
153
|
+
if (!((*size) & ((*size) - 1))) {\
|
154
|
+
/*double alloc size if it's a power of two*/\
|
155
|
+
void** data_void = reinterpret_cast<void**>(data);\
|
156
|
+
*data_void = (*size) == 0 ? malloc(sizeof(**data))\
|
157
|
+
: realloc((*data), (*size) * 2 * sizeof(**data));\
|
158
|
+
}\
|
159
|
+
(*data)[(*size)] = (value);\
|
160
|
+
(*size)++;\
|
161
|
+
}
|
162
|
+
#else /* C gives problems with strict-aliasing rules for (void**) cast */
|
163
|
+
#define ZOPFLI_APPEND_DATA(/* T */ value, /* T** */ data, /* size_t* */ size) {\
|
164
|
+
if (!((*size) & ((*size) - 1))) {\
|
165
|
+
/*double alloc size if it's a power of two*/\
|
166
|
+
(*data) = (*size) == 0 ? malloc(sizeof(**data))\
|
167
|
+
: realloc((*data), (*size) * 2 * sizeof(**data));\
|
168
|
+
}\
|
169
|
+
(*data)[(*size)] = (value);\
|
170
|
+
(*size)++;\
|
171
|
+
}
|
172
|
+
#endif
|
173
|
+
|
174
|
+
|
175
|
+
#endif /* ZOPFLI_UTIL_H_ */
|