feat: support packed extended bit depths (9-15, 17-31 bits per sample)

TIFF files can store samples at non-standard bit depths (e.g. 10, 12,
14, 24 bits) packed tightly in a continuous bit stream. Previously
these were rejected as unsupported color types.

This adds:
- MSB-first bit extraction from packed byte streams (unpack_bits,
  extract_bits_msb) to expand N-bit samples to standard-width output
  (9-16 bit → u16, 17-32 bit → u32)
- Output buffer sizing using the promoted sample width instead of the
  packed bit depth (output_bits_per_sample field in ReadoutLayout)
- A new code path in expand_chunk that reads packed rows, unpacks to
  standard-width samples, then applies predictor and color inversion
- Horizontal predictor support for extended depths

Tested with 12-bit grayscale and RGB images (existing test suite),
plus all libtiffpic depth test images (10/12/14/24-bit in gray, RGB
contig, and RGB planar configurations).
This commit is contained in:
Lilith River committed 2026-03-10 06:01:48 -06:00
1 parent 8a5c2da511
commit 3d3c16e26f
2 files changed
+152 -8

No files matched your search

+3 -2
View File
@@ -43,6 +43,7 @@ test_image_sum!(test_image_sum_f64, F64, f64);
/// Tests that a decoder can be constructed for an image and the color type
/// read from the IFD and is of the appropriate type, but the type is
/// unsupported.
#[allow(dead_code)]
fn test_image_color_type_unsupported(file: &str, expected_type: ColorType) {
let path = PathBuf::from(TEST_IMAGE_DIR).join(file);
let img_file = File::open(path).expect("Cannot find test image!");
@@ -121,7 +122,7 @@ fn test_gray_u8() {
#[test]
fn test_gray_u12() {
test_image_color_type_unsupported("12bit.cropped.tiff", ColorType::Gray(12));
test_image_sum_u16("12bit.cropped.tiff", ColorType::Gray(12), 10973);
}
#[test]
@@ -160,7 +161,7 @@ fn test_rgb_u8() {
#[test]
fn test_rgb_u12() {
test_image_color_type_unsupported("12bit.cropped.rgb.tiff", ColorType::RGB(12));
test_image_sum_u16("12bit.cropped.rgb.tiff", ColorType::RGB(12), 32919);
}
#[test]