Skip to content

Commit

Permalink
Clamp jpeg quality values to 0..100
Browse files Browse the repository at this point in the history
  • Loading branch information
lilith committed Dec 23, 2022
1 parent 740b66a commit cae75ff
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
16 changes: 13 additions & 3 deletions imageflow_core/src/codecs/mozjpeg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,20 @@ pub struct MozjpegEncoder {
matte: Option<Color>
}

const DEFAULT_QUALITY:u8 = 75;

impl MozjpegEncoder {
// Quality is in range 0-100
pub(crate) fn create(c: &Context, quality: Option<u8>, progressive: Option<bool>, matte: Option<Color>, io: IoProxy) -> Result<Self> {
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,

Expand All @@ -49,7 +55,9 @@ impl MozjpegEncoder {

pub(crate) fn create_classic(c: &Context, quality: Option<u8>, progressive: Option<bool>, optimize_coding: Option<bool>, matte: Option<Color>, io: IoProxy) -> Result<Self> {
Ok(MozjpegEncoder {
io, quality, progressive, matte,
io,
quality: Some(u8::min(100,quality.unwrap_or(DEFAULT_QUALITY)))
,progressive, matte,
optimize_coding,
defaults: Defaults::LibJPEGv6,
})
Expand Down Expand Up @@ -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 {
Expand Down
32 changes: 32 additions & 0 deletions imageflow_core/tests/visuals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(){

Expand Down

0 comments on commit cae75ff

Please sign in to comment.