From c3c37feb33b146f1eb7c76e8ad1ca706e99cb828 Mon Sep 17 00:00:00 2001 From: Hector Villarreal Date: Sat, 31 Oct 2026 05:08:21 -0600 Subject: [PATCH 1/3] A 1-bit TIFF opens in Bitmap mode, as in Photoshop (#10480) A flat TIFF whose page has one 1-bit black-and-white sample per pixel (PhotometricInterpretation 0 or 1) now opens in Bitmap mode instead of Grayscale, as Photoshop opens it. The pixels are kept as gray, as a Bitmap PSD already opens. Co-Authored-By: Claude Opus 5.5 --- .../tests/proptest-regressions/malformed.txt | 7 ++ crates/io/src/flat.rs | 67 ++++++++++++++++++- 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 crates/codecs/tests/proptest-regressions/malformed.txt diff --git a/crates/codecs/tests/proptest-regressions/malformed.txt b/crates/codecs/tests/proptest-regressions/malformed.txt new file mode 100644 index 0000000000..d7b093a926 --- /dev/null +++ b/crates/codecs/tests/proptest-regressions/malformed.txt @@ -0,0 +1,7 @@ +# Seeds for failure cases proptest has generated in the past. It is +# automatically read and these particular cases re-run before any +# novel cases are generated. +# +# It is recommended to check this file in to source control so that +# everyone who runs the test benefits from these saved cases. +cc 21943bbd236f11391c857b5be8700e74396690710402e768dd0f091dbb066616 # shrinks to which = 0, pos = 0, val = 0, cut = 0 diff --git a/crates/io/src/flat.rs b/crates/io/src/flat.rs index f56052d9a9..28cac342eb 100644 --- a/crates/io/src/flat.rs +++ b/crates/io/src/flat.rs @@ -367,7 +367,14 @@ pub fn import_tiff_page_with_options( drop(bands); return import_tiff_with_extras(name, bytes, page, opts, ctl); } - tiff_to_document(name, bands.upright(), crate::memory::available, BAND_BYTES, ctl) + let mut r = tiff_to_document(name, bands.upright(), crate::memory::available, BAND_BYTES, ctl)?; + // A 1-bit black-and-white TIFF opens in Bitmap mode, as in Photoshop (#10480); like a + // Bitmap PSD, its pixels are kept as gray. Its one sample has no extra samples beside it, + // so it always opens here. + if r.document.mode == ColorMode::Grayscale && tiff_is_bilevel(bytes, page) { + r.document.mode = ColorMode::Bitmap; + } + Ok(r) } /// A layered TIFF page: its layer tag and resources are read into a plan, the plan's estimate is @@ -467,6 +474,15 @@ fn import_tiff_with_extras( image_to_document(name, &img.oriented(orientation)?) } +/// Whether the TIFF page that opens (`page`, else the default one) is bilevel: one 1-bit sample +/// per pixel, black-and-white (PhotometricInterpretation 0 or 1). +fn tiff_is_bilevel(bytes: &[u8], page: Option) -> bool { + let Ok(info) = codecs::tiff_info(bytes) else { return false }; + page.or_else(|| info.default_page()) + .and_then(|i| info.pages.get(i)) + .is_some_and(|p| p.bits_per_sample == 1 && p.samples_per_pixel == 1 && matches!(p.photometric, Some(0 | 1))) +} + /// A flat TIFF page as a single-layer document, decoded a group of rows at a time into the /// layer's tiles: only one group exists beside the document. `available`: the memory this /// process can still use (see [`crate::memory::available`]); `budget`: the source bytes per core @@ -2787,3 +2803,52 @@ mod fused_tile_tests { #[cfg(test)] mod png_stream_tests; + +#[cfg(test)] +mod bilevel_tiff_tests { + use super::*; + + /// A 16×8 uncompressed TIFF with one `bits`-bit sample per pixel, BlackIsZero, rows of + /// alternating stripes. + fn tiff(bits: u16) -> Vec { + let (w, h) = (16u32, 8u32); + let row: Vec = if bits == 1 { vec![0b1010_1010, 0b1111_0000] } else { (0..w).map(|x| if x % 2 == 0 { 255 } else { 0 }).collect() }; + let data: Vec = (0..h).flat_map(|_| row.clone()).collect(); + let entries: [(u16, u16, u32); 9] = + [(256, 3, w), (257, 3, h), (258, 3, u32::from(bits)), (259, 3, 1), (262, 3, 1), (273, 4, 0), (277, 3, 1), (278, 3, h), (279, 4, data.len() as u32)]; + let data_at = 8 + 2 + entries.len() as u32 * 12 + 4; + let mut out = b"II*\0".to_vec(); + out.extend(8u32.to_le_bytes()); + out.extend((entries.len() as u16).to_le_bytes()); + for (tag, ty, value) in entries { + let value = if tag == 273 { data_at } else { value }; + out.extend(tag.to_le_bytes()); + out.extend(ty.to_le_bytes()); + out.extend(1u32.to_le_bytes()); + if ty == 3 { + out.extend((value as u16).to_le_bytes()); + out.extend([0, 0]); + } else { + out.extend(value.to_le_bytes()); + } + } + out.extend(0u32.to_le_bytes()); + out.extend(data); + out + } + + /// #10480: Photoshop 2025 opens a 1-bit TIFF in Bitmap mode (`DocumentMode.BITMAP`, 1 bit); + /// it opened as Grayscale. An 8-bit gray TIFF stays Grayscale. + #[test] + fn a_one_bit_tiff_opens_in_bitmap_mode() { + let r = import_flat("bilevel.tif", &tiff(1)).unwrap(); + assert_eq!(r.document.mode, ColorMode::Bitmap); + let gray = import_flat("gray.tif", &tiff(8)).unwrap(); + assert_eq!(gray.document.mode, ColorMode::Grayscale); + // The same black and white pixels either way. + let px = |r: &ImportResult, x: i32| r.document.layers[0].surface().unwrap().pixel(x, 3)[0]; + for x in 0..8 { + assert_eq!(px(&r, x), px(&gray, x), "x={x}"); + } + } +} From b4861b787049731bd7b9c381d9e519d27c43c1c7 Mon Sep 17 00:00:00 2001 From: Hector Villarreal Date: Sat, 31 Oct 2026 12:53:14 -0600 Subject: [PATCH 2/3] Drop a stray proptest seed from an unrelated codecs run crates/codecs/tests/proptest-regressions/malformed.txt came along with the first commit from a local run of the codecs malformed-input proptests; it has nothing to do with 1-bit TIFFs and those tests pass without it. Co-Authored-By: Claude Opus 5.5 --- crates/codecs/tests/proptest-regressions/malformed.txt | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 crates/codecs/tests/proptest-regressions/malformed.txt diff --git a/crates/codecs/tests/proptest-regressions/malformed.txt b/crates/codecs/tests/proptest-regressions/malformed.txt deleted file mode 100644 index d7b093a926..0000000000 --- a/crates/codecs/tests/proptest-regressions/malformed.txt +++ /dev/null @@ -1,7 +0,0 @@ -# Seeds for failure cases proptest has generated in the past. It is -# automatically read and these particular cases re-run before any -# novel cases are generated. -# -# It is recommended to check this file in to source control so that -# everyone who runs the test benefits from these saved cases. -cc 21943bbd236f11391c857b5be8700e74396690710402e768dd0f091dbb066616 # shrinks to which = 0, pos = 0, val = 0, cut = 0 From 1478078168084c0a6b324586bcb90a8ad7d7ea34 Mon Sep 17 00:00:00 2001 From: Hector Villarreal Date: Sat, 31 Oct 2026 21:53:28 -0600 Subject: [PATCH 3/3] Tests: min-is-white and second-page 1-bit TIFFs open in Bitmap mode (FredApps review) Co-Authored-By: Claude Opus 5.5 --- crates/io/src/flat.rs | 93 ++++++++++++++++++++++++++++++++----------- 1 file changed, 70 insertions(+), 23 deletions(-) diff --git a/crates/io/src/flat.rs b/crates/io/src/flat.rs index 28cac342eb..c0edb9233e 100644 --- a/crates/io/src/flat.rs +++ b/crates/io/src/flat.rs @@ -2808,32 +2808,51 @@ mod png_stream_tests; mod bilevel_tiff_tests { use super::*; - /// A 16×8 uncompressed TIFF with one `bits`-bit sample per pixel, BlackIsZero, rows of - /// alternating stripes. - fn tiff(bits: u16) -> Vec { + /// A 16×8 uncompressed TIFF with one `bits`-bit sample per pixel, PhotometricInterpretation + /// `photometric` (0 WhiteIsZero, 1 BlackIsZero), rows of alternating stripes. + fn tiff(bits: u16, photometric: u16) -> Vec { + tiff_pages(&[(bits, photometric)]) + } + + /// A TIFF with one 16×8 page per `(bits, photometric)`, chained through the IFDs' next + /// offsets: each page's IFD is followed by its strip. + fn tiff_pages(pages: &[(u16, u16)]) -> Vec { let (w, h) = (16u32, 8u32); - let row: Vec = if bits == 1 { vec![0b1010_1010, 0b1111_0000] } else { (0..w).map(|x| if x % 2 == 0 { 255 } else { 0 }).collect() }; - let data: Vec = (0..h).flat_map(|_| row.clone()).collect(); - let entries: [(u16, u16, u32); 9] = - [(256, 3, w), (257, 3, h), (258, 3, u32::from(bits)), (259, 3, 1), (262, 3, 1), (273, 4, 0), (277, 3, 1), (278, 3, h), (279, 4, data.len() as u32)]; - let data_at = 8 + 2 + entries.len() as u32 * 12 + 4; let mut out = b"II*\0".to_vec(); out.extend(8u32.to_le_bytes()); - out.extend((entries.len() as u16).to_le_bytes()); - for (tag, ty, value) in entries { - let value = if tag == 273 { data_at } else { value }; - out.extend(tag.to_le_bytes()); - out.extend(ty.to_le_bytes()); - out.extend(1u32.to_le_bytes()); - if ty == 3 { - out.extend((value as u16).to_le_bytes()); - out.extend([0, 0]); - } else { - out.extend(value.to_le_bytes()); + for (i, &(bits, photometric)) in pages.iter().enumerate() { + let row: Vec = if bits == 1 { vec![0b1010_1010, 0b1111_0000] } else { (0..w).map(|x| if x % 2 == 0 { 255 } else { 0 }).collect() }; + let data: Vec = (0..h).flat_map(|_| row.clone()).collect(); + let entries: [(u16, u16, u32); 9] = [ + (256, 3, w), + (257, 3, h), + (258, 3, u32::from(bits)), + (259, 3, 1), + (262, 3, u32::from(photometric)), + (273, 4, 0), + (277, 3, 1), + (278, 3, h), + (279, 4, data.len() as u32), + ]; + let ifd_at = out.len() as u32; + let data_at = ifd_at + 2 + entries.len() as u32 * 12 + 4; + let next_ifd = if i + 1 < pages.len() { data_at + data.len() as u32 } else { 0 }; + out.extend((entries.len() as u16).to_le_bytes()); + for (tag, ty, value) in entries { + let value = if tag == 273 { data_at } else { value }; + out.extend(tag.to_le_bytes()); + out.extend(ty.to_le_bytes()); + out.extend(1u32.to_le_bytes()); + if ty == 3 { + out.extend((value as u16).to_le_bytes()); + out.extend([0, 0]); + } else { + out.extend(value.to_le_bytes()); + } } + out.extend(next_ifd.to_le_bytes()); + out.extend(data); } - out.extend(0u32.to_le_bytes()); - out.extend(data); out } @@ -2841,9 +2860,9 @@ mod bilevel_tiff_tests { /// it opened as Grayscale. An 8-bit gray TIFF stays Grayscale. #[test] fn a_one_bit_tiff_opens_in_bitmap_mode() { - let r = import_flat("bilevel.tif", &tiff(1)).unwrap(); + let r = import_flat("bilevel.tif", &tiff(1, 1)).unwrap(); assert_eq!(r.document.mode, ColorMode::Bitmap); - let gray = import_flat("gray.tif", &tiff(8)).unwrap(); + let gray = import_flat("gray.tif", &tiff(8, 1)).unwrap(); assert_eq!(gray.document.mode, ColorMode::Grayscale); // The same black and white pixels either way. let px = |r: &ImportResult, x: i32| r.document.layers[0].surface().unwrap().pixel(x, 3)[0]; @@ -2851,4 +2870,32 @@ mod bilevel_tiff_tests { assert_eq!(px(&r, x), px(&gray, x), "x={x}"); } } + + /// A WhiteIsZero (min-is-white) 1-bit TIFF also opens in Bitmap mode, with a 0 bit white and + /// a 1 bit black: the same bits as the BlackIsZero file, inverted. + #[test] + fn a_min_is_white_one_bit_tiff_opens_in_bitmap_mode_inverted() { + let miw = import_flat("min-is-white.tif", &tiff(1, 0)).unwrap(); + assert_eq!(miw.document.mode, ColorMode::Bitmap); + let mib = import_flat("min-is-black.tif", &tiff(1, 1)).unwrap(); + let px = |r: &ImportResult, x: i32| r.document.layers[0].surface().unwrap().pixel(x, 3)[0]; + // Row bits 1010_1010 1111_0000: a 1 bit at even x in the first byte, then four 1s, four 0s. + for x in 0..16 { + let bit = if x < 8 { x % 2 == 0 } else { x < 12 }; + let white = if bit { 0.0 } else { 1.0 }; + assert_eq!(px(&miw, x), white, "x={x}: bit {}", u8::from(bit)); + assert_eq!(px(&miw, x), 1.0 - px(&mib, x), "x={x}"); + } + } + + /// The Bitmap check reads the page that opens: a second page that is bilevel opens in Bitmap + /// mode, while the 8-bit gray first page stays Grayscale. + #[test] + fn a_bilevel_second_page_opens_in_bitmap_mode() { + let bytes = tiff_pages(&[(8, 1), (1, 1)]); + let second = import_tiff_page("pages.tif", &bytes, Some(1)).unwrap(); + assert_eq!(second.document.mode, ColorMode::Bitmap); + let first = import_tiff_page("pages.tif", &bytes, Some(0)).unwrap(); + assert_eq!(first.document.mode, ColorMode::Grayscale); + } }