Files
mtusi2026lab7-safronov/bench/make_images.py
ricoromaandClaude Opus 5.5 089d78609d chore: port lab 6 solution into the lab 7 skeleton
Source: mtusi2026lab6-safronov, solution/bvt2301-safronov, 6c6a065.
Files follow AGENTS.md, "Переход в следующую лабу". Contracts, public
tests, README, REPORT and evidence of lab 6 are not carried over; infra
and scripts of the lab 7 skeleton stay as they are.
Makefile is the lab 6 one with the lab 7 skeleton part restored
(HTTPS BASE_URL on 8443, test-security); .gitignore and .dockerignore
are taken from lab 6 because both skeletons have the same files;
.env.example is the lab 6 one without changes.
The compose project, image and CI prefix are renamed to lab7, and
references to the previous lab now name lab6.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 03:59:04 +03:00

189 lines
5.7 KiB
Python
Raw Permalink 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.
"""Файлы классов профиля очереди (design change `track1-queue-overload`,
решение 5): `s`, `m`, `lj`, `lp`, детерминированно от зерна; класс `bad` —
профиль цены телеметрии (design change `track1-telemetry-cost`, решение 8).
Только в контейнере `mtusi-bench-images` на образе стенда: Pillow образа и та же
его версия, что у процесса обработки; `src` не импортируется. На stdout — JSON с
размерами, пикселями и SHA-256; файл больше лимита контракта — код 1.
python make_images.py <зерно> <каталог>
"""
import hashlib
import io
import json
import random
import sys
from pathlib import Path
from PIL import Image, ImageChops, UnidentifiedImageError
from uploads import png_noise
MAX_BYTES = 10_485_760
MAX_PIXELS = 40_000_000
LARGE = (8000, 5000)
SMALL = (1600, 1200)
NOISE_HEIGHT = 3409
CELLS_LOW = 64
CELLS_HIGH = 512
FINE_DIVISOR = 6
TILE = 512
AMPLITUDE = 24
POSTERIZE_MASK = 0xF0
# точность в SOF, которую Pillow не декодирует
PRECISION_12 = 12
def noise_l(width: int, height: int, seed: int) -> Image.Image:
data = random.Random(seed).randbytes(width * height)
return Image.frombytes("L", (width, height), data)
def clouds(
width: int,
height: int,
seed: int,
cells: int = CELLS_LOW,
) -> Image.Image:
"""Гладкое поле из шума низкого разрешения: низкие частоты фото."""
return noise_l(cells, max(2, cells * height // width), seed).resize(
(width, height),
Image.Resampling.BICUBIC,
)
def detail_tile(
width: int,
height: int,
seed: int,
amplitude: int,
) -> Image.Image:
"""Плитка шума амплитуды `amplitude`, повторённая на всё поле."""
tile = noise_l(TILE, TILE, seed).point(lambda v: v * amplitude // 255)
field = Image.new("L", (width, height))
for y in range(0, height, TILE):
for x in range(0, width, TILE):
field.paste(tile, (x, y))
return field
def bands(
width: int,
height: int,
seed: int,
amplitude: int,
) -> list[Image.Image]:
out = []
for number in range(3):
band = ImageChops.add(
clouds(width, height, seed + number),
clouds(
width,
height,
seed + 10 + number,
cells=CELLS_HIGH,
).point(lambda v: v // FINE_DIVISOR),
)
if amplitude:
band = ImageChops.add(
band,
detail_tile(width, height, seed + 20 + number, amplitude),
)
out.append(band)
return out
def jpeg(image: Image.Image, quality: int) -> bytes:
out = io.BytesIO()
image.save(out, format="JPEG", quality=quality, subsampling="4:2:0")
return out.getvalue()
def build_s(seed: int) -> bytes:
return jpeg(Image.merge("RGB", bands(*SMALL, seed, AMPLITUDE)), 85)
def build_lj(seed: int) -> bytes:
return jpeg(Image.merge("RGB", bands(*LARGE, seed + 200, AMPLITUDE)), 88)
def build_lp(seed: int) -> bytes:
colour = [
band.point(lambda v: v & POSTERIZE_MASK)
for band in bands(*LARGE, seed + 200, 0)
]
alpha = clouds(*LARGE, seed + 300).point(
lambda v: (128 + v // 2) & POSTERIZE_MASK,
)
out = io.BytesIO()
Image.merge("RGBA", (*colour, alpha)).save(
out,
format="PNG",
compress_level=6,
)
return out.getvalue()
def build_m(seed: int) -> bytes:
return png_noise(NOISE_HEIGHT, seed + NOISE_HEIGHT)
def build_bad(seed: int) -> bytes:
"""Класс `bad` профиля цены телеметрии (design change
`track1-telemetry-cost`, решение 8): байты `s` с 12 битами точности в
SOF — структура верна (202), Pillow не декодирует (`failed`,
`invalid_image`), как `images.jpeg_precision_12()` тестов."""
data = bytearray(build_s(seed))
sof = data.index(b"\xff\xc0")
data[sof + 4] = PRECISION_12
return bytes(data)
def image_size(data: bytes) -> tuple[int, int]:
"""Ширина и высота: Pillow, а у `bad`, который Pillow не открывает, —
поля SOF (высота и ширина после точности)."""
try:
with Image.open(io.BytesIO(data)) as image:
return image.size
except UnidentifiedImageError:
sof = data.index(b"\xff\xc0")
height = int.from_bytes(data[sof + 5 : sof + 7], "big")
width = int.from_bytes(data[sof + 7 : sof + 9], "big")
return width, height
BUILDERS = {
"s": (build_s, "s.jpg", "image/jpeg"),
"m": (build_m, "m.png", "image/png"),
"lj": (build_lj, "lj.jpg", "image/jpeg"),
"lp": (build_lp, "lp.png", "image/png"),
"bad": (build_bad, "bad.jpg", "image/jpeg"),
}
def main() -> int:
seed, directory = int(sys.argv[1]), Path(sys.argv[2])
report = {}
code = 0
for name, (build, filename, media) in BUILDERS.items():
data = build(seed)
width, height = image_size(data)
(directory / filename).write_bytes(data)
report[name] = {
"file": filename,
"media_type": media,
"bytes": len(data),
"width": width,
"height": height,
"sha256": hashlib.sha256(data).hexdigest(),
}
if len(data) > MAX_BYTES or width * height > MAX_PIXELS:
print(f"{name}: over the contract limit", file=sys.stderr)
code = 1
print(json.dumps(report))
return code
if __name__ == "__main__":
sys.exit(main())