From c912159261fb47f59b6d19408bbb69e4d460b10f Mon Sep 17 00:00:00 2001 From: Matteo Paonessa Date: Thu, 10 Oct 2024 21:24:35 +0200 Subject: [PATCH] Progressive/baseline JPEG switch while optimizing --- src/jpeg.rs | 14 +++++++++----- src/lib.rs | 8 ++++---- src/parameters.rs | 8 +++++++- src/webp.rs | 10 +++++----- tests/convert.rs | 24 ++++++++++++------------ 5 files changed, 37 insertions(+), 27 deletions(-) diff --git a/src/jpeg.rs b/src/jpeg.rs index 4199a5d..6de5a29 100644 --- a/src/jpeg.rs +++ b/src/jpeg.rs @@ -4,6 +4,7 @@ use std::io::Write; use std::mem; use std::panic::catch_unwind; use std::ptr::null; +use std::sync::atomic::{AtomicI32, Ordering}; use image::ImageFormat::Jpeg; use img_parts::{ImageEXIF, ImageICC}; use img_parts::jpeg::Jpeg as PartsJpeg; @@ -15,7 +16,7 @@ use crate::error::CaesiumError; use crate::parameters::ChromaSubsampling; use crate::resize::resize; -static mut JPEG_ERROR: c_int = 0; +static JPEG_ERROR: AtomicI32 = AtomicI32::new(0); pub fn compress( input_path: String, @@ -63,7 +64,7 @@ pub fn compress_in_memory( }) .unwrap_or_else(|_| { Err(CaesiumError { - message: format!("Internal JPEG error: {}", JPEG_ERROR), + message: format!("Internal JPEG error: {}", JPEG_ERROR.load(Ordering::SeqCst)), code: 20104, }) }) @@ -107,6 +108,9 @@ unsafe fn lossless(in_file: Vec, parameters: &CSParameters) -> Result Self { + Self::new() + } +} + impl CSParameters { pub fn new() -> CSParameters { initialize_parameters() } } -pub fn initialize_parameters() -> CSParameters { +fn initialize_parameters() -> CSParameters { let jpeg = JpegParameters { quality: 80, chroma_subsampling: ChromaSubsampling::Auto, diff --git a/src/webp.rs b/src/webp.rs index 53bb1eb..28c85b1 100644 --- a/src/webp.rs +++ b/src/webp.rs @@ -59,7 +59,7 @@ pub fn compress_in_memory( message: e.to_string(), code: 20306, })? - .map_or((None, None), |dimg| (dimg.icc_profile(), dimg.exif())); + .map_or((None, None), |dyn_img| (dyn_img.icc_profile(), dyn_img.exif())); } let must_resize = parameters.width > 0 || parameters.height > 0; @@ -171,13 +171,13 @@ pub fn compress_in_memory( if iccp.is_some() || exif.is_some() { let mut image_with_metadata: Vec = vec![]; - let mut dimg = match PartsWebp::from_bytes(encoded_image.clone().into()) { + let mut dyn_img = match PartsWebp::from_bytes(encoded_image.clone().into()) { Ok(d) => d, Err(_) => return Ok(encoded_image) }; - dimg.set_icc_profile(iccp); - dimg.set_exif(exif); - dimg.encoder() + dyn_img.set_icc_profile(iccp); + dyn_img.set_exif(exif); + dyn_img.encoder() .write_to(&mut image_with_metadata) .map_err(|e| CaesiumError { message: e.to_string(), diff --git a/tests/convert.rs b/tests/convert.rs index 24ebe49..ef2d1e5 100644 --- a/tests/convert.rs +++ b/tests/convert.rs @@ -27,7 +27,7 @@ fn convert_jpg_to_png() { String::from(output), ¶ms, SupportedFileTypes::Png).expect("Image converted successfully"); - assert!(std::path::Path::new(output).exists()); + assert!(Path::new(output).exists()); assert_eq!( infer::get_from_path(output).unwrap().unwrap().mime_type(), "image/png" @@ -48,7 +48,7 @@ fn convert_jpg_to_webp() { String::from(output), ¶ms, SupportedFileTypes::WebP).expect("Image converted successfully"); - assert!(std::path::Path::new(output).exists()); + assert!(Path::new(output).exists()); assert_eq!( infer::get_from_path(output).unwrap().unwrap().mime_type(), "image/webp" @@ -65,7 +65,7 @@ fn convert_jpg_to_tiff() { String::from(output), ¶ms, SupportedFileTypes::Tiff).expect("Image converted successfully"); - assert!(std::path::Path::new(output).exists()); + assert!(Path::new(output).exists()); assert_eq!( infer::get_from_path(output).unwrap().unwrap().mime_type(), "image/tiff" @@ -83,7 +83,7 @@ fn convert_png_to_jpg() { String::from(output), ¶ms, SupportedFileTypes::Jpeg).expect("Image converted successfully"); - assert!(std::path::Path::new(output).exists()); + assert!(Path::new(output).exists()); assert_eq!( infer::get_from_path(output).unwrap().unwrap().mime_type(), "image/jpeg" @@ -104,7 +104,7 @@ fn convert_png_to_webp() { String::from(output), ¶ms, SupportedFileTypes::WebP).expect("Image converted successfully"); - assert!(std::path::Path::new(output).exists()); + assert!(Path::new(output).exists()); assert_eq!( infer::get_from_path(output).unwrap().unwrap().mime_type(), "image/webp" @@ -121,7 +121,7 @@ fn convert_png_to_tiff() { String::from(output), ¶ms, SupportedFileTypes::Tiff).expect("Image converted successfully"); - assert!(std::path::Path::new(output).exists()); + assert!(Path::new(output).exists()); assert_eq!( infer::get_from_path(output).unwrap().unwrap().mime_type(), "image/tiff" @@ -139,7 +139,7 @@ fn convert_webp_to_jpg() { String::from(output), ¶ms, SupportedFileTypes::Jpeg).expect("Image converted successfully"); - assert!(std::path::Path::new(output).exists()); + assert!(Path::new(output).exists()); assert_eq!( infer::get_from_path(output).unwrap().unwrap().mime_type(), "image/jpeg" @@ -160,7 +160,7 @@ fn convert_webp_to_png() { String::from(output), ¶ms, SupportedFileTypes::Png).expect("Image converted successfully"); - assert!(std::path::Path::new(output).exists()); + assert!(Path::new(output).exists()); assert_eq!( infer::get_from_path(output).unwrap().unwrap().mime_type(), "image/png" @@ -177,7 +177,7 @@ fn convert_webp_to_tiff() { String::from(output), ¶ms, SupportedFileTypes::Tiff).expect("Image converted successfully"); - assert!(std::path::Path::new(output).exists()); + assert!(Path::new(output).exists()); assert_eq!( infer::get_from_path(output).unwrap().unwrap().mime_type(), "image/tiff" @@ -194,7 +194,7 @@ fn convert_tiff_to_jpg() { String::from(output), ¶ms, SupportedFileTypes::Jpeg).expect("Image converted successfully"); - assert!(std::path::Path::new(output).exists()); + assert!(Path::new(output).exists()); assert_eq!( infer::get_from_path(output).unwrap().unwrap().mime_type(), "image/jpeg" @@ -211,7 +211,7 @@ fn convert_tiff_to_png() { String::from(output), ¶ms, SupportedFileTypes::Png).expect("Image converted successfully"); - assert!(std::path::Path::new(output).exists()); + assert!(Path::new(output).exists()); assert_eq!( infer::get_from_path(output).unwrap().unwrap().mime_type(), "image/png" @@ -228,7 +228,7 @@ fn convert_tiff_to_webp() { String::from(output), ¶ms, SupportedFileTypes::WebP).expect("Image converted successfully"); - assert!(std::path::Path::new(output).exists()); + assert!(Path::new(output).exists()); assert_eq!( infer::get_from_path(output).unwrap().unwrap().mime_type(), "image/webp"