Intelligent pictures (format 4) and direction-aware picture order
Format 4 decoded from Editor output: control words of eight 4-bit ops (runs up to 13, 12-bit long runs, literal blocks) over 8 colour slots, plus a run-length 7-bit alpha section. The encoder reproduces all 28 Editor samples byte for byte and the Editor keeps our pictures as is. Pictures are stored rotated by the display direction, byte 0 of N.i is 0x0A + direction; this applies to format 0 too. add_picture now writes the right format and order for the project; the renderer decodes format 4 and blends alpha. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a93cb58b96
commit
193bcd5d97
8 files changed
+301
-56
No files matched your search
@@ -27,6 +27,66 @@ class Pictures(unittest.TestCase):
|
||||
self.assertEqual(pictures.source_file(s), b"PNGDATA")
|
||||
self.assertEqual(pictures.source_info(s)["type"], "png")
|
||||
|
||||
def test_stored_order_follows_direction(self):
|
||||
self.assertEqual(pictures.stored_order(2, 2, 0), [(0, 0), (1, 0), (0, 1), (1, 1)])
|
||||
self.assertEqual(pictures.stored_order(2, 2, 1), [(0, 1), (0, 0), (1, 1), (1, 0)])
|
||||
self.assertEqual(pictures.stored_order(2, 2, 2), [(1, 1), (0, 1), (1, 0), (0, 0)])
|
||||
self.assertEqual(pictures.stored_order(2, 2, 3), [(1, 0), (1, 1), (0, 0), (0, 1)])
|
||||
self.assertEqual(pictures.make_picture(b"", 0, 0, 1)[0], 0x0B) # 0x0A + direction
|
||||
|
||||
|
||||
def _distinct(i):
|
||||
return (((i * 37) % 32) << 3, ((i * 23 + 5) % 64) << 2, ((i * 11 + 3) % 32) << 3)
|
||||
|
||||
|
||||
class Format4(unittest.TestCase):
|
||||
"""Intelligent pictures: Nextion Editor 1.68 output for small test images (see FORMAT.md)."""
|
||||
EDITOR = { # name: (direction, pixel function, w, h, Editor bytes)
|
||||
"solid 16x16": (0, lambda x, y: (255, 0, 0), 16, 16,
|
||||
"0a6401040000000018000000100010003c0000000000000001000000100000001000000000000000000000000e0d0000"
|
||||
"00f800f800f800f800f800f800f800f8dd9d000000f800f800f800f80000000000000000"),
|
||||
"distinct 3x3": (0, lambda x, y: _distinct(y * 3 + x + 1), 3, 3,
|
||||
"0a6401040000000018000000030003003c0000000000000001000000030000000300000000000000000000008f000000"
|
||||
"8e2b795644792fa41acfe5f1d01cbb4701000000866a0000000000000000000000000000"),
|
||||
"alpha 4x4": (0, lambda x, y: (255, 0, 0, (y * 4 + x) * 17), 4, 4,
|
||||
"0a64010400000000180000000400040038000000280000000100000004000000040000000000000000000000d1020000"
|
||||
"000000f800f80000000000000000000080889099a1aab2bbc3ccd4dde5eef6ff"),
|
||||
"alpha 50% 4x4": (0, lambda x, y: (0, 0, 255, 128), 4, 4,
|
||||
"0a6401040000000018000000040004002a0000002800000001000000040000000400000000000000000000003d000000"
|
||||
"1f001f000000000000000000000000003f10"),
|
||||
"distinct 5x7, 90 degrees": (1, lambda x, y: _distinct(y * 5 + x + 1), 5, 7,
|
||||
"0b6401040000000018000000050007006c0000000000000001000000070000000500000000000000000000000f020000"
|
||||
"d8d961130a4db3865cb8e5f18e2ba3044c3ef5779ea927e3d01c79568e2f3761c09a69d4120ebb4744797952028cab"
|
||||
"c554fffd30866a2fa4447dedb696e83f2211010000c85b71951acf00000000000000000000"),
|
||||
}
|
||||
|
||||
def test_encode_matches_editor(self):
|
||||
for name, (d, f, w, h, hexed) in self.EDITOR.items():
|
||||
self.assertEqual(pictures.encode_format4(f, w, h, d).hex(), hexed, name)
|
||||
|
||||
def test_long_run_and_literals(self):
|
||||
blob = pictures.encode_format4(lambda x, y: (255, 0, 0), 40, 40, 0)
|
||||
self.assertEqual(struct.unpack_from("<I", blob, 44)[0], 14 | 1560 << 4) # 15 x 104 px in one op
|
||||
w, h, px, alpha = pictures.decode_format4(blob)
|
||||
self.assertEqual((w, h, len(px), set(px), alpha), (40, 40, 1600, {0xF800}, None))
|
||||
rnd = random.Random(3)
|
||||
cols = [(rnd.randrange(256), rnd.randrange(256), rnd.randrange(256)) for _ in range(32 * 32)]
|
||||
blob = pictures.encode_format4(lambda x, y: cols[y * 32 + x], 32, 32, 0)
|
||||
self.assertEqual(struct.unpack_from("<I", blob, 44)[0] & 15, 15) # one literal block
|
||||
_, _, px, _ = pictures.decode_format4(blob)
|
||||
self.assertEqual(px, [pictures._565(c) for c in cols])
|
||||
|
||||
@unittest.skipUnless(HAVE_PIL, "needs Pillow")
|
||||
def test_decode_to_image_all_directions(self):
|
||||
for d in range(4):
|
||||
blob = pictures.encode_format4(lambda x, y: _distinct(y * 5 + x + 1) + (255 if x else 128,), 5, 7, d)
|
||||
img = pictures.to_image(blob)
|
||||
self.assertEqual(img.mode, "RGBA")
|
||||
for x, y in ((0, 0), (4, 0), (2, 6)):
|
||||
r, g, b = _distinct(y * 5 + x + 1)
|
||||
self.assertEqual(tuple(v >> 3 for v in img.getpixel((x, y))[:3]), (r >> 3, g >> 3, b >> 3))
|
||||
self.assertLess(img.getpixel((0, 3))[3], 255)
|
||||
|
||||
@unittest.skipUnless(HAVE_PIL, "needs Pillow")
|
||||
def test_image_roundtrip(self):
|
||||
from PIL import Image
|
||||
|
||||
Reference in new issue
Block a user