diff --git a/src/types.rs b/src/types.rs index f236422..49a4f7c 100644 --- a/src/types.rs +++ b/src/types.rs @@ -810,19 +810,27 @@ impl BomType { #[derive(Clone, Copy, Debug, ValueEnum, PartialEq, Eq, PartialOrd, Ord)] /// PNG compression level pub enum PngCompressionLevel { + #[value(alias = "n")] + /// No compression whatsoever. Fastest, but results in large files. + NoCompression, #[value(alias = "d")] - /// Default level + /// Default level (usually balanced) Default, + /// Extremely fast but light compression. + /// + /// Note: When used in streaming mode, this compression level can actually result in files + /// *larger* than would be produced by `NoCompression` on incompressible data because + /// it doesn't do any buffering of the output stream to detect whether the data is being compressed or not. + Fastest, #[value(alias = "f")] /// Fast minimal compression Fast, #[value(alias = "b")] - /// 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. + /// Higher compression level. Same as high Best, + #[value(alias = "h")] + /// Spend much more time to produce a slightly smaller file than with `Balanced`. + High, } #[cfg(feature = "image")] @@ -837,9 +845,12 @@ impl PngCompressionLevel { /// Converts the [PngCompressionLevel] to a [png::Compression] enum. pub fn to_compression(&self) -> png::Compression { match self { - PngCompressionLevel::Default => png::Compression::Default, + PngCompressionLevel::NoCompression => png::Compression::NoCompression, + PngCompressionLevel::Fastest => png::Compression::Fastest, + PngCompressionLevel::Default => png::Compression::Balanced, PngCompressionLevel::Fast => png::Compression::Fast, - PngCompressionLevel::Best => png::Compression::Best, + PngCompressionLevel::Best => png::Compression::High, + PngCompressionLevel::High => png::Compression::High, } } } diff --git a/src/utils/img.rs b/src/utils/img.rs index 13cc87f..950aa53 100644 --- a/src/utils/img.rs +++ b/src/utils/img.rs @@ -1,4 +1,5 @@ //! Image Utilities +use crate::ext::io::*; use crate::types::*; use anyhow::Result; @@ -288,8 +289,8 @@ pub fn encode_img( } /// Loads a PNG image from the given reader and returns its data. -pub fn load_png(data: R) -> Result { - let decoder = png::Decoder::new(data); +pub fn load_png(data: R) -> Result { + let decoder = png::Decoder::new(std::io::BufReader::new(data)); let mut reader = decoder.read_info()?; let bit_depth = match reader.info().bit_depth { png::BitDepth::One => 1, @@ -360,7 +361,8 @@ pub fn decode_img(typ: ImageOutputType, filename: &str) -> Result { match typ { ImageOutputType::Png => { let file = crate::utils::files::read_file(filename)?; - load_png(&file[..]) + let reader = MemReader::new(file); + load_png(reader) } #[cfg(feature = "image-jpg")] ImageOutputType::Jpg => {