Port final lab 5 validation, session and PNG recovery fixes
This commit is contained in:
@@ -14,6 +14,33 @@ def chunk(kind: bytes, data: bytes) -> bytes:
|
||||
return struct.pack(">I", len(data)) + kind + data + struct.pack(">I", crc)
|
||||
|
||||
|
||||
def png_from_idat(
|
||||
data: bytes,
|
||||
width: int = 1,
|
||||
height: int = 1,
|
||||
*,
|
||||
depth: int = 8,
|
||||
color: int = 2,
|
||||
interlace: int = 0,
|
||||
chunk_size: int = 65_536,
|
||||
) -> bytes:
|
||||
header = struct.pack(">IIBBBBB", width, height, depth, color, 0, 0, interlace)
|
||||
palette = chunk(b"PLTE", bytes(range(256)) * 3) if color == 3 else b""
|
||||
blocks = [
|
||||
chunk(b"IDAT", data[start : start + chunk_size])
|
||||
for start in range(0, len(data), chunk_size)
|
||||
]
|
||||
return SIGNATURE + chunk(b"IHDR", header) + palette + b"".join(blocks) + chunk(b"IEND", b"")
|
||||
|
||||
|
||||
BROKEN_PNG = png_from_idat(b"not a zlib stream")
|
||||
DUPLICATE_IHDR_PNG = (
|
||||
SIGNATURE
|
||||
+ chunk(b"IHDR", struct.pack(">IIBBBBB", 1, 1, 8, 2, 0, 0, 0))
|
||||
+ png_from_idat(zlib.compress(bytes(4)), 2, 2)[len(SIGNATURE) :]
|
||||
)
|
||||
|
||||
|
||||
def png(width: int, height: int, *, size: int | None = None, rows: bool = True) -> bytes:
|
||||
header = struct.pack(">IIBBBBB", width, height, 1, 0, 0, 0, 0)
|
||||
raw = (b"\x00" * (1 + (width + 7) // 8)) * height if rows else b"\x00"
|
||||
|
||||
Reference in New Issue
Block a user