Reset run from within _write_run

This commit is contained in:
Andrew Murray 2025-06-11 19:42:24 +10:00
parent dcf674a48f
commit 23a7fefced

View File

@ -137,9 +137,12 @@ class QoiEncoder(ImageFile.PyEncoder):
_pushes_fd = True _pushes_fd = True
_previous_pixel: tuple[int, int, int, int] | None = None _previous_pixel: tuple[int, int, int, int] | None = None
_previously_seen_pixels: dict[int, tuple[int, int, int, int]] = {} _previously_seen_pixels: dict[int, tuple[int, int, int, int]] = {}
_run = 0
def _write_run(self, run: int) -> bytes: def _write_run(self) -> bytes:
return o8(0b11000000 | (run - 1)) # QOI_OP_RUN data = o8(0b11000000 | (self._run - 1)) # QOI_OP_RUN
self._run = 0
return data
def _delta(self, left: int, right: int) -> int: def _delta(self, left: int, right: int) -> int:
result = (left - right) & 0xFF result = (left - right) & 0xFF
@ -155,7 +158,6 @@ class QoiEncoder(ImageFile.PyEncoder):
data = bytearray() data = bytearray()
w, h = self.im.size w, h = self.im.size
run = 0
bands = Image.getmodebands(self.im.mode) bands = Image.getmodebands(self.im.mode)
for y in range(h): for y in range(h):
@ -165,14 +167,12 @@ class QoiEncoder(ImageFile.PyEncoder):
pixel = (*pixel, 255) pixel = (*pixel, 255)
if pixel == self._previous_pixel: if pixel == self._previous_pixel:
run += 1 self._run += 1
if run == 62: if self._run == 62:
data += self._write_run(run) data += self._write_run()
run = 0
else: else:
if run > 0: if self._run > 0:
data += self._write_run(run) data += self._write_run()
run = 0
r, g, b, a = pixel r, g, b, a = pixel
hash_value = (r * 3 + g * 5 + b * 7 + a * 11) % 64 hash_value = (r * 3 + g * 5 + b * 7 + a * 11) % 64
@ -208,8 +208,8 @@ class QoiEncoder(ImageFile.PyEncoder):
self._previous_pixel = pixel self._previous_pixel = pixel
if run > 0: if self._run > 0:
data += self._write_run(run) data += self._write_run()
data += bytes((0, 0, 0, 0, 0, 0, 0, 1)) # padding data += bytes((0, 0, 0, 0, 0, 0, 0, 1)) # padding
return len(data), 0, data return len(data), 0, data