Skip to content

Commit f15886a

Browse files
committed
WIP 3 -- do all the remaining pixels in one chunk2
1 parent 63ff7ca commit f15886a

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

src/PIL/DdsImagePlugin.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import struct
1616
import sys
17+
import math
1718
from enum import IntEnum, IntFlag
1819
from typing import IO
1920

@@ -513,14 +514,34 @@ def decode(self, buffer: bytes | Image.SupportsArrayInterface) -> tuple[int, int
513514
bytecount = bitcount // 8
514515
dest_length = self.state.xsize * self.state.ysize * len(masks)
515516
consolidated_mask = list(zip(masks, mask_offsets, mask_totals))
516-
while len(data) < dest_length:
517-
value = int.from_bytes(self.fd.read(bytecount), "little")
517+
# consume the data
518+
has_more = True
519+
while len(data) < dest_length and has_more:
520+
chunk = self.fd.read(bytecount)
521+
has_more = len(chunk) > 0
522+
value = int.from_bytes(chunk, "little")
518523
for mask, offset, total in consolidated_mask:
519524
masked_value = value & mask
520525
# Remove the zero padding, and scale it to 8 bits
521526
data += o8(
522527
int((masked_value >> offset) * total)
523528
)
529+
530+
# extra padding pixels
531+
if len(data) < dest_length:
532+
value = int.from_bytes(b'', "little")
533+
pixel = bytearray()
534+
for mask, offset, total in consolidated_mask:
535+
masked_value = value & mask
536+
# Remove the zero padding, and scale it to 8 bits
537+
pixel += o8(
538+
int((masked_value >> offset) * total)
539+
)
540+
541+
ct_pixels = math.ceil((dest_length - len(data)) / bytecount)
542+
data += pixel * ct_pixels
543+
544+
524545
self.set_as_raw(data)
525546
return -1, 0
526547

0 commit comments

Comments
 (0)