-
Notifications
You must be signed in to change notification settings - Fork 18k
image/png: Encode is not as efficient as zlib-based encoders #16196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Oh, file sizes are within 1-2% between 1.4 and 1.7beta2. |
FWIW, Fileoptimzer can squeeze out an additional 5% with its lossless recompression tools. I haven't dug into PNG, but this seems more like there are some compression methods missing rather than an entropy encoding (deflate) problem. I looked through the filter chooser function, and couldn't spot anything immediately wrong, but it does seem that there is something sub-optimal in there. |
It's been a while since I looked at it, but IIRC the Go PNG filter chooser has the same algorithm as libpng. That's just about PNG filtering as in https://www.w3.org/TR/PNG/#9Filters and not anything to do with zlib. |
I found the issue. I dug into the PIL code and found out that it they do not apply filters for paletted images. Changing a single line from This is in line with the results from PIL and Image Magick. |
I found the following comment in the PNG book
|
@olt, nice find. |
I did tests with more files. Compression is better for almost all files, except for a few tiny files:
Code/test files can be found here: https://github.com/olt/compressbench I created (my first) patch https://go-review.googlesource.com/#/c/29872/ |
CL https://golang.org/cl/29872 mentions this issue. |
Paletted PNGs like this map tile (http://a.tile.openstreetmap.org/13/4404/2688.png) will increase by 20-30% in size when encoded with image/png.Encode.
See https://gist.github.com/olt/022a206444f20c147c4bc9a54fd1a433 for example re-encoder.
The size does not change much when I re-encode the image with Image Magick (convert) or Python Image Library (PIL/Pillow):
Changing the compression level in Go does not make any huge difference.
zlib allows to set different compress strategies. One of the strategies is Z_FILTERED, which is optimized for filtered data as found in PNGs.
From http://www.zlib.net/manual.html:
PIL allows to select the strategy and it gives file sizes in the range of 34521 to 45117:
Implementing a Z_FILTERED-like strategy should give much smaller files and it should have more impact than #15622.
The text was updated successfully, but these errors were encountered: