Skip to content
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

Add compression level #488

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

marcosc90
Copy link

This PR adds Compression::Level(u32) to allow finer control over compression settings. It maps directly to flate2::Compression::new(level).

@fintelia
Copy link
Contributor

fintelia commented Jul 12, 2024

Could you say a bit about why you want this functionality?

One weird aspect of this PR is that CompressionLevel::Level(1) is actually different from CompressionLevel::Fast due to this match statement:

image-png/src/encoder.rs

Lines 696 to 697 in 2cfde02

let zlib_encoded = match self.info.compression {
Compression::Fast => {

Another complication is that CompressionLevel isn't marked an non_exhaustive so it would need to wait for the next breaking release.

@marcosc90
Copy link
Author

marcosc90 commented Jul 12, 2024

Apologies for the short PR description it was a bit late for me when I opened it.

Could you say a bit about why you want this functionality?

I'm building a Wasm package for compressing PNG, mostly canvas-generated PNG images which lack compression. And for my specific case, QR Images, Fast increases the image size, while Default is a bit slow, using Compression::Level(2-3) gives me the best ratio between file size and speed.

Also wanted to give users a more familiar compression setting, most libs (libvips, ImageMagick) I've used for handling images usually have a PNG compression level between 0-9

Another complication is that CompressionLevel isn't marked an non_exhaustive so it would need to wait for the next breaking release.

I'm currently building it myself with this patch applied, but thought that others may need this feature as well. So in my specific case I don't mind waiting for the next breaking release.

@Shnatsel
Copy link
Contributor

I also need this for the drop-in replacement for imagemagick that I am building.

@Shnatsel
Copy link
Contributor

Could we instead add this as a new method on the encoder, like set_deflate_compression(), and avoid the API break that way?

@0rvar
Copy link

0rvar commented Aug 22, 2024

I would also like this option, since the "Best" compression level in this crate, according to it's docs, does not actually use maximum compression, and there are no other variants that are higher either:

    /// Higher compression level
    ///
    /// Best in this context isn't actually the highest possible level
    /// the encoder can do, but is meant to emulate the `Best` setting in the `Flate2`
    /// library.
    Best,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants