decoder: Only warn about invalid ICC profile

Before the image failed loading

Part-of: <https://gitlab.gnome.org/Incubator/loupe/-/merge_requests/190>
This commit is contained in:
Sophie Herold 2023-04-20 16:18:09 +02:00 • committed by Marge Bot
parent 670329cd95
commit 7b6ee24651
3 changed files with 32 additions and 9 deletions

View file

@ -190,11 +190,17 @@ impl<'a> Decoded<'a> {
if let Some(icc_profile) = &self.icc_profile { if let Some(icc_profile) = &self.icc_profile {
if memory_format == gdk::MemoryFormat::R8g8b8 { if memory_format == gdk::MemoryFormat::R8g8b8 {
util::new_trafo::<rgb::RGB8>(icc_profile, lcms2::PixelFormat::RGB_8)? util::appply_icc_profile::<rgb::RGB8>(
.transform_in_place(buffer.as_pixels_mut()); icc_profile,
lcms2::PixelFormat::RGB_8,
buffer.as_pixels_mut(),
);
} else if memory_format == gdk::MemoryFormat::R8g8b8a8 { } else if memory_format == gdk::MemoryFormat::R8g8b8a8 {
util::new_trafo::<rgb::RGBA8>(icc_profile, lcms2::PixelFormat::RGBA_8)? util::appply_icc_profile::<rgb::RGBA8>(
.transform_in_place(buffer.as_pixels_mut()); icc_profile,
lcms2::PixelFormat::RGBA_8,
buffer.as_pixels_mut(),
);
} }
} }

View file

@ -297,8 +297,11 @@ impl Decoded {
} }
image_rs::DynamicImage::ImageRgb8(mut buffer) => { image_rs::DynamicImage::ImageRgb8(mut buffer) => {
if let Some(icc_profile) = &self.icc_profile { if let Some(icc_profile) = &self.icc_profile {
util::new_trafo::<rgb::RGB8>(icc_profile, lcms2::PixelFormat::RGB_8)? util::appply_icc_profile::<rgb::RGB8>(
.transform_in_place(buffer.as_pixels_mut()); icc_profile,
lcms2::PixelFormat::RGB_8,
buffer.as_pixels_mut(),
);
} }
( (
1, 1,
@ -309,8 +312,11 @@ impl Decoded {
} }
image_rs::DynamicImage::ImageRgba8(mut buffer) => { image_rs::DynamicImage::ImageRgba8(mut buffer) => {
if let Some(icc_profile) = &self.icc_profile { if let Some(icc_profile) = &self.icc_profile {
util::new_trafo::<rgb::RGBA8>(icc_profile, lcms2::PixelFormat::RGBA_8)? util::appply_icc_profile::<rgb::RGBA8>(
.transform_in_place(buffer.as_pixels_mut()); icc_profile,
lcms2::PixelFormat::RGBA_8,
buffer.as_pixels_mut(),
);
} }
( (
1, 1,

View file

@ -251,7 +251,7 @@ pub fn new_trafo<F: Copy>(
icc_profile: &[u8], icc_profile: &[u8],
src_format: lcms2::PixelFormat, src_format: lcms2::PixelFormat,
) -> anyhow::Result<lcms2::Transform<F, F>> { ) -> anyhow::Result<lcms2::Transform<F, F>> {
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(); let target_profile = lcms2::Profile::new_srgb();
@ -263,3 +263,14 @@ pub fn new_trafo<F: Copy>(
lcms2::Intent::Perceptual, lcms2::Intent::Perceptual,
)?) )?)
} }
pub fn appply_icc_profile<F: Copy>(
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}"),
}
}