|
14 | 14 |
|
15 | 15 | import struct |
16 | 16 | import sys |
| 17 | +import math |
17 | 18 | from enum import IntEnum, IntFlag |
18 | 19 | from typing import IO |
19 | 20 |
|
@@ -513,14 +514,34 @@ def decode(self, buffer: bytes | Image.SupportsArrayInterface) -> tuple[int, int |
513 | 514 | bytecount = bitcount // 8 |
514 | 515 | dest_length = self.state.xsize * self.state.ysize * len(masks) |
515 | 516 | 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") |
518 | 523 | for mask, offset, total in consolidated_mask: |
519 | 524 | masked_value = value & mask |
520 | 525 | # Remove the zero padding, and scale it to 8 bits |
521 | 526 | data += o8( |
522 | 527 | int((masked_value >> offset) * total) |
523 | 528 | ) |
| 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 | + |
524 | 545 | self.set_as_raw(data) |
525 | 546 | return -1, 0 |
526 | 547 |
|
|
0 commit comments