File size: 5,961 Bytes
5b3cd1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"""The design test's frames (see design.py): a capture JPEG decoded as OpenCV decodes it (libjpeg-turbo's accurate
integer IDCT), the whole-number shrink from the 1920 x 1440 capture to the 1280 x 960 grey frame every scanner reads,
each frame's SHA-256 over its 1,228,800 bytes row by row, the box where the plain code's frame differs from the
design's, the plain code's patch (its frame inside that box, as a grey PNG at compression level 1), the paste that
rebuilds its frame from the design's, and a fallback frame: a whole frame as a grey PNG at level 1, built from what the
server holds for its scene and checked against the scene's own hash before it is sent.

The shrink: on each axis, output index i reads the source (3i + 1) >> 1 with weight 2/3 and 3(i >> 1) + 1 with weight
1/3. With a the source pixel weighted 2/3 both ways, b and c 2/3 one way and 1/3 the other, and d 1/3 both ways, a frame
pixel is (4a + 2b + 2c + d + 4) div 9: OpenCV's INTER_AREA at this size, exactly, in whole numbers, so the page makes
the same frames from the same JPEG on any processor.

The test worker imports this module, and the server itself only for its first fallback frame: it never loads the scene
renderer or the scanners.
"""

from __future__ import annotations

import base64
import hashlib

import cv2
import numpy as np

CAPTURE = (1920, 1440)   # width, height
FRAME = (1280, 960)
PNG = [cv2.IMWRITE_PNG_COMPRESSION, 1]
_X, _Y = np.arange(FRAME[0]), np.arange(FRAME[1])
_X2, _X1 = (3 * _X + 1) >> 1, 3 * (_X >> 1) + 1   # each output's source weighted 2/3, and the one weighted 1/3
_Y2, _Y1 = (3 * _Y + 1) >> 1, 3 * (_Y >> 1) + 1


def decode_grey(jpeg: bytes) -> np.ndarray | None:
    """A JPEG's grey pixels as OpenCV decodes them; None when it doesn't decode."""
    g = cv2.imdecode(np.frombuffer(jpeg, np.uint8), cv2.IMREAD_GRAYSCALE)
    return g if g is not None and g.dtype == np.uint8 and g.ndim == 2 else None


def shrink(capture: np.ndarray) -> np.ndarray:
    """The 1280 x 960 frame of a 1920 x 1440 grey capture, by the whole-number rule."""
    if capture.shape != (CAPTURE[1], CAPTURE[0]) or capture.dtype != np.uint8:
        raise ValueError(f"not a {CAPTURE[0]} x {CAPTURE[1]} grey capture: {capture.shape} {capture.dtype}")
    g = capture.astype(np.uint16)   # at most 9 x 255: no overflow
    v = 2 * g[_Y2] + g[_Y1]
    t = 2 * v[:, _X2] + v[:, _X1]
    return ((t + 4) // 9).astype(np.uint8)


def frame_of(jpeg: bytes) -> np.ndarray | None:
    """The frame the scanners read from a capture JPEG; None when it isn't a 1920 x 1440 grey JPEG."""
    g = decode_grey(jpeg)
    return None if g is None or g.shape != (CAPTURE[1], CAPTURE[0]) else shrink(g)


def sha256(frame: np.ndarray) -> str:
    """The SHA-256 of a frame's bytes, row by row, one byte a pixel."""
    return hashlib.sha256(np.ascontiguousarray(frame, np.uint8).tobytes()).hexdigest()


def box_of(plain: np.ndarray, design: np.ndarray) -> list[int]:
    """[x, y, w, h], in frame pixels, of where the plain code's frame differs from the design's; [0, 0, 0, 0] when
    they are equal."""
    differ = plain != design
    rows = np.flatnonzero(differ.any(axis=1))
    if rows.size == 0:
        return [0, 0, 0, 0]
    cols = np.flatnonzero(differ.any(axis=0))
    return [int(cols[0]), int(rows[0]), int(cols[-1] + 1 - cols[0]), int(rows[-1] + 1 - rows[0])]


def png_of(grey: np.ndarray) -> bytes:
    """A grey picture as a PNG at compression level 1."""
    ok, enc = cv2.imencode(".png", np.ascontiguousarray(grey, np.uint8), PNG)
    if not ok:
        raise RuntimeError("PNG encoding failed")
    return enc.tobytes()


def patch_of(plain: np.ndarray, box: list[int]) -> bytes | None:
    """The plain code's frame inside the box, as a grey PNG at level 1; None for an empty box."""
    x, y, w, h = box
    return png_of(plain[y:y + h, x:x + w]) if w and h else None


def decode_png(png: bytes, w: int, h: int) -> np.ndarray | None:
    """A grey PNG of exactly w x h pixels, one byte each; None otherwise."""
    g = cv2.imdecode(np.frombuffer(png, np.uint8), cv2.IMREAD_UNCHANGED)
    return g if g is not None and g.dtype == np.uint8 and g.shape == (h, w) else None


def paste(design: np.ndarray, patch: bytes | None, box: list[int]) -> np.ndarray | None:
    """The plain code's frame: a copy of the design's with the patch written at the box; the design's own for an
    empty box. None when the box doesn't lie inside the frame or the patch isn't exactly its size."""
    x, y, w, h = box
    if not (w and h):
        return design.copy()
    if x < 0 or y < 0 or x + w > FRAME[0] or y + h > FRAME[1] or patch is None:
        return None
    p = decode_png(patch, w, h)
    if p is None:
        return None
    out = design.copy()
    out[y:y + h, x:x + w] = p
    return out


def fallback(held: dict, plain: bool) -> bytes | None:
    """A scene's whole frame as a grey PNG at level 1: the design's (from its capture JPEG, or its PNG frame), or the
    plain code's (its patch pasted over the design's). None when what is held doesn't rebuild the frame to the scene's
    own hash, so a frame that fails is never sent."""
    if held.get("png") is not None:
        design = decode_png(held["png"], *FRAME)
    else:
        design = frame_of(held["jpeg"]) if held.get("jpeg") is not None else None
    if design is None:
        return None
    frame = paste(design, held.get("patch"), held["box"]) if plain else design
    if frame is None or sha256(frame) != held["sha256"]["plain" if plain else "design"]:
        return None
    return png_of(frame)


def block_means(frame: np.ndarray) -> str:
    """The frame's 32 x 32 block means, each round(16 x the mean), as 30 x 40 little-endian uint16 in base64: the
    start-up self-test's pixel check."""
    m = frame.reshape(FRAME[1] // 32, 32, FRAME[0] // 32, 32).astype(np.float64).mean(axis=(1, 3))
    return base64.b64encode(np.rint(16 * m).astype("<u2").tobytes()).decode("ascii")