Files
openhmi-nx/tests/test_media.py
T

188 lines
9.8 KiB
Python
Raw Normal View History

"""Pictures and fonts."""
import io
import random
import struct
import unittest
from helpers import FIXTURE, HAVE_PIL
from nxhmi import fonts, pictures
from nxhmi.container import HMIFile
class Pictures(unittest.TestCase):
def test_layout_rotated_bottom_up(self):
px = {(0, 0): (255, 0, 0), (1, 0): (0, 255, 0), (0, 1): (0, 0, 255), (1, 1): (255, 255, 255)}
raw = pictures.to_rgb565(lambda x, y: px[(x, y)], 2, 2)
self.assertEqual([struct.unpack_from("<H", raw, 2 * i)[0] for i in range(4)], [0x001F, 0xF800, 0xFFFF, 0x07E0])
def test_truncation(self):
raw = pictures.to_rgb565(lambda x, y: (7, 3, 255), 1, 1)
self.assertEqual(struct.unpack("<H", raw)[0], (0 << 11) | (0 << 5) | 31)
def test_headers(self):
i = pictures.make_picture(b"\0" * 8, 2, 2)
self.assertEqual(pictures.info(i), {"format": 0, "width": 2, "height": 2, "length": 8})
s = pictures.make_source(b"PNGDATA", 2, 2)
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)
def test_intelligent_colours_and_alpha(self):
# Intelligent simulator: channels scaled v*255//31, alpha 8-bit = 2 * stored 7-bit (127 = 255)
blob = pictures.encode_format4(lambda x, y: (198, 195, 198, [255, 227, 3][x]), 3, 1, 0)
img = pictures.to_image(blob)
self.assertEqual(img.getpixel((0, 0)), (197, 194, 197, 255))
self.assertEqual(img.getpixel((1, 0))[3], 226) # 227 * 127 // 255 = 113 -> 226
self.assertEqual(img.getpixel((2, 0))[3], 2)
def test_overhang_columns(self):
# Font Creator fonts: map bytes 3/4 = extra columns left/right of the advance width
rows = [[1, 0, 0, 1, 1]] * 2
g = fonts.Glyph(ord("j"), 4, rows, bytes([1, 0]))
f = fonts.Font("t", 4, 2, 1, 0x6A, 0x6A, [g])
back = fonts.Font.parse(f.pack())
self.assertEqual((back.glyphs[0].width, back.glyphs[0].data_width, back.glyphs[0].left), (4, 5, 1))
self.assertEqual(back.glyphs[0].pixels, rows)
self.assertFalse(back.antialias)
@unittest.skipUnless(HAVE_PIL, "needs Pillow")
def test_font_follows_project_direction(self):
# an Intelligent landscape project (0 degrees): glyphs row by row, header byte 3 = 0x0A,
# glyph data 4-byte aligned -- what the Editor converts a 90-degree font to
from nxhmi import project
h = project.new_project("NX8048P050_011")
f = fonts.from_ttf(None, 16)
n = project.add_font(h, f)
blob = h.read("%d.zi" % n)
self.assertEqual(blob[3], 0x0A)
back = fonts.Font.parse(blob)
self.assertEqual((back.direction, back.align), (0, True))
self.assertEqual(back.glyph("0").pixels, fonts.from_ttf(None, 16).glyph("0").pixels)
self.assertEqual(back.pack(), blob)
m = 0x2C + blob[17]
self.assertEqual((blob[m + 5] | blob[m + 6] << 8) % 4, 0)
@unittest.skipUnless(HAVE_PIL, "needs Pillow")
def test_image_roundtrip(self):
from PIL import Image
img = Image.new("RGB", (5, 3))
for x in range(5):
for y in range(3):
img.putpixel((x, y), (x * 50, y * 100, 200))
b = io.BytesIO(); img.save(b, "PNG")
pic, src = pictures.from_image_file(b.getvalue())
self.assertEqual(pictures.source_file(src), b.getvalue())
back = pictures.to_image(pic)
for x in range(5):
for y in range(3):
r, g, bb = img.getpixel((x, y))
self.assertEqual(back.getpixel((x, y))[0] >> 3, r >> 3)
def test_editor_pictures_decode(self):
h = HMIFile.load(FIXTURE)
for n in h.names():
if n.endswith(".i"):
self.assertEqual(pictures.info(h.read(n))["format"], 0)
class Fonts(unittest.TestCase):
def test_rle_roundtrip_random(self):
random.seed(3)
for _ in range(60):
w, h = random.choice([(8, 16), (16, 32), (12, 24), (5, 7)])
rows = [[1 if random.random() < 0.35 else 0 for _ in range(w)] for _ in range(h)]
self.assertEqual(fonts.decode_glyph(b"\x01" + fonts.encode_glyph(rows, w, h), w, h), rows)
def test_codes(self):
# pixels are scanned per column from the bottom row up; rows are returned top -> bottom
col = [r[0] for r in fonts.decode_glyph(b"\x01\x7e", 1, 32)] # 01 1 11110: 30 transparent, 2 ink
self.assertEqual(col, [1, 1] + [0] * 30)
self.assertEqual(fonts.decode_glyph(b"\x01\x22", 1, 2), [[1], [1]]) # 00 1 00010: 2 ink
self.assertEqual(fonts.decode_glyph(b"\x01\x02\x41", 1, 4), [[1], [0], [0], [0]]) # 2 t, then 01 0: 1 t + 1 ink
self.assertEqual(fonts.decode_glyph(b"\x01\xca", 1, 4), [[0], [1], [1], [0]]) # 11 001 010: 1 t + 2 ink
self.assertEqual(fonts.decode_glyph(b"\x01\x84", 1, 7), [[1], [1], [1], [0], [0], [0], [0]]) # 10 0: 4 t + 3 ink
def test_font_pack_parse(self):
f = fonts.Font("T-8", 8, 8, 1, 0x20, 0x21, [fonts.Glyph(0x20, 8, [[0] * 8 for _ in range(8)]),
fonts.Glyph(0x21, 4, [[1, 0, 0, 1] for _ in range(8)])])
g = fonts.Font.parse(f.pack())
self.assertEqual((g.name, g.glyph("!").width, g.glyph("!").pixels), ("T-8", 4, f.glyphs[1].pixels))
def test_editor_fonts_reencode_identical(self):
h = HMIFile.load(FIXTURE)
for n in h.names():
if n.endswith(".zi"):
blob = h.read(n)
self.assertEqual(fonts.Font.parse(blob).pack(), blob, n)
@unittest.skipUnless(HAVE_PIL, "needs Pillow")
def test_from_ttf_default_font(self):
f = fonts.from_ttf(None, 20)
self.assertEqual(len(f.glyphs), 95)
self.assertTrue(any(any(r) for r in f.glyph("A").pixels))
self.assertFalse(any(any(r) for r in f.glyph(" ").pixels))
self.assertEqual(fonts.Font.parse(f.pack()).name, "Aileron-20ascii")
if __name__ == "__main__":
unittest.main()