mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-07-07 13:25:24 +03:00
Merge pull request #6270 from radarhere/samples_per_pixel
Adjust BITSPERSAMPLE to match SAMPLESPERPIXEL
This commit is contained in:
commit
779fb4975b
BIN
Tests/images/tiff_wrong_bits_per_sample_3.tiff
Normal file
BIN
Tests/images/tiff_wrong_bits_per_sample_3.tiff
Normal file
Binary file not shown.
|
@ -92,17 +92,33 @@ class TestFileTiff:
|
||||||
assert_image_equal_tofile(im, "Tests/images/hopper.tif")
|
assert_image_equal_tofile(im, "Tests/images/hopper.tif")
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"file_name,mode,size,offset",
|
"file_name,mode,size,tile",
|
||||||
[
|
[
|
||||||
("tiff_wrong_bits_per_sample.tiff", "RGBA", (52, 53), 160),
|
(
|
||||||
("tiff_wrong_bits_per_sample_2.tiff", "RGB", (16, 16), 8),
|
"tiff_wrong_bits_per_sample.tiff",
|
||||||
|
"RGBA",
|
||||||
|
(52, 53),
|
||||||
|
[("raw", (0, 0, 52, 53), 160, ("RGBA", 0, 1))],
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"tiff_wrong_bits_per_sample_2.tiff",
|
||||||
|
"RGB",
|
||||||
|
(16, 16),
|
||||||
|
[("raw", (0, 0, 16, 16), 8, ("RGB", 0, 1))],
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"tiff_wrong_bits_per_sample_3.tiff",
|
||||||
|
"RGBA",
|
||||||
|
(512, 256),
|
||||||
|
[("libtiff", (0, 0, 512, 256), 0, ("RGBA", "tiff_lzw", False, 48782))],
|
||||||
|
),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
def test_wrong_bits_per_sample(self, file_name, mode, size, offset):
|
def test_wrong_bits_per_sample(self, file_name, mode, size, tile):
|
||||||
with Image.open("Tests/images/" + file_name) as im:
|
with Image.open("Tests/images/" + file_name) as im:
|
||||||
assert im.mode == mode
|
assert im.mode == mode
|
||||||
assert im.size == size
|
assert im.size == size
|
||||||
assert im.tile == [("raw", (0, 0) + size, offset, (mode, 0, 1))]
|
assert im.tile == tile
|
||||||
im.load()
|
im.load()
|
||||||
|
|
||||||
def test_set_legacy_api(self):
|
def test_set_legacy_api(self):
|
||||||
|
|
|
@ -1355,19 +1355,19 @@ class TiffImageFile(ImageFile.ImageFile):
|
||||||
bps_count = 1
|
bps_count = 1
|
||||||
bps_count += len(extra_tuple)
|
bps_count += len(extra_tuple)
|
||||||
bps_actual_count = len(bps_tuple)
|
bps_actual_count = len(bps_tuple)
|
||||||
if bps_count < bps_actual_count:
|
|
||||||
# If a file has more values in bps_tuple than expected,
|
|
||||||
# remove the excess.
|
|
||||||
bps_tuple = bps_tuple[:bps_count]
|
|
||||||
elif bps_count > bps_actual_count and bps_actual_count == 1:
|
|
||||||
# If a file has only one value in bps_tuple, when it should have more,
|
|
||||||
# presume it is the same number of bits for all of the samples.
|
|
||||||
bps_tuple = bps_tuple * bps_count
|
|
||||||
|
|
||||||
samples_per_pixel = self.tag_v2.get(
|
samples_per_pixel = self.tag_v2.get(
|
||||||
SAMPLESPERPIXEL,
|
SAMPLESPERPIXEL,
|
||||||
3 if self._compression == "tiff_jpeg" and photo in (2, 6) else 1,
|
3 if self._compression == "tiff_jpeg" and photo in (2, 6) else 1,
|
||||||
)
|
)
|
||||||
|
if samples_per_pixel < bps_actual_count:
|
||||||
|
# If a file has more values in bps_tuple than expected,
|
||||||
|
# remove the excess.
|
||||||
|
bps_tuple = bps_tuple[:samples_per_pixel]
|
||||||
|
elif samples_per_pixel > bps_actual_count and bps_actual_count == 1:
|
||||||
|
# If a file has only one value in bps_tuple, when it should have more,
|
||||||
|
# presume it is the same number of bits for all of the samples.
|
||||||
|
bps_tuple = bps_tuple * samples_per_pixel
|
||||||
|
|
||||||
if len(bps_tuple) != samples_per_pixel:
|
if len(bps_tuple) != samples_per_pixel:
|
||||||
raise SyntaxError("unknown data organization")
|
raise SyntaxError("unknown data organization")
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user