Some checks failed
Build and deploy / Validate source (push) Successful in 1m41s
Build and deploy / Publish images (push) Has been cancelled
Build and deploy / Secret scan and release gate (push) Has been cancelled
Build and deploy / Integration suite on a real stack (push) Has been cancelled
#81's 91,5 m took 6 hours to generate. The PNG writer ran Pillow's row filters and then deflate at level 6; no filter at level 1 gives the same size in under half the time, for the file and the viewer's strips alike (1 m: 34 s -> 13 s). Its Prova OK came while the file was still being generated, so the order stayed in Arte recebida. The approval now also runs when the file finishes. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
108 lines
4.3 KiB
Python
108 lines
4.3 KiB
Python
"""The print file in pieces, for the Kanban's viewer (Dropstar's third round, item 7).
|
|
|
|
"Abrir e conferir" shows the PNG at up to 100% (one file pixel, one screen
|
|
pixel). A 57 cm x 15 m film is 160 million pixels per 2 m: loaded whole it
|
|
freezes the designer's computer, so the viewer loads it in pieces, like a map.
|
|
|
|
The pieces are strips of the film's whole width, STRIP_ROWS tall, at 100%
|
|
and at each halving down to a few strips for the whole film: level 0 is the
|
|
file itself, level k is 1/2^k of it. They are written while the PNG is, from
|
|
the same bands, so the file is never read back.
|
|
"""
|
|
import io
|
|
import math
|
|
|
|
from PIL import Image
|
|
|
|
STRIP_ROWS = 512
|
|
# Levels stop when the whole film fits in about one screen.
|
|
SMALLEST_ROWS = 1024
|
|
|
|
|
|
def levels_for(height):
|
|
"""How many levels a film of `height` px gets: halving until it is short."""
|
|
count = 1
|
|
while math.ceil(height / 2 ** (count - 1)) > SMALLEST_ROWS and count < 12:
|
|
count += 1
|
|
return count
|
|
|
|
|
|
class Pyramid:
|
|
"""Fed the film's bands from the top, it hands each finished strip, as a
|
|
PNG, to `emit(level, index, png_bytes)`."""
|
|
|
|
def __init__(self, width, height, emit):
|
|
self.width, self.height, self.emit = width, height, emit
|
|
self.levels = levels_for(height)
|
|
self.pending = [None] * self.levels # rows of each level not yet a strip
|
|
self.count = [0] * self.levels # strips written per level
|
|
# A level's odd last row waits for its pair.
|
|
self.odd = [None] * self.levels
|
|
|
|
def band(self, image):
|
|
self._feed(0, image)
|
|
|
|
def _feed(self, level, image):
|
|
if level >= self.levels:
|
|
return
|
|
rows = self.pending[level]
|
|
if rows is None:
|
|
rows = image
|
|
else:
|
|
joined = Image.new('RGBA', (image.width, rows.height + image.height))
|
|
joined.paste(rows, (0, 0))
|
|
joined.paste(image, (0, rows.height))
|
|
rows = joined
|
|
while rows.height >= STRIP_ROWS:
|
|
self._strip(level, rows.crop((0, 0, rows.width, STRIP_ROWS)))
|
|
rows = rows.crop((0, STRIP_ROWS, rows.width, rows.height)) if rows.height > STRIP_ROWS else None
|
|
if rows is None:
|
|
break
|
|
self.pending[level] = rows
|
|
# The next level down, from these rows, two by two.
|
|
self._halve(level, image)
|
|
|
|
def _halve(self, level, image):
|
|
if level + 1 >= self.levels:
|
|
return
|
|
if self.odd[level] is not None:
|
|
joined = Image.new('RGBA', (image.width, image.height + 1))
|
|
joined.paste(self.odd[level], (0, 0))
|
|
joined.paste(image, (0, 1))
|
|
image, self.odd[level] = joined, None
|
|
if image.height % 2:
|
|
self.odd[level] = image.crop((0, image.height - 1, image.width, image.height))
|
|
image = image.crop((0, 0, image.width, image.height - 1))
|
|
if image.height:
|
|
small = image.resize((max(1, math.ceil(image.width / 2)), image.height // 2), Image.BOX)
|
|
self._feed(level + 1, small)
|
|
|
|
def _strip(self, level, image):
|
|
# The print file's own writer, the fast one (imported here: printpng
|
|
# imports this module).
|
|
from .printpng import PngWriter
|
|
out = io.BytesIO()
|
|
writer = PngWriter(out, image.width, image.height)
|
|
writer.band(image)
|
|
writer.close()
|
|
self.emit(level, self.count[level], out.getvalue())
|
|
self.count[level] += 1
|
|
|
|
def close(self):
|
|
"""Flush what is left at each level, from the top level down."""
|
|
for level in range(self.levels):
|
|
if self.odd[level] is not None and level + 1 < self.levels:
|
|
last = self.odd[level]
|
|
self.odd[level] = None
|
|
self._feed(level + 1, last.resize((max(1, math.ceil(last.width / 2)), 1), Image.BOX))
|
|
if self.pending[level] is not None and self.pending[level].height:
|
|
self._strip(level, self.pending[level])
|
|
self.pending[level] = None
|
|
return self.manifest()
|
|
|
|
def manifest(self):
|
|
return {'width': self.width, 'height': self.height, 'strip_rows': STRIP_ROWS,
|
|
'levels': [{'scale': 1 / 2 ** k, 'width': math.ceil(self.width / 2 ** k),
|
|
'height': math.ceil(self.height / 2 ** k), 'strips': self.count[k]}
|
|
for k in range(self.levels)]}
|