Files
second-screen/tests/test_regions.py
T
rickzkmandClaude Sonnet 5.5 edba341f30 Send only the changed regions of each frame
The link moves ~150 KB/s, so a full frame takes 2 s. After the first frame the runner
diffs against what the screen shows and sends tight boxes; full frame on big changes,
after a failed push, and every 15 minutes.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
2026-10-22 14:25:09 +02:00

53 lines
1.6 KiB
Python

import numpy as np
import pytest
from PIL import Image
from lcdkit import BG
from regions import changed_regions
def blank(w=480, h=320):
return Image.new("RGB", (w, h), BG)
def with_pixels(points, colour=(0, 0, 0)):
img = blank()
for p in points:
img.putpixel(p, colour)
return img
def test_identical_images_have_no_changed_regions():
assert changed_regions(blank(), blank()) == []
def test_single_pixel_gives_a_tight_box():
assert changed_regions(blank(), with_pixels([(100, 50)])) == [(100, 50, 101, 51)]
def test_far_apart_changes_are_separate_boxes():
boxes = changed_regions(blank(), with_pixels([(10, 10), (400, 250)]))
assert boxes == [(10, 10, 11, 11), (400, 250, 401, 251)]
def test_neighbouring_tiles_in_a_row_merge_into_one_box():
# x=10 is in tile 0 and x=40 in tile 1 (32 px tiles): one run, tight bounding box
assert changed_regions(blank(), with_pixels([(10, 10), (40, 12)])) == [(10, 10, 41, 13)]
def test_boxes_always_cover_every_changed_pixel():
rng = np.random.default_rng(7)
for _ in range(20):
points = [(int(rng.integers(0, 480)), int(rng.integers(0, 320))) for _ in range(int(rng.integers(1, 12)))]
new = with_pixels(points)
boxes = changed_regions(blank(), new)
for x, y in points:
assert any(x0 <= x < x1 and y0 <= y < y1 for x0, y0, x1, y1 in boxes), (x, y, boxes)
for x0, y0, x1, y1 in boxes: # and stay inside the image
assert 0 <= x0 < x1 <= 480 and 0 <= y0 < y1 <= 320
def test_size_mismatch_is_an_error():
with pytest.raises(ValueError):
changed_regions(blank(480, 320), blank(320, 480))