Skip to content

Commit 148a19e

Browse files
authored
Fix ZeroDivisionError in DdsImagePlugin (#9272)
2 parents a63ba0e + e1f4352 commit 148a19e

File tree

3 files changed

+8
-0
lines changed

3 files changed

+8
-0
lines changed

Tests/images/zero_mask_totals.dds

131 Bytes
Binary file not shown.

Tests/test_file_dds.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,11 @@ def test_palette() -> None:
380380
assert_image_equal_tofile(im, "Tests/images/transparent.gif")
381381

382382

383+
def test_zero_mask_totals() -> None:
384+
with Image.open("Tests/images/zero_mask_totals.dds") as im:
385+
im.load()
386+
387+
383388
def test_unsupported_header_size() -> None:
384389
with pytest.raises(OSError, match="Unsupported header size 0"):
385390
with Image.open(BytesIO(b"DDS " + b"\x00" * 4)):

src/PIL/DdsImagePlugin.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ class DdsImageFile(ImageFile.ImageFile):
333333
format_description = "DirectDraw Surface"
334334

335335
def _open(self) -> None:
336+
assert self.fp is not None
336337
if not _accept(self.fp.read(4)):
337338
msg = "not a DDS file"
338339
raise SyntaxError(msg)
@@ -516,6 +517,8 @@ def decode(self, buffer: bytes | Image.SupportsArrayInterface) -> tuple[int, int
516517
# Remove the zero padding, and scale it to 8 bits
517518
data += o8(
518519
int(((masked_value >> mask_offsets[i]) / mask_totals[i]) * 255)
520+
if mask_totals[i]
521+
else 0
519522
)
520523
self.set_as_raw(data)
521524
return -1, 0

0 commit comments

Comments
 (0)