diff --git a/imageflow_core/src/codecs/mozjpeg.rs b/imageflow_core/src/codecs/mozjpeg.rs index 1790fe12..e5432961 100644 --- a/imageflow_core/src/codecs/mozjpeg.rs +++ b/imageflow_core/src/codecs/mozjpeg.rs @@ -33,14 +33,20 @@ pub struct MozjpegEncoder { matte: Option } +const DEFAULT_QUALITY:u8 = 75; + impl MozjpegEncoder { // Quality is in range 0-100 pub(crate) fn create(c: &Context, quality: Option, progressive: Option, matte: Option, io: IoProxy) -> Result { if !c.enabled_codecs.encoders.contains(&crate::codecs::NamedEncoders::MozJpegEncoder){ return Err(nerror!(ErrorKind::CodecDisabledError, "The MozJpeg encoder has been disabled")); } + Ok(MozjpegEncoder { - io, quality, progressive, matte, + io, + quality: Some(u8::min(100,quality.unwrap_or(DEFAULT_QUALITY))), + progressive, + matte, optimize_coding: Some(true), defaults: Defaults::MozJPEG, @@ -49,7 +55,9 @@ impl MozjpegEncoder { pub(crate) fn create_classic(c: &Context, quality: Option, progressive: Option, optimize_coding: Option, matte: Option, io: IoProxy) -> Result { Ok(MozjpegEncoder { - io, quality, progressive, matte, + io, + quality: Some(u8::min(100,quality.unwrap_or(DEFAULT_QUALITY))) + ,progressive, matte, optimize_coding, defaults: Defaults::LibJPEGv6, }) @@ -103,7 +111,9 @@ impl Encoder for MozjpegEncoder { cinfo.set_optimize_coding(o); } - let chroma_quality = self.quality.unwrap_or(75) as f32; // Lower values allow blurrier color + let chroma_quality = self.quality.unwrap_or(DEFAULT_QUALITY) as f32; // Lower values allow blurrier color + + let pixels_buffer = unsafe {frame.pixels_buffer()}.ok_or(nerror!(ErrorKind::BitmapPointerNull))?; let max_sampling = PixelSize{cb:(2,2), cr:(2,2)}; // Set to 1 to force higher res let res = match pixels_buffer { diff --git a/imageflow_core/tests/visuals.rs b/imageflow_core/tests/visuals.rs index c0bd7336..2d653e8a 100644 --- a/imageflow_core/tests/visuals.rs +++ b/imageflow_core/tests/visuals.rs @@ -1087,6 +1087,7 @@ fn test_zoom_with_preshrink() { } + #[test] fn webp_lossless_alpha_decode_and_scale() { let matched = compare(Some(IoTestEnum::Url("https://imageflow-resources.s3-us-west-2.amazonaws.com/test_inputs/1_webp_ll.webp".to_owned())), 500, @@ -1240,6 +1241,37 @@ fn smoke_test_ignore_invalid_color_profile(){ ).unwrap(); } + +#[test] +fn smoke_test_invalid_params(){ + + + let tinypng = vec![0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52, 0x00, + 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x06, 0x00, 0x00, 0x00, 0x1F, 0x15, 0xC4, 0x89, 0x00, + 0x00, 0x00, 0x0A, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9C, 0x63, 0x00, 0x01, 0x00, 0x00, 0x05, 0x00, 0x01, + 0x0D, 0x0A, 0x2D, 0xB4, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82 ]; + + + let steps = vec![ + Node::CommandString{ + kind: CommandStringKind::ImageResizer4, + value: "quality=957".to_owned(), + decode: Some(0), + encode: Some(1), + watermarks: None + } + ]; + + smoke_test(Some(IoTestEnum::ByteArray(tinypng)), + Some(IoTestEnum::OutputBuffer), + None, + DEBUG_GRAPH, + steps, + ).unwrap(); +} + + + #[test] fn test_max_encode_dimensions(){