fix: handle WhiteIsZero for extended bit depths, add comprehensive tests

Fix invert_colors to support extended bit depths (9-15 promoted to u16,
17-31 promoted to u32). Byte-wise NOT doesn't work for promoted samples
because it flips unused upper bits; use (max - value) instead.

Add regression tests covering the extended depth matrix:
- Gray 10/14/24-bit (contiguous, BlackIsZero)
- RGB 10/14/24-bit (contiguous)
- RGB 10/12/14/24-bit (planar)
- Gray 12-bit with WhiteIsZero (synthetic 4x2 image)
- Gray 12-bit with horizontal predictor (synthetic 4x2 image)
This commit is contained in:
Lilith River committed 2026-03-10 06:02:19 -06:00
1 parent 4ff374990e
commit 29e43d2314
4 files changed
+167

No files matched your search

+17
View File
@@ -740,6 +740,23 @@ fn invert_colors(
*x = !*x;
}
}
(ColorType::Gray(n @ 9..=15), SampleFormat::Uint) => {
// Extended depths promoted to u16: invert within the valid bit range.
// Byte-wise NOT would flip the upper padding bits, so we use (max - v).
let max = (1u16 << n) - 1;
for chunk in buf.as_chunks_mut::<2>().0 {
let v = u16::from_ne_bytes(*chunk);
*chunk = (max - v).to_ne_bytes();
}
}
(ColorType::Gray(n @ 17..=31), SampleFormat::Uint) => {
// Extended depths promoted to u32: invert within the valid bit range.
let max = (1u32 << n) - 1;
for chunk in buf.as_chunks_mut::<4>().0 {
let v = u32::from_ne_bytes(*chunk);
*chunk = (max - v).to_ne_bytes();
}
}
(ColorType::Gray(32), SampleFormat::IEEEFP) => {
for x in buf.as_chunks_mut::<4>().0 {
let v = f32::from_ne_bytes(*x);