forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathtest_jpegio.py
More file actions
87 lines (64 loc) · 2.25 KB
/
test_jpegio.py
File metadata and controls
87 lines (64 loc) · 2.25 KB
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
# SPDX-FileCopyrightText: 2026 Scott Shawcroft for Adafruit Industries
# SPDX-License-Identifier: MIT
import shutil
from pathlib import Path
import pytest
from PIL import Image
_TEST_JPG_PATH = Path(__file__).parent / "test.jpg"
_TEST_JPG_BYTES = _TEST_JPG_PATH.read_bytes()
def _read_image(path: Path) -> tuple[int, int, bytes]:
with Image.open(path) as img:
rgb = img.convert("RGB")
return rgb.width, rgb.height, rgb.tobytes()
def _golden_compare_or_update(request, captures, golden_path):
if not captures or not captures[0].exists():
pytest.skip("display capture was not produced")
if request.config.getoption("--update-goldens"):
golden_path.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(captures[0], golden_path)
return
gw, gh, gpx = _read_image(golden_path)
dw, dh, dpx = _read_image(captures[0])
assert (dw, dh) == (gw, gh)
assert gpx == dpx
JPEGIO_DECODE_CODE = """\
import board
import displayio
import time
from jpegio import JpegDecoder
decoder = JpegDecoder()
width, height = decoder.open("/test.jpg")
print('size', width, height)
bitmap = displayio.Bitmap(width, height, 65535)
decoder.decode(bitmap)
for i in range(min(width, 8)):
print('px', i, hex(bitmap[i, 0]))
scale = 10
tg = displayio.TileGrid(
bitmap,
pixel_shader=displayio.ColorConverter(
input_colorspace=displayio.Colorspace.RGB565_SWAPPED
),
)
g = displayio.Group(scale=scale)
g.x = (board.DISPLAY.width - width * scale) // 2
g.y = (board.DISPLAY.height - height * scale) // 2
g.append(tg)
board.DISPLAY.auto_refresh = False
board.DISPLAY.root_group = g
board.DISPLAY.refresh()
print('rendered')
while True:
time.sleep(1)
"""
_JPEGIO_DRIVE = {"code.py": JPEGIO_DECODE_CODE, "test.jpg": _TEST_JPG_BYTES}
@pytest.mark.circuitpy_drive(_JPEGIO_DRIVE)
@pytest.mark.display(capture_times_ns=[14_000_000_000])
@pytest.mark.duration(18)
def test_jpegio_decode(request, circuitpython):
circuitpython.wait_until_done()
output = circuitpython.serial.all_output
assert "size 20 20" in output
assert "rendered" in output
golden = Path(__file__).parent / "golden" / "jpegio_test_pattern_320x240.png"
_golden_compare_or_update(request, circuitpython.display_capture_paths(), golden)