Add support for png 0.18

This commit is contained in:
2025-09-11 15:40:26 +08:00
parent 600ffc9c83
commit 8df08f487b
2 changed files with 24 additions and 11 deletions

View File

@@ -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,
}
}
}

View File

@@ -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<R: std::io::Read>(data: R) -> Result<ImageData> {
let decoder = png::Decoder::new(data);
pub fn load_png<R: std::io::Read + std::io::Seek>(data: R) -> Result<ImageData> {
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<ImageData> {
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 => {