From 7b6ee246510e5430b561600cd6ab4a53382c232a Mon Sep 17 00:00:00 2001 From: Sophie Herold Date: Thu, 20 Apr 2023 16:18:09 +0200 Subject: [PATCH] decoder: Only warn about invalid ICC profile Before the image failed loading Part-of: --- src/decoder/formats/heif.rs | 14 ++++++++++---- src/decoder/formats/image_rs_other.rs | 14 ++++++++++---- src/util/mod.rs | 13 ++++++++++++- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/decoder/formats/heif.rs b/src/decoder/formats/heif.rs index d526b34..7e31b80 100644 --- a/src/decoder/formats/heif.rs +++ b/src/decoder/formats/heif.rs @@ -190,11 +190,17 @@ impl<'a> Decoded<'a> { if let Some(icc_profile) = &self.icc_profile { if memory_format == gdk::MemoryFormat::R8g8b8 { - util::new_trafo::(icc_profile, lcms2::PixelFormat::RGB_8)? - .transform_in_place(buffer.as_pixels_mut()); + util::appply_icc_profile::( + icc_profile, + lcms2::PixelFormat::RGB_8, + buffer.as_pixels_mut(), + ); } else if memory_format == gdk::MemoryFormat::R8g8b8a8 { - util::new_trafo::(icc_profile, lcms2::PixelFormat::RGBA_8)? - .transform_in_place(buffer.as_pixels_mut()); + util::appply_icc_profile::( + icc_profile, + lcms2::PixelFormat::RGBA_8, + buffer.as_pixels_mut(), + ); } } diff --git a/src/decoder/formats/image_rs_other.rs b/src/decoder/formats/image_rs_other.rs index 57d757c..15a46e4 100644 --- a/src/decoder/formats/image_rs_other.rs +++ b/src/decoder/formats/image_rs_other.rs @@ -297,8 +297,11 @@ impl Decoded { } image_rs::DynamicImage::ImageRgb8(mut buffer) => { if let Some(icc_profile) = &self.icc_profile { - util::new_trafo::(icc_profile, lcms2::PixelFormat::RGB_8)? - .transform_in_place(buffer.as_pixels_mut()); + util::appply_icc_profile::( + icc_profile, + lcms2::PixelFormat::RGB_8, + buffer.as_pixels_mut(), + ); } ( 1, @@ -309,8 +312,11 @@ impl Decoded { } image_rs::DynamicImage::ImageRgba8(mut buffer) => { if let Some(icc_profile) = &self.icc_profile { - util::new_trafo::(icc_profile, lcms2::PixelFormat::RGBA_8)? - .transform_in_place(buffer.as_pixels_mut()); + util::appply_icc_profile::( + icc_profile, + lcms2::PixelFormat::RGBA_8, + buffer.as_pixels_mut(), + ); } ( 1, diff --git a/src/util/mod.rs b/src/util/mod.rs index 1655148..ca6db2a 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -251,7 +251,7 @@ pub fn new_trafo( icc_profile: &[u8], src_format: lcms2::PixelFormat, ) -> anyhow::Result> { - let src_profile = lcms2::Profile::new_icc(icc_profile)?; + let src_profile = lcms2::Profile::new_icc(icc_profile).context("Invalid ICC profile")?; let target_profile = lcms2::Profile::new_srgb(); @@ -263,3 +263,14 @@ pub fn new_trafo( lcms2::Intent::Perceptual, )?) } + +pub fn appply_icc_profile( + icc_profile: &[u8], + src_format: lcms2::PixelFormat, + buf: &mut [F], +) { + match new_trafo(icc_profile, src_format) { + Ok(trafo) => trafo.transform_in_place(buf), + Err(err) => log::warn!("Failed to apply ICC profile: {err}"), + } +}