Commit graph

27 commits

Author SHA1 Message Date
Alejandro González
491d753edc
Add option to skip grayscale reduction of RGB(A) images (#409)
For RGB(A) images that contain gray colors, this reduction can achieve
significant space savings. However, in the absence of gamma correction
data, some PNG decoders assume more exotic color spaces for grayscale
images instead of the ubiquitous sRGB. This results in gamma
miscorrection, and for the end user this means that colors will look
wrong, like "washed-out". Java's ImageIO class, which is popular in the
JVM world to read PNG files, uses rather unconventional defaults, as
explained in this StackOverflow question: https://stackoverflow.com/questions/31312645/java-imageio-grayscale-png-issue

Gamma miscorrection problems aside, OxiPNG currently tries hard to
reduce RGB(A) images to grayscale, because it expects that reduction to
be quite effective. However, in some cases, OxiPNG generates smaller
PNG files when reducing grasycale RGB(A) images to paletted color than
actual grayscale color. For example, let's say that "~/gray.png" is a
256x256 RGBA image entirely filled with (119, 119, 119, 255) pixels.
OxiPNG, by default, reduces this image to grayscale and achieves a
68.23% decrease:

$ cargo build --release && target/release/oxipng -omax --out ~/out.png ~/gray.png
Processing: /home/user/gray.png
    256x256 pixels, PNG format
    4x8 bits/pixel, RGBA
    IDAT size = 604 bytes
    File size = 661 bytes
Reducing image to 1x4 bits/pixel, Grayscale
Trying: 144 combinations
Found better combination:
    zc = 6  zs = 0  f = 0        153 bytes
    IDAT size = 153 bytes (451 bytes decrease)
    file size = 210 bytes (451 bytes = 68.23% decrease)
Output: /home/user/out.png

However, if the --ng option that this commit adds is used to skip the
grayscale reduction step, OxiPNG reduces to a single color palette
instead, which is much more efficient, achieving a 84.42% decrease:

$ cargo build --release && target/release/oxipng -omax --ng --out ~/out.png ~/gray.png
Processing: /home/alejandro/gray.png
    256x256 pixels, PNG format
    4x8 bits/pixel, RGBA
    IDAT size = 604 bytes
    File size = 661 bytes
Reducing image to 1 bits/pixel, 1 colors in palette
Trying: 144 combinations
Found better combination:
    zc = 3  zs = 3  f = 0        31 bytes
    IDAT size = 31 bytes (573 bytes decrease)
    file size = 103 bytes (558 bytes = 84.42% decrease)
Output: /home/alejandro/out.png

While OxiPNG should arguably be made smarter to better handle these
cases, in the meantime, adding an option to manually skip that grayscale
reduction can't hurt. In fact, it may even help users achieving the most
out of current versions of OxiPNG, and developers reasoning about what
makes a grayscale-like RGB(A) image compress better with a color
palette.

Due to the reasons stated above, this adds a simple "grayscale_reduction"
option to the Options struct, and a "no-grayscale-reduction" command
line switch, that makes OxiPNG not try this problematic grayscale
reduction on RGB(A) images.
2021-07-11 23:21:38 -04:00
Ingvar Stepanyan
23ae9c302f
Switch to standard logging library (#218)
This allows to configure or compile away logging in the library from a single place in Rust apps.

For the CLI side, the usage and output remained the same, except it's now colour-coded.

Fixes #217.
2020-04-18 18:33:48 -04:00
Josh Holmer
139b2878d0 Fix benchmark compilation
Closes #197
2020-04-15 13:26:28 -04:00
Ingvar Stepanyan
121558bf5c
Add libdeflater as an option (#203)
libdeflater is a Rust wrapper around
[libdeflate](https://github.com/ebiggers/libdeflate) - an alternative
heavily optimised library for deflate/zlib/gzip compression and
decompression that is intended for situations where upper bounds of the
output are well-known.

In my benchmarks on test files in the repo it has shown to be usually
both slightly faster and providing better compressed output than
cloudflare-zlib, but in some cases showing the opposite, so rather
than swapping defaults, it's currently provided as another option,
similarly to zopfli.

Since it's not strictly better in all cases, I'm not providing median
numbers, but you can check distribution histograms for time and size
differences here (all using `oxipng -o 6 -t 6 -P`):
https://docs.google.com/spreadsheets/d/1WOKgeYZBhLkQvMGAC36snN4azilElzOFhx63RJu0EZY/edit?usp=sharing
2020-03-31 20:24:57 -04:00
Josh Holmer
d86bc80f43 Prepare for next release 2019-02-01 17:27:03 -05:00
Kornel
9af874d21b Make reductions return a new uncompressed image (#158)
* Fix verbose message

* No cloning when restoring original data

* Make reductions return a new uncompressed image

Partially fixes #145

* Async reduction evaluator

* Assert

* Faster bit depth check

* Also try 4-bit depth for small-depth images

* Skip test when using miniz

* Ensure palette is trimmed after depth reduction

Fixes #159

* Fudge factor for reductions to prefer better reductions even if gzip estimation says otherwise
2019-01-27 03:22:49 -05:00
Kornel
55d85df9fc Non-self-mutable reductions (#154)
* Move reductions to a module. Make copy instead of changing in-place.

* Alpha reductions

* Immutable color reductions

* Immutable interlace reductions
2019-01-12 05:07:57 -05:00
Joshua Holmer
1305b5cf17 Fix clippy lints and run rustfmt 2018-11-26 13:41:14 -05:00
Kornel
588d5bd52b Tweaks and Microoptimizations (#146)
* Fixed alpha benchmark

* Dedupe function

* Use integer math when rounding

* Fewer temporaries when slicing palette

* Filtering microoptimizations
2018-11-20 08:39:46 -05:00
Josh Holmer
d110949d2a Version 2.0.0 2018-07-20 19:19:20 -04:00
Kornel
c99b3778b2 Expose only one internal module specifically for tests (#123)
Closes #98
2018-07-17 23:38:11 -04:00
Kornel
442eaf766c Bail early (#104) 2018-06-08 00:09:22 -04:00
Kornel Lesiński
ef763a8550 Return result from bench iter to avoid unused results 2018-06-01 12:18:39 +01:00
Josh Holmer
fe9cd01b82 Replace miniz_sys with miniz_oxide, a Rust implementation of miniz
This also improves decompression performance by 15%, and obsoletes the
`--zm` parameter.

Closes #57
2018-01-30 23:52:37 -05:00
Josh Holmer
88f5bb0931 Hide modules from documentation, and only export structs used in options or return types 2018-01-10 10:37:01 -05:00
Josh Holmer
0db03c6faf Fix new clippy lints 2017-09-03 23:36:38 -04:00
Josh Holmer
ec65bf7676 Code cleanup 2017-07-26 07:57:13 -04:00
Josh Holmer
f41ef551d3 Enable option for multiple alpha transformations 2017-07-26 00:37:55 -04:00
Josh Holmer
413f1aac7c Automatically change transparent pixels to black 2017-07-24 08:47:27 -04:00
Josh Holmer
84cf6b09c2 Run rustfmt since it updated to v0.8.0.
I don't really like the new changes, but we might as well follow along.
2017-03-08 11:14:33 -05:00
Josh Holmer
af76f11f36 Bump dependencies 2017-02-13 08:51:00 -05:00
Josh Holmer
33f1fad711 Minor refactoring 2016-12-03 15:18:11 -05:00
Josh Holmer
6d49d7b8f9 Implement zopfli compression, available as a CLI flag (-Z)
Improves compression by about 10% over DEFLATE, but takes a very long
time at the moment, so it is off by default.
2016-12-01 12:51:30 -05:00
Joshua Holmer
15e41c9c6d Fix benchmarks broken by adding --fix 2016-05-02 13:20:27 -04:00
Joshua Holmer
1089754436 Add inflate bench for testing different implementations [ci skip] 2016-04-20 22:49:47 -04:00
Joshua Holmer
b67083da0c Revise benchmarks to be more direct [ci skip]
Now actually runs through the whole suite without file locking issues.

Initial results:

     Running target/release/deflate-032c8bb6310daa8e

running 20 tests
test bench_deflate_16_bits_strategy_0 ... bench:  34,772,065 ns/iter (+/- 3,927,686)
test bench_deflate_16_bits_strategy_1 ... bench:  35,829,570 ns/iter (+/- 1,683,748)
test bench_deflate_16_bits_strategy_2 ... bench:  12,428,913 ns/iter (+/- 1,005,308)
test bench_deflate_16_bits_strategy_3 ... bench:  13,247,589 ns/iter (+/- 1,026,688)
test bench_deflate_1_bits_strategy_0  ... bench:     232,037 ns/iter (+/- 5,818)
test bench_deflate_1_bits_strategy_1  ... bench:     235,463 ns/iter (+/- 25,972)
test bench_deflate_1_bits_strategy_2  ... bench:     168,770 ns/iter (+/- 20,463)
test bench_deflate_1_bits_strategy_3  ... bench:      32,254 ns/iter (+/- 1,034)
test bench_deflate_2_bits_strategy_0  ... bench:     537,638 ns/iter (+/- 27,704)
test bench_deflate_2_bits_strategy_1  ... bench:     532,766 ns/iter (+/- 26,777)
test bench_deflate_2_bits_strategy_2  ... bench:     313,566 ns/iter (+/- 29,872)
test bench_deflate_2_bits_strategy_3  ... bench:      44,436 ns/iter (+/- 3,554)
test bench_deflate_4_bits_strategy_0  ... bench:   2,259,173 ns/iter (+/- 236,430)
test bench_deflate_4_bits_strategy_1  ... bench:   2,259,694 ns/iter (+/- 254,753)
test bench_deflate_4_bits_strategy_2  ... bench:     731,383 ns/iter (+/- 21,518)
test bench_deflate_4_bits_strategy_3  ... bench:     193,049 ns/iter (+/- 24,552)
test bench_deflate_8_bits_strategy_0  ... bench:  26,799,023 ns/iter (+/- 2,322,131)
test bench_deflate_8_bits_strategy_1  ... bench:  26,986,947 ns/iter (+/- 3,026,671)
test bench_deflate_8_bits_strategy_2  ... bench:   5,844,986 ns/iter (+/- 547,120)
test bench_deflate_8_bits_strategy_3  ... bench:   5,401,345 ns/iter (+/- 592,391)

test result: ok. 0 passed; 0 failed; 0 ignored; 20 measured

     Running target/release/filters-4cdef7ed188d5a43

running 30 tests
test bench_16_bits_filter_0 ... bench:   1,201,636 ns/iter (+/- 122,922)
test bench_16_bits_filter_1 ... bench:   2,515,136 ns/iter (+/- 351,661)
test bench_16_bits_filter_2 ... bench:   2,536,454 ns/iter (+/- 595,207)
test bench_16_bits_filter_3 ... bench:   2,555,551 ns/iter (+/- 519,059)
test bench_16_bits_filter_4 ... bench:   6,413,073 ns/iter (+/- 505,450)
test bench_16_bits_filter_5 ... bench:  27,124,584 ns/iter (+/- 3,171,162)
test bench_1_bits_filter_0  ... bench:      55,761 ns/iter (+/- 19,412)
test bench_1_bits_filter_1  ... bench:      80,915 ns/iter (+/- 13,928)
test bench_1_bits_filter_2  ... bench:      80,479 ns/iter (+/- 9,268)
test bench_1_bits_filter_3  ... bench:      86,080 ns/iter (+/- 47,504)
test bench_1_bits_filter_4  ... bench:     154,723 ns/iter (+/- 3,932)
test bench_1_bits_filter_5  ... bench:     805,938 ns/iter (+/- 118,853)
test bench_2_bits_filter_0  ... bench:      74,940 ns/iter (+/- 14,205)
test bench_2_bits_filter_1  ... bench:     126,821 ns/iter (+/- 22,497)
test bench_2_bits_filter_2  ... bench:     126,445 ns/iter (+/- 17,852)
test bench_2_bits_filter_3  ... bench:     129,264 ns/iter (+/- 52,772)
test bench_2_bits_filter_4  ... bench:     267,978 ns/iter (+/- 7,064)
test bench_2_bits_filter_5  ... bench:   1,354,604 ns/iter (+/- 206,764)
test bench_4_bits_filter_0  ... bench:     119,468 ns/iter (+/- 13,806)
test bench_4_bits_filter_1  ... bench:     219,357 ns/iter (+/- 34,642)
test bench_4_bits_filter_2  ... bench:     218,588 ns/iter (+/- 11,280)
test bench_4_bits_filter_3  ... bench:     222,955 ns/iter (+/- 60,112)
test bench_4_bits_filter_4  ... bench:     516,987 ns/iter (+/- 33,155)
test bench_4_bits_filter_5  ... bench:   2,424,414 ns/iter (+/- 244,989)
test bench_8_bits_filter_0  ... bench:     551,986 ns/iter (+/- 89,602)
test bench_8_bits_filter_1  ... bench:   1,234,364 ns/iter (+/- 307,291)
test bench_8_bits_filter_2  ... bench:   1,150,679 ns/iter (+/- 35,718)
test bench_8_bits_filter_3  ... bench:   1,161,331 ns/iter (+/- 124,379)
test bench_8_bits_filter_4  ... bench:   3,704,653 ns/iter (+/- 659,212)
test bench_8_bits_filter_5  ... bench:  13,863,376 ns/iter (+/- 2,134,142)

test result: ok. 0 passed; 0 failed; 0 ignored; 30 measured

     Running target/release/interlacing-3a5b64514b03c920

running 10 tests
test bench_deinterlace_16_bits ... bench: 144,075,159 ns/iter (+/- 3,388,596)
test bench_deinterlace_1_bits  ... bench:   2,848,418 ns/iter (+/- 480,685)
test bench_deinterlace_2_bits  ... bench:   5,608,614 ns/iter (+/- 224,235)
test bench_deinterlace_4_bits  ... bench:  11,114,761 ns/iter (+/- 943,506)
test bench_deinterlace_8_bits  ... bench:  71,918,245 ns/iter (+/- 5,374,438)
test bench_interlace_16_bits   ... bench: 186,786,301 ns/iter (+/- 108,170,992)
test bench_interlace_1_bits    ... bench:   3,298,814 ns/iter (+/- 299,550)
test bench_interlace_2_bits    ... bench:   6,615,721 ns/iter (+/- 886,353)
test bench_interlace_4_bits    ... bench:  13,148,900 ns/iter (+/- 1,015,178)
test bench_interlace_8_bits    ... bench:  86,973,845 ns/iter (+/- 2,876,862)

test result: ok. 0 passed; 0 failed; 0 ignored; 10 measured

     Running target/release/oxipng-0aa81de33b66b9ba

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured

     Running target/release/oxipng-8dc3c38a24f45aca

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured

     Running target/release/reductions-706f4e9274315f2d

running 20 tests
test bench_16_to_8_bits                ... bench:   1,462,010 ns/iter (+/- 245,041)
test bench_2_to_1_bits                 ... bench:   9,090,966 ns/iter (+/- 949,980)
test bench_4_to_1_bits                 ... bench:  17,291,283 ns/iter (+/- 2,295,195)
test bench_4_to_2_bits                 ... bench:  18,106,877 ns/iter (+/- 3,257,698)
test bench_8_to_1_bits                 ... bench:  33,967,094 ns/iter (+/- 1,963,155)
test bench_8_to_2_bits                 ... bench:  34,834,040 ns/iter (+/- 2,992,472)
test bench_8_to_4_bits                 ... bench:  36,904,665 ns/iter (+/- 2,767,626)
test bench_palette_duplicate_reduction ... bench:   5,113,086 ns/iter (+/- 449,931)
test bench_palette_full_reduction      ... bench:  14,751,743 ns/iter (+/- 1,412,029)
test bench_palette_unused_reduction    ... bench:   9,639,936 ns/iter (+/- 462,188)
test bench_rgb_to_grayscale_16         ... bench:  29,428,159 ns/iter (+/- 3,303,357)
test bench_rgb_to_grayscale_8          ... bench:   7,224,088 ns/iter (+/- 951,724)
test bench_rgb_to_palette_8            ... bench:  34,767,333 ns/iter (+/- 1,765,318)
test bench_rgba_to_grayscale_16        ... bench:  28,374,910 ns/iter (+/- 1,495,313)
test bench_rgba_to_grayscale_8         ... bench:  14,288,546 ns/iter (+/- 1,106,506)
test bench_rgba_to_grayscale_alpha_16  ... bench:  20,908,298 ns/iter (+/- 1,351,356)
test bench_rgba_to_grayscale_alpha_8   ... bench:  10,504,579 ns/iter (+/- 1,093,582)
test bench_rgba_to_palette_8           ... bench:  42,195,599 ns/iter (+/- 1,868,201)
test bench_rgba_to_rgb_16              ... bench:  15,955,026 ns/iter (+/- 2,242,433)
test bench_rgba_to_rgb_8               ... bench:   8,185,576 ns/iter (+/- 1,116,708)

test result: ok. 0 passed; 0 failed; 0 ignored; 20 measured
2016-04-20 11:30:08 -04:00
Joshua Holmer
f2c16696a5 Add benchmark suite using cargo
Closes #7
2016-04-19 23:26:05 -04:00