For separate planar configuration, ignore unspecified extra components

This commit is contained in:
Andrew Murray 2026-03-30 19:42:07 +11:00
parent 07c180b21e
commit 84cb30d7a7
3 changed files with 14 additions and 4 deletions

Binary file not shown.

View File

@ -1055,6 +1055,10 @@ class TestFileLibTiff(LibTiffTestCase):
with Image.open("Tests/images/tiff_strip_planar_16bit_RGBa.tiff") as im:
assert_image_equal_tofile(im, "Tests/images/tiff_16bit_RGBa_target.png")
def test_separate_planar_extra_samples(self) -> None:
with Image.open("Tests/images/separate_planar_extra_samples.tiff") as im:
assert im.mode == "L"
@pytest.mark.parametrize("compression", (None, "jpeg"))
def test_block_tile_tags(self, compression: str | None, tmp_path: Path) -> None:
im = hopper()

View File

@ -1483,18 +1483,24 @@ class TiffImageFile(ImageFile.ImageFile):
bps_tuple = self.tag_v2.get(BITSPERSAMPLE, (1,))
extra_tuple = self.tag_v2.get(EXTRASAMPLES, ())
samples_per_pixel = self.tag_v2.get(
SAMPLESPERPIXEL,
3 if self._compression == "tiff_jpeg" and photo in (2, 6) else 1,
)
if photo in (2, 6, 8): # RGB, YCbCr, LAB
bps_count = 3
elif photo == 5: # CMYK
bps_count = 4
else:
bps_count = 1
if self._planar_configuration == 2 and extra_tuple and max(extra_tuple) == 0:
# If components are stored separately,
# then unspecified extra components at the end can be ignored
bps_tuple = bps_tuple[: -len(extra_tuple)]
samples_per_pixel -= len(extra_tuple)
extra_tuple = ()
bps_count += len(extra_tuple)
bps_actual_count = len(bps_tuple)
samples_per_pixel = self.tag_v2.get(
SAMPLESPERPIXEL,
3 if self._compression == "tiff_jpeg" and photo in (2, 6) else 1,
)
if samples_per_pixel > MAX_SAMPLESPERPIXEL:
# DOS check, samples_per_pixel can be a Long, and we extend the tuple below