Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Integration: CMYK TIFFs with alpha report their pixel size (from #830)

aiongg258fd2d7c9e0
diff --git a/crates/images/src/lib.rs b/crates/images/src/lib.rs
index 61a454f59..2aa9892f2 100644
--- a/crates/images/src/lib.rs
+++ b/crates/images/src/lib.rs
@@ -221,7 +221,13 @@ pub fn pixel_size(bytes: &[u8]) -> Option<(u32, u32)> {
let be = |i: usize| u32::from_be_bytes([bytes[i], bytes[i + 1], bytes[i + 2], bytes[i + 3]]);
return Some((be(18), be(14)));
}
- image::ImageReader::new(std::io::Cursor::new(bytes)).with_guessed_format().ok()?.into_dimensions().ok()
+ image::ImageReader::new(std::io::Cursor::new(bytes))
+ .with_guessed_format()
+ .ok()?
+ .into_dimensions()
+ .ok()
+ // `image` doesn't read five-sample CMYK with alpha.
+ .or_else(|| tiff_decoder(bytes)?.dimensions().ok())
}
/// Straight (non-premultiplied) RGBA8 of a raster in display sRGB: an embedded ICC profile is
@@ -564,6 +570,13 @@ mod tests {
assert_eq!(decode_cmyk_tiff(&b[..b.len() / 2]), None, "truncated");
}
+ #[test]
+ fn cmyk_tiff_with_alpha_reports_its_pixel_size() {
+ use tiff::encoder::colortype::CMYKA8;
+ let b = tiff_of::<CMYKA8>(2, 1, &[0, 0, 0, 255, 255, 0, 0, 0, 0, 0], true);
+ assert_eq!(pixel_size(&b), Some((2, 1)));
+ }
+
#[test]
fn bmp_decodes() {
let mut bmp = Vec::new();