From 6a25b62d43dfcf3f0d0f76638cdbce6cc511452a Mon Sep 17 00:00:00 2001 From: Tanishq Date: Wed, 3 Apr 2024 15:55:49 +0530 Subject: [PATCH] Fix resizing for images with EXIF orientation (#2468) * Fix resizing for images with EXIF orientation * Added test for asymmetric resize for exif images --------- Co-authored-by: Tanishq --- components/imageproc/src/processor.rs | 5 ++--- components/imageproc/tests/resize_image.rs | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/components/imageproc/src/processor.rs b/components/imageproc/src/processor.rs index 63346663a6..2c6a17b725 100644 --- a/components/imageproc/src/processor.rs +++ b/components/imageproc/src/processor.rs @@ -38,7 +38,8 @@ impl ImageOp { return Ok(()); } - let mut img = image::open(&self.input_path)?; + let img = image::open(&self.input_path)?; + let mut img = fix_orientation(&img, &self.input_path).unwrap_or(img); let img = match self.instr.crop_instruction { Some((x, y, w, h)) => img.crop(x, y, w, h), @@ -49,8 +50,6 @@ impl ImageOp { None => img, }; - let img = fix_orientation(&img, &self.input_path).unwrap_or(img); - let f = File::create(&self.output_path)?; let mut buffered_f = BufWriter::new(f); diff --git a/components/imageproc/tests/resize_image.rs b/components/imageproc/tests/resize_image.rs index c52ff9321e..41114abf50 100644 --- a/components/imageproc/tests/resize_image.rs +++ b/components/imageproc/tests/resize_image.rs @@ -248,3 +248,25 @@ fn check_img(img: DynamicImage) -> bool { // bottom right is white && img.get_pixel(15, 15).channels() == [255, 255, 255, 255] } + +#[test] +fn asymmetric_resize_with_exif_orientations() { + // No exif metadata + image_op_test("exif_0.jpg", "scale", Some(16), Some(32), "auto", "jpg", 16, 32, 16, 16); + // 1: Horizontal (normal) + image_op_test("exif_1.jpg", "scale", Some(16), Some(32), "auto", "jpg", 16, 32, 16, 16); + // 2: Mirror horizontal + image_op_test("exif_2.jpg", "scale", Some(16), Some(32), "auto", "jpg", 16, 32, 16, 16); + // 3: Rotate 180 + image_op_test("exif_3.jpg", "scale", Some(16), Some(32), "auto", "jpg", 16, 32, 16, 16); + // 4: Mirror vertical + image_op_test("exif_4.jpg", "scale", Some(16), Some(32), "auto", "jpg", 16, 32, 16, 16); + // 5: Mirror horizontal and rotate 270 CW + image_op_test("exif_5.jpg", "scale", Some(16), Some(32), "auto", "jpg", 16, 32, 16, 16); + // 6: Rotate 90 CW + image_op_test("exif_6.jpg", "scale", Some(16), Some(32), "auto", "jpg", 16, 32, 16, 16); + // 7: Mirror horizontal and rotate 90 CW + image_op_test("exif_7.jpg", "scale", Some(16), Some(32), "auto", "jpg", 16, 32, 16, 16); + // 8: Rotate 270 CW + image_op_test("exif_8.jpg", "scale", Some(16), Some(32), "auto", "jpg", 16, 32, 16, 16); +}