Compare commits
768 commits
rust-every
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0466546981 | ||
|
|
4e80dd009a | ||
|
|
028294cc8c | ||
|
|
628e241e23 | ||
|
|
e167b04b85 | ||
|
|
fbae35922d | ||
|
|
55b9758575 | ||
|
|
010b76eada | ||
|
|
d239592b58 | ||
|
|
0cf8b782ff | ||
|
|
25e147f6a0 | ||
|
|
a75343800c | ||
|
|
7d52fd66d0 | ||
|
|
5bbd826ae3 | ||
|
|
e4a7d0fd99 | ||
|
|
a1a18ffd21 | ||
|
|
587fd03d0e | ||
|
|
ce52ab8748 | ||
|
|
494edba245 | ||
|
|
2e861b95db | ||
|
|
7834e4709e | ||
|
|
8284676f50 | ||
|
|
2a79fa4c6c | ||
|
|
00b9e33508 | ||
|
|
2585529089 | ||
|
|
6a99181fec | ||
|
|
ea9caac249 | ||
|
|
764d057c32 | ||
|
|
4a537c087d | ||
|
|
015d31e6a1 | ||
|
|
854510a864 | ||
|
|
8466e390b8 | ||
|
|
baaafee4e4 | ||
|
|
9169f99674 | ||
|
|
47d5628a68 | ||
|
|
d54e7c04ea | ||
|
|
ff168f5d2d | ||
|
|
a8edb2871c | ||
|
|
d48b147b3d | ||
|
|
155eeb3637 | ||
|
|
8665b2da8c | ||
|
|
e5e1568196 | ||
|
|
8754013cbc | ||
|
|
a0f4e751bf | ||
|
|
fa2c69071c | ||
|
|
f99ce277da | ||
|
|
0b68c08852 | ||
|
|
58eec39cc8 | ||
|
|
6a2379ce78 | ||
|
|
33cea5603e | ||
|
|
285b0aae10 | ||
|
|
e868af9106 | ||
|
|
1a9e0d79e5 | ||
|
|
cb132b1cc4 | ||
|
|
99eccf3f30 | ||
|
|
fb9be2f5d2 | ||
|
|
779336f60c | ||
|
|
e091767278 | ||
|
|
aebddf443a | ||
|
|
7cc91b1308 | ||
|
|
14532e8bf5 | ||
|
|
2a3aa83928 | ||
|
|
82cae58794 | ||
|
|
71290acc62 | ||
|
|
ff9beb00fe | ||
|
|
c60dbd12a5 | ||
|
|
54c49a1644 | ||
|
|
9d3e6e6f33 | ||
|
|
5ee3c2afa0 | ||
|
|
1c02ee9299 | ||
|
|
0ff97448c2 | ||
|
|
420ea940bc | ||
|
|
fe1f5a0889 | ||
|
|
fd5b7a4aa8 | ||
|
|
8631eb1c79 | ||
|
|
d57d880770 | ||
|
|
bf276f29b8 | ||
|
|
54a31a65a7 | ||
|
|
788997c437 | ||
|
|
a451711e96 | ||
|
|
ef1d87427d | ||
|
|
fa7d3d9067 | ||
|
|
8c6532ca1b | ||
|
|
c7d462f909 | ||
|
|
a44ba5f88b | ||
|
|
d168fffb12 | ||
|
|
748d5987a5 | ||
|
|
26b084c161 | ||
|
|
f7a66f2075 | ||
|
|
7650953faf | ||
|
|
6cf42fbefa | ||
|
|
1e3f4520ef | ||
|
|
d649beacac | ||
|
|
bc8c364568 | ||
|
|
30387b26d9 | ||
|
|
1188b9a91e | ||
|
|
5a4cfa9505 | ||
|
|
174f36ad8f | ||
|
|
e6f84264e5 | ||
|
|
8f298be7fb | ||
|
|
5f0a0d6938 | ||
|
|
2632be2034 | ||
|
|
bbde68ddce | ||
|
|
8a44cdbc84 | ||
|
|
14b8b0e93a | ||
|
|
cca23b5479 | ||
|
|
09c4851118 | ||
|
|
5fdaa12c96 | ||
|
|
fec6b2b9f3 | ||
|
|
60911977f6 | ||
|
|
6c7ab5d79f | ||
|
|
3a6b792ca1 | ||
|
|
ffbd927d82 | ||
|
|
a72bc945fc | ||
|
|
8270c6a034 | ||
|
|
910e779b09 | ||
|
|
8740f475d7 | ||
|
|
4df1464f39 | ||
|
|
8ebb5e6dfd | ||
|
|
3b7467a5da | ||
|
|
262082f546 | ||
|
|
f8c84c258d | ||
|
|
e8e8309c2d | ||
|
|
e638807d33 | ||
|
|
ca05d99be4 | ||
|
|
1efacace9f | ||
|
|
1936861b91 | ||
|
|
f602e84fa6 | ||
|
|
e367f23c01 | ||
|
|
cb97e69074 | ||
|
|
490a4691cb | ||
|
|
ef64dd0768 | ||
|
|
c81a863e69 | ||
|
|
e7f1c04488 | ||
|
|
e83c6725dc | ||
|
|
95eea28c82 | ||
|
|
ac943da875 | ||
|
|
59e0509a58 | ||
|
|
44619fd24d | ||
|
|
7a29741f18 | ||
|
|
0d60e8b7c6 | ||
|
|
0f24120c9a | ||
|
|
1bb7109804 | ||
|
|
e2d58c112b | ||
|
|
61deab369b | ||
|
|
d30da6e8a0 | ||
|
|
e8366882ad | ||
|
|
664b27c4ca | ||
|
|
0506418157 | ||
|
|
a8846b897d | ||
|
|
f7b837af69 | ||
|
|
f6e3614fe9 | ||
|
|
f70fe61e05 | ||
|
|
a2dedcfed2 | ||
|
|
3759ca85ec | ||
|
|
90ceef9796 | ||
|
|
e2c5b45f4b | ||
|
|
290bf9d11f | ||
|
|
e7dbe8f3b0 | ||
|
|
b5b1233901 | ||
|
|
ca1f423323 | ||
|
|
dd94bbef37 | ||
|
|
1ad90b389f | ||
|
|
c00cfc601d | ||
|
|
13d37a0ef3 | ||
|
|
8161b628dd | ||
|
|
ea5f0bb731 | ||
|
|
48c496af1e | ||
|
|
8086867055 | ||
|
|
a70e523241 | ||
|
|
375013f60f | ||
|
|
4302ae2367 | ||
|
|
5a66e77581 | ||
|
|
8a59f5aa1a | ||
|
|
268499b0db | ||
|
|
2d3555fe0f | ||
|
|
fd96c47e09 | ||
|
|
05c8c4e261 | ||
|
|
db7da039fa | ||
|
|
fca76a7afb | ||
|
|
1ddab42edb | ||
|
|
a30ee05ca8 | ||
|
|
eae53362b2 | ||
|
|
a3590df20c | ||
|
|
36de54318d | ||
|
|
d063119d9c | ||
|
|
f4e631bce7 | ||
|
|
2c3321b68a | ||
|
|
3e3c027cd0 | ||
|
|
e503a5c8f9 | ||
|
|
703ebb4813 | ||
|
|
a426fabe13 | ||
|
|
0608ab9cab | ||
|
|
98fd4de30a | ||
|
|
4cb8296b02 | ||
|
|
1b500da585 | ||
|
|
7818599de1 | ||
|
|
19ad37073b | ||
|
|
cd5f9f8cc0 | ||
|
|
87d5c2441b | ||
|
|
63efc76f3e | ||
|
|
f6c24409f1 | ||
|
|
97af04a539 | ||
|
|
0a988768ab | ||
|
|
a6152b06c4 | ||
|
|
e8f4ca88a3 | ||
|
|
e1db84fd97 | ||
|
|
93c3e7dfae | ||
|
|
0b3302316b | ||
|
|
b808807198 | ||
|
|
6763eea1e3 | ||
|
|
904beeec6b | ||
|
|
c16519b38b | ||
|
|
8eb5a3f419 | ||
|
|
2293b32cce | ||
|
|
25d0685bdf | ||
|
|
fa47c82617 | ||
|
|
b6a238c67a | ||
|
|
462e982784 | ||
|
|
b8ec65b3ca | ||
|
|
1f2e0f336a | ||
|
|
aa956fbc47 | ||
|
|
02bd47ba29 | ||
|
|
708a019ce2 | ||
|
|
0288cc38fc | ||
|
|
b4e98a4dd1 | ||
|
|
b883c660fc | ||
|
|
4ae64c568b | ||
|
|
31e796c4e8 | ||
|
|
775e718dff | ||
|
|
39b00910ae | ||
|
|
2a59419bdf | ||
|
|
b846a2e909 | ||
|
|
75a0f0de95 | ||
|
|
d81236f71e | ||
|
|
129f1e6f76 | ||
|
|
798fdfe24c | ||
|
|
96122fa45a | ||
|
|
c71fd44454 | ||
|
|
86fccf082a | ||
|
|
12761bbfb1 | ||
|
|
88b930b5b1 | ||
|
|
c1222368b4 | ||
|
|
ea5f1884be | ||
|
|
a5832706bd | ||
|
|
d8b7ebaf47 | ||
|
|
9a500941d8 | ||
|
|
a7be8751dc | ||
|
|
2f622fc7bd | ||
|
|
a26d225d81 | ||
|
|
798a120926 | ||
|
|
c97572be3e | ||
|
|
36af4198ed | ||
|
|
e88b9be12b | ||
|
|
be19ed592d | ||
|
|
110eae7c19 | ||
|
|
a234c39e41 | ||
|
|
0ff1049eb7 | ||
|
|
f78586ff7b | ||
|
|
cc37f645ba | ||
|
|
5341de2163 | ||
|
|
fd5027a017 | ||
|
|
d43e63f562 | ||
|
|
a874fe9cb7 | ||
|
|
87ba51c936 | ||
|
|
95d521c81b | ||
|
|
91e29144b0 | ||
|
|
a3b104a2ed | ||
|
|
2008d09915 | ||
|
|
2599b9fe82 | ||
|
|
815f12df46 | ||
|
|
131a3c6af7 | ||
|
|
54ee621e41 | ||
|
|
19be14021b | ||
|
|
bd5a0b526b | ||
|
|
1934587253 | ||
|
|
3365aca12f | ||
|
|
bab00cb961 | ||
|
|
6022fc2aa1 | ||
|
|
15a9b4a3e7 | ||
|
|
a41d7de348 | ||
|
|
420d904ba9 | ||
|
|
446c788eb3 | ||
|
|
f688d20fe6 | ||
|
|
a02d4ff0dd | ||
|
|
8396a262c3 | ||
|
|
86a72ea127 | ||
|
|
9479f5f4f5 | ||
|
|
9aa27a75bd | ||
|
|
5fe6350c68 | ||
|
|
5810ae4f49 | ||
|
|
84bbec0666 | ||
|
|
94ba8b70b3 | ||
|
|
e5d1c7dbaa | ||
|
|
9b14628364 | ||
|
|
703a25efad | ||
|
|
17f5cc36ca | ||
|
|
9054b2d947 | ||
|
|
7ffbe1fb78 | ||
|
|
acdd66b4c5 | ||
|
|
faf10c64a9 | ||
|
|
ece9787822 | ||
|
|
7f6fe08b06 | ||
|
|
f5053f65bb | ||
|
|
c888142bcb | ||
|
|
bc0d673af0 | ||
|
|
2d16819cbf | ||
|
|
8053211bf4 | ||
|
|
bd432c470d | ||
|
|
44d89ecff4 | ||
|
|
d089176030 | ||
|
|
40a88f57f7 | ||
|
|
491d753edc | ||
|
|
c4e46b2f3e | ||
|
|
06e3feeee1 | ||
|
|
ea75f6ccc1 | ||
|
|
286aaffa31 | ||
|
|
91a18ebe97 | ||
|
|
c4910709d9 | ||
|
|
3e996a0f82 | ||
|
|
dac408627b | ||
|
|
f8bb4fa637 | ||
|
|
d2cebc3c2d | ||
|
|
a38b2fd943 | ||
|
|
b6cc8e2414 | ||
|
|
14867c7abc | ||
|
|
ef3bb7444b | ||
|
|
6c7c067662 | ||
|
|
19eff4755b | ||
|
|
eb7c5be7cc | ||
|
|
6fe1fb39fb | ||
|
|
0be3631788 | ||
|
|
f2e89140ed | ||
|
|
f44b5aa2d0 | ||
|
|
c6337398df | ||
|
|
b0c6c4195e | ||
|
|
5c1e2636c2 | ||
|
|
76da6b5468 | ||
|
|
f8e4a8b170 | ||
|
|
aaee0a6a2e | ||
|
|
9abb757c31 | ||
|
|
da78af20ac | ||
|
|
dddcffb633 | ||
|
|
c73a6b4a83 | ||
|
|
04010978a0 | ||
|
|
3163a92594 | ||
|
|
53ddd721a7 | ||
|
|
17212477c5 | ||
|
|
db289689b8 | ||
|
|
b2bb4e1888 | ||
|
|
618da006a5 | ||
|
|
228c43890f | ||
|
|
f71ed29e96 | ||
|
|
9d55331980 | ||
|
|
0b4221d7f3 | ||
|
|
12f412d915 | ||
|
|
7ea5c4d38c | ||
|
|
b45ab40b21 | ||
|
|
dea23211ae | ||
|
|
9b5cd9a1a4 | ||
|
|
1ebe7b01bc | ||
|
|
17a8c79e21 | ||
|
|
9c55fcfcbf | ||
|
|
7744335020 | ||
|
|
a9651788b2 | ||
|
|
9c2b9dc40f | ||
|
|
257390e78c | ||
|
|
f1675828d9 | ||
|
|
ff3787781e | ||
|
|
3460116d3b | ||
|
|
194e82433a | ||
|
|
4de40cfa9b | ||
|
|
6f6740ecee | ||
|
|
c1e6e16e49 | ||
|
|
3ca13fe6a5 | ||
|
|
4fb262c477 | ||
|
|
77fbebf893 | ||
|
|
9ccf39b49a | ||
|
|
e38c759563 | ||
|
|
c612166c73 | ||
|
|
08fb044086 | ||
|
|
adcc64c1b1 | ||
|
|
9d67d9d4bd | ||
|
|
1c386b2bd2 | ||
|
|
70f911fa3e | ||
|
|
17cd51259e | ||
|
|
c21739c9e4 | ||
|
|
07ef6c84d1 | ||
|
|
922483bb45 | ||
|
|
c99f92f782 | ||
|
|
b848423346 | ||
|
|
4a865430b0 | ||
|
|
bb94e3b228 | ||
|
|
b446a8a92e | ||
|
|
bd0c4061bd | ||
|
|
147e0fbf5b | ||
|
|
39978b475d | ||
|
|
5e7c4bc702 | ||
|
|
7167a5e44d | ||
|
|
460aeb32e2 | ||
|
|
0d9c23f4ce | ||
|
|
0ff9edd605 | ||
|
|
f5e2de6795 | ||
|
|
c84009e9f9 | ||
|
|
cfcda58ba7 | ||
|
|
c4d1f3d354 | ||
|
|
38e62f7ce5 | ||
|
|
fba3ee23ea | ||
|
|
a6fa079e64 | ||
|
|
cb88641d5a | ||
|
|
badc90bae1 | ||
|
|
2458b981f8 | ||
|
|
fba35861b3 | ||
|
|
186ab3945f | ||
|
|
ec1c398243 | ||
|
|
17cb82c393 | ||
|
|
d1e6264059 | ||
|
|
4478b7cd71 | ||
|
|
9fcc055dc5 | ||
|
|
bb44fa84b2 | ||
|
|
4878540fb0 | ||
|
|
99af96f74d | ||
|
|
1f7b493e6c | ||
|
|
b69554b493 | ||
|
|
7bddeb1b57 | ||
|
|
b71dac0128 | ||
|
|
d5bf3bbb59 | ||
|
|
983f70dc0d | ||
|
|
6c4e1d431b | ||
|
|
afe6806da2 | ||
|
|
b47fec5f33 | ||
|
|
e9873f83ec | ||
|
|
c4cddbe60a | ||
|
|
744f576ba7 | ||
|
|
f821060197 | ||
|
|
bff5109144 | ||
|
|
90c99e4509 | ||
|
|
3202c36846 | ||
|
|
aeedcaa750 | ||
|
|
594bf7af05 | ||
|
|
a2f9578859 | ||
|
|
74e3bc7c2e | ||
|
|
c335250f35 | ||
|
|
1d05a8a224 | ||
|
|
8cff959878 | ||
|
|
cecfaaf440 | ||
|
|
9cf8c84b07 | ||
|
|
43dd0fe5be | ||
|
|
9dfea8dde0 | ||
|
|
9d8028e044 | ||
|
|
3797df0b1d | ||
|
|
12e1863a54 | ||
|
|
23c0a948d2 | ||
|
|
ad054cadb5 | ||
|
|
6d2756dc49 | ||
|
|
a3579a9ecf | ||
|
|
71fa08b13a | ||
|
|
3d6229c697 | ||
|
|
b20448b19c | ||
|
|
29b2c91626 | ||
|
|
ae1403edb9 | ||
|
|
0edec4d945 | ||
|
|
57e8236ef7 | ||
|
|
3b337ac908 | ||
|
|
ea746da96c | ||
|
|
0f516c68eb | ||
|
|
c1e7ec54f6 | ||
|
|
46a3dbe84b | ||
|
|
876e1c51fb | ||
|
|
c9dd553941 | ||
|
|
a49b06f2f4 | ||
|
|
6a06117f75 | ||
|
|
9e84ed336c | ||
|
|
d7dac92d5f | ||
|
|
8ca39923dd | ||
|
|
328e82f630 | ||
|
|
cea75d6aa2 | ||
|
|
db3cd3589b | ||
|
|
506e733049 | ||
|
|
d7ca5e9549 | ||
|
|
d70fd3bfcc | ||
|
|
528f17eb6a | ||
|
|
c444e29cbb | ||
|
|
4ad586e427 | ||
|
|
403c80d7ca | ||
|
|
50de0a6fa4 | ||
|
|
f5d8aa0249 | ||
|
|
974dfcfeac | ||
|
|
65e4d57e88 | ||
|
|
9a3a402be0 | ||
|
|
3038fd8c7d | ||
|
|
e4d4c93d9c | ||
|
|
0a51fb06ea | ||
|
|
9849adcbd1 | ||
|
|
48b3c3ba61 | ||
|
|
971fd5aaed | ||
|
|
ef33e11a62 | ||
|
|
ba36c4b046 | ||
|
|
f9bb89481b | ||
|
|
50d5ec48cf | ||
|
|
820667c608 | ||
|
|
9d156723c0 | ||
|
|
2f791fe909 | ||
|
|
fa11401eb1 | ||
|
|
b06e077f8d | ||
|
|
fee76ca44f | ||
|
|
b2725d5149 | ||
|
|
ec8ecf5a80 | ||
|
|
0a4bde1ade | ||
|
|
b55eced0ed | ||
|
|
23ae9c302f | ||
|
|
3b754bfa09 | ||
|
|
a497513d89 | ||
|
|
380c21ea97 | ||
|
|
e8ff6f7526 | ||
|
|
bb332fa6d7 | ||
|
|
4fd9203800 | ||
|
|
9b8361171f | ||
|
|
11a7b68c4f | ||
|
|
9098de22c1 | ||
|
|
139b2878d0 | ||
|
|
53368fcbbf | ||
|
|
4ef92089d5 | ||
|
|
43735086d1 | ||
|
|
63e36ed43f | ||
|
|
f747269151 | ||
|
|
878bbedb37 | ||
|
|
4b1a47e902 | ||
|
|
121558bf5c | ||
|
|
681089f8c7 | ||
|
|
d417a712d3 | ||
|
|
6593a5f6fe | ||
|
|
e33d120481 | ||
|
|
39ff7cfd5c | ||
|
|
bded03908f | ||
|
|
b9dc9e72bb | ||
|
|
0e3c0b922d | ||
|
|
139d29cc9b | ||
|
|
659717cb1f | ||
|
|
68db304a2a | ||
|
|
e473958377 | ||
|
|
68b763bcdd | ||
|
|
239ca81db7 | ||
|
|
4adcc5e14a | ||
|
|
4cb26d462e | ||
|
|
9408a50b1b | ||
|
|
b2364d2398 | ||
|
|
34ee78dbbc | ||
|
|
56b94542a6 | ||
|
|
c09e8f6d12 | ||
|
|
5b5aeaa683 | ||
|
|
c60bd82da7 | ||
|
|
9d8ce5e4ee | ||
|
|
458e9f64a7 | ||
|
|
3809421de7 | ||
|
|
5b1121c9dd | ||
|
|
5dcfe44ccd | ||
|
|
cc290c0bdb | ||
|
|
9c6190c1ff | ||
|
|
7bd7c0cfc9 | ||
|
|
b18414ba8c | ||
|
|
6430166ec9 | ||
|
|
c7da7f2073 | ||
|
|
bd6e7dbbb1 | ||
|
|
d86bc80f43 | ||
|
|
485a127cf6 | ||
|
|
c92b03d908 | ||
|
|
dad6230cf1 | ||
|
|
33890cf0fd | ||
|
|
0d4a9e06d6 | ||
|
|
9af874d21b | ||
|
|
55d85df9fc | ||
|
|
d91fc890cd | ||
|
|
918c52eed8 | ||
|
|
7594ec632b | ||
|
|
565d08ed72 | ||
|
|
1305b5cf17 | ||
|
|
041c433ccb | ||
|
|
ff7a9547c3 | ||
|
|
602cd6991e | ||
|
|
686adcdebd | ||
|
|
588d5bd52b | ||
|
|
d82406a959 | ||
|
|
4765eaa7f3 | ||
|
|
96eda85806 | ||
|
|
88035ccbae | ||
|
|
87e738dda2 | ||
|
|
903f962f29 | ||
|
|
a4e0a34357 | ||
|
|
3c466df80e | ||
|
|
edd4966cea | ||
|
|
72393d06c9 | ||
|
|
df07d10a5c | ||
|
|
bb1737928e | ||
|
|
7895e42687 | ||
|
|
71d0f6fa31 | ||
|
|
4ea310fa68 | ||
|
|
3b42f42491 | ||
|
|
1322416ba4 | ||
|
|
947ccaad04 | ||
|
|
f862a0df24 | ||
|
|
868fd502b9 | ||
|
|
a8ef802c78 | ||
|
|
2241fd2c3b | ||
|
|
7303b94afc | ||
|
|
b948264c32 | ||
|
|
ff0e3df948 | ||
|
|
d110949d2a | ||
|
|
879ca01042 | ||
|
|
2702145f3f | ||
|
|
2f619f214b | ||
|
|
b235e5601f | ||
|
|
76a2e51c15 | ||
|
|
c530bba261 | ||
|
|
c99b3778b2 | ||
|
|
efde5747c4 | ||
|
|
ebd94934a5 | ||
|
|
e1cf3bf54b | ||
|
|
cb52d8466a | ||
|
|
24735b6ab3 | ||
|
|
ebf541219c | ||
|
|
d2b1d56e8b | ||
|
|
67fd229f20 | ||
|
|
55a095d536 | ||
|
|
ae65e4be8d | ||
|
|
2d303f5dc2 | ||
|
|
e83762d3b1 | ||
|
|
442eaf766c | ||
|
|
3e5b61e0c8 | ||
|
|
5570a24781 | ||
|
|
e472c82876 | ||
|
|
a857cfcc1e | ||
|
|
ef763a8550 | ||
|
|
a846f9da53 | ||
|
|
cff34358d7 | ||
|
|
8a2bdcd133 | ||
|
|
4e6188919f | ||
|
|
d7369707ba | ||
|
|
88636d269d | ||
|
|
4a0445b663 | ||
|
|
10123daad2 | ||
|
|
c5b08c225c | ||
|
|
6f123c6382 | ||
|
|
27e6dfeaab | ||
|
|
3d643b16b2 | ||
|
|
d614bba3ac | ||
|
|
66c1662537 | ||
|
|
fe9cd01b82 | ||
|
|
0c2e3777b1 | ||
|
|
348c7dbc56 | ||
|
|
3497040b78 | ||
|
|
cf5b90b444 | ||
|
|
eb739c9ff9 | ||
|
|
b971b362c2 | ||
|
|
ece85e6d15 | ||
|
|
4d81e23542 | ||
|
|
aa8b58628e | ||
|
|
9de0eeaf51 | ||
|
|
8270254823 | ||
|
|
e3ba535a79 | ||
|
|
d204427f73 | ||
|
|
88f5bb0931 | ||
|
|
fe9fd95b21 | ||
|
|
4016202806 | ||
|
|
215b3d3d59 | ||
|
|
9bb60794da | ||
|
|
32e3d245f9 | ||
|
|
bb6a2513eb | ||
|
|
30ecc0cc05 | ||
|
|
f8c62de6c0 | ||
|
|
833611ba2f | ||
|
|
ce5f83ac6c | ||
|
|
4d62f9f7f9 | ||
|
|
c36cc240e2 | ||
|
|
91b3772e7d | ||
|
|
47af54fb3e | ||
|
|
9c7fdfd9d1 | ||
|
|
fa334e0750 | ||
|
|
55a147143b | ||
|
|
de7f93d98b | ||
|
|
7822d89d65 | ||
|
|
b417e2bde7 | ||
|
|
2b60d3a78c | ||
|
|
4711cd6620 | ||
|
|
37deccd8d3 | ||
|
|
ddd1921deb | ||
|
|
c084a94138 | ||
|
|
e17f911334 | ||
|
|
6d1005d199 | ||
|
|
e3a26b77a4 | ||
|
|
0db03c6faf | ||
|
|
ec65bf7676 | ||
|
|
f7dd2f0323 | ||
|
|
f41ef551d3 | ||
|
|
413f1aac7c | ||
|
|
ca657150ec | ||
|
|
d0c167347d | ||
|
|
270b3fbe61 | ||
|
|
19405cabd6 | ||
|
|
55154564df | ||
|
|
f255fea57e | ||
|
|
cf3789564e | ||
|
|
aa240f1f7b | ||
|
|
83a09c4bc1 | ||
|
|
5f4ddbb8f4 | ||
|
|
2925358856 | ||
|
|
ed3b843dd2 | ||
|
|
8118a29493 | ||
|
|
3833ee6dfe | ||
|
|
96d76545bd | ||
|
|
84cf6b09c2 | ||
|
|
d665c61645 | ||
|
|
d18850ebab | ||
|
|
a7c2c9d9df | ||
|
|
6499933eab | ||
|
|
848a73723e | ||
|
|
6c8e7d253d | ||
|
|
c79ba3ab8a | ||
|
|
87810829ce | ||
|
|
44818a854b | ||
|
|
a5e34cce52 | ||
|
|
8e02aa8294 | ||
|
|
705d280f8b | ||
|
|
ce2d166de2 | ||
|
|
af76f11f36 | ||
|
|
fd23d27239 | ||
|
|
f5ab197b4f | ||
|
|
427803d644 | ||
|
|
21cdc5579e | ||
|
|
1b315b5eb5 | ||
|
|
b82c043d65 | ||
|
|
8116d69c3c | ||
|
|
b95c562651 | ||
|
|
ac1620c072 | ||
|
|
3f60848122 | ||
|
|
784cd4bab3 | ||
|
|
f4b643d54b | ||
|
|
dbe82c077a | ||
|
|
95c9a373bf | ||
|
|
7261fa3398 | ||
|
|
bfdb36d674 | ||
|
|
e779ddaaea | ||
|
|
2810316f4e | ||
|
|
33f1fad711 | ||
|
|
8286c3ab85 | ||
|
|
bf341dd20f | ||
|
|
5958efe958 | ||
|
|
6d49d7b8f9 | ||
|
|
af45b45afe | ||
|
|
00d2359308 | ||
|
|
645baf61de | ||
|
|
7460ffac73 | ||
|
|
4977dd0948 | ||
|
|
ce971cc665 | ||
|
|
03c5098730 | ||
|
|
6d24cbfb3c | ||
|
|
77d3191170 | ||
|
|
f654cbeac4 | ||
|
|
4da65aef3f | ||
|
|
e0dfbf3533 | ||
|
|
9ebd652a32 | ||
|
|
dbc3d97513 | ||
|
|
9d018a3f3b | ||
|
|
fad74c8c98 | ||
|
|
d99db3ad23 | ||
|
|
723c0c29fb | ||
|
|
3166285150 | ||
|
|
e41586ecd3 |
2
.cargo/config.toml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
[alias]
|
||||
xtask = "run --manifest-path xtask/Cargo.toml --"
|
||||
|
|
@ -1,18 +0,0 @@
|
|||
engines:
|
||||
csslint:
|
||||
enabled: false
|
||||
duplication:
|
||||
enabled: true
|
||||
config:
|
||||
languages:
|
||||
- rust
|
||||
eslint:
|
||||
enabled: false
|
||||
fixme:
|
||||
enabled: true
|
||||
ratings:
|
||||
paths:
|
||||
- "**.rs"
|
||||
exclude_paths:
|
||||
- tests/
|
||||
- doc/
|
||||
7
.dockerignore
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
.editorconfig
|
||||
.gitignore
|
||||
/.git/
|
||||
/.github/
|
||||
/.pre-commit-hooks.yaml
|
||||
/scripts/
|
||||
/target/
|
||||
|
|
@ -1,10 +1,11 @@
|
|||
root = true
|
||||
|
||||
[*]
|
||||
charset = utf-8
|
||||
end_of_line = lf
|
||||
indent_style = space
|
||||
insert_final_newline = true
|
||||
trim_trailing_whitespace = true
|
||||
|
||||
[*.rs]
|
||||
charset = utf-8
|
||||
indent_style = space
|
||||
indent_size = 4
|
||||
|
|
|
|||
2
.gitattributes
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
# Enforce Unix newlines
|
||||
* text=auto eol=lf
|
||||
24
.github/dependabot.yml
vendored
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
version: 2
|
||||
updates:
|
||||
- package-ecosystem: "cargo"
|
||||
directory: "/"
|
||||
schedule:
|
||||
interval: "weekly"
|
||||
open-pull-requests-limit: 5
|
||||
commit-message:
|
||||
prefix: "chore"
|
||||
include: "scope"
|
||||
labels:
|
||||
- "dependencies"
|
||||
- "rust"
|
||||
groups:
|
||||
rust-dependencies:
|
||||
patterns:
|
||||
- "*"
|
||||
update-types:
|
||||
- "minor"
|
||||
- "patch"
|
||||
- package-ecosystem: "github-actions"
|
||||
directory: "/"
|
||||
schedule:
|
||||
interval: "weekly"
|
||||
138
.github/workflows/deploy.yml
vendored
Normal file
|
|
@ -0,0 +1,138 @@
|
|||
name: deploy
|
||||
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- v*.*.*
|
||||
|
||||
concurrency:
|
||||
# If we ever end up with two concurrent jobs for releasing the same tag, the former should be cancelled
|
||||
group: release-${{ github.ref }}
|
||||
cancel-in-progress: true
|
||||
|
||||
permissions:
|
||||
actions: read
|
||||
contents: write
|
||||
|
||||
jobs:
|
||||
deploy:
|
||||
name: Deploy release
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 30
|
||||
|
||||
# Prevent job from running on forks
|
||||
if: ${{ !github.event.repository.fork }}
|
||||
|
||||
strategy:
|
||||
# Execute one job at a time to avoid potential race conditions in the
|
||||
# GitHub release management APIs. See:
|
||||
# https://github.com/softprops/action-gh-release/issues/445#issuecomment-2407940052
|
||||
max-parallel: 1
|
||||
matrix:
|
||||
target:
|
||||
- x86_64-unknown-linux-gnu
|
||||
- x86_64-unknown-linux-musl
|
||||
- aarch64-unknown-linux-gnu
|
||||
- aarch64-unknown-linux-musl
|
||||
- x86_64-pc-windows-msvc
|
||||
- i686-pc-windows-msvc
|
||||
- x86_64-apple-darwin
|
||||
- aarch64-apple-darwin
|
||||
|
||||
steps:
|
||||
- name: Checkout source
|
||||
uses: actions/checkout@v6
|
||||
|
||||
- name: Install Rust toolchain
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||
with:
|
||||
toolchain: nightly
|
||||
cache-bin: false
|
||||
cache-shared-key: cache
|
||||
|
||||
- name: Install cargo-deb
|
||||
if: endsWith(matrix.target, '-linux-gnu')
|
||||
uses: taiki-e/install-action@v2
|
||||
with:
|
||||
tool: cargo-deb
|
||||
|
||||
- name: Get the Oxipng version
|
||||
id: oxipngMeta
|
||||
run: >
|
||||
echo "version=$(cargo metadata --format-version 1 --no-deps |
|
||||
jq -r '.packages[] | select(.name == "oxipng").version')" >> "$GITHUB_OUTPUT"
|
||||
|
||||
- name: Retrieve ${{ matrix.target }} binary
|
||||
uses: dawidd6/action-download-artifact@v21
|
||||
with:
|
||||
workflow: oxipng.yml
|
||||
commit: ${{ github.sha }}
|
||||
name: Oxipng binary (${{ matrix.target }})
|
||||
path: target
|
||||
|
||||
- name: Generate up to date manual
|
||||
run: scripts/manual.sh
|
||||
|
||||
- name: Build archives
|
||||
working-directory: target
|
||||
run: |
|
||||
ARCHIVE_NAME="oxipng-${{ steps.oxipngMeta.outputs.version }}-${{ matrix.target }}"
|
||||
|
||||
mkdir "$ARCHIVE_NAME"
|
||||
cp ../CHANGELOG.md ../README.md ../MANUAL.txt "$ARCHIVE_NAME"
|
||||
|
||||
case '${{ matrix.target }}' in
|
||||
*-windows-*)
|
||||
cp ../LICENSE "$ARCHIVE_NAME/LICENSE.txt"
|
||||
cp oxipng.exe "$ARCHIVE_NAME"
|
||||
zip "${ARCHIVE_NAME}.zip" "$ARCHIVE_NAME"/*;;
|
||||
*)
|
||||
cp ../LICENSE "$ARCHIVE_NAME"
|
||||
cp oxipng "$ARCHIVE_NAME"
|
||||
# Execute permissions are not stored in artifact files,
|
||||
# so make the binary world-executable to meet user
|
||||
# expectations set by preceding releases.
|
||||
# Related issue:
|
||||
# https://github.com/oxipng/oxipng/issues/575
|
||||
chmod ugo+x "$ARCHIVE_NAME"/oxipng
|
||||
tar -vczf "${ARCHIVE_NAME}.tar.gz" "$ARCHIVE_NAME"/*;;
|
||||
esac
|
||||
|
||||
- name: Install AArch64 libc components
|
||||
if: matrix.target == 'aarch64-unknown-linux-gnu'
|
||||
run: |
|
||||
sudo apt-get -yq update
|
||||
# The shared libc AArch64 libraries are needed for cargo deb below
|
||||
# to be able to infer package requirements with dpkg-shlibdeps
|
||||
# properly
|
||||
sudo apt-get -yq install libc6-arm64-cross libgcc-s1-arm64-cross
|
||||
|
||||
- name: Build Debian packages
|
||||
if: endsWith(matrix.target, '-linux-gnu')
|
||||
env:
|
||||
# The *-arm64-cross packages above install AArch64 libraries in
|
||||
# /usr/<arch>/lib instead of /usr/lib/<arch>, as expected by
|
||||
# cargo-deb and dpkg-shlibdeps to find such shared libraries.
|
||||
# Make both of them visible to such commands by adding that directory
|
||||
# to the dynamic linker's library search path. See:
|
||||
# - <https://man7.org/linux/man-pages/man1/dpkg-shlibdeps.1.html> ("Errors" section)
|
||||
# - <https://github.com/kornelski/cargo-deb/issues/21>
|
||||
LD_LIBRARY_PATH: /usr/aarch64-linux-gnu/lib
|
||||
run: |
|
||||
mkdir -p "target/${{ matrix.target }}/release"
|
||||
mv target/oxipng "target/${{ matrix.target }}/release"
|
||||
cargo deb --target "${{ matrix.target }}" --no-build --no-strip
|
||||
|
||||
- name: Create release notes
|
||||
run: tail -n +3 CHANGELOG.md | sed -e '/^$/,$d' > RELEASE_NOTES.txt
|
||||
|
||||
- name: Create release
|
||||
uses: softprops/action-gh-release@v3
|
||||
with:
|
||||
name: v${{ steps.oxipngMeta.outputs.version }}
|
||||
body_path: RELEASE_NOTES.txt
|
||||
files: |
|
||||
target/*.zip
|
||||
target/*.tar.gz
|
||||
target/${{ matrix.target }}/debian/*.deb
|
||||
60
.github/workflows/docker.yml
vendored
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
name: docker
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
tags:
|
||||
- v*.*.*
|
||||
pull_request:
|
||||
types:
|
||||
- opened
|
||||
- synchronize
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
use_cache:
|
||||
description: "Use build cache"
|
||||
required: true
|
||||
type: boolean
|
||||
default: true
|
||||
|
||||
concurrency:
|
||||
# If we ever end up with two concurrent Docker build jobs for the same commit
|
||||
# and same versioning context, the former should be cancelled
|
||||
group: docker-${{ github.sha }}-${{ github.ref_type }}-${{ github.head_ref }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
build:
|
||||
uses: docker/github-builder/.github/workflows/build.yml@v1
|
||||
permissions:
|
||||
contents: read
|
||||
packages: write
|
||||
id-token: write
|
||||
attestations: write
|
||||
with:
|
||||
output: image
|
||||
platforms: linux/amd64,linux/arm64
|
||||
push: ${{ github.event_name != 'pull_request' }}
|
||||
cache: ${{ github.event_name != 'workflow_dispatch' || inputs.use_cache }}
|
||||
# We don't sign the image because ghcr.io doesn't support OCI Referrers API yet, and the fallback relies on pushing new tags on each build
|
||||
# https://github.com/docker/github-builder/issues/109#issuecomment-3885082486
|
||||
# https://github.com/opencontainers/distribution-spec/blob/v1.1.0/spec.md#backwards-compatibility
|
||||
sign: false
|
||||
sbom: true
|
||||
meta-images: ghcr.io/${{ github.repository }}
|
||||
meta-tags: |
|
||||
type=ref,event=branch
|
||||
type=ref,event=pr
|
||||
type=semver,pattern={{raw}}
|
||||
set-meta-annotations: true
|
||||
set-meta-labels: true
|
||||
labels: |
|
||||
org.opencontainers.image.title=Oxipng
|
||||
annotations: |
|
||||
org.opencontainers.image.title=Oxipng
|
||||
|
||||
secrets:
|
||||
registry-auths: |
|
||||
- registry: ghcr.io
|
||||
username: ${{ github.actor }}
|
||||
password: ${{ secrets.GITHUB_TOKEN }}
|
||||
180
.github/workflows/oxipng.yml
vendored
Normal file
|
|
@ -0,0 +1,180 @@
|
|||
name: oxipng
|
||||
|
||||
on:
|
||||
push:
|
||||
pull_request:
|
||||
types:
|
||||
- opened
|
||||
- synchronize
|
||||
workflow_dispatch:
|
||||
|
||||
concurrency:
|
||||
# If we ever end up with two concurrent CI jobs for the same commit, the former should be cancelled
|
||||
group: ci-${{ github.sha }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
ci:
|
||||
name: CI
|
||||
|
||||
runs-on: ${{ matrix.os }}
|
||||
timeout-minutes: 60
|
||||
|
||||
# Prevent tags and in-repo PRs from triggering this workflow more than once for a commit
|
||||
if: github.ref_type != 'tag' && (github.event_name != 'pull_request' || github.event.pull_request.head.repo.fork)
|
||||
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
target:
|
||||
- x86_64-unknown-linux-gnu
|
||||
- x86_64-unknown-linux-musl
|
||||
- aarch64-unknown-linux-gnu
|
||||
- aarch64-unknown-linux-musl
|
||||
- x86_64-pc-windows-msvc
|
||||
- i686-pc-windows-msvc
|
||||
- x86_64-apple-darwin
|
||||
- aarch64-apple-darwin
|
||||
|
||||
include:
|
||||
- target: x86_64-unknown-linux-gnu
|
||||
os: ubuntu-24.04
|
||||
target-apt-arch: amd64
|
||||
- target: x86_64-unknown-linux-musl
|
||||
os: ubuntu-24.04
|
||||
target-apt-arch: amd64
|
||||
- target: aarch64-unknown-linux-gnu
|
||||
os: ubuntu-24.04-arm
|
||||
target-apt-arch: arm64
|
||||
- target: aarch64-unknown-linux-musl
|
||||
os: ubuntu-24.04-arm
|
||||
target-apt-arch: arm64
|
||||
- target: x86_64-pc-windows-msvc
|
||||
os: windows-latest
|
||||
- target: i686-pc-windows-msvc
|
||||
os: windows-latest
|
||||
- target: x86_64-apple-darwin
|
||||
os: macos-15
|
||||
- target: aarch64-apple-darwin
|
||||
os: macos-15
|
||||
|
||||
env:
|
||||
CARGO_BUILD_TARGET: ${{ matrix.target }}
|
||||
|
||||
steps:
|
||||
- name: Checkout source
|
||||
uses: actions/checkout@v6
|
||||
with:
|
||||
persist-credentials: false
|
||||
|
||||
- name: Install musl development files
|
||||
if: endsWith(matrix.target, '-musl')
|
||||
run: |
|
||||
sudo apt-get -yq update
|
||||
sudo apt-get -yq install musl-tools musl-dev:${{ matrix.target-apt-arch }}
|
||||
|
||||
- name: Install Rust toolchain
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||
with:
|
||||
toolchain: nightly
|
||||
target: ${{ env.CARGO_BUILD_TARGET }}
|
||||
components: clippy, rustfmt, rust-src
|
||||
cache-bin: false
|
||||
cache-shared-key: cache
|
||||
|
||||
- name: Install nextest
|
||||
uses: taiki-e/install-action@nextest
|
||||
|
||||
- name: Install cargo-hack
|
||||
if: matrix.target == 'x86_64-unknown-linux-gnu'
|
||||
uses: taiki-e/install-action@cargo-hack
|
||||
|
||||
- name: Install clippy-sarif
|
||||
if: matrix.target == 'x86_64-unknown-linux-gnu'
|
||||
uses: taiki-e/install-action@v2
|
||||
with:
|
||||
tool: clippy-sarif
|
||||
|
||||
- name: Install sarif-fmt
|
||||
if: matrix.target == 'x86_64-unknown-linux-gnu'
|
||||
uses: taiki-e/install-action@v2
|
||||
with:
|
||||
tool: sarif-fmt
|
||||
|
||||
- name: Run rustfmt
|
||||
if: matrix.target == 'x86_64-unknown-linux-gnu'
|
||||
run: cargo fmt --check
|
||||
|
||||
- name: Run Clippy for all feature combinations
|
||||
if: matrix.target == 'x86_64-unknown-linux-gnu'
|
||||
run: >
|
||||
set -o pipefail;
|
||||
cargo hack clippy --no-deps --all-targets --feature-powerset \
|
||||
--exclude-features sanity-checks,system-libdeflate \
|
||||
--message-format=json -- -D warnings \
|
||||
| clippy-sarif
|
||||
| tee clippy-results.sarif
|
||||
| sarif-fmt
|
||||
|
||||
- name: Run tests
|
||||
run: |
|
||||
cargo nextest run --release --features sanity-checks
|
||||
cargo test --doc --release --features sanity-checks
|
||||
|
||||
- name: Build benchmarks
|
||||
run: cargo bench --no-run
|
||||
|
||||
- name: Build docs
|
||||
if: matrix.target == 'x86_64-unknown-linux-gnu'
|
||||
run: cargo doc --release --no-deps
|
||||
|
||||
- name: Build CLI binary
|
||||
run: cargo build --release -Zbuild-std
|
||||
env:
|
||||
RUSTFLAGS: "-Zlocation-detail=none -Zunstable-options -Cpanic=immediate-abort"
|
||||
|
||||
- name: Upload CLI binary as artifact
|
||||
uses: actions/upload-artifact@v7
|
||||
with:
|
||||
name: Oxipng binary (${{ matrix.target }})
|
||||
path: |
|
||||
target/${{ env.CARGO_BUILD_TARGET }}/release/oxipng
|
||||
target/${{ env.CARGO_BUILD_TARGET }}/release/oxipng.exe
|
||||
|
||||
- name: Upload analysis results to GitHub
|
||||
uses: github/codeql-action/upload-sarif@v4
|
||||
if: always() && matrix.target == 'x86_64-unknown-linux-gnu'
|
||||
continue-on-error: true
|
||||
with:
|
||||
sarif_file: clippy-results.sarif
|
||||
category: clippy
|
||||
|
||||
msrv-check:
|
||||
name: MSRV check
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 30
|
||||
|
||||
# Prevent tags and in-repo PRs from triggering this workflow more than once for a commit
|
||||
if: github.ref_type != 'tag' && (github.event_name != 'pull_request' || github.event.pull_request.head.repo.fork)
|
||||
|
||||
steps:
|
||||
- name: Checkout source
|
||||
uses: actions/checkout@v6
|
||||
with:
|
||||
persist-credentials: false
|
||||
|
||||
- name: Install MSRV Rust toolchain
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||
with:
|
||||
toolchain: 1.85.1
|
||||
cache-bin: false
|
||||
cache-shared-key: cache
|
||||
|
||||
- name: Install nextest
|
||||
uses: taiki-e/install-action@nextest
|
||||
|
||||
- name: Run tests
|
||||
run: |
|
||||
cargo nextest run --release --features sanity-checks
|
||||
cargo test --doc --release --features sanity-checks
|
||||
7
.gitignore
vendored
|
|
@ -1,5 +1,6 @@
|
|||
target
|
||||
*.bk
|
||||
.DS_Store
|
||||
*.bk
|
||||
*.out.png
|
||||
/.idea
|
||||
/.idea/
|
||||
/node_modules/
|
||||
/target/
|
||||
|
|
|
|||
7
.pre-commit-hooks.yaml
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
- id: oxipng
|
||||
name: oxipng
|
||||
description: 'Multithreaded PNG optimizer written in Rust.'
|
||||
entry: oxipng
|
||||
language: rust
|
||||
types: [png]
|
||||
require_serial: true
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
language: rust
|
||||
rust:
|
||||
- 1.8.0
|
||||
- stable
|
||||
- beta
|
||||
- nightly
|
||||
cache: cargo
|
||||
637
CHANGELOG.md
|
|
@ -1,80 +1,587 @@
|
|||
**Version 0.11.0 (unreleased)**
|
||||
- [SEMVER_MAJOR] Bump minimum rustc version to 1.8.0, required by dependencies
|
||||
## Version 10.1.1
|
||||
|
||||
**Version 0.10.0**
|
||||
- [SEMVER_MINOR] Make clap and regex dependencies optional
|
||||
- Enabled by default, needed for executable build; can be disabled for use in crates
|
||||
- Remove reduction from palette to grayscale, which was not working and provided minimal benefit
|
||||
- [Performance] Improve Bigrams performance, giving notably faster results at lower levels.
|
||||
- [Improvement] Change `--np` flag to also prevent conversion to indexed from other color types.
|
||||
- [Improvement] Improve support for recompressing ICC profiles with high compression ratios.
|
||||
- [Misc] Add warning when `--ziwi` exceeds `--zi`.
|
||||
- [Build] Further reduce size of binaries.
|
||||
|
||||
**Version 0.9.0**
|
||||
- [SEMVER_MAJOR] Significant refactoring of modules
|
||||
- Use `itertools` to cleanup areas of code
|
||||
- Use multiple threads for filtering trials
|
||||
## Version 10.1.0
|
||||
|
||||
**Version 0.8.2**
|
||||
- Fix issue where images smaller than 4px width would crash on interlacing ([#42](https://github.com/shssoichiro/oxipng/issues/42))
|
||||
- [Feature] Add `--json` option for machine-readable output.
|
||||
- [Improvement] New default output with file counter and summary. (Use an extra `-v` flag to get the same output as before.)
|
||||
- [Bugfix] Fix fast mode sometimes giving suboptimal results for small, indexed images.
|
||||
|
||||
**Version 0.8.1**
|
||||
- Minor optimizations
|
||||
- Fix issue where interlaced images with certain widths would fail to optimize
|
||||
## Version 10.0.0
|
||||
|
||||
**Version 0.8.0**
|
||||
- [SEMVER_MINOR] Add support for optimizing PNGs already loaded into memory via library function
|
||||
- [Breaking] CLI: Change short zopfli flag from `-Z` to `-z`.
|
||||
- [Breaking] CLI: Change `--pretend`/`-P` to `--dry-run`/`-d`.
|
||||
- [Breaking] CLI: Change `--interlace` options from `1`/`0` to `on`/`off`.
|
||||
- [Breaking] API: Change `Options.interlace: Option<Interlacing>` to `Options.interlace: Option<bool>`.
|
||||
- [Breaking] API: Change `Options.filter: IndexSet<RowFilter>` to `Options.filters: IndexSet<FilterStrategy>`.
|
||||
- [Breaking] API: Change `Options.deflate: Deflaters` to `Options.deflater: Deflater`.
|
||||
- [Breaking] API: Change `Deflaters::Zopfli { .. }` to `Deflater::Zopfli(ZopfliOptions { .. })`.
|
||||
- [Breaking] API: Change `optimize()` to return `(usize, usize)`, with original and optimized sizes.
|
||||
- [Feature] Allow zopfli iterations higher than 255.
|
||||
- [Feature] Add `--ziwi` option for zopfli iterations without improvement.
|
||||
- [Feature] Add `--max-raw-size` option to skip images that are too large.
|
||||
- [Feature] Add `--brute-level` and `--brute-lines` options for advanced control of Brute filter.
|
||||
- [Improvement] Reduce memory usage for `fast` mode.
|
||||
- [Improvement] Slightly improve compression with Brute filter at levels 5 and 6.
|
||||
- [Misc] Change `--preserve` option to no longer preserve last access time.
|
||||
- [Misc] Make help output colored.
|
||||
|
||||
**Version 0.7.0**
|
||||
- Minor compression improvement on interlaced images
|
||||
- Performance optimizations
|
||||
- [SEMVER_MINOR] Move default Options into a Default impl
|
||||
- [SEMVER_MINOR] Add option for setting number of threads ([#39](https://github.com/shssoichiro/oxipng/issues/39))
|
||||
## Version 9.1.5
|
||||
|
||||
**Version 0.6.0**
|
||||
- Fix issue where output directory would not be created if it did not exist
|
||||
- Use miniz for compression strategies where it outperforms zlib
|
||||
- [SEMVER_MINOR] Partially implement -p / --preserve, as far as stable Rust will allow for now
|
||||
- [SEMVER_MINOR] Implement --fix to ignore CRC errors and recalculate correct CRC in output
|
||||
- [Feature] Add `--sequential` option to process files sequentially rather than in parallel.
|
||||
- [Performance] Update to latest Zopfli with greatly improved performance.
|
||||
- [Improvement] Reduce memory usage.
|
||||
- [Bugfix] Correct handling of grayscale conversion when ICC profile is present.
|
||||
|
||||
**Version 0.5.0**
|
||||
- [SEMVER_MINOR] Palette entries can now reduced, on by default ([#11](https://github.com/shssoichiro/oxipng/issues/11))
|
||||
- Don't report that we are in pretend mode if verbosity is set to none
|
||||
- Add cargo bench suite ([#7](https://github.com/shssoichiro/oxipng/issues/7))
|
||||
## Version 9.1.4
|
||||
|
||||
**Version 0.4.0**
|
||||
- Performance optimizations
|
||||
- [SEMVER_MAJOR] `-s` automatically infers `--strip safe` ([#31](https://github.com/shssoichiro/oxipng/issues/31))
|
||||
- Update byteorder and clap crates
|
||||
- Fix issue where interlaced images incorrectly applied filters on the first line of a pass
|
||||
- [Improvement] Improve optimization of APNG files (reductions still not supported yet).
|
||||
- [Improvement] Improve reductions for small images and ensure consistent results for repeat runs.
|
||||
- [Build] Add feature `system-libdeflate` to use the system-installed version of libdeflate.
|
||||
- [Misc] Strip C2PA metadata by default.
|
||||
|
||||
**Version 0.3.0**
|
||||
- Properly decode interlaced images
|
||||
- [SEMVER_MINOR] Allow converting between progressive and interlaced images ([#3](https://github.com/shssoichiro/oxipng/issues/3))
|
||||
- Fix a bug that would cause oxipng to crash on very small images
|
||||
## Version 9.1.3
|
||||
|
||||
**Version 0.2.2**
|
||||
- Limit number of threads to 1.5x number of cores
|
||||
- Significantly improve memory usage, especially with high optimization levels. ([#32](https://github.com/shssoichiro/oxipng/issues/32))
|
||||
- Refactor output code ([#19](https://github.com/shssoichiro/oxipng/issues/19))
|
||||
- [Feature] Add `--zi` option to control the number of Zopfli iterations.
|
||||
- [Improvement] Allow setting compression level to 0.
|
||||
- [Performance] Improve filtering performance for some images.
|
||||
- [Build] Move man page generation to an xtask.
|
||||
|
||||
**Version 0.2.1**
|
||||
- Add rustdoc for public methods and structs
|
||||
- Improve filter mode 5 heuristic ([#16](https://github.com/shssoichiro/oxipng/issues/16))
|
||||
- Add tests for edge-case images with subtitles ([#29](https://github.com/shssoichiro/oxipng/issues/29))
|
||||
## Version 9.1.2
|
||||
|
||||
**Version 0.2.0**
|
||||
- Fix program version that is displayed when running `oxipng -V`
|
||||
- Ensure `--quiet` mode is actually quiet (@SethDusek [#20](https://github.com/shssoichiro/oxipng/pull/20))
|
||||
- Write status/debug information to stderr instead of stdout
|
||||
- Use heuristics to determine best combination for `-o1` ([#21](https://github.com/shssoichiro/oxipng/issues/21))
|
||||
- [SEMVER_MAJOR] Allow 'safe', 'all', or comma-separated list as options for `--strip`
|
||||
- [SEMVER_MINOR] Add `-s` alias for `--strip`
|
||||
- [Bugfix] Fix `--nx` still applying deinterlacing by default.
|
||||
- [Bugfix] Fix wildcard matching being case-sensitive on Windows.
|
||||
- [Bugfix] Fix optimized APNGs not being compatible with some programs.
|
||||
- [Build] Fix feature `sanity-checks` not working without `parallel`.
|
||||
- [Misc] Resolve ambiguity between optional dependencies and crate features.
|
||||
|
||||
**Version 0.1.1**
|
||||
- Fix `oxipng *` writing all input files to one output file ([#15](https://github.com/shssoichiro/oxipng/issues/15))
|
||||
## Version 9.1.1
|
||||
|
||||
**Version 0.1.0**
|
||||
- Initial beta release
|
||||
- Reduce color type and bit depth
|
||||
- Recompress with zlib
|
||||
- Multithreading
|
||||
- Strip headers option
|
||||
- Backup file before writing option
|
||||
- Write to stdout option
|
||||
- [Build] Change man page generation path to resolve issue with cargo publish.
|
||||
|
||||
## Version 9.1.0
|
||||
|
||||
- [Improvement] Add `--keep display` equivalent to `--strip safe`.
|
||||
- [Improvement] Add modified zeng palette sorting method, improving optimization of indexed images.
|
||||
- [Improvement] If only one filter is specified, guarantee to only use this one.
|
||||
- [Improvement] Evaluate low-depth indexed even if low-depth grayscale was already achieved.
|
||||
- [Bugfix] Fix battiato palette sorting method not being used if the input was not already indexed.
|
||||
- [Bugfix] Fix rare crash caused by a truncated palette.
|
||||
- [Build] Reduce size of binaries.
|
||||
- [Build] Add man page generation.
|
||||
- [Build] Publish deb archives for Linux.
|
||||
- [Misc] Bump minimum Rust version to 1.74.0.
|
||||
|
||||
## Version 9.0.0
|
||||
|
||||
- [Breaking] Remove `--backup` option. Use `--out` or `--dir` to preserve existing files.
|
||||
- [Breaking] Remove `--check` option. Use `--nx --nz` to perform a non-optimizing run.
|
||||
- [Breaking] API: Replace `pretend` option with `OutFile::None`.
|
||||
- [Breaking] API: Move `preserve_attrs` into `OutFile::Path`.
|
||||
- [Breaking] Default to removing interlacing. Use `-i keep` to retain existing interlacing.
|
||||
- [Feature] Add Raw API for creating optimised PNGs from raw image data.
|
||||
- [Feature] Add basic support for APNG files.
|
||||
- [Feature] Add `--scale16` option to forcibly reduce 16-bit images to 8-bit.
|
||||
- [Improvement] Process multiple files in parallel.
|
||||
- [Improvement] Improve reductions, particularly for indexed or very small images.
|
||||
- [Improvement] Improve compression with latest version of libdeflate.
|
||||
- [Improvement] Recompress iCCP chunks.
|
||||
- [Improvement] Change recursive mode to only process .png/.apng files.
|
||||
- [Improvement] Add support for glob patterns in quotes on Windows.
|
||||
- [Improvement] Quieter default output logging, with multiple levels of verbosity.
|
||||
- [Bugfix] Fix deadlock when using oxipng within an existing Rayon thread pool.
|
||||
- [Bugfix] Fix early abort in recursive mode when a read error occurred.
|
||||
- [Bugfix] Fix losing aux chunks when there's more than one of the same type.
|
||||
- [Bugfix] Fix sometimes writing output even when it was larger.
|
||||
- [Misc] Revamp CI workflow to upload artifacts and generate binaries for additional architectures.
|
||||
- [Misc] Bump minimum Rust version to 1.66.0.
|
||||
|
||||
## Version 8.0.0
|
||||
|
||||
- [Breaking] Revamp alpha optimization
|
||||
- [Bugfix] Fix grayscale depth reduction with tRNS pixel
|
||||
- [Bugfix] Fix fast mode with zopfli
|
||||
- [Improvement] Tweaks to interlacing and format display
|
||||
- [Improvement] Ability to reduce alpha channel to tRNS pixel
|
||||
- [Improvement] Performance improvements to colorspace reduction
|
||||
- [Misc] Disable image validation in release mode, as it is deemed no longer necessary
|
||||
|
||||
## Version 7.0.0
|
||||
|
||||
- [Breaking] Switch to libdeflater as the default
|
||||
- [Breaking] Rebalance presets
|
||||
- [Breaking] Update safe headers to remove
|
||||
- [Breaking] Bump minimum Rust version to 1.61.0
|
||||
- [Feature] Introduce new filter strategies
|
||||
- [Feature] Allow setting libdeflate compression level
|
||||
- [Feature] Add optional dockerization
|
||||
- [Improvement] Improve alpha reductions
|
||||
- [Improvement] Fast deinterlace for 8-bit
|
||||
- [Improvement] Refactor heuristics for fast filter evaluation
|
||||
- [Improvement] Improve file size reduction further
|
||||
- [Bugfix] Avoid printing output bytes on decompress error
|
||||
- [Bugfix] Fix compression level 0
|
||||
- [Bugfix] Fix directional alpha reductions
|
||||
- [Bugfix] Fix edge cases in palette reduction
|
||||
|
||||
### Version 6.0.1
|
||||
|
||||
- [Bugfix] Fix an issue where Zopfli mode could generate corrupt images
|
||||
|
||||
### Version 6.0.0
|
||||
|
||||
- [Breaking] Bump minimum Rust version to 1.57.0
|
||||
- [Feature] Add `--check`/`-c` CLI option
|
||||
- [Security] Update stderrlog to 0.5.2 (Fixes RUSTSEC-2022-0006)
|
||||
- [Security] Remove chrono as a transitive dependency (Fixes RUSTSEC-2020-0159)
|
||||
- [Misc] Bump `clap` to 3.2
|
||||
- [Misc] Bump `zopfli` to 0.7
|
||||
- [Misc] Bump `libdeflater` to 0.10
|
||||
- [Misc] Remove `byteorder` dependency in favor of stdlib functions
|
||||
- [Misc] Bump `image` to 0.24
|
||||
- [Misc] Bump `crc` to 3.0
|
||||
- [Misc] Bump `miniz_oxide` to 0.6
|
||||
- [Misc] Update to Rust edition 2021
|
||||
- [Misc] Various internal improvements
|
||||
|
||||
### Version 5.0.1
|
||||
|
||||
- [Bugfix] Fix an issue where -o5 and -o6 were not testing all options
|
||||
- [Misc] Bump miniz_oxide to 0.5
|
||||
|
||||
### Version 5.0.0
|
||||
|
||||
- [Breaking] Bump minimum Rust version to 1.46.0
|
||||
- [Bugfix] Avoid many panics that may occur on broken files
|
||||
- [Bugfix] Fix race condition that may cause tests to fail
|
||||
- [Performance] Optimizations to unfiltering and copying
|
||||
- [Feature] Add `--ng` option to skip grayscale reduction
|
||||
- [Feature] Also preserve timestamps when using `--preserve`
|
||||
- [Misc] Bump `crc` to 2.x
|
||||
|
||||
### Version 4.0.3
|
||||
|
||||
- Bump itertools to 0.10.x
|
||||
- Temporarily disable i686 releases, which were failing due to an odd linker issue,
|
||||
so that at least amd64 builds will publish successfully
|
||||
- This only relates to the releases published on Github. You can still manually compile oxipng for any platform.
|
||||
|
||||
### Version 4.0.2
|
||||
|
||||
- Includes another update to libdeflater that improves support for targets
|
||||
without a C stdlib, like wasm32.
|
||||
|
||||
### Version 4.0.1
|
||||
|
||||
- Includes an update to libdeflater that improves support for targets
|
||||
without a C stdlib, like wasm32.
|
||||
|
||||
### Version 4.0.0
|
||||
|
||||
- [Breaking] Bump minimum Rust version to 1.45.0
|
||||
- [Feature] Make `libdeflater` and `zopfli` optional for API users
|
||||
- [Bugfix] Fix cloudflare-zlib on aarch64 CPUs
|
||||
- [Bugfix] Don't exit on finding a non-PNG file when crawling recursively
|
||||
- [Bugfix] Make `rayon` truly optional
|
||||
- Various internal improvements
|
||||
|
||||
### Version 3.0.1
|
||||
|
||||
- [Bugfix] Re-add `--force` flag to CLI
|
||||
- This was accidentally removed somehow
|
||||
- Many non-breaking dependency version bumps
|
||||
|
||||
### Version 3.0.0
|
||||
|
||||
- [Breaking] Bump minimum Rust version to 1.41.0
|
||||
- [Breaking] Use IndexMap/IndexSet to provide more consistent performance ([#202](https://github.com/oxipng/oxipng/pull/202))
|
||||
- This changes some public-facing types.
|
||||
`IndexMap` and `IndexSet` are reexported
|
||||
at the crate root to aid migration.
|
||||
- [Breaking] Remove fields from the `Options` struct which were never used ([#211](https://github.com/oxipng/oxipng/pull/211/files#diff-b4aea3e418ccdb71239b96952d9cddb6L217), [#212](https://github.com/oxipng/oxipng/pull/212/files#diff-b4aea3e418ccdb71239b96952d9cddb6L134))
|
||||
- [Breaking] Refactor zlib-specific options in the `Options` struct ([#210](https://github.com/oxipng/oxipng/pull/210/files))
|
||||
- [Feature] Add libdeflater as an option ([#203](https://github.com/oxipng/oxipng/pull/203))
|
||||
- [Feature] Use standard `log` library ([#218](https://github.com/oxipng/oxipng/pull/218))
|
||||
- [Feature] Add `-o max` setting which will always reference the highest compression preset ([#224](https://github.com/oxipng/oxipng/pull/224))
|
||||
- [Deprecated] `-o 4` was found to be equivalent to `-o 3` and is deprecated.
|
||||
It will likely be removed in a future release.
|
||||
For now it remains equivalent to `-o 3`. ([#224](https://github.com/oxipng/oxipng/pull/224))
|
||||
- [Bugfix] Ensure output is deterministic ([#199](https://github.com/oxipng/oxipng/pull/199))
|
||||
- Update `image` crate to 0.23
|
||||
- Update `itertools` crate to 0.9
|
||||
- Various performance and internal improvements
|
||||
|
||||
### Version 2.3.0
|
||||
|
||||
- Allow disabling all alpha optimizations ([#181](https://github.com/oxipng/oxipng/pull/181))
|
||||
- Fix interlacing issues on tiny images ([#182](https://github.com/oxipng/oxipng/pull/182))
|
||||
- Reduce memory usage in filtering ([#191](https://github.com/oxipng/oxipng/pull/191))
|
||||
- Implement palette sorting to improve compression ([#193](https://github.com/oxipng/oxipng/pull/193))
|
||||
- Disable alpha optimizations by default ([#187](https://github.com/oxipng/oxipng/pull/187))
|
||||
- Add support for WASM ([#194](https://github.com/oxipng/oxipng/pull/194))
|
||||
|
||||
### Version 2.2.2
|
||||
|
||||
- Fix grayscale bit-depth reduction ([#171](https://github.com/oxipng/oxipng/pull/171))
|
||||
- Fix typos and incorrect log message ([#172](https://github.com/oxipng/oxipng/pull/172))
|
||||
- Make metadata order deterministic ([#174](https://github.com/oxipng/oxipng/pull/174))
|
||||
- Fix 32-bit builds ([#176](https://github.com/oxipng/oxipng/pull/176))
|
||||
- Enable LTO in release builds ([#177](https://github.com/oxipng/oxipng/pull/177))
|
||||
- Use deterministic compression strategy ([#179](https://github.com/oxipng/oxipng/pull/179))
|
||||
- Fix decoding interlaced images with height or width <= 2 ([#175](https://github.com/oxipng/oxipng/pull/175))
|
||||
- Preallocate memory in reduced_alpha_to_up ([#180](https://github.com/oxipng/oxipng/pull/180))
|
||||
- Update `bit-vec` crate to 0.6
|
||||
|
||||
### Version 2.2.1
|
||||
|
||||
- Fix compression of very large files ([#167](https://github.com/oxipng/oxipng/pull/167)) ([#168](https://github.com/oxipng/oxipng/pull/168))
|
||||
|
||||
### Version 2.2.0
|
||||
|
||||
- Various internal improvements ([#154](https://github.com/oxipng/oxipng/pull/154)) ([#158](https://github.com/oxipng/oxipng/pull/158)) ([#160](https://github.com/oxipng/oxipng/pull/160)) ([#161](https://github.com/oxipng/oxipng/pull/161)) ([#162](https://github.com/oxipng/oxipng/pull/162)) ([#163](https://github.com/oxipng/oxipng/pull/163))
|
||||
- Update `image` crate to 0.21.0
|
||||
- Update `itertools` crate to 0.8.0
|
||||
- Update `zopfli` crate to 0.4.0
|
||||
- Use Rust edition 2018
|
||||
- Bump minimum required Rust version to 1.31.0
|
||||
|
||||
### Version 2.1.8
|
||||
|
||||
- Fix non-standard sBIT headers in other code locations ([#153](https://github.com/oxipng/oxipng/issues/153))
|
||||
|
||||
### Version 2.1.7
|
||||
|
||||
- 80x faster palette reduction ([#150](https://github.com/oxipng/oxipng/pull/150))
|
||||
- Optimize RGB to palette conversion ([#148](https://github.com/oxipng/oxipng/pull/148))
|
||||
- Various microoptimizations ([#146](https://github.com/oxipng/oxipng/pull/146))
|
||||
- Introduce third-party safe wrapper around cloudflare-zlib ([#149](https://github.com/oxipng/oxipng/pull/149))
|
||||
|
||||
### Version 2.1.6
|
||||
|
||||
- Identify and drop useless sRGB profiles ([#143](https://github.com/oxipng/oxipng/pull/143))
|
||||
- Alpha heuristic improvements ([#144](https://github.com/oxipng/oxipng/pull/144))
|
||||
- Bump `miniz_oxide` and `cloudflare-zlib-sys` to 0.2.0
|
||||
|
||||
### Version 2.1.5
|
||||
|
||||
- Fix issue where some images will incorrectly reduce bit depth ([#140](https://github.com/oxipng/oxipng/issues/140))
|
||||
|
||||
### Version 2.1.4
|
||||
|
||||
- Bump `image` crate to 0.20.0
|
||||
|
||||
### Version 2.1.3
|
||||
|
||||
- Fix i686 builds
|
||||
- Performance improvements
|
||||
|
||||
### Version 2.1.2
|
||||
|
||||
- Fix issue with PNG to Indexed reduction on images with transparency pixel ([#129](https://github.com/oxipng/oxipng/issues/129))
|
||||
|
||||
### Version 2.1.1
|
||||
|
||||
- More fixes for alpha optimization on interlaced images ([#133](https://github.com/oxipng/oxipng/issues/133))
|
||||
|
||||
### Version 2.1.0
|
||||
|
||||
- [SEMVER_MINOR] Bump minimum Rust version to 1.27.0
|
||||
- [SEMVER_MINOR] Reenable faster Cloudflare zlib compression on platforms that support it
|
||||
- Fix memory leak with Cloudflare zlib ([#126](https://github.com/oxipng/oxipng/issues/126))
|
||||
- Minor fixes and cleanup
|
||||
|
||||
### Version 2.0.2
|
||||
|
||||
- Fix an issue in alpha optimization on interlaced images ([#113](https://github.com/oxipng/oxipng/issues/113))
|
||||
|
||||
### Version 2.0.1
|
||||
|
||||
- Revert making Cloudflare zlib the default, as it introduced a major memory leak. It will be put back behind a feature flag, and reenabled when the issue is fixed.
|
||||
- Revert minimum Rust version to 1.24.0
|
||||
|
||||
### Version 2.0.0
|
||||
|
||||
- [SEMVER_MAJOR] Make PngError a proper Error enum
|
||||
- [SEMVER_MINOR] Bump minimum Rust version to 1.27.0
|
||||
- [SEMVER_MINOR] Make Rayon an optional dependency (enabled by default)
|
||||
- [SEMVER_MINOR] Option to limit wall clock time spent in optimization trials
|
||||
- [SEMVER_MINOR] `--keep` option (works similar to `--strip`, but takes a comma-separated list of headers to keep, and removes all other non-critical headers)
|
||||
- Use faster Cloudflare zlib compression on platforms that support it
|
||||
- Allow specifying more than 2 filter types via the CLI
|
||||
- Avoid double glob processing on unix
|
||||
- Fix reading from stdin
|
||||
- Cleanup help text
|
||||
- Various performance improvements
|
||||
|
||||
### Version 1.0.4
|
||||
|
||||
- Bump `image` to 0.19.0
|
||||
- Bump `bit-vec` to 0.5.0
|
||||
- Bump `regex` to 1.0.0
|
||||
|
||||
### Version 1.0.3
|
||||
|
||||
- Bump dependencies
|
||||
|
||||
### Version 1.0.2
|
||||
|
||||
- Fix a change that breaks Itertools::flatten with recent Rust nightlies
|
||||
|
||||
### Version 1.0.1
|
||||
|
||||
- Bump rayon to 1.0 ([#99](https://github.com/oxipng/oxipng/pull/99) @cuviper)
|
||||
- Bump minor versions of other dependencies for binary distribution
|
||||
|
||||
### Version 1.0.0
|
||||
|
||||
- Remove the C dependency on miniz, and replace it with a Rust version ([#57](https://github.com/oxipng/oxipng/issues/57))
|
||||
- This improves decompression speed by 15%. Compression speed is not affected.
|
||||
- [SEMVER_MAJOR] This also obsoletes the `-zm` command line option and the `memory` key on the `Options` struct.
|
||||
- Presets will be updated automatically. This means that presets 3 and higher will run significantly more quickly.
|
||||
- [SEMVER_MAJOR] Adjust the presets, now that `-zm` is no longer an option.
|
||||
- `-o3` now tests all filter types. This will result in 50% more trials than before, but may give up to 10% more compression gain.
|
||||
- `-o4` and higher now test all alpha optimization types. This adds 5 trials specific to the alpha channel. Only transparent images are affected.
|
||||
- Implement unix-specific permissions copying for `-p` option
|
||||
- Performance optimizations
|
||||
- Refactor of internal code
|
||||
|
||||
### Version 0.19.0
|
||||
|
||||
- [SEMVER_MAJOR] Default to overwriting the input file if `out_file` is not set.
|
||||
This does not affect the CLI, but with the library, it was easy to forget to set the `out_file`,
|
||||
and there was no warning that no output file would be written.
|
||||
- Bump dependencies, reduces binary size by a considerable amount
|
||||
- Hide all modules from documentation, and only export the specific structures that should be public.
|
||||
Previously there were too many implementation details made public. The modules are still public for the purposes of our integration tests,
|
||||
but we strongly advise against using undocumented modules. These may become private in the future.
|
||||
- Internal refactoring and code cleanup
|
||||
- Fix an error message that was displaying the wrong file path
|
||||
- Fix an issue where the output file would not be written if the input was already optimized,
|
||||
even if the output path was different from the input path
|
||||
|
||||
### Version 0.18.3
|
||||
|
||||
- Return exit code of 1 if an error occurred while processing a file using the CLI app ([#93](https://github.com/oxipng/oxipng/issues/93))
|
||||
|
||||
### Version 0.18.2
|
||||
|
||||
- Bump `image` to 0.18
|
||||
- Fix unfiltering of scan lines in interlaced images ([#92](https://github.com/oxipng/oxipng/issues/92))
|
||||
|
||||
### Version 0.18.1
|
||||
|
||||
- Bump `rayon` to 0.9
|
||||
- Fix failure to optimize on certain grayscale images ([#89](https://github.com/oxipng/oxipng/issues/89))
|
||||
|
||||
### Version 0.18.0
|
||||
|
||||
- Bump `itertools` to 0.7
|
||||
- Bump `image` to 0.17
|
||||
- [SEMVER_MAJOR] Bump minimum rustc version to 1.20.0
|
||||
- Fix parsing of glob paths on Windows ([#90](https://github.com/oxipng/oxipng/issues/90))
|
||||
|
||||
### Version 0.17.2
|
||||
|
||||
- Bump `image` to 0.16
|
||||
- Quickly pass over files that do not have a PNG header ([#85](https://github.com/oxipng/oxipng/issues/85) @emielbeinema)
|
||||
- Return an error instead of crashing on APNG files ([#83](https://github.com/oxipng/oxipng/issues/83) @emielbeinema)
|
||||
|
||||
### Version 0.17.1
|
||||
|
||||
- Remove VC++ download requirement for Windows users
|
||||
|
||||
### Version 0.17.0
|
||||
|
||||
- [SEMVER_MAJOR] Bump minimum required rustc version to 1.19.0
|
||||
- [SEMVER_MINOR] Oxipng will now, by default, attempt to change all transparent pixels to `rgba(0, 0, 0, 0)` to improve compression.
|
||||
It does fast trials with filters 0 and 5 to see if this is an improvement over
|
||||
the existing alpha channel.
|
||||
- [SEMVER_MINOR] Add a `-a` option to the command line (`alphas` in the struct) which enables 6 different
|
||||
trials for optimizing the alpha channel, using the previously mentioned fast heuristic.
|
||||
This option will make optimization of images with transparency somewhat slower,
|
||||
but may improve compression.
|
||||
- Fixed a bug in reducing palettes for images with bit depth of two ([#80](https://github.com/oxipng/oxipng/issues/80))
|
||||
- Fixed another bug in reducing palettes for images with bit depth less than eight ([#82](https://github.com/oxipng/oxipng/issues/82))
|
||||
- Code cleanup
|
||||
- Bump `image` to 0.15
|
||||
|
||||
### Version 0.16.3
|
||||
|
||||
- Fix command-line help text ([#70](https://github.com/oxipng/oxipng/issues/70))
|
||||
|
||||
### Version 0.16.2
|
||||
|
||||
- Publicly export `error` module
|
||||
|
||||
### Version 0.16.1
|
||||
|
||||
- Fix rayon's breaking changes that they made in a point release
|
||||
|
||||
### Version 0.16.0
|
||||
|
||||
- [SEMVER_MAJOR] Bump minimum rustc version to 1.17.0
|
||||
- Bump `image` to 0.14
|
||||
- Bump `rayon` to 0.8
|
||||
|
||||
### Version 0.15.2
|
||||
|
||||
- Bump `image` to 0.13 ([#65](https://github.com/oxipng/oxipng/pull/65))
|
||||
- Bump `rayon` to 0.7
|
||||
- Bump `itertools` to 0.6
|
||||
|
||||
### Version 0.15.1
|
||||
|
||||
- Ignore color reductions that would increase file size ([#61](https://github.com/oxipng/oxipng/issues/61))
|
||||
|
||||
### Version 0.15.0
|
||||
|
||||
- [SEMVER_MINOR] Check images for correctness before writing result ([#60](https://github.com/oxipng/oxipng/issues/60))
|
||||
- Fix invalid output when reducing image to a different color type but file size does not improve ([#60](https://github.com/oxipng/oxipng/issues/60))
|
||||
- Don't write new file if moving from interlaced to non-interlaced if new file would be larger
|
||||
|
||||
### Version 0.14.4
|
||||
|
||||
- Fix bug when reducing RGBA to Indexed if image has 256 colors plus a background color
|
||||
|
||||
### Version 0.14.3
|
||||
|
||||
- Fix multiple bugs when reducing transparency palettes
|
||||
|
||||
### Version 0.14.2
|
||||
|
||||
- Fix a bug when reducing palette in images with bit depth less than 8
|
||||
- Fix a bug when reducing palette in images with transparency
|
||||
|
||||
### Version 0.14.1
|
||||
|
||||
- Remove zlib dependency and switch entirely to miniz, since zlib 1.2.11 was not working with oxipng. This costs some performance, but is better than having a broken application.
|
||||
|
||||
### Version 0.14.0
|
||||
|
||||
- Performance optimizations
|
||||
- [SEMVER_MAJOR] Bump minimum rustc version to 1.13.0
|
||||
- Add categories on crates.io
|
||||
|
||||
### Version 0.13.1
|
||||
|
||||
- Bump regex dependency to 0.2
|
||||
- Bump byteorder dependency to 1.0
|
||||
- Bump rayon dependency to 0.6
|
||||
|
||||
### Version 0.13.0
|
||||
|
||||
- Fix bug in certain PNG headers when reducing color type ([#52](https://github.com/oxipng/oxipng/issues/52))
|
||||
- [SEMVER_MAJOR] Reduction functions now take `&mut PngData` and return a `bool` indicating whether the image was reduced
|
||||
- [SMEVER_MAJOR] Bump minimum required rustc version to 1.12.0
|
||||
|
||||
### Version 0.12.0
|
||||
|
||||
- Performance optimizations
|
||||
- Fix processing filenames that contain commas (@aliceatlas [#50](https://github.com/oxipng/oxipng/pull/50))
|
||||
- [SEMVER_MINOR] Add zopfli option (-Z), disabled by default. Gives about 10% better compression, but is currently 50-100x slower.
|
||||
|
||||
### Version 0.11.0
|
||||
|
||||
- [SEMVER_MAJOR] Bump minimum rustc version to 1.9.0, required by dependencies
|
||||
- [SEMVER_MINOR] Allow calling optimization presets via crate using `Options::from_preset`
|
||||
- [SEMVER_MAJOR] Return proper `PngError` type which implements `std::error::Error` from `Result`s
|
||||
- [SEMVER_MAJOR] Rename module `deflate::deflate` to `deflate`
|
||||
- Performance optimizations
|
||||
|
||||
### Version 0.10.0
|
||||
|
||||
- [SEMVER_MINOR] Make clap and regex dependencies optional
|
||||
- Enabled by default, needed for executable build; can be disabled for use in crates
|
||||
- Remove reduction from palette to grayscale, which was not working and provided minimal benefit
|
||||
|
||||
### Version 0.9.0
|
||||
|
||||
- [SEMVER_MAJOR] Significant refactoring of modules
|
||||
- Use `itertools` to cleanup areas of code
|
||||
- Use multiple threads for filtering trials
|
||||
|
||||
### Version 0.8.2
|
||||
|
||||
- Fix issue where images smaller than 4px width would crash on interlacing ([#42](https://github.com/oxipng/oxipng/issues/42))
|
||||
|
||||
### Version 0.8.1
|
||||
|
||||
- Minor optimizations
|
||||
- Fix issue where interlaced images with certain widths would fail to optimize
|
||||
|
||||
### Version 0.8.0
|
||||
|
||||
- [SEMVER_MINOR] Add support for optimizing PNGs already loaded into memory via library function
|
||||
|
||||
### Version 0.7.0
|
||||
|
||||
- Minor compression improvement on interlaced images
|
||||
- Performance optimizations
|
||||
- [SEMVER_MINOR] Move default Options into a Default impl
|
||||
- [SEMVER_MINOR] Add option for setting number of threads ([#39](https://github.com/oxipng/oxipng/issues/39))
|
||||
|
||||
### Version 0.6.0
|
||||
|
||||
- Fix issue where output directory would not be created if it did not exist
|
||||
- Use miniz for compression strategies where it outperforms zlib
|
||||
- [SEMVER_MINOR] Partially implement -p / --preserve, as far as stable Rust will allow for now
|
||||
- [SEMVER_MINOR] Implement --fix to ignore CRC errors and recalculate correct CRC in output
|
||||
|
||||
### Version 0.5.0
|
||||
|
||||
- [SEMVER_MINOR] Palette entries can now reduced, on by default ([#11](https://github.com/oxipng/oxipng/issues/11))
|
||||
- Don't report that we are in pretend mode if verbosity is set to none
|
||||
- Add cargo bench suite ([#7](https://github.com/oxipng/oxipng/issues/7))
|
||||
|
||||
### Version 0.4.0
|
||||
|
||||
- Performance optimizations
|
||||
- [SEMVER_MAJOR] `-s` automatically infers `--strip safe` ([#31](https://github.com/oxipng/oxipng/issues/31))
|
||||
- Update byteorder and clap crates
|
||||
- Fix issue where interlaced images incorrectly applied filters on the first line of a pass
|
||||
|
||||
### Version 0.3.0
|
||||
|
||||
- Properly decode interlaced images
|
||||
- [SEMVER_MINOR] Allow converting between progressive and interlaced images ([#3](https://github.com/oxipng/oxipng/issues/3))
|
||||
- Fix a bug that would cause oxipng to crash on very small images
|
||||
|
||||
### Version 0.2.2
|
||||
|
||||
- Limit number of threads to 1.5x number of cores
|
||||
- Significantly improve memory usage, especially with high optimization levels. ([#32](https://github.com/oxipng/oxipng/issues/32))
|
||||
- Refactor output code ([#19](https://github.com/oxipng/oxipng/issues/19))
|
||||
|
||||
### Version 0.2.1
|
||||
|
||||
- Add rustdoc for public methods and structs
|
||||
- Improve filter mode 5 heuristic ([#16](https://github.com/oxipng/oxipng/issues/16))
|
||||
- Add tests for edge-case images with subtitles ([#29](https://github.com/oxipng/oxipng/issues/29))
|
||||
|
||||
### Version 0.2.0
|
||||
|
||||
- Fix program version that is displayed when running `oxipng -V`
|
||||
- Ensure `--quiet` mode is actually quiet (@SethDusek [#20](https://github.com/oxipng/oxipng/pull/20))
|
||||
- Write status/debug information to stderr instead of stdout
|
||||
- Use heuristics to determine best combination for `-o1` ([#21](https://github.com/oxipng/oxipng/issues/21))
|
||||
- [SEMVER_MAJOR] Allow 'safe', 'all', or comma-separated list as options for `--strip`
|
||||
- [SEMVER_MINOR] Add `-s` alias for `--strip`
|
||||
|
||||
### Version 0.1.1
|
||||
|
||||
- Fix `oxipng *` writing all input files to one output file ([#15](https://github.com/oxipng/oxipng/issues/15))
|
||||
|
||||
### Version 0.1.0
|
||||
|
||||
- Initial beta release
|
||||
- Reduce color type and bit depth
|
||||
- Recompress with zlib
|
||||
- Multithreading
|
||||
- Strip headers option
|
||||
- Backup file before writing option
|
||||
- Write to stdout option
|
||||
|
|
|
|||
775
Cargo.lock
generated
|
|
@ -1,474 +1,649 @@
|
|||
[root]
|
||||
name = "oxipng"
|
||||
version = "0.10.0"
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "adler2"
|
||||
version = "2.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa"
|
||||
|
||||
[[package]]
|
||||
name = "anstream"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "824a212faf96e9acacdbd09febd34438f8f711fb84e09a8916013cd7815ca28d"
|
||||
dependencies = [
|
||||
"bit-vec 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"byteorder 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"clap 2.10.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"clippy 0.0.80 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"crc 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"image 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"itertools 0.4.17 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libz-sys 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"miniz-sys 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"num_cpus 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"regex 0.1.73 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"scoped-pool 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"anstyle",
|
||||
"anstyle-parse",
|
||||
"anstyle-query",
|
||||
"anstyle-wincon",
|
||||
"colorchoice",
|
||||
"is_terminal_polyfill",
|
||||
"utf8parse",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "0.5.2"
|
||||
name = "anstyle"
|
||||
version = "1.0.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000"
|
||||
|
||||
[[package]]
|
||||
name = "anstyle-parse"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "52ce7f38b242319f7cabaa6813055467063ecdc9d355bbb4ce0c68908cd8130e"
|
||||
dependencies = [
|
||||
"memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"utf8parse",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ansi_term"
|
||||
version = "0.8.0"
|
||||
name = "anstyle-query"
|
||||
version = "1.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc"
|
||||
dependencies = [
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bit-vec"
|
||||
version = "0.4.3"
|
||||
name = "anstyle-wincon"
|
||||
version = "3.0.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d"
|
||||
dependencies = [
|
||||
"anstyle",
|
||||
"once_cell_polyfill",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "0.7.0"
|
||||
version = "2.11.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c4512299f36f043ab09a583e57bceb5a5aab7a73db1805848e8fef3c9e8c78b3"
|
||||
|
||||
[[package]]
|
||||
name = "byteorder"
|
||||
version = "0.5.3"
|
||||
name = "bitvec"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1bc2832c24239b0141d5674bb9174f9d68a8b5b3f2753311927c172ca46f7e9c"
|
||||
dependencies = [
|
||||
"funty",
|
||||
"radium",
|
||||
"tap",
|
||||
"wyz",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bumpalo"
|
||||
version = "3.20.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5d20789868f4b01b2f2caec9f5c4e0213b41e3e5702a50157d699ae31ced2fcb"
|
||||
|
||||
[[package]]
|
||||
name = "bytemuck"
|
||||
version = "1.25.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c8efb64bd706a16a1bdde310ae86b351e4d21550d98d056f22f8a7f7a2183fec"
|
||||
|
||||
[[package]]
|
||||
name = "byteorder-lite"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8f1fe948ff07f4bd06c30984e69f5b4899c516a3ef74f34df92a2df2ab535495"
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.2.60"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "43c5703da9466b66a946814e1adf53ea2c90f10063b86290cc9eb67ce3478a20"
|
||||
dependencies = [
|
||||
"find-msvc-tools",
|
||||
"shlex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
|
||||
|
||||
[[package]]
|
||||
name = "clap"
|
||||
version = "2.10.0"
|
||||
version = "4.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1ddb117e43bbf7dacf0a4190fef4d345b9bad68dfc649cb349e7d17d28428e51"
|
||||
dependencies = [
|
||||
"ansi_term 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"strsim 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"term_size 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"unicode-width 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"vec_map 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"clap_builder",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clippy"
|
||||
version = "0.0.80"
|
||||
name = "clap_builder"
|
||||
version = "4.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "714a53001bf66416adb0e2ef5ac857140e7dc3a0c48fb28b2f10762fc4b5069f"
|
||||
dependencies = [
|
||||
"clippy_lints 0.0.80 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"anstream",
|
||||
"anstyle",
|
||||
"clap_lex",
|
||||
"strsim",
|
||||
"terminal_size",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clippy_lints"
|
||||
version = "0.0.80"
|
||||
name = "clap_lex"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c8d4a3bb8b1e0c1050499d1815f5ab16d04f0959b233085fb31653fbfc9d98f9"
|
||||
|
||||
[[package]]
|
||||
name = "colorchoice"
|
||||
version = "1.0.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570"
|
||||
|
||||
[[package]]
|
||||
name = "crc32fast"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9481c1c90cbf2ac953f07c8d4a58aa3945c425b7185c9154d67a65e4230da511"
|
||||
dependencies = [
|
||||
"matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"quine-mc_cluskey 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"regex-syntax 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"semver 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"toml 0.1.30 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"unicode-normalization 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crc"
|
||||
version = "1.3.0"
|
||||
name = "crossbeam-deque"
|
||||
version = "0.8.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9dd111b7b7f7d55b72c0a6ae361660ee5853c9af73f70c3c2ef6858b950e2e51"
|
||||
dependencies = [
|
||||
"lazy_static 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"crossbeam-epoch",
|
||||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam"
|
||||
version = "0.2.9"
|
||||
name = "crossbeam-epoch"
|
||||
version = "0.9.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e"
|
||||
dependencies = [
|
||||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "enum_primitive"
|
||||
version = "0.1.0"
|
||||
name = "crossbeam-utils"
|
||||
version = "0.8.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28"
|
||||
|
||||
[[package]]
|
||||
name = "either"
|
||||
version = "1.15.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719"
|
||||
|
||||
[[package]]
|
||||
name = "env_filter"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "32e90c2accc4b07a8456ea0debdc2e7587bdd890680d71173a15d4ae604f6eef"
|
||||
dependencies = [
|
||||
"num 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"log",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "env_logger"
|
||||
version = "0.11.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0621c04f2196ac3f488dd583365b9c09be011a4ab8b9f37248ffcc8f6198b56a"
|
||||
dependencies = [
|
||||
"anstream",
|
||||
"anstyle",
|
||||
"env_filter",
|
||||
"log",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "equivalent"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f"
|
||||
|
||||
[[package]]
|
||||
name = "errno"
|
||||
version = "0.3.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fdeflate"
|
||||
version = "0.3.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1e6853b52649d4ac5c0bd02320cddc5ba956bdb407c4b75a2c6b75bf51500f8c"
|
||||
dependencies = [
|
||||
"simd-adler32",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "find-msvc-tools"
|
||||
version = "0.1.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582"
|
||||
|
||||
[[package]]
|
||||
name = "flate2"
|
||||
version = "0.2.14"
|
||||
version = "1.1.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "843fba2746e448b37e26a819579957415c8cef339bf08564fe8b7ddbd959573c"
|
||||
dependencies = [
|
||||
"libc 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"miniz-sys 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"crc32fast",
|
||||
"miniz_oxide",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "gcc"
|
||||
version = "0.3.32"
|
||||
name = "funty"
|
||||
version = "2.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c"
|
||||
|
||||
[[package]]
|
||||
name = "glob"
|
||||
version = "0.2.11"
|
||||
version = "0.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0cc23270f6e1808e30a928bdc84dea0b9b4136a8bc82338574f23baf47bbd280"
|
||||
|
||||
[[package]]
|
||||
name = "hashbrown"
|
||||
version = "0.17.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4f467dd6dccf739c208452f8014c75c18bb8301b050ad1cfb27153803edb0f51"
|
||||
|
||||
[[package]]
|
||||
name = "image"
|
||||
version = "0.10.1"
|
||||
version = "0.25.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e6506c6c10786659413faa717ceebcb8f70731c0a60cbae39795fdf114519c1a"
|
||||
dependencies = [
|
||||
"byteorder 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"enum_primitive 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"num-iter 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"num-rational 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"num-traits 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"png 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"bytemuck",
|
||||
"byteorder-lite",
|
||||
"moxcms",
|
||||
"num-traits",
|
||||
"png",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "inflate"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "itertools"
|
||||
version = "0.4.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "kernel32-sys"
|
||||
version = "0.2.2"
|
||||
name = "indexmap"
|
||||
version = "2.14.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d466e9454f08e4a911e14806c24e16fba1b4c121d1ea474396f396069cf949d9"
|
||||
dependencies = [
|
||||
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"equivalent",
|
||||
"hashbrown",
|
||||
"rayon",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "0.2.1"
|
||||
name = "is_terminal_polyfill"
|
||||
version = "1.70.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "1.0.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.14"
|
||||
version = "0.2.185"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "52ff2c0fe9bc6cb6b14a0592c2ff4fa9ceb83eea9db979b0487cd054946a2b8f"
|
||||
|
||||
[[package]]
|
||||
name = "libz-sys"
|
||||
version = "1.0.5"
|
||||
name = "libdeflate-sys"
|
||||
version = "1.25.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "72753e0008ea87963d2f0770042d0df7abe51fafbb8dcaf618ac440f2f1fec0a"
|
||||
dependencies = [
|
||||
"gcc 0.3.32 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"pkg-config 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"cc",
|
||||
"pkg-config",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "matches"
|
||||
version = "0.1.2"
|
||||
name = "libdeflater"
|
||||
version = "1.25.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d1ee41cf6fb1bb6030dfb59ffb7bc01ab26aade44142084c87f0fc7a1658fe71"
|
||||
dependencies = [
|
||||
"libdeflate-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "linux-raw-sys"
|
||||
version = "0.12.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53"
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.4.30"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "616ec5685824bcc94416c6d4a7a446eea774a31efd7062c8480ba6fd06d7a6e5"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "0.1.11"
|
||||
version = "2.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79"
|
||||
|
||||
[[package]]
|
||||
name = "miniz_oxide"
|
||||
version = "0.8.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1fa76a2c86f704bdb222d66965fb3d63269ce38518b83cb0575fca855ebb6316"
|
||||
dependencies = [
|
||||
"libc 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"adler2",
|
||||
"simd-adler32",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "miniz-sys"
|
||||
version = "0.1.7"
|
||||
name = "moxcms"
|
||||
version = "0.7.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ac9557c559cd6fc9867e122e20d2cbefc9ca29d80d027a8e39310920ed2f0a97"
|
||||
dependencies = [
|
||||
"gcc 0.3.32 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nom"
|
||||
version = "1.2.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "num"
|
||||
version = "0.1.34"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"num-iter 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"num-traits 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-bigint"
|
||||
version = "0.1.33"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"num-traits 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-integer"
|
||||
version = "0.1.32"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"num-traits 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-iter"
|
||||
version = "0.1.32"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"num-traits 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-rational"
|
||||
version = "0.1.32"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"num-bigint 0.1.33 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"num-traits 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"num-traits",
|
||||
"pxfm",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.1.34"
|
||||
version = "0.2.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num_cpus"
|
||||
version = "1.0.0"
|
||||
name = "once_cell_polyfill"
|
||||
version = "1.70.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"
|
||||
|
||||
[[package]]
|
||||
name = "oxipng"
|
||||
version = "10.1.1"
|
||||
dependencies = [
|
||||
"libc 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"bitvec",
|
||||
"clap",
|
||||
"env_logger",
|
||||
"glob",
|
||||
"image",
|
||||
"indexmap",
|
||||
"libdeflater",
|
||||
"log",
|
||||
"parse-size",
|
||||
"rayon",
|
||||
"rgb",
|
||||
"rustc-hash",
|
||||
"serde_json",
|
||||
"zopfli",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "parse-size"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "487f2ccd1e17ce8c1bfab3a65c89525af41cfad4c8659021a1e9a2aacd73b89b"
|
||||
|
||||
[[package]]
|
||||
name = "pkg-config"
|
||||
version = "0.3.8"
|
||||
version = "0.3.33"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "19f132c84eca552bf34cab8ec81f1c1dcc229b811638f9d283dceabe58c5569e"
|
||||
|
||||
[[package]]
|
||||
name = "png"
|
||||
version = "0.5.2"
|
||||
version = "0.18.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "60769b8b31b2a9f263dae2776c37b1b28ae246943cf719eb6946a1db05128a61"
|
||||
dependencies = [
|
||||
"bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"flate2 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"inflate 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"num-iter 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"bitflags",
|
||||
"crc32fast",
|
||||
"fdeflate",
|
||||
"flate2",
|
||||
"miniz_oxide",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quine-mc_cluskey"
|
||||
version = "0.2.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.3.14"
|
||||
name = "proc-macro2"
|
||||
version = "1.0.106"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934"
|
||||
dependencies = [
|
||||
"libc 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "0.1.73"
|
||||
name = "pxfm"
|
||||
version = "0.1.29"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e0c5ccf5294c6ccd63a74f1565028353830a9c2f5eb0c682c355c471726a6e3f"
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.45"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924"
|
||||
dependencies = [
|
||||
"aho-corasick 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"regex-syntax 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"simd 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"thread_local 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.3.4"
|
||||
name = "radium"
|
||||
version = "0.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09"
|
||||
|
||||
[[package]]
|
||||
name = "rustc-serialize"
|
||||
version = "0.3.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "scoped-pool"
|
||||
version = "1.0.0"
|
||||
name = "rayon"
|
||||
version = "1.12.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fb39b166781f92d482534ef4b4b1b2568f42613b53e5b6c160e24cfbfa30926d"
|
||||
dependencies = [
|
||||
"crossbeam 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"scopeguard 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"variance 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"either",
|
||||
"rayon-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "scopeguard"
|
||||
version = "0.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "semver"
|
||||
version = "0.2.3"
|
||||
name = "rayon-core"
|
||||
version = "1.13.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "22e18b0f0062d30d4230b2e85ff77fdfe4326feb054b9783a3460d8435c8ab91"
|
||||
dependencies = [
|
||||
"nom 1.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"crossbeam-deque",
|
||||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "simd"
|
||||
version = "0.1.1"
|
||||
name = "rgb"
|
||||
version = "0.8.53"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "47b34b781b31e5d73e9fbc8689c70551fd1ade9a19e3e28cfec8580a79290cc4"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustc-hash"
|
||||
version = "2.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "94300abf3f1ae2e2b8ffb7b58043de3d399c73fa6f4b73826402a5c457614dbe"
|
||||
|
||||
[[package]]
|
||||
name = "rustix"
|
||||
version = "1.1.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b6fe4565b9518b83ef4f91bb47ce29620ca828bd32cb7e408f0062e9930ba190"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"errno",
|
||||
"libc",
|
||||
"linux-raw-sys",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.228"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e"
|
||||
dependencies = [
|
||||
"serde_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_core"
|
||||
version = "1.0.228"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.228"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.150"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e8014e44b4736ed0538adeecded0fce2a272f22dc9578a7eb6b2d9993c74cfb9"
|
||||
dependencies = [
|
||||
"itoa",
|
||||
"memchr",
|
||||
"serde",
|
||||
"serde_core",
|
||||
"zmij",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "shlex"
|
||||
version = "1.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
|
||||
|
||||
[[package]]
|
||||
name = "simd-adler32"
|
||||
version = "0.3.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "703d5c7ef118737c72f1af64ad2f6f8c5e1921f818cdcb97b8fe6fc69bf66214"
|
||||
|
||||
[[package]]
|
||||
name = "strsim"
|
||||
version = "0.4.1"
|
||||
version = "0.11.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
|
||||
|
||||
[[package]]
|
||||
name = "term_size"
|
||||
version = "0.1.0"
|
||||
name = "syn"
|
||||
version = "2.0.117"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99"
|
||||
dependencies = [
|
||||
"libc 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thread-id"
|
||||
version = "2.0.0"
|
||||
name = "tap"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369"
|
||||
|
||||
[[package]]
|
||||
name = "terminal_size"
|
||||
version = "0.4.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "230a1b821ccbd75b185820a1f1ff7b14d21da1e442e22c0863ea5f08771a8874"
|
||||
dependencies = [
|
||||
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rustix",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thread_local"
|
||||
version = "0.2.6"
|
||||
name = "unicode-ident"
|
||||
version = "1.0.24"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
|
||||
|
||||
[[package]]
|
||||
name = "utf8parse"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
|
||||
|
||||
[[package]]
|
||||
name = "windows-link"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
|
||||
|
||||
[[package]]
|
||||
name = "windows-sys"
|
||||
version = "0.61.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
|
||||
dependencies = [
|
||||
"thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"windows-link",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "toml"
|
||||
version = "0.1.30"
|
||||
name = "wyz"
|
||||
version = "0.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "05f360fc0b24296329c78fda852a1e9ae82de9cf7b27dae4b7f62f118f77b9ed"
|
||||
dependencies = [
|
||||
"rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"tap",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-normalization"
|
||||
version = "0.1.2"
|
||||
name = "zmij"
|
||||
version = "1.0.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-width"
|
||||
version = "0.1.3"
|
||||
name = "zopfli"
|
||||
version = "0.8.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "utf8-ranges"
|
||||
version = "0.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "variance"
|
||||
version = "0.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "vec_map"
|
||||
version = "0.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "winapi"
|
||||
version = "0.2.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "winapi-build"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[metadata]
|
||||
"checksum aho-corasick 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "2b3fb52b09c1710b961acb35390d514be82e4ac96a9969a8e38565a29b878dc9"
|
||||
"checksum ansi_term 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c877397e09fec7a240af5fa74ad0124054b8066149d6544cd1ace93f8de3be68"
|
||||
"checksum bit-vec 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "5b97c2c8e8bbb4251754f559df8af22fb264853c7d009084a576cdf12565089d"
|
||||
"checksum bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "aad18937a628ec6abcd26d1489012cc0e18c21798210f491af69ded9b881106d"
|
||||
"checksum byteorder 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "0fc10e8cc6b2580fda3f36eb6dc5316657f812a3df879a44a66fc9f0fdbc4855"
|
||||
"checksum clap 2.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6adb6a046b8155874daf331e6cb6f4a3edf3ea3cbc625809eb4077a384124761"
|
||||
"checksum clippy 0.0.80 (registry+https://github.com/rust-lang/crates.io-index)" = "e96469b413984c78285727f94f9c626a1f2006cecdcf813b5d6893c0c85df42f"
|
||||
"checksum clippy_lints 0.0.80 (registry+https://github.com/rust-lang/crates.io-index)" = "f11938c4b10c556903bb1c1e717eb038658324bf7197e4cfc159a16417327345"
|
||||
"checksum crc 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2a3f9159e74024e2cdb6f574e9117cdc2e91523a890930a022823d17abedc90a"
|
||||
"checksum crossbeam 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)" = "fb974f835e90390c5f9dfac00f05b06dc117299f5ea4e85fbc7bb443af4911cc"
|
||||
"checksum enum_primitive 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f79eff5be92a4d7d5bddf7daa7d650717ea71628634efe6ca7bcda85b2183c23"
|
||||
"checksum flate2 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "3eeb481e957304178d2e782f2da1257f1434dfecbae883bafb61ada2a9fea3bb"
|
||||
"checksum gcc 0.3.32 (registry+https://github.com/rust-lang/crates.io-index)" = "dcb000abd6df9df4c637f75190297ebe56c1d7e66b56bbf3b4aa7aece15f61a2"
|
||||
"checksum glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb"
|
||||
"checksum image 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b7cd7efa3558b1acd537f2a6f9bcf15c4ae4842da38d2e2ea7c9ad023f36239b"
|
||||
"checksum inflate 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e7e0062d2dc2f17d2f13750d95316ae8a2ff909af0fda957084f5defd87c43bb"
|
||||
"checksum itertools 0.4.17 (registry+https://github.com/rust-lang/crates.io-index)" = "7ddb83e0e9730d830afba4e6839077948b4efa22075f7cd80a3867ec286c610f"
|
||||
"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
|
||||
"checksum lazy_static 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "49247ec2a285bb3dcb23cbd9c35193c025e7251bfce77c1d5da97e6362dffe7f"
|
||||
"checksum libc 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "39dfaaa0f4da0f1a06876c5d94329d739ad0150868069cc235f1ddf80a0480e7"
|
||||
"checksum libz-sys 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "ab80d82d16acfbf5c0e731297db302378896c0cffbc428dddc61d995a82e15cf"
|
||||
"checksum matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "15305656809ce5a4805b1ff2946892810992197ce1270ff79baded852187942e"
|
||||
"checksum memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d8b629fb514376c675b98c1421e80b151d3817ac42d7c667717d282761418d20"
|
||||
"checksum miniz-sys 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "9d1f4d337a01c32e1f2122510fed46393d53ca35a7f429cb0450abaedfa3ed54"
|
||||
"checksum nom 1.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "a5b8c256fd9471521bcb84c3cdba98921497f1a331cbc15b8030fc63b82050ce"
|
||||
"checksum num 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)" = "d2ee34a0338c16ae67afb55824aaf8852700eb0f77ccd977807ccb7606b295f6"
|
||||
"checksum num-bigint 0.1.33 (registry+https://github.com/rust-lang/crates.io-index)" = "fbc450723a2fe91d332a29edd8660e099b937d29e1a3ebe914e0da3f77ac1ad3"
|
||||
"checksum num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)" = "fb24d9bfb3f222010df27995441ded1e954f8f69cd35021f6bef02ca9552fb92"
|
||||
"checksum num-iter 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)" = "287a1c9969a847055e1122ec0ea7a5c5d6f72aad97934e131c83d5c08ab4e45c"
|
||||
"checksum num-rational 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)" = "48cdcc9ff4ae2a8296805ac15af88b3d88ce62128ded0cb74ffb63a587502a84"
|
||||
"checksum num-traits 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)" = "95e58eac34596aac30ab134c8a8da9aa2dc99caa4b4b4838e6fc6e298016278f"
|
||||
"checksum num_cpus 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a859041cbf7a70ea1ece4b87d1a2c6ef364dcb68749c88db1f97304b9ec09d5f"
|
||||
"checksum pkg-config 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "8cee804ecc7eaf201a4a207241472cc870e825206f6c031e3ee2a72fa425f2fa"
|
||||
"checksum png 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "06208e2ee243e3118a55dda9318f821f206d8563fb8d4df258767f8e62bb0997"
|
||||
"checksum quine-mc_cluskey 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "e8f9a75e9319beed349f32bb252bcd7cafe4cce6c57a9ddcc9a8ffd3c49c3659"
|
||||
"checksum rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)" = "2791d88c6defac799c3f20d74f094ca33b9332612d9aef9078519c82e4fe04a5"
|
||||
"checksum regex 0.1.73 (registry+https://github.com/rust-lang/crates.io-index)" = "56b7ee9f764ecf412c6e2fff779bca4b22980517ae335a21aeaf4e32625a5df2"
|
||||
"checksum regex-syntax 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "31040aad7470ad9d8c46302dcffba337bb4289ca5da2e3cd6e37b64109a85199"
|
||||
"checksum rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)" = "6159e4e6e559c81bd706afe9c8fd68f547d3e851ce12e76b1de7914bab61691b"
|
||||
"checksum scoped-pool 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "817a3a15e704545ce59ed2b5c60a5d32bda4d7869befb8b36667b658a6c00b43"
|
||||
"checksum scopeguard 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "59a076157c1e2dc561d8de585151ee6965d910dd4dcb5dabb7ae3e83981a6c57"
|
||||
"checksum semver 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2d5b7638a1f03815d94e88cb3b3c08e87f0db4d683ef499d1836aaf70a45623f"
|
||||
"checksum simd 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "63b5847c2d766ca7ce7227672850955802fabd779ba616aeabead4c2c3877023"
|
||||
"checksum strsim 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0d5f575d5ced6634a5c4cb842163dab907dc7e9148b28dc482d81b8855cbe985"
|
||||
"checksum term_size 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a6a7c9a4de31e5622ec38533988a9e965aab09b26ee8bd7b8b0f56d488c3784d"
|
||||
"checksum thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a9539db560102d1cef46b8b78ce737ff0bb64e7e18d35b2a5688f7d097d0ff03"
|
||||
"checksum thread_local 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "55dd963dbaeadc08aa7266bf7f91c3154a7805e32bb94b820b769d2ef3b4744d"
|
||||
"checksum toml 0.1.30 (registry+https://github.com/rust-lang/crates.io-index)" = "0590d72182e50e879c4da3b11c6488dae18fccb1ae0c7a3eda18e16795844796"
|
||||
"checksum unicode-normalization 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "26643a2f83bac55f1976fb716c10234485f9202dcd65cfbdf9da49867b271172"
|
||||
"checksum unicode-width 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2d6722facc10989f63ee0e20a83cd4e1714a9ae11529403ac7e0afd069abc39e"
|
||||
"checksum utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a1ca13c08c41c9c3e04224ed9ff80461d97e121589ff27c753a16cb10830ae0f"
|
||||
"checksum variance 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3abfc2be1fb59663871379ea884fd81de80c496f2274e021c01d6fe56cd77b05"
|
||||
"checksum vec_map 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cac5efe5cb0fa14ec2f84f83c701c562ee63f6dcc680861b21d65c682adfb05f"
|
||||
"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
|
||||
"checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"
|
||||
checksum = "f05cd8797d63865425ff89b5c4a48804f35ba0ce8d125800027ad6017d2b5249"
|
||||
dependencies = [
|
||||
"bumpalo",
|
||||
"crc32fast",
|
||||
"log",
|
||||
"simd-adler32",
|
||||
]
|
||||
|
|
|
|||
147
Cargo.toml
|
|
@ -1,56 +1,121 @@
|
|||
[package]
|
||||
name = "oxipng"
|
||||
version = "0.10.0"
|
||||
authors = ["Joshua Holmer <jholmer.in@gmail.com>"]
|
||||
categories = ["command-line-utilities", "compression"]
|
||||
description = "A lossless PNG compression optimizer"
|
||||
license = "MIT"
|
||||
documentation = "https://shssoichiro.github.io/oxipng/"
|
||||
homepage = "https://github.com/shssoichiro/oxipng"
|
||||
repository = "https://github.com/shssoichiro/oxipng"
|
||||
keywords = ["png", "image-compression", "optimization", "multi-threading", "lossless"]
|
||||
documentation = "https://docs.rs/oxipng"
|
||||
edition = "2024"
|
||||
exclude = [
|
||||
".editorconfig",
|
||||
".gitattributes",
|
||||
".github/*",
|
||||
".gitignore",
|
||||
".pre-commit-hooks.yaml",
|
||||
"Dockerfile",
|
||||
"scripts/*",
|
||||
"tests/*",
|
||||
"bench/*",
|
||||
"xtask/*",
|
||||
]
|
||||
homepage = "https://github.com/oxipng/oxipng"
|
||||
license = "MIT"
|
||||
name = "oxipng"
|
||||
repository = "https://github.com/oxipng/oxipng"
|
||||
version = "10.1.1"
|
||||
rust-version = "1.85.1"
|
||||
|
||||
[badges]
|
||||
travis-ci = { repository = "oxipng/oxipng", branch = "master" }
|
||||
maintenance = { status = "actively-developed" }
|
||||
|
||||
[[bin]]
|
||||
doc = false
|
||||
name = "oxipng"
|
||||
path = "src/main.rs"
|
||||
required-features = ["binary"]
|
||||
|
||||
[[bench]]
|
||||
name = "zopfli"
|
||||
required-features = ["zopfli"]
|
||||
|
||||
[dependencies]
|
||||
bitvec = "1.0.1"
|
||||
clap = { version = "4.6.0", optional = true, features = ["wrap_help"] }
|
||||
env_logger = { version = "0.11.10", optional = true, default-features = false, features = ["auto-color"] }
|
||||
image = { version = "0.25.9", optional = true, default-features = false, features = ["png"] }
|
||||
indexmap = "2.14.0"
|
||||
libdeflater = "1.25.2"
|
||||
log = "0.4.30"
|
||||
parse-size = { version = "1.1.0", optional = true }
|
||||
rayon = { version = "1.11.0", optional = true }
|
||||
rgb = "0.8.53"
|
||||
rustc-hash = "2.1.2"
|
||||
zopfli = { version = "0.8.3", optional = true, default-features = false, features = ["std", "zlib"] }
|
||||
|
||||
[target.'cfg(windows)'.dependencies]
|
||||
glob = { version = "0.3.3", optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
serde_json = "1.0.150"
|
||||
|
||||
[features]
|
||||
binary = ["dep:clap", "dep:glob", "dep:env_logger", "dep:parse-size"]
|
||||
default = ["binary", "parallel", "zopfli"]
|
||||
parallel = ["dep:rayon", "indexmap/rayon"]
|
||||
freestanding = ["libdeflater/freestanding"]
|
||||
sanity-checks = ["dep:image"]
|
||||
zopfli = ["dep:zopfli"]
|
||||
system-libdeflate = ["libdeflater/dynamic"]
|
||||
|
||||
[lib]
|
||||
name = "oxipng"
|
||||
path = "src/lib.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "oxipng"
|
||||
path = "src/main.rs"
|
||||
doc = false
|
||||
[profile.dev]
|
||||
opt-level = 2
|
||||
|
||||
[dependencies]
|
||||
bit-vec = "^0.4.2"
|
||||
byteorder = "^0.5.0"
|
||||
crc = "^1.2.0"
|
||||
itertools = "^0.4.16"
|
||||
libc = "^0.2.4"
|
||||
libz-sys = "^1.0.0"
|
||||
miniz-sys = "^0.1.7"
|
||||
num_cpus = "^1.0.0"
|
||||
scoped-pool = "^1.0.0"
|
||||
[profile.release]
|
||||
lto = "fat"
|
||||
strip = "symbols"
|
||||
panic = "abort"
|
||||
|
||||
[dependencies.clap]
|
||||
optional = true
|
||||
version = "^2.2.5"
|
||||
[package.metadata.deb]
|
||||
assets = [
|
||||
["target/release/oxipng", "usr/bin/", "755"],
|
||||
["target/xtask/mangen/manpages/oxipng.1", "usr/share/man/man1/", "644"],
|
||||
["README.md", "usr/share/doc/oxipng/", "644"],
|
||||
["CHANGELOG.md", "usr/share/doc/oxipng/", "644"],
|
||||
]
|
||||
|
||||
[dependencies.clippy]
|
||||
optional = true
|
||||
version = "0.0.80"
|
||||
[lints.rust]
|
||||
missing_copy_implementations = "deny"
|
||||
missing_debug_implementations = "deny"
|
||||
trivial_casts = "warn"
|
||||
trivial_numeric_casts = "warn"
|
||||
unused_import_braces = "warn"
|
||||
|
||||
[dependencies.regex]
|
||||
optional = true
|
||||
version = "^0.1.63"
|
||||
|
||||
[dev-dependencies.image]
|
||||
version = "^0.10.0"
|
||||
default-features = false
|
||||
features = ["png_codec"]
|
||||
|
||||
[features]
|
||||
default = ["binary"]
|
||||
binary = ["clap", "regex"]
|
||||
nightly = ["clippy"]
|
||||
nightly-binary = ["binary", "nightly", "regex/simd-accel"]
|
||||
[lints.clippy]
|
||||
cloned_instead_of_copied = "warn"
|
||||
expl_impl_clone_on_copy = "warn"
|
||||
float_cmp_const = "warn"
|
||||
if_not_else = "warn"
|
||||
ignored_unit_patterns = "warn"
|
||||
linkedlist = "warn"
|
||||
manual_let_else = "warn"
|
||||
map_flatten = "warn"
|
||||
map_unwrap_or = "warn"
|
||||
match_same_arms = "warn"
|
||||
mem_forget = "warn"
|
||||
missing_const_for_fn = "warn"
|
||||
mut_mut = "warn"
|
||||
mutex_integer = "warn"
|
||||
needless_continue = "warn"
|
||||
option_if_let_else = "warn"
|
||||
path_buf_push_overwrite = "warn"
|
||||
range_plus_one = "warn"
|
||||
redundant_clone = "warn"
|
||||
redundant_closure_for_method_calls = "warn"
|
||||
semicolon_if_nothing_returned = "warn"
|
||||
unnecessary_semicolon = "warn"
|
||||
unseparated_literal_suffix = "warn"
|
||||
use_self = "warn"
|
||||
useless_let_if_seq = "warn"
|
||||
|
|
|
|||
46
Dockerfile
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
# syntax=docker/dockerfile:1
|
||||
# check=error=true
|
||||
FROM --platform=$BUILDPLATFORM tonistiigi/xx AS xx
|
||||
|
||||
FROM --platform=$BUILDPLATFORM rust:1.85.1-alpine AS base
|
||||
|
||||
RUN apk update && \
|
||||
apk add \
|
||||
gcc \
|
||||
g++ \
|
||||
clang
|
||||
|
||||
COPY --from=xx / /
|
||||
|
||||
ARG TARGETPLATFORM
|
||||
RUN xx-info env
|
||||
|
||||
RUN xx-apk add \
|
||||
gcc \
|
||||
musl-dev \
|
||||
libdeflate
|
||||
|
||||
WORKDIR /src
|
||||
|
||||
COPY . .
|
||||
|
||||
RUN --mount=type=cache,target=/root/.cargo/git/db \
|
||||
--mount=type=cache,target=/root/.cargo/registry/cache \
|
||||
--mount=type=cache,target=/root/.cargo/registry/index \
|
||||
xx-cargo build --release && \
|
||||
xx-verify /src/target/$(xx-cargo --print-target-triple)/release/oxipng && \
|
||||
cp /src/target/$(xx-cargo --print-target-triple)/release/oxipng /src/target/oxipng
|
||||
|
||||
FROM scratch AS tool
|
||||
|
||||
LABEL org.opencontainers.image.title="Oxipng"
|
||||
LABEL org.opencontainers.image.description="Multithreaded PNG optimizer written in Rust"
|
||||
LABEL org.opencontainers.image.authors="Joshua Holmer <jholmer.in@gmail.com>"
|
||||
LABEL org.opencontainers.image.licenses="MIT"
|
||||
LABEL org.opencontainers.image.source="https://github.com/oxipng/oxipng"
|
||||
|
||||
COPY --from=base /src/target/oxipng /usr/local/bin/oxipng
|
||||
|
||||
WORKDIR /work
|
||||
ENTRYPOINT [ "oxipng" ]
|
||||
CMD [ "--help" ]
|
||||
227
MANUAL.txt
Normal file
|
|
@ -0,0 +1,227 @@
|
|||
oxipng 10.1.0
|
||||
Losslessly improve compression of PNG files
|
||||
|
||||
Usage: oxipng [OPTIONS] <files>...
|
||||
|
||||
Arguments:
|
||||
<files>...
|
||||
File(s) to compress (use '-' for stdin)
|
||||
|
||||
Options:
|
||||
-o, --opt <level>
|
||||
Set the optimization level preset. The default level 2 is quite fast and provides good
|
||||
compression. Lower levels are faster, higher levels provide better compression, though
|
||||
with increasingly diminishing returns.
|
||||
|
||||
0 => --zc 5 --fast (filter chosen heuristically)
|
||||
1 => --zc 10 --fast (filter chosen heuristically)
|
||||
2 => --zc 11 -f 0,1,6,7 --fast
|
||||
3 => --zc 11 -f 0,7,8,9 --brute-level 1 --brute-lines 3
|
||||
4 => --zc 12 -f 0,7,8,9 --brute-level 1 --brute-lines 4
|
||||
5 => --zc 12 -f 0,1,2,5,6,7,8,9 --brute-level 4 --brute-lines 4
|
||||
6 => --zc 12 -f 0-9 --brute-level 5 --brute-lines 8
|
||||
max => (stable alias for the maximum level)
|
||||
|
||||
Manually specifying a compression option (zc, f, etc.) will override the optimization
|
||||
preset, regardless of the order you write the arguments.
|
||||
|
||||
[default: 2]
|
||||
|
||||
-r, --recursive
|
||||
When directories are given as input, traverse the directory trees and optimize all PNG
|
||||
files found (files with “.png” or “.apng” extension).
|
||||
|
||||
--dir <directory>
|
||||
Write output file(s) to <directory>. If the directory does not exist, it will be created.
|
||||
Note that this will not preserve the directory structure of the input files when used with
|
||||
'--recursive'.
|
||||
|
||||
--out <file>
|
||||
Write output file to <file>
|
||||
|
||||
--stdout
|
||||
Write output to stdout
|
||||
|
||||
-p, --preserve
|
||||
Preserve file permissions and timestamps if possible
|
||||
|
||||
-d, --dry-run
|
||||
Do not write any files, only show compression results
|
||||
|
||||
-s
|
||||
Strip safely-removable chunks, same as '--strip safe'
|
||||
|
||||
--strip <mode>
|
||||
Strip metadata chunks, where <mode> is one of:
|
||||
|
||||
safe => Strip all non-critical chunks, except for the following:
|
||||
cICP, iCCP, sRGB, pHYs, acTL, fcTL, fdAT
|
||||
all => Strip all non-critical chunks
|
||||
<list> => Strip chunks in the comma-separated list, e.g. 'bKGD,cHRM'
|
||||
|
||||
CAUTION: 'all' will convert APNGs to standard PNGs.
|
||||
|
||||
Please note that regardless of any options set, some chunks will necessarily be stripped
|
||||
when invalidated by the optimization:
|
||||
bKGD, sBIT, hIST: Stripped if the color type or bit depth changes.
|
||||
iDOT: Always stripped.
|
||||
caBX: Stripped if it contains C2PA metadata. If explicitly retained by `--keep`,
|
||||
optimization will be aborted.
|
||||
|
||||
The default when --strip is not passed is to keep all chunks that remain valid.
|
||||
|
||||
--keep <list>
|
||||
Strip all metadata chunks except those in the comma-separated list. The special value
|
||||
'display' includes chunks that affect the image appearance, equivalent to '--strip safe'.
|
||||
|
||||
E.g. '--keep eXIf,display' will strip chunks, keeping only eXIf and those that affect the
|
||||
image appearance.
|
||||
|
||||
-a, --alpha
|
||||
Perform additional optimization on images with an alpha channel, by altering the color
|
||||
values of fully transparent pixels. This is generally recommended for better compression,
|
||||
but take care as while this is “visually lossless”, it is technically a lossy
|
||||
transformation and may be unsuitable for some applications.
|
||||
|
||||
-i, --interlace <mode>
|
||||
Set the PNG interlacing mode, where <mode> is one of:
|
||||
|
||||
off => Remove interlacing from all images that are processed
|
||||
on => Apply Adam7 interlacing on all images that are processed
|
||||
keep => Keep the existing interlacing mode of each image
|
||||
|
||||
Note that interlacing can add 25-50% to the size of an optimized image. Only use it if you
|
||||
believe the benefits outweigh the costs for your use case.
|
||||
|
||||
[default: off]
|
||||
|
||||
--scale16
|
||||
Forcibly reduce images with 16 bits per channel to 8 bits per channel. This is a lossy
|
||||
operation but can provide significant savings when you have no need for higher depth.
|
||||
Reduction is performed by scaling the values such that, e.g. 0x00FF is reduced to 0x01
|
||||
rather than 0x00.
|
||||
|
||||
Without this flag, 16-bit images will only be reduced in depth if it can be done
|
||||
losslessly.
|
||||
|
||||
-v, --verbose...
|
||||
Show per-file info (use multiple times for more detail)
|
||||
|
||||
-q, --quiet
|
||||
Suppress all output messages
|
||||
|
||||
-j, --json
|
||||
Print results as JSON
|
||||
|
||||
-f, --filters <list>
|
||||
Perform compression trials with each of the given filter types. You can specify a
|
||||
comma-separated list, or a range of values. E.g. '-f 0-3' is the same as '-f 0,1,2,3'.
|
||||
|
||||
PNG delta filters (apply the same filter to every line)
|
||||
0 => None (recommended to always include this filter)
|
||||
1 => Sub
|
||||
2 => Up
|
||||
3 => Average
|
||||
4 => Paeth
|
||||
|
||||
Heuristic strategies (try to find the best delta filter for each line)
|
||||
5 => MinSum Minimum sum of absolute differences
|
||||
6 => Entropy Smallest Shannon entropy
|
||||
7 => Bigrams Lowest count of distinct bigrams
|
||||
8 => BigEnt Smallest Shannon entropy of bigrams
|
||||
9 => Brute Smallest compressed size (slow)
|
||||
|
||||
The default value depends on the optimization level preset.
|
||||
|
||||
--fast
|
||||
Perform a fast compression evaluation of each enabled filter, followed by a single main
|
||||
compression trial of the best result. Recommended if you have more filters enabled than
|
||||
CPU cores.
|
||||
|
||||
--zc <level>
|
||||
Deflate compression level (0-12) for main compression trials. The levels here are defined
|
||||
by the libdeflate compression library.
|
||||
|
||||
The default value depends on the optimization level preset.
|
||||
|
||||
--nb
|
||||
Do not change bit depth
|
||||
|
||||
--nc
|
||||
Do not change color type
|
||||
|
||||
--np
|
||||
Do not change color palette
|
||||
|
||||
--ng
|
||||
Do not change to or from grayscale
|
||||
|
||||
--nx
|
||||
Do not perform any transformations and do not deinterlace by default.
|
||||
|
||||
--nz
|
||||
Do not recompress IDAT unless required due to transformations. Recompression of other
|
||||
compressed chunks (such as iCCP) will also be disabled. Note that the combination of
|
||||
'--nx' and '--nz' will fully disable all optimization.
|
||||
|
||||
--fix
|
||||
Do not perform checksum validation of PNG chunks. This may allow some files with errors to
|
||||
be processed successfully. The output will always have correct checksums.
|
||||
|
||||
--force
|
||||
Write the output even if it is larger than the input
|
||||
|
||||
-z, --zopfli
|
||||
Use the much slower but stronger Zopfli compressor for main compression trials.
|
||||
Recommended use is with '-o max' and '--fast'.
|
||||
|
||||
--zi <iterations>
|
||||
Set the number of iterations to use for Zopfli compression. Using fewer iterations may
|
||||
speed up compression for large files. This option requires '--zopfli' to be set.
|
||||
|
||||
[default: 15]
|
||||
|
||||
--ziwi <iterations>
|
||||
Stop Zopfli compression after this number of iterations without improvement. Use this in
|
||||
conjunction with a high value for '--zi' to achieve better compression in reasonable time.
|
||||
|
||||
--brute-level <level>
|
||||
Set the libdeflate compression level to use with the Brute filter strategy. Sane values
|
||||
are 1-5. Higher values are not necessarily better.
|
||||
|
||||
--brute-lines <lines>
|
||||
Set the number of lines to compress at once with the Brute filter strategy. Sane values
|
||||
are 2-16. Higher values are not necessarily better.
|
||||
|
||||
--timeout <secs>
|
||||
Maximum amount of time, in seconds, to spend on optimizations. Oxipng will check the
|
||||
timeout before each transformation or compression trial, and will stop trying to optimize
|
||||
the file if the timeout is exceeded. Note that this does not cut short any operations that
|
||||
are already in progress, so it is currently of limited effectiveness for large files with
|
||||
high compression levels.
|
||||
|
||||
--max-raw-size <bytes>
|
||||
Maximum size to allow for the input image. If the raw, decompressed image data (or the
|
||||
file size) of the image exceeds this size, it will be skipped. This is useful for limiting
|
||||
memory usage or avoiding long processing times on large images. The value may be specified
|
||||
with a unit suffix such as k, KB, m, MB, etc.
|
||||
|
||||
The decompressed size of an image is roughly equal to width * height * bit-depth / 8. E.g.
|
||||
a 1920x1080 image with 24-bit color depth would be roughly 6MB.
|
||||
|
||||
-t, --threads <num>
|
||||
Set the maximum number of threads to use. Oxipng uses multithreading to evaluate multiple
|
||||
optimizations on the same file in parallel as well as process multiple files in parallel.
|
||||
You can set this to a lower value if you need to limit memory or CPU usage.
|
||||
|
||||
[default: num logical CPUs]
|
||||
|
||||
--sequential
|
||||
Process multiple files sequentially rather than in parallel. Use this if you need
|
||||
determinism in the processing order. Note this is not necessary if using '--threads 1'.
|
||||
|
||||
-h, --help
|
||||
Print help (see a summary with '-h')
|
||||
|
||||
-V, --version
|
||||
Print version
|
||||
159
README.md
|
|
@ -1,85 +1,151 @@
|
|||
# Oxipng
|
||||
|
||||
[](https://travis-ci.org/shssoichiro/oxipng)
|
||||
[](https://github.com/oxipng/oxipng/actions?query=branch%3Amaster)
|
||||
[](https://crates.io/crates/oxipng)
|
||||
[](https://github.com/shssoichiro/oxipng/blob/master/LICENSE)
|
||||
[](https://github.com/oxipng/oxipng/blob/master/LICENSE)
|
||||
[](https://docs.rs/oxipng)
|
||||
|
||||
## Overview
|
||||
|
||||
Oxipng is a multithreaded lossless PNG compression optimizer. It can be used via a command-line
|
||||
interface or as a library in other Rust programs.
|
||||
Oxipng is a multithreaded lossless PNG/APNG compression optimizer. It can be used via a command-line
|
||||
interface or as a library in other Rust programs. It is fast and highly effective.
|
||||
|
||||
## Installing
|
||||
|
||||
Oxipng can be installed from Cargo, via the following command:
|
||||
Oxipng for Windows can be downloaded via the
|
||||
[Releases](https://github.com/oxipng/oxipng/releases) section on its GitHub page. Recently,
|
||||
however, Oxipng has also been made available through package managers. Check the list below for
|
||||
up-to-date options.
|
||||
|
||||
For MacOS or Linux, it is recommended to install from your distro's package repository, provided
|
||||
Oxipng is available there in a not too outdated version for your use case.
|
||||
|
||||
Oxipng is known to be packaged for the environments listed below.
|
||||
|
||||
[](https://repology.org/project/oxipng/versions)
|
||||
|
||||
Alternatively, Oxipng can be installed from Cargo, via the following command:
|
||||
|
||||
```
|
||||
cargo install oxipng
|
||||
```
|
||||
|
||||
Alternatively, oxipng can be built from source using the latest stable or nightly Rust:
|
||||
Oxipng can also be built from source using the latest stable or nightly Rust.
|
||||
This is primarily useful for developing on Oxipng.
|
||||
|
||||
```
|
||||
git clone https://github.com/shssoichiro/oxipng.git
|
||||
git clone https://github.com/oxipng/oxipng.git
|
||||
cd oxipng
|
||||
cargo build --release
|
||||
cp target/release/oxipng /usr/local/bin
|
||||
```
|
||||
|
||||
Please note that zlib is a required build dependency. Oxipng should work with any 1.x version of zlib,
|
||||
but you are advised to use the latest version (currently 1.2.8) for security and bug fixes.
|
||||
|
||||
The current minimum supported Rust version is **1.8.0**. Oxipng may compile on earlier versions of Rust,
|
||||
but there is no guarantee.
|
||||
The current minimum supported Rust version is **1.85.1**.
|
||||
|
||||
Oxipng follows Semantic Versioning.
|
||||
|
||||
## Usage
|
||||
|
||||
Oxipng is a command-line utility. Basic usage looks similar to the following:
|
||||
Oxipng is a command-line utility. An example usage, suitable for web, may be the following:
|
||||
|
||||
```
|
||||
oxipng -o 4 -i 1 --strip safe *.png
|
||||
oxipng -o 4 --strip safe --alpha *.png
|
||||
```
|
||||
|
||||
The most commonly used options are as follows:
|
||||
* Optimization: `-o 1` through `-o 6`, lower is faster, higher is better compression.
|
||||
The default (`-o 2`) is sufficiently fast on a modern CPU and provides 30-50% compression
|
||||
gains over an unoptimized PNG. `-o 4` is 6 times slower than `-o 2` but can provide 5-10%
|
||||
extra compression over `-o 2`. Using any setting higher than `-o 4` is unlikely
|
||||
to give any extra compression gains and is not recommended.
|
||||
* Interlacing: `-i 1` will enable [Adam7](https://en.wikipedia.org/wiki/Adam7_algorithm)
|
||||
PNG interlacing on any images that are processed. `-i 0` will remove interlacing from all
|
||||
processed images. Not specifying either will keep the same interlacing state as the
|
||||
input image. Note: Interlacing can add 25-50% to the size of an optimized image. Only use
|
||||
it if you believe the benefits outweight the costs for your use case.
|
||||
* Strip: Used to remove metadata info from processed images. Used via `--strip [safe,all]`.
|
||||
Can save a few kilobytes if you don't need the metadata. "Safe" removes only metadata that
|
||||
will never affect rendering of the image. "All" removes all metadata that is not critical
|
||||
to the image. You can also pass a comma-separated list of specific metadata chunks to remove.
|
||||
`-s` can be used as a shorthand for `--strip safe`.
|
||||
|
||||
More advanced options can be found by running `oxipng -h`.
|
||||
- Optimization: `-o 0` through `-o 6` (or `-o max`), lower is faster, higher is better compression.
|
||||
The default (`-o 2`) is quite fast and provides good compression. Higher levels can be notably
|
||||
better* but generally have increasingly diminishing returns.
|
||||
- Strip: Used to remove metadata info from processed images. Used via `--strip [safe,all]`.
|
||||
Can save a few kilobytes if you don't need the metadata. "Safe" removes only metadata that
|
||||
will never affect rendering of the image. "All" removes all metadata that is not critical
|
||||
to the image. You can also pass a comma-separated list of specific metadata chunks to remove.
|
||||
`-s` can be used as a shorthand for `--strip safe`.
|
||||
- Alpha: `--alpha` can improve compression of images with transparency, by altering the color
|
||||
values of fully transparent pixels. This is generally recommended, but take care as this is
|
||||
technically a lossy transformation and may be unsuitable for some specific applications.
|
||||
|
||||
More advanced options can be found by running `oxipng --help`, or viewed [here](MANUAL.txt).
|
||||
|
||||
Some options have both short (`-a`) and long (`--alpha`) forms. Which form you use is just a
|
||||
matter of preference. Multiple short options can be combined together, e.g.:
|
||||
`-savvo6` is equivalent to to `--strip safe --alpha --verbose --verbose --opt 6`.
|
||||
All options are case-sensitive.
|
||||
|
||||
\* Note that oxipng is not a brute-force optimizer. This means that while higher optimization levels
|
||||
are almost always better or equal to lower levels, this is not guaranteed and it is possible in
|
||||
rare circumstances that a lower level may give a marginally smaller output. Similarly, using Zopfli
|
||||
compression (`-z`) is not guaranteed to always be better than default compression.
|
||||
|
||||
## APNG support
|
||||
|
||||
Oxipng currently only supports limited optimization of animated PNGs (APNGs). It can perform
|
||||
alpha-optimization, refiltering and recompression of all frames, but all transformations will be
|
||||
disabled. For best results, it is recommended to use another tool such as
|
||||
[apngopt](https://sourceforge.net/projects/apng/files/APNG_Optimizer/) before running Oxipng.
|
||||
|
||||
## Git integration via [pre-commit]
|
||||
|
||||
Create a `.pre-commit-config.yaml` file like this, or add the lines after the `repos` map
|
||||
preamble to an already existing one:
|
||||
|
||||
```yaml
|
||||
repos:
|
||||
- repo: https://github.com/oxipng/oxipng
|
||||
rev: v10.0.0
|
||||
hooks:
|
||||
- id: oxipng
|
||||
args: ["-o", "4", "--strip", "safe", "--alpha"]
|
||||
```
|
||||
[pre-commit]: https://pre-commit.com/
|
||||
|
||||
## Docker
|
||||
|
||||
A Docker image is availlable at [`ghcr.io/oxipng/oxipng`](https://github.com/oxipng/oxipng/pkgs/container/oxipng) for `linux/amd64` and `linux/arm64`.
|
||||
|
||||
You can use it the following way:
|
||||
|
||||
```bash
|
||||
docker run --rm -v $(pwd):/work ghcr.io/oxipng/oxipng -o 4 /work/file.png
|
||||
```
|
||||
|
||||
Some older images are also available at [`ghcr.io/shssoichiro/oxipng`](https://github.com/users/shssoichiro/packages/container/package/oxipng).
|
||||
|
||||
## Library Usage
|
||||
|
||||
Although originally intended to be used as an executable, oxipng can also be used as a library in
|
||||
other Rust projects. To do so, simply add oxipng as a dependency in your Cargo.toml,
|
||||
then `extern crate oxipng` in your project. You should then have access to all of the library
|
||||
functions [documented here](https://shssoichiro.github.io/oxipng/doc/oxipng/). The simplest
|
||||
method of usage involves creating an
|
||||
[Options struct](https://shssoichiro.github.io/oxipng/doc/oxipng/struct.Options.html) and
|
||||
passing it, along with an input filename, into the
|
||||
[optimize function](https://shssoichiro.github.io/oxipng/doc/oxipng/fn.optimize.html).
|
||||
Although originally intended to be used as an executable, Oxipng can also be used as a library in
|
||||
other Rust projects. To do so, simply add Oxipng as a dependency in your Cargo.toml. You should then
|
||||
have access to all of the library functions [documented here](https://docs.rs/oxipng). The simplest
|
||||
method of usage involves creating an [Options
|
||||
struct](https://docs.rs/oxipng/latest/oxipng/struct.Options.html) and passing it, along with an
|
||||
input filename, into the [optimize function](https://docs.rs/oxipng/latest/oxipng/fn.optimize.html).
|
||||
|
||||
It is recommended to disable the "binary" feature when including Oxipng as a library. Currently, there is
|
||||
no simple way to just disable one feature in Cargo, it has to be done by disabling default features
|
||||
and specifying the desired ones, for example:
|
||||
`oxipng = { version = "10.0", features = ["parallel", "zopfli", "filetime"], default-features = false }`
|
||||
|
||||
## Software using Oxipng
|
||||
|
||||
- [ImageOptim](https://imageoptim.com): Mac app and web service for optimizing images
|
||||
- [Squoosh](https://squoosh.app): Web app for optimizing images
|
||||
- [FileOptimizer](https://nikkhokkho.sourceforge.io/?page=FileOptimizer): Windows app for optimizing files
|
||||
- [Curtail](https://github.com/Huluti/Curtail): Linux app for optimizing images
|
||||
- [pyoxipng](https://pypi.org/project/pyoxipng/): Python wrapper for Oxipng
|
||||
- [jSquash](https://github.com/jamsinclair/jSquash): Collection of WebAssembly image codecs
|
||||
- [Trunk](https://trunk.io): Developer experience toolkit for managing code
|
||||
|
||||
## History
|
||||
|
||||
Oxipng began as a complete rewrite of the OptiPNG project,
|
||||
which was assumed to be dead as no commit had been made to it since March 2014.
|
||||
The name has been changed to avoid confusion and potential legal issues.
|
||||
|
||||
The core goal of rewriting OptiPNG was to implement multithreading,
|
||||
which would be very difficult to do within the existing C codebase of OptiPNG.
|
||||
Oxipng began in 2015 as a rewrite of the OptiPNG project. The core goal was to implement
|
||||
multithreading, which would have been very difficult to do within the existing C codebase of OptiPNG.
|
||||
This also served as an opportunity to choose a more modern, safer language (Rust).
|
||||
|
||||
However, Oxipng has evolved considerably since then. While some of the options remain similar to
|
||||
OptiPNG, the architecture and capabilities are now quite different. It is not a drop-in
|
||||
replacement - if you are migrating from OptiPNG, please check the [help](MANUAL.txt) before use.
|
||||
|
||||
## Contributing
|
||||
|
||||
Any contributions are welcome and will be accepted via pull request on GitHub. Bug reports can be
|
||||
|
|
@ -88,6 +154,11 @@ to submit a fix with the bug report, it is preferred that you do so via pull req
|
|||
however you do not need to be a Rust developer to contribute.
|
||||
Other contributions (such as improving documentation or translations) are also welcome via GitHub.
|
||||
|
||||
## Benchmarks
|
||||
|
||||
An independent benchmark is linked here with permission by the author:\
|
||||
[oxipng and friends: A comparison of PNG optimization tools](https://op111.net/posts/2025/09/png-compression-oxipng-optipng-fileoptimizer-cwebp/)
|
||||
|
||||
## License
|
||||
|
||||
Oxipng is open-source software, distributed under the MIT license.
|
||||
|
|
|
|||
13
SECURITY.md
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
# Security Policy
|
||||
|
||||
## Supported Versions
|
||||
|
||||
Only the latest version will be supported with security updates. We will not maintain old branches.
|
||||
For this reason, you should attempt to always use the latest released version.
|
||||
|
||||
## Reporting a Vulnerability
|
||||
|
||||
To privately report a vulnerability in oxipng, please visit [our advisories page](https://github.com/oxipng/oxipng/security/advisories) and use the button to report a vulnerability.
|
||||
We will review it as soon as possible.
|
||||
|
||||
For vulnerabilities in dependencies, please open a pull request updating the dependency to a patched version.
|
||||
|
|
@ -1,219 +1,63 @@
|
|||
#![feature(test)]
|
||||
|
||||
extern crate test;
|
||||
extern crate oxipng;
|
||||
extern crate test;
|
||||
|
||||
use oxipng::png;
|
||||
use oxipng::deflate::deflate;
|
||||
use test::Bencher;
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[bench]
|
||||
fn bench_deflate_16_bits_strategy_0(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgb_16_should_be_rgb_16.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
use oxipng::{internal_tests::*, *};
|
||||
use test::Bencher;
|
||||
|
||||
b.iter(|| {
|
||||
deflate::deflate(png.raw_data.as_ref(), 9, 9, 0, 15).ok();
|
||||
});
|
||||
#[bench]
|
||||
fn deflate_16_bits(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgb_16_should_be_rgb_16.png"));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| deflate(png.raw.data.as_ref(), 12, None));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_deflate_8_bits_strategy_0(b: &mut Bencher) {
|
||||
fn deflate_8_bits(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgb_8_should_be_rgb_8.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
deflate::deflate(png.raw_data.as_ref(), 9, 9, 0, 15).ok();
|
||||
});
|
||||
b.iter(|| deflate(png.raw.data.as_ref(), 12, None));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_deflate_4_bits_strategy_0(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/palette_4_should_be_palette_4.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
fn deflate_4_bits(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/palette_4_should_be_palette_4.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
deflate::deflate(png.raw_data.as_ref(), 9, 9, 0, 15).ok();
|
||||
});
|
||||
b.iter(|| deflate(png.raw.data.as_ref(), 12, None));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_deflate_2_bits_strategy_0(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/palette_2_should_be_palette_2.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
fn deflate_2_bits(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/palette_2_should_be_palette_2.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
deflate::deflate(png.raw_data.as_ref(), 9, 9, 0, 15).ok();
|
||||
});
|
||||
b.iter(|| deflate(png.raw.data.as_ref(), 12, None));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_deflate_1_bits_strategy_0(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/palette_1_should_be_palette_1.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
fn deflate_1_bits(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/palette_1_should_be_palette_1.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
deflate::deflate(png.raw_data.as_ref(), 9, 9, 0, 15).ok();
|
||||
});
|
||||
b.iter(|| deflate(png.raw.data.as_ref(), 12, None));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_deflate_16_bits_strategy_1(b: &mut Bencher) {
|
||||
fn inflate_generic(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgb_16_should_be_rgb_16.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
deflate::deflate(png.raw_data.as_ref(), 9, 9, 1, 15).ok();
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_deflate_8_bits_strategy_1(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgb_8_should_be_rgb_8.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
deflate::deflate(png.raw_data.as_ref(), 9, 9, 1, 15).ok();
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_deflate_4_bits_strategy_1(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/palette_4_should_be_palette_4.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
deflate::deflate(png.raw_data.as_ref(), 9, 9, 1, 15).ok();
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_deflate_2_bits_strategy_1(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/palette_2_should_be_palette_2.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
deflate::deflate(png.raw_data.as_ref(), 9, 9, 1, 15).ok();
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_deflate_1_bits_strategy_1(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/palette_1_should_be_palette_1.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
deflate::deflate(png.raw_data.as_ref(), 9, 9, 1, 15).ok();
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_deflate_16_bits_strategy_2(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgb_16_should_be_rgb_16.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
deflate::deflate(png.raw_data.as_ref(), 9, 9, 2, 15).ok();
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_deflate_8_bits_strategy_2(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgb_8_should_be_rgb_8.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
deflate::deflate(png.raw_data.as_ref(), 9, 9, 2, 15).ok();
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_deflate_4_bits_strategy_2(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/palette_4_should_be_palette_4.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
deflate::deflate(png.raw_data.as_ref(), 9, 9, 2, 15).ok();
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_deflate_2_bits_strategy_2(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/palette_2_should_be_palette_2.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
deflate::deflate(png.raw_data.as_ref(), 9, 9, 2, 15).ok();
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_deflate_1_bits_strategy_2(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/palette_1_should_be_palette_1.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
deflate::deflate(png.raw_data.as_ref(), 9, 9, 2, 15).ok();
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_deflate_16_bits_strategy_3(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgb_16_should_be_rgb_16.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
deflate::deflate(png.raw_data.as_ref(), 9, 9, 3, 15).ok();
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_deflate_8_bits_strategy_3(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgb_8_should_be_rgb_8.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
deflate::deflate(png.raw_data.as_ref(), 9, 9, 3, 15).ok();
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_deflate_4_bits_strategy_3(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/palette_4_should_be_palette_4.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
deflate::deflate(png.raw_data.as_ref(), 9, 9, 3, 15).ok();
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_deflate_2_bits_strategy_3(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/palette_2_should_be_palette_2.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
deflate::deflate(png.raw_data.as_ref(), 9, 9, 3, 15).ok();
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_deflate_1_bits_strategy_3(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/palette_1_should_be_palette_1.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
deflate::deflate(png.raw_data.as_ref(), 9, 9, 3, 15).ok();
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_inflate_generic(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgb_16_should_be_rgb_16.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
deflate::inflate(png.idat_data.as_ref()).ok();
|
||||
});
|
||||
b.iter(|| inflate(png.idat_data.as_ref(), png.raw.ihdr.raw_data_size()));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,308 +1,285 @@
|
|||
#![feature(test)]
|
||||
|
||||
extern crate test;
|
||||
extern crate oxipng;
|
||||
extern crate test;
|
||||
|
||||
use oxipng::png;
|
||||
use test::Bencher;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use oxipng::{internal_tests::*, *};
|
||||
use test::Bencher;
|
||||
|
||||
#[bench]
|
||||
fn bench_16_bits_filter_0(b: &mut Bencher) {
|
||||
fn filters_16_bits_filter_0(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgb_16_should_be_rgb_16.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
png.filter_image(0);
|
||||
});
|
||||
b.iter(|| png.raw.filter_image(FilterStrategy::NONE, false));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_8_bits_filter_0(b: &mut Bencher) {
|
||||
fn filters_8_bits_filter_0(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgb_8_should_be_rgb_8.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
png.filter_image(0);
|
||||
});
|
||||
b.iter(|| png.raw.filter_image(FilterStrategy::NONE, false));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_4_bits_filter_0(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/palette_4_should_be_palette_4.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
fn filters_4_bits_filter_0(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/palette_4_should_be_palette_4.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
png.filter_image(0);
|
||||
});
|
||||
b.iter(|| png.raw.filter_image(FilterStrategy::NONE, false));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_2_bits_filter_0(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/palette_2_should_be_palette_2.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
fn filters_2_bits_filter_0(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/palette_2_should_be_palette_2.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
png.filter_image(0);
|
||||
});
|
||||
b.iter(|| png.raw.filter_image(FilterStrategy::NONE, false));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_1_bits_filter_0(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/palette_1_should_be_palette_1.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
fn filters_1_bits_filter_0(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/palette_1_should_be_palette_1.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
png.filter_image(0);
|
||||
});
|
||||
b.iter(|| png.raw.filter_image(FilterStrategy::NONE, false));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_16_bits_filter_1(b: &mut Bencher) {
|
||||
fn filters_16_bits_filter_1(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgb_16_should_be_rgb_16.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
png.filter_image(1);
|
||||
});
|
||||
b.iter(|| png.raw.filter_image(FilterStrategy::SUB, false));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_8_bits_filter_1(b: &mut Bencher) {
|
||||
fn filters_8_bits_filter_1(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgb_8_should_be_rgb_8.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
png.filter_image(1);
|
||||
});
|
||||
b.iter(|| png.raw.filter_image(FilterStrategy::SUB, false));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_4_bits_filter_1(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/palette_4_should_be_palette_4.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
fn filters_4_bits_filter_1(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/palette_4_should_be_palette_4.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
png.filter_image(1);
|
||||
});
|
||||
b.iter(|| png.raw.filter_image(FilterStrategy::SUB, false));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_2_bits_filter_1(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/palette_2_should_be_palette_2.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
fn filters_2_bits_filter_1(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/palette_2_should_be_palette_2.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
png.filter_image(1);
|
||||
});
|
||||
b.iter(|| png.raw.filter_image(FilterStrategy::SUB, false));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_1_bits_filter_1(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/palette_1_should_be_palette_1.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
fn filters_1_bits_filter_1(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/palette_1_should_be_palette_1.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
png.filter_image(1);
|
||||
});
|
||||
b.iter(|| png.raw.filter_image(FilterStrategy::SUB, false));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_16_bits_filter_2(b: &mut Bencher) {
|
||||
fn filters_16_bits_filter_2(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgb_16_should_be_rgb_16.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
png.filter_image(2);
|
||||
});
|
||||
b.iter(|| png.raw.filter_image(FilterStrategy::UP, false));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_8_bits_filter_2(b: &mut Bencher) {
|
||||
fn filters_8_bits_filter_2(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgb_8_should_be_rgb_8.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
png.filter_image(2);
|
||||
});
|
||||
b.iter(|| png.raw.filter_image(FilterStrategy::UP, false));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_4_bits_filter_2(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/palette_4_should_be_palette_4.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
fn filters_4_bits_filter_2(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/palette_4_should_be_palette_4.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
png.filter_image(2);
|
||||
});
|
||||
b.iter(|| png.raw.filter_image(FilterStrategy::UP, false));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_2_bits_filter_2(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/palette_2_should_be_palette_2.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
fn filters_2_bits_filter_2(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/palette_2_should_be_palette_2.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
png.filter_image(2);
|
||||
});
|
||||
b.iter(|| png.raw.filter_image(FilterStrategy::UP, false));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_1_bits_filter_2(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/palette_1_should_be_palette_1.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
fn filters_1_bits_filter_2(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/palette_1_should_be_palette_1.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
png.filter_image(2);
|
||||
});
|
||||
b.iter(|| png.raw.filter_image(FilterStrategy::UP, false));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_16_bits_filter_3(b: &mut Bencher) {
|
||||
fn filters_16_bits_filter_3(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgb_16_should_be_rgb_16.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
png.filter_image(3);
|
||||
});
|
||||
b.iter(|| png.raw.filter_image(FilterStrategy::AVERAGE, false));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_8_bits_filter_3(b: &mut Bencher) {
|
||||
fn filters_8_bits_filter_3(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgb_8_should_be_rgb_8.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
png.filter_image(3);
|
||||
});
|
||||
b.iter(|| png.raw.filter_image(FilterStrategy::AVERAGE, false));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_4_bits_filter_3(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/palette_4_should_be_palette_4.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
fn filters_4_bits_filter_3(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/palette_4_should_be_palette_4.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
png.filter_image(3);
|
||||
});
|
||||
b.iter(|| png.raw.filter_image(FilterStrategy::AVERAGE, false));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_2_bits_filter_3(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/palette_2_should_be_palette_2.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
fn filters_2_bits_filter_3(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/palette_2_should_be_palette_2.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
png.filter_image(3);
|
||||
});
|
||||
b.iter(|| png.raw.filter_image(FilterStrategy::AVERAGE, false));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_1_bits_filter_3(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/palette_1_should_be_palette_1.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
fn filters_1_bits_filter_3(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/palette_1_should_be_palette_1.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
png.filter_image(3);
|
||||
});
|
||||
b.iter(|| png.raw.filter_image(FilterStrategy::AVERAGE, false));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_16_bits_filter_4(b: &mut Bencher) {
|
||||
fn filters_16_bits_filter_4(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgb_16_should_be_rgb_16.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
png.filter_image(4);
|
||||
});
|
||||
b.iter(|| png.raw.filter_image(FilterStrategy::PAETH, false));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_8_bits_filter_4(b: &mut Bencher) {
|
||||
fn filters_8_bits_filter_4(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgb_8_should_be_rgb_8.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
png.filter_image(4);
|
||||
});
|
||||
b.iter(|| png.raw.filter_image(FilterStrategy::PAETH, false));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_4_bits_filter_4(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/palette_4_should_be_palette_4.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
fn filters_4_bits_filter_4(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/palette_4_should_be_palette_4.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
png.filter_image(4);
|
||||
});
|
||||
b.iter(|| png.raw.filter_image(FilterStrategy::PAETH, false));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_2_bits_filter_4(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/palette_2_should_be_palette_2.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
fn filters_2_bits_filter_4(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/palette_2_should_be_palette_2.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
png.filter_image(4);
|
||||
});
|
||||
b.iter(|| png.raw.filter_image(FilterStrategy::PAETH, false));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_1_bits_filter_4(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/palette_1_should_be_palette_1.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
fn filters_1_bits_filter_4(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/palette_1_should_be_palette_1.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
png.filter_image(4);
|
||||
});
|
||||
b.iter(|| png.raw.filter_image(FilterStrategy::PAETH, false));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_16_bits_filter_5(b: &mut Bencher) {
|
||||
fn filters_16_bits_filter_5(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgb_16_should_be_rgb_16.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
png.filter_image(5);
|
||||
});
|
||||
b.iter(|| png.raw.filter_image(FilterStrategy::MinSum, false));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_8_bits_filter_5(b: &mut Bencher) {
|
||||
fn filters_8_bits_filter_5(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgb_8_should_be_rgb_8.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
png.filter_image(5);
|
||||
});
|
||||
b.iter(|| png.raw.filter_image(FilterStrategy::MinSum, false));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_4_bits_filter_5(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/palette_4_should_be_palette_4.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
fn filters_4_bits_filter_5(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/palette_4_should_be_palette_4.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
png.filter_image(5);
|
||||
});
|
||||
b.iter(|| png.raw.filter_image(FilterStrategy::MinSum, false));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_2_bits_filter_5(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/palette_2_should_be_palette_2.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
fn filters_2_bits_filter_5(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/palette_2_should_be_palette_2.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
png.filter_image(5);
|
||||
});
|
||||
b.iter(|| png.raw.filter_image(FilterStrategy::MinSum, false));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_1_bits_filter_5(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/palette_1_should_be_palette_1.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
fn filters_1_bits_filter_5(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/palette_1_should_be_palette_1.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
png.filter_image(5);
|
||||
});
|
||||
b.iter(|| png.raw.filter_image(FilterStrategy::MinSum, false));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,118 +1,105 @@
|
|||
#![feature(test)]
|
||||
|
||||
extern crate test;
|
||||
extern crate oxipng;
|
||||
extern crate test;
|
||||
|
||||
use oxipng::png;
|
||||
use test::Bencher;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use oxipng::{internal_tests::*, *};
|
||||
use test::Bencher;
|
||||
|
||||
#[bench]
|
||||
fn bench_interlace_16_bits(b: &mut Bencher) {
|
||||
fn interlacing_16_bits(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgb_16_should_be_rgb_16.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
let mut safe_png = png.clone();
|
||||
safe_png.change_interlacing(1);
|
||||
});
|
||||
b.iter(|| png.raw.change_interlacing(true));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_interlace_8_bits(b: &mut Bencher) {
|
||||
fn interlacing_8_bits(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgb_8_should_be_rgb_8.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
let mut safe_png = png.clone();
|
||||
safe_png.change_interlacing(1);
|
||||
});
|
||||
b.iter(|| png.raw.change_interlacing(true));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_interlace_4_bits(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/palette_4_should_be_palette_4.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
fn interlacing_4_bits(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/palette_4_should_be_palette_4.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
let mut safe_png = png.clone();
|
||||
safe_png.change_interlacing(1);
|
||||
});
|
||||
b.iter(|| png.raw.change_interlacing(true));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_interlace_2_bits(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/palette_2_should_be_palette_2.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
fn interlacing_2_bits(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/palette_2_should_be_palette_2.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
let mut safe_png = png.clone();
|
||||
safe_png.change_interlacing(1);
|
||||
});
|
||||
b.iter(|| png.raw.change_interlacing(true));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_interlace_1_bits(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/palette_1_should_be_palette_1.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
fn interlacing_1_bits(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/palette_1_should_be_palette_1.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
let mut safe_png = png.clone();
|
||||
safe_png.change_interlacing(1);
|
||||
});
|
||||
b.iter(|| png.raw.change_interlacing(true));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_deinterlace_16_bits(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/interlaced_rgb_16_should_be_rgb_16.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
fn deinterlacing_16_bits(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/interlaced_rgb_16_should_be_rgb_16.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
let mut safe_png = png.clone();
|
||||
safe_png.change_interlacing(0);
|
||||
});
|
||||
b.iter(|| png.raw.change_interlacing(false));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_deinterlace_8_bits(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/interlaced_rgb_8_should_be_rgb_8.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
fn deinterlacing_8_bits(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/interlaced_rgb_8_should_be_rgb_8.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
let mut safe_png = png.clone();
|
||||
safe_png.change_interlacing(0);
|
||||
});
|
||||
b.iter(|| png.raw.change_interlacing(false));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_deinterlace_4_bits(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/interlaced_palette_4_should_be_palette_4.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
fn deinterlacing_4_bits(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/interlaced_palette_4_should_be_palette_4.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
let mut safe_png = png.clone();
|
||||
safe_png.change_interlacing(0);
|
||||
});
|
||||
b.iter(|| png.raw.change_interlacing(false));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_deinterlace_2_bits(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/interlaced_palette_2_should_be_palette_2.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
fn deinterlacing_2_bits(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/interlaced_palette_2_should_be_palette_2.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
let mut safe_png = png.clone();
|
||||
safe_png.change_interlacing(0);
|
||||
});
|
||||
b.iter(|| png.raw.change_interlacing(false));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_deinterlace_1_bits(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/interlaced_palette_1_should_be_palette_1.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
fn deinterlacing_1_bits(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/interlaced_palette_1_should_be_palette_1.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
let mut safe_png = png.clone();
|
||||
safe_png.change_interlacing(0);
|
||||
});
|
||||
b.iter(|| png.raw.change_interlacing(false));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,228 +1,281 @@
|
|||
#![feature(test)]
|
||||
|
||||
extern crate test;
|
||||
extern crate oxipng;
|
||||
extern crate test;
|
||||
|
||||
use oxipng::png;
|
||||
use test::Bencher;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use oxipng::{internal_tests::*, *};
|
||||
use test::Bencher;
|
||||
|
||||
#[bench]
|
||||
fn bench_16_to_8_bits(b: &mut Bencher) {
|
||||
fn reductions_16_to_8_bits(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgb_16_should_be_rgb_8.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
let mut safe_png = png.clone();
|
||||
safe_png.reduce_bit_depth();
|
||||
});
|
||||
b.iter(|| bit_depth::reduced_bit_depth_16_to_8(&png.raw, false));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_8_to_4_bits(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/palette_8_should_be_palette_4.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
fn reductions_16_to_8_bits_scaled(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgb_16_should_be_rgb_16.png"));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
let mut safe_png = png.clone();
|
||||
safe_png.reduce_bit_depth();
|
||||
});
|
||||
b.iter(|| bit_depth::reduced_bit_depth_16_to_8(&png.raw, true));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_8_to_2_bits(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/palette_8_should_be_palette_2.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
fn reductions_8_to_4_bits(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/palette_8_should_be_palette_4.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
let mut safe_png = png.clone();
|
||||
safe_png.reduce_bit_depth();
|
||||
});
|
||||
b.iter(|| bit_depth::reduced_bit_depth_8_or_less(&png.raw));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_8_to_1_bits(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/palette_8_should_be_palette_1.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
fn reductions_8_to_2_bits(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/palette_8_should_be_palette_2.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
let mut safe_png = png.clone();
|
||||
safe_png.reduce_bit_depth();
|
||||
});
|
||||
b.iter(|| bit_depth::reduced_bit_depth_8_or_less(&png.raw));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_4_to_2_bits(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/palette_4_should_be_palette_2.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
fn reductions_8_to_1_bits(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/palette_8_should_be_palette_1.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
let mut safe_png = png.clone();
|
||||
safe_png.reduce_bit_depth();
|
||||
});
|
||||
b.iter(|| bit_depth::reduced_bit_depth_8_or_less(&png.raw));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_4_to_1_bits(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/palette_4_should_be_palette_1.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
fn reductions_grayscale_8_to_4_bits(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/grayscale_8_should_be_grayscale_4.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
let mut safe_png = png.clone();
|
||||
safe_png.reduce_bit_depth();
|
||||
});
|
||||
b.iter(|| bit_depth::reduced_bit_depth_8_or_less(&png.raw));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_2_to_1_bits(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/palette_2_should_be_palette_1.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
fn reductions_grayscale_8_to_2_bits(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/grayscale_8_should_be_grayscale_2.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
let mut safe_png = png.clone();
|
||||
safe_png.reduce_bit_depth();
|
||||
});
|
||||
b.iter(|| bit_depth::reduced_bit_depth_8_or_less(&png.raw));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_rgba_to_rgb_16(b: &mut Bencher) {
|
||||
fn reductions_grayscale_8_to_1_bits(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/grayscale_8_should_be_grayscale_1.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| bit_depth::reduced_bit_depth_8_or_less(&png.raw));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn reductions_rgba_to_rgb_16(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgba_16_should_be_rgb_16.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
let mut safe_png = png.clone();
|
||||
safe_png.reduce_color_type();
|
||||
});
|
||||
b.iter(|| alpha::reduced_alpha_channel(&png.raw, true));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_rgba_to_rgb_8(b: &mut Bencher) {
|
||||
fn reductions_rgba_to_rgb_8(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgba_8_should_be_rgb_8.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
let mut safe_png = png.clone();
|
||||
safe_png.reduce_color_type();
|
||||
});
|
||||
b.iter(|| alpha::reduced_alpha_channel(&png.raw, true));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_rgba_to_grayscale_alpha_16(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgba_16_should_be_grayscale_alpha_16.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
fn reductions_rgba_to_rgb_trns_8(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgba_8_should_be_rgb_trns_8.png"));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
let mut safe_png = png.clone();
|
||||
safe_png.reduce_color_type();
|
||||
});
|
||||
b.iter(|| alpha::reduced_alpha_channel(&png.raw, true));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_rgba_to_grayscale_alpha_8(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgba_8_should_be_grayscale_alpha_8.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
fn reductions_rgba_to_grayscale_alpha_16(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/rgba_16_should_be_grayscale_alpha_16.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
let mut safe_png = png.clone();
|
||||
safe_png.reduce_color_type();
|
||||
});
|
||||
b.iter(|| color::reduced_rgb_to_grayscale(&png.raw));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_rgba_to_grayscale_16(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgba_16_should_be_grayscale_16.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
fn reductions_rgba_to_grayscale_alpha_8(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/rgba_8_should_be_grayscale_alpha_8.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
let mut safe_png = png.clone();
|
||||
safe_png.reduce_color_type();
|
||||
});
|
||||
b.iter(|| color::reduced_rgb_to_grayscale(&png.raw));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_rgba_to_grayscale_8(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgba_8_should_be_grayscale_8.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
fn reductions_rgb_to_grayscale_16(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/rgb_16_should_be_grayscale_16.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
let mut safe_png = png.clone();
|
||||
safe_png.reduce_color_type();
|
||||
});
|
||||
b.iter(|| color::reduced_rgb_to_grayscale(&png.raw));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_rgb_to_grayscale_16(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgb_16_should_be_grayscale_16.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
let mut safe_png = png.clone();
|
||||
safe_png.reduce_color_type();
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_rgb_to_grayscale_8(b: &mut Bencher) {
|
||||
fn reductions_rgb_to_grayscale_8(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgb_8_should_be_grayscale_8.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
let mut safe_png = png.clone();
|
||||
safe_png.reduce_color_type();
|
||||
});
|
||||
b.iter(|| color::reduced_rgb_to_grayscale(&png.raw));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_rgba_to_palette_8(b: &mut Bencher) {
|
||||
fn reductions_rgba_to_palette_8(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgba_8_should_be_palette_8.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
let mut safe_png = png.clone();
|
||||
safe_png.reduce_color_type();
|
||||
});
|
||||
b.iter(|| color::reduced_to_indexed(&png.raw, true));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_rgb_to_palette_8(b: &mut Bencher) {
|
||||
fn reductions_rgb_to_palette_8(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgb_8_should_be_palette_8.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
let mut safe_png = png.clone();
|
||||
safe_png.reduce_color_type();
|
||||
});
|
||||
b.iter(|| color::reduced_to_indexed(&png.raw, true));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_palette_duplicate_reduction(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/palette_should_be_reduced_with_dupes.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
fn reductions_grayscale_alpha_to_grayscale_16(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/grayscale_alpha_16_should_be_grayscale_16.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
let mut safe_png = png.clone();
|
||||
safe_png.reduce_palette();
|
||||
});
|
||||
b.iter(|| alpha::reduced_alpha_channel(&png.raw, true));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_palette_unused_reduction(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/palette_should_be_reduced_with_unused.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
fn reductions_grayscale_alpha_to_grayscale_8(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/grayscale_alpha_8_should_be_grayscale_8.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
let mut safe_png = png.clone();
|
||||
safe_png.reduce_palette();
|
||||
});
|
||||
b.iter(|| alpha::reduced_alpha_channel(&png.raw, true));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_palette_full_reduction(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/palette_should_be_reduced_with_both.png"));
|
||||
let png = png::PngData::new(&input, false).unwrap();
|
||||
fn reductions_grayscale_alpha_to_grayscale_trns_8(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/grayscale_alpha_8_should_be_grayscale_trns_8.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
let mut safe_png = png.clone();
|
||||
safe_png.reduce_palette();
|
||||
});
|
||||
b.iter(|| alpha::reduced_alpha_channel(&png.raw, true));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn reductions_grayscale_8_to_palette_8(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/grayscale_8_should_be_palette_8.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| color::reduced_to_indexed(&png.raw, true));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn reductions_palette_8_to_grayscale_8(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/palette_8_should_be_grayscale_8.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| color::indexed_to_channels(&png.raw, true, false));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn reductions_palette_duplicate_reduction(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/palette_should_be_reduced_with_dupes.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| palette::reduced_palette(&png.raw, false));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn reductions_palette_unused_reduction(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/palette_should_be_reduced_with_unused.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| palette::reduced_palette(&png.raw, false));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn reductions_palette_full_reduction(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/palette_should_be_reduced_with_both.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| palette::reduced_palette(&png.raw, false));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn reductions_palette_sort(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/palette_8_should_be_palette_8.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| palette::sorted_palette(&png.raw));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn reductions_palette_sort_mzeng(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/palette_8_should_be_palette_8.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| palette::sorted_palette_mzeng(&png.raw));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn reductions_palette_sort_battiato(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/palette_8_should_be_palette_8.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| palette::sorted_palette_battiato(&png.raw));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn reductions_alpha(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgba_8_reduce_alpha.png"));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| alpha::cleaned_alpha_channel(&png.raw));
|
||||
}
|
||||
|
|
|
|||
57
benches/strategies.rs
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
#![feature(test)]
|
||||
|
||||
extern crate oxipng;
|
||||
extern crate test;
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use oxipng::{internal_tests::*, *};
|
||||
use test::Bencher;
|
||||
|
||||
#[bench]
|
||||
fn filters_minsum(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgb_8_should_be_rgb_8.png"));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| png.raw.filter_image(FilterStrategy::MinSum, false));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn filters_entropy(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgb_8_should_be_rgb_8.png"));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| png.raw.filter_image(FilterStrategy::Entropy, false));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn filters_bigrams(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgb_8_should_be_rgb_8.png"));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| png.raw.filter_image(FilterStrategy::Bigrams, false));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn filters_bigent(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgb_8_should_be_rgb_8.png"));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| png.raw.filter_image(FilterStrategy::BigEnt, false));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn filters_brute(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgb_8_should_be_rgb_8.png"));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
png.raw.filter_image(
|
||||
FilterStrategy::Brute {
|
||||
num_lines: 4,
|
||||
level: 1,
|
||||
},
|
||||
false,
|
||||
)
|
||||
});
|
||||
}
|
||||
65
benches/zopfli.rs
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
#![feature(test)]
|
||||
|
||||
extern crate oxipng;
|
||||
extern crate test;
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use oxipng::{internal_tests::*, *};
|
||||
use test::Bencher;
|
||||
|
||||
#[bench]
|
||||
fn zopfli_16_bits_strategy_0(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgb_16_should_be_rgb_16.png"));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
zopfli_deflate(png.raw.data.as_ref(), ZopfliOptions::default()).ok();
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn zopfli_8_bits_strategy_0(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from("tests/files/rgb_8_should_be_rgb_8.png"));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
zopfli_deflate(png.raw.data.as_ref(), ZopfliOptions::default()).ok();
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn zopfli_4_bits_strategy_0(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/palette_4_should_be_palette_4.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
zopfli_deflate(png.raw.data.as_ref(), ZopfliOptions::default()).ok();
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn zopfli_2_bits_strategy_0(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/palette_2_should_be_palette_2.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
zopfli_deflate(png.raw.data.as_ref(), ZopfliOptions::default()).ok();
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn zopfli_1_bits_strategy_0(b: &mut Bencher) {
|
||||
let input = test::black_box(PathBuf::from(
|
||||
"tests/files/palette_1_should_be_palette_1.png",
|
||||
));
|
||||
let png = PngData::new(&input, &Options::default()).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
zopfli_deflate(png.raw.data.as_ref(), ZopfliOptions::default()).ok();
|
||||
});
|
||||
}
|
||||
|
|
@ -1 +0,0 @@
|
|||
<script>(function() {window.location.assign(window.location.href + "doc/oxipng/");})();</script>
|
||||
|
|
@ -1 +0,0 @@
|
|||
format_strings = false
|
||||
19
scripts/compare.sh
Executable file
|
|
@ -0,0 +1,19 @@
|
|||
#!/bin/bash
|
||||
cargo build --release
|
||||
sed -i.bak '/## Benchmarks/,$d' README.md
|
||||
rm README.md.bak
|
||||
|
||||
CORES=$(sysctl -n hw.ncpu 2>/dev/null || grep -c ^processor /proc/cpuinfo)
|
||||
CPU=$(sysctl -n machdep.cpu.brand_string 2>/dev/null || grep '^model name' /proc/cpuinfo | sed 's/model name.\+: //g' | head -n 1)
|
||||
OXIPNG_VERSION=$(./target/release/oxipng -V)
|
||||
OPTIPNG_VERSION=$(optipng -v | head -n 1)
|
||||
RUST_VERSION=$(rustc -V)
|
||||
echo -e '## Benchmarks\n' >> README.md
|
||||
echo "Tested $OXIPNG_VERSION (compiled on $RUST_VERSION) against $OPTIPNG_VERSION on $CPU with $CORES logical cores" >> README.md
|
||||
echo -e '\n```\n' >> README.md
|
||||
|
||||
hyperfine --style basic --warmup 5 './target/release/oxipng -P ./tests/files/rgb_16_should_be_grayscale_8.png' 'optipng -simulate ./tests/files/rgb_16_should_be_grayscale_8.png' >> README.md
|
||||
echo -e '\n\n' >> README.md
|
||||
hyperfine --style basic --warmup 5 './target/release/oxipng -o4 -P ./tests/files/rgb_16_should_be_grayscale_8.png' 'optipng -o 4 -simulate ./tests/files/rgb_16_should_be_grayscale_8.png' >> README.md
|
||||
|
||||
echo -e '\n```' >> README.md
|
||||
7
scripts/manual.sh
Executable file
|
|
@ -0,0 +1,7 @@
|
|||
#!/bin/sh -eu
|
||||
cargo build
|
||||
cargo xtask mangen
|
||||
|
||||
./target/debug/oxipng -V > MANUAL.txt
|
||||
#Redirect all streams to prevent detection of the terminal width and force an internal default of 100
|
||||
./target/debug/oxipng --help >> MANUAL.txt 2>/dev/null </dev/null
|
||||
72
src/apng.rs
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
use crate::{
|
||||
PngResult,
|
||||
error::PngError,
|
||||
headers::{read_be_u16, read_be_u32},
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
/// Animated PNG frame
|
||||
pub struct Frame {
|
||||
/// Width of the frame
|
||||
pub width: u32,
|
||||
/// Height of the frame
|
||||
pub height: u32,
|
||||
/// X offset of the frame
|
||||
pub x_offset: u32,
|
||||
/// Y offset of the frame
|
||||
pub y_offset: u32,
|
||||
/// Frame delay numerator
|
||||
pub delay_num: u16,
|
||||
/// Frame delay denominator
|
||||
pub delay_den: u16,
|
||||
/// Frame disposal operation
|
||||
pub dispose_op: u8,
|
||||
/// Frame blend operation
|
||||
pub blend_op: u8,
|
||||
/// Frame data, from fdAT chunks
|
||||
pub data: Vec<u8>,
|
||||
}
|
||||
|
||||
impl Frame {
|
||||
/// Construct a new Frame from the data in a fcTL chunk
|
||||
pub fn from_fctl_data(byte_data: &[u8]) -> PngResult<Self> {
|
||||
if byte_data.len() < 26 {
|
||||
return Err(PngError::TruncatedData);
|
||||
}
|
||||
Ok(Self {
|
||||
width: read_be_u32(&byte_data[4..8]),
|
||||
height: read_be_u32(&byte_data[8..12]),
|
||||
x_offset: read_be_u32(&byte_data[12..16]),
|
||||
y_offset: read_be_u32(&byte_data[16..20]),
|
||||
delay_num: read_be_u16(&byte_data[20..22]),
|
||||
delay_den: read_be_u16(&byte_data[22..24]),
|
||||
dispose_op: byte_data[24],
|
||||
blend_op: byte_data[25],
|
||||
data: vec![],
|
||||
})
|
||||
}
|
||||
|
||||
/// Construct the data for a fcTL chunk using the given sequence number
|
||||
#[must_use]
|
||||
pub fn fctl_data(&self, sequence_number: u32) -> Vec<u8> {
|
||||
let mut byte_data = Vec::with_capacity(26);
|
||||
byte_data.extend_from_slice(&sequence_number.to_be_bytes());
|
||||
byte_data.extend_from_slice(&self.width.to_be_bytes());
|
||||
byte_data.extend_from_slice(&self.height.to_be_bytes());
|
||||
byte_data.extend_from_slice(&self.x_offset.to_be_bytes());
|
||||
byte_data.extend_from_slice(&self.y_offset.to_be_bytes());
|
||||
byte_data.extend_from_slice(&self.delay_num.to_be_bytes());
|
||||
byte_data.extend_from_slice(&self.delay_den.to_be_bytes());
|
||||
byte_data.extend_from_slice(&[self.dispose_op, self.blend_op]);
|
||||
byte_data
|
||||
}
|
||||
|
||||
/// Construct the data for a fdAT chunk using the given sequence number
|
||||
#[must_use]
|
||||
pub fn fdat_data(&self, sequence_number: u32) -> Vec<u8> {
|
||||
let mut byte_data = Vec::with_capacity(4 + self.data.len());
|
||||
byte_data.extend_from_slice(&sequence_number.to_be_bytes());
|
||||
byte_data.extend_from_slice(&self.data);
|
||||
byte_data
|
||||
}
|
||||
}
|
||||
25
src/atomicmin.rs
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
use std::sync::atomic::{AtomicUsize, Ordering::SeqCst};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct AtomicMin {
|
||||
val: AtomicUsize,
|
||||
}
|
||||
|
||||
impl AtomicMin {
|
||||
#[must_use]
|
||||
pub fn new(init: Option<usize>) -> Self {
|
||||
Self {
|
||||
val: AtomicUsize::new(init.unwrap_or(usize::MAX)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get(&self) -> Option<usize> {
|
||||
let val = self.val.load(SeqCst);
|
||||
if val == usize::MAX { None } else { Some(val) }
|
||||
}
|
||||
|
||||
/// Try a new value, returning true if it is the new minimum
|
||||
pub fn set_min(&self, new_val: usize) -> bool {
|
||||
new_val < self.val.fetch_min(new_val, SeqCst)
|
||||
}
|
||||
}
|
||||
450
src/cli.rs
Normal file
|
|
@ -0,0 +1,450 @@
|
|||
use std::{num::NonZeroU64, path::PathBuf};
|
||||
|
||||
use clap::builder::Styles;
|
||||
use clap::builder::styling::{AnsiColor, Effects};
|
||||
use clap::{Arg, ArgAction, Command, builder::ArgPredicate, value_parser};
|
||||
use parse_size::parse_size;
|
||||
|
||||
include!("display_chunks.rs");
|
||||
|
||||
const STYLES: Styles = Styles::styled()
|
||||
.header(AnsiColor::Green.on_default().effects(Effects::BOLD))
|
||||
.usage(AnsiColor::Green.on_default().effects(Effects::BOLD))
|
||||
.literal(AnsiColor::Cyan.on_default().effects(Effects::BOLD))
|
||||
.placeholder(AnsiColor::Cyan.on_default());
|
||||
|
||||
pub fn build_command() -> Command {
|
||||
// Note: clap 'wrap_help' is enabled to automatically wrap lines according to terminal width.
|
||||
// To keep things tidy though, short help descriptions should be no more than 54 characters,
|
||||
// so that they can fit on a single line in an 80 character terminal.
|
||||
// Long help descriptions are soft wrapped here at 90 characters (column 91) but this does not
|
||||
// affect output, it simply matches what is rendered when help is output to a file.
|
||||
Command::new("oxipng")
|
||||
.version(env!("CARGO_PKG_VERSION"))
|
||||
.author("Joshua Holmer <jholmer.in@gmail.com>")
|
||||
.about("Losslessly improve compression of PNG files")
|
||||
.styles(STYLES)
|
||||
.arg(
|
||||
Arg::new("files")
|
||||
.help("File(s) to compress (use '-' for stdin)")
|
||||
.index(1)
|
||||
.num_args(1..)
|
||||
.use_value_delimiter(false)
|
||||
.required(true)
|
||||
.value_parser(value_parser!(PathBuf)),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("optimization")
|
||||
.help("Optimization level (0-6, or max)")
|
||||
.long_help("\
|
||||
Set the optimization level preset. The default level 2 is quite fast and provides good \
|
||||
compression. Lower levels are faster, higher levels provide better compression, though \
|
||||
with increasingly diminishing returns.
|
||||
|
||||
0 => --zc 5 --fast (filter chosen heuristically)
|
||||
1 => --zc 10 --fast (filter chosen heuristically)
|
||||
2 => --zc 11 -f 0,1,6,7 --fast
|
||||
3 => --zc 11 -f 0,7,8,9 --brute-level 1 --brute-lines 3
|
||||
4 => --zc 12 -f 0,7,8,9 --brute-level 1 --brute-lines 4
|
||||
5 => --zc 12 -f 0,1,2,5,6,7,8,9 --brute-level 4 --brute-lines 4
|
||||
6 => --zc 12 -f 0-9 --brute-level 5 --brute-lines 8
|
||||
max => (stable alias for the maximum level)
|
||||
|
||||
Manually specifying a compression option (zc, f, etc.) will override the optimization \
|
||||
preset, regardless of the order you write the arguments.")
|
||||
.short('o')
|
||||
.long("opt")
|
||||
.value_name("level")
|
||||
.default_value("2")
|
||||
.value_parser(["0", "1", "2", "3", "4", "5", "6", "max"])
|
||||
.hide_possible_values(true),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("recursive")
|
||||
.help("Recurse input directories, optimizing all PNG files")
|
||||
.long_help("\
|
||||
When directories are given as input, traverse the directory trees and optimize all PNG \
|
||||
files found (files with “.png” or “.apng” extension).")
|
||||
.short('r')
|
||||
.long("recursive")
|
||||
.action(ArgAction::SetTrue),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("output_dir")
|
||||
.help("Write output file(s) to <directory>")
|
||||
.long_help("\
|
||||
Write output file(s) to <directory>. If the directory does not exist, it will be created. \
|
||||
Note that this will not preserve the directory structure of the input files when used with \
|
||||
'--recursive'.")
|
||||
.long("dir")
|
||||
.value_name("directory")
|
||||
.value_parser(value_parser!(PathBuf))
|
||||
.conflicts_with("output_file")
|
||||
.conflicts_with("stdout"),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("output_file")
|
||||
.help("Write output file to <file>")
|
||||
.long("out")
|
||||
.value_name("file")
|
||||
.value_parser(value_parser!(PathBuf))
|
||||
.conflicts_with("output_dir")
|
||||
.conflicts_with("stdout"),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("stdout")
|
||||
.help("Write output to stdout")
|
||||
.long("stdout")
|
||||
.action(ArgAction::SetTrue)
|
||||
.conflicts_with("output_dir")
|
||||
.conflicts_with("output_file"),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("preserve")
|
||||
.help("Preserve file permissions and timestamps if possible")
|
||||
.short('p')
|
||||
.long("preserve")
|
||||
.action(ArgAction::SetTrue),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("dry-run")
|
||||
.help("Do not write any files, only show compression results")
|
||||
.short('d')
|
||||
.long("dry-run")
|
||||
.action(ArgAction::SetTrue),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("strip-safe")
|
||||
.help("Strip safely-removable chunks, same as '--strip safe'")
|
||||
.short('s')
|
||||
.action(ArgAction::SetTrue)
|
||||
.conflicts_with("strip"),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("strip")
|
||||
.help("Strip metadata (safe, all, or comma-separated list)\nCAUTION: 'all' will convert APNGs to standard PNGs")
|
||||
.long_help(format!("\
|
||||
Strip metadata chunks, where <mode> is one of:
|
||||
|
||||
safe => Strip all non-critical chunks, except for the following:
|
||||
{}
|
||||
all => Strip all non-critical chunks
|
||||
<list> => Strip chunks in the comma-separated list, e.g. 'bKGD,cHRM'
|
||||
|
||||
CAUTION: 'all' will convert APNGs to standard PNGs.
|
||||
|
||||
Please note that regardless of any options set, some chunks will necessarily be stripped \
|
||||
when invalidated by the optimization:
|
||||
bKGD, sBIT, hIST: Stripped if the color type or bit depth changes.
|
||||
iDOT: Always stripped.
|
||||
caBX: Stripped if it contains C2PA metadata. If explicitly retained by `--keep`, \
|
||||
optimization will be aborted.
|
||||
|
||||
The default when --strip is not passed is to keep all chunks that remain valid.",
|
||||
DISPLAY_CHUNKS
|
||||
.iter()
|
||||
.map(|c| String::from_utf8_lossy(c))
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ")))
|
||||
.long("strip")
|
||||
.value_name("mode")
|
||||
.conflicts_with("strip-safe"),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("keep")
|
||||
.help("Strip all metadata except in the comma-separated list")
|
||||
.long_help("\
|
||||
Strip all metadata chunks except those in the comma-separated list. The special value \
|
||||
'display' includes chunks that affect the image appearance, equivalent to '--strip safe'.
|
||||
|
||||
E.g. '--keep eXIf,display' will strip chunks, keeping only eXIf and those that affect the \
|
||||
image appearance.")
|
||||
.long("keep")
|
||||
.value_name("list")
|
||||
.conflicts_with("strip")
|
||||
.conflicts_with("strip-safe"),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("alpha")
|
||||
.help("Perform additional alpha channel optimization")
|
||||
.long_help("\
|
||||
Perform additional optimization on images with an alpha channel, by altering the color \
|
||||
values of fully transparent pixels. This is generally recommended for better compression, \
|
||||
but take care as while this is “visually lossless”, it is technically a lossy \
|
||||
transformation and may be unsuitable for some applications.")
|
||||
.short('a')
|
||||
.long("alpha")
|
||||
.action(ArgAction::SetTrue),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("interlace")
|
||||
.help("Set PNG interlacing (off, on, keep)")
|
||||
.long_help("\
|
||||
Set the PNG interlacing mode, where <mode> is one of:
|
||||
|
||||
off => Remove interlacing from all images that are processed
|
||||
on => Apply Adam7 interlacing on all images that are processed
|
||||
keep => Keep the existing interlacing mode of each image
|
||||
|
||||
Note that interlacing can add 25-50% to the size of an optimized image. Only use it if you \
|
||||
believe the benefits outweigh the costs for your use case.")
|
||||
.short('i')
|
||||
.long("interlace")
|
||||
.value_name("mode")
|
||||
.value_parser(["off", "on", "keep", "0", "1"])
|
||||
.default_value("off")
|
||||
.default_value_if("no-reductions", ArgPredicate::IsPresent, "keep")
|
||||
.hide_possible_values(true),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("scale16")
|
||||
.help("Forcibly reduce 16-bit images to 8-bit (lossy)")
|
||||
.long_help("\
|
||||
Forcibly reduce images with 16 bits per channel to 8 bits per channel. This is a lossy \
|
||||
operation but can provide significant savings when you have no need for higher depth. \
|
||||
Reduction is performed by scaling the values such that, e.g. 0x00FF is reduced to 0x01 \
|
||||
rather than 0x00.
|
||||
|
||||
Without this flag, 16-bit images will only be reduced in depth if it can be done \
|
||||
losslessly.")
|
||||
.long("scale16")
|
||||
.action(ArgAction::SetTrue),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("verbose")
|
||||
.help("Show per-file info (use multiple times for more detail)")
|
||||
.short('v')
|
||||
.long("verbose")
|
||||
.action(ArgAction::Count)
|
||||
.conflicts_with("quiet"),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("quiet")
|
||||
.help("Suppress all output messages")
|
||||
.short('q')
|
||||
.long("quiet")
|
||||
.action(ArgAction::SetTrue)
|
||||
.conflicts_with("verbose"),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("json")
|
||||
.help("Print results as JSON")
|
||||
.short('j')
|
||||
.long("json")
|
||||
.action(ArgAction::SetTrue)
|
||||
.conflicts_with("stdout"),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("filters")
|
||||
.help("Filters to try (0-9; see '--help' for details)")
|
||||
.long_help("\
|
||||
Perform compression trials with each of the given filter types. You can specify a \
|
||||
comma-separated list, or a range of values. E.g. '-f 0-3' is the same as '-f 0,1,2,3'.
|
||||
|
||||
PNG delta filters (apply the same filter to every line)
|
||||
0 => None (recommended to always include this filter)
|
||||
1 => Sub
|
||||
2 => Up
|
||||
3 => Average
|
||||
4 => Paeth
|
||||
|
||||
Heuristic strategies (try to find the best delta filter for each line)
|
||||
5 => MinSum Minimum sum of absolute differences
|
||||
6 => Entropy Smallest Shannon entropy
|
||||
7 => Bigrams Lowest count of distinct bigrams
|
||||
8 => BigEnt Smallest Shannon entropy of bigrams
|
||||
9 => Brute Smallest compressed size (slow)
|
||||
|
||||
The default value depends on the optimization level preset.")
|
||||
.short('f')
|
||||
.long("filters")
|
||||
.value_name("list"),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("fast")
|
||||
.help("Use fast filter evaluation")
|
||||
.long_help("\
|
||||
Perform a fast compression evaluation of each enabled filter, followed by a single main \
|
||||
compression trial of the best result. Recommended if you have more filters enabled than \
|
||||
CPU cores.")
|
||||
.long("fast")
|
||||
.action(ArgAction::SetTrue),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("compression")
|
||||
.help("Deflate compression level (0-12)")
|
||||
.long_help("\
|
||||
Deflate compression level (0-12) for main compression trials. The levels here are defined \
|
||||
by the libdeflate compression library.
|
||||
|
||||
The default value depends on the optimization level preset.")
|
||||
.long("zc")
|
||||
.value_name("level")
|
||||
.value_parser(0..=12)
|
||||
.conflicts_with("zopfli"),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("no-bit-reduction")
|
||||
.help("Do not change bit depth")
|
||||
.long("nb")
|
||||
.action(ArgAction::SetTrue),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("no-color-reduction")
|
||||
.help("Do not change color type")
|
||||
.long("nc")
|
||||
.action(ArgAction::SetTrue),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("no-palette-reduction")
|
||||
.help("Do not change color palette")
|
||||
.long_help("\
|
||||
Do not convert to indexed and do not modify an existing color palette.")
|
||||
.long("np")
|
||||
.action(ArgAction::SetTrue),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("no-grayscale-reduction")
|
||||
.help("Do not change to or from grayscale")
|
||||
.long("ng")
|
||||
.action(ArgAction::SetTrue),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("no-reductions")
|
||||
.help("Do not perform any transformations")
|
||||
.long_help("\
|
||||
Do not perform any transformations and do not deinterlace by default.")
|
||||
.long("nx")
|
||||
.action(ArgAction::SetTrue),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("no-recoding")
|
||||
.help("Do not recompress unless transformations occur")
|
||||
.long_help("\
|
||||
Do not recompress IDAT unless required due to transformations. Recompression of other \
|
||||
compressed chunks (such as iCCP) will also be disabled. Note that the combination of \
|
||||
'--nx' and '--nz' will fully disable all optimization.")
|
||||
.long("nz")
|
||||
.action(ArgAction::SetTrue),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("fix")
|
||||
.help("Disable checksum validation")
|
||||
.long_help("\
|
||||
Do not perform checksum validation of PNG chunks. This may allow some files with errors to \
|
||||
be processed successfully. The output will always have correct checksums.")
|
||||
.long("fix")
|
||||
.action(ArgAction::SetTrue),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("force")
|
||||
.help("Write the output even if it is larger than the input")
|
||||
.long("force")
|
||||
.action(ArgAction::SetTrue),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("zopfli")
|
||||
.help("Use the much slower but stronger Zopfli compressor")
|
||||
.long_help("\
|
||||
Use the much slower but stronger Zopfli compressor for main compression trials. \
|
||||
Recommended use is with '-o max' and '--fast'.")
|
||||
.short('z')
|
||||
.short_alias('Z') // Kept for backwards compatibility
|
||||
.long("zopfli")
|
||||
.action(ArgAction::SetTrue),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("iterations")
|
||||
.help("Number of Zopfli iterations")
|
||||
.long_help("\
|
||||
Set the number of iterations to use for Zopfli compression. Using fewer iterations may \
|
||||
speed up compression for large files. This option requires '--zopfli' to be set.")
|
||||
.long("zi")
|
||||
.value_name("iterations")
|
||||
.default_value("15")
|
||||
.value_parser(value_parser!(NonZeroU64))
|
||||
.requires("zopfli"),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("iterations-without-improvement")
|
||||
.hide_short_help(true)
|
||||
.long_help("\
|
||||
Stop Zopfli compression after this number of iterations without improvement. Use this in \
|
||||
conjunction with a high value for '--zi' to achieve better compression in reasonable time.")
|
||||
.long("ziwi")
|
||||
.value_name("iterations")
|
||||
.value_parser(value_parser!(NonZeroU64))
|
||||
.requires("zopfli"),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("brute-level")
|
||||
.hide_short_help(true)
|
||||
.long_help("\
|
||||
Set the libdeflate compression level to use with the Brute filter strategy. Sane values \
|
||||
are 1-5. Higher values are not necessarily better.")
|
||||
.long("brute-level")
|
||||
.value_name("level")
|
||||
.value_parser(1..=12),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("brute-lines")
|
||||
.hide_short_help(true)
|
||||
.long_help("\
|
||||
Set the number of lines to compress at once with the Brute filter strategy. Sane values \
|
||||
are 2-16. Higher values are not necessarily better.")
|
||||
.long("brute-lines")
|
||||
.value_name("lines")
|
||||
.value_parser(value_parser!(usize)),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("timeout")
|
||||
.help("Maximum amount of time to spend on optimizations")
|
||||
.long_help("\
|
||||
Maximum amount of time, in seconds, to spend on optimizations. Oxipng will check the \
|
||||
timeout before each transformation or compression trial, and will stop trying to optimize \
|
||||
the file if the timeout is exceeded. Note that this does not cut short any operations that \
|
||||
are already in progress, so it is currently of limited effectiveness for large files with \
|
||||
high compression levels.")
|
||||
.long("timeout")
|
||||
.value_name("secs")
|
||||
.value_parser(value_parser!(u64)),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("max-size")
|
||||
.help("Skip image if the decompressed size exceeds this limit")
|
||||
.long_help("\
|
||||
Maximum size to allow for the input image. If the raw, decompressed image data (or the \
|
||||
file size) of the image exceeds this size, it will be skipped. This is useful for limiting \
|
||||
memory usage or avoiding long processing times on large images. The value may be specified \
|
||||
with a unit suffix such as k, KB, m, MB, etc.
|
||||
|
||||
The decompressed size of an image is roughly equal to width * height * bit-depth / 8. E.g. \
|
||||
a 1920x1080 image with 24-bit color depth would be roughly 6MB.")
|
||||
.long("max-raw-size")
|
||||
.value_name("bytes")
|
||||
.value_parser(|s: &str| parse_size(s)),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("threads")
|
||||
.help("Number of threads to use [default: num logical CPUs]")
|
||||
.long_help("\
|
||||
Set the maximum number of threads to use. Oxipng uses multithreading to evaluate multiple \
|
||||
optimizations on the same file in parallel as well as process multiple files in parallel. \
|
||||
You can set this to a lower value if you need to limit memory or CPU usage.
|
||||
|
||||
[default: num logical CPUs]")
|
||||
.short('t')
|
||||
.long("threads")
|
||||
.value_name("num")
|
||||
.value_parser(value_parser!(usize)),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("parallel-files")
|
||||
.help("Process multiple files sequentially")
|
||||
.long_help("\
|
||||
Process multiple files sequentially rather than in parallel. Use this if you need \
|
||||
determinism in the processing order. Note this is not necessary if using '--threads 1'.")
|
||||
.long("sequential")
|
||||
.action(ArgAction::SetFalse),
|
||||
)
|
||||
}
|
||||
160
src/colors.rs
|
|
@ -1,101 +1,129 @@
|
|||
use std::fmt;
|
||||
use std::{fmt, fmt::Display};
|
||||
|
||||
#[derive(Debug,PartialEq,Clone,Copy)]
|
||||
use rgb::{RGB16, RGBA8};
|
||||
|
||||
use crate::PngError;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Clone)]
|
||||
/// The color type used to represent this image
|
||||
pub enum ColorType {
|
||||
/// Grayscale, with one color channel
|
||||
Grayscale,
|
||||
Grayscale {
|
||||
/// Optional shade of gray that should be rendered as transparent
|
||||
transparent_shade: Option<u16>,
|
||||
},
|
||||
/// RGB, with three color channels
|
||||
RGB,
|
||||
/// Indexed, with one byte per pixel representing one of up to 256 colors in the image
|
||||
Indexed,
|
||||
RGB {
|
||||
/// Optional color value that should be rendered as transparent
|
||||
transparent_color: Option<RGB16>,
|
||||
},
|
||||
/// Indexed, with one byte per pixel representing a color from the palette
|
||||
Indexed {
|
||||
/// The palette containing the colors used, up to 256 entries
|
||||
palette: Vec<RGBA8>,
|
||||
},
|
||||
/// Grayscale + Alpha, with two color channels
|
||||
GrayscaleAlpha,
|
||||
/// RGBA, with four color channels
|
||||
RGBA,
|
||||
}
|
||||
|
||||
impl fmt::Display for ColorType {
|
||||
impl Display for ColorType {
|
||||
#[inline]
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f,
|
||||
"{}",
|
||||
match *self {
|
||||
ColorType::Grayscale => "Grayscale",
|
||||
ColorType::RGB => "RGB",
|
||||
ColorType::Indexed => "Indexed",
|
||||
ColorType::GrayscaleAlpha => "Grayscale + Alpha",
|
||||
ColorType::RGBA => "RGB + Alpha",
|
||||
})
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::Grayscale { .. } => write!(f, "Grayscale"),
|
||||
Self::RGB { .. } => write!(f, "RGB"),
|
||||
Self::Indexed { palette } => write!(f, "Indexed ({} colors)", palette.len()),
|
||||
Self::GrayscaleAlpha => write!(f, "Grayscale + Alpha"),
|
||||
Self::RGBA => write!(f, "RGB + Alpha"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ColorType {
|
||||
/// Get the code used by the PNG specification to denote this color type
|
||||
#[inline]
|
||||
pub fn png_header_code(&self) -> u8 {
|
||||
match *self {
|
||||
ColorType::Grayscale => 0,
|
||||
ColorType::RGB => 2,
|
||||
ColorType::Indexed => 3,
|
||||
ColorType::GrayscaleAlpha => 4,
|
||||
ColorType::RGBA => 6,
|
||||
#[must_use]
|
||||
pub const fn png_header_code(&self) -> u8 {
|
||||
match self {
|
||||
Self::Grayscale { .. } => 0,
|
||||
Self::RGB { .. } => 2,
|
||||
Self::Indexed { .. } => 3,
|
||||
Self::GrayscaleAlpha => 4,
|
||||
Self::RGBA => 6,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) const fn channels_per_pixel(&self) -> u8 {
|
||||
match self {
|
||||
Self::Grayscale { .. } | Self::Indexed { .. } => 1,
|
||||
Self::GrayscaleAlpha => 2,
|
||||
Self::RGB { .. } => 3,
|
||||
Self::RGBA => 4,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) const fn is_rgb(&self) -> bool {
|
||||
matches!(self, Self::RGB { .. } | Self::RGBA)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) const fn is_gray(&self) -> bool {
|
||||
matches!(self, Self::Grayscale { .. } | Self::GrayscaleAlpha)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) const fn has_alpha(&self) -> bool {
|
||||
matches!(self, Self::GrayscaleAlpha | Self::RGBA)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) const fn has_trns(&self) -> bool {
|
||||
match self {
|
||||
Self::Grayscale { transparent_shade } => transparent_shade.is_some(),
|
||||
Self::RGB { transparent_color } => transparent_color.is_some(),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug,PartialEq,Clone,Copy)]
|
||||
#[repr(u8)]
|
||||
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
|
||||
/// The number of bits to be used per channel per pixel
|
||||
pub enum BitDepth {
|
||||
/// One bit per channel per pixel
|
||||
One,
|
||||
One = 1,
|
||||
/// Two bits per channel per pixel
|
||||
Two,
|
||||
Two = 2,
|
||||
/// Four bits per channel per pixel
|
||||
Four,
|
||||
Four = 4,
|
||||
/// Eight bits per channel per pixel
|
||||
Eight,
|
||||
Eight = 8,
|
||||
/// Sixteen bits per channel per pixel
|
||||
Sixteen,
|
||||
Sixteen = 16,
|
||||
}
|
||||
|
||||
impl fmt::Display for BitDepth {
|
||||
#[inline]
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f,
|
||||
"{}",
|
||||
match *self {
|
||||
BitDepth::One => "1",
|
||||
BitDepth::Two => "2",
|
||||
BitDepth::Four => "4",
|
||||
BitDepth::Eight => "8",
|
||||
BitDepth::Sixteen => "16",
|
||||
})
|
||||
}
|
||||
}
|
||||
impl TryFrom<u8> for BitDepth {
|
||||
type Error = PngError;
|
||||
|
||||
impl BitDepth {
|
||||
/// Retrieve the number of bits per channel per pixel as a `u8`
|
||||
#[inline]
|
||||
pub fn as_u8(&self) -> u8 {
|
||||
match *self {
|
||||
BitDepth::One => 1,
|
||||
BitDepth::Two => 2,
|
||||
BitDepth::Four => 4,
|
||||
BitDepth::Eight => 8,
|
||||
BitDepth::Sixteen => 16,
|
||||
}
|
||||
}
|
||||
/// Parse a number of bits per channel per pixel into a `BitDepth`
|
||||
#[inline]
|
||||
pub fn from_u8(depth: u8) -> BitDepth {
|
||||
match depth {
|
||||
1 => BitDepth::One,
|
||||
2 => BitDepth::Two,
|
||||
4 => BitDepth::Four,
|
||||
8 => BitDepth::Eight,
|
||||
16 => BitDepth::Sixteen,
|
||||
_ => panic!("Unsupported bit depth"),
|
||||
fn try_from(value: u8) -> Result<Self, Self::Error> {
|
||||
match value {
|
||||
1 => Ok(Self::One),
|
||||
2 => Ok(Self::Two),
|
||||
4 => Ok(Self::Four),
|
||||
8 => Ok(Self::Eight),
|
||||
16 => Ok(Self::Sixteen),
|
||||
_ => Err(PngError::InvalidData),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for BitDepth {
|
||||
#[inline]
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
Display::fmt(&(*self as u8).to_string(), f)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,60 +0,0 @@
|
|||
use libz_sys;
|
||||
use miniz_sys;
|
||||
use libc::c_int;
|
||||
use std::cmp::max;
|
||||
|
||||
/// Decompress a data stream using the DEFLATE algorithm
|
||||
pub fn inflate(data: &[u8]) -> Result<Vec<u8>, String> {
|
||||
let mut input = data.to_owned();
|
||||
let mut stream = super::libz_stream::Stream::new_decompress();
|
||||
let mut output = Vec::with_capacity(data.len());
|
||||
loop {
|
||||
match stream.decompress_vec(input.as_mut(), output.as_mut()) {
|
||||
libz_sys::Z_OK => output.reserve(data.len()),
|
||||
libz_sys::Z_STREAM_END => break,
|
||||
c => return Err(format!("Error code on decompress: {}", c)),
|
||||
}
|
||||
}
|
||||
output.shrink_to_fit();
|
||||
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
/// Compress a data stream using the zlib implementation of the DEFLATE algorithm
|
||||
pub fn deflate(data: &[u8], zc: u8, zm: u8, zs: u8, zw: u8) -> Result<Vec<u8>, String> {
|
||||
let mut input = data.to_owned();
|
||||
// Compressed input should be smaller than decompressed, so allocate less than data.len()
|
||||
// However, it needs a minimum capacity in order to handle very small images
|
||||
let mut output = Vec::with_capacity(max(1024, data.len() / 20));
|
||||
if zs == 0 || zs == 1 {
|
||||
// Miniz performs 1-2 orders of magnitude better for strategies 0 and 1
|
||||
let mut stream = super::miniz_stream::Stream::new_compress(zc as c_int,
|
||||
zw as c_int,
|
||||
zm as c_int,
|
||||
zs as c_int);
|
||||
loop {
|
||||
match stream.compress_vec(input.as_mut(), output.as_mut()) {
|
||||
miniz_sys::MZ_OK => output.reserve(max(1024, data.len() / 20)),
|
||||
miniz_sys::MZ_STREAM_END => break,
|
||||
c => return Err(format!("Error code on compress: {}", c)),
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// libz performs an order of magnitude better for strategies 2 and 3
|
||||
let mut stream = super::libz_stream::Stream::new_compress(zc as c_int,
|
||||
zw as c_int,
|
||||
zm as c_int,
|
||||
zs as c_int);
|
||||
loop {
|
||||
match stream.compress_vec(input.as_mut(), output.as_mut()) {
|
||||
libz_sys::Z_OK => output.reserve(max(1024, data.len() / 20)),
|
||||
libz_sys::Z_STREAM_END => break,
|
||||
c => return Err(format!("Error code on compress: {}", c)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
output.shrink_to_fit();
|
||||
|
||||
Ok(output)
|
||||
}
|
||||
36
src/deflate/deflater.rs
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
use libdeflater::*;
|
||||
|
||||
use crate::{PngError, PngResult};
|
||||
|
||||
pub fn deflate(data: &[u8], level: u8, max_size: Option<usize>) -> PngResult<Vec<u8>> {
|
||||
let mut compressor = Compressor::new(CompressionLvl::new(level.into()).unwrap());
|
||||
let capacity = max_size.unwrap_or_else(|| compressor.zlib_compress_bound(data.len()));
|
||||
let mut dest = vec![0; capacity];
|
||||
let len = compressor
|
||||
.zlib_compress(data, &mut dest)
|
||||
.map_err(|err| match err {
|
||||
CompressionError::InsufficientSpace => PngError::DeflatedDataTooLong(capacity),
|
||||
})?;
|
||||
dest.truncate(len);
|
||||
Ok(dest)
|
||||
}
|
||||
|
||||
pub fn inflate(data: &[u8], out_size: usize) -> PngResult<Vec<u8>> {
|
||||
let mut decompressor = Decompressor::new();
|
||||
let mut dest = vec![0; out_size];
|
||||
let len = decompressor
|
||||
.zlib_decompress(data, &mut dest)
|
||||
.map_err(|err| match err {
|
||||
DecompressionError::BadData => PngError::InvalidData,
|
||||
DecompressionError::InsufficientSpace => PngError::InflatedDataTooLong(out_size),
|
||||
})?;
|
||||
dest.truncate(len);
|
||||
Ok(dest)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn crc32(data: &[u8]) -> u32 {
|
||||
let mut crc = Crc::new();
|
||||
crc.update(data);
|
||||
crc.sum()
|
||||
}
|
||||
|
|
@ -1,136 +0,0 @@
|
|||
// Raw un-exported bindings to libz for encoding/decoding
|
||||
// Copyright (c) 2014 Alex Crichton, MIT & Apache licenses
|
||||
// Originally from flate2 crate for miniz
|
||||
// Modified for use in oxipng
|
||||
|
||||
use std::marker;
|
||||
use std::mem;
|
||||
use libc::{c_int, c_uint};
|
||||
use libz_sys;
|
||||
|
||||
pub struct Stream<D: Direction> {
|
||||
raw: libz_sys::z_stream,
|
||||
_marker: marker::PhantomData<D>,
|
||||
}
|
||||
|
||||
pub enum Compress {}
|
||||
pub enum Decompress {}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub trait Direction {
|
||||
unsafe fn destroy(stream: *mut libz_sys::z_stream) -> c_int;
|
||||
}
|
||||
|
||||
impl Stream<Compress> {
|
||||
pub fn new_compress(lvl: c_int,
|
||||
window_bits: c_int,
|
||||
mem_size: c_int,
|
||||
strategy: c_int)
|
||||
-> Stream<Compress> {
|
||||
unsafe {
|
||||
let mut state: libz_sys::z_stream = mem::zeroed();
|
||||
let ret = libz_sys::deflateInit2_(&mut state,
|
||||
lvl,
|
||||
libz_sys::Z_DEFLATED,
|
||||
window_bits,
|
||||
mem_size,
|
||||
strategy,
|
||||
libz_sys::zlibVersion(),
|
||||
mem::size_of::<libz_sys::z_stream>() as i32);
|
||||
debug_assert_eq!(ret, 0);
|
||||
Stream {
|
||||
raw: state,
|
||||
_marker: marker::PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_decompress() -> Stream<Decompress> {
|
||||
unsafe {
|
||||
let mut state: libz_sys::z_stream = mem::zeroed();
|
||||
let ret = libz_sys::inflateInit2_(&mut state,
|
||||
15,
|
||||
libz_sys::zlibVersion(),
|
||||
mem::size_of::<libz_sys::z_stream>() as i32);
|
||||
debug_assert_eq!(ret, 0);
|
||||
Stream {
|
||||
raw: state,
|
||||
_marker: marker::PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Direction> Stream<T> {
|
||||
#[inline]
|
||||
pub fn total_in(&self) -> usize {
|
||||
self.raw.total_in as usize
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn total_out(&self) -> usize {
|
||||
self.raw.total_out as usize
|
||||
}
|
||||
}
|
||||
|
||||
impl Stream<Decompress> {
|
||||
pub fn decompress_vec(&mut self, input: &mut [u8], output: &mut Vec<u8>) -> c_int {
|
||||
self.raw.avail_in = (input.len() - self.total_in()) as c_uint;
|
||||
self.raw.avail_out = (output.capacity() - self.total_out()) as c_uint;
|
||||
|
||||
unsafe {
|
||||
self.raw.next_in = input.as_mut_ptr().offset(self.total_in() as isize);
|
||||
self.raw.next_out = output.as_mut_ptr().offset(self.total_out() as isize);
|
||||
let rc = libz_sys::inflate(&mut self.raw, libz_sys::Z_NO_FLUSH);
|
||||
output.set_len(self.total_out() as usize);
|
||||
rc
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Stream<Compress> {
|
||||
pub fn compress_vec(&mut self, input: &mut [u8], output: &mut Vec<u8>) -> c_int {
|
||||
self.raw.avail_in = (input.len() - self.total_in() as usize) as c_uint;
|
||||
self.raw.avail_out = (output.capacity() - self.total_out() as usize) as c_uint;
|
||||
|
||||
unsafe {
|
||||
self.raw.next_in = input.as_mut_ptr().offset(self.total_in() as isize);
|
||||
self.raw.next_out = output.as_mut_ptr().offset(self.total_out() as isize);
|
||||
let rc = libz_sys::deflate(&mut self.raw,
|
||||
if self.raw.avail_in > 0 {
|
||||
libz_sys::Z_NO_FLUSH
|
||||
} else {
|
||||
libz_sys::Z_FINISH
|
||||
});
|
||||
output.set_len(self.total_out() as usize);
|
||||
rc
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn reset(&mut self) -> c_int {
|
||||
unsafe { libz_sys::deflateReset(&mut self.raw) }
|
||||
}
|
||||
}
|
||||
|
||||
impl Direction for Compress {
|
||||
#[inline]
|
||||
unsafe fn destroy(stream: *mut libz_sys::z_stream) -> c_int {
|
||||
libz_sys::deflateEnd(stream)
|
||||
}
|
||||
}
|
||||
impl Direction for Decompress {
|
||||
#[inline]
|
||||
unsafe fn destroy(stream: *mut libz_sys::z_stream) -> c_int {
|
||||
libz_sys::inflateEnd(stream)
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Direction> Drop for Stream<D> {
|
||||
#[inline]
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
let _ = <D as Direction>::destroy(&mut self.raw);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,131 +0,0 @@
|
|||
// Raw un-exported bindings to miniz for encoding/decoding
|
||||
// Copyright (c) 2014 Alex Crichton, MIT & Apache licenses
|
||||
// Originally from flate2 crate
|
||||
// Modified for use in oxipng
|
||||
|
||||
use std::marker;
|
||||
use std::mem;
|
||||
use libc::{c_int, c_uint};
|
||||
use miniz_sys;
|
||||
|
||||
pub struct Stream<D: Direction> {
|
||||
raw: miniz_sys::mz_stream,
|
||||
_marker: marker::PhantomData<D>,
|
||||
}
|
||||
|
||||
pub enum Compress {}
|
||||
pub enum Decompress {}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub trait Direction {
|
||||
unsafe fn destroy(stream: *mut miniz_sys::mz_stream) -> c_int;
|
||||
}
|
||||
|
||||
impl Stream<Compress> {
|
||||
pub fn new_compress(lvl: c_int,
|
||||
window_bits: c_int,
|
||||
mem_size: c_int,
|
||||
strategy: c_int)
|
||||
-> Stream<Compress> {
|
||||
unsafe {
|
||||
let mut state: miniz_sys::mz_stream = mem::zeroed();
|
||||
let ret = miniz_sys::mz_deflateInit2(&mut state,
|
||||
lvl,
|
||||
miniz_sys::MZ_DEFLATED,
|
||||
window_bits,
|
||||
mem_size,
|
||||
strategy);
|
||||
debug_assert_eq!(ret, 0);
|
||||
Stream {
|
||||
raw: state,
|
||||
_marker: marker::PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_decompress() -> Stream<Decompress> {
|
||||
unsafe {
|
||||
let mut state: miniz_sys::mz_stream = mem::zeroed();
|
||||
let ret = miniz_sys::mz_inflateInit2(&mut state, 15);
|
||||
debug_assert_eq!(ret, 0);
|
||||
Stream {
|
||||
raw: state,
|
||||
_marker: marker::PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Direction> Stream<T> {
|
||||
#[inline]
|
||||
pub fn total_in(&self) -> usize {
|
||||
self.raw.total_in as usize
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn total_out(&self) -> usize {
|
||||
self.raw.total_out as usize
|
||||
}
|
||||
}
|
||||
|
||||
impl Stream<Decompress> {
|
||||
pub fn decompress_vec(&mut self, input: &mut [u8], output: &mut Vec<u8>) -> c_int {
|
||||
self.raw.avail_in = (input.len() - self.total_in()) as c_uint;
|
||||
self.raw.avail_out = (output.capacity() - self.total_out()) as c_uint;
|
||||
|
||||
unsafe {
|
||||
self.raw.next_in = input.as_mut_ptr().offset(self.total_in() as isize);
|
||||
self.raw.next_out = output.as_mut_ptr().offset(self.total_out() as isize);
|
||||
let rc = miniz_sys::mz_inflate(&mut self.raw, miniz_sys::MZ_NO_FLUSH);
|
||||
output.set_len(self.total_out() as usize);
|
||||
rc
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Stream<Compress> {
|
||||
pub fn compress_vec(&mut self, input: &mut [u8], output: &mut Vec<u8>) -> c_int {
|
||||
self.raw.avail_in = (input.len() - self.total_in() as usize) as c_uint;
|
||||
self.raw.avail_out = (output.capacity() - self.total_out() as usize) as c_uint;
|
||||
|
||||
unsafe {
|
||||
self.raw.next_in = input.as_mut_ptr().offset(self.total_in() as isize);
|
||||
self.raw.next_out = output.as_mut_ptr().offset(self.total_out() as isize);
|
||||
let rc = miniz_sys::mz_deflate(&mut self.raw,
|
||||
if self.raw.avail_in > 0 {
|
||||
miniz_sys::MZ_NO_FLUSH
|
||||
} else {
|
||||
miniz_sys::MZ_FINISH
|
||||
});
|
||||
output.set_len(self.total_out() as usize);
|
||||
rc
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn reset(&mut self) -> c_int {
|
||||
unsafe { miniz_sys::mz_deflateReset(&mut self.raw) }
|
||||
}
|
||||
}
|
||||
|
||||
impl Direction for Compress {
|
||||
#[inline]
|
||||
unsafe fn destroy(stream: *mut miniz_sys::mz_stream) -> c_int {
|
||||
miniz_sys::mz_deflateEnd(stream)
|
||||
}
|
||||
}
|
||||
impl Direction for Decompress {
|
||||
#[inline]
|
||||
unsafe fn destroy(stream: *mut miniz_sys::mz_stream) -> c_int {
|
||||
miniz_sys::mz_inflateEnd(stream)
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Direction> Drop for Stream<D> {
|
||||
#[inline]
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
let _ = <D as Direction>::destroy(&mut self.raw);
|
||||
}
|
||||
}
|
||||
}
|
||||
53
src/deflate/mod.rs
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
mod deflater;
|
||||
use std::{fmt, fmt::Display};
|
||||
|
||||
pub use deflater::{crc32, deflate, inflate};
|
||||
|
||||
use crate::{PngError, PngResult};
|
||||
|
||||
#[cfg(feature = "zopfli")]
|
||||
mod zopfli_oxipng;
|
||||
#[cfg(feature = "zopfli")]
|
||||
pub use zopfli::Options as ZopfliOptions;
|
||||
#[cfg(feature = "zopfli")]
|
||||
pub use zopfli_oxipng::deflate as zopfli_deflate;
|
||||
|
||||
/// DEFLATE algorithms supported by oxipng (for use in [`Options`][crate::Options])
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum Deflater {
|
||||
/// Use libdeflater.
|
||||
Libdeflater {
|
||||
/// Which compression level to use on the file (0-12)
|
||||
compression: u8,
|
||||
},
|
||||
#[cfg(feature = "zopfli")]
|
||||
/// Use the better but slower Zopfli implementation
|
||||
Zopfli(ZopfliOptions),
|
||||
}
|
||||
|
||||
impl Deflater {
|
||||
pub(crate) fn deflate(self, data: &[u8], max_size: Option<usize>) -> PngResult<Vec<u8>> {
|
||||
let compressed = match self {
|
||||
Self::Libdeflater { compression } => deflate(data, compression, max_size)?,
|
||||
#[cfg(feature = "zopfli")]
|
||||
Self::Zopfli(options) => zopfli_deflate(data, options)?,
|
||||
};
|
||||
if let Some(max) = max_size {
|
||||
if compressed.len() > max {
|
||||
return Err(PngError::DeflatedDataTooLong(max));
|
||||
}
|
||||
}
|
||||
Ok(compressed)
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Deflater {
|
||||
#[inline]
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::Libdeflater { compression } => write!(f, "zc = {compression}"),
|
||||
#[cfg(feature = "zopfli")]
|
||||
Self::Zopfli(options) => write!(f, "zopfli, zi = {}", options.iteration_count),
|
||||
}
|
||||
}
|
||||
}
|
||||
13
src/deflate/zopfli_oxipng.rs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
use crate::{PngError, PngResult};
|
||||
|
||||
pub fn deflate(data: &[u8], options: zopfli::Options) -> PngResult<Vec<u8>> {
|
||||
let mut output = Vec::with_capacity(data.len());
|
||||
// Since Rust v1.74, passing &[u8] directly into zopfli causes a regression in compressed size
|
||||
// for some files. Wrapping the slice in another Read implementer such as Box fixes it for now.
|
||||
match zopfli::compress(options, zopfli::Format::Zlib, Box::new(data), &mut output) {
|
||||
Ok(()) => (),
|
||||
Err(_) => return Err(PngError::new("Failed to compress in zopfli")),
|
||||
}
|
||||
output.shrink_to_fit();
|
||||
Ok(output)
|
||||
}
|
||||
4
src/display_chunks.rs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
/// List of chunks that affect image display and will be kept when using the `Safe` chunk strip option
|
||||
pub const DISPLAY_CHUNKS: [[u8; 4]; 7] = [
|
||||
*b"cICP", *b"iCCP", *b"sRGB", *b"pHYs", *b"acTL", *b"fcTL", *b"fdAT",
|
||||
];
|
||||
69
src/error.rs
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
use std::{error::Error, fmt};
|
||||
|
||||
use crate::colors::{BitDepth, ColorType};
|
||||
|
||||
#[derive(Debug)]
|
||||
#[non_exhaustive]
|
||||
pub enum PngError {
|
||||
APNGOutOfOrder,
|
||||
C2PAMetadataPreventsChanges,
|
||||
ChunkMissing(&'static str),
|
||||
CRCMismatch([u8; 4]),
|
||||
DeflatedDataTooLong(usize),
|
||||
IncorrectDataLength(usize, usize),
|
||||
InflatedDataTooLong(usize),
|
||||
InvalidData,
|
||||
InvalidDepthForType(BitDepth, ColorType),
|
||||
NotPNG,
|
||||
ReadFailed(String, std::io::Error),
|
||||
TruncatedData,
|
||||
WriteFailed(String, std::io::Error),
|
||||
Other(Box<str>),
|
||||
}
|
||||
|
||||
impl Error for PngError {}
|
||||
|
||||
impl fmt::Display for PngError {
|
||||
#[inline]
|
||||
#[cold]
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match *self {
|
||||
Self::APNGOutOfOrder => f.write_str("APNG chunks are out of order"),
|
||||
Self::C2PAMetadataPreventsChanges => f.write_str(
|
||||
"The image contains C2PA manifest that would be invalidated by any file changes",
|
||||
),
|
||||
Self::ChunkMissing(s) => write!(f, "Chunk {s} missing or empty"),
|
||||
Self::CRCMismatch(ref c) => write!(
|
||||
f,
|
||||
"CRC mismatch in {} chunk; May be recoverable by using --fix",
|
||||
String::from_utf8_lossy(c)
|
||||
),
|
||||
Self::DeflatedDataTooLong(_) => f.write_str("Deflated data too long"),
|
||||
Self::IncorrectDataLength(l1, l2) => write!(
|
||||
f,
|
||||
"Data length {l1} does not match the expected length {l2}"
|
||||
),
|
||||
Self::InflatedDataTooLong(max) => write!(
|
||||
f,
|
||||
"Inflated data would exceed the maximum size ({max} bytes)"
|
||||
),
|
||||
Self::InvalidData => f.write_str("Invalid data found; unable to read PNG file"),
|
||||
Self::InvalidDepthForType(d, ref c) => {
|
||||
write!(f, "Invalid bit depth {d} for color type {c}")
|
||||
}
|
||||
Self::NotPNG => f.write_str("Invalid header detected; Not a PNG file"),
|
||||
Self::ReadFailed(ref s, ref e) => write!(f, "Failed to read from {s}: {e}"),
|
||||
Self::TruncatedData => f.write_str("Missing data in the file; the file is truncated"),
|
||||
Self::WriteFailed(ref s, ref e) => write!(f, "Failed to write to {s}: {e}"),
|
||||
Self::Other(ref s) => f.write_str(s),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PngError {
|
||||
#[cold]
|
||||
#[must_use]
|
||||
pub fn new(description: &str) -> Self {
|
||||
Self::Other(description.into())
|
||||
}
|
||||
}
|
||||
202
src/evaluate.rs
Normal file
|
|
@ -0,0 +1,202 @@
|
|||
//! Check if a reduction makes file smaller, and keep best reductions.
|
||||
//! Works asynchronously when possible
|
||||
|
||||
#[cfg(not(feature = "parallel"))]
|
||||
use std::cell::RefCell;
|
||||
use std::sync::{
|
||||
Arc,
|
||||
atomic::{AtomicUsize, Ordering::*},
|
||||
};
|
||||
|
||||
use deflate::Deflater;
|
||||
use indexmap::IndexSet;
|
||||
use log::trace;
|
||||
use rayon::prelude::*;
|
||||
#[cfg(feature = "parallel")]
|
||||
use std::sync::mpsc::{Receiver, Sender, channel};
|
||||
|
||||
#[cfg(not(feature = "parallel"))]
|
||||
use crate::rayon;
|
||||
use crate::{
|
||||
Deadline, PngError, atomicmin::AtomicMin, deflate, filters::FilterStrategy, png::PngImage,
|
||||
};
|
||||
|
||||
pub(crate) struct Candidate {
|
||||
pub image: Arc<PngImage>,
|
||||
pub idat_data: Option<Vec<u8>>,
|
||||
pub estimated_output_size: usize,
|
||||
/// The input filter, which is retained for printing and for APNG frames.
|
||||
pub filter: FilterStrategy,
|
||||
/// The filter returned by the filter function, which may be Predefined.
|
||||
/// Use this for the next round to avoid recomputing the filter.
|
||||
pub filter_used: FilterStrategy,
|
||||
/// For determining tie-breaker
|
||||
nth: usize,
|
||||
}
|
||||
|
||||
impl Candidate {
|
||||
fn cmp_key(&self) -> impl Ord + use<> {
|
||||
(
|
||||
self.estimated_output_size,
|
||||
self.image.data.len(),
|
||||
self.filter.clone(),
|
||||
// Prefer the later image added (e.g. baseline, which is always added last)
|
||||
usize::MAX - self.nth,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Collect image versions and pick one that compresses best
|
||||
pub(crate) struct Evaluator {
|
||||
deadline: Arc<Deadline>,
|
||||
filters: IndexSet<FilterStrategy>,
|
||||
deflater: Deflater,
|
||||
optimize_alpha: bool,
|
||||
final_round: bool,
|
||||
nth: AtomicUsize,
|
||||
executed: Arc<AtomicUsize>,
|
||||
best_candidate_size: Arc<AtomicMin>,
|
||||
/// images are sent to the caller thread for evaluation
|
||||
#[cfg(feature = "parallel")]
|
||||
eval_channel: (Sender<Candidate>, Receiver<Candidate>),
|
||||
// in non-parallel mode, images are evaluated synchronously
|
||||
#[cfg(not(feature = "parallel"))]
|
||||
eval_best_candidate: RefCell<Option<Candidate>>,
|
||||
}
|
||||
|
||||
impl Evaluator {
|
||||
pub fn new(
|
||||
deadline: Arc<Deadline>,
|
||||
filters: IndexSet<FilterStrategy>,
|
||||
deflater: Deflater,
|
||||
optimize_alpha: bool,
|
||||
final_round: bool,
|
||||
) -> Self {
|
||||
#[cfg(feature = "parallel")]
|
||||
let eval_channel = channel();
|
||||
Self {
|
||||
deadline,
|
||||
filters,
|
||||
deflater,
|
||||
optimize_alpha,
|
||||
final_round,
|
||||
nth: AtomicUsize::new(0),
|
||||
executed: Arc::new(AtomicUsize::new(0)),
|
||||
best_candidate_size: Arc::new(AtomicMin::new(None)),
|
||||
#[cfg(feature = "parallel")]
|
||||
eval_channel,
|
||||
#[cfg(not(feature = "parallel"))]
|
||||
eval_best_candidate: RefCell::new(None),
|
||||
}
|
||||
}
|
||||
|
||||
/// Wait for all evaluations to finish and return smallest reduction
|
||||
/// Or `None` if the queue is empty.
|
||||
#[cfg(feature = "parallel")]
|
||||
pub fn get_best_candidate(self) -> Option<Candidate> {
|
||||
let (eval_send, eval_recv) = self.eval_channel;
|
||||
// Disconnect the sender, breaking the loop in the thread
|
||||
drop(eval_send);
|
||||
let nth = self.nth.load(SeqCst);
|
||||
// Yield to ensure all evaluations are executed
|
||||
// This can prevent deadlocks when run within an existing rayon thread pool
|
||||
while self.executed.load(Relaxed) < nth {
|
||||
rayon::yield_local();
|
||||
}
|
||||
eval_recv.into_iter().min_by_key(Candidate::cmp_key)
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "parallel"))]
|
||||
pub fn get_best_candidate(self) -> Option<Candidate> {
|
||||
self.eval_best_candidate.into_inner()
|
||||
}
|
||||
|
||||
/// Set best size, if known in advance
|
||||
pub fn set_best_size(&self, size: usize) {
|
||||
self.best_candidate_size.set_min(size);
|
||||
}
|
||||
|
||||
/// Check if the image is smaller than others
|
||||
pub fn try_image(&self, image: Arc<PngImage>) {
|
||||
let description = image.ihdr.color_type.to_string();
|
||||
self.try_image_with_description(image, &description);
|
||||
}
|
||||
|
||||
/// Check if the image is smaller than others, with a description for verbose mode
|
||||
pub fn try_image_with_description(&self, image: Arc<PngImage>, description: &str) {
|
||||
let nth = self.nth.fetch_add(1, SeqCst);
|
||||
// These clones are only cheap refcounts
|
||||
let deadline = self.deadline.clone();
|
||||
let filters = self.filters.clone();
|
||||
let deflater = self.deflater;
|
||||
let optimize_alpha = self.optimize_alpha;
|
||||
let final_round = self.final_round;
|
||||
let executed = self.executed.clone();
|
||||
let best_candidate_size = self.best_candidate_size.clone();
|
||||
let description = description.to_string();
|
||||
// sends it off asynchronously for compression,
|
||||
// but results will be collected via the message queue
|
||||
#[cfg(feature = "parallel")]
|
||||
let eval_send = self.eval_channel.0.clone();
|
||||
rayon::spawn(move || {
|
||||
executed.fetch_add(1, Relaxed);
|
||||
let filters_iter = filters.par_iter().with_max_len(1);
|
||||
|
||||
// Updating of best result inside the parallel loop would require locks,
|
||||
// which are dangerous to do in side Rayon's loop.
|
||||
// Instead, only update (atomic) best size in real time,
|
||||
// and the best result later without need for locks.
|
||||
filters_iter.for_each(|filter| {
|
||||
if deadline.passed() {
|
||||
return;
|
||||
}
|
||||
let (filtered, filter_used) = image.filter_image(filter.clone(), optimize_alpha);
|
||||
let idat_data = deflater.deflate(&filtered, best_candidate_size.get());
|
||||
if let Ok(idat_data) = idat_data {
|
||||
let estimated_output_size = image.estimated_output_size(&idat_data);
|
||||
trace!(
|
||||
"Eval: {}-bit {:23} {:8} {} bytes",
|
||||
image.ihdr.bit_depth, description, filter, estimated_output_size
|
||||
);
|
||||
|
||||
// Skip if it exceeds best known size. (This is important to ensure
|
||||
// the evaluator returns no result when all candidates are too large.)
|
||||
if let Some(max) = best_candidate_size.get() {
|
||||
if estimated_output_size > max {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// We only need to retain the IDAT data in the final round
|
||||
let new = Candidate {
|
||||
image: image.clone(),
|
||||
idat_data: if final_round { Some(idat_data) } else { None },
|
||||
estimated_output_size,
|
||||
filter: filter.clone(),
|
||||
filter_used,
|
||||
nth,
|
||||
};
|
||||
best_candidate_size.set_min(estimated_output_size);
|
||||
|
||||
#[cfg(feature = "parallel")]
|
||||
{
|
||||
eval_send.send(new).expect("send");
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "parallel"))]
|
||||
{
|
||||
match &mut *self.eval_best_candidate.borrow_mut() {
|
||||
Some(prev) if prev.cmp_key() < new.cmp_key() => {}
|
||||
best => *best = Some(new),
|
||||
}
|
||||
}
|
||||
} else if let Err(PngError::DeflatedDataTooLong(size)) = idat_data {
|
||||
trace!(
|
||||
"Eval: {}-bit {:23} {:8} >{} bytes",
|
||||
image.ihdr.bit_depth, description, filter, size
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
427
src/filters.rs
|
|
@ -1,158 +1,285 @@
|
|||
pub fn filter_line(filter: u8, bpp: usize, data: &[u8], last_line: &[u8]) -> Vec<u8> {
|
||||
let mut filtered = Vec::with_capacity(data.len());
|
||||
match filter {
|
||||
0 => {
|
||||
filtered.extend_from_slice(data);
|
||||
}
|
||||
1 => {
|
||||
filtered.extend_from_slice(&data[0..bpp]);
|
||||
filtered.extend_from_slice(&data.iter()
|
||||
.skip(bpp)
|
||||
.zip(data.iter())
|
||||
.map(|(cur, last)| cur.wrapping_sub(*last))
|
||||
.collect::<Vec<u8>>());
|
||||
}
|
||||
2 => {
|
||||
if last_line.is_empty() {
|
||||
filtered.extend_from_slice(data);
|
||||
} else {
|
||||
filtered.extend_from_slice(&data.iter()
|
||||
.zip(last_line.iter())
|
||||
.map(|(cur, last)| cur.wrapping_sub(*last))
|
||||
.collect::<Vec<u8>>());
|
||||
};
|
||||
}
|
||||
3 => {
|
||||
for (i, byte) in data.iter().enumerate() {
|
||||
if last_line.is_empty() {
|
||||
filtered.push(match i.checked_sub(bpp) {
|
||||
Some(x) => byte.wrapping_sub(data[x] >> 1),
|
||||
None => *byte,
|
||||
});
|
||||
} else {
|
||||
filtered.push(match i.checked_sub(bpp) {
|
||||
Some(x) => {
|
||||
byte.wrapping_sub(((data[x] as u16 + last_line[i] as u16) >> 1) as u8)
|
||||
}
|
||||
None => byte.wrapping_sub(last_line[i] >> 1),
|
||||
});
|
||||
};
|
||||
}
|
||||
}
|
||||
4 => {
|
||||
for (i, byte) in data.iter().enumerate() {
|
||||
if last_line.is_empty() {
|
||||
filtered.push(match i.checked_sub(bpp) {
|
||||
Some(x) => byte.wrapping_sub(data[x]),
|
||||
None => *byte,
|
||||
});
|
||||
} else {
|
||||
filtered.push(match i.checked_sub(bpp) {
|
||||
Some(x) => {
|
||||
byte.wrapping_sub(paeth_predictor(data[x], last_line[i], last_line[x]))
|
||||
}
|
||||
None => byte.wrapping_sub(last_line[i]),
|
||||
});
|
||||
};
|
||||
}
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
filtered
|
||||
use std::{fmt, fmt::Display, mem::transmute};
|
||||
|
||||
/// Filtering strategy for use in [`Options`][crate::Options]
|
||||
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Hash)]
|
||||
pub enum FilterStrategy {
|
||||
/// Same filter for all rows
|
||||
Basic(RowFilter),
|
||||
/// Minimum sum of absolute differences
|
||||
MinSum,
|
||||
/// Shannon entropy
|
||||
Entropy,
|
||||
/// Count of distinct bigrams
|
||||
Bigrams,
|
||||
/// Shannon entropy of bigrams
|
||||
BigEnt,
|
||||
/// Deflate compression
|
||||
Brute {
|
||||
/// The number of lines to compress at once
|
||||
num_lines: usize,
|
||||
/// The compression level to use (1-12)
|
||||
level: u8,
|
||||
},
|
||||
/// Predefined filter for each row
|
||||
Predefined(Vec<RowFilter>),
|
||||
}
|
||||
|
||||
pub fn unfilter_line(filter: u8, bpp: usize, data: &[u8], last_line: &[u8]) -> Vec<u8> {
|
||||
let mut unfiltered = Vec::with_capacity(data.len());
|
||||
match filter {
|
||||
0 => {
|
||||
unfiltered.extend_from_slice(data);
|
||||
}
|
||||
1 => {
|
||||
for (i, byte) in data.iter().enumerate() {
|
||||
match i.checked_sub(bpp) {
|
||||
Some(x) => {
|
||||
let b = unfiltered[x];
|
||||
unfiltered.push(byte.wrapping_add(b));
|
||||
}
|
||||
None => {
|
||||
unfiltered.push(*byte);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
2 => {
|
||||
if last_line.is_empty() {
|
||||
unfiltered.extend_from_slice(data);
|
||||
} else {
|
||||
unfiltered.extend_from_slice(&data.iter()
|
||||
.zip(last_line.iter())
|
||||
.map(|(cur, last)| cur.wrapping_add(*last))
|
||||
.collect::<Vec<u8>>());
|
||||
};
|
||||
}
|
||||
3 => {
|
||||
for (i, byte) in data.iter().enumerate() {
|
||||
if last_line.is_empty() {
|
||||
match i.checked_sub(bpp) {
|
||||
Some(x) => {
|
||||
let b = unfiltered[x];
|
||||
unfiltered.push(byte.wrapping_add(b >> 1));
|
||||
}
|
||||
None => {
|
||||
unfiltered.push(*byte);
|
||||
}
|
||||
};
|
||||
} else {
|
||||
match i.checked_sub(bpp) {
|
||||
Some(x) => {
|
||||
let b = unfiltered[x];
|
||||
unfiltered.push(byte.wrapping_add(((b as u16 + last_line[i] as u16) >> 1) as u8));
|
||||
}
|
||||
None => {
|
||||
unfiltered.push(byte.wrapping_add(last_line[i] >> 1));
|
||||
}
|
||||
};
|
||||
};
|
||||
}
|
||||
}
|
||||
4 => {
|
||||
for (i, byte) in data.iter().enumerate() {
|
||||
if last_line.is_empty() {
|
||||
match i.checked_sub(bpp) {
|
||||
Some(x) => {
|
||||
let b = unfiltered[x];
|
||||
unfiltered.push(byte.wrapping_add(b));
|
||||
}
|
||||
None => {
|
||||
unfiltered.push(*byte);
|
||||
}
|
||||
};
|
||||
} else {
|
||||
match i.checked_sub(bpp) {
|
||||
Some(x) => {
|
||||
let b = unfiltered[x];
|
||||
unfiltered.push(byte.wrapping_add(paeth_predictor(b,
|
||||
last_line[i],
|
||||
last_line[x])));
|
||||
}
|
||||
None => {
|
||||
unfiltered.push(byte.wrapping_add(last_line[i]));
|
||||
}
|
||||
};
|
||||
};
|
||||
}
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
unfiltered
|
||||
impl FilterStrategy {
|
||||
pub const NONE: Self = Self::Basic(RowFilter::None);
|
||||
pub const SUB: Self = Self::Basic(RowFilter::Sub);
|
||||
pub const UP: Self = Self::Basic(RowFilter::Up);
|
||||
pub const AVERAGE: Self = Self::Basic(RowFilter::Average);
|
||||
pub const PAETH: Self = Self::Basic(RowFilter::Paeth);
|
||||
}
|
||||
|
||||
impl Display for FilterStrategy {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::Basic(filter) => filter.fmt(f),
|
||||
Self::MinSum => "MinSum".fmt(f),
|
||||
Self::Entropy => "Entropy".fmt(f),
|
||||
Self::Bigrams => "Bigrams".fmt(f),
|
||||
Self::BigEnt => "BigEnt".fmt(f),
|
||||
Self::Brute { .. } => "Brute".fmt(f),
|
||||
Self::Predefined(_) => "Predefined".fmt(f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// PNG delta filters
|
||||
#[repr(u8)]
|
||||
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
|
||||
pub enum RowFilter {
|
||||
None,
|
||||
Sub,
|
||||
Up,
|
||||
Average,
|
||||
Paeth,
|
||||
}
|
||||
|
||||
impl TryFrom<u8> for RowFilter {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(value: u8) -> Result<Self, Self::Error> {
|
||||
if value > 4 {
|
||||
return Err(());
|
||||
}
|
||||
unsafe { transmute(value as i8) }
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for RowFilter {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
Display::fmt(
|
||||
match self {
|
||||
Self::None => "None",
|
||||
Self::Sub => "Sub",
|
||||
Self::Up => "Up",
|
||||
Self::Average => "Average",
|
||||
Self::Paeth => "Paeth",
|
||||
},
|
||||
f,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl RowFilter {
|
||||
pub(crate) const ALL: [Self; 5] = [Self::None, Self::Sub, Self::Up, Self::Average, Self::Paeth];
|
||||
pub(crate) const SINGLE_LINE: [Self; 2] = [Self::None, Self::Sub];
|
||||
|
||||
pub(crate) fn filter_line(
|
||||
self,
|
||||
bpp: usize,
|
||||
data: &mut [u8],
|
||||
prev_line: &[u8],
|
||||
buf: &mut Vec<u8>,
|
||||
alpha_bytes: usize,
|
||||
) {
|
||||
assert!(data.len() >= bpp);
|
||||
assert_eq!(data.len(), prev_line.len());
|
||||
|
||||
if alpha_bytes != 0 {
|
||||
self.optimize_alpha(bpp, data, prev_line, bpp - alpha_bytes);
|
||||
}
|
||||
|
||||
buf.clear();
|
||||
buf.reserve(data.len() + 1);
|
||||
buf.push(self as u8);
|
||||
match self {
|
||||
Self::None => {
|
||||
buf.extend_from_slice(data);
|
||||
}
|
||||
Self::Sub => {
|
||||
buf.extend_from_slice(&data[0..bpp]);
|
||||
buf.extend(
|
||||
data.iter()
|
||||
.skip(bpp)
|
||||
.zip(data.iter())
|
||||
.map(|(cur, last)| cur.wrapping_sub(*last)),
|
||||
);
|
||||
}
|
||||
Self::Up => {
|
||||
buf.extend(
|
||||
data.iter()
|
||||
.zip(prev_line.iter())
|
||||
.map(|(cur, last)| cur.wrapping_sub(*last)),
|
||||
);
|
||||
}
|
||||
Self::Average => {
|
||||
for (i, byte) in data.iter().enumerate() {
|
||||
buf.push(byte.wrapping_sub(i.checked_sub(bpp).map_or_else(
|
||||
|| prev_line[i] >> 1,
|
||||
|x| ((u16::from(data[x]) + u16::from(prev_line[i])) >> 1) as u8,
|
||||
)));
|
||||
}
|
||||
}
|
||||
Self::Paeth => {
|
||||
for (i, byte) in data.iter().enumerate() {
|
||||
buf.push(byte.wrapping_sub(i.checked_sub(bpp).map_or_else(
|
||||
|| prev_line[i],
|
||||
|x| paeth_predictor(data[x], prev_line[i], prev_line[x]),
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Optimize fully transparent pixels of a scanline such that they will be zeroed when filtered
|
||||
fn optimize_alpha(self, bpp: usize, data: &mut [u8], prev_line: &[u8], color_bytes: usize) {
|
||||
if self == Self::None {
|
||||
// Assume transparent pixels already set to 0
|
||||
return;
|
||||
}
|
||||
|
||||
let mut pixels: Vec<_> = data.chunks_exact_mut(bpp).collect();
|
||||
let prev_pixels: Vec<_> = prev_line.chunks_exact(bpp).collect();
|
||||
for i in 0..pixels.len() {
|
||||
if pixels[i].iter().skip(color_bytes).all(|b| *b == 0) {
|
||||
// If the first pixel in the row is transparent, find the next non-transparent pixel and pretend
|
||||
// it is the previous one. This can help improve effectiveness of the Sub and Paeth filters.
|
||||
let prev = match i {
|
||||
0 => pixels
|
||||
.iter()
|
||||
.position(|px| px.iter().skip(color_bytes).any(|b| *b != 0))
|
||||
.unwrap_or(i),
|
||||
_ => i - 1,
|
||||
};
|
||||
|
||||
// These assertions help eliminate a few bounds checks in the slice accesses below
|
||||
assert!(prev < pixels.len());
|
||||
assert!(i < prev_pixels.len());
|
||||
|
||||
match self {
|
||||
Self::None => unreachable!(),
|
||||
Self::Sub => {
|
||||
// The code below is roughly equivalent to pixels[i][0..color_bytes].copy_from_slice(&pixels[prev][0..color_bytes]),
|
||||
// if such a thing was possible to do without violating Rust aliasing rules. See:
|
||||
// https://users.rust-lang.org/t/problem-borrowing-two-elements-of-vec-mutably/21446/2
|
||||
|
||||
if prev < i {
|
||||
let (pixels_head, pixels_tail) = pixels.split_at_mut(prev + 1);
|
||||
pixels_tail[i - prev - 1][0..color_bytes]
|
||||
.copy_from_slice(&pixels_head[prev][0..color_bytes]);
|
||||
} else if prev > i {
|
||||
let (pixels_head, pixels_tail) = pixels.split_at_mut(i + 1);
|
||||
pixels_head[i][0..color_bytes]
|
||||
.copy_from_slice(&pixels_tail[prev - i - 1][0..color_bytes]);
|
||||
} else {
|
||||
// If prev == i, we'd be copying the pixels onto themselves, which is useless
|
||||
}
|
||||
}
|
||||
Self::Up => {
|
||||
pixels[i][0..color_bytes].copy_from_slice(&prev_pixels[i][0..color_bytes]);
|
||||
}
|
||||
Self::Average => {
|
||||
for j in 0..color_bytes {
|
||||
pixels[i][j] = match i {
|
||||
0 => prev_pixels[i][j] >> 1,
|
||||
_ => {
|
||||
((u16::from(pixels[i - 1][j]) + u16::from(prev_pixels[i][j]))
|
||||
>> 1) as u8
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
Self::Paeth => {
|
||||
for j in 0..color_bytes {
|
||||
pixels[i][j] = match i {
|
||||
0 => pixels[prev][j].min(prev_pixels[i][j]),
|
||||
_ => paeth_predictor(
|
||||
pixels[i - 1][j],
|
||||
prev_pixels[i][j],
|
||||
prev_pixels[i - 1][j],
|
||||
),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn unfilter_line(
|
||||
self,
|
||||
bpp: usize,
|
||||
data: &[u8],
|
||||
prev_line: &[u8],
|
||||
buf: &mut Vec<u8>,
|
||||
) {
|
||||
buf.clear();
|
||||
buf.reserve(data.len());
|
||||
assert!(data.len() >= bpp);
|
||||
assert_eq!(data.len(), prev_line.len());
|
||||
match self {
|
||||
Self::None => {
|
||||
buf.extend_from_slice(data);
|
||||
}
|
||||
Self::Sub => {
|
||||
for (i, &cur) in data.iter().enumerate() {
|
||||
let prev_byte = i.checked_sub(bpp).and_then(|x| buf.get(x).copied());
|
||||
buf.push(prev_byte.map_or(cur, |b| cur.wrapping_add(b)));
|
||||
}
|
||||
}
|
||||
Self::Up => {
|
||||
buf.extend(
|
||||
data.iter()
|
||||
.zip(prev_line)
|
||||
.map(|(&cur, &last)| cur.wrapping_add(last)),
|
||||
);
|
||||
}
|
||||
Self::Average => {
|
||||
for (i, (&cur, &last)) in data.iter().zip(prev_line).enumerate() {
|
||||
let prev_byte = i.checked_sub(bpp).and_then(|x| buf.get(x).copied());
|
||||
buf.push(cur.wrapping_add(prev_byte.map_or_else(
|
||||
|| last >> 1,
|
||||
|b| ((u16::from(b) + u16::from(last)) >> 1) as u8,
|
||||
)));
|
||||
}
|
||||
}
|
||||
Self::Paeth => {
|
||||
for (i, (&cur, &up)) in data.iter().zip(prev_line).enumerate() {
|
||||
buf.push(
|
||||
match i
|
||||
.checked_sub(bpp)
|
||||
.map(|x| (buf.get(x).copied(), prev_line.get(x).copied()))
|
||||
{
|
||||
Some((Some(left), Some(left_up))) => {
|
||||
cur.wrapping_add(paeth_predictor(left, up, left_up))
|
||||
}
|
||||
_ => cur.wrapping_add(up),
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn paeth_predictor(a: u8, b: u8, c: u8) -> u8 {
|
||||
let p = a as i32 + b as i32 - c as i32;
|
||||
let pa = (p - a as i32).abs();
|
||||
let pb = (p - b as i32).abs();
|
||||
let pc = (p - c as i32).abs();
|
||||
let p = i32::from(a) + i32::from(b) - i32::from(c);
|
||||
let pa = (p - i32::from(a)).abs();
|
||||
let pb = (p - i32::from(b)).abs();
|
||||
let pc = (p - i32::from(c)).abs();
|
||||
if pa <= pb && pa <= pc {
|
||||
a
|
||||
} else if pb <= pc {
|
||||
|
|
|
|||
452
src/headers.rs
|
|
@ -1,9 +1,16 @@
|
|||
use byteorder::{BigEndian, ReadBytesExt};
|
||||
use colors::{BitDepth, ColorType};
|
||||
use crc::crc32;
|
||||
use std::io::Cursor;
|
||||
use indexmap::IndexSet;
|
||||
use log::{debug, trace, warn};
|
||||
use rgb::{RGB16, RGBA8};
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
use crate::{
|
||||
Deflater, Options, PngResult,
|
||||
colors::{BitDepth, ColorType},
|
||||
deflate::{crc32, inflate},
|
||||
display_chunks::DISPLAY_CHUNKS,
|
||||
error::PngError,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
/// Headers from the IHDR chunk of the image
|
||||
pub struct IhdrData {
|
||||
/// The width of the image in pixels
|
||||
|
|
@ -14,27 +21,84 @@ pub struct IhdrData {
|
|||
pub color_type: ColorType,
|
||||
/// The bit depth of the image
|
||||
pub bit_depth: BitDepth,
|
||||
/// The compression method used for this image (0 for DEFLATE)
|
||||
pub compression: u8,
|
||||
/// The filter mode used for this image (currently only 0 is valid)
|
||||
pub filter: u8,
|
||||
/// The interlacing mode of the image (0 = None, 1 = Adam7)
|
||||
pub interlaced: u8,
|
||||
/// Whether the image is interlaced
|
||||
pub interlaced: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
/// Options to use for performing operations on headers (such as stripping)
|
||||
pub enum Headers {
|
||||
impl IhdrData {
|
||||
/// Bits per pixel
|
||||
#[must_use]
|
||||
#[inline]
|
||||
pub const fn bpp(&self) -> usize {
|
||||
self.bit_depth as usize * self.color_type.channels_per_pixel() as usize
|
||||
}
|
||||
|
||||
/// Byte length of IDAT that is correct for this IHDR
|
||||
#[must_use]
|
||||
pub const fn raw_data_size(&self) -> usize {
|
||||
let w = self.width as usize;
|
||||
let h = self.height as usize;
|
||||
let bpp = self.bpp();
|
||||
|
||||
const fn bitmap_size(bpp: usize, w: usize, h: usize) -> usize {
|
||||
(w * bpp).div_ceil(8) * h
|
||||
}
|
||||
|
||||
if self.interlaced {
|
||||
let mut size = bitmap_size(bpp, (w + 7) >> 3, (h + 7) >> 3) + ((h + 7) >> 3);
|
||||
if w > 4 {
|
||||
size += bitmap_size(bpp, (w + 3) >> 3, (h + 7) >> 3) + ((h + 7) >> 3);
|
||||
}
|
||||
size += bitmap_size(bpp, (w + 3) >> 2, (h + 3) >> 3) + ((h + 3) >> 3);
|
||||
if w > 2 {
|
||||
size += bitmap_size(bpp, (w + 1) >> 2, (h + 3) >> 2) + ((h + 3) >> 2);
|
||||
}
|
||||
size += bitmap_size(bpp, (w + 1) >> 1, (h + 1) >> 2) + ((h + 1) >> 2);
|
||||
if w > 1 {
|
||||
size += bitmap_size(bpp, w >> 1, (h + 1) >> 1) + ((h + 1) >> 1);
|
||||
}
|
||||
size + bitmap_size(bpp, w, h >> 1) + (h >> 1)
|
||||
} else {
|
||||
bitmap_size(bpp, w, h) + h
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Chunk {
|
||||
pub name: [u8; 4],
|
||||
pub data: Vec<u8>,
|
||||
}
|
||||
|
||||
/// [`Options`][crate::Options] to use when stripping chunks (metadata)
|
||||
#[derive(Debug, PartialEq, Eq, Clone)]
|
||||
pub enum StripChunks {
|
||||
/// None
|
||||
///
|
||||
/// ...except caBX chunk if it contains a C2PA.org signature.
|
||||
None,
|
||||
/// Some, with a list of 4-character chunk codes
|
||||
Some(Vec<String>),
|
||||
/// Headers that won't affect rendering (all but cHRM, gAMA, iCCP, sBIT, sRGB, bKGD, hIST, pHYs, sPLT)
|
||||
/// Remove specific chunks
|
||||
Strip(IndexSet<[u8; 4]>),
|
||||
/// Remove all chunks that won't affect image display
|
||||
Safe,
|
||||
/// All non-critical headers
|
||||
/// Remove all non-critical chunks except these
|
||||
Keep(IndexSet<[u8; 4]>),
|
||||
/// All non-critical chunks
|
||||
All,
|
||||
}
|
||||
|
||||
impl StripChunks {
|
||||
pub(crate) fn keep(&self, name: &[u8; 4]) -> bool {
|
||||
match &self {
|
||||
Self::None => true,
|
||||
Self::Keep(names) => names.contains(name),
|
||||
Self::Strip(names) => !names.contains(name),
|
||||
Self::Safe => DISPLAY_CHUNKS.contains(name),
|
||||
Self::All => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn file_header_is_valid(bytes: &[u8]) -> bool {
|
||||
let expected_header: [u8; 8] = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A];
|
||||
|
|
@ -42,80 +106,320 @@ pub fn file_header_is_valid(bytes: &[u8]) -> bool {
|
|||
*bytes == expected_header
|
||||
}
|
||||
|
||||
pub fn parse_next_header(byte_data: &[u8],
|
||||
byte_offset: &mut usize,
|
||||
fix_errors: bool)
|
||||
-> Result<Option<(String, Vec<u8>)>, String> {
|
||||
let mut rdr = Cursor::new(byte_data.iter()
|
||||
.skip(*byte_offset)
|
||||
.take(4)
|
||||
.cloned()
|
||||
.collect::<Vec<u8>>());
|
||||
let length: u32 = match rdr.read_u32::<BigEndian>() {
|
||||
Ok(x) => x,
|
||||
Err(_) => return Err("Invalid data found--unable to read PNG file".to_owned()),
|
||||
};
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct RawChunk<'a> {
|
||||
pub name: [u8; 4],
|
||||
pub data: &'a [u8],
|
||||
}
|
||||
|
||||
impl RawChunk<'_> {
|
||||
// Is it a chunk for C2PA/CAI JUMBF metadata
|
||||
pub(crate) fn is_c2pa(&self) -> bool {
|
||||
if self.name == *b"caBX" {
|
||||
if let Some((b"jumb", data)) = parse_jumbf_box(self.data) {
|
||||
if let Some((b"jumd", data)) = parse_jumbf_box(data) {
|
||||
if data.get(..4) == Some(b"c2pa") {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_jumbf_box(data: &[u8]) -> Option<(&[u8], &[u8])> {
|
||||
if data.len() < 8 {
|
||||
return None;
|
||||
}
|
||||
let (len, rest) = data.split_at(4);
|
||||
let len = read_be_u32(len) as usize;
|
||||
if len < 8 || len > data.len() {
|
||||
return None;
|
||||
}
|
||||
let (box_name, data) = rest.split_at(4);
|
||||
let data = data.get(..len - 8)?;
|
||||
Some((box_name, data))
|
||||
}
|
||||
|
||||
pub fn parse_next_chunk<'a>(
|
||||
byte_data: &'a [u8],
|
||||
byte_offset: &mut usize,
|
||||
fix_errors: bool,
|
||||
) -> PngResult<Option<RawChunk<'a>>> {
|
||||
let length = read_be_u32(
|
||||
byte_data
|
||||
.get(*byte_offset..*byte_offset + 4)
|
||||
.ok_or(PngError::TruncatedData)?,
|
||||
);
|
||||
if byte_data.len() < *byte_offset + 12 + length as usize {
|
||||
return Err(PngError::TruncatedData);
|
||||
}
|
||||
*byte_offset += 4;
|
||||
|
||||
let mut header_bytes: Vec<u8> = byte_data.iter().skip(*byte_offset).take(4).cloned().collect();
|
||||
let header = match String::from_utf8(header_bytes.clone()) {
|
||||
Ok(x) => x,
|
||||
Err(_) => return Err("Invalid data found--unable to read PNG file".to_owned()),
|
||||
};
|
||||
if header == "IEND" {
|
||||
let chunk_start = *byte_offset;
|
||||
let chunk_name = &byte_data[chunk_start..chunk_start + 4];
|
||||
if chunk_name == b"IEND" {
|
||||
// End of data
|
||||
return Ok(None);
|
||||
}
|
||||
*byte_offset += 4;
|
||||
|
||||
let data: Vec<u8> = byte_data.iter()
|
||||
.skip(*byte_offset)
|
||||
.take(length as usize)
|
||||
.cloned()
|
||||
.collect();
|
||||
let data = &byte_data[*byte_offset..*byte_offset + length as usize];
|
||||
*byte_offset += length as usize;
|
||||
let mut rdr = Cursor::new(byte_data.iter()
|
||||
.skip(*byte_offset)
|
||||
.take(4)
|
||||
.cloned()
|
||||
.collect::<Vec<u8>>());
|
||||
let crc: u32 = match rdr.read_u32::<BigEndian>() {
|
||||
Ok(x) => x,
|
||||
Err(_) => return Err("Invalid data found--unable to read PNG file".to_owned()),
|
||||
};
|
||||
let crc = read_be_u32(&byte_data[*byte_offset..*byte_offset + 4]);
|
||||
*byte_offset += 4;
|
||||
header_bytes.extend(data.clone());
|
||||
if !fix_errors && crc32::checksum_ieee(header_bytes.as_ref()) != crc {
|
||||
return Err(format!("Corrupt data chunk found--CRC Mismatch in {}\nThis may be recoverable by using --fix",
|
||||
header));
|
||||
|
||||
let chunk_bytes = &byte_data[chunk_start..chunk_start + 4 + length as usize];
|
||||
if !fix_errors && crc32(chunk_bytes) != crc {
|
||||
return Err(PngError::CRCMismatch(chunk_name.try_into().unwrap()));
|
||||
}
|
||||
|
||||
Ok(Some((header, data)))
|
||||
let name: [u8; 4] = chunk_name.try_into().unwrap();
|
||||
Ok(Some(RawChunk { name, data }))
|
||||
}
|
||||
|
||||
pub fn parse_ihdr_header(byte_data: &[u8]) -> Result<IhdrData, String> {
|
||||
let mut rdr = Cursor::new(&byte_data[0..8]);
|
||||
pub fn parse_ihdr_chunk(
|
||||
byte_data: &[u8],
|
||||
palette_data: Option<Vec<u8>>,
|
||||
trns_data: Option<Vec<u8>>,
|
||||
) -> PngResult<IhdrData> {
|
||||
// This eliminates bounds checks for the rest of the function
|
||||
let interlaced = byte_data.get(12).copied().ok_or(PngError::TruncatedData)?;
|
||||
Ok(IhdrData {
|
||||
color_type: match byte_data[9] {
|
||||
0 => ColorType::Grayscale,
|
||||
2 => ColorType::RGB,
|
||||
3 => ColorType::Indexed,
|
||||
0 => ColorType::Grayscale {
|
||||
transparent_shade: trns_data
|
||||
.filter(|t| t.len() >= 2)
|
||||
.map(|t| read_be_u16(&t[0..2])),
|
||||
},
|
||||
2 => ColorType::RGB {
|
||||
transparent_color: trns_data.filter(|t| t.len() >= 6).map(|t| RGB16 {
|
||||
r: read_be_u16(&t[0..2]),
|
||||
g: read_be_u16(&t[2..4]),
|
||||
b: read_be_u16(&t[4..6]),
|
||||
}),
|
||||
},
|
||||
3 => ColorType::Indexed {
|
||||
palette: palette_to_rgba(palette_data, trns_data).unwrap_or_default(),
|
||||
},
|
||||
4 => ColorType::GrayscaleAlpha,
|
||||
6 => ColorType::RGBA,
|
||||
_ => return Err("Unexpected color type in header".to_owned()),
|
||||
_ => return Err(PngError::InvalidData),
|
||||
},
|
||||
bit_depth: match byte_data[8] {
|
||||
1 => BitDepth::One,
|
||||
2 => BitDepth::Two,
|
||||
4 => BitDepth::Four,
|
||||
8 => BitDepth::Eight,
|
||||
16 => BitDepth::Sixteen,
|
||||
_ => return Err("Unexpected bit depth in header".to_owned()),
|
||||
bit_depth: byte_data[8].try_into()?,
|
||||
width: read_be_u32(&byte_data[0..4]),
|
||||
height: read_be_u32(&byte_data[4..8]),
|
||||
interlaced: match interlaced {
|
||||
0 => false,
|
||||
1 => true,
|
||||
_ => return Err(PngError::InvalidData),
|
||||
},
|
||||
width: rdr.read_u32::<BigEndian>().unwrap(),
|
||||
height: rdr.read_u32::<BigEndian>().unwrap(),
|
||||
compression: byte_data[10],
|
||||
filter: byte_data[11],
|
||||
interlaced: byte_data[12],
|
||||
})
|
||||
}
|
||||
|
||||
/// Construct an RGBA palette from the raw palette and transparency data
|
||||
fn palette_to_rgba(
|
||||
palette_data: Option<Vec<u8>>,
|
||||
trns_data: Option<Vec<u8>>,
|
||||
) -> Result<Vec<RGBA8>, PngError> {
|
||||
let palette_data = palette_data.ok_or(PngError::ChunkMissing("PLTE"))?;
|
||||
let mut palette: Vec<_> = palette_data
|
||||
.chunks_exact(3)
|
||||
.map(|color| RGBA8::new(color[0], color[1], color[2], 255))
|
||||
.collect();
|
||||
|
||||
if let Some(trns_data) = trns_data {
|
||||
for (color, trns) in palette.iter_mut().zip(trns_data) {
|
||||
color.a = trns;
|
||||
}
|
||||
}
|
||||
Ok(palette)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn read_be_u16(bytes: &[u8]) -> u16 {
|
||||
u16::from_be_bytes(bytes.try_into().unwrap())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn read_be_u32(bytes: &[u8]) -> u32 {
|
||||
u32::from_be_bytes(bytes.try_into().unwrap())
|
||||
}
|
||||
|
||||
/// Extract and decompress the ICC profile from an iCCP chunk
|
||||
pub fn extract_icc(iccp: &Chunk, max_size: Option<usize>) -> Option<Vec<u8>> {
|
||||
// Skip (useless) profile name
|
||||
let mut data = iccp.data.as_slice();
|
||||
loop {
|
||||
let (&n, rest) = data.split_first()?;
|
||||
data = rest;
|
||||
if n == 0 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
let (&compression_method, compressed_data) = data.split_first()?;
|
||||
if compression_method != 0 {
|
||||
return None; // The profile is supposed to be compressed (method 0)
|
||||
}
|
||||
// Libdeflate works with a fixed size buffer. Since the decompressed size is unknown we have to
|
||||
// guess the required buffer size. We allow a fairly generous 10x factor with a minimum of 1000.
|
||||
let mut out_size = (compressed_data.len() * 10).max(1000);
|
||||
// For sanity, impose a default limit of 1MB.
|
||||
out_size = out_size.min(max_size.unwrap_or(1_000_000));
|
||||
match inflate(compressed_data, out_size) {
|
||||
Ok(icc) => Some(icc),
|
||||
Err(e) => {
|
||||
// Log the error so we can know if the buffer size needs to be adjusted
|
||||
warn!("Failed to decompress icc: {e}");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Make an iCCP chunk by compressing the ICC profile
|
||||
pub fn make_iccp(icc: &[u8], deflater: Deflater, max_size: Option<usize>) -> PngResult<Chunk> {
|
||||
let mut compressed = deflater.deflate(icc, max_size)?;
|
||||
let mut data = Vec::with_capacity(compressed.len() + 5);
|
||||
data.extend(b"icc"); // Profile name - generally unused, can be anything
|
||||
data.extend([0, 0]); // Null separator, zlib compression method
|
||||
data.append(&mut compressed);
|
||||
Ok(Chunk {
|
||||
name: *b"iCCP",
|
||||
data,
|
||||
})
|
||||
}
|
||||
|
||||
/// If the profile is sRGB, extracts the rendering intent value from it
|
||||
pub fn srgb_rendering_intent(icc_data: &[u8]) -> Option<u8> {
|
||||
let rendering_intent = *icc_data.get(67)?;
|
||||
|
||||
// The known profiles are the same as in libpng's `png_sRGB_checks`.
|
||||
// The Profile ID header of ICC has a fixed layout,
|
||||
// and is supposed to contain MD5 of profile data at this offset
|
||||
match icc_data.get(84..100)? {
|
||||
b"\x29\xf8\x3d\xde\xaf\xf2\x55\xae\x78\x42\xfa\xe4\xca\x83\x39\x0d"
|
||||
| b"\xc9\x5b\xd6\x37\xe9\x5d\x8a\x3b\x0d\xf3\x8f\x99\xc1\x32\x03\x89"
|
||||
| b"\xfc\x66\x33\x78\x37\xe2\x88\x6b\xfd\x72\xe9\x83\x82\x28\xf1\xb8"
|
||||
| b"\x34\x56\x2a\xbf\x99\x4c\xcd\x06\x6d\x2c\x57\x21\xd0\xd6\x8c\x5d" => {
|
||||
Some(rendering_intent)
|
||||
}
|
||||
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" => {
|
||||
// Known-bad profiles are identified by their CRC
|
||||
match (crc32(icc_data), icc_data.len()) {
|
||||
(0x5d51_29ce, 3024) | (0x182e_a552, 3144) | (0xf29e_526d, 3144) => {
|
||||
Some(rendering_intent)
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Process aux chunks and potentially adjust options before optimizing
|
||||
pub fn preprocess_chunks(aux_chunks: &mut Vec<Chunk>, opts: &mut Options) {
|
||||
let has_srgb = aux_chunks.iter().any(|c| &c.name == b"sRGB");
|
||||
// Grayscale conversion should not be performed if the image is not in the sRGB colorspace
|
||||
// An sRGB profile would need to be stripped on conversion, so disallow if stripping is disabled
|
||||
let mut allow_grayscale = !has_srgb || opts.strip != StripChunks::None;
|
||||
|
||||
if let Some(iccp_idx) = aux_chunks.iter().position(|c| &c.name == b"iCCP") {
|
||||
allow_grayscale = false;
|
||||
// See if we can replace an iCCP chunk with an sRGB chunk
|
||||
let may_replace_iccp = opts.strip != StripChunks::None && opts.strip.keep(b"sRGB");
|
||||
if may_replace_iccp && has_srgb {
|
||||
// Files aren't supposed to have both chunks, so we chose to honor sRGB
|
||||
trace!("Removing iCCP chunk due to conflict with sRGB chunk");
|
||||
aux_chunks.remove(iccp_idx);
|
||||
allow_grayscale = true;
|
||||
} else if let Some(icc) = extract_icc(&aux_chunks[iccp_idx], opts.max_decompressed_size) {
|
||||
let intent = if may_replace_iccp {
|
||||
srgb_rendering_intent(&icc)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
// sRGB-like profile can be replaced with an sRGB chunk with the same rendering intent
|
||||
if let Some(intent) = intent {
|
||||
trace!("Replacing iCCP chunk with equivalent sRGB chunk");
|
||||
aux_chunks[iccp_idx] = Chunk {
|
||||
name: *b"sRGB",
|
||||
data: vec![intent],
|
||||
};
|
||||
allow_grayscale = true;
|
||||
} else if opts.idat_recoding {
|
||||
// Try recompressing the profile
|
||||
let cur_len = aux_chunks[iccp_idx].data.len();
|
||||
if let Ok(iccp) = make_iccp(&icc, opts.deflater, Some(cur_len - 1)) {
|
||||
debug!(
|
||||
"Recompressed iCCP chunk: {} ({} bytes decrease)",
|
||||
iccp.data.len(),
|
||||
cur_len - iccp.data.len()
|
||||
);
|
||||
aux_chunks[iccp_idx] = iccp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !allow_grayscale && opts.grayscale_reduction {
|
||||
debug!("Disabling grayscale reduction due to presence of sRGB or iCCP chunk");
|
||||
opts.grayscale_reduction = false;
|
||||
}
|
||||
|
||||
// Check for APNG by presence of acTL chunk
|
||||
if aux_chunks.iter().any(|c| &c.name == b"acTL") {
|
||||
warn!("APNG detected, disabling all reductions");
|
||||
opts.interlace = None;
|
||||
opts.bit_depth_reduction = false;
|
||||
opts.color_type_reduction = false;
|
||||
opts.palette_reduction = false;
|
||||
opts.grayscale_reduction = false;
|
||||
}
|
||||
}
|
||||
|
||||
/// Perform cleanup of certain aux chunks after optimization has been completed
|
||||
pub fn postprocess_chunks(aux_chunks: &mut Vec<Chunk>, ihdr: &IhdrData, orig_ihdr: &IhdrData) {
|
||||
// If the depth/color type has changed, some chunks may be invalid and should be dropped
|
||||
// While these could potentially be converted, they have no known use case today and are
|
||||
// generally more trouble than they're worth
|
||||
if orig_ihdr.bit_depth != ihdr.bit_depth || orig_ihdr.color_type != ihdr.color_type {
|
||||
aux_chunks.retain(|c| {
|
||||
let invalid = &c.name == b"bKGD" || &c.name == b"sBIT" || &c.name == b"hIST";
|
||||
if invalid {
|
||||
warn!(
|
||||
"Removing {} chunk as it no longer matches the image data",
|
||||
std::str::from_utf8(&c.name).unwrap()
|
||||
);
|
||||
}
|
||||
!invalid
|
||||
});
|
||||
}
|
||||
|
||||
// Remove any sRGB or iCCP chunks if the image was converted to or from grayscale
|
||||
if orig_ihdr.color_type.is_gray() != ihdr.color_type.is_gray() {
|
||||
aux_chunks.retain(|c| {
|
||||
let invalid = &c.name == b"sRGB" || &c.name == b"iCCP";
|
||||
if invalid {
|
||||
trace!(
|
||||
"Removing {} chunk as it no longer matches the color type",
|
||||
std::str::from_utf8(&c.name).unwrap()
|
||||
);
|
||||
}
|
||||
!invalid
|
||||
});
|
||||
}
|
||||
|
||||
// Remove iDOT which will necessarily be invalid after successful optimization
|
||||
aux_chunks.retain(|c| {
|
||||
let invalid = &c.name == b"iDOT";
|
||||
if invalid {
|
||||
trace!(
|
||||
"Removing {} chunk as it no longer matches the IDAT",
|
||||
std::str::from_utf8(&c.name).unwrap()
|
||||
);
|
||||
}
|
||||
!invalid
|
||||
});
|
||||
}
|
||||
|
|
|
|||
325
src/interlace.rs
|
|
@ -1,73 +1,36 @@
|
|||
use bit_vec::BitVec;
|
||||
use png::PngData;
|
||||
use bitvec::prelude::*;
|
||||
|
||||
pub fn interlace_image(png: &mut PngData) {
|
||||
let mut passes: Vec<BitVec> = vec![BitVec::new(); 7];
|
||||
let bits_per_pixel = png.ihdr_data.bit_depth.as_u8() * png.channels_per_pixel();
|
||||
for (index, line) in png.scan_lines().enumerate() {
|
||||
match index % 8 {
|
||||
// Add filter bytes to passes that will be in the output image
|
||||
0 => {
|
||||
passes[0].extend(BitVec::from_elem(8, false));
|
||||
if png.ihdr_data.width >= 5 {
|
||||
passes[1].extend(BitVec::from_elem(8, false));
|
||||
}
|
||||
if png.ihdr_data.width >= 3 {
|
||||
passes[3].extend(BitVec::from_elem(8, false));
|
||||
}
|
||||
if png.ihdr_data.width >= 2 {
|
||||
passes[5].extend(BitVec::from_elem(8, false));
|
||||
}
|
||||
}
|
||||
4 => {
|
||||
passes[2].extend(BitVec::from_elem(8, false));
|
||||
if png.ihdr_data.width >= 3 {
|
||||
passes[3].extend(BitVec::from_elem(8, false));
|
||||
}
|
||||
if png.ihdr_data.width >= 2 {
|
||||
passes[5].extend(BitVec::from_elem(8, false));
|
||||
}
|
||||
}
|
||||
2 | 6 => {
|
||||
passes[4].extend(BitVec::from_elem(8, false));
|
||||
if png.ihdr_data.width >= 2 {
|
||||
passes[5].extend(BitVec::from_elem(8, false));
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
passes[6].extend(BitVec::from_elem(8, false));
|
||||
}
|
||||
}
|
||||
let bit_vec = BitVec::from_bytes(&line.data);
|
||||
for (i, bit) in bit_vec.iter().enumerate() {
|
||||
use crate::{headers::IhdrData, png::PngImage};
|
||||
|
||||
#[must_use]
|
||||
pub fn interlace_image(png: &PngImage) -> PngImage {
|
||||
let mut passes: Vec<BitVec<u8, Msb0>> = vec![BitVec::new(); 7];
|
||||
let bits_per_pixel = png.ihdr.bpp();
|
||||
for (index, line) in png.scan_lines(false).enumerate() {
|
||||
let bit_vec = line.data.view_bits::<Msb0>();
|
||||
for (i, bit) in bit_vec.iter().by_vals().enumerate() {
|
||||
// Avoid moving padded 0's into new image
|
||||
if i >= (png.ihdr_data.width * bits_per_pixel as u32) as usize {
|
||||
if i >= (png.ihdr.width as usize * bits_per_pixel) {
|
||||
break;
|
||||
}
|
||||
// Copy pixels into interlaced passes
|
||||
let pix_modulo = (i / bits_per_pixel as usize) % 8;
|
||||
let pix_modulo = (i / bits_per_pixel) % 8;
|
||||
match index % 8 {
|
||||
0 => {
|
||||
match pix_modulo {
|
||||
0 => passes[0].push(bit),
|
||||
4 => passes[1].push(bit),
|
||||
2 | 6 => passes[3].push(bit),
|
||||
_ => passes[5].push(bit),
|
||||
}
|
||||
}
|
||||
4 => {
|
||||
match pix_modulo {
|
||||
0 | 4 => passes[2].push(bit),
|
||||
2 | 6 => passes[3].push(bit),
|
||||
_ => passes[5].push(bit),
|
||||
}
|
||||
}
|
||||
2 | 6 => {
|
||||
match pix_modulo % 2 {
|
||||
0 => passes[4].push(bit),
|
||||
_ => passes[5].push(bit),
|
||||
}
|
||||
}
|
||||
0 => match pix_modulo {
|
||||
0 => passes[0].push(bit),
|
||||
4 => passes[1].push(bit),
|
||||
2 | 6 => passes[3].push(bit),
|
||||
_ => passes[5].push(bit),
|
||||
},
|
||||
4 => match pix_modulo {
|
||||
0 | 4 => passes[2].push(bit),
|
||||
2 | 6 => passes[3].push(bit),
|
||||
_ => passes[5].push(bit),
|
||||
},
|
||||
2 | 6 => match pix_modulo % 2 {
|
||||
0 => passes[4].push(bit),
|
||||
_ => passes[5].push(bit),
|
||||
},
|
||||
_ => {
|
||||
passes[6].push(bit);
|
||||
}
|
||||
|
|
@ -80,64 +43,136 @@ pub fn interlace_image(png: &mut PngData) {
|
|||
}
|
||||
}
|
||||
}
|
||||
let mut output = Vec::new();
|
||||
|
||||
let mut output = Vec::with_capacity(png.data.len());
|
||||
for pass in &passes {
|
||||
output.extend(pass.to_bytes());
|
||||
output.extend_from_slice(pass.as_raw_slice());
|
||||
}
|
||||
|
||||
PngImage {
|
||||
data: output,
|
||||
ihdr: IhdrData {
|
||||
color_type: png.ihdr.color_type.clone(),
|
||||
interlaced: true,
|
||||
..png.ihdr
|
||||
},
|
||||
}
|
||||
png.raw_data = output;
|
||||
}
|
||||
|
||||
pub fn deinterlace_image(png: &mut PngData) {
|
||||
let bits_per_pixel = png.ihdr_data.bit_depth.as_u8() * png.channels_per_pixel();
|
||||
let bits_per_line = 8 + bits_per_pixel as usize * png.ihdr_data.width as usize;
|
||||
// Initialize each output line with a starting filter byte of 0
|
||||
// as well as some blank data
|
||||
let mut lines: Vec<BitVec> =
|
||||
vec![BitVec::from_elem(bits_per_line, false); png.ihdr_data.height as usize];
|
||||
pub fn deinterlace_image(png: &PngImage) -> PngImage {
|
||||
PngImage {
|
||||
data: match png.ihdr.bpp() {
|
||||
8.. => deinterlace_bytes(png),
|
||||
_ => deinterlace_bits(png),
|
||||
},
|
||||
ihdr: IhdrData {
|
||||
color_type: png.ihdr.color_type.clone(),
|
||||
interlaced: false,
|
||||
..png.ihdr
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Deinterlace by bits, for images with less than 8bpp
|
||||
fn deinterlace_bits(png: &PngImage) -> Vec<u8> {
|
||||
let bits_per_pixel = png.ihdr.bpp();
|
||||
let bits_per_line = bits_per_pixel * png.ihdr.width as usize;
|
||||
// Initialize each output line with blank data
|
||||
let mut lines: Vec<BitVec<u8, Msb0>> =
|
||||
vec![bitvec![u8, Msb0; 0; bits_per_line]; png.ihdr.height as usize];
|
||||
let mut current_pass = 1;
|
||||
let mut pass_constants = interlaced_constants(current_pass);
|
||||
let mut current_y: usize = pass_constants.y_shift as usize;
|
||||
for line in png.scan_lines() {
|
||||
let bit_vec = BitVec::from_bytes(&line.data);
|
||||
let bits_in_line = ((png.ihdr_data.width - pass_constants.x_shift as u32) as f32 /
|
||||
pass_constants.x_step as f32)
|
||||
.ceil() as usize * bits_per_pixel as usize;
|
||||
for (i, bit) in bit_vec.iter().enumerate() {
|
||||
for line in png.scan_lines(false) {
|
||||
let bit_vec = line.data.view_bits::<Msb0>();
|
||||
let bits_in_line = (png.ihdr.width - u32::from(pass_constants.x_shift))
|
||||
.div_ceil(u32::from(pass_constants.x_step)) as usize
|
||||
* bits_per_pixel;
|
||||
for (i, bit) in bit_vec.iter().by_vals().enumerate() {
|
||||
// Avoid moving padded 0's into new image
|
||||
if i >= bits_in_line {
|
||||
break;
|
||||
}
|
||||
let current_x: usize = pass_constants.x_shift as usize +
|
||||
(i / bits_per_pixel as usize) * pass_constants.x_step as usize;
|
||||
// Copy this bit into the output line, offset by 8 because of filter byte
|
||||
let index = 8 + (i % bits_per_pixel as usize) + current_x * bits_per_pixel as usize;
|
||||
let current_x: usize = pass_constants.x_shift as usize
|
||||
+ (i / bits_per_pixel) * pass_constants.x_step as usize;
|
||||
// Copy this bit into the output line
|
||||
let index = (i % bits_per_pixel) + current_x * bits_per_pixel;
|
||||
lines[current_y].set(index, bit);
|
||||
}
|
||||
// Calculate the next line and move to next pass if necessary
|
||||
current_y += pass_constants.y_step as usize;
|
||||
if current_y >= png.ihdr_data.height as usize {
|
||||
if current_pass == 7 {
|
||||
if current_y >= png.ihdr.height as usize {
|
||||
if !increment_pass(&mut current_pass, &png.ihdr) {
|
||||
break;
|
||||
}
|
||||
current_pass += 1;
|
||||
if current_pass == 2 && png.ihdr_data.width <= 4 {
|
||||
current_pass += 1;
|
||||
}
|
||||
if current_pass == 3 && png.ihdr_data.height <= 4 {
|
||||
current_pass += 1;
|
||||
}
|
||||
pass_constants = interlaced_constants(current_pass);
|
||||
current_y = pass_constants.y_shift as usize;
|
||||
}
|
||||
}
|
||||
let mut output = Vec::new();
|
||||
let mut output = Vec::with_capacity(png.data.len());
|
||||
for line in &mut lines {
|
||||
while line.len() % 8 != 0 {
|
||||
line.push(false);
|
||||
}
|
||||
output.extend(line.to_bytes());
|
||||
output.extend_from_slice(line.as_raw_slice());
|
||||
}
|
||||
png.raw_data = output;
|
||||
output
|
||||
}
|
||||
|
||||
/// Deinterlace by bytes, for images with at least 8bpp
|
||||
fn deinterlace_bytes(png: &PngImage) -> Vec<u8> {
|
||||
let bytes_per_pixel = png.ihdr.bpp() / 8;
|
||||
let bytes_per_line = bytes_per_pixel * png.ihdr.width as usize;
|
||||
// Initialize each output line with some blank data
|
||||
let mut lines: Vec<Vec<u8>> = vec![vec![0; bytes_per_line]; png.ihdr.height as usize];
|
||||
let mut current_pass = 1;
|
||||
let mut pass_constants = interlaced_constants(current_pass);
|
||||
let mut current_y: usize = pass_constants.y_shift as usize;
|
||||
for line in png.scan_lines(false) {
|
||||
for (i, byte) in line.data.iter().enumerate() {
|
||||
let current_x: usize = pass_constants.x_shift as usize
|
||||
+ (i / bytes_per_pixel) * pass_constants.x_step as usize;
|
||||
// Copy this byte into the output line
|
||||
let index = (i % bytes_per_pixel) + current_x * bytes_per_pixel;
|
||||
lines[current_y][index] = *byte;
|
||||
}
|
||||
// Calculate the next line and move to next pass if necessary
|
||||
current_y += pass_constants.y_step as usize;
|
||||
if current_y >= png.ihdr.height as usize {
|
||||
if !increment_pass(&mut current_pass, &png.ihdr) {
|
||||
break;
|
||||
}
|
||||
pass_constants = interlaced_constants(current_pass);
|
||||
current_y = pass_constants.y_shift as usize;
|
||||
}
|
||||
}
|
||||
lines.concat()
|
||||
}
|
||||
|
||||
const fn increment_pass(current_pass: &mut u8, ihdr: &IhdrData) -> bool {
|
||||
if *current_pass == 7 {
|
||||
return false;
|
||||
}
|
||||
*current_pass += 1;
|
||||
if *current_pass == 2 && ihdr.width <= 4 {
|
||||
*current_pass += 1;
|
||||
}
|
||||
if *current_pass == 3 && ihdr.height <= 4 {
|
||||
*current_pass += 1;
|
||||
}
|
||||
if *current_pass == 4 && ihdr.width <= 2 {
|
||||
*current_pass += 1;
|
||||
}
|
||||
if *current_pass == 5 && ihdr.height <= 2 {
|
||||
*current_pass += 1;
|
||||
}
|
||||
if *current_pass == 6 && ihdr.width == 1 {
|
||||
*current_pass += 1;
|
||||
}
|
||||
if *current_pass == 7 && ihdr.height == 1 {
|
||||
return false;
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
|
|
@ -150,62 +185,48 @@ struct InterlacedConstants {
|
|||
|
||||
fn interlaced_constants(pass: u8) -> InterlacedConstants {
|
||||
match pass {
|
||||
1 => {
|
||||
InterlacedConstants {
|
||||
x_shift: 0,
|
||||
y_shift: 0,
|
||||
x_step: 8,
|
||||
y_step: 8,
|
||||
}
|
||||
}
|
||||
2 => {
|
||||
InterlacedConstants {
|
||||
x_shift: 4,
|
||||
y_shift: 0,
|
||||
x_step: 8,
|
||||
y_step: 8,
|
||||
}
|
||||
}
|
||||
3 => {
|
||||
InterlacedConstants {
|
||||
x_shift: 0,
|
||||
y_shift: 4,
|
||||
x_step: 4,
|
||||
y_step: 8,
|
||||
}
|
||||
}
|
||||
4 => {
|
||||
InterlacedConstants {
|
||||
x_shift: 2,
|
||||
y_shift: 0,
|
||||
x_step: 4,
|
||||
y_step: 4,
|
||||
}
|
||||
}
|
||||
5 => {
|
||||
InterlacedConstants {
|
||||
x_shift: 0,
|
||||
y_shift: 2,
|
||||
x_step: 2,
|
||||
y_step: 4,
|
||||
}
|
||||
}
|
||||
6 => {
|
||||
InterlacedConstants {
|
||||
x_shift: 1,
|
||||
y_shift: 0,
|
||||
x_step: 2,
|
||||
y_step: 2,
|
||||
}
|
||||
}
|
||||
7 => {
|
||||
InterlacedConstants {
|
||||
x_shift: 0,
|
||||
y_shift: 1,
|
||||
x_step: 1,
|
||||
y_step: 2,
|
||||
}
|
||||
}
|
||||
1 => InterlacedConstants {
|
||||
x_shift: 0,
|
||||
y_shift: 0,
|
||||
x_step: 8,
|
||||
y_step: 8,
|
||||
},
|
||||
2 => InterlacedConstants {
|
||||
x_shift: 4,
|
||||
y_shift: 0,
|
||||
x_step: 8,
|
||||
y_step: 8,
|
||||
},
|
||||
3 => InterlacedConstants {
|
||||
x_shift: 0,
|
||||
y_shift: 4,
|
||||
x_step: 4,
|
||||
y_step: 8,
|
||||
},
|
||||
4 => InterlacedConstants {
|
||||
x_shift: 2,
|
||||
y_shift: 0,
|
||||
x_step: 4,
|
||||
y_step: 4,
|
||||
},
|
||||
5 => InterlacedConstants {
|
||||
x_shift: 0,
|
||||
y_shift: 2,
|
||||
x_step: 2,
|
||||
y_step: 4,
|
||||
},
|
||||
6 => InterlacedConstants {
|
||||
x_shift: 1,
|
||||
y_shift: 0,
|
||||
x_step: 2,
|
||||
y_step: 2,
|
||||
},
|
||||
7 => InterlacedConstants {
|
||||
x_shift: 0,
|
||||
y_shift: 1,
|
||||
x_step: 1,
|
||||
y_step: 2,
|
||||
},
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
1104
src/lib.rs
1119
src/main.rs
303
src/options.rs
Normal file
|
|
@ -0,0 +1,303 @@
|
|||
use std::{
|
||||
fmt,
|
||||
path::{Path, PathBuf},
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
use indexmap::{IndexSet, indexset};
|
||||
use log::warn;
|
||||
|
||||
use crate::{deflate::Deflater, filters::FilterStrategy, headers::StripChunks};
|
||||
|
||||
/// Write destination for [`optimize`][crate::optimize].
|
||||
/// You can use [`optimize_from_memory`](crate::optimize_from_memory) to avoid external I/O.
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum OutFile {
|
||||
/// Don't actually write any output, just calculate the best results.
|
||||
None,
|
||||
/// Write output to a file.
|
||||
///
|
||||
/// * `path`: Path to write the output file. `None` means same as input.
|
||||
/// * `preserve_attrs`: Ensure the output file has the same permissions & timestamps as the input file.
|
||||
Path {
|
||||
path: Option<PathBuf>,
|
||||
preserve_attrs: bool,
|
||||
},
|
||||
/// Write to standard output.
|
||||
StdOut,
|
||||
}
|
||||
|
||||
impl OutFile {
|
||||
/// Construct a new `OutFile` with the given path.
|
||||
///
|
||||
/// This is a convenience method for `OutFile::Path { path: Some(path), preserve_attrs: false }`.
|
||||
#[must_use]
|
||||
pub const fn from_path(path: PathBuf) -> Self {
|
||||
Self::Path {
|
||||
path: Some(path),
|
||||
preserve_attrs: false,
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn path(&self) -> Option<&Path> {
|
||||
match *self {
|
||||
Self::Path {
|
||||
path: Some(ref p), ..
|
||||
} => Some(p.as_path()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Where to read images from in [`optimize`][crate::optimize].
|
||||
/// You can use [`optimize_from_memory`](crate::optimize_from_memory) to avoid external I/O.
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum InFile {
|
||||
Path(PathBuf),
|
||||
StdIn,
|
||||
}
|
||||
|
||||
impl InFile {
|
||||
#[must_use]
|
||||
pub fn path(&self) -> Option<&Path> {
|
||||
match *self {
|
||||
Self::Path(ref p) => Some(p.as_path()),
|
||||
Self::StdIn => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for InFile {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match *self {
|
||||
Self::Path(ref p) => write!(f, "{}", p.display()),
|
||||
Self::StdIn => f.write_str("stdin"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Into<PathBuf>> From<T> for InFile {
|
||||
fn from(s: T) -> Self {
|
||||
Self::Path(s.into())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
/// Options controlling the output of the `optimize` function
|
||||
pub struct Options {
|
||||
/// Attempt to fix errors when decoding the input file rather than returning an `Err`.
|
||||
///
|
||||
/// Default: `false`
|
||||
pub fix_errors: bool,
|
||||
/// Write to output even if there was no improvement in compression.
|
||||
///
|
||||
/// Default: `false`
|
||||
pub force: bool,
|
||||
/// Which `FilterStrategy` to try on the file
|
||||
///
|
||||
/// Default: `None,Sub,Entropy,Bigrams`
|
||||
pub filters: IndexSet<FilterStrategy>,
|
||||
/// Whether to change the interlacing of the file.
|
||||
///
|
||||
/// - `None` will not change the current interlacing.
|
||||
/// - `Some(x)` will turn interlacing on or off.
|
||||
///
|
||||
/// Default: `Some(false)`
|
||||
pub interlace: Option<bool>,
|
||||
/// Whether to allow transparent pixels to be altered to improve compression.
|
||||
///
|
||||
/// Default: `false`
|
||||
pub optimize_alpha: bool,
|
||||
/// Whether to attempt bit depth reduction
|
||||
///
|
||||
/// Default: `true`
|
||||
pub bit_depth_reduction: bool,
|
||||
/// Whether to attempt color type reduction
|
||||
///
|
||||
/// Default: `true`
|
||||
pub color_type_reduction: bool,
|
||||
/// Whether to attempt palette reduction
|
||||
///
|
||||
/// Default: `true`
|
||||
pub palette_reduction: bool,
|
||||
/// Whether to attempt grayscale reduction
|
||||
///
|
||||
/// Default: `true`
|
||||
pub grayscale_reduction: bool,
|
||||
/// Whether to perform recoding of IDAT and other compressed chunks
|
||||
///
|
||||
/// If any type of reduction is performed, IDAT recoding will be performed
|
||||
/// regardless of this setting
|
||||
///
|
||||
/// Default: `true`
|
||||
pub idat_recoding: bool,
|
||||
/// Whether to forcibly reduce 16-bit to 8-bit by scaling
|
||||
///
|
||||
/// Default: `false`
|
||||
pub scale_16: bool,
|
||||
/// Which chunks to strip from the PNG file, if any
|
||||
///
|
||||
/// Default: `None`
|
||||
pub strip: StripChunks,
|
||||
/// Which DEFLATE (zlib) algorithm to use
|
||||
#[cfg_attr(feature = "zopfli", doc = "(e.g. Zopfli)")]
|
||||
///
|
||||
/// Default: `Libdeflater`
|
||||
pub deflater: Deflater,
|
||||
/// Whether to use fast evaluation to pick the best filter
|
||||
///
|
||||
/// Default: `true`
|
||||
pub fast_evaluation: bool,
|
||||
/// Maximum amount of time to spend on optimizations.
|
||||
/// Further potential optimizations are skipped if the timeout is exceeded.
|
||||
///
|
||||
/// Default: `None`
|
||||
pub timeout: Option<Duration>,
|
||||
/// Maximum decompressed size of the input IDAT.
|
||||
/// If decompression would exceed this size, it will be rejected.
|
||||
///
|
||||
/// Default: `None`
|
||||
pub max_decompressed_size: Option<usize>,
|
||||
}
|
||||
|
||||
impl Options {
|
||||
#[must_use]
|
||||
pub fn from_preset(level: u8) -> Self {
|
||||
let opts = Self::default();
|
||||
match level {
|
||||
0 => opts.apply_preset_0(),
|
||||
1 => opts.apply_preset_1(),
|
||||
2 => opts.apply_preset_2(),
|
||||
3 => opts.apply_preset_3(),
|
||||
4 => opts.apply_preset_4(),
|
||||
5 => opts.apply_preset_5(),
|
||||
6 => opts.apply_preset_6(),
|
||||
_ => {
|
||||
warn!("Level 7 and above don't exist yet and are identical to level 6");
|
||||
opts.apply_preset_6()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn max_compression() -> Self {
|
||||
Self::from_preset(6)
|
||||
}
|
||||
|
||||
// The following methods make assumptions that they are operating
|
||||
// on an `Options` struct generated by the `default` method.
|
||||
fn apply_preset_0(mut self) -> Self {
|
||||
self.filters.clear();
|
||||
self.deflater = Deflater::Libdeflater { compression: 5 };
|
||||
self
|
||||
}
|
||||
|
||||
fn apply_preset_1(mut self) -> Self {
|
||||
self.filters.clear();
|
||||
self.deflater = Deflater::Libdeflater { compression: 10 };
|
||||
self
|
||||
}
|
||||
|
||||
const fn apply_preset_2(self) -> Self {
|
||||
self
|
||||
}
|
||||
|
||||
fn apply_preset_3(mut self) -> Self {
|
||||
self.fast_evaluation = false;
|
||||
self.filters = indexset! {
|
||||
FilterStrategy::NONE,
|
||||
FilterStrategy::Bigrams,
|
||||
FilterStrategy::BigEnt,
|
||||
FilterStrategy::Brute {
|
||||
num_lines: 3,
|
||||
level: 1,
|
||||
},
|
||||
};
|
||||
self
|
||||
}
|
||||
|
||||
fn apply_preset_4(mut self) -> Self {
|
||||
self.fast_evaluation = false;
|
||||
self.filters = indexset! {
|
||||
FilterStrategy::NONE,
|
||||
FilterStrategy::Bigrams,
|
||||
FilterStrategy::BigEnt,
|
||||
FilterStrategy::Brute {
|
||||
num_lines: 4,
|
||||
level: 1,
|
||||
},
|
||||
};
|
||||
self.deflater = Deflater::Libdeflater { compression: 12 };
|
||||
self
|
||||
}
|
||||
|
||||
fn apply_preset_5(mut self) -> Self {
|
||||
self.fast_evaluation = false;
|
||||
self.filters = indexset! {
|
||||
FilterStrategy::NONE,
|
||||
FilterStrategy::SUB,
|
||||
FilterStrategy::UP,
|
||||
FilterStrategy::MinSum,
|
||||
FilterStrategy::Entropy,
|
||||
FilterStrategy::Bigrams,
|
||||
FilterStrategy::BigEnt,
|
||||
FilterStrategy::Brute {
|
||||
num_lines: 4,
|
||||
level: 4,
|
||||
},
|
||||
};
|
||||
self.deflater = Deflater::Libdeflater { compression: 12 };
|
||||
self
|
||||
}
|
||||
|
||||
fn apply_preset_6(mut self) -> Self {
|
||||
self.fast_evaluation = false;
|
||||
self.filters = indexset! {
|
||||
FilterStrategy::NONE,
|
||||
FilterStrategy::SUB,
|
||||
FilterStrategy::UP,
|
||||
FilterStrategy::AVERAGE,
|
||||
FilterStrategy::PAETH,
|
||||
FilterStrategy::MinSum,
|
||||
FilterStrategy::Entropy,
|
||||
FilterStrategy::Bigrams,
|
||||
FilterStrategy::BigEnt,
|
||||
FilterStrategy::Brute {
|
||||
num_lines: 8,
|
||||
level: 5,
|
||||
},
|
||||
};
|
||||
self.deflater = Deflater::Libdeflater { compression: 12 };
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Options {
|
||||
fn default() -> Self {
|
||||
// Default settings based on -o 2 from the CLI interface
|
||||
Self {
|
||||
fix_errors: false,
|
||||
force: false,
|
||||
filters: indexset! {
|
||||
FilterStrategy::NONE,
|
||||
FilterStrategy::SUB,
|
||||
FilterStrategy::Entropy,
|
||||
FilterStrategy::Bigrams
|
||||
},
|
||||
interlace: Some(false),
|
||||
optimize_alpha: false,
|
||||
bit_depth_reduction: true,
|
||||
color_type_reduction: true,
|
||||
palette_reduction: true,
|
||||
grayscale_reduction: true,
|
||||
idat_recoding: true,
|
||||
scale_16: false,
|
||||
strip: StripChunks::None,
|
||||
deflater: Deflater::Libdeflater { compression: 11 },
|
||||
fast_evaluation: true,
|
||||
timeout: None,
|
||||
max_decompressed_size: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
764
src/png.rs
|
|
@ -1,764 +0,0 @@
|
|||
use bit_vec::BitVec;
|
||||
use byteorder::{BigEndian, WriteBytesExt};
|
||||
use colors::{BitDepth, ColorType};
|
||||
use crc::crc32;
|
||||
use filters::*;
|
||||
use headers::*;
|
||||
use interlace::{interlace_image, deinterlace_image};
|
||||
use itertools::Itertools;
|
||||
use reduction::*;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
use std::iter::Iterator;
|
||||
use std::path::Path;
|
||||
|
||||
#[derive(Debug,Clone)]
|
||||
/// An iterator over the scan lines of a PNG image
|
||||
pub struct ScanLines<'a> {
|
||||
/// A reference to the PNG image being iterated upon
|
||||
pub png: &'a PngData,
|
||||
start: usize,
|
||||
end: usize,
|
||||
/// Current pass number, and 0-indexed row within the pass
|
||||
pass: Option<(u8, u32)>,
|
||||
}
|
||||
|
||||
impl<'a> Iterator for ScanLines<'a> {
|
||||
type Item = ScanLine;
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.end == self.png.raw_data.len() {
|
||||
None
|
||||
} else if self.png.ihdr_data.interlaced == 1 {
|
||||
// Scanlines for interlaced PNG files
|
||||
if self.pass.is_none() {
|
||||
self.pass = Some((1, 0));
|
||||
}
|
||||
// Handle edge cases for images smaller than 5 pixels in either direction
|
||||
if self.png.ihdr_data.width < 5 && self.pass.unwrap().0 == 2 {
|
||||
if let Some(pass) = self.pass.as_mut() {
|
||||
pass.0 = 3;
|
||||
pass.1 = 4;
|
||||
}
|
||||
}
|
||||
// Intentionally keep these separate so that they can be applied one after another
|
||||
if self.png.ihdr_data.height < 5 && self.pass.unwrap().0 == 3 {
|
||||
if let Some(pass) = self.pass.as_mut() {
|
||||
pass.0 = 4;
|
||||
pass.1 = 0;
|
||||
}
|
||||
}
|
||||
let bits_per_pixel = self.png.ihdr_data.bit_depth.as_u8() as u32 *
|
||||
self.png.channels_per_pixel() as u32;
|
||||
let y_steps;
|
||||
let pixels_factor;
|
||||
match self.pass {
|
||||
Some((1, _)) | Some((2, _)) => {
|
||||
pixels_factor = 8;
|
||||
y_steps = 8;
|
||||
}
|
||||
Some((3, _)) => {
|
||||
pixels_factor = 4;
|
||||
y_steps = 8;
|
||||
}
|
||||
Some((4, _)) => {
|
||||
pixels_factor = 4;
|
||||
y_steps = 4;
|
||||
}
|
||||
Some((5, _)) => {
|
||||
pixels_factor = 2;
|
||||
y_steps = 4;
|
||||
}
|
||||
Some((6, _)) => {
|
||||
pixels_factor = 2;
|
||||
y_steps = 2;
|
||||
}
|
||||
Some((7, _)) => {
|
||||
pixels_factor = 1;
|
||||
y_steps = 2;
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
let mut pixels_per_line = self.png.ihdr_data.width / pixels_factor as u32;
|
||||
// Determine whether to add pixels if there is a final, incomplete 8x8 block
|
||||
let gap = self.png.ihdr_data.width % pixels_factor;
|
||||
if gap > 0 {
|
||||
match self.pass.unwrap().0 {
|
||||
1 | 3 | 5 => {
|
||||
pixels_per_line += 1;
|
||||
}
|
||||
2 => {
|
||||
if gap >= 5 {
|
||||
pixels_per_line += 1;
|
||||
}
|
||||
}
|
||||
4 => {
|
||||
if gap >= 3 {
|
||||
pixels_per_line += 1;
|
||||
}
|
||||
}
|
||||
6 => {
|
||||
if gap >= 2 {
|
||||
pixels_per_line += 1;
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
};
|
||||
}
|
||||
let current_pass = if let Some(pass) = self.pass {
|
||||
Some(pass.0)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let bytes_per_line = ((pixels_per_line * bits_per_pixel) as f32 / 8f32).ceil() as usize;
|
||||
self.start = self.end;
|
||||
self.end = self.start + bytes_per_line + 1;
|
||||
if let Some(pass) = self.pass.as_mut() {
|
||||
if pass.1 + y_steps >= self.png.ihdr_data.height {
|
||||
pass.0 += 1;
|
||||
pass.1 = match pass.0 {
|
||||
3 => 4,
|
||||
5 => 2,
|
||||
7 => 1,
|
||||
_ => 0,
|
||||
};
|
||||
} else {
|
||||
pass.1 += y_steps;
|
||||
}
|
||||
}
|
||||
Some(ScanLine {
|
||||
filter: self.png.raw_data[self.start],
|
||||
data: self.png.raw_data[(self.start + 1)..self.end].to_owned(),
|
||||
pass: current_pass,
|
||||
})
|
||||
} else {
|
||||
// Standard, non-interlaced PNG scanlines
|
||||
let bits_per_line = self.png.ihdr_data.width as usize *
|
||||
self.png.ihdr_data.bit_depth.as_u8() as usize *
|
||||
self.png.channels_per_pixel() as usize;
|
||||
let bytes_per_line = (bits_per_line as f32 / 8f32).ceil() as usize;
|
||||
self.start = self.end;
|
||||
self.end = self.start + bytes_per_line + 1;
|
||||
Some(ScanLine {
|
||||
filter: self.png.raw_data[self.start],
|
||||
data: self.png.raw_data[(self.start + 1)..self.end].to_owned(),
|
||||
pass: None,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone)]
|
||||
/// A scan line in a PNG image
|
||||
pub struct ScanLine {
|
||||
/// The filter type used to encode the current scan line (0-4)
|
||||
pub filter: u8,
|
||||
/// The byte data for the current scan line, encoded with the filter specified in the `filter` field
|
||||
pub data: Vec<u8>,
|
||||
/// The current pass if the image is interlaced
|
||||
pub pass: Option<u8>,
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone)]
|
||||
/// Contains all data relevant to a PNG image
|
||||
pub struct PngData {
|
||||
/// The filtered and compressed data of the IDAT chunk
|
||||
pub idat_data: Vec<u8>,
|
||||
/// The headers stored in the IHDR chunk
|
||||
pub ihdr_data: IhdrData,
|
||||
/// The uncompressed, optionally filtered data from the IDAT chunk
|
||||
pub raw_data: Vec<u8>,
|
||||
/// The palette containing colors used in an Indexed image
|
||||
/// Contains 3 bytes per color (R+G+B), up to 768
|
||||
pub palette: Option<Vec<u8>>,
|
||||
/// The pixel value that should be rendered as transparent
|
||||
pub transparency_pixel: Option<Vec<u8>>,
|
||||
/// A map of how transparent each color in the palette should be
|
||||
pub transparency_palette: Option<Vec<u8>>,
|
||||
/// All non-critical headers from the PNG are stored here
|
||||
pub aux_headers: HashMap<String, Vec<u8>>,
|
||||
}
|
||||
|
||||
impl PngData {
|
||||
/// Create a new `PngData` struct by opening a file
|
||||
pub fn new(filepath: &Path, fix_errors: bool) -> Result<PngData, String> {
|
||||
let mut file = match File::open(filepath) {
|
||||
Ok(f) => f,
|
||||
Err(_) => return Err("Failed to open file for reading".to_owned()),
|
||||
};
|
||||
let mut byte_data: Vec<u8> = Vec::new();
|
||||
// Read raw png data into memory
|
||||
match file.read_to_end(&mut byte_data) {
|
||||
Ok(_) => (),
|
||||
Err(_) => return Err("Failed to read from file".to_owned()),
|
||||
}
|
||||
|
||||
PngData::from_slice(&byte_data, fix_errors)
|
||||
}
|
||||
|
||||
/// Create a new `PngData` struct by reading a slice
|
||||
pub fn from_slice(byte_data: &[u8], fix_errors: bool) -> Result<PngData, String> {
|
||||
let mut byte_offset: usize = 0;
|
||||
// Test that png header is valid
|
||||
let header: Vec<u8> = byte_data.iter().take(8).cloned().collect();
|
||||
if !file_header_is_valid(header.as_ref()) {
|
||||
return Err("Invalid PNG header detected".to_owned());
|
||||
}
|
||||
byte_offset += 8;
|
||||
// Read the data headers
|
||||
let mut aux_headers: HashMap<String, Vec<u8>> = HashMap::new();
|
||||
let mut idat_headers: Vec<u8> = Vec::new();
|
||||
loop {
|
||||
let header = parse_next_header(byte_data.as_ref(), &mut byte_offset, fix_errors);
|
||||
let header = match header {
|
||||
Ok(x) => x,
|
||||
Err(x) => return Err(x),
|
||||
};
|
||||
let header = match header {
|
||||
Some(x) => x,
|
||||
None => break,
|
||||
};
|
||||
if header.0 == "IDAT" {
|
||||
idat_headers.extend(header.1);
|
||||
} else {
|
||||
aux_headers.insert(header.0, header.1);
|
||||
}
|
||||
}
|
||||
// Parse the headers into our PngData
|
||||
if idat_headers.is_empty() {
|
||||
return Err("Image data was empty, skipping".to_owned());
|
||||
}
|
||||
if aux_headers.get("IHDR").is_none() {
|
||||
return Err("Image header data was missing, skipping".to_owned());
|
||||
}
|
||||
let ihdr_header = match parse_ihdr_header(aux_headers.remove("IHDR").unwrap().as_ref()) {
|
||||
Ok(x) => x,
|
||||
Err(x) => return Err(x),
|
||||
};
|
||||
let raw_data = match super::deflate::deflate::inflate(idat_headers.as_ref()) {
|
||||
Ok(x) => x,
|
||||
Err(x) => return Err(x),
|
||||
};
|
||||
// Handle transparency header
|
||||
let mut has_transparency_pixel = false;
|
||||
let mut has_transparency_palette = false;
|
||||
if aux_headers.contains_key("tRNS") {
|
||||
if ihdr_header.color_type == ColorType::Indexed {
|
||||
has_transparency_palette = true;
|
||||
} else {
|
||||
has_transparency_pixel = true;
|
||||
}
|
||||
}
|
||||
let mut png_data = PngData {
|
||||
idat_data: idat_headers.clone(),
|
||||
ihdr_data: ihdr_header,
|
||||
raw_data: raw_data,
|
||||
palette: aux_headers.remove("PLTE"),
|
||||
transparency_pixel: if has_transparency_pixel {
|
||||
aux_headers.remove("tRNS")
|
||||
} else {
|
||||
None
|
||||
},
|
||||
transparency_palette: if has_transparency_palette {
|
||||
aux_headers.remove("tRNS")
|
||||
} else {
|
||||
None
|
||||
},
|
||||
aux_headers: aux_headers,
|
||||
};
|
||||
png_data.raw_data = png_data.unfilter_image();
|
||||
// Return the PngData
|
||||
Ok(png_data)
|
||||
}
|
||||
|
||||
/// Return the number of channels in the image, based on color type
|
||||
#[inline]
|
||||
pub fn channels_per_pixel(&self) -> u8 {
|
||||
match self.ihdr_data.color_type {
|
||||
ColorType::Grayscale | ColorType::Indexed => 1,
|
||||
ColorType::GrayscaleAlpha => 2,
|
||||
ColorType::RGB => 3,
|
||||
ColorType::RGBA => 4,
|
||||
}
|
||||
}
|
||||
|
||||
/// Format the `PngData` struct into a valid PNG bytestream
|
||||
pub fn output(&self) -> Vec<u8> {
|
||||
// PNG header
|
||||
let mut output = vec![0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A];
|
||||
// IHDR
|
||||
let mut ihdr_data = Vec::with_capacity(13);
|
||||
ihdr_data.write_u32::<BigEndian>(self.ihdr_data.width).ok();
|
||||
ihdr_data.write_u32::<BigEndian>(self.ihdr_data.height).ok();
|
||||
ihdr_data.write_u8(self.ihdr_data.bit_depth.as_u8()).ok();
|
||||
ihdr_data.write_u8(self.ihdr_data.color_type.png_header_code()).ok();
|
||||
ihdr_data.write_u8(0).ok(); // Compression -- deflate
|
||||
ihdr_data.write_u8(0).ok(); // Filter method -- 5-way adaptive filtering
|
||||
ihdr_data.write_u8(self.ihdr_data.interlaced).ok();
|
||||
write_png_block(b"IHDR", &ihdr_data, &mut output);
|
||||
// Ancillary headers
|
||||
for (key, header) in self.aux_headers
|
||||
.iter()
|
||||
.filter(|&(ref key, _)| !(**key == "bKGD" || **key == "hIST" || **key == "tRNS")) {
|
||||
write_png_block(key.as_bytes(), header, &mut output);
|
||||
}
|
||||
// Palette
|
||||
if let Some(palette) = self.palette.clone() {
|
||||
write_png_block(b"PLTE", &palette, &mut output);
|
||||
if let Some(transparency_palette) = self.transparency_palette.clone() {
|
||||
// Transparency pixel
|
||||
write_png_block(b"tRNS", &transparency_palette, &mut output);
|
||||
}
|
||||
} else if let Some(transparency_pixel) = self.transparency_pixel.clone() {
|
||||
// Transparency pixel
|
||||
write_png_block(b"tRNS", &transparency_pixel, &mut output);
|
||||
}
|
||||
// Special ancillary headers that need to come after PLTE but before IDAT
|
||||
for (key, header) in self.aux_headers
|
||||
.iter()
|
||||
.filter(|&(ref key, _)| **key == "bKGD" || **key == "hIST" || **key == "tRNS") {
|
||||
write_png_block(key.as_bytes(), header, &mut output);
|
||||
}
|
||||
// IDAT data
|
||||
write_png_block(b"IDAT", &self.idat_data, &mut output);
|
||||
// Stream end
|
||||
write_png_block(b"IEND", &[], &mut output);
|
||||
|
||||
output
|
||||
}
|
||||
|
||||
/// Return an iterator over the scanlines of the image
|
||||
pub fn scan_lines(&self) -> ScanLines {
|
||||
ScanLines {
|
||||
png: self,
|
||||
start: 0,
|
||||
end: 0,
|
||||
pass: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Reverse all filters applied on the image, returning an unfiltered IDAT bytestream
|
||||
pub fn unfilter_image(&self) -> Vec<u8> {
|
||||
let mut unfiltered = Vec::with_capacity(self.raw_data.len());
|
||||
let bpp = (((self.ihdr_data.bit_depth.as_u8() * self.channels_per_pixel()) as f32) /
|
||||
8f32)
|
||||
.ceil() as usize;
|
||||
let mut last_line: Vec<u8> = Vec::new();
|
||||
for line in self.scan_lines() {
|
||||
let unfiltered_line = unfilter_line(line.filter, bpp, &line.data, &last_line);
|
||||
unfiltered.push(0);
|
||||
unfiltered.extend_from_slice(&unfiltered_line);
|
||||
last_line = unfiltered_line;
|
||||
}
|
||||
unfiltered
|
||||
}
|
||||
|
||||
/// Apply the specified filter type to all rows in the image
|
||||
/// 0: None
|
||||
/// 1: Sub
|
||||
/// 2: Up
|
||||
/// 3: Average
|
||||
/// 4: Paeth
|
||||
/// 5: All (heuristically pick the best filter for each line)
|
||||
pub fn filter_image(&self, filter: u8) -> Vec<u8> {
|
||||
let mut filtered = Vec::with_capacity(self.raw_data.len());
|
||||
let bpp = (((self.ihdr_data.bit_depth.as_u8() * self.channels_per_pixel()) as f32) /
|
||||
8f32)
|
||||
.ceil() as usize;
|
||||
let mut last_line: Vec<u8> = Vec::new();
|
||||
let mut last_pass: Option<u8> = None;
|
||||
for line in self.scan_lines() {
|
||||
match filter {
|
||||
0 | 1 | 2 | 3 | 4 => {
|
||||
if last_pass == line.pass || filter <= 1 {
|
||||
filtered.push(filter);
|
||||
filtered.extend_from_slice(&filter_line(filter,
|
||||
bpp,
|
||||
&line.data,
|
||||
&last_line));
|
||||
} else {
|
||||
// Avoid vertical filtering on first line of each interlacing pass
|
||||
filtered.push(0);
|
||||
filtered.extend_from_slice(&filter_line(0, bpp, &line.data, &last_line));
|
||||
}
|
||||
}
|
||||
5 => {
|
||||
// Heuristically guess best filter per line
|
||||
// Uses MSAD algorithm mentioned in libpng reference docs
|
||||
// http://www.libpng.org/pub/png/book/chapter09.html
|
||||
let mut trials: HashMap<u8, Vec<u8>> = HashMap::with_capacity(5);
|
||||
// Avoid vertical filtering on first line of each interlacing pass
|
||||
for filter in if last_pass == line.pass {
|
||||
0..5
|
||||
} else {
|
||||
0..2
|
||||
} {
|
||||
trials.insert(filter, filter_line(filter, bpp, &line.data, &last_line));
|
||||
}
|
||||
let (best_filter, best_line) = trials.iter()
|
||||
.min_by_key(|x| {
|
||||
x.1.iter().fold(0u64, |acc, &x| {
|
||||
let signed = x as i8;
|
||||
acc + (signed as i16).abs() as u64
|
||||
})
|
||||
})
|
||||
.unwrap();
|
||||
filtered.push(*best_filter);
|
||||
filtered.extend_from_slice(best_line);
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
last_line = line.data.clone();
|
||||
last_pass = line.pass;
|
||||
}
|
||||
filtered
|
||||
}
|
||||
|
||||
/// Attempt to reduce the bit depth of the image
|
||||
/// Returns true if the bit depth was reduced, false otherwise
|
||||
pub fn reduce_bit_depth(&mut self) -> bool {
|
||||
if self.ihdr_data.bit_depth != BitDepth::Sixteen {
|
||||
if self.ihdr_data.color_type == ColorType::Indexed ||
|
||||
self.ihdr_data.color_type == ColorType::Grayscale {
|
||||
return match reduce_bit_depth_8_or_less(self) {
|
||||
Some((data, depth)) => {
|
||||
self.raw_data = data;
|
||||
self.ihdr_data.bit_depth = BitDepth::from_u8(depth);
|
||||
true
|
||||
}
|
||||
None => false,
|
||||
};
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Reduce from 16 to 8 bits per channel per pixel
|
||||
let mut reduced = Vec::with_capacity((self.ihdr_data.width * self.ihdr_data.height *
|
||||
self.channels_per_pixel() as u32 +
|
||||
self.ihdr_data
|
||||
.height) as usize);
|
||||
let mut high_byte = 0;
|
||||
|
||||
for line in self.scan_lines() {
|
||||
reduced.push(line.filter);
|
||||
for (i, byte) in line.data.iter().enumerate() {
|
||||
if i % 2 == 0 {
|
||||
// High byte
|
||||
high_byte = *byte;
|
||||
} else {
|
||||
// Low byte
|
||||
if high_byte != *byte {
|
||||
// Can't reduce, exit early
|
||||
return false;
|
||||
}
|
||||
reduced.push(*byte);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.ihdr_data.bit_depth = BitDepth::Eight;
|
||||
self.raw_data = reduced;
|
||||
true
|
||||
}
|
||||
|
||||
/// Attempt to reduce the number of colors in the palette
|
||||
/// Returns true if the palette was reduced, false otherwise
|
||||
pub fn reduce_palette(&mut self) -> bool {
|
||||
if self.ihdr_data.color_type != ColorType::Indexed {
|
||||
// Can't reduce if there is no palette
|
||||
return false;
|
||||
}
|
||||
if self.ihdr_data.bit_depth == BitDepth::One {
|
||||
// Gains from 1-bit images will be at most 1 byte
|
||||
// Not worth the CPU time
|
||||
return false;
|
||||
}
|
||||
|
||||
// A palette with RGB slices
|
||||
let palette = self.palette.clone().unwrap();
|
||||
let mut indexed_palette: Vec<&[u8]> = palette.chunks(3).collect();
|
||||
// A map of old indexes to new ones, for any moved
|
||||
let mut index_map: HashMap<u8, u8> = HashMap::new();
|
||||
|
||||
// A list of (original) indices that are duplicates and no longer needed
|
||||
let mut duplicates: Vec<u8> = Vec::new();
|
||||
{
|
||||
// Find duplicate entries in the palette
|
||||
let mut seen: HashMap<&[u8], u8> = HashMap::with_capacity(indexed_palette.len());
|
||||
for (i, color) in indexed_palette.iter().enumerate() {
|
||||
if seen.contains_key(color) {
|
||||
let index = seen.get(color).unwrap();
|
||||
duplicates.push(i as u8);
|
||||
index_map.insert(i as u8, *index);
|
||||
} else {
|
||||
seen.insert(*color, i as u8);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Remove duplicates from the data
|
||||
if !duplicates.is_empty() {
|
||||
self.do_palette_reduction(&duplicates, &mut index_map, &mut indexed_palette);
|
||||
}
|
||||
|
||||
// Find palette entries that are never used
|
||||
let mut seen = HashSet::with_capacity(indexed_palette.len());
|
||||
for line in self.scan_lines() {
|
||||
match self.ihdr_data.bit_depth {
|
||||
BitDepth::Eight => {
|
||||
for byte in &line.data {
|
||||
seen.insert(*byte);
|
||||
}
|
||||
}
|
||||
BitDepth::Four => {
|
||||
let bitvec = BitVec::from_bytes(&line.data);
|
||||
let mut current = 0u8;
|
||||
for (i, bit) in bitvec.iter().enumerate() {
|
||||
let mod_i = i % 4;
|
||||
if bit {
|
||||
current += 2u8.pow(3u32 - mod_i as u32);
|
||||
}
|
||||
if mod_i == 3 {
|
||||
seen.insert(current);
|
||||
current = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
BitDepth::Two => {
|
||||
let bitvec = BitVec::from_bytes(&line.data);
|
||||
let mut current = 0u8;
|
||||
for (i, bit) in bitvec.iter().enumerate() {
|
||||
let mod_i = i % 2;
|
||||
if bit {
|
||||
current += 2u8.pow(1u32 - mod_i as u32);
|
||||
}
|
||||
if mod_i == 1 {
|
||||
seen.insert(current);
|
||||
current = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
|
||||
if seen.len() == indexed_palette.len() {
|
||||
// Exit early if no further possible optimizations
|
||||
// Check at the end of each line
|
||||
// Checking after every pixel would be overly expensive
|
||||
return !duplicates.is_empty();
|
||||
}
|
||||
}
|
||||
|
||||
let unused: Vec<u8> =
|
||||
(0..indexed_palette.len() as u8).filter(|i| !seen.contains(&i)).collect();
|
||||
|
||||
// Remove unused palette indices
|
||||
self.do_palette_reduction(&unused, &mut index_map, &mut indexed_palette);
|
||||
|
||||
true
|
||||
}
|
||||
|
||||
fn do_palette_reduction(&mut self,
|
||||
indices: &[u8],
|
||||
index_map: &mut HashMap<u8, u8>,
|
||||
indexed_palette: &mut Vec<&[u8]>) {
|
||||
let mut new_data = Vec::with_capacity(self.raw_data.len());
|
||||
let mut alpha_palette = self.aux_headers.get("tRNS").cloned();
|
||||
let original_len = indexed_palette.len();
|
||||
for idx in indices.iter().sorted_by(|a, b| b.cmp(a)) {
|
||||
for i in (*idx as usize + 1)..original_len {
|
||||
let existing = index_map.entry(i as u8).or_insert(i as u8);
|
||||
if *existing >= *idx {
|
||||
*existing -= 1;
|
||||
}
|
||||
}
|
||||
indexed_palette.remove(*idx as usize);
|
||||
if let Some(ref mut alpha) = alpha_palette {
|
||||
alpha.remove(*idx as usize);
|
||||
}
|
||||
}
|
||||
if alpha_palette.is_some() {
|
||||
let alpha_header = self.aux_headers.get_mut("tRNS");
|
||||
if let Some(alpha_hdr) = alpha_header {
|
||||
*alpha_hdr = alpha_palette.unwrap();
|
||||
}
|
||||
}
|
||||
// Reassign data bytes to new indices
|
||||
for line in self.scan_lines() {
|
||||
new_data.push(line.filter);
|
||||
match self.ihdr_data.bit_depth {
|
||||
BitDepth::Eight => {
|
||||
for byte in &line.data {
|
||||
if let Some(new_idx) = index_map.get(byte) {
|
||||
new_data.push(*new_idx);
|
||||
} else {
|
||||
new_data.push(*byte);
|
||||
}
|
||||
}
|
||||
}
|
||||
BitDepth::Four => {
|
||||
for byte in &line.data {
|
||||
let upper = *byte >> 4;
|
||||
let lower = *byte & 0b00001111;
|
||||
let mut new_byte = 0u8;
|
||||
if let Some(new_idx) = index_map.get(&upper) {
|
||||
new_byte &= *new_idx << 4;
|
||||
} else {
|
||||
new_byte &= upper << 4;
|
||||
}
|
||||
if let Some(new_idx) = index_map.get(&lower) {
|
||||
new_byte &= *new_idx;
|
||||
} else {
|
||||
new_byte &= lower;
|
||||
}
|
||||
new_data.push(new_byte);
|
||||
}
|
||||
}
|
||||
BitDepth::Two => {
|
||||
for byte in &line.data {
|
||||
let one = *byte >> 6;
|
||||
let two = (*byte >> 4) & 0b00000011;
|
||||
let three = (*byte >> 2) & 0b00000011;
|
||||
let four = *byte & 0b00000011;
|
||||
let mut new_byte = 0u8;
|
||||
if let Some(new_idx) = index_map.get(&one) {
|
||||
new_byte &= *new_idx << 6;
|
||||
} else {
|
||||
new_byte &= one << 6;
|
||||
}
|
||||
if let Some(new_idx) = index_map.get(&two) {
|
||||
new_byte &= *new_idx << 4;
|
||||
} else {
|
||||
new_byte &= two << 4;
|
||||
}
|
||||
if let Some(new_idx) = index_map.get(&three) {
|
||||
new_byte &= *new_idx << 2;
|
||||
} else {
|
||||
new_byte &= three << 2;
|
||||
}
|
||||
if let Some(new_idx) = index_map.get(&four) {
|
||||
new_byte &= *new_idx;
|
||||
} else {
|
||||
new_byte &= four;
|
||||
}
|
||||
new_data.push(new_byte);
|
||||
}
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
index_map.clear();
|
||||
self.raw_data = new_data;
|
||||
let new_palette = indexed_palette.iter().cloned().flatten().cloned().collect::<Vec<u8>>();
|
||||
self.palette = Some(new_palette);
|
||||
}
|
||||
|
||||
/// Attempt to reduce the color type of the image
|
||||
/// Returns true if the color type was reduced, false otherwise
|
||||
pub fn reduce_color_type(&mut self) -> bool {
|
||||
let mut changed = false;
|
||||
let mut should_reduce_bit_depth = false;
|
||||
|
||||
// Go down one step at a time
|
||||
// Maybe not the most efficient, but it's safe
|
||||
if self.ihdr_data.color_type == ColorType::RGBA {
|
||||
if let Some(data) = reduce_rgba_to_grayscale_alpha(self) {
|
||||
self.raw_data = data;
|
||||
self.ihdr_data.color_type = ColorType::GrayscaleAlpha;
|
||||
changed = true;
|
||||
} else if let Some(data) = reduce_rgba_to_rgb(self) {
|
||||
self.raw_data = data;
|
||||
self.ihdr_data.color_type = ColorType::RGB;
|
||||
changed = true;
|
||||
} else if let Some((data, palette, trans)) = reduce_rgba_to_palette(self) {
|
||||
self.raw_data = data;
|
||||
self.palette = Some(palette);
|
||||
if trans.iter().any(|x| *x != 255) {
|
||||
self.transparency_palette = Some(trans);
|
||||
} else {
|
||||
self.transparency_palette = None;
|
||||
}
|
||||
self.ihdr_data.color_type = ColorType::Indexed;
|
||||
changed = true;
|
||||
should_reduce_bit_depth = true;
|
||||
}
|
||||
}
|
||||
|
||||
if self.ihdr_data.color_type == ColorType::GrayscaleAlpha {
|
||||
if let Some(data) = reduce_grayscale_alpha_to_grayscale(self) {
|
||||
self.raw_data = data;
|
||||
self.ihdr_data.color_type = ColorType::Grayscale;
|
||||
changed = true;
|
||||
should_reduce_bit_depth = true;
|
||||
}
|
||||
}
|
||||
|
||||
if self.ihdr_data.color_type == ColorType::RGB {
|
||||
if let Some(data) = reduce_rgb_to_grayscale(self) {
|
||||
self.raw_data = data;
|
||||
self.ihdr_data.color_type = ColorType::Grayscale;
|
||||
changed = true;
|
||||
should_reduce_bit_depth = true;
|
||||
} else if let Some((data, palette)) = reduce_rgb_to_palette(self) {
|
||||
self.raw_data = data;
|
||||
self.palette = Some(palette);
|
||||
self.ihdr_data.color_type = ColorType::Indexed;
|
||||
changed = true;
|
||||
should_reduce_bit_depth = true;
|
||||
}
|
||||
}
|
||||
|
||||
if self.ihdr_data.color_type == ColorType::Grayscale {
|
||||
if let Some((data, palette)) = reduce_grayscale_to_palette(self) {
|
||||
self.raw_data = data;
|
||||
self.palette = Some(palette);
|
||||
self.ihdr_data.color_type = ColorType::Indexed;
|
||||
changed = true;
|
||||
should_reduce_bit_depth = true;
|
||||
}
|
||||
}
|
||||
|
||||
if should_reduce_bit_depth {
|
||||
// Some conversions will allow us to perform bit depth reduction that
|
||||
// wasn't possible before
|
||||
if let Some((data, depth)) = reduce_bit_depth_8_or_less(self) {
|
||||
self.raw_data = data;
|
||||
self.ihdr_data.bit_depth = BitDepth::from_u8(depth);
|
||||
}
|
||||
}
|
||||
|
||||
changed
|
||||
}
|
||||
|
||||
/// Convert the image to the specified interlacing type
|
||||
/// Returns true if the interlacing was changed, false otherwise
|
||||
/// The `interlace` parameter specifies the *new* interlacing mode
|
||||
/// Assumes that the data has already been de-filtered
|
||||
#[inline]
|
||||
pub fn change_interlacing(&mut self, interlace: u8) -> bool {
|
||||
if interlace == self.ihdr_data.interlaced {
|
||||
return false;
|
||||
}
|
||||
|
||||
if interlace == 1 {
|
||||
// Convert progressive to interlaced data
|
||||
interlace_image(self);
|
||||
} else {
|
||||
// Convert interlaced to progressive data
|
||||
deinterlace_image(self);
|
||||
}
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn write_png_block(key: &[u8], header: &[u8], output: &mut Vec<u8>) {
|
||||
let mut header_data = Vec::with_capacity(header.len() + 4);
|
||||
header_data.extend_from_slice(key);
|
||||
header_data.extend_from_slice(header);
|
||||
output.reserve(header_data.len() + 8);
|
||||
output.write_u32::<BigEndian>(header_data.len() as u32 - 4).ok();
|
||||
let crc = crc32::checksum_ieee(&header_data);
|
||||
output.append(&mut header_data);
|
||||
output.write_u32::<BigEndian>(crc).ok();
|
||||
}
|
||||
578
src/png/mod.rs
Normal file
|
|
@ -0,0 +1,578 @@
|
|||
use std::{fs, path::Path, sync::Arc};
|
||||
|
||||
use libdeflater::{CompressionLvl, Compressor};
|
||||
use log::warn;
|
||||
use rustc_hash::FxHashMap;
|
||||
|
||||
use crate::{
|
||||
Options, PngResult,
|
||||
apng::*,
|
||||
colors::{BitDepth, ColorType},
|
||||
deflate,
|
||||
error::PngError,
|
||||
filters::*,
|
||||
headers::*,
|
||||
interlace::{deinterlace_image, interlace_image},
|
||||
};
|
||||
|
||||
pub(crate) mod scan_lines;
|
||||
|
||||
use self::scan_lines::ScanLines;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct PngImage {
|
||||
/// The headers stored in the IHDR chunk
|
||||
pub ihdr: IhdrData,
|
||||
/// The uncompressed, unfiltered data from the IDAT chunk
|
||||
pub data: Vec<u8>,
|
||||
}
|
||||
|
||||
/// Contains all data relevant to a PNG image
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct PngData {
|
||||
/// Uncompressed image data
|
||||
pub raw: Arc<PngImage>,
|
||||
/// The filtered and compressed data of the IDAT chunk
|
||||
pub idat_data: Vec<u8>,
|
||||
/// All non-critical chunks from the PNG are stored here
|
||||
pub aux_chunks: Vec<Chunk>,
|
||||
/// APNG frames
|
||||
pub frames: Vec<Frame>,
|
||||
}
|
||||
|
||||
impl PngData {
|
||||
/// Create a new `PngData` struct by opening a file
|
||||
#[inline]
|
||||
pub fn new(filepath: &Path, opts: &Options) -> PngResult<Self> {
|
||||
let byte_data = Self::read_file(filepath)?;
|
||||
|
||||
Self::from_slice(&byte_data, opts)
|
||||
}
|
||||
|
||||
pub fn read_file(filepath: &Path) -> PngResult<Vec<u8>> {
|
||||
fs::read(filepath).map_err(|e| PngError::ReadFailed(filepath.display().to_string(), e))
|
||||
}
|
||||
|
||||
/// Create a new `PngData` struct by reading a slice
|
||||
pub fn from_slice(byte_data: &[u8], opts: &Options) -> PngResult<Self> {
|
||||
let mut byte_offset: usize = 0;
|
||||
// Test that png header is valid
|
||||
let header = byte_data.get(0..8).ok_or(PngError::TruncatedData)?;
|
||||
if !file_header_is_valid(header) {
|
||||
return Err(PngError::NotPNG);
|
||||
}
|
||||
byte_offset += 8;
|
||||
|
||||
// Read the data chunks
|
||||
let mut idat_data: Vec<u8> = Vec::new();
|
||||
let mut key_chunks: FxHashMap<[u8; 4], Vec<u8>> = FxHashMap::default();
|
||||
let mut aux_chunks: Vec<Chunk> = Vec::new();
|
||||
let mut frames: Vec<Frame> = Vec::new();
|
||||
let mut sequence_number = 0;
|
||||
while let Some(chunk) = parse_next_chunk(byte_data, &mut byte_offset, opts.fix_errors)? {
|
||||
match &chunk.name {
|
||||
b"IDAT" => {
|
||||
if idat_data.is_empty() {
|
||||
// Keep track of where the first IDAT sits relative to other chunks
|
||||
aux_chunks.push(Chunk {
|
||||
name: chunk.name,
|
||||
data: Vec::new(),
|
||||
});
|
||||
}
|
||||
idat_data.extend_from_slice(chunk.data);
|
||||
}
|
||||
b"IHDR" | b"PLTE" | b"tRNS" => {
|
||||
key_chunks.insert(chunk.name, chunk.data.to_owned());
|
||||
}
|
||||
_ if opts.strip.keep(&chunk.name) => {
|
||||
if chunk.is_c2pa() {
|
||||
// StripChunks::None is the default value, so to keep optimizing by default,
|
||||
// interpret it as stripping the C2PA metadata.
|
||||
// The C2PA metadata is invalidated if the file changes, so it shouldn't be kept.
|
||||
if opts.strip == StripChunks::None {
|
||||
continue;
|
||||
}
|
||||
return Err(PngError::C2PAMetadataPreventsChanges);
|
||||
}
|
||||
if chunk.name == *b"fcTL" || chunk.name == *b"fdAT" {
|
||||
// Validate the sequence number
|
||||
if read_be_u32(&chunk.data[0..4]) != sequence_number {
|
||||
return Err(PngError::APNGOutOfOrder);
|
||||
}
|
||||
sequence_number += 1;
|
||||
if chunk.name == *b"fcTL" && !idat_data.is_empty() {
|
||||
// Only create a Frame if it's after the IDAT (else store it as an aux chunk)
|
||||
frames.push(Frame::from_fctl_data(chunk.data)?);
|
||||
continue;
|
||||
} else if chunk.name == *b"fdAT" {
|
||||
// Append the data to the last frame
|
||||
frames
|
||||
.last_mut()
|
||||
.ok_or(PngError::APNGOutOfOrder)?
|
||||
.data
|
||||
.extend_from_slice(&chunk.data[4..]);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
// Regular ancillary chunk
|
||||
aux_chunks.push(Chunk {
|
||||
name: chunk.name,
|
||||
data: chunk.data.to_owned(),
|
||||
});
|
||||
}
|
||||
b"acTL" => {
|
||||
warn!("Stripping animation data from APNG - image will become standard PNG");
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
// Parse the chunks into our PngData
|
||||
if idat_data.is_empty() {
|
||||
return Err(PngError::ChunkMissing("IDAT"));
|
||||
}
|
||||
let Some(ihdr_chunk) = key_chunks.remove(b"IHDR") else {
|
||||
return Err(PngError::ChunkMissing("IHDR"));
|
||||
};
|
||||
let ihdr = parse_ihdr_chunk(
|
||||
&ihdr_chunk,
|
||||
key_chunks.remove(b"PLTE"),
|
||||
key_chunks.remove(b"tRNS"),
|
||||
)?;
|
||||
if let Some(max) = opts.max_decompressed_size {
|
||||
if ihdr.raw_data_size() > max {
|
||||
return Err(PngError::InflatedDataTooLong(max));
|
||||
}
|
||||
}
|
||||
|
||||
let raw = PngImage::new(ihdr, &idat_data)?;
|
||||
|
||||
// Return the PngData
|
||||
Ok(Self {
|
||||
idat_data,
|
||||
raw: Arc::new(raw),
|
||||
aux_chunks,
|
||||
frames,
|
||||
})
|
||||
}
|
||||
|
||||
/// Format the `PngData` struct into a valid PNG bytestream
|
||||
#[must_use]
|
||||
pub fn output(&self) -> Vec<u8> {
|
||||
// PNG header
|
||||
let mut output = vec![0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A];
|
||||
// IHDR
|
||||
let mut ihdr_data = Vec::with_capacity(13);
|
||||
ihdr_data.extend_from_slice(&self.raw.ihdr.width.to_be_bytes());
|
||||
ihdr_data.extend_from_slice(&self.raw.ihdr.height.to_be_bytes());
|
||||
ihdr_data.extend_from_slice(&[self.raw.ihdr.bit_depth as u8]);
|
||||
ihdr_data.extend_from_slice(&[
|
||||
self.raw.ihdr.color_type.png_header_code(),
|
||||
0, // Compression -- deflate
|
||||
0, // Filter method -- 5-way adaptive filtering
|
||||
self.raw.ihdr.interlaced as u8,
|
||||
]);
|
||||
write_png_block(b"IHDR", &ihdr_data, &mut output);
|
||||
// Ancillary chunks - split into those that come before IDAT and those that come after
|
||||
let mut aux_split = self.aux_chunks.split(|c| &c.name == b"IDAT");
|
||||
let aux_pre = aux_split.next().unwrap();
|
||||
// Many chunks need to be before PLTE, so write all except those that explicitly need to be after
|
||||
// Note: the PNG spec does not say that fcTL needs to be after PLTE, but some decoders expect
|
||||
// that (see issue #625)
|
||||
for chunk in aux_pre
|
||||
.iter()
|
||||
.filter(|c| !matches!(&c.name, b"bKGD" | b"hIST" | b"tRNS" | b"fcTL"))
|
||||
{
|
||||
write_png_block(&chunk.name, &chunk.data, &mut output);
|
||||
}
|
||||
// Palette and transparency
|
||||
match &self.raw.ihdr.color_type {
|
||||
ColorType::Indexed { palette } => {
|
||||
let mut palette_data = Vec::with_capacity(palette.len() * 3);
|
||||
for px in palette {
|
||||
palette_data.extend_from_slice(px.rgb().as_ref());
|
||||
}
|
||||
write_png_block(b"PLTE", &palette_data, &mut output);
|
||||
if let Some(last_trns) = palette.iter().rposition(|px| px.a != 255) {
|
||||
let trns_data: Vec<_> = palette[0..=last_trns].iter().map(|px| px.a).collect();
|
||||
write_png_block(b"tRNS", &trns_data, &mut output);
|
||||
}
|
||||
}
|
||||
ColorType::Grayscale {
|
||||
transparent_shade: Some(trns),
|
||||
} => {
|
||||
// Transparency pixel - 2 byte u16
|
||||
write_png_block(b"tRNS", &trns.to_be_bytes(), &mut output);
|
||||
}
|
||||
ColorType::RGB {
|
||||
transparent_color: Some(trns),
|
||||
} => {
|
||||
// Transparency pixel - 6 byte RGB16
|
||||
let trns_data: Vec<_> = trns.iter().flat_map(u16::to_be_bytes).collect();
|
||||
write_png_block(b"tRNS", &trns_data, &mut output);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
// Special ancillary chunks that need to come after PLTE but before IDAT
|
||||
let mut sequence_number = 0;
|
||||
for chunk in aux_pre
|
||||
.iter()
|
||||
.filter(|c| matches!(&c.name, b"bKGD" | b"hIST" | b"tRNS" | b"fcTL"))
|
||||
{
|
||||
write_png_block(&chunk.name, &chunk.data, &mut output);
|
||||
if &chunk.name == b"fcTL" {
|
||||
sequence_number += 1;
|
||||
}
|
||||
}
|
||||
// IDAT data
|
||||
write_png_block(b"IDAT", &self.idat_data, &mut output);
|
||||
// APNG frames
|
||||
for frame in self.frames.iter() {
|
||||
write_png_block(b"fcTL", &frame.fctl_data(sequence_number), &mut output);
|
||||
write_png_block(b"fdAT", &frame.fdat_data(sequence_number + 1), &mut output);
|
||||
sequence_number += 2;
|
||||
}
|
||||
// Ancillary chunks that come after IDAT
|
||||
for aux_post in aux_split {
|
||||
for chunk in aux_post {
|
||||
write_png_block(&chunk.name, &chunk.data, &mut output);
|
||||
}
|
||||
}
|
||||
// Stream end
|
||||
write_png_block(b"IEND", &[], &mut output);
|
||||
|
||||
output
|
||||
}
|
||||
}
|
||||
|
||||
impl PngImage {
|
||||
pub fn new(ihdr: IhdrData, compressed_data: &[u8]) -> PngResult<Self> {
|
||||
let raw_data = deflate::inflate(compressed_data, ihdr.raw_data_size())?;
|
||||
|
||||
// Reject files with incorrect width/height or truncated data
|
||||
if raw_data.len() != ihdr.raw_data_size() {
|
||||
return Err(PngError::TruncatedData);
|
||||
}
|
||||
|
||||
let mut image = Self {
|
||||
ihdr,
|
||||
data: raw_data,
|
||||
};
|
||||
image.data = image.unfilter_image()?;
|
||||
Ok(image)
|
||||
}
|
||||
|
||||
/// Enable or disable interlacing
|
||||
/// Returns the new image if the interlacing was changed, None otherwise
|
||||
/// Assumes that the data has already been de-filtered
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn change_interlacing(&self, interlace: bool) -> Option<Self> {
|
||||
if interlace == self.ihdr.interlaced {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(if interlace {
|
||||
// Convert progressive to interlaced data
|
||||
interlace_image(self)
|
||||
} else {
|
||||
// Convert interlaced to progressive data
|
||||
deinterlace_image(self)
|
||||
})
|
||||
}
|
||||
|
||||
/// Return the number of channels in the image, based on color type
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub const fn channels_per_pixel(&self) -> usize {
|
||||
self.ihdr.color_type.channels_per_pixel() as usize
|
||||
}
|
||||
|
||||
/// Return the number of bytes per channel in the image
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub const fn bytes_per_channel(&self) -> usize {
|
||||
match self.ihdr.bit_depth {
|
||||
BitDepth::Sixteen => 2,
|
||||
// Depths lower than 8 will round up to 1 byte
|
||||
_ => 1,
|
||||
}
|
||||
}
|
||||
|
||||
/// Calculate the size of the PLTE and tRNS chunks
|
||||
#[must_use]
|
||||
pub fn key_chunks_size(&self) -> usize {
|
||||
match &self.ihdr.color_type {
|
||||
ColorType::Indexed { palette } => {
|
||||
let plte = 12 + palette.len() * 3;
|
||||
palette
|
||||
.iter()
|
||||
.rposition(|p| p.a != 255)
|
||||
.map_or(plte, |trns| plte + 12 + trns + 1)
|
||||
}
|
||||
ColorType::Grayscale { transparent_shade } if transparent_shade.is_some() => 12 + 2,
|
||||
ColorType::RGB { transparent_color } if transparent_color.is_some() => 12 + 6,
|
||||
_ => 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Return an estimate of the output size which can help with evaluation of very small data
|
||||
#[must_use]
|
||||
pub fn estimated_output_size(&self, idat_data: &[u8]) -> usize {
|
||||
idat_data.len() + self.key_chunks_size()
|
||||
}
|
||||
|
||||
/// Return an iterator over the scanlines of the image
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn scan_lines(&self, has_filter: bool) -> ScanLines<'_> {
|
||||
ScanLines::new(self, has_filter)
|
||||
}
|
||||
|
||||
/// Reverse all filters applied on the image, returning an unfiltered IDAT bytestream
|
||||
fn unfilter_image(&self) -> PngResult<Vec<u8>> {
|
||||
let mut unfiltered = Vec::with_capacity(self.data.len());
|
||||
let bpp = self.bytes_per_channel() * self.channels_per_pixel();
|
||||
let mut last_line: Vec<u8> = Vec::new();
|
||||
let mut last_pass = None;
|
||||
let mut unfiltered_buf = Vec::new();
|
||||
for line in self.scan_lines(true) {
|
||||
if last_pass != line.pass {
|
||||
last_line.clear();
|
||||
last_pass = line.pass;
|
||||
}
|
||||
last_line.resize(line.data.len(), 0);
|
||||
let filter = RowFilter::try_from(line.filter).map_err(|()| PngError::InvalidData)?;
|
||||
filter.unfilter_line(bpp, line.data, &last_line, &mut unfiltered_buf);
|
||||
unfiltered.extend_from_slice(&unfiltered_buf);
|
||||
std::mem::swap(&mut last_line, &mut unfiltered_buf);
|
||||
unfiltered_buf.clear();
|
||||
}
|
||||
Ok(unfiltered)
|
||||
}
|
||||
|
||||
/// Apply the specified filter type to all rows in the image
|
||||
#[must_use]
|
||||
pub fn filter_image(
|
||||
&self,
|
||||
strategy: FilterStrategy,
|
||||
optimize_alpha: bool,
|
||||
) -> (Vec<u8>, FilterStrategy) {
|
||||
let mut filtered = Vec::with_capacity(self.data.len());
|
||||
let bpp = self.bytes_per_channel() * self.channels_per_pixel();
|
||||
// If alpha optimization is enabled, determine how many bytes of alpha there are per pixel
|
||||
let alpha_bytes = if optimize_alpha && self.ihdr.color_type.has_alpha() {
|
||||
self.bytes_per_channel()
|
||||
} else {
|
||||
0
|
||||
};
|
||||
|
||||
let mut prev_line = Vec::new();
|
||||
let mut prev_pass: Option<u8> = None;
|
||||
let mut f_buf = Vec::new();
|
||||
// For heuristic strategies, keep track of the actual filter used for each line
|
||||
let mut filters_used = Vec::new();
|
||||
// Pre-allocate buffers for the Bigrams strategy to avoid per-line allocations
|
||||
let (mut bigram_seen, mut bigram_touched) = if matches!(strategy, FilterStrategy::Bigrams) {
|
||||
(vec![false; 0x10000], Vec::<u16>::new())
|
||||
} else {
|
||||
(Vec::new(), Vec::new())
|
||||
};
|
||||
for (i, line) in self.scan_lines(false).enumerate() {
|
||||
if prev_pass != line.pass || line.data.len() != prev_line.len() {
|
||||
prev_line = vec![0; line.data.len()];
|
||||
}
|
||||
// Alpha optimisation may alter the line data, so we need a mutable copy of it
|
||||
let mut line_data = line.data.to_vec();
|
||||
|
||||
if let FilterStrategy::Basic(mut filter) = strategy {
|
||||
// Standard filters
|
||||
if prev_pass != line.pass && filter > RowFilter::Sub {
|
||||
filter = RowFilter::None;
|
||||
}
|
||||
filter.filter_line(bpp, &mut line_data, &prev_line, &mut f_buf, alpha_bytes);
|
||||
filtered.extend_from_slice(&f_buf);
|
||||
prev_line = line_data;
|
||||
} else if let FilterStrategy::Predefined(lines) = &strategy {
|
||||
// Predefined filter for each line
|
||||
let filter = lines.get(i).unwrap_or(&RowFilter::None);
|
||||
filter.filter_line(bpp, &mut line_data, &prev_line, &mut f_buf, alpha_bytes);
|
||||
filtered.extend_from_slice(&f_buf);
|
||||
prev_line = line_data;
|
||||
} else {
|
||||
// Heuristic filter selection strategies
|
||||
|
||||
let mut best_filter = RowFilter::None;
|
||||
if line_data.iter().all(|&x| x == 0) {
|
||||
// Assume None if the line is all zeros
|
||||
filtered.push(best_filter as u8);
|
||||
filtered.extend_from_slice(&line_data);
|
||||
prev_line = line_data;
|
||||
filters_used.push(best_filter);
|
||||
continue;
|
||||
}
|
||||
|
||||
let mut best_line = Vec::new();
|
||||
let mut best_line_raw = Vec::new();
|
||||
// Avoid vertical filtering on first line of each interlacing pass
|
||||
let try_filters = if prev_pass == line.pass {
|
||||
RowFilter::ALL.iter()
|
||||
} else {
|
||||
RowFilter::SINGLE_LINE.iter()
|
||||
};
|
||||
match strategy {
|
||||
FilterStrategy::MinSum => {
|
||||
// MSAD algorithm mentioned in libpng reference docs
|
||||
// http://www.libpng.org/pub/png/book/chapter09.html
|
||||
let mut best_size = usize::MAX;
|
||||
for f in try_filters {
|
||||
f.filter_line(bpp, &mut line_data, &prev_line, &mut f_buf, alpha_bytes);
|
||||
let size = f_buf.iter().fold(0, |acc, &x| {
|
||||
let signed = x as i8;
|
||||
acc + signed.unsigned_abs() as usize
|
||||
});
|
||||
if size < best_size {
|
||||
best_size = size;
|
||||
std::mem::swap(&mut best_line, &mut f_buf);
|
||||
best_line_raw.clone_from(&line_data);
|
||||
best_filter = *f;
|
||||
}
|
||||
}
|
||||
}
|
||||
FilterStrategy::Entropy => {
|
||||
// Shannon entropy algorithm, from LodePNG
|
||||
// https://github.com/lvandeve/lodepng
|
||||
let mut best_size = i32::MIN;
|
||||
for f in try_filters {
|
||||
f.filter_line(bpp, &mut line_data, &prev_line, &mut f_buf, alpha_bytes);
|
||||
let mut counts = vec![0; 0x100];
|
||||
for &i in &f_buf {
|
||||
counts[i as usize] += 1;
|
||||
}
|
||||
let size = counts.into_iter().fold(0, |acc, x| {
|
||||
if x == 0 {
|
||||
return acc;
|
||||
}
|
||||
acc + ilog2i(x)
|
||||
}) as i32;
|
||||
if size > best_size {
|
||||
best_size = size;
|
||||
std::mem::swap(&mut best_line, &mut f_buf);
|
||||
best_line_raw.clone_from(&line_data);
|
||||
best_filter = *f;
|
||||
}
|
||||
}
|
||||
}
|
||||
FilterStrategy::Bigrams => {
|
||||
// Count distinct bigrams, from pngwolf
|
||||
// https://bjoern.hoehrmann.de/pngwolf/
|
||||
let mut best_size = usize::MAX;
|
||||
for f in try_filters {
|
||||
f.filter_line(bpp, &mut line_data, &prev_line, &mut f_buf, alpha_bytes);
|
||||
let mut count = 0;
|
||||
for pair in f_buf.windows(2) {
|
||||
let bigram = ((pair[0] as usize) << 8) | pair[1] as usize;
|
||||
if !bigram_seen[bigram] {
|
||||
count += 1;
|
||||
if count >= best_size {
|
||||
break;
|
||||
}
|
||||
bigram_seen[bigram] = true;
|
||||
bigram_touched.push(bigram as u16);
|
||||
}
|
||||
}
|
||||
if count < best_size {
|
||||
best_size = count;
|
||||
std::mem::swap(&mut best_line, &mut f_buf);
|
||||
best_line_raw.clone_from(&line_data);
|
||||
best_filter = *f;
|
||||
}
|
||||
// Clear only the entries that were touched
|
||||
for &idx in &bigram_touched {
|
||||
bigram_seen[idx as usize] = false;
|
||||
}
|
||||
bigram_touched.clear();
|
||||
}
|
||||
}
|
||||
FilterStrategy::BigEnt => {
|
||||
// Bigram entropy, combined from Entropy and Bigrams filters
|
||||
let mut best_size = i32::MIN;
|
||||
// FxHasher is the fastest rust hasher currently available for this purpose
|
||||
let mut counts = FxHashMap::<u16, u32>::default();
|
||||
for f in try_filters {
|
||||
f.filter_line(bpp, &mut line_data, &prev_line, &mut f_buf, alpha_bytes);
|
||||
counts.clear();
|
||||
for pair in f_buf.windows(2) {
|
||||
let bigram = (u16::from(pair[0]) << 8) | u16::from(pair[1]);
|
||||
counts.entry(bigram).and_modify(|e| *e += 1).or_insert(1);
|
||||
}
|
||||
let size = counts.values().fold(0, |acc, &x| acc + ilog2i(x)) as i32;
|
||||
if size > best_size {
|
||||
best_size = size;
|
||||
std::mem::swap(&mut best_line, &mut f_buf);
|
||||
best_line_raw.clone_from(&line_data);
|
||||
best_filter = *f;
|
||||
}
|
||||
}
|
||||
}
|
||||
FilterStrategy::Brute { num_lines, level } => {
|
||||
// Brute force by compressing each filter attempt
|
||||
// Similar to that of LodePNG but includes some previous lines for context
|
||||
let mut best_size = usize::MAX;
|
||||
let line_start = filtered.len();
|
||||
filtered.resize(filtered.len() + line.data.len() + 1, 0);
|
||||
let mut compressor =
|
||||
Compressor::new(CompressionLvl::new(level.into()).unwrap());
|
||||
let limit = filtered.len().min((line.data.len() + 1) * num_lines);
|
||||
let capacity = compressor.deflate_compress_bound(limit);
|
||||
let mut dest = vec![0; capacity];
|
||||
|
||||
for f in try_filters {
|
||||
f.filter_line(bpp, &mut line_data, &prev_line, &mut f_buf, alpha_bytes);
|
||||
filtered[line_start..].copy_from_slice(&f_buf);
|
||||
let size = compressor
|
||||
.deflate_compress(&filtered[filtered.len() - limit..], &mut dest)
|
||||
.unwrap_or(usize::MAX);
|
||||
if size < best_size {
|
||||
best_size = size;
|
||||
std::mem::swap(&mut best_line, &mut f_buf);
|
||||
best_line_raw.clone_from(&line_data);
|
||||
best_filter = *f;
|
||||
}
|
||||
}
|
||||
filtered.resize(line_start, 0);
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
filtered.extend_from_slice(&best_line);
|
||||
prev_line = best_line_raw;
|
||||
filters_used.push(best_filter);
|
||||
}
|
||||
|
||||
prev_pass = line.pass;
|
||||
}
|
||||
|
||||
if filters_used.is_empty() {
|
||||
(filtered, strategy)
|
||||
} else {
|
||||
(filtered, FilterStrategy::Predefined(filters_used))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn write_png_block(key: &[u8], chunk: &[u8], output: &mut Vec<u8>) {
|
||||
let mut chunk_data = Vec::with_capacity(chunk.len() + 4);
|
||||
chunk_data.extend_from_slice(key);
|
||||
chunk_data.extend_from_slice(chunk);
|
||||
output.reserve(chunk_data.len() + 8);
|
||||
output.extend_from_slice(&(chunk_data.len() as u32 - 4).to_be_bytes());
|
||||
let crc = deflate::crc32(&chunk_data);
|
||||
output.append(&mut chunk_data);
|
||||
output.extend_from_slice(&crc.to_be_bytes());
|
||||
}
|
||||
|
||||
// Integer approximation for i * log2(i) - much faster than float calculations
|
||||
const fn ilog2i(i: u32) -> u32 {
|
||||
let log = 32 - i.leading_zeros() - 1;
|
||||
i * log + ((i - (1 << log)) << 1)
|
||||
}
|
||||
180
src/png/scan_lines.rs
Normal file
|
|
@ -0,0 +1,180 @@
|
|||
use crate::png::PngImage;
|
||||
|
||||
/// An iterator over the scan lines of a PNG image
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ScanLines<'a> {
|
||||
iter: ScanLineRanges,
|
||||
/// A reference to the PNG image being iterated upon
|
||||
raw_data: &'a [u8],
|
||||
/// Whether the raw data contains filter bytes
|
||||
has_filter: bool,
|
||||
}
|
||||
|
||||
impl<'a> ScanLines<'a> {
|
||||
#[must_use]
|
||||
pub fn new(png: &'a PngImage, has_filter: bool) -> Self {
|
||||
Self {
|
||||
iter: ScanLineRanges::new(png, has_filter),
|
||||
raw_data: &png.data,
|
||||
has_filter,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Iterator for ScanLines<'a> {
|
||||
type Item = ScanLine<'a>;
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
let (len, pass, num_pixels) = self.iter.next()?;
|
||||
debug_assert!(self.raw_data.len() >= len);
|
||||
debug_assert!(!self.has_filter || len > 1);
|
||||
// The data length should always be correct here but this check assures
|
||||
// the compiler that it doesn't need to account for a potential panic
|
||||
if self.raw_data.len() < len {
|
||||
return None;
|
||||
}
|
||||
let (data, rest) = self.raw_data.split_at(len);
|
||||
self.raw_data = rest;
|
||||
let (&filter, data) = if self.has_filter {
|
||||
data.split_first().unwrap()
|
||||
} else {
|
||||
(&0, data)
|
||||
};
|
||||
Some(ScanLine {
|
||||
filter,
|
||||
data,
|
||||
pass,
|
||||
num_pixels,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
/// An iterator over the scan line locations of a PNG image
|
||||
struct ScanLineRanges {
|
||||
/// Current pass number, and 0-indexed row within the pass
|
||||
pass: Option<(u8, u32)>,
|
||||
bits_per_pixel: usize,
|
||||
width: u32,
|
||||
height: u32,
|
||||
left: usize,
|
||||
has_filter: bool,
|
||||
}
|
||||
|
||||
impl ScanLineRanges {
|
||||
pub fn new(png: &PngImage, has_filter: bool) -> Self {
|
||||
Self {
|
||||
bits_per_pixel: png.ihdr.bpp(),
|
||||
width: png.ihdr.width,
|
||||
height: png.ihdr.height,
|
||||
left: png.data.len(),
|
||||
pass: if png.ihdr.interlaced {
|
||||
Some((1, 0))
|
||||
} else {
|
||||
None
|
||||
},
|
||||
has_filter,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator for ScanLineRanges {
|
||||
type Item = (usize, Option<u8>, usize);
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.left == 0 {
|
||||
return None;
|
||||
}
|
||||
let (pixels_per_line, current_pass) = if let Some(ref mut pass) = self.pass {
|
||||
// Scanlines for interlaced PNG files
|
||||
// Handle edge cases for images smaller than 5 pixels in either direction
|
||||
// No extra case needed for skipping pass 7 as this is already handled by the
|
||||
// self.left == 0 check above
|
||||
if self.width < 5 && pass.0 == 2 {
|
||||
pass.0 = 3;
|
||||
pass.1 = 4;
|
||||
}
|
||||
// Intentionally keep these separate so that they can be applied one after another
|
||||
if self.height < 5 && pass.0 == 3 {
|
||||
pass.0 = 4;
|
||||
pass.1 = 0;
|
||||
}
|
||||
if self.width < 3 && pass.0 == 4 {
|
||||
pass.0 = 5;
|
||||
pass.1 = 2;
|
||||
}
|
||||
if self.height < 3 && pass.0 == 5 {
|
||||
pass.0 = 6;
|
||||
pass.1 = 0;
|
||||
}
|
||||
if self.width == 1 && pass.0 == 6 {
|
||||
pass.0 = 7;
|
||||
pass.1 = 1;
|
||||
}
|
||||
let (pixels_factor, y_steps) = match pass.0 {
|
||||
1 | 2 => (8, 8),
|
||||
3 => (4, 8),
|
||||
4 => (4, 4),
|
||||
5 => (2, 4),
|
||||
6 => (2, 2),
|
||||
7 => (1, 2),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
let mut pixels_per_line = self.width / pixels_factor;
|
||||
// Determine whether to add pixels if there is a final, incomplete 8x8 block
|
||||
let gap = self.width % pixels_factor;
|
||||
match pass.0 {
|
||||
1 | 3 | 5 if gap > 0 => {
|
||||
pixels_per_line += 1;
|
||||
}
|
||||
2 if gap >= 5 => {
|
||||
pixels_per_line += 1;
|
||||
}
|
||||
4 if gap >= 3 => {
|
||||
pixels_per_line += 1;
|
||||
}
|
||||
6 if gap >= 2 => {
|
||||
pixels_per_line += 1;
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
let current_pass = Some(pass.0);
|
||||
if pass.1 + y_steps >= self.height {
|
||||
pass.0 += 1;
|
||||
pass.1 = match pass.0 {
|
||||
3 => 4,
|
||||
5 => 2,
|
||||
7 => 1,
|
||||
_ => 0,
|
||||
};
|
||||
} else {
|
||||
pass.1 += y_steps;
|
||||
}
|
||||
(pixels_per_line, current_pass)
|
||||
} else {
|
||||
// Standard, non-interlaced PNG scanlines
|
||||
(self.width, None)
|
||||
};
|
||||
let bits_per_line = pixels_per_line as usize * self.bits_per_pixel;
|
||||
let mut len = bits_per_line.div_ceil(8);
|
||||
if self.has_filter {
|
||||
len += 1;
|
||||
}
|
||||
self.left = self.left.checked_sub(len)?;
|
||||
Some((len, current_pass, pixels_per_line as usize))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
/// A scan line in a PNG image
|
||||
pub struct ScanLine<'a> {
|
||||
/// The filter type used to encode the current scan line (0-4)
|
||||
pub filter: u8,
|
||||
/// The byte data for the current scan line, encoded with the filter specified in the `filter` field
|
||||
pub data: &'a [u8],
|
||||
/// The current pass if the image is interlaced
|
||||
pub pass: Option<u8>,
|
||||
/// The number of pixels in the current scan line
|
||||
pub num_pixels: usize,
|
||||
}
|
||||
81
src/rayon.rs
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
#![allow(dead_code)]
|
||||
|
||||
pub mod prelude {
|
||||
pub use super::*;
|
||||
}
|
||||
|
||||
pub trait ParallelIterator: Iterator + Sized {
|
||||
fn with_max_len(self, _l: usize) -> Self {
|
||||
self
|
||||
}
|
||||
fn reduce_with<OP>(mut self, op: OP) -> Option<Self::Item>
|
||||
where
|
||||
OP: Fn(Self::Item, Self::Item) -> Self::Item + Sync,
|
||||
{
|
||||
self.next().map(|a| self.fold(a, op))
|
||||
}
|
||||
}
|
||||
|
||||
pub trait IntoParallelIterator {
|
||||
type Iter: Iterator<Item = Self::Item>;
|
||||
type Item: Send;
|
||||
fn into_par_iter(self) -> Self::Iter;
|
||||
}
|
||||
|
||||
pub trait IntoParallelRefIterator<'data> {
|
||||
type Iter: Iterator<Item = Self::Item>;
|
||||
type Item: Send + 'data;
|
||||
fn par_iter(&'data self) -> Self::Iter;
|
||||
}
|
||||
|
||||
pub trait IntoParallelRefMutIterator<'data> {
|
||||
type Iter: ParallelIterator<Item = Self::Item>;
|
||||
type Item: Send + 'data;
|
||||
fn par_iter_mut(&'data mut self) -> Self::Iter;
|
||||
}
|
||||
|
||||
impl<I: IntoIterator> IntoParallelIterator for I
|
||||
where
|
||||
I::Item: Send,
|
||||
{
|
||||
type Iter = I::IntoIter;
|
||||
type Item = I::Item;
|
||||
|
||||
fn into_par_iter(self) -> Self::Iter {
|
||||
self.into_iter()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'data, I: 'data + ?Sized> IntoParallelRefIterator<'data> for I
|
||||
where
|
||||
&'data I: IntoParallelIterator,
|
||||
{
|
||||
type Iter = <&'data I as IntoParallelIterator>::Iter;
|
||||
type Item = <&'data I as IntoParallelIterator>::Item;
|
||||
|
||||
fn par_iter(&'data self) -> Self::Iter {
|
||||
self.into_par_iter()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'data, I: 'data + ?Sized> IntoParallelRefMutIterator<'data> for I
|
||||
where
|
||||
&'data mut I: IntoParallelIterator,
|
||||
{
|
||||
type Iter = <&'data mut I as IntoParallelIterator>::Iter;
|
||||
type Item = <&'data mut I as IntoParallelIterator>::Item;
|
||||
|
||||
fn par_iter_mut(&'data mut self) -> Self::Iter {
|
||||
self.into_par_iter()
|
||||
}
|
||||
}
|
||||
|
||||
impl<I: Iterator> ParallelIterator for I {}
|
||||
|
||||
pub fn join<A, B>(a: impl FnOnce() -> A, b: impl FnOnce() -> B) -> (A, B) {
|
||||
(a(), b())
|
||||
}
|
||||
|
||||
pub fn spawn<A>(a: impl FnOnce() -> A) -> A {
|
||||
a()
|
||||
}
|
||||
295
src/reduction.rs
|
|
@ -1,295 +0,0 @@
|
|||
use bit_vec::BitVec;
|
||||
use colors::BitDepth;
|
||||
use itertools::Itertools;
|
||||
use png::PngData;
|
||||
|
||||
pub fn reduce_bit_depth_8_or_less(png: &PngData) -> Option<(Vec<u8>, u8)> {
|
||||
let mut reduced = BitVec::with_capacity(png.raw_data.len() * 8);
|
||||
let bit_depth: usize = png.ihdr_data.bit_depth.as_u8() as usize;
|
||||
let mut allowed_bits = 1;
|
||||
for line in png.scan_lines() {
|
||||
let bit_vec = BitVec::from_bytes(&line.data);
|
||||
for (i, bit) in bit_vec.iter().enumerate() {
|
||||
let bit_index = bit_depth - (i % bit_depth);
|
||||
if bit && bit_index > allowed_bits {
|
||||
allowed_bits = bit_index.next_power_of_two();
|
||||
if allowed_bits == bit_depth {
|
||||
// Not reducable
|
||||
return None;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for line in png.scan_lines() {
|
||||
reduced.extend(BitVec::from_bytes(&[line.filter]));
|
||||
let bit_vec = BitVec::from_bytes(&line.data);
|
||||
for (i, bit) in bit_vec.iter().enumerate() {
|
||||
let bit_index = bit_depth - (i % bit_depth);
|
||||
if bit_index <= allowed_bits {
|
||||
reduced.push(bit);
|
||||
}
|
||||
}
|
||||
// Pad end of line to get 8 bits per byte
|
||||
while reduced.len() % 8 != 0 {
|
||||
reduced.push(false);
|
||||
}
|
||||
}
|
||||
|
||||
Some((reduced.to_bytes(), allowed_bits as u8))
|
||||
}
|
||||
|
||||
pub fn reduce_rgba_to_rgb(png: &PngData) -> Option<Vec<u8>> {
|
||||
let mut reduced = Vec::with_capacity(png.raw_data.len());
|
||||
let byte_depth: u8 = png.ihdr_data.bit_depth.as_u8() >> 3;
|
||||
let bpp: usize = 4 * byte_depth as usize;
|
||||
let colored_bytes = bpp - byte_depth as usize;
|
||||
for line in png.scan_lines() {
|
||||
reduced.push(line.filter);
|
||||
for (i, byte) in line.data.iter().enumerate() {
|
||||
if i % bpp >= colored_bytes {
|
||||
if *byte != 255 {
|
||||
return None;
|
||||
}
|
||||
} else {
|
||||
reduced.push(*byte);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Some(reduced)
|
||||
}
|
||||
|
||||
pub fn reduce_rgba_to_grayscale_alpha(png: &PngData) -> Option<Vec<u8>> {
|
||||
let mut reduced = Vec::with_capacity(png.raw_data.len());
|
||||
let byte_depth: u8 = png.ihdr_data.bit_depth.as_u8() >> 3;
|
||||
let bpp: usize = 4 * byte_depth as usize;
|
||||
let colored_bytes = bpp - byte_depth as usize;
|
||||
for line in png.scan_lines() {
|
||||
reduced.push(line.filter);
|
||||
let mut low_bytes = Vec::with_capacity(4);
|
||||
let mut high_bytes = Vec::with_capacity(4);
|
||||
let mut trans_bytes = Vec::with_capacity(byte_depth as usize);
|
||||
for (i, byte) in line.data.iter().enumerate() {
|
||||
if i % bpp < colored_bytes {
|
||||
if byte_depth == 1 || i % 2 == 1 {
|
||||
low_bytes.push(*byte);
|
||||
} else {
|
||||
high_bytes.push(*byte);
|
||||
}
|
||||
} else {
|
||||
trans_bytes.push(*byte);
|
||||
}
|
||||
|
||||
if i % bpp == bpp - 1 {
|
||||
if low_bytes.iter().unique().count() > 1 {
|
||||
return None;
|
||||
}
|
||||
if byte_depth == 2 {
|
||||
if high_bytes.iter().unique().count() > 1 {
|
||||
return None;
|
||||
}
|
||||
reduced.push(high_bytes[0]);
|
||||
high_bytes.clear();
|
||||
}
|
||||
reduced.push(low_bytes[0]);
|
||||
low_bytes.clear();
|
||||
reduced.extend_from_slice(&trans_bytes);
|
||||
trans_bytes.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Some(reduced)
|
||||
}
|
||||
|
||||
pub fn reduce_rgba_to_palette(png: &PngData) -> Option<(Vec<u8>, Vec<u8>, Vec<u8>)> {
|
||||
if png.ihdr_data.bit_depth != BitDepth::Eight {
|
||||
return None;
|
||||
}
|
||||
let mut reduced = Vec::with_capacity(png.raw_data.len());
|
||||
let mut palette = Vec::with_capacity(256);
|
||||
let bpp: usize = (4 * png.ihdr_data.bit_depth.as_u8() as usize) >> 3;
|
||||
for line in png.scan_lines() {
|
||||
reduced.push(line.filter);
|
||||
let mut cur_pixel = Vec::with_capacity(bpp);
|
||||
for (i, byte) in line.data.iter().enumerate() {
|
||||
cur_pixel.push(*byte);
|
||||
if i % bpp == bpp - 1 {
|
||||
if let Some(idx) = palette.iter().position(|x| x == &cur_pixel) {
|
||||
reduced.push(idx as u8);
|
||||
} else {
|
||||
let len = palette.len();
|
||||
if len == 256 {
|
||||
return None;
|
||||
}
|
||||
palette.push(cur_pixel.clone());
|
||||
reduced.push(len as u8);
|
||||
}
|
||||
cur_pixel.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut color_palette = Vec::with_capacity(palette.len() * 3);
|
||||
let mut trans_palette = Vec::with_capacity(palette.len());
|
||||
for color in &palette {
|
||||
for (i, byte) in color.iter().enumerate() {
|
||||
if i < 3 {
|
||||
color_palette.push(*byte);
|
||||
} else {
|
||||
trans_palette.push(*byte);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Some((reduced, color_palette, trans_palette))
|
||||
}
|
||||
|
||||
pub fn reduce_rgb_to_palette(png: &PngData) -> Option<(Vec<u8>, Vec<u8>)> {
|
||||
if png.ihdr_data.bit_depth != BitDepth::Eight {
|
||||
return None;
|
||||
}
|
||||
let mut reduced = Vec::with_capacity(png.raw_data.len());
|
||||
let mut palette = Vec::with_capacity(256);
|
||||
let bpp: usize = (3 * png.ihdr_data.bit_depth.as_u8() as usize) >> 3;
|
||||
for line in png.scan_lines() {
|
||||
reduced.push(line.filter);
|
||||
let mut cur_pixel = Vec::with_capacity(bpp);
|
||||
for (i, byte) in line.data.iter().enumerate() {
|
||||
cur_pixel.push(*byte);
|
||||
if i % bpp == bpp - 1 {
|
||||
if let Some(idx) = palette.iter().position(|x| x == &cur_pixel) {
|
||||
reduced.push(idx as u8);
|
||||
} else {
|
||||
let len = palette.len();
|
||||
if len == 256 {
|
||||
return None;
|
||||
}
|
||||
palette.push(cur_pixel.clone());
|
||||
reduced.push(len as u8);
|
||||
}
|
||||
cur_pixel.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut color_palette = Vec::with_capacity(palette.len() * 3);
|
||||
for color in &palette {
|
||||
color_palette.extend_from_slice(color);
|
||||
}
|
||||
|
||||
Some((reduced, color_palette))
|
||||
}
|
||||
|
||||
pub fn reduce_grayscale_to_palette(png: &PngData) -> Option<(Vec<u8>, Vec<u8>)> {
|
||||
if png.ihdr_data.bit_depth == BitDepth::Sixteen {
|
||||
return None;
|
||||
}
|
||||
let mut reduced = BitVec::with_capacity(png.raw_data.len() * 8);
|
||||
// Only perform reduction if we can get to 4-bits or less
|
||||
let mut palette = Vec::with_capacity(16);
|
||||
let bpp: usize = png.ihdr_data.bit_depth.as_u8() as usize;
|
||||
let bpp_inverse = 8 - bpp;
|
||||
for line in png.scan_lines() {
|
||||
reduced.extend(BitVec::from_bytes(&[line.filter]));
|
||||
let bit_vec = BitVec::from_bytes(&line.data);
|
||||
let mut cur_pixel = BitVec::with_capacity(bpp);
|
||||
for (i, bit) in bit_vec.iter().enumerate() {
|
||||
cur_pixel.push(bit);
|
||||
if i % bpp == bpp - 1 {
|
||||
let pix_value = cur_pixel.to_bytes()[0] >> bpp_inverse;
|
||||
let pix_slice = vec![pix_value, pix_value, pix_value];
|
||||
if palette.contains(&pix_slice) {
|
||||
let index = palette.iter().enumerate().find(|&x| x.1 == &pix_slice).unwrap().0;
|
||||
let idx = BitVec::from_bytes(&[(index as u8) << bpp_inverse]);
|
||||
for b in idx.iter().take(bpp) {
|
||||
reduced.push(b);
|
||||
}
|
||||
} else {
|
||||
let len = palette.len();
|
||||
if len == 16 {
|
||||
return None;
|
||||
}
|
||||
palette.push(pix_slice.clone());
|
||||
let idx = BitVec::from_bytes(&[(len as u8) << bpp_inverse]);
|
||||
for b in idx.iter().take(bpp) {
|
||||
reduced.push(b);
|
||||
}
|
||||
}
|
||||
cur_pixel = BitVec::with_capacity(bpp);
|
||||
}
|
||||
}
|
||||
// Pad end of line to get 8 bits per byte
|
||||
while reduced.len() % 8 != 0 {
|
||||
reduced.push(false);
|
||||
}
|
||||
}
|
||||
|
||||
let mut color_palette = Vec::with_capacity(palette.len() * 3);
|
||||
for color in &palette {
|
||||
color_palette.extend_from_slice(color);
|
||||
}
|
||||
|
||||
Some((reduced.to_bytes(), color_palette))
|
||||
}
|
||||
|
||||
pub fn reduce_rgb_to_grayscale(png: &PngData) -> Option<Vec<u8>> {
|
||||
let mut reduced = Vec::with_capacity(png.raw_data.len());
|
||||
let byte_depth: u8 = png.ihdr_data.bit_depth.as_u8() >> 3;
|
||||
let bpp: usize = 3 * byte_depth as usize;
|
||||
let mut cur_pixel = Vec::with_capacity(bpp);
|
||||
for line in png.scan_lines() {
|
||||
reduced.push(line.filter);
|
||||
for (i, byte) in line.data.iter().enumerate() {
|
||||
cur_pixel.push(*byte);
|
||||
if i % bpp == bpp - 1 {
|
||||
if bpp == 3 {
|
||||
if cur_pixel.iter().unique().count() > 1 {
|
||||
return None;
|
||||
}
|
||||
reduced.push(cur_pixel[0]);
|
||||
} else {
|
||||
let pixel_bytes = cur_pixel.iter()
|
||||
.step(2)
|
||||
.cloned()
|
||||
.zip(cur_pixel.iter()
|
||||
.skip(1)
|
||||
.step(2)
|
||||
.cloned())
|
||||
.unique()
|
||||
.collect::<Vec<(u8, u8)>>();
|
||||
if pixel_bytes.len() > 1 {
|
||||
return None;
|
||||
}
|
||||
reduced.push(pixel_bytes[0].0);
|
||||
reduced.push(pixel_bytes[0].1);
|
||||
}
|
||||
cur_pixel.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Some(reduced)
|
||||
}
|
||||
|
||||
pub fn reduce_grayscale_alpha_to_grayscale(png: &PngData) -> Option<Vec<u8>> {
|
||||
let mut reduced = Vec::with_capacity(png.raw_data.len());
|
||||
let byte_depth: u8 = png.ihdr_data.bit_depth.as_u8() >> 3;
|
||||
let bpp: usize = 2 * byte_depth as usize;
|
||||
let colored_bytes = bpp - byte_depth as usize;
|
||||
for line in png.scan_lines() {
|
||||
reduced.push(line.filter);
|
||||
for (i, byte) in line.data.iter().enumerate() {
|
||||
if i % bpp >= colored_bytes {
|
||||
if *byte != 255 {
|
||||
return None;
|
||||
}
|
||||
} else {
|
||||
reduced.push(*byte);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Some(reduced)
|
||||
}
|
||||
108
src/reduction/alpha.rs
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
use rgb::RGB16;
|
||||
|
||||
use crate::{
|
||||
colors::{BitDepth, ColorType},
|
||||
headers::IhdrData,
|
||||
png::PngImage,
|
||||
};
|
||||
|
||||
/// Clean the alpha channel by setting the color of all fully transparent pixels to black
|
||||
#[must_use]
|
||||
pub fn cleaned_alpha_channel(png: &PngImage) -> Option<PngImage> {
|
||||
if !png.ihdr.color_type.has_alpha() {
|
||||
return None;
|
||||
}
|
||||
let byte_depth = png.bytes_per_channel();
|
||||
let bpp = png.channels_per_pixel() * byte_depth;
|
||||
let colored_bytes = bpp - byte_depth;
|
||||
|
||||
let mut reduced = Vec::with_capacity(png.data.len());
|
||||
for pixel in png.data.chunks_exact(bpp) {
|
||||
if pixel.iter().skip(colored_bytes).all(|b| *b == 0) {
|
||||
reduced.resize(reduced.len() + bpp, 0);
|
||||
} else {
|
||||
reduced.extend_from_slice(pixel);
|
||||
}
|
||||
}
|
||||
|
||||
Some(PngImage {
|
||||
data: reduced,
|
||||
ihdr: png.ihdr.clone(),
|
||||
})
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn reduced_alpha_channel(png: &PngImage, optimize_alpha: bool) -> Option<PngImage> {
|
||||
if !png.ihdr.color_type.has_alpha() {
|
||||
return None;
|
||||
}
|
||||
let byte_depth = png.bytes_per_channel();
|
||||
let bpp = png.channels_per_pixel() * byte_depth;
|
||||
let colored_bytes = bpp - byte_depth;
|
||||
|
||||
// If alpha optimisation is enabled, see if the image contains only fully opaque and fully transparent pixels.
|
||||
// In case this occurs, we want to try and find an unused color we can use for the tRNS chunk.
|
||||
// Rather than an exhaustive search, we will just keep track of 256 shades of gray, which should cover many cases.
|
||||
let mut has_transparency = false;
|
||||
let mut used_colors = vec![false; 256];
|
||||
|
||||
for pixel in png.data.chunks_exact(bpp) {
|
||||
if optimize_alpha && pixel.iter().skip(colored_bytes).all(|b| *b == 0) {
|
||||
// Fully transparent, we may be able to reduce with tRNS
|
||||
has_transparency = true;
|
||||
} else if pixel.iter().skip(colored_bytes).any(|b| *b != 255) {
|
||||
// Partially transparent, the image is not reducible
|
||||
return None;
|
||||
} else if optimize_alpha && pixel.iter().take(colored_bytes).all(|b| *b == pixel[0]) {
|
||||
// Opaque shade of gray, we can't use this color for tRNS
|
||||
used_colors[pixel[0] as usize] = true;
|
||||
}
|
||||
}
|
||||
|
||||
let transparency_pixel = if has_transparency {
|
||||
// For grayscale, start by checking 4 specific values in the hope that we may reduce depth
|
||||
let unused = match png.ihdr.color_type {
|
||||
ColorType::GrayscaleAlpha => [0x00, 0xFF, 0x55, 0xAA]
|
||||
.into_iter()
|
||||
.find(|&v| !used_colors[v as usize]),
|
||||
_ => None,
|
||||
}
|
||||
.or_else(|| used_colors.iter().position(|&u| !u).map(|v| v as u8));
|
||||
// If no unused color was found we will have to fail here
|
||||
Some(unused?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let mut raw_data = Vec::with_capacity(png.data.len());
|
||||
for pixel in png.data.chunks_exact(bpp) {
|
||||
match transparency_pixel {
|
||||
Some(trns) if pixel.iter().skip(colored_bytes).all(|b| *b == 0) => {
|
||||
raw_data.resize(raw_data.len() + colored_bytes, trns);
|
||||
}
|
||||
_ => raw_data.extend_from_slice(&pixel[0..colored_bytes]),
|
||||
}
|
||||
}
|
||||
|
||||
// Construct the color type with appropriate transparency data
|
||||
let transparent = transparency_pixel.map(|trns| match png.ihdr.bit_depth {
|
||||
BitDepth::Sixteen => (u16::from(trns) << 8) | u16::from(trns),
|
||||
_ => u16::from(trns),
|
||||
});
|
||||
let target_color_type = match png.ihdr.color_type {
|
||||
ColorType::GrayscaleAlpha => ColorType::Grayscale {
|
||||
transparent_shade: transparent,
|
||||
},
|
||||
_ => ColorType::RGB {
|
||||
transparent_color: transparent.map(|t| RGB16::new(t, t, t)),
|
||||
},
|
||||
};
|
||||
|
||||
Some(PngImage {
|
||||
data: raw_data,
|
||||
ihdr: IhdrData {
|
||||
color_type: target_color_type,
|
||||
..png.ihdr
|
||||
},
|
||||
})
|
||||
}
|
||||
230
src/reduction/bit_depth.rs
Normal file
|
|
@ -0,0 +1,230 @@
|
|||
use crate::{
|
||||
colors::{BitDepth, ColorType},
|
||||
headers::IhdrData,
|
||||
png::PngImage,
|
||||
};
|
||||
|
||||
/// Attempt to reduce a 16-bit image to 8-bit, returning the reduced image if successful
|
||||
#[must_use]
|
||||
pub fn reduced_bit_depth_16_to_8(png: &PngImage, force_scale: bool) -> Option<PngImage> {
|
||||
if png.ihdr.bit_depth != BitDepth::Sixteen {
|
||||
return None;
|
||||
}
|
||||
|
||||
if force_scale {
|
||||
return scaled_bit_depth_16_to_8(png);
|
||||
}
|
||||
|
||||
// Reduce from 16 to 8 bits per channel per pixel
|
||||
if png.data.chunks_exact(2).any(|pair| pair[0] != pair[1]) {
|
||||
// Can't reduce
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(PngImage {
|
||||
data: png.data.chunks_exact(2).map(|pair| pair[0]).collect(),
|
||||
ihdr: IhdrData {
|
||||
color_type: png.ihdr.color_type.clone(),
|
||||
bit_depth: BitDepth::Eight,
|
||||
..png.ihdr
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
/// Forcibly reduce a 16-bit image to 8-bit by scaling, returning the reduced image if successful
|
||||
#[must_use]
|
||||
pub fn scaled_bit_depth_16_to_8(png: &PngImage) -> Option<PngImage> {
|
||||
if png.ihdr.bit_depth != BitDepth::Sixteen {
|
||||
return None;
|
||||
}
|
||||
|
||||
// Reduce from 16 to 8 bits per channel per pixel by scaling when necessary
|
||||
let data = png
|
||||
.data
|
||||
.chunks_exact(2)
|
||||
.map(|pair| {
|
||||
if pair[0] == pair[1] {
|
||||
return pair[0];
|
||||
}
|
||||
// See: http://www.libpng.org/pub/png/spec/1.2/PNG-Decoders.html#D.Sample-depth-rescaling
|
||||
// This allows values such as 0x00FF to be rounded to 0x01 rather than truncated to 0x00
|
||||
let val = f32::from(u16::from_be_bytes([pair[0], pair[1]]));
|
||||
(val * (255.0 / 65535.0)).round() as u8
|
||||
})
|
||||
.collect();
|
||||
|
||||
Some(PngImage {
|
||||
data,
|
||||
ihdr: IhdrData {
|
||||
color_type: png.ihdr.color_type.clone(),
|
||||
bit_depth: BitDepth::Eight,
|
||||
..png.ihdr
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
/// Attempt to reduce an 8-bit image to a lower bit depth, returning the reduced image if successful
|
||||
#[must_use]
|
||||
pub fn reduced_bit_depth_8_or_less(png: &PngImage) -> Option<PngImage> {
|
||||
if png.ihdr.bit_depth != BitDepth::Eight || png.channels_per_pixel() != 1 {
|
||||
return None;
|
||||
}
|
||||
|
||||
let minimum_bits = if let ColorType::Indexed { palette } = &png.ihdr.color_type {
|
||||
// We can easily determine minimum depth by the palette size
|
||||
match palette.len() {
|
||||
0..=2 => 1,
|
||||
3..=4 => 2,
|
||||
5..=16 => 4,
|
||||
_ => return None,
|
||||
}
|
||||
} else {
|
||||
let mut minimum_bits = 1;
|
||||
|
||||
// Finding minimum depth for grayscale is much more complicated
|
||||
let mut mask = 1;
|
||||
let mut divisions = 1..8;
|
||||
for &b in &png.data {
|
||||
if b == 0 || b == 255 {
|
||||
continue;
|
||||
}
|
||||
'try_depth: loop {
|
||||
// Align the first pixel division with the mask
|
||||
let mut byte = b.rotate_left(minimum_bits as u32);
|
||||
// Each potential division of this pixel must be identical to successfully reduce
|
||||
let compare = byte & mask;
|
||||
for _ in divisions.clone() {
|
||||
// Align the next division with the mask
|
||||
byte = byte.rotate_left(minimum_bits as u32);
|
||||
if byte & mask != compare {
|
||||
// This depth is not possible, try the next one up
|
||||
minimum_bits <<= 1;
|
||||
if minimum_bits == 8 {
|
||||
return None;
|
||||
}
|
||||
mask = (1 << minimum_bits) - 1;
|
||||
divisions = 1..(8 / minimum_bits);
|
||||
continue 'try_depth;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
minimum_bits
|
||||
};
|
||||
|
||||
let mut reduced = Vec::with_capacity(png.data.len());
|
||||
let mask = (1 << minimum_bits) - 1;
|
||||
for line in png.scan_lines(false) {
|
||||
// Loop over the data in chunks that will produce 1 byte of output
|
||||
for chunk in line.data.chunks(8 / minimum_bits) {
|
||||
let mut new_byte = 0;
|
||||
let mut shift = 8;
|
||||
for byte in chunk {
|
||||
shift -= minimum_bits;
|
||||
// Take the low bits of the pixel and shift them into the output byte
|
||||
new_byte |= (byte & mask) << shift;
|
||||
}
|
||||
reduced.push(new_byte);
|
||||
}
|
||||
}
|
||||
|
||||
// If the image is grayscale we also need to reduce the transparency pixel
|
||||
let color_type = if let ColorType::Grayscale {
|
||||
transparent_shade: Some(trans),
|
||||
} = png.ihdr.color_type
|
||||
{
|
||||
let reduced_trans = (trans & 0xFF) >> (8 - minimum_bits);
|
||||
// Verify the reduction is valid by restoring back to original bit depth
|
||||
let mut check = reduced_trans;
|
||||
let mut bits = minimum_bits;
|
||||
while bits < 8 {
|
||||
check = (check << bits) | check;
|
||||
bits <<= 1;
|
||||
}
|
||||
// If the transparency doesn't fit the new bit depth it is therefore unused - set it to None
|
||||
ColorType::Grayscale {
|
||||
transparent_shade: if trans == check {
|
||||
Some(reduced_trans)
|
||||
} else {
|
||||
None
|
||||
},
|
||||
}
|
||||
} else {
|
||||
png.ihdr.color_type.clone()
|
||||
};
|
||||
|
||||
Some(PngImage {
|
||||
data: reduced,
|
||||
ihdr: IhdrData {
|
||||
color_type,
|
||||
bit_depth: (minimum_bits as u8).try_into().unwrap(),
|
||||
..png.ihdr
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
/// Expand a 1/2/4-bit image to 8-bit, returning the expanded image if successful
|
||||
#[must_use]
|
||||
pub fn expanded_bit_depth_to_8(png: &PngImage) -> Option<PngImage> {
|
||||
let bit_depth = png.ihdr.bit_depth as u32;
|
||||
if bit_depth >= 8 {
|
||||
return None;
|
||||
}
|
||||
// Calculate the current number of pixels per byte
|
||||
let ppb = 8 / bit_depth;
|
||||
let is_gray = matches!(png.ihdr.color_type, ColorType::Grayscale { .. });
|
||||
|
||||
let mut reduced = Vec::with_capacity((png.ihdr.width * png.ihdr.height) as usize);
|
||||
let mut length = 0;
|
||||
let mask = (1 << bit_depth) - 1;
|
||||
for line in png.scan_lines(false) {
|
||||
for &(mut byte) in line.data {
|
||||
// Loop over each pixel in the byte
|
||||
for _ in 0..ppb {
|
||||
// Align the current pixel with the mask
|
||||
byte = byte.rotate_left(bit_depth);
|
||||
let mut val = byte & mask;
|
||||
if is_gray {
|
||||
// Expand gray by repeating the bits
|
||||
let mut bits = bit_depth;
|
||||
while bits < 8 {
|
||||
val = (val << bits) | val;
|
||||
bits <<= 1;
|
||||
}
|
||||
}
|
||||
reduced.push(val);
|
||||
}
|
||||
}
|
||||
// Trim any overflow
|
||||
length += line.num_pixels;
|
||||
reduced.truncate(length);
|
||||
}
|
||||
|
||||
// If the image is grayscale we also need to expand the transparency pixel
|
||||
let color_type = if let ColorType::Grayscale {
|
||||
transparent_shade: Some(mut trans),
|
||||
} = png.ihdr.color_type
|
||||
{
|
||||
let mut bits = bit_depth;
|
||||
while bits < 8 {
|
||||
trans = (trans << bits) | trans;
|
||||
bits <<= 1;
|
||||
}
|
||||
ColorType::Grayscale {
|
||||
transparent_shade: Some(trans),
|
||||
}
|
||||
} else {
|
||||
png.ihdr.color_type.clone()
|
||||
};
|
||||
|
||||
Some(PngImage {
|
||||
data: reduced,
|
||||
ihdr: IhdrData {
|
||||
color_type,
|
||||
bit_depth: BitDepth::Eight,
|
||||
..png.ihdr
|
||||
},
|
||||
})
|
||||
}
|
||||
206
src/reduction/color.rs
Normal file
|
|
@ -0,0 +1,206 @@
|
|||
use std::hash::{BuildHasherDefault, Hash};
|
||||
|
||||
use indexmap::IndexSet;
|
||||
use rgb::{ComponentMap, FromSlice, RGB, RGBA, alt::Gray};
|
||||
use rustc_hash::FxHasher;
|
||||
|
||||
use crate::{
|
||||
colors::{BitDepth, ColorType},
|
||||
headers::IhdrData,
|
||||
png::PngImage,
|
||||
};
|
||||
|
||||
type FxIndexSet<V> = IndexSet<V, BuildHasherDefault<FxHasher>>;
|
||||
|
||||
/// Maximum size difference between indexed and channels to consider a candidate for evaluation
|
||||
pub const INDEXED_MAX_DIFF: usize = 20000;
|
||||
|
||||
fn build_palette<T>(
|
||||
iter: impl IntoIterator<Item = T>,
|
||||
reduced: &mut Vec<u8>,
|
||||
) -> Option<FxIndexSet<T>>
|
||||
where
|
||||
T: Eq + Hash,
|
||||
{
|
||||
let mut palette = FxIndexSet::default();
|
||||
palette.reserve(257);
|
||||
for pixel in iter {
|
||||
let (idx, _) = palette.insert_full(pixel);
|
||||
if idx == 256 {
|
||||
return None;
|
||||
}
|
||||
reduced.push(idx as u8);
|
||||
}
|
||||
Some(palette)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn reduced_to_indexed(png: &PngImage, allow_grayscale: bool) -> Option<PngImage> {
|
||||
if png.ihdr.bit_depth != BitDepth::Eight {
|
||||
return None;
|
||||
}
|
||||
if matches!(png.ihdr.color_type, ColorType::Indexed { .. }) {
|
||||
return None;
|
||||
}
|
||||
if !allow_grayscale && png.ihdr.color_type.is_gray() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut raw_data = Vec::with_capacity(png.data.len() / png.channels_per_pixel());
|
||||
let palette: Vec<_> = match png.ihdr.color_type {
|
||||
ColorType::Grayscale { transparent_shade } => {
|
||||
let pmap = build_palette(png.data.as_gray().iter().copied(), &mut raw_data)?;
|
||||
// Convert the Gray16 transparency to Gray8
|
||||
let transparency_pixel = transparent_shade.map(|t| Gray::from(t as u8));
|
||||
pmap.into_iter()
|
||||
.map(|px| {
|
||||
RGB::from(px).with_alpha(if Some(px) == transparency_pixel {
|
||||
0
|
||||
} else {
|
||||
255
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
ColorType::RGB { transparent_color } => {
|
||||
let pmap = build_palette(png.data.as_rgb().iter().copied(), &mut raw_data)?;
|
||||
// Convert the RGB16 transparency to RGB8
|
||||
let transparency_pixel = transparent_color.map(|t| t.map(|c| c as u8));
|
||||
pmap.into_iter()
|
||||
.map(|px| {
|
||||
px.with_alpha(if Some(px) == transparency_pixel {
|
||||
0
|
||||
} else {
|
||||
255
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
ColorType::GrayscaleAlpha => {
|
||||
let pmap = build_palette(png.data.as_gray_alpha().iter().copied(), &mut raw_data)?;
|
||||
pmap.into_iter().map(RGBA::from).collect()
|
||||
}
|
||||
ColorType::RGBA => {
|
||||
let pmap = build_palette(png.data.as_rgba().iter().copied(), &mut raw_data)?;
|
||||
pmap.into_iter().collect()
|
||||
}
|
||||
_ => return None,
|
||||
};
|
||||
|
||||
Some(PngImage {
|
||||
data: raw_data,
|
||||
ihdr: IhdrData {
|
||||
color_type: ColorType::Indexed { palette },
|
||||
..png.ihdr
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn reduced_rgb_to_grayscale(png: &PngImage) -> Option<PngImage> {
|
||||
if !png.ihdr.color_type.is_rgb() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut reduced = Vec::with_capacity(png.data.len());
|
||||
let byte_depth = png.bytes_per_channel();
|
||||
let bpp = png.channels_per_pixel() * byte_depth;
|
||||
let last_color = 2 * byte_depth;
|
||||
for pixel in png.data.chunks_exact(bpp) {
|
||||
if byte_depth == 1 {
|
||||
if pixel[0] != pixel[1] || pixel[1] != pixel[2] {
|
||||
return None;
|
||||
}
|
||||
} else if pixel[0..2] != pixel[2..4] || pixel[2..4] != pixel[4..6] {
|
||||
return None;
|
||||
}
|
||||
reduced.extend_from_slice(&pixel[last_color..]);
|
||||
}
|
||||
|
||||
let color_type = match png.ihdr.color_type {
|
||||
ColorType::RGB { transparent_color } => ColorType::Grayscale {
|
||||
// Copy the transparent component if it is also gray
|
||||
transparent_shade: transparent_color
|
||||
.filter(|t| t.r == t.g && t.g == t.b)
|
||||
.map(|t| t.r),
|
||||
},
|
||||
_ => ColorType::GrayscaleAlpha,
|
||||
};
|
||||
|
||||
Some(PngImage {
|
||||
data: reduced,
|
||||
ihdr: IhdrData {
|
||||
color_type,
|
||||
..png.ihdr
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
/// Attempt to convert indexed to a different color type, returning the resulting image if successful
|
||||
#[must_use]
|
||||
pub fn indexed_to_channels(
|
||||
png: &PngImage,
|
||||
allow_grayscale: bool,
|
||||
optimize_alpha: bool,
|
||||
) -> Option<PngImage> {
|
||||
if png.ihdr.bit_depth != BitDepth::Eight {
|
||||
return None;
|
||||
}
|
||||
let mut palette = match &png.ihdr.color_type {
|
||||
ColorType::Indexed { palette } => palette.clone(),
|
||||
_ => return None,
|
||||
};
|
||||
|
||||
// Ensure fully transparent colors are black, which can help with grayscale conversion
|
||||
if optimize_alpha {
|
||||
for color in &mut palette {
|
||||
if color.a == 0 {
|
||||
color.r = 0;
|
||||
color.g = 0;
|
||||
color.b = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Determine which channels are required
|
||||
let is_gray = if allow_grayscale {
|
||||
palette.iter().all(|c| c.r == c.g && c.g == c.b)
|
||||
} else {
|
||||
false
|
||||
};
|
||||
let has_alpha = palette.iter().any(|c| c.a != 255);
|
||||
let color_type = match (is_gray, has_alpha) {
|
||||
(false, true) => ColorType::RGBA,
|
||||
(false, false) => ColorType::RGB {
|
||||
transparent_color: None,
|
||||
},
|
||||
(true, true) => ColorType::GrayscaleAlpha,
|
||||
(true, false) => ColorType::Grayscale {
|
||||
transparent_shade: None,
|
||||
},
|
||||
};
|
||||
|
||||
// Don't proceed if output would be too much larger
|
||||
let out_size = color_type.channels_per_pixel() as usize * png.data.len();
|
||||
if out_size - png.data.len() > INDEXED_MAX_DIFF {
|
||||
return None;
|
||||
}
|
||||
|
||||
// Construct the new data
|
||||
let black = RGBA::new(0, 0, 0, 255);
|
||||
let ch_start = if is_gray { 2 } else { 0 };
|
||||
let ch_end = if has_alpha { 3 } else { 2 };
|
||||
let mut data = Vec::with_capacity(out_size);
|
||||
for b in &png.data {
|
||||
let color = palette.get(*b as usize).unwrap_or(&black);
|
||||
data.extend_from_slice(&color.as_ref()[ch_start..=ch_end]);
|
||||
}
|
||||
|
||||
Some(PngImage {
|
||||
ihdr: IhdrData {
|
||||
color_type,
|
||||
..png.ihdr
|
||||
},
|
||||
data,
|
||||
})
|
||||
}
|
||||
203
src/reduction/mod.rs
Normal file
|
|
@ -0,0 +1,203 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use crate::{ColorType, Deadline, Deflater, Options, evaluate::Evaluator, png::PngImage};
|
||||
|
||||
pub mod alpha;
|
||||
use crate::alpha::*;
|
||||
pub mod bit_depth;
|
||||
use crate::bit_depth::*;
|
||||
pub mod color;
|
||||
use crate::color::*;
|
||||
pub mod palette;
|
||||
use crate::palette::*;
|
||||
|
||||
pub(crate) fn perform_reductions(
|
||||
mut png: Arc<PngImage>,
|
||||
opts: &Options,
|
||||
deadline: &Deadline,
|
||||
eval: &Evaluator,
|
||||
) -> Arc<PngImage> {
|
||||
let mut evaluation_added = false;
|
||||
|
||||
// At low compression levels, skip some transformations which are less likely to be effective
|
||||
// This currently affects optimization presets 0-2
|
||||
let cheap = match opts.deflater {
|
||||
Deflater::Libdeflater { compression } => compression < 12 && opts.fast_evaluation,
|
||||
_ => false,
|
||||
};
|
||||
|
||||
// Interlacing must be processed first in order to evaluate the rest correctly
|
||||
if let Some(interlacing) = opts.interlace {
|
||||
if let Some(reduced) = png.change_interlacing(interlacing) {
|
||||
png = Arc::new(reduced);
|
||||
}
|
||||
}
|
||||
|
||||
// If alpha optimization is enabled, clean the alpha channel before continuing
|
||||
// This can allow some color type reductions which may not have been possible otherwise
|
||||
if opts.optimize_alpha && !deadline.passed() {
|
||||
if let Some(reduced) = cleaned_alpha_channel(&png) {
|
||||
png = Arc::new(reduced);
|
||||
}
|
||||
}
|
||||
|
||||
// Attempt to reduce 16-bit to 8-bit
|
||||
// This is just removal of bytes and does not need to be evaluated
|
||||
if opts.bit_depth_reduction && !deadline.passed() {
|
||||
if let Some(reduced) = reduced_bit_depth_16_to_8(&png, opts.scale_16) {
|
||||
png = Arc::new(reduced);
|
||||
}
|
||||
}
|
||||
|
||||
// Attempt to reduce RGB to grayscale
|
||||
// This is just removal of bytes and does not need to be evaluated
|
||||
if opts.color_type_reduction && opts.grayscale_reduction && !deadline.passed() {
|
||||
if let Some(reduced) = reduced_rgb_to_grayscale(&png) {
|
||||
png = Arc::new(reduced);
|
||||
}
|
||||
}
|
||||
|
||||
// Attempt to expand the bit depth to 8
|
||||
// This does need to be evaluated but will be done so later when it gets reduced again
|
||||
if opts.bit_depth_reduction && !deadline.passed() {
|
||||
if let Some(reduced) = expanded_bit_depth_to_8(&png) {
|
||||
png = Arc::new(reduced);
|
||||
}
|
||||
}
|
||||
|
||||
// Now retain the current png for the evaluator baseline
|
||||
// It will only be entered into the evaluator if there are also others to evaluate
|
||||
let mut baseline = png.clone();
|
||||
|
||||
// Attempt to reduce and sort the palette
|
||||
if opts.palette_reduction && !deadline.passed() {
|
||||
if let Some(reduced) = reduced_palette(&png, opts.optimize_alpha) {
|
||||
png = Arc::new(reduced);
|
||||
// If the palette was reduced but the data is unchanged then this should become the baseline
|
||||
if png.data == baseline.data {
|
||||
baseline = png.clone();
|
||||
}
|
||||
}
|
||||
if let Some(reduced) = sorted_palette(&png) {
|
||||
png = Arc::new(reduced);
|
||||
}
|
||||
// If either action changed the data then enter this into the evaluator
|
||||
if !Arc::ptr_eq(&png, &baseline) {
|
||||
eval.try_image_with_description(png.clone(), "Indexed (luma sort)");
|
||||
evaluation_added = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Attempt alpha removal
|
||||
if opts.color_type_reduction && !deadline.passed() {
|
||||
if let Some(reduced) = reduced_alpha_channel(&png, opts.optimize_alpha) {
|
||||
png = Arc::new(reduced);
|
||||
// For small differences, if a tRNS chunk is required then enter this into the evaluator
|
||||
// Otherwise it is mostly just removal of bytes and should become the baseline
|
||||
if png.ihdr.color_type.has_trns() && baseline.data.len() - png.data.len() <= 1000 {
|
||||
eval.try_image(png.clone());
|
||||
evaluation_added = true;
|
||||
} else {
|
||||
baseline = png.clone();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Attempt to convert from indexed to channels
|
||||
// This may give a better result due to dropping the PLTE chunk
|
||||
if !cheap && opts.color_type_reduction && !deadline.passed() {
|
||||
if let Some(reduced) =
|
||||
indexed_to_channels(&png, opts.grayscale_reduction, opts.optimize_alpha)
|
||||
{
|
||||
// This result should not be passed on to subsequent reductions
|
||||
eval.try_image(Arc::new(reduced));
|
||||
evaluation_added = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Attempt to reduce to indexed
|
||||
// Keep the existing `png` var in case it is grayscale - we can test both for depth reduction later
|
||||
let mut indexed = None;
|
||||
if opts.color_type_reduction && opts.palette_reduction && !deadline.passed() {
|
||||
if let Some(reduced) = reduced_to_indexed(&png, opts.grayscale_reduction) {
|
||||
// Make sure the palette gets sorted (but don't bother evaluating both results)
|
||||
let new = Arc::new(sorted_palette(&reduced).unwrap_or(reduced));
|
||||
// For relatively small differences, enter this into the evaluator
|
||||
// Otherwise we're confident enough for it to become the baseline
|
||||
if png.data.len() - new.data.len() <= INDEXED_MAX_DIFF {
|
||||
eval.try_image_with_description(new.clone(), "Indexed (luma sort)");
|
||||
evaluation_added = true;
|
||||
} else {
|
||||
baseline = new.clone();
|
||||
}
|
||||
indexed = Some(new);
|
||||
}
|
||||
}
|
||||
|
||||
// Attempt additional palette sorting techniques
|
||||
if !cheap && opts.palette_reduction {
|
||||
// Collect a list of palettes so we can avoid evaluating the same one twice
|
||||
let mut palettes = Vec::new();
|
||||
if let ColorType::Indexed { palette } = &baseline.ihdr.color_type {
|
||||
palettes.push(palette.clone());
|
||||
}
|
||||
// Make sure we use the `indexed` var as input if it exists
|
||||
// This one doesn't need to be kept in the palette list as the sorters will fail if there's no change
|
||||
let input = indexed.as_ref().unwrap_or(&png);
|
||||
|
||||
// Attempt to sort the palette using the battiato method
|
||||
if !deadline.passed() {
|
||||
if let Some(reduced) = sorted_palette_battiato(input) {
|
||||
if let ColorType::Indexed { palette } = &reduced.ihdr.color_type {
|
||||
if !palettes.contains(palette) {
|
||||
palettes.push(palette.clone());
|
||||
eval.try_image_with_description(
|
||||
Arc::new(reduced),
|
||||
"Indexed (battiato sort)",
|
||||
);
|
||||
evaluation_added = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Attempt to sort the palette using the mzeng method
|
||||
if !deadline.passed() {
|
||||
if let Some(reduced) = sorted_palette_mzeng(input) {
|
||||
if let ColorType::Indexed { palette } = &reduced.ihdr.color_type {
|
||||
if !palettes.contains(palette) {
|
||||
palettes.push(palette.clone());
|
||||
eval.try_image_with_description(Arc::new(reduced), "Indexed (mzeng sort)");
|
||||
evaluation_added = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Attempt to reduce to a lower bit depth
|
||||
if opts.bit_depth_reduction && !deadline.passed() {
|
||||
// First try the `png` var
|
||||
let reduced = reduced_bit_depth_8_or_less(&png);
|
||||
// Then try the `indexed` var, unless we're doing cheap evaluations and already have a reduction
|
||||
if (!cheap || reduced.is_none()) && !deadline.passed() {
|
||||
if let Some(indexed) = indexed.and_then(|png| reduced_bit_depth_8_or_less(&png)) {
|
||||
// Only evaluate this if it's different from the first result (which must be grayscale if it exists)
|
||||
if reduced.as_ref().is_none_or(|r| r.data != indexed.data) {
|
||||
eval.try_image(Arc::new(indexed));
|
||||
evaluation_added = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Enter the first result into the evaluator
|
||||
if let Some(reduced) = reduced {
|
||||
eval.try_image(Arc::new(reduced));
|
||||
evaluation_added = true;
|
||||
}
|
||||
}
|
||||
|
||||
if evaluation_added {
|
||||
eval.try_image(baseline.clone());
|
||||
}
|
||||
baseline
|
||||
}
|
||||
439
src/reduction/palette.rs
Normal file
|
|
@ -0,0 +1,439 @@
|
|||
use indexmap::IndexSet;
|
||||
use rgb::RGBA8;
|
||||
|
||||
use crate::{
|
||||
colors::{BitDepth, ColorType},
|
||||
headers::IhdrData,
|
||||
png::{PngImage, scan_lines::ScanLine},
|
||||
};
|
||||
|
||||
/// Attempt to reduce the number of colors in the palette, returning the reduced image if successful
|
||||
#[must_use]
|
||||
pub fn reduced_palette(png: &PngImage, optimize_alpha: bool) -> Option<PngImage> {
|
||||
if png.ihdr.bit_depth != BitDepth::Eight {
|
||||
return None;
|
||||
}
|
||||
let ColorType::Indexed { palette } = &png.ihdr.color_type else {
|
||||
return None;
|
||||
};
|
||||
|
||||
let mut used = [false; 256];
|
||||
for &byte in &png.data {
|
||||
used[byte as usize] = true;
|
||||
}
|
||||
|
||||
let black = RGBA8::new(0, 0, 0, 255);
|
||||
let mut condensed = IndexSet::with_capacity(palette.len());
|
||||
let mut byte_map = [0; 256];
|
||||
let mut did_change = false;
|
||||
for (i, used) in used.iter().enumerate() {
|
||||
if !used {
|
||||
continue;
|
||||
}
|
||||
// There are invalid files that use pixel indices beyond palette size
|
||||
let color = *palette.get(i).unwrap_or(&black);
|
||||
byte_map[i] = add_color_to_set(color, &mut condensed, optimize_alpha);
|
||||
if byte_map[i] as usize != i {
|
||||
did_change = true;
|
||||
}
|
||||
}
|
||||
|
||||
let data = if did_change {
|
||||
// Reassign data bytes to new indices
|
||||
png.data.iter().map(|b| byte_map[*b as usize]).collect()
|
||||
} else if condensed.len() != palette.len() {
|
||||
// Data is unchanged but palette is different size
|
||||
// Note the new palette could potentially be larger if the original had a missing entry
|
||||
png.data.clone()
|
||||
} else {
|
||||
// Nothing has changed
|
||||
return None;
|
||||
};
|
||||
|
||||
let palette: Vec<_> = condensed.into_iter().collect();
|
||||
|
||||
Some(PngImage {
|
||||
ihdr: IhdrData {
|
||||
color_type: ColorType::Indexed { palette },
|
||||
..png.ihdr
|
||||
},
|
||||
data,
|
||||
})
|
||||
}
|
||||
|
||||
fn add_color_to_set(mut color: RGBA8, set: &mut IndexSet<RGBA8>, optimize_alpha: bool) -> u8 {
|
||||
// If there are multiple fully transparent entries, reduce them into one
|
||||
if optimize_alpha && color.a == 0 {
|
||||
color.r = 0;
|
||||
color.g = 0;
|
||||
color.b = 0;
|
||||
}
|
||||
let (idx, _) = set.insert_full(color);
|
||||
idx as u8
|
||||
}
|
||||
|
||||
/// Attempt to sort the colors in the palette by luma, returning the sorted image if successful
|
||||
#[must_use]
|
||||
pub fn sorted_palette(png: &PngImage) -> Option<PngImage> {
|
||||
if png.ihdr.bit_depth != BitDepth::Eight {
|
||||
return None;
|
||||
}
|
||||
let palette = match &png.ihdr.color_type {
|
||||
ColorType::Indexed { palette } if palette.len() > 1 => palette,
|
||||
_ => return None,
|
||||
};
|
||||
|
||||
let mut enumerated: Vec<_> = palette.iter().enumerate().collect();
|
||||
// Put the most popular edge color first, which can help slightly if the filter bytes are 0
|
||||
let keep_first = most_popular_edge_color(palette.len(), png);
|
||||
let first = keep_first.map(|f| enumerated.remove(f));
|
||||
|
||||
// Sort the palette
|
||||
enumerated.sort_by(|a, b| {
|
||||
// Sort by ascending alpha and descending luma
|
||||
let color_val = |color: &RGBA8| {
|
||||
let a = i32::from(color.a);
|
||||
// Put 7 high bits of alpha first, then luma, then low bit of alpha
|
||||
// This provides notable improvement in images with a lot of alpha
|
||||
((a & 0xFE) << 18) + (a & 0x01)
|
||||
// These are coefficients for standard sRGB to luma conversion
|
||||
- i32::from(color.r) * 299
|
||||
- i32::from(color.g) * 587
|
||||
- i32::from(color.b) * 114
|
||||
};
|
||||
color_val(a.1).cmp(&color_val(b.1))
|
||||
});
|
||||
if let Some(first) = first {
|
||||
enumerated.insert(0, first);
|
||||
}
|
||||
|
||||
// Extract the new palette and determine if anything changed
|
||||
let (remapping, palette): (Vec<_>, Vec<RGBA8>) = enumerated.into_iter().unzip();
|
||||
if remapping.iter().enumerate().all(|(a, b)| a == *b) {
|
||||
return None;
|
||||
}
|
||||
|
||||
// Construct the new mapping and convert the data
|
||||
let mut byte_map = [0; 256];
|
||||
for (i, &v) in remapping.iter().enumerate() {
|
||||
byte_map[v] = i as u8;
|
||||
}
|
||||
let data = png.data.iter().map(|&b| byte_map[b as usize]).collect();
|
||||
|
||||
Some(PngImage {
|
||||
ihdr: IhdrData {
|
||||
color_type: ColorType::Indexed { palette },
|
||||
..png.ihdr
|
||||
},
|
||||
data,
|
||||
})
|
||||
}
|
||||
|
||||
/// Sort the colors in the palette using the mzeng technique, returning the sorted image if successful
|
||||
#[must_use]
|
||||
pub fn sorted_palette_mzeng(png: &PngImage) -> Option<PngImage> {
|
||||
// Interlacing not currently supported
|
||||
if png.ihdr.bit_depth != BitDepth::Eight || png.ihdr.interlaced {
|
||||
return None;
|
||||
}
|
||||
let palette = match &png.ihdr.color_type {
|
||||
// Images with only two colors will remain unchanged from previous luma sort
|
||||
ColorType::Indexed { palette } if palette.len() > 2 => palette,
|
||||
_ => return None,
|
||||
};
|
||||
|
||||
let matrix = co_occurrence_matrix(palette.len(), png);
|
||||
let edges = weighted_edges(&matrix);
|
||||
let mut remapping = mzeng_reindex(palette.len(), edges, &matrix);
|
||||
|
||||
apply_most_popular_color(png, &mut remapping);
|
||||
|
||||
apply_palette_reorder(png, &remapping)
|
||||
}
|
||||
|
||||
/// Sort the colors in the palette using the battiato technique, returning the sorted image if successful
|
||||
#[must_use]
|
||||
pub fn sorted_palette_battiato(png: &PngImage) -> Option<PngImage> {
|
||||
// Interlacing not currently supported
|
||||
if png.ihdr.bit_depth != BitDepth::Eight || png.ihdr.interlaced {
|
||||
return None;
|
||||
}
|
||||
let palette = match &png.ihdr.color_type {
|
||||
// Images with only two colors will remain unchanged from previous luma sort
|
||||
ColorType::Indexed { palette } if palette.len() > 2 => palette,
|
||||
_ => return None,
|
||||
};
|
||||
|
||||
let matrix = co_occurrence_matrix(palette.len(), png);
|
||||
let edges = weighted_edges(&matrix);
|
||||
let mut remapping = battiato_reindex(palette.len(), edges);
|
||||
|
||||
apply_most_popular_color(png, &mut remapping);
|
||||
|
||||
apply_palette_reorder(png, &remapping)
|
||||
}
|
||||
|
||||
// Apply the palette reordering to the image data
|
||||
fn apply_palette_reorder(png: &PngImage, remapping: &[usize]) -> Option<PngImage> {
|
||||
let ColorType::Indexed { palette } = &png.ihdr.color_type else {
|
||||
return None;
|
||||
};
|
||||
|
||||
// Check if anything changed
|
||||
if remapping.iter().enumerate().all(|(a, b)| a == *b) {
|
||||
return None;
|
||||
}
|
||||
|
||||
// Construct the palette and byte maps and convert the data
|
||||
let mut new_palette = Vec::new();
|
||||
let mut byte_map = [0; 256];
|
||||
for (i, &v) in remapping.iter().enumerate() {
|
||||
new_palette.push(palette[v]);
|
||||
byte_map[v] = i as u8;
|
||||
}
|
||||
let data = png.data.iter().map(|&b| byte_map[b as usize]).collect();
|
||||
|
||||
Some(PngImage {
|
||||
ihdr: IhdrData {
|
||||
color_type: ColorType::Indexed {
|
||||
palette: new_palette,
|
||||
},
|
||||
..png.ihdr
|
||||
},
|
||||
data,
|
||||
})
|
||||
}
|
||||
|
||||
// Find the most popular color on the image edges (the pixels neighboring the filter bytes)
|
||||
fn most_popular_edge_color(num_colors: usize, png: &PngImage) -> Option<usize> {
|
||||
let mut counts = [0_u32; 256];
|
||||
for line in png.scan_lines(false) {
|
||||
if let &[first, .., last] = line.data {
|
||||
counts[first as usize] += 1;
|
||||
counts[last as usize] += 1;
|
||||
}
|
||||
}
|
||||
let max = counts
|
||||
.iter()
|
||||
.take(num_colors)
|
||||
.enumerate()
|
||||
.max_by_key(|&(_, v)| v)
|
||||
.unwrap();
|
||||
// Ensure there's a clear winner - return None if multiple colors are tied
|
||||
let max_equal = counts.iter().filter(|&v| v == max.1).count();
|
||||
if max_equal > 1 {
|
||||
return None;
|
||||
}
|
||||
Some(max.0)
|
||||
}
|
||||
|
||||
// Find the most popular color in the image, along with its count
|
||||
fn most_popular_color(num_colors: usize, png: &PngImage) -> (usize, u32) {
|
||||
let mut counts = [0_u32; 256];
|
||||
for &val in &png.data {
|
||||
counts[val as usize] += 1;
|
||||
}
|
||||
counts
|
||||
.iter()
|
||||
.copied()
|
||||
.take(num_colors)
|
||||
.enumerate()
|
||||
.max_by_key(|&(_, v)| v)
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
// Put the most popular color first
|
||||
fn apply_most_popular_color(png: &PngImage, remapping: &mut [usize]) {
|
||||
let most_popular = most_popular_color(remapping.len(), png);
|
||||
// If the most popular color is less than 15% of the image, don't use it
|
||||
if most_popular.1 < png.data.len() as u32 * 3 / 20 {
|
||||
return;
|
||||
}
|
||||
let first_idx = remapping.iter().position(|&i| i == most_popular.0).unwrap();
|
||||
// If the index is past halfway, reverse the order so as to minimize the change
|
||||
if first_idx >= remapping.len() / 2 {
|
||||
remapping.reverse();
|
||||
remapping.rotate_right(first_idx + 1);
|
||||
} else {
|
||||
remapping.rotate_left(first_idx);
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate co-occurences matrix
|
||||
fn co_occurrence_matrix(num_colors: usize, png: &PngImage) -> Vec<Vec<u32>> {
|
||||
let mut matrix = vec![vec![0_u32; num_colors]; num_colors];
|
||||
let mut prev: Option<ScanLine> = None;
|
||||
let mut prev_val = None;
|
||||
for line in png.scan_lines(false) {
|
||||
for i in 0..line.data.len() {
|
||||
let val = line.data[i] as usize;
|
||||
if val > num_colors {
|
||||
continue;
|
||||
}
|
||||
if let Some(prev_val) = prev_val.replace(val) {
|
||||
matrix[prev_val][val] += 1;
|
||||
matrix[val][prev_val] += 1;
|
||||
}
|
||||
if let Some(prev) = &prev {
|
||||
let prev_val = prev.data[i] as usize;
|
||||
if prev_val > num_colors {
|
||||
continue;
|
||||
}
|
||||
matrix[prev_val][val] += 1;
|
||||
matrix[val][prev_val] += 1;
|
||||
}
|
||||
}
|
||||
prev = Some(line);
|
||||
}
|
||||
matrix
|
||||
}
|
||||
|
||||
// Calculate edge list sorted by weight
|
||||
fn weighted_edges(matrix: &[Vec<u32>]) -> Vec<(usize, usize)> {
|
||||
let mut edges = Vec::new();
|
||||
for (i, m_row) in matrix.iter().enumerate() {
|
||||
for (j, val) in m_row.iter().enumerate().take(i) {
|
||||
edges.push(((j, i), val));
|
||||
}
|
||||
}
|
||||
edges.sort_by(|(_, w1), (_, w2)| w2.cmp(w1));
|
||||
edges.into_iter().map(|(e, _)| e).collect()
|
||||
}
|
||||
|
||||
// Apply a greedy index assignment using the modified version of Zeng's techinque from
|
||||
// "A note on Zeng's technique for color reindexing of palette-based images" by Pinho et al
|
||||
// https://ieeexplore.ieee.org/document/1261987
|
||||
// Based on the C implementation in libwebp
|
||||
fn mzeng_reindex(num_colors: usize, edges: Vec<(usize, usize)>, matrix: &[Vec<u32>]) -> Vec<usize> {
|
||||
// Initialize the mapping list with the two best indices.
|
||||
let mut remapping = vec![edges[0].0, edges[0].1];
|
||||
|
||||
// Initialize the sums with the first two remappings and find the best one
|
||||
let mut sums = Vec::new();
|
||||
let mut best_sum_pos = 0;
|
||||
let mut best_sum = (0, 0);
|
||||
for (i, m_row) in matrix.iter().enumerate() {
|
||||
if i == remapping[0] || i == remapping[1] {
|
||||
continue;
|
||||
}
|
||||
let sum = (i, m_row[remapping[0]] + m_row[remapping[1]]);
|
||||
if sum.1 > best_sum.1 {
|
||||
best_sum_pos = sums.len();
|
||||
best_sum = sum;
|
||||
}
|
||||
sums.push(sum);
|
||||
}
|
||||
|
||||
while !sums.is_empty() {
|
||||
let best_index = best_sum.0;
|
||||
// Compute delta to know if we need to prepend or append the best index.
|
||||
let mut delta: isize = 0;
|
||||
let n = (num_colors - sums.len()) as isize;
|
||||
for (i, &index) in remapping.iter().enumerate() {
|
||||
delta += (n - 1 - 2 * i as isize) * matrix[best_index][index] as isize;
|
||||
}
|
||||
if delta > 0 {
|
||||
remapping.insert(0, best_index);
|
||||
} else {
|
||||
remapping.push(best_index);
|
||||
}
|
||||
// Remove best_sum from sums.
|
||||
sums.swap_remove(best_sum_pos);
|
||||
if !sums.is_empty() {
|
||||
// Update all the sums and find the best one.
|
||||
best_sum_pos = 0;
|
||||
best_sum = (0, 0);
|
||||
for (i, sum) in sums.iter_mut().enumerate() {
|
||||
sum.1 += matrix[best_index][sum.0];
|
||||
if sum.1 > best_sum.1 {
|
||||
best_sum_pos = i;
|
||||
best_sum = *sum;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Return the completed remapping
|
||||
remapping
|
||||
}
|
||||
|
||||
// Calculate an approximate solution of the Traveling Salesman Problem using the algorithm
|
||||
// from "An efficient Re-indexing algorithm for color-mapped images" by Battiato et al
|
||||
// https://ieeexplore.ieee.org/document/1344033
|
||||
fn battiato_reindex(num_colors: usize, edges: Vec<(usize, usize)>) -> Vec<usize> {
|
||||
let mut chains = Vec::new();
|
||||
// Keep track of the state of each vertex (.0) and it's chain number (.1)
|
||||
// 0 = an unvisited vertex (White)
|
||||
// 1 = an endpoint of a chain (Red)
|
||||
// 2 = part of the middle of a chain (Black)
|
||||
let mut vx = vec![(0, 0); num_colors];
|
||||
|
||||
// Iterate the edges and assemble them into a chain
|
||||
for (i, j) in edges {
|
||||
let vi = vx[i];
|
||||
let vj = vx[j];
|
||||
if vi.0 == 0 && vj.0 == 0 {
|
||||
// Two unvisited vertices - create a new chain
|
||||
vx[i].0 = 1;
|
||||
vx[i].1 = chains.len();
|
||||
vx[j].0 = 1;
|
||||
vx[j].1 = chains.len();
|
||||
chains.push(vec![i, j]);
|
||||
} else if vi.0 == 0 && vj.0 == 1 {
|
||||
// An unvisited vertex connects with an endpoint of an existing chain
|
||||
vx[i].0 = 1;
|
||||
vx[i].1 = vj.1;
|
||||
vx[j].0 = 2;
|
||||
let chain = &mut chains[vj.1];
|
||||
if chain[0] == j {
|
||||
chain.insert(0, i);
|
||||
} else {
|
||||
chain.push(i);
|
||||
}
|
||||
} else if vi.0 == 1 && vj.0 == 0 {
|
||||
// An unvisited vertex connects with an endpoint of an existing chain
|
||||
vx[j].0 = 1;
|
||||
vx[j].1 = vi.1;
|
||||
vx[i].0 = 2;
|
||||
let chain = &mut chains[vi.1];
|
||||
if chain[0] == i {
|
||||
chain.insert(0, j);
|
||||
} else {
|
||||
chain.push(j);
|
||||
}
|
||||
} else if vi.0 == 1 && vj.0 == 1 && vi.1 != vj.1 {
|
||||
// Two endpoints of different chains are connected together
|
||||
vx[i].0 = 2;
|
||||
vx[j].0 = 2;
|
||||
let (a, b) = if vi.1 < vj.1 { (i, j) } else { (j, i) };
|
||||
let ca = vx[a].1;
|
||||
let cb = vx[b].1;
|
||||
let chainb = std::mem::take(&mut chains[cb]);
|
||||
for &v in &chainb {
|
||||
vx[v].1 = ca;
|
||||
}
|
||||
let chaina = &mut chains[ca];
|
||||
if chaina[0] == a && chainb[0] == b {
|
||||
for v in chainb {
|
||||
chaina.insert(0, v);
|
||||
}
|
||||
} else if chaina[0] == a {
|
||||
chaina.splice(0..0, chainb);
|
||||
} else if chainb[0] == b {
|
||||
chaina.extend(chainb);
|
||||
} else {
|
||||
let pos = chaina.len();
|
||||
for v in chainb {
|
||||
chaina.insert(pos, v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if chains[0].len() == num_colors {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Return the completed chain
|
||||
chains.swap_remove(0)
|
||||
}
|
||||
65
src/sanity_checks.rs
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
use std::io::Cursor;
|
||||
|
||||
use image::{codecs::png::PngDecoder, *};
|
||||
use log::{error, warn};
|
||||
|
||||
#[cfg(not(feature = "parallel"))]
|
||||
use crate::rayon;
|
||||
|
||||
/// Validate that the output png data still matches the original image
|
||||
pub fn validate_output(output: &[u8], original_data: &[u8]) -> bool {
|
||||
let (old_frames, new_frames) = rayon::join(
|
||||
|| load_png_image_from_memory(original_data),
|
||||
|| load_png_image_from_memory(output),
|
||||
);
|
||||
|
||||
match (new_frames, old_frames) {
|
||||
(Err(new_err), _) => {
|
||||
error!("Failed to read output image for validation: {}", new_err);
|
||||
false
|
||||
}
|
||||
(_, Err(old_err)) => {
|
||||
// The original image might be invalid if, for example, there is a CRC error,
|
||||
// and we set fix_errors to true. In that case, all we can do is check that the
|
||||
// new image is decodable.
|
||||
warn!("Failed to read input image for validation: {}", old_err);
|
||||
true
|
||||
}
|
||||
(Ok(new_frames), Ok(old_frames)) if new_frames.len() != old_frames.len() => false,
|
||||
(Ok(new_frames), Ok(old_frames)) => {
|
||||
for (a, b) in old_frames.iter().zip(new_frames) {
|
||||
if !images_equal(a, &b) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Loads a PNG image from memory to frames of [RgbaImage]
|
||||
fn load_png_image_from_memory(png_data: &[u8]) -> Result<Vec<RgbaImage>, image::ImageError> {
|
||||
let decoder = PngDecoder::new(Cursor::new(png_data))?;
|
||||
if decoder.is_apng()? {
|
||||
decoder
|
||||
.apng()?
|
||||
.into_frames()
|
||||
.map(|f| f.map(image::Frame::into_buffer))
|
||||
.collect()
|
||||
} else {
|
||||
DynamicImage::from_decoder(decoder).map(|i| vec![i.into_rgba8()])
|
||||
}
|
||||
}
|
||||
|
||||
/// Compares images pixel by pixel for equivalent content
|
||||
fn images_equal(old_png: &RgbaImage, new_png: &RgbaImage) -> bool {
|
||||
let a = old_png.pixels().filter(|x| {
|
||||
let p = x.channels();
|
||||
!(p.len() == 4 && p[3] == 0)
|
||||
});
|
||||
let b = new_png.pixels().filter(|x| {
|
||||
let p = x.channels();
|
||||
!(p.len() == 4 && p[3] == 0)
|
||||
});
|
||||
a.eq(b)
|
||||
}
|
||||
346
tests/alpha.rs
Normal file
|
|
@ -0,0 +1,346 @@
|
|||
use std::{
|
||||
fs::remove_file,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use oxipng::{internal_tests::*, *};
|
||||
|
||||
const GRAYSCALE_ALPHA: u8 = 4;
|
||||
const RGBA: u8 = 6;
|
||||
|
||||
fn get_opts(input: &Path) -> (OutFile, oxipng::Options) {
|
||||
let options = oxipng::Options {
|
||||
force: true,
|
||||
optimize_alpha: true,
|
||||
..Default::default()
|
||||
};
|
||||
(OutFile::from_path(input.with_extension("out.png")), options)
|
||||
}
|
||||
|
||||
fn test_it_converts(
|
||||
input: &str,
|
||||
filter: FilterStrategy,
|
||||
color_type_in: u8,
|
||||
bit_depth_in: BitDepth,
|
||||
color_type_out: u8,
|
||||
bit_depth_out: BitDepth,
|
||||
) {
|
||||
let input = PathBuf::from(input);
|
||||
|
||||
let (output, mut opts) = get_opts(&input);
|
||||
let png = PngData::new(&input, &opts).unwrap();
|
||||
opts.filters = indexset! {filter};
|
||||
assert_eq!(png.raw.ihdr.color_type.png_header_code(), color_type_in);
|
||||
assert_eq!(png.raw.ihdr.bit_depth, bit_depth_in);
|
||||
|
||||
match oxipng::optimize(&InFile::Path(input), &output, &opts) {
|
||||
Ok(_) => (),
|
||||
Err(x) => panic!("{}", x),
|
||||
}
|
||||
let output = output.path().unwrap();
|
||||
assert!(output.exists());
|
||||
|
||||
let png = match PngData::new(output, &opts) {
|
||||
Ok(x) => x,
|
||||
Err(x) => {
|
||||
remove_file(output).ok();
|
||||
panic!("{}", x)
|
||||
}
|
||||
};
|
||||
|
||||
assert_eq!(png.raw.ihdr.color_type.png_header_code(), color_type_out);
|
||||
assert_eq!(png.raw.ihdr.bit_depth, bit_depth_out);
|
||||
if let ColorType::Indexed { palette } = &png.raw.ihdr.color_type {
|
||||
assert!(palette.len() <= 1 << (png.raw.ihdr.bit_depth as u8));
|
||||
}
|
||||
|
||||
remove_file(output).ok();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alpha_filter_0_for_rgba_16() {
|
||||
test_it_converts(
|
||||
"tests/files/filter_0_for_rgba_16.png",
|
||||
FilterStrategy::NONE,
|
||||
RGBA,
|
||||
BitDepth::Sixteen,
|
||||
RGBA,
|
||||
BitDepth::Sixteen,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alpha_filter_1_for_rgba_16() {
|
||||
test_it_converts(
|
||||
"tests/files/filter_1_for_rgba_16.png",
|
||||
FilterStrategy::SUB,
|
||||
RGBA,
|
||||
BitDepth::Sixteen,
|
||||
RGBA,
|
||||
BitDepth::Sixteen,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alpha_filter_2_for_rgba_16() {
|
||||
test_it_converts(
|
||||
"tests/files/filter_2_for_rgba_16.png",
|
||||
FilterStrategy::UP,
|
||||
RGBA,
|
||||
BitDepth::Sixteen,
|
||||
RGBA,
|
||||
BitDepth::Sixteen,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alpha_filter_3_for_rgba_16() {
|
||||
test_it_converts(
|
||||
"tests/files/filter_3_for_rgba_16.png",
|
||||
FilterStrategy::AVERAGE,
|
||||
RGBA,
|
||||
BitDepth::Sixteen,
|
||||
RGBA,
|
||||
BitDepth::Sixteen,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alpha_filter_4_for_rgba_16() {
|
||||
test_it_converts(
|
||||
"tests/files/filter_4_for_rgba_16.png",
|
||||
FilterStrategy::PAETH,
|
||||
RGBA,
|
||||
BitDepth::Sixteen,
|
||||
RGBA,
|
||||
BitDepth::Sixteen,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alpha_filter_5_for_rgba_16() {
|
||||
test_it_converts(
|
||||
"tests/files/filter_5_for_rgba_16.png",
|
||||
FilterStrategy::MinSum,
|
||||
RGBA,
|
||||
BitDepth::Sixteen,
|
||||
RGBA,
|
||||
BitDepth::Sixteen,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alpha_filter_0_for_rgba_8() {
|
||||
test_it_converts(
|
||||
"tests/files/filter_0_for_rgba_8.png",
|
||||
FilterStrategy::NONE,
|
||||
RGBA,
|
||||
BitDepth::Eight,
|
||||
RGBA,
|
||||
BitDepth::Eight,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alpha_filter_1_for_rgba_8() {
|
||||
test_it_converts(
|
||||
"tests/files/filter_1_for_rgba_8.png",
|
||||
FilterStrategy::SUB,
|
||||
RGBA,
|
||||
BitDepth::Eight,
|
||||
RGBA,
|
||||
BitDepth::Eight,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alpha_filter_2_for_rgba_8() {
|
||||
test_it_converts(
|
||||
"tests/files/filter_2_for_rgba_8.png",
|
||||
FilterStrategy::UP,
|
||||
RGBA,
|
||||
BitDepth::Eight,
|
||||
RGBA,
|
||||
BitDepth::Eight,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alpha_filter_3_for_rgba_8() {
|
||||
test_it_converts(
|
||||
"tests/files/filter_3_for_rgba_8.png",
|
||||
FilterStrategy::AVERAGE,
|
||||
RGBA,
|
||||
BitDepth::Eight,
|
||||
RGBA,
|
||||
BitDepth::Eight,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alpha_filter_4_for_rgba_8() {
|
||||
test_it_converts(
|
||||
"tests/files/filter_4_for_rgba_8.png",
|
||||
FilterStrategy::PAETH,
|
||||
RGBA,
|
||||
BitDepth::Eight,
|
||||
RGBA,
|
||||
BitDepth::Eight,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alpha_filter_5_for_rgba_8() {
|
||||
test_it_converts(
|
||||
"tests/files/filter_5_for_rgba_8.png",
|
||||
FilterStrategy::MinSum,
|
||||
RGBA,
|
||||
BitDepth::Eight,
|
||||
RGBA,
|
||||
BitDepth::Eight,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alpha_filter_0_for_grayscale_alpha_16() {
|
||||
test_it_converts(
|
||||
"tests/files/filter_0_for_grayscale_alpha_16.png",
|
||||
FilterStrategy::NONE,
|
||||
GRAYSCALE_ALPHA,
|
||||
BitDepth::Sixteen,
|
||||
GRAYSCALE_ALPHA,
|
||||
BitDepth::Sixteen,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alpha_filter_1_for_grayscale_alpha_16() {
|
||||
test_it_converts(
|
||||
"tests/files/filter_1_for_grayscale_alpha_16.png",
|
||||
FilterStrategy::SUB,
|
||||
GRAYSCALE_ALPHA,
|
||||
BitDepth::Sixteen,
|
||||
GRAYSCALE_ALPHA,
|
||||
BitDepth::Sixteen,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alpha_filter_2_for_grayscale_alpha_16() {
|
||||
test_it_converts(
|
||||
"tests/files/filter_2_for_grayscale_alpha_16.png",
|
||||
FilterStrategy::UP,
|
||||
GRAYSCALE_ALPHA,
|
||||
BitDepth::Sixteen,
|
||||
GRAYSCALE_ALPHA,
|
||||
BitDepth::Sixteen,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alpha_filter_3_for_grayscale_alpha_16() {
|
||||
test_it_converts(
|
||||
"tests/files/filter_3_for_grayscale_alpha_16.png",
|
||||
FilterStrategy::AVERAGE,
|
||||
GRAYSCALE_ALPHA,
|
||||
BitDepth::Sixteen,
|
||||
GRAYSCALE_ALPHA,
|
||||
BitDepth::Sixteen,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alpha_filter_4_for_grayscale_alpha_16() {
|
||||
test_it_converts(
|
||||
"tests/files/filter_4_for_grayscale_alpha_16.png",
|
||||
FilterStrategy::PAETH,
|
||||
GRAYSCALE_ALPHA,
|
||||
BitDepth::Sixteen,
|
||||
GRAYSCALE_ALPHA,
|
||||
BitDepth::Sixteen,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alpha_filter_5_for_grayscale_alpha_16() {
|
||||
test_it_converts(
|
||||
"tests/files/filter_5_for_grayscale_alpha_16.png",
|
||||
FilterStrategy::MinSum,
|
||||
GRAYSCALE_ALPHA,
|
||||
BitDepth::Sixteen,
|
||||
GRAYSCALE_ALPHA,
|
||||
BitDepth::Sixteen,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alpha_filter_0_for_grayscale_alpha_8() {
|
||||
test_it_converts(
|
||||
"tests/files/filter_0_for_grayscale_alpha_8.png",
|
||||
FilterStrategy::NONE,
|
||||
GRAYSCALE_ALPHA,
|
||||
BitDepth::Eight,
|
||||
GRAYSCALE_ALPHA,
|
||||
BitDepth::Eight,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alpha_filter_1_for_grayscale_alpha_8() {
|
||||
test_it_converts(
|
||||
"tests/files/filter_1_for_grayscale_alpha_8.png",
|
||||
FilterStrategy::SUB,
|
||||
GRAYSCALE_ALPHA,
|
||||
BitDepth::Eight,
|
||||
GRAYSCALE_ALPHA,
|
||||
BitDepth::Eight,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alpha_filter_2_for_grayscale_alpha_8() {
|
||||
test_it_converts(
|
||||
"tests/files/filter_2_for_grayscale_alpha_8.png",
|
||||
FilterStrategy::UP,
|
||||
GRAYSCALE_ALPHA,
|
||||
BitDepth::Eight,
|
||||
GRAYSCALE_ALPHA,
|
||||
BitDepth::Eight,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alpha_filter_3_for_grayscale_alpha_8() {
|
||||
test_it_converts(
|
||||
"tests/files/filter_3_for_grayscale_alpha_8.png",
|
||||
FilterStrategy::AVERAGE,
|
||||
GRAYSCALE_ALPHA,
|
||||
BitDepth::Eight,
|
||||
GRAYSCALE_ALPHA,
|
||||
BitDepth::Eight,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alpha_filter_4_for_grayscale_alpha_8() {
|
||||
test_it_converts(
|
||||
"tests/files/filter_4_for_grayscale_alpha_8.png",
|
||||
FilterStrategy::PAETH,
|
||||
GRAYSCALE_ALPHA,
|
||||
BitDepth::Eight,
|
||||
GRAYSCALE_ALPHA,
|
||||
BitDepth::Eight,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alpha_filter_5_for_grayscale_alpha_8() {
|
||||
test_it_converts(
|
||||
"tests/files/filter_5_for_grayscale_alpha_8.png",
|
||||
FilterStrategy::MinSum,
|
||||
GRAYSCALE_ALPHA,
|
||||
BitDepth::Eight,
|
||||
GRAYSCALE_ALPHA,
|
||||
BitDepth::Eight,
|
||||
);
|
||||
}
|
||||
BIN
tests/files/apng_file.png
Normal file
|
After Width: | Height: | Size: 62 KiB |
BIN
tests/files/badsrgb.png
Normal file
|
After Width: | Height: | Size: 4.2 KiB |
BIN
tests/files/c2pa-signed.png
Normal file
|
After Width: | Height: | Size: 66 KiB |
|
Before Width: | Height: | Size: 210 KiB After Width: | Height: | Size: 195 KiB |
|
Before Width: | Height: | Size: 63 KiB After Width: | Height: | Size: 47 KiB |
|
Before Width: | Height: | Size: 328 KiB After Width: | Height: | Size: 296 KiB |
|
Before Width: | Height: | Size: 95 KiB After Width: | Height: | Size: 45 KiB |
|
Before Width: | Height: | Size: 1 KiB After Width: | Height: | Size: 193 B |
|
Before Width: | Height: | Size: 1.1 KiB After Width: | Height: | Size: 285 B |
|
Before Width: | Height: | Size: 1.4 KiB After Width: | Height: | Size: 546 B |
|
Before Width: | Height: | Size: 207 KiB After Width: | Height: | Size: 206 KiB |
|
Before Width: | Height: | Size: 112 KiB After Width: | Height: | Size: 111 KiB |
|
Before Width: | Height: | Size: 102 KiB After Width: | Height: | Size: 89 KiB |
|
Before Width: | Height: | Size: 32 KiB After Width: | Height: | Size: 39 KiB |
|
Before Width: | Height: | Size: 210 KiB After Width: | Height: | Size: 195 KiB |
|
Before Width: | Height: | Size: 63 KiB After Width: | Height: | Size: 47 KiB |
|
Before Width: | Height: | Size: 328 KiB After Width: | Height: | Size: 296 KiB |
|
Before Width: | Height: | Size: 95 KiB After Width: | Height: | Size: 45 KiB |
|
Before Width: | Height: | Size: 1 KiB After Width: | Height: | Size: 193 B |
|
Before Width: | Height: | Size: 1.1 KiB After Width: | Height: | Size: 285 B |
|
Before Width: | Height: | Size: 1.4 KiB After Width: | Height: | Size: 546 B |
|
Before Width: | Height: | Size: 207 KiB After Width: | Height: | Size: 206 KiB |
|
Before Width: | Height: | Size: 112 KiB After Width: | Height: | Size: 111 KiB |
|
Before Width: | Height: | Size: 102 KiB After Width: | Height: | Size: 89 KiB |
|
Before Width: | Height: | Size: 32 KiB After Width: | Height: | Size: 39 KiB |
|
Before Width: | Height: | Size: 210 KiB After Width: | Height: | Size: 195 KiB |
|
Before Width: | Height: | Size: 63 KiB After Width: | Height: | Size: 47 KiB |
|
Before Width: | Height: | Size: 328 KiB After Width: | Height: | Size: 296 KiB |
|
Before Width: | Height: | Size: 95 KiB After Width: | Height: | Size: 45 KiB |
|
Before Width: | Height: | Size: 1 KiB After Width: | Height: | Size: 193 B |
|
Before Width: | Height: | Size: 1.1 KiB After Width: | Height: | Size: 285 B |
|
Before Width: | Height: | Size: 1.4 KiB After Width: | Height: | Size: 546 B |
|
Before Width: | Height: | Size: 207 KiB After Width: | Height: | Size: 206 KiB |
|
Before Width: | Height: | Size: 112 KiB After Width: | Height: | Size: 111 KiB |
|
Before Width: | Height: | Size: 102 KiB After Width: | Height: | Size: 89 KiB |
|
Before Width: | Height: | Size: 32 KiB After Width: | Height: | Size: 39 KiB |
|
Before Width: | Height: | Size: 210 KiB After Width: | Height: | Size: 195 KiB |
|
Before Width: | Height: | Size: 63 KiB After Width: | Height: | Size: 47 KiB |
|
Before Width: | Height: | Size: 328 KiB After Width: | Height: | Size: 296 KiB |
|
Before Width: | Height: | Size: 95 KiB After Width: | Height: | Size: 45 KiB |