PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
XFA: turn TIFF pictures by their Orientation tag
Marco Arnulfo947f0f2994b0
diff --git a/crates/xfa/src/image.rs b/crates/xfa/src/image.rsindex 7af9c0cb3..5ec42873e 100644--- a/crates/xfa/src/image.rs+++ b/crates/xfa/src/image.rs@@ -71,10 +71,31 @@ fn header_size(data: &[u8]) -> Option<(u32, u32)> {}// The decoders read only the headers (TIFF: the first IFD) until asked for pixels.Kind::Bmp => reader(data, image::ImageFormat::Bmp).into_dimensions().ok().filter(|&(w, h)| w > 0 && h > 0),- Kind::Tiff => reader(data, image::ImageFormat::Tiff).into_dimensions().ok().filter(|&(w, h)| w > 0 && h > 0),+ Kind::Tiff => {+ use image::{ImageDecoder, metadata::Orientation};+ let mut decoder = reader(data, image::ImageFormat::Tiff).into_decoder().ok()?;+ let (w, h) = decoder.dimensions();+ // Shown turned a quarter: the size viewers show is the stored one swapped.+ let turned = matches!(+ decoder.orientation().ok()?,+ Orientation::Rotate90 | Orientation::Rotate270 | Orientation::Rotate90FlipH | Orientation::Rotate270FlipH+ );+ let (w, h) = if turned { (h, w) } else { (w, h) };+ (w > 0 && h > 0).then_some((w, h))+ }}}+/// A TIFF's first page as viewers show it: turned and flipped as its Orientation tag says.+fn decode_tiff(data: &[u8]) -> image::ImageResult<image::DynamicImage> {+ use image::ImageDecoder;+ let mut decoder = reader(data, image::ImageFormat::Tiff).into_decoder()?;+ let orientation = decoder.orientation()?;+ let mut img = image::DynamicImage::from_decoder(decoder)?;+ img.apply_orientation(orientation);+ Ok(img)+}+/// A decoder told the limits, so a frame larger than its header claims is refused before it is/// allocated.fn reader(data: &[u8], format: image::ImageFormat) -> image::ImageReader<std::io::Cursor<&[u8]>> {@@ -153,8 +174,8 @@ pub fn decode(data: &[u8]) -> Result<Decoded, String> {Kind::Bmp => image::ImageFormat::Bmp,_ => image::ImageFormat::Tiff,};- let img =- reader(data, format).decode().map_err(|e| if matches!(e, image::ImageError::Limits(_)) { too_big() } else { bad(e.to_string()) })?;+ let img = if k == Kind::Tiff { decode_tiff(data) } else { reader(data, format).decode() }+ .map_err(|e| if matches!(e, image::ImageError::Limits(_)) { too_big() } else { bad(e.to_string()) })?;let rgba = img.into_rgba8();let (width, height) = rgba.dimensions();if !fits(width, height) {diff --git a/crates/xfa/src/tests.rs b/crates/xfa/src/tests.rsindex cfae8f42f..3e4a16e9b 100644--- a/crates/xfa/src/tests.rs+++ b/crates/xfa/src/tests.rs@@ -2117,3 +2117,40 @@ fn som_indices_count_earlier_same_named_siblings() {let sibs = SomIndices::of(&children);assert_eq!((0..=children.len()).map(|i| sibs.at(i)).collect::<Vec<_>>(), [0, 0, 1, 0, 2, 1, 0]);}++/// A TIFF is drawn as viewers show it, turned by its Orientation tag: a 2 × 1 picture (red,+/// blue) tagged 6 (turn 90° clockwise) is 1 × 2, red above blue, in layout and in the PDF.+#[test]+fn a_tiff_picture_is_turned_by_its_orientation_tag() {+ let strip: &[u8] = &[255, 0, 0, 0, 0, 255];+ let (bps_at, strip_at) = (8 + 2 + 10 * 12 + 4, 8 + 2 + 10 * 12 + 4 + 6);+ let mut b = b"II*\0".to_vec();+ b.extend_from_slice(&8u32.to_le_bytes());+ b.extend_from_slice(&10u16.to_le_bytes());+ for (tag, ty, count, value) in [+ (256u16, 3u16, 1u32, 2u32),+ (257, 3, 1, 1),+ (258, 3, 3, bps_at as u32),+ (259, 3, 1, 1),+ (262, 3, 1, 2),+ (273, 4, 1, strip_at as u32),+ (274, 3, 1, 6),+ (277, 3, 1, 3),+ (278, 3, 1, 1),+ (279, 4, 1, strip.len() as u32),+ ] {+ b.extend_from_slice(&tag.to_le_bytes());+ b.extend_from_slice(&ty.to_le_bytes());+ b.extend_from_slice(&count.to_le_bytes());+ b.extend_from_slice(&value.to_le_bytes());+ }+ b.extend_from_slice(&0u32.to_le_bytes());+ for _ in 0..3 {+ b.extend_from_slice(&8u16.to_le_bytes());+ }+ b.extend_from_slice(strip);+ let d = crate::image::decode(&b).unwrap();+ assert_eq!(crate::image::size(&b), Some((1, 2)), "layout size");+ assert_eq!((d.width, d.height), (1, 2), "decoded size");+ assert_eq!(d.samples, vec![255, 0, 0, 0, 0, 255]);+}