Files
mtusi2026lab4/student-tests/images.py
T

241 lines
7.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Изображения для тестов вложений: настоящие, повреждённые, огромные.
PNG собираются на stdlib — любые IHDR и IDAT, Adam7, точный размер файла
чанком-заполнителем `tEXt`, «бомба», порча CRC. JPEG — через Pillow
(dev-группа): прогрессивный, серый, CMYK, с EXIF, с подменой размеров в
SOF. Большие файлы и «бомбы» живут только в памяти теста и в git не
попадают (`tests/fixtures/README.md`).
"""
import functools
import io
import random
import struct
import zlib
from pathlib import Path
PNG_SIGNATURE = b"\x89PNG\r\n\x1a\n"
SAMPLE_PNG_PATH = (
Path(__file__).resolve().parents[1] / "tests" / "fixtures" / "sample.png"
)
# лимиты контракта лабы 4
MAX_BYTES = 10 * 1024 * 1024
MAX_PIXELS = 40_000_000
_CHANNELS = {0: 1, 2: 3, 3: 1, 4: 2, 6: 4}
_ADAM7 = (
(0, 0, 8, 8),
(4, 0, 8, 8),
(0, 4, 4, 8),
(2, 0, 4, 4),
(0, 2, 2, 4),
(1, 0, 2, 2),
(0, 1, 1, 2),
)
@functools.cache
def sample_png() -> bytes:
"""Публичный fixture `tests/fixtures/sample.png` (320 × 200)."""
return SAMPLE_PNG_PATH.read_bytes()
def chunk(kind: bytes, body: bytes) -> bytes:
"""Чанк PNG с верной CRC."""
crc = zlib.crc32(kind + body)
return struct.pack(">I", len(body)) + kind + body + struct.pack(">I", crc)
def _rows(
width: int,
height: int,
*,
bits_per_pixel: int,
interlace: bool,
) -> bytes:
"""Распакованные данные: строки с фильтром 0 и узором в пикселях."""
passes = _ADAM7 if interlace else ((0, 0, 1, 1),)
out = bytearray()
for x0, y0, dx, dy in passes:
pass_width = (width - x0 + dx - 1) // dx if width > x0 else 0
pass_height = (height - y0 + dy - 1) // dy if height > y0 else 0
if not pass_width or not pass_height:
continue
row_bytes = (pass_width * bits_per_pixel + 7) // 8
row = bytes((i * 7) % 256 for i in range(row_bytes))
out += (b"\x00" + row) * pass_height
return bytes(out)
def png(
width: int = 64,
height: int = 48,
*,
color: int = 2,
depth: int = 8,
interlace: bool = False,
idat: bytes | None = None,
) -> bytes:
"""Настоящий PNG: IHDR, PLTE для палитры, IDAT, IEND.
`idat` — готовый сжатый поток вместо строк по размерам (огромные
размеры в маленьком файле, «бомба»).
"""
ihdr = struct.pack(
">IIBBBBB",
width,
height,
depth,
color,
0,
0,
1 if interlace else 0,
)
if idat is None:
idat = zlib.compress(
_rows(
width,
height,
bits_per_pixel=depth * _CHANNELS[color],
interlace=interlace,
),
9,
)
palette = chunk(b"PLTE", bytes(range(48))) if color == 3 else b""
return (
PNG_SIGNATURE
+ chunk(b"IHDR", ihdr)
+ palette
+ chunk(b"IDAT", idat)
+ chunk(b"IEND", b"")
)
def png_of_size(size: int) -> bytes:
"""Настоящий PNG 64 × 48 ровно `size` байт: добор чанком `tEXt`."""
base = png()
filler = size - len(base) - 12
assert filler >= 2, f"size {size} is too small"
text = chunk(b"tEXt", b"k\x00" + b"x" * (filler - 2))
# tEXt — после IHDR (8 + 25 байт), до IDAT
data = base[:33] + text + base[33:]
assert len(data) == size
return data
def png_noise(width: int = 1024, height: int = 341, *, seed: int = 0) -> bytes:
"""Настоящий RGB PNG из шума: сжимается плохо, поэтому размер файла —
около `width × height × 3` байт (по умолчанию ~1 MiB). Разные `seed` —
разные байты; один `seed` — одни и те же."""
noise = random.Random(seed)
row_bytes = width * 3
rows = b"".join(
b"\x00" + noise.randbytes(row_bytes) for _ in range(height)
)
return png(width, height, color=2, idat=zlib.compress(rows, 1))
def png_huge(width: int, height: int) -> bytes:
"""Несколько килобайт с огромными размерами в IHDR."""
return png(
width,
height,
color=0,
depth=1,
idat=zlib.compress(bytes(4096)),
)
def png_bomb(unpacked_mib: int = 200) -> bytes:
"""PNG 100 × 100, IDAT которого распаковывается в `unpacked_mib` MiB."""
compressor = zlib.compressobj(9)
block = bytes(1024 * 1024)
stream = b"".join(compressor.compress(block) for _ in range(unpacked_mib))
return png(100, 100, idat=stream + compressor.flush())
def flip_byte(data: bytes, offset: int) -> bytes:
damaged = bytearray(data)
damaged[offset] ^= 0x55
return bytes(damaged)
def png_idat_byte_changed(data: bytes | None = None) -> bytes:
"""Изменённый байт внутри первого IDAT (CRC чанка не сходится)."""
data = data or sample_png()
return flip_byte(data, data.index(b"IDAT") + 40)
def truncated(data: bytes, keep: float = 0.5) -> bytes:
return data[: int(len(data) * keep)]
def with_tail(data: bytes, tail: bytes = b"TAIL" * 64) -> bytes:
"""Байты после IEND или EOI."""
return data + tail
def _pillow_image(mode: str, size: tuple[int, int]) -> object:
from PIL import Image # noqa: PLC0415 — Pillow только для JPEG
width, height = size
image = Image.new("RGB", size)
image.putdata(
[
((x * 7) % 256, (y * 13) % 256, (x * y) % 256)
for y in range(height)
for x in range(width)
],
)
return image.convert(mode)
def jpeg(
mode: str = "RGB",
size: tuple[int, int] = (64, 48),
**save: object,
) -> bytes:
"""Настоящий JPEG через Pillow; `save` — параметры `Image.save`."""
from PIL import Image # noqa: PLC0415
buffer = io.BytesIO()
image = _pillow_image(mode, size)
if "exif" in save and save["exif"] is True:
exif = Image.Exif()
exif[0x010F] = "student-tests"
exif[0x0112] = 1
save["exif"] = exif
image.save(buffer, "JPEG", **save)
return buffer.getvalue()
def jpeg_huge(width: int, height: int) -> bytes:
"""Маленький JPEG с подменёнными размерами в SOF0."""
data = bytearray(jpeg())
sof = data.index(b"\xff\xc0")
data[sof + 5 : sof + 9] = struct.pack(">HH", height, width)
return bytes(data)
def png_variants() -> dict[str, bytes]:
"""Разновидности PNG спека: палитра, альфа, серый с альфой, 16 и 1
бит, Adam7."""
return {
"palette": png(color=3, depth=8),
"rgba": png(color=6),
"gray-alpha": png(color=4),
"rgb-16-bit": png(color=2, depth=16),
"gray-1-bit": png(color=0, depth=1),
"adam7": png(37, 29, interlace=True),
}
def jpeg_variants() -> dict[str, bytes]:
"""Разновидности JPEG спека: прогрессивный, серый, CMYK, с EXIF."""
return {
"progressive": jpeg(progressive=True),
"gray": jpeg("L"),
"cmyk": jpeg("CMYK"),
"exif": jpeg(exif=True),
}