Chat draws MCOimg pixel images in half blocks under their caption

A v3 image (im3:) is now decoded in full: all 16 palette profiles, 4 scan
orders, 8 containers, 16 block algorithms, and every local palette form.
The decoder is written from the published spec; the palette colours are
interop data generated from meshcore-open's tables (MIT, credited).
Hostile input returns None and never raises; a 64x64 image takes ~20 ms.

The chat draws two pixels per cell (top colour over bottom background),
at the body's indent, shrunk by nearest pixel to fit the lane and the
platform's rows. All 45 reference vectors pass, copied to tests/data.
Refs #37.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Jean-Pierre MartineauandClaude Opus 5.5 committed 2026-11-19 22:18:21 -05:00
1 parent 017fdcfce7
commit 5fb11e1acd
10 files changed
+3596 -13

No files matched your search

+1241 -7
View File
File diff suppressed because it is too large. Load diff
+279
View File
@@ -0,0 +1,279 @@
# SPDX-License-Identifier: Apache-2.0
"""The colour tables of the MCOimg palette profiles.
This module is data for interoperability, and has no code. A script made it from the
files ``lib/helpers/mcoimg_palette.dart`` and ``lib/helpers/mcoimg_dynamic_palettes.dart``
of the meshcore-open repository (HDDen/meshcore-open, a fork of zjs81/meshcore-open, MIT).
Do not edit the values by hand. A different value changes the colour of a pixel.
- The fixed profiles are tuples of (red, green, blue), in the order of the colour ids.
- ``GLOBAL_512`` is the dynamic global palette of 512 colours.
- Each ``DYNAMIC_n`` tuple gives the index in ``GLOBAL_512`` of each colour id of the
dynamic profile of n colours.
"""
from __future__ import annotations
# The tables keep several values on each line, so the formatter must not change them.
# fmt: off
MONO: tuple[tuple[int, int, int], ...] = (
(255, 255, 255), (0, 0, 0),
)
MASTER_4: tuple[tuple[int, int, int], ...] = (
(255, 255, 255), (192, 192, 192), (86, 86, 86), (0, 0, 0),
)
MASTER_8: tuple[tuple[int, int, int], ...] = (
(255, 255, 255), (141, 141, 141), (0, 0, 0), (254, 36, 0), (241, 209, 0),
(71, 192, 0), (61, 105, 255), (121, 0, 255),
)
MASTER_16: tuple[tuple[int, int, int], ...] = (
(255, 255, 255), (164, 164, 164), (0, 0, 0), (209, 30, 1), (98, 14, 1),
(255, 132, 0), (123, 64, 0), (241, 209, 0), (144, 124, 2), (65, 176, 0),
(40, 110, 0), (127, 220, 255), (0, 58, 255), (0, 34, 150), (106, 0, 227),
(47, 0, 100),
)
MASTER_32: tuple[tuple[int, int, int], ...] = (
(255, 255, 255), (179, 179, 179), (102, 102, 102), (0, 0, 0), (255, 176, 163),
(255, 85, 65), (254, 36, 0), (98, 14, 1), (255, 179, 99), (255, 132, 0),
(197, 102, 1), (142, 73, 0), (245, 222, 91), (241, 209, 0), (181, 157, 2),
(120, 105, 2), (149, 218, 118), (71, 192, 0), (40, 110, 0), (29, 79, 0),
(196, 241, 255), (1, 195, 255), (3, 141, 184), (1, 109, 143), (117, 150, 255),
(0, 58, 255), (2, 46, 202), (0, 34, 150), (215, 178, 255), (178, 135, 255),
(133, 61, 255), (47, 0, 100),
)
MASTER_64: tuple[tuple[int, int, int], ...] = (
(255, 255, 255), (217, 217, 217), (179, 179, 179), (138, 139, 138), (111, 111, 111),
(79, 79, 79), (36, 36, 36), (0, 0, 0), (255, 176, 163), (255, 154, 137),
(255, 85, 65), (254, 36, 0), (209, 30, 1), (145, 21, 0), (98, 14, 1),
(69, 10, 0), (255, 179, 99), (255, 168, 85), (255, 147, 51), (255, 132, 0),
(228, 118, 1), (197, 102, 1), (142, 73, 0), (123, 64, 0), (247, 229, 114),
(245, 222, 91), (241, 209, 0), (223, 193, 2), (203, 177, 1), (181, 157, 2),
(144, 124, 2), (120, 105, 2), (183, 230, 155), (149, 218, 118), (109, 205, 75),
(71, 192, 0), (65, 176, 0), (54, 148, 1), (40, 110, 0), (29, 79, 0),
(196, 241, 255), (171, 233, 255), (127, 220, 255), (1, 195, 255), (0, 182, 238),
(1, 170, 223), (3, 141, 184), (1, 109, 143), (145, 170, 255), (117, 150, 255),
(59, 100, 255), (0, 58, 255), (2, 51, 225), (2, 46, 202), (1, 40, 176),
(0, 34, 150), (215, 178, 255), (178, 135, 255), (154, 101, 255), (133, 61, 255),
(121, 0, 255), (105, 2, 221), (83, 1, 175), (47, 0, 100),
)
GREY_16: tuple[tuple[int, int, int], ...] = (
(255, 255, 255), (238, 238, 238), (221, 221, 221), (204, 204, 204), (187, 187, 187),
(170, 170, 170), (153, 153, 153), (136, 136, 136), (119, 119, 119), (102, 102, 102),
(85, 85, 85), (68, 68, 68), (51, 51, 51), (34, 34, 34), (17, 17, 17),
(0, 0, 0),
)
GREY_32: tuple[tuple[int, int, int], ...] = (
(255, 255, 255), (247, 247, 247), (239, 239, 239), (230, 230, 230), (222, 222, 222),
(214, 214, 214), (206, 206, 206), (197, 197, 197), (189, 189, 189), (181, 181, 181),
(173, 173, 173), (165, 165, 165), (156, 156, 156), (148, 148, 148), (140, 140, 140),
(132, 132, 132), (123, 123, 123), (115, 115, 115), (107, 107, 107), (99, 99, 99),
(90, 90, 90), (82, 82, 82), (74, 74, 74), (66, 66, 66), (57, 57, 57),
(49, 49, 49), (41, 41, 41), (33, 33, 33), (24, 24, 24), (16, 16, 16),
(8, 8, 8), (0, 0, 0),
)
GREY_8: tuple[tuple[int, int, int], ...] = (
(255, 255, 255), (219, 219, 219), (182, 182, 182), (145, 145, 145), (109, 109, 109),
(72, 72, 72), (36, 36, 36), (0, 0, 0),
)
GLOBAL_512: tuple[tuple[int, int, int], ...] = (
(255, 255, 255), (241, 211, 179), (215, 174, 131), (187, 135, 93), (155, 99, 62),
(124, 73, 45), (94, 51, 31), (65, 33, 22), (37, 17, 11), (217, 217, 217),
(255, 237, 229), (255, 229, 211), (255, 216, 198), (248, 204, 191), (244, 191, 165),
(235, 184, 143), (224, 169, 132), (210, 151, 123), (179, 179, 179), (255, 214, 224),
(255, 220, 200), (255, 242, 184), (205, 236, 207), (201, 238, 240), (206, 221, 250),
(224, 210, 244), (234, 207, 227), (138, 139, 138), (228, 224, 174), (202, 199, 131),
(170, 169, 95), (137, 139, 67), (109, 113, 52), (83, 86, 41), (58, 60, 30),
(35, 36, 22), (111, 111, 111), (255, 241, 184), (255, 224, 138), (244, 201, 93),
(221, 175, 55), (196, 147, 36), (157, 113, 24), (116, 80, 15), (73, 48, 8),
(79, 79, 79), (185, 243, 232), (130, 221, 207), (78, 195, 181), (39, 164, 151),
(22, 132, 121), (14, 99, 92), (8, 68, 63), (3, 38, 34), (36, 36, 36),
(244, 192, 168), (231, 155, 124), (212, 118, 87), (185, 86, 62), (150, 61, 45),
(114, 43, 34), (79, 28, 24), (43, 14, 13), (0, 0, 0), (255, 176, 163),
(250, 243, 232), (239, 227, 210), (226, 209, 185), (211, 191, 162), (194, 169, 135),
(170, 144, 111), (141, 118, 92), (111, 92, 73), (255, 154, 137), (255, 224, 194),
(244, 194, 159), (223, 165, 126), (200, 137, 98), (170, 107, 73), (130, 80, 58),
(91, 53, 42), (53, 32, 27), (255, 85, 65), (141, 141, 141), (164, 164, 164),
(102, 102, 102), (255, 63, 36), (254, 58, 29), (254, 52, 22), (254, 47, 14),
(254, 41, 7), (254, 36, 0), (249, 35, 0), (244, 35, 0), (239, 34, 0),
(234, 33, 0), (229, 33, 1), (224, 32, 1), (219, 31, 1), (214, 31, 1),
(209, 30, 1), (202, 29, 1), (195, 28, 1), (188, 27, 1), (181, 26, 1),
(173, 25, 0), (166, 24, 0), (159, 23, 0), (152, 22, 0), (145, 21, 0),
(140, 20, 0), (135, 19, 0), (129, 19, 0), (124, 18, 0), (119, 17, 1),
(114, 16, 1), (108, 16, 1), (103, 15, 1), (98, 14, 1), (95, 14, 1),
(92, 13, 1), (88, 13, 1), (85, 12, 1), (82, 12, 0), (79, 11, 0),
(75, 11, 0), (72, 10, 0), (69, 10, 0), (255, 179, 99), (255, 178, 97),
(255, 177, 96), (255, 175, 94), (255, 174, 93), (255, 173, 91), (255, 172, 90),
(255, 170, 88), (255, 169, 87), (255, 168, 85), (255, 166, 81), (255, 163, 77),
(255, 161, 74), (255, 159, 70), (255, 156, 66), (255, 154, 62), (255, 152, 59),
(255, 149, 55), (255, 147, 51), (255, 145, 45), (255, 144, 40), (255, 142, 34),
(255, 140, 28), (255, 139, 23), (255, 137, 17), (255, 135, 11), (255, 134, 6),
(255, 132, 0), (252, 130, 0), (249, 129, 0), (246, 127, 0), (243, 126, 0),
(240, 124, 1), (237, 123, 1), (234, 121, 1), (231, 120, 1), (228, 118, 1),
(225, 116, 1), (221, 114, 1), (218, 113, 1), (214, 111, 1), (211, 109, 1),
(207, 107, 1), (204, 106, 1), (200, 104, 1), (197, 102, 1), (191, 99, 1),
(185, 96, 1), (179, 92, 1), (173, 89, 1), (166, 86, 0), (160, 83, 0),
(154, 79, 0), (148, 76, 0), (142, 73, 0), (140, 72, 0), (138, 71, 0),
(136, 70, 0), (134, 69, 0), (131, 68, 0), (129, 67, 0), (127, 66, 0),
(125, 65, 0), (123, 64, 0), (247, 229, 114), (247, 228, 111), (247, 227, 109),
(246, 227, 106), (246, 226, 104), (246, 225, 101), (246, 224, 99), (245, 224, 96),
(245, 223, 94), (245, 222, 91), (245, 221, 81), (244, 219, 71), (244, 218, 61),
(243, 216, 51), (243, 215, 40), (242, 213, 30), (242, 212, 20), (241, 210, 10),
(241, 209, 0), (239, 207, 0), (237, 205, 0), (235, 204, 1), (233, 202, 1),
(231, 200, 1), (229, 198, 1), (227, 197, 2), (225, 195, 2), (223, 193, 2),
(221, 191, 2), (219, 189, 2), (216, 188, 2), (214, 186, 2), (212, 184, 1),
(210, 182, 1), (207, 181, 1), (205, 179, 1), (203, 177, 1), (201, 175, 1),
(198, 173, 1), (196, 170, 1), (193, 168, 1), (191, 166, 2), (188, 164, 2),
(186, 161, 2), (183, 159, 2), (181, 157, 2), (177, 153, 2), (173, 150, 2),
(169, 146, 2), (165, 142, 2), (160, 139, 2), (156, 135, 2), (152, 131, 2),
(148, 128, 2), (144, 124, 2), (141, 122, 2), (139, 120, 2), (136, 118, 2),
(133, 116, 2), (131, 113, 2), (128, 111, 2), (125, 109, 2), (123, 107, 2),
(120, 105, 2), (183, 230, 155), (179, 229, 151), (175, 227, 147), (172, 226, 143),
(168, 225, 139), (164, 223, 134), (160, 222, 130), (157, 221, 126), (153, 219, 122),
(149, 218, 118), (145, 217, 113), (140, 215, 108), (136, 214, 104), (131, 212, 99),
(127, 211, 94), (122, 209, 89), (118, 208, 85), (113, 206, 80), (109, 205, 75),
(105, 204, 67), (101, 202, 58), (96, 201, 50), (92, 199, 42), (88, 198, 33),
(84, 196, 25), (79, 195, 17), (75, 193, 8), (71, 192, 0), (70, 190, 0),
(70, 188, 0), (69, 187, 0), (68, 185, 0), (68, 183, 0), (67, 181, 0),
(66, 180, 0), (66, 178, 0), (65, 176, 0), (64, 173, 0), (63, 170, 0),
(61, 167, 0), (60, 164, 0), (59, 160, 1), (58, 157, 1), (56, 154, 1),
(55, 151, 1), (54, 148, 1), (52, 144, 1), (51, 140, 1), (49, 135, 1),
(48, 131, 1), (46, 127, 0), (45, 123, 0), (43, 118, 0), (42, 114, 0),
(40, 110, 0), (39, 107, 0), (38, 103, 0), (36, 100, 0), (35, 96, 0),
(34, 93, 0), (33, 89, 0), (31, 86, 0), (30, 82, 0), (29, 79, 0),
(196, 241, 255), (193, 240, 255), (190, 239, 255), (188, 238, 255), (185, 237, 255),
(182, 237, 255), (179, 236, 255), (177, 235, 255), (174, 234, 255), (171, 233, 255),
(166, 232, 255), (161, 230, 255), (156, 229, 255), (151, 227, 255), (147, 226, 255),
(142, 224, 255), (137, 223, 255), (132, 221, 255), (127, 220, 255), (113, 217, 255),
(99, 214, 255), (85, 212, 255), (71, 209, 255), (57, 206, 255), (43, 203, 255),
(29, 201, 255), (15, 198, 255), (1, 195, 255), (1, 194, 253), (1, 192, 251),
(1, 191, 249), (1, 189, 247), (0, 188, 246), (0, 186, 244), (0, 185, 242),
(0, 183, 240), (0, 182, 238), (0, 181, 236), (0, 179, 235), (0, 178, 233),
(0, 177, 231), (1, 175, 230), (1, 174, 228), (1, 173, 226), (1, 171, 225),
(1, 170, 223), (1, 167, 219), (1, 164, 214), (2, 160, 210), (2, 157, 206),
(2, 154, 201), (2, 151, 197), (3, 147, 193), (3, 144, 188), (3, 141, 184),
(3, 137, 179), (3, 134, 175), (2, 130, 170), (2, 127, 166), (2, 123, 161),
(2, 120, 157), (1, 116, 152), (1, 113, 148), (1, 109, 143), (145, 170, 255),
(142, 168, 255), (139, 166, 255), (136, 163, 255), (133, 161, 255), (129, 159, 255),
(126, 157, 255), (123, 154, 255), (120, 152, 255), (117, 150, 255), (111, 144, 255),
(104, 139, 255), (98, 133, 255), (91, 128, 255), (85, 122, 255), (78, 117, 255),
(72, 111, 255), (61, 105, 255), (59, 100, 255), (52, 95, 255), (46, 91, 255),
(39, 86, 255), (33, 81, 255), (26, 77, 255), (20, 72, 255), (13, 67, 255),
(7, 63, 255), (0, 58, 255), (0, 57, 252), (0, 56, 248), (1, 56, 245),
(1, 55, 242), (1, 54, 238), (1, 53, 235), (2, 53, 232), (2, 52, 228),
(2, 51, 225), (2, 50, 222), (2, 50, 220), (2, 49, 217), (2, 49, 215),
(2, 48, 212), (2, 48, 210), (2, 47, 207), (2, 47, 205), (2, 46, 202),
(2, 45, 199), (2, 45, 196), (2, 44, 193), (2, 43, 190), (1, 43, 188),
(1, 42, 185), (1, 41, 182), (1, 41, 179), (1, 40, 176), (1, 39, 173),
(1, 39, 170), (1, 38, 167), (1, 37, 164), (0, 37, 162), (0, 36, 159),
(0, 35, 156), (0, 35, 153), (0, 34, 150), (215, 178, 255), (211, 173, 255),
(207, 168, 255), (203, 164, 255), (199, 159, 255), (194, 154, 255), (190, 149, 255),
(186, 145, 255), (182, 140, 255), (178, 135, 255), (175, 131, 255), (173, 127, 255),
(170, 124, 255), (167, 120, 255), (165, 116, 255), (162, 112, 255), (159, 109, 255),
(157, 105, 255), (154, 101, 255), (152, 97, 255), (149, 92, 255), (147, 88, 255),
(145, 83, 255), (142, 79, 255), (140, 74, 255), (138, 70, 255), (135, 65, 255),
(133, 61, 255), (132, 54, 255), (130, 47, 255), (129, 41, 255), (128, 34, 255),
(126, 27, 255), (125, 20, 255), (124, 14, 255), (122, 7, 255), (121, 0, 255),
(119, 0, 251), (117, 0, 247), (116, 1, 244), (114, 1, 240), (112, 1, 236),
(110, 1, 232), (109, 2, 229), (106, 0, 227), (105, 2, 221), (103, 2, 216),
(100, 2, 211), (98, 2, 206), (95, 2, 201), (93, 1, 195), (90, 1, 190),
(88, 1, 185), (85, 1, 180), (83, 1, 175), (79, 1, 167), (75, 1, 158),
(71, 1, 150), (67, 1, 142), (63, 0, 133), (59, 0, 125), (55, 0, 117),
(51, 0, 108), (47, 0, 100),
)
DYNAMIC_8: tuple[int, ...] = (
0, 83, 63, 91, 210, 283, 401, 484,
)
DYNAMIC_16: tuple[int, ...] = (
0, 84, 63, 100, 118, 155, 191, 210, 246, 292, 310, 338, 411, 447, 492, 511,
)
DYNAMIC_32: tuple[int, ...] = (
0, 18, 85, 63, 64, 82, 91, 118, 128, 155, 173, 182, 201, 210, 237, 255,
265, 283, 310, 319, 320, 347, 374, 383, 393, 411, 429, 447, 448, 457, 475, 511,
)
DYNAMIC_64: tuple[int, ...] = (
0, 9, 18, 27, 36, 45, 54, 63, 64, 73, 82, 91, 100, 109, 118, 127,
128, 137, 146, 155, 164, 173, 182, 191, 192, 201, 210, 219, 228, 237, 246, 255,
256, 265, 274, 283, 292, 301, 310, 319, 320, 329, 338, 347, 356, 365, 374, 383,
384, 393, 402, 411, 420, 429, 438, 447, 448, 457, 466, 475, 484, 493, 502, 511,
)
DYNAMIC_128: tuple[int, ...] = (
0, 9, 18, 27, 36, 45, 54, 63, 64, 73, 82, 91, 100, 109, 118, 127,
128, 137, 146, 155, 164, 173, 182, 191, 192, 201, 210, 219, 228, 237, 246, 255,
256, 265, 274, 283, 292, 301, 310, 319, 320, 329, 338, 347, 356, 365, 374, 383,
384, 393, 402, 411, 420, 429, 438, 447, 448, 457, 466, 475, 484, 493, 502, 511,
1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 16, 17,
19, 20, 21, 22, 23, 24, 25, 26, 28, 29, 30, 31, 32, 33, 34, 35,
37, 38, 39, 40, 41, 42, 43, 44, 46, 47, 48, 49, 50, 51, 52, 53,
55, 56, 57, 58, 59, 60, 61, 62, 65, 66, 67, 68, 69, 70, 71, 72,
)
DYNAMIC_256: tuple[int, ...] = (
0, 9, 18, 27, 36, 45, 54, 63, 64, 73, 82, 91, 100, 109, 118, 127,
128, 137, 146, 155, 164, 173, 182, 191, 192, 201, 210, 219, 228, 237, 246, 255,
256, 265, 274, 283, 292, 301, 310, 319, 320, 329, 338, 347, 356, 365, 374, 383,
384, 393, 402, 411, 420, 429, 438, 447, 448, 457, 466, 475, 484, 493, 502, 511,
1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 16, 17,
19, 20, 21, 22, 23, 24, 25, 26, 28, 29, 30, 31, 32, 33, 34, 35,
37, 38, 39, 40, 41, 42, 43, 44, 46, 47, 48, 49, 50, 51, 52, 53,
55, 56, 57, 58, 59, 60, 61, 62, 65, 66, 67, 68, 69, 70, 71, 72,
74, 75, 76, 77, 78, 79, 80, 81, 83, 84, 85, 86, 87, 88, 89, 90,
92, 93, 94, 95, 96, 97, 98, 99, 101, 102, 103, 104, 105, 106, 107, 108,
110, 111, 112, 113, 114, 115, 116, 117, 119, 120, 121, 122, 123, 124, 125, 126,
129, 130, 131, 132, 133, 134, 135, 136, 138, 139, 140, 141, 142, 143, 144, 145,
147, 148, 149, 150, 151, 152, 153, 154, 156, 157, 158, 159, 160, 161, 162, 163,
165, 166, 167, 168, 169, 170, 171, 172, 174, 175, 176, 177, 178, 179, 180, 181,
183, 184, 185, 186, 187, 188, 189, 190, 193, 194, 195, 196, 197, 198, 199, 200,
202, 203, 204, 205, 206, 207, 208, 209, 211, 212, 213, 214, 215, 216, 217, 218,
)
DYNAMIC_512: tuple[int, ...] = (
0, 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, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255,
256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271,
272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287,
288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303,
304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319,
320, 321, 322, 323, 324, 325, 326, 327, 328, 329, 330, 331, 332, 333, 334, 335,
336, 337, 338, 339, 340, 341, 342, 343, 344, 345, 346, 347, 348, 349, 350, 351,
352, 353, 354, 355, 356, 357, 358, 359, 360, 361, 362, 363, 364, 365, 366, 367,
368, 369, 370, 371, 372, 373, 374, 375, 376, 377, 378, 379, 380, 381, 382, 383,
384, 385, 386, 387, 388, 389, 390, 391, 392, 393, 394, 395, 396, 397, 398, 399,
400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415,
416, 417, 418, 419, 420, 421, 422, 423, 424, 425, 426, 427, 428, 429, 430, 431,
432, 433, 434, 435, 436, 437, 438, 439, 440, 441, 442, 443, 444, 445, 446, 447,
448, 449, 450, 451, 452, 453, 454, 455, 456, 457, 458, 459, 460, 461, 462, 463,
464, 465, 466, 467, 468, 469, 470, 471, 472, 473, 474, 475, 476, 477, 478, 479,
480, 481, 482, 483, 484, 485, 486, 487, 488, 489, 490, 491, 492, 493, 494, 495,
496, 497, 498, 499, 500, 501, 502, 503, 504, 505, 506, 507, 508, 509, 510, 511,
)
# fmt: on
+3 -1
View File
@@ -1219,7 +1219,9 @@ def _names_us(last: ChatMessage, us: tuple[str | None, str | None]) -> bool:
name = us[0]
if not name:
return False
body = split_channel_sender(last.text)[1] if last.is_channel and not last.outbound else last.text
body = (
split_channel_sender(last.text)[1] if last.is_channel and not last.outbound else last.text
)
if mcoimg.recognize(body) is not None:
return False # the Base91 text of an image can hold an ``@[`` by chance
return any(match.group(1) == name for match in MENTION.finditer(body))
+11
View File
@@ -40,6 +40,7 @@ from ..core.models import (
)
from ..core.regions import WILDCARD
from ..platforms import Platform, on_platform
from . import pixelart
from .compose import MentionEditor
from .pathline import PATH_INK, PathHop
from .qr import QrScreen, qr_strip
@@ -901,6 +902,10 @@ class ChatScreen(Screen):
:func:`~meshterm.ui.tui.render.render_hanging`). This is because a terminal opens
a link, and a user copies one, only while it is on one line.
A body that is a pixel image (MCOimg) shows a short line with its size, and the
image under it in half blocks, at the indent of the body (refer to
:mod:`~meshterm.ui.pixelart`).
Where the platform draws them (:data:`_URL_CODES`, the PicoCalc), each URL in the
body gets a QR code under it. The codes are side by side, at the indent of the
body. Thus the user can open a link from the handheld on a phone, and does not
@@ -945,6 +950,12 @@ class ChatScreen(Screen):
)
indent = prefix.cell_len
lines += render_hanging(Text(" " * indent), note, width, indent=indent)
if image is not None:
picture = pixelart.decoded(body)
if picture is not None:
indent = prefix.cell_len
art = pixelart.picture_text(picture, width - indent, indent=indent)
lines += render_lines(art, width, no_wrap=True)
urls = links.urls(body) if _URL_CODES and image is None else []
if urls:
lines += render_lines(qr_strip(urls, width, indent=prefix.cell_len), width)
+124
View File
@@ -0,0 +1,124 @@
# SPDX-License-Identifier: Apache-2.0
"""Draw a pixel image (MCOimg) in the terminal with half blocks.
A cell is approximately two times as tall as it is wide. Thus a cell holds two pixels, one
above the other, and each pixel is square: ``▀`` with the top pixel as its colour and the
bottom pixel as its background. A transparent pixel shows the page. A cell with only the
bottom pixel is ``▄`` in that colour, and a cell with no pixel is a space.
An image that is wider than the lane, or taller than the rows that the platform can give
it, becomes smaller (refer to :func:`fit`). Pixel art loses detail when it becomes smaller,
but an image that is cut loses more: the user sees only a part of it.
"""
from __future__ import annotations
from functools import lru_cache
from typing import TYPE_CHECKING
from rich.style import Style
from rich.text import Text
from ..core import mcoimg
from ..platforms import Platform, on_platform
if TYPE_CHECKING:
from ..core.mcoimg import Picture
RGB = tuple[int, int, int]
#: The most rows that an image can use under a message, on the active platform. The frame
#: takes some rows (the title bar, the footer, a header, the sender chip), so an image of
#: this height and its message are visible together.
_max_rows = 20
#: The rows of a frame that an image does not get: the title bar, the header or footer,
#: the sender chip, and the line of the message.
_CHROME_ROWS = 4
def _bind(platform: Platform) -> None:
global _max_rows
_max_rows = max(4, platform.readable_rows - _CHROME_ROWS)
on_platform(_bind)
@lru_cache(maxsize=128)
def decoded(body: str) -> Picture | None:
"""The image of a message body, or ``None``. It keeps the last results.
A transcript paints many times while a message is visible. Each paint asks again, and
the cache answers without a second decode.
"""
return mcoimg.decode(body)
def fit(width: int, height: int, max_cols: int, max_rows: int) -> tuple[int, int]:
"""The size in pixels at which to draw an image of ``width`` × ``height``.
The image keeps its proportions. It never becomes larger, and it becomes smaller only
to fit ``max_cols`` cells and ``max_rows`` rows (two pixels in each row).
"""
scale = min(1.0, max_cols / width, (max_rows * 2) / height)
return max(1, int(width * scale)), max(1, int(height * scale))
def picture_text(picture: Picture, max_cols: int, *, indent: int = 0) -> Text:
"""Draw ``picture`` as lines of half blocks, each after ``indent`` spaces.
Args:
picture: The decoded image.
max_cols: The most cells that the image can use, after the indent.
indent: The cells of page before each line (the indent of the body of a message).
Returns:
One :class:`~rich.text.Text` with a line for each two rows of pixels. It has no
newline after the last line.
"""
cols, rows = fit(picture.width, picture.height, max(1, max_cols), _max_rows)
pixel = _sampler(picture, cols, rows)
text = Text(no_wrap=True)
for y in range(0, rows, 2):
if y:
text.append("\n")
text.append(" " * indent)
for x in range(cols):
top = pixel(x, y)
bottom = pixel(x, y + 1) if y + 1 < rows else None
text.append(*_cell(top, bottom))
return text
def _sampler(picture: Picture, cols: int, rows: int): # noqa: ANN202
"""A function that gives the pixel at ``(x, y)`` of the image drawn at ``cols`` × ``rows``.
A smaller image takes the nearest pixel of the original, so the colours stay the
colours of the palette. A mix of colours makes pixel art look blurred.
"""
width, height, pixels = picture.width, picture.height, picture.pixels
def pixel(x: int, y: int) -> RGB | None:
return pixels[(y * height // rows) * width + (x * width // cols)]
return pixel
def _cell(top: RGB | None, bottom: RGB | None) -> tuple[str, Style | None]:
"""The character and the style of one cell, for its top and bottom pixels."""
if top is None and bottom is None:
return " ", None
if top is None:
return "▄", Style(color=_hex(bottom))
if bottom is None:
return "▀", Style(color=_hex(top))
if top == bottom:
return "█", Style(color=_hex(top))
return "▀", Style(color=_hex(top), bgcolor=_hex(bottom))
def _hex(rgb: RGB | None) -> str:
"""The colour of a pixel as ``#rrggbb``."""
r, g, b = rgb # type: ignore[misc]
return f"#{r:02x}{g:02x}{b:02x}"
+42
View File
@@ -0,0 +1,42 @@
# MCOimg test vectors
These two files are the test vectors of version 3 of the MCOimg pixel image format. They
come without change from the folder `docs/mcoimg-js/tests/` of the meshcore-open repository
([HDDen/meshcore-open](https://github.com/HDDen/meshcore-open), a fork of
[zjs81/meshcore-open](https://github.com/zjs81/meshcore-open)):
- `v3-js-decoder-fixtures.json`: 39 images, one or more for each container, block
algorithm, scan order, and local palette form.
- `v3-js-encoder-fixtures.json`: 6 images that the JavaScript encoder of the format made.
Each vector has the `im3:` text of an image, its size, its palette profile, and the palette
reference of each pixel. `tests/test_mcoimg.py` decodes each text and compares the result.
MeshTerm does not include these files in its package.
The files are under the MIT licence of the meshcore-open repository:
<!-- ste: off -->
MIT License
Copyright (c) 2025 zjs81
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
<!-- ste: on -->
@@ -0,0 +1,847 @@
{
"schema": 1,
"generator": "docs/mcoimg-js/tests/v3-decoder-fixtures.js",
"fixtures": [
{
"name": "rawGlobal",
"bodyBase64": "egGNAEK+AQ==",
"appPayloadBase64": "E3oBjQBCvgE=",
"text": "im3:g/5WjAL`NA",
"width": 4,
"height": 2,
"paletteProfile": "master4",
"pixels": [
0,
1,
2,
3,
3,
2,
1,
0
],
"transparentColor": null,
"container": "compactBlock",
"algorithm": "rawGlobal"
},
{
"name": "rawLocal",
"bodyBase64": "egGNECLWAg==",
"appPayloadBase64": "E3oBjRAi1gI=",
"text": "im3:g/5W6LuitA",
"width": 4,
"height": 2,
"paletteProfile": "master4",
"pixels": [
0,
3,
0,
3,
3,
0,
3,
0
],
"transparentColor": null,
"container": "compactBlock",
"algorithm": "rawLocal"
},
{
"name": "compactRle",
"bodyBase64": "egGNICCmIQ==",
"appPayloadBase64": "E3oBjSAgpiE=",
"text": "im3:g/5W[Wqi^F",
"width": 4,
"height": 2,
"paletteProfile": "master4",
"pixels": [
0,
0,
0,
3,
3,
0,
0,
0
],
"transparentColor": null,
"container": "block",
"algorithm": "compactRle"
},
{
"name": "compactSparse",
"bodyBase64": "egGNMABOIgA=",
"appPayloadBase64": "E3oBjTAATiIA",
"text": "im3:g/5WNi+>CGA",
"width": 4,
"height": 2,
"paletteProfile": "master4",
"pixels": [
0,
3,
3,
0,
0,
3,
0,
0
],
"transparentColor": null,
"container": "block",
"algorithm": "compactSparse"
},
{
"name": "biColorMask",
"bodyBase64": "egGNQMJa",
"appPayloadBase64": "E3oBjUDCWg==",
"text": "im3:g/5Wktw8F",
"width": 4,
"height": 2,
"paletteProfile": "master4",
"pixels": [
0,
3,
0,
3,
3,
0,
3,
0
],
"transparentColor": null,
"container": "compactBlock",
"algorithm": "biColorMask"
},
{
"name": "rowRepeat",
"bodyBase64": "egGNUCDW",
"appPayloadBase64": "E3oBjVAg1g==",
"text": "im3:g/5W74qiN",
"width": 4,
"height": 2,
"paletteProfile": "master4",
"pixels": [
0,
3,
0,
3,
0,
3,
0,
3
],
"transparentColor": null,
"container": "block",
"algorithm": "rowRepeat"
},
{
"name": "lzPixels",
"bodyBase64": "egGNYCAWHw==",
"appPayloadBase64": "E3oBjWAgFh8=",
"text": "im3:g/5W](qiqF",
"width": 4,
"height": 2,
"paletteProfile": "master4",
"pixels": [
0,
3,
0,
3,
0,
3,
0,
3
],
"transparentColor": null,
"container": "block",
"algorithm": "lzPixels"
},
{
"name": "quadtree",
"bodyBase64": "egEEB2J9",
"appPayloadBase64": "E3oBBAdifQ==",
"text": "im3:g/N\"qC8]D",
"width": 2,
"height": 2,
"paletteProfile": "master4",
"pixels": [
0,
3,
3,
0
],
"transparentColor": null,
"container": "block",
"algorithm": "quadtree"
},
{
"name": "bitplanesRawAndRle",
"bodyBase64": "egGNgGByqm0=",
"appPayloadBase64": "E3oBjYBgcqpt",
"text": "im3:g/5Wl\"#zslb",
"width": 4,
"height": 2,
"paletteProfile": "master4",
"pixels": [
0,
1,
0,
1,
2,
3,
2,
3
],
"transparentColor": null,
"container": "block",
"algorithm": "bitplanes"
},
{
"name": "adaptiveBitplanesRaw",
"bodyBase64": "egGNkCCmBQ==",
"appPayloadBase64": "E3oBjZAgpgU=",
"text": "im3:g/5W6Lri\"A",
"width": 4,
"height": 2,
"paletteProfile": "master4",
"pixels": [
0,
3,
0,
3,
3,
0,
3,
0
],
"transparentColor": null,
"container": "block",
"algorithm": "adaptiveBitplanes"
},
{
"name": "directBitplanes",
"bodyBase64": "egiNoEAVM3g=",
"appPayloadBase64": "E3oIjaBAFTN4",
"text": "im3:g/WX[Wyd~I8",
"width": 4,
"height": 2,
"paletteProfile": "grayscale8",
"pixels": [
0,
1,
2,
3,
4,
5,
6,
7
],
"transparentColor": null,
"container": "block",
"algorithm": "directBitplanes"
},
{
"name": "compactRowDelta",
"bodyBase64": "egGNsCCmAA==",
"appPayloadBase64": "E3oBjbAgpgA=",
"text": "im3:g/5WNiriKA",
"width": 4,
"height": 2,
"paletteProfile": "master4",
"pixels": [
0,
3,
0,
3,
0,
3,
0,
3
],
"transparentColor": null,
"container": "block",
"algorithm": "compactRowDelta"
},
{
"name": "directRowDelta",
"bodyBase64": "egiNwADRAA==",
"appPayloadBase64": "E3oIjcAA0QA=",
"text": "im3:g/WXkt6FNA",
"width": 4,
"height": 2,
"paletteProfile": "grayscale8",
"pixels": [
0,
1,
2,
3,
0,
1,
2,
3
],
"transparentColor": null,
"container": "block",
"algorithm": "directRowDelta"
},
{
"name": "rowDelta",
"bodyBase64": "egGN0CBGAw==",
"appPayloadBase64": "E3oBjdAgRgM=",
"text": "im3:g/5W74ri0A",
"width": 4,
"height": 2,
"paletteProfile": "master4",
"pixels": [
0,
3,
0,
3,
0,
3,
0,
3
],
"transparentColor": null,
"container": "block",
"algorithm": "rowDelta"
},
{
"name": "varUintRle",
"bodyBase64": "egGN4CA2UMAA",
"appPayloadBase64": "E3oBjeAgNlDAAA==",
"text": "im3:g/5W](riJOFBA",
"width": 4,
"height": 2,
"paletteProfile": "master4",
"pixels": [
0,
0,
0,
3,
3,
0,
0,
0
],
"transparentColor": null,
"container": "block",
"algorithm": "varUintRle"
},
{
"name": "varUintSparse",
"bodyBase64": "egGN8AAWCBAQCAA=",
"appPayloadBase64": "E3oBjfAAFggQEAgA",
"text": "im3:g/5WO?+hmB2WgAA",
"width": 4,
"height": 2,
"paletteProfile": "master4",
"pixels": [
0,
3,
3,
0,
0,
3,
0,
0
],
"transparentColor": null,
"container": "block",
"algorithm": "varUintSparse"
},
{
"name": "adaptiveLegacyRle",
"bodyBase64": "egEdkCDOBg==",
"appPayloadBase64": "E3oBHZAgzgY=",
"text": "im3:g/5WeLs?RB",
"width": 8,
"height": 1,
"paletteProfile": "master4",
"pixels": [
0,
0,
0,
0,
3,
3,
3,
3
],
"transparentColor": null,
"container": "block",
"algorithm": "adaptiveBitplanes"
},
{
"name": "adaptiveShortRle",
"bodyBase64": "egEdkCCeNg==",
"appPayloadBase64": "E3oBHZAgnjY=",
"text": "im3:g/5WeLs?2J",
"width": 8,
"height": 1,
"paletteProfile": "master4",
"pixels": [
0,
0,
3,
3,
3,
0,
0,
0
],
"transparentColor": null,
"container": "block",
"algorithm": "adaptiveBitplanes"
},
{
"name": "adaptiveConstantZero",
"bodyBase64": "egEdkCA+",
"appPayloadBase64": "E3oBHZAgPg==",
"text": "im3:g/5WeLs?D",
"width": 8,
"height": 1,
"paletteProfile": "master4",
"pixels": [
0,
0,
0,
0,
0,
0,
0,
0
],
"transparentColor": null,
"container": "block",
"algorithm": "adaptiveBitplanes"
},
{
"name": "adaptiveConstantOne",
"bodyBase64": "egEdkCB+",
"appPayloadBase64": "E3oBHZAgfg==",
"text": "im3:g/5WeLs?H",
"width": 8,
"height": 1,
"paletteProfile": "master4",
"pixels": [
3,
3,
3,
3,
3,
3,
3,
3
],
"transparentColor": null,
"container": "block",
"algorithm": "adaptiveBitplanes"
},
{
"name": "adaptiveSparseOne",
"bodyBase64": "egEdkCC+CQE=",
"appPayloadBase64": "E3oBHZAgvgkB",
"text": "im3:g/5WeLs?%uA",
"width": 8,
"height": 1,
"paletteProfile": "master4",
"pixels": [
0,
3,
0,
0,
0,
0,
3,
0
],
"transparentColor": null,
"container": "block",
"algorithm": "adaptiveBitplanes"
},
{
"name": "adaptiveSparseZero",
"bodyBase64": "egEdkCD+CQE=",
"appPayloadBase64": "E3oBHZAg/gkB",
"text": "im3:g/5WeLs?*uA",
"width": 8,
"height": 1,
"paletteProfile": "master4",
"pixels": [
3,
0,
3,
3,
3,
3,
0,
3
],
"transparentColor": null,
"container": "block",
"algorithm": "adaptiveBitplanes"
},
{
"name": "boundsBlock",
"bodyBase64": "egEMQgQEBATgAQ==",
"appPayloadBase64": "E3oBDEIEBAQE4AE=",
"text": "im3:g/N\"UXbLi\"+>BA",
"width": 4,
"height": 4,
"paletteProfile": "master4",
"pixels": [
0,
0,
0,
0,
0,
3,
3,
0,
0,
3,
3,
0,
0,
0,
0,
0
],
"transparentColor": null,
"container": "boundsBlock",
"algorithm": "compactRle"
},
{
"name": "compactBoundsBlock",
"bodyBase64": "egEMYFTlAQ==",
"appPayloadBase64": "E3oBDGBU5QE=",
"text": "im3:g/N\"+h\"OPA",
"width": 4,
"height": 4,
"paletteProfile": "master4",
"pixels": [
0,
0,
0,
0,
0,
1,
2,
0,
0,
3,
1,
0,
0,
0,
0,
0
],
"transparentColor": null,
"container": "compactBoundsBlock",
"algorithm": "rawGlobal"
},
{
"name": "regions",
"bodyBase64": "egEMgAQEBAQEtQE=",
"appPayloadBase64": "E3oBDIAEBAQEBLUB",
"text": "im3:g/N\"CtbLi\"lB/EA",
"width": 4,
"height": 4,
"paletteProfile": "master4",
"pixels": [
0,
0,
0,
0,
0,
1,
3,
0,
0,
3,
1,
0,
0,
0,
0,
0
],
"transparentColor": null,
"container": "regions",
"algorithm": "rawGlobal"
},
{
"name": "compactRegionsSharedDelta",
"bodyBase64": "egGNEMqDGrUAQAI=",
"appPayloadBase64": "E3oBjRDKgxq1AEAC",
"text": "im3:g/5W6LdV9x\"AdZA",
"width": 4,
"height": 2,
"paletteProfile": "master4",
"pixels": [
1,
3,
3,
1,
3,
1,
1,
3
],
"transparentColor": null,
"container": "compactRegionsStream",
"algorithm": "rawGlobal"
},
{
"name": "solidBackground",
"bodyBase64": "egEAwg==",
"appPayloadBase64": "E3oBAMI=",
"text": "im3:g/LAU)",
"width": 1,
"height": 1,
"paletteProfile": "master4",
"pixels": [
2
],
"transparentColor": null,
"container": "solidBackground",
"algorithm": "rawGlobal"
},
{
"name": "solidRects",
"bodyBase64": "egEM4GBV",
"appPayloadBase64": "E3oBDOBgVQ==",
"text": "im3:g/N\",>M8C",
"width": 4,
"height": 4,
"paletteProfile": "master4",
"pixels": [
0,
0,
0,
0,
0,
3,
3,
0,
0,
3,
3,
0,
0,
0,
0,
0
],
"transparentColor": null,
"container": "solidRects",
"algorithm": "rawGlobal"
},
{
"name": "implicitWhiteSolid",
"bodyBase64": "ekEEwA==",
"appPayloadBase64": "E3pBBMA=",
"text": "im3:g/)FCtB",
"width": 2,
"height": 2,
"paletteProfile": "master4",
"pixels": [
0,
0,
0,
0
],
"transparentColor": null,
"container": "solidBackground",
"algorithm": "rawGlobal"
},
{
"name": "transparentSolid",
"bodyBase64": "eoEEwgM=",
"appPayloadBase64": "E3qBBMID",
"text": "im3:g/iLnuHA",
"width": 2,
"height": 2,
"paletteProfile": "master4",
"pixels": [
2,
2,
2,
2
],
"transparentColor": 3,
"container": "solidBackground",
"algorithm": "rawGlobal"
},
{
"name": "scanV",
"bodyBase64": "ehGJIGByEGQQ",
"appPayloadBase64": "E3oRiSBgchBkEA==",
"text": "im3:g/DY@WhN[CFXA",
"width": 3,
"height": 2,
"paletteProfile": "master4",
"pixels": [
0,
2,
0,
1,
3,
1
],
"transparentColor": null,
"container": "block",
"algorithm": "compactRle"
},
{
"name": "scanS",
"bodyBase64": "eiGJIGByEGQQ",
"appPayloadBase64": "E3ohiSBgchBkEA==",
"text": "im3:g/oZ@WhN[CFXA",
"width": 3,
"height": 2,
"paletteProfile": "master4",
"pixels": [
0,
1,
2,
1,
0,
3
],
"transparentColor": null,
"container": "block",
"algorithm": "compactRle"
},
{
"name": "scanSV",
"bodyBase64": "ejGJIGByEGQQ",
"appPayloadBase64": "E3oxiSBgchBkEA==",
"text": "im3:g/=a@WhN[CFXA",
"width": 3,
"height": 2,
"paletteProfile": "master4",
"pixels": [
0,
3,
0,
1,
2,
1
],
"transparentColor": null,
"container": "block",
"algorithm": "compactRle"
},
{
"name": "paletteBitmap",
"bodyBase64": "egENEJI0",
"appPayloadBase64": "E3oBDRCSNA==",
"text": "im3:g/5WaL%ZD",
"width": 4,
"height": 1,
"paletteProfile": "master4",
"pixels": [
0,
3,
3,
0
],
"transparentColor": null,
"container": "compactBlock",
"algorithm": "rawLocal"
},
{
"name": "paletteSortedDelta",
"bodyBase64": "egINEDJBIgM=",
"appPayloadBase64": "E3oCDRAyQSID",
"text": "im3:g/%WaL&GDzB",
"width": 4,
"height": 1,
"paletteProfile": "master8",
"pixels": [
0,
2,
4,
2
],
"transparentColor": null,
"container": "compactBlock",
"algorithm": "rawLocal"
},
{
"name": "paletteRangeRuns",
"bodyBase64": "egINENJAFTk=",
"appPayloadBase64": "E3oCDRDSQBU5",
"text": "im3:g/%WaL4E)wc",
"width": 4,
"height": 1,
"paletteProfile": "master8",
"pixels": [
0,
1,
5,
6
],
"transparentColor": null,
"container": "compactBlock",
"algorithm": "rawLocal"
},
{
"name": "paletteBankBitmaps",
"bodyBase64": "eg8NEPIrEAAAAAAAAAAgAAAAAAAAAFA=",
"appPayloadBase64": "E3oPDRDyKxAAAAAAAAAAIAAAAAAAAABQ",
"text": "im3:g/>XaLT(<CAAAAAAAAQAAAAAAAuWB",
"width": 4,
"height": 1,
"paletteProfile": "dynamicGlobal512",
"pixels": [
1,
130,
1,
130
],
"transparentColor": null,
"container": "compactBlock",
"algorithm": "rawLocal"
},
{
"name": "paletteOrderedSingleBank",
"bodyBase64": "eg8NEPInWJBEZA==",
"appPayloadBase64": "E3oPDRDyJ1iQRGQ=",
"text": "im3:g/>XaL$stPmGlE",
"width": 4,
"height": 1,
"paletteProfile": "dynamicGlobal512",
"pixels": [
193,
201,
209,
201
],
"transparentColor": null,
"container": "compactBlock",
"algorithm": "rawLocal"
},
{
"name": "paletteOrderedMultiBank",
"bodyBase64": "eg8NEPI3LBAUDAly",
"appPayloadBase64": "E3oPDRDyNywQFAwJcg==",
"text": "im3:g/>XaL$s+HUc,LJo",
"width": 4,
"height": 1,
"paletteProfile": "dynamicGlobal512",
"pixels": [
1,
130,
3,
132
],
"transparentColor": null,
"container": "compactBlock",
"algorithm": "rawLocal"
}
]
}
@@ -0,0 +1,792 @@
{
"schema": 1,
"generator": "mcoimg-v3-codec.global.js Normal/High encoder",
"fixtures": [
{
"name": "mono-checker-normal",
"width": 9,
"height": 7,
"paletteProfile": "mono",
"pixels": [
0,
1,
0,
1,
0,
1,
0,
1,
0,
1,
0,
1,
0,
1,
0,
1,
0,
1,
0,
1,
0,
1,
0,
1,
0,
1,
0,
1,
0,
1,
0,
1,
0,
1,
0,
1,
0,
1,
0,
1,
0,
1,
0,
1,
0,
1,
0,
1,
0,
1,
0,
1,
0,
1,
0,
1,
0,
1,
0,
1,
0,
1,
0
],
"compressionLevel": "normal",
"maxRegions": 16,
"bodyBase64": "QAAhY6DkMwE=",
"appPayloadBase64": "E0AAIWOg5DMB",
"text": "im3:TAYL>iBNGbA",
"byteLength": 8,
"charLength": 15,
"subtypeVersion": 19,
"packetNonce": 64,
"mode": "extended",
"scan": "h",
"container": "block",
"algorithm": "lzPixels",
"backgroundColor": 0,
"regionCount": 0
},
{
"name": "master8-sparse-high",
"width": 12,
"height": 10,
"paletteProfile": "master8",
"pixels": [
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
1,
1,
1,
1,
0,
0,
0,
0,
0,
0,
0,
0,
1,
2,
2,
1,
0,
0,
0,
0,
0,
0,
0,
0,
1,
2,
2,
1,
0,
0,
0,
0,
0,
0,
0,
0,
1,
1,
1,
1,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
3,
3,
3,
0,
0,
0,
0,
0,
0,
0,
0,
0,
3,
4,
3,
0,
0,
0,
0,
0,
0,
0,
0,
0,
3,
3,
3,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0
],
"compressionLevel": "high",
"maxRegions": 32,
"bodyBase64": "QUKtFJoQMSMRmAGWkhEC",
"appPayloadBase64": "E0FCrRSaEDEjEZgBlpIRAg==",
"text": "im3:CDecxOjD61JY`R)0nxQA",
"byteLength": 15,
"charLength": 24,
"subtypeVersion": 19,
"packetNonce": 65,
"mode": "regionsBg",
"scan": "h",
"container": "compactRegionsStream",
"algorithm": "rawGlobal",
"backgroundColor": 0,
"regionCount": 2
},
{
"name": "master16-regions-high",
"width": 16,
"height": 12,
"paletteProfile": "master16",
"pixels": [
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
1,
1,
1,
1,
0,
0,
0,
0,
0,
0,
0,
2,
2,
0,
0,
0,
1,
3,
3,
1,
0,
0,
0,
0,
0,
0,
0,
2,
2,
0,
0,
0,
1,
3,
3,
1,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
1,
1,
1,
1,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
4,
4,
4,
4,
4,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
4,
5,
5,
5,
4,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
4,
5,
6,
5,
4,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
4,
5,
5,
5,
4,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
4,
4,
4,
4,
4,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0
],
"compressionLevel": "high",
"maxRegions": 32,
"bodyBase64": "QkO9JZpExIyIAWbAUQApIxZEwCkHLkI=",
"appPayloadBase64": "E0JDvSWaRMSMiAFmwFEAKSMWRMApBy5C",
"text": "im3:<Fmcxa[ZuiCM1(7([Azj,H)A3@QQ&A",
"byteLength": 23,
"charLength": 34,
"subtypeVersion": 19,
"packetNonce": 66,
"mode": "regionsBg",
"scan": "h",
"container": "compactRegionsStream",
"algorithm": "rawGlobal",
"backgroundColor": 0,
"regionCount": 3
},
{
"name": "grayscale16-gradient-high",
"width": 16,
"height": 8,
"paletteProfile": "grayscale16",
"pixels": [
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
0,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
0,
1,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
0,
1,
2,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
0,
1,
2,
3,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
0,
1,
2,
3,
4,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
0,
1,
2,
3,
4,
5,
7,
8,
9,
10,
11,
12,
13,
14,
15,
0,
1,
2,
3,
4,
5,
6
],
"compressionLevel": "high",
"maxRegions": 16,
"bodyBase64": "Qwa9wwBChsoOU5fb//////8=",
"appPayloadBase64": "E0MGvcMAQobKDlOX2///////",
"text": "im3:7IFX6vYL3X/Um]T=B\"B\"B\"B",
"byteLength": 17,
"charLength": 27,
"subtypeVersion": 19,
"packetNonce": 67,
"mode": "extended",
"scan": "h",
"container": "block",
"algorithm": "directRowDelta",
"backgroundColor": 0,
"regionCount": 0
},
{
"name": "master8-transparent-normal",
"width": 8,
"height": 8,
"paletteProfile": "master8",
"transparentColor": 0,
"pixels": [
0,
0,
0,
0,
0,
0,
0,
0,
0,
1,
1,
1,
1,
1,
1,
0,
0,
1,
2,
2,
2,
2,
1,
0,
0,
1,
2,
3,
3,
2,
1,
0,
0,
1,
2,
3,
3,
2,
1,
0,
0,
1,
2,
2,
2,
2,
1,
0,
0,
1,
1,
1,
1,
1,
1,
0,
0,
0,
0,
0,
0,
0,
0,
0
],
"compressionLevel": "normal",
"maxRegions": 16,
"bodyBase64": "RMIcbUjaONR6LWPLtB4=",
"appPayloadBase64": "E0TCHG1I2jjUei1jy7Qe",
"text": "im3:qLHR(<=5\"J)])jv,i{A",
"byteLength": 14,
"charLength": 23,
"subtypeVersion": 19,
"packetNonce": 68,
"mode": "rowDelta",
"scan": "h",
"container": "compactBoundsBlock",
"algorithm": "rowDelta",
"backgroundColor": 0,
"regionCount": 0
},
{
"name": "dynamic-global8-high",
"width": 10,
"height": 8,
"paletteProfile": "dynamicGlobal8",
"pixels": [
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
83,
83,
83,
0,
0,
63,
63,
63,
0,
0,
83,
91,
83,
0,
0,
63,
210,
63,
0,
0,
83,
83,
83,
0,
0,
63,
63,
63,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
283,
283,
283,
0,
0,
401,
401,
401,
0,
0,
283,
484,
283,
0,
0,
401,
484,
401,
0,
0,
283,
283,
283,
0,
0,
401,
401,
401,
0
],
"compressionLevel": "high",
"maxRegions": 32,
"bodyBase64": "RUmlM5oQEUkGwaJEhBAVpYdgVfEhAA==",
"appPayloadBase64": "E0VJpTOaEBFJBsGiRIQQFaWHYFXxIQA=",
"text": "im3:ZO{cdkjDBw<I\"eMYqugYiFXeo|QAA",
"byteLength": 22,
"charLength": 33,
"subtypeVersion": 19,
"packetNonce": 69,
"mode": "regionsBg",
"scan": "h",
"container": "compactRegionsStream",
"algorithm": "rawGlobal",
"backgroundColor": 0,
"regionCount": 4
}
]
}
+6 -4
View File
@@ -979,14 +979,16 @@ def test_chat_screen_shows_delivery_glyphs() -> None:
def test_a_pixel_image_shows_a_short_line_not_its_text() -> None:
"""A body that is an MCOimg image shows its size in place of its Base91 text."""
"""A body that is an MCOimg image shows its size and the image, not its Base91 text."""
text = "im3:g/5WjAL`NA"
messages = [ChatMessage(text=text, peer="d4e5f6a7")]
screen = _screen(_StubSession(), send=None, messages=messages)
lines = [_strip_ansi(ln) for ln in screen._body_lines(text, messages[0], 60)]
assert len(lines) == 1
assert lines[0].endswith("image · 4×2 · 4 colours")
assert "g/5W" not in lines[0]
assert not any("g/5W" in line for line in lines)
# Under the line, the 4 x 2 image is one row of four half blocks at the body indent.
assert len(lines) == 2
assert lines[1].strip() and set(lines[1].strip()) <= set("▀▄█")
def test_wrapped_body_hangs_under_the_first_line() -> None:
@@ -3909,4 +3911,4 @@ def test_public_offer_add_writes_slot_zero(monkeypatch: pytest.MonkeyPatch) -> N
ctx, writes = _offer_ctx(True, monkeypatch)
added = asyncio.run(_add_public_channel(ctx))
assert writes == [(0, "Public", DEFAULT_PUBLIC_SECRET)]
assert added is not None and added.on_device and added.channel_idx == 0
assert added is not None and added.on_device and added.channel_idx == 0
+251 -1
View File
@@ -9,9 +9,15 @@ the palette profile that its encoder used.
from __future__ import annotations
import json
import random
import time
from pathlib import Path
import pytest
from meshterm.core.mcoimg import PixelImage, base91_decode, recognize
from meshterm.core import mcoimg, mcoimg_palettes
from meshterm.core.mcoimg import PixelImage, _decode_refs, base91_decode, decode, recognize
#: (text, width, height, colours) from the version 3 vectors: one case for each mode of
#: the dimensions and for each palette profile that the vectors use.
@@ -27,6 +33,7 @@ _V3_CASES = [
def test_a_version_3_image_gives_its_size_and_colours(
text: str, width: int, height: int, colours: int
) -> None:
"""The header of a version 3 image gives its size and the colours of its profile."""
assert recognize(text) == PixelImage(3, width, height, colours)
@@ -36,10 +43,12 @@ def test_base91_decodes_to_the_payload_of_the_vector() -> None:
def test_a_version_1_or_2_image_has_no_header_to_read() -> None:
"""An ``im:`` image is an image, but MeshTerm cannot read its header."""
assert recognize("im:CC&tj68MFSB") == PixelImage(None)
def test_spaces_at_the_ends_are_permitted() -> None:
"""Spaces and newlines around the image do not stop the recognition."""
assert recognize(" im3:g/5WjAL`NA \n") == PixelImage(3, 4, 2, 4)
@@ -56,8 +65,249 @@ def test_spaces_at_the_ends_are_permitted() -> None:
],
)
def test_text_that_is_not_an_image(body: str) -> None:
"""Words that a person typed are not an image."""
assert recognize(body) is None
def test_an_unknown_version_is_an_image_with_no_header() -> None:
"""A version that MeshTerm does not know is an image with no header."""
assert recognize("im4:CC&tj68MFSB") == PixelImage(4)
# The pixel decoder of version 3.
_DATA = Path(__file__).parent / "data" / "mcoimg"
#: The palette profiles by name, in the order of their ids.
_PROFILE_NAMES = (
"mono",
"master4",
"master8",
"master16",
"master32",
"master64",
"grayscale16",
"grayscale32",
"grayscale8",
"dynamicGlobal8",
"dynamicGlobal16",
"dynamicGlobal32",
"dynamicGlobal64",
"dynamicGlobal128",
"dynamicGlobal256",
"dynamicGlobal512",
)
def _vectors() -> list[dict]:
"""All the vectors of the two files of ``tests/data/mcoimg/``."""
found = []
for name in ("v3-js-decoder-fixtures.json", "v3-js-encoder-fixtures.json"):
found += json.loads((_DATA / name).read_text(encoding="utf-8"))["fixtures"]
return found
_VECTORS = _vectors()
def test_the_vector_files_have_all_their_cases() -> None:
"""The two vector files have 39 and 6 cases."""
assert len(_VECTORS) == 45
@pytest.mark.parametrize("vector", _VECTORS, ids=[vector["name"] for vector in _VECTORS])
def test_a_vector_decodes_to_its_palette_references(vector: dict) -> None:
"""Each vector decodes to its size, its profile, and the reference of each pixel."""
indexed = _decode_refs(vector["text"])
assert indexed is not None
assert (indexed.width, indexed.height) == (vector["width"], vector["height"])
assert _PROFILE_NAMES[indexed.profile] == vector["paletteProfile"]
assert indexed.refs == vector["pixels"]
assert indexed.transparent == vector.get("transparentColor")
def _rgb(profile: str, ref: int) -> tuple[int, int, int]:
"""The colour of a palette reference of a profile, from the colour tables."""
if profile.startswith("dynamic"):
return mcoimg_palettes.GLOBAL_512[ref]
names = {
"mono": "MONO",
"grayscale8": "GREY_8",
"grayscale16": "GREY_16",
"grayscale32": "GREY_32",
}
name = names.get(profile) or "MASTER_" + profile.removeprefix("master")
return getattr(mcoimg_palettes, name)[ref]
@pytest.mark.parametrize("vector", _VECTORS, ids=[vector["name"] for vector in _VECTORS])
def test_a_vector_decodes_to_the_colours_of_its_profile(vector: dict) -> None:
"""Each vector decodes to the RGB colours of its profile, with transparent pixels."""
picture = decode(vector["text"])
assert picture is not None
assert (picture.width, picture.height) == (vector["width"], vector["height"])
clear = vector.get("transparentColor")
expected = tuple(
None if ref == clear else _rgb(vector["paletteProfile"], ref) for ref in vector["pixels"]
)
assert picture.pixels == expected
def test_mono_is_white_and_black() -> None:
"""In the ``mono`` profile, colour 0 is white and colour 1 is black."""
picture = decode("im3:TAYL>iBNGbA") # mono-checker-normal, 9 x 7
assert picture is not None
assert picture.pixels[:2] == ((255, 255, 255), (0, 0, 0))
assert set(picture.pixels) == {(255, 255, 255), (0, 0, 0)}
def test_the_transparent_colour_gives_no_colour() -> None:
"""A pixel of the transparent colour has no colour."""
vector = next(v for v in _VECTORS if v["name"] == "master8-transparent-normal")
picture = decode(vector["text"])
assert picture is not None
clear = [pixel is None for pixel in picture.pixels]
assert clear == [ref == 0 for ref in vector["pixels"]]
assert any(clear)
assert not all(clear)
def test_the_colour_tables_have_their_sizes() -> None:
"""Each colour table has the size of its profile, and a dynamic table has no duplicates."""
sizes = {
"MONO": 2,
"MASTER_4": 4,
"MASTER_8": 8,
"MASTER_16": 16,
"MASTER_32": 32,
"MASTER_64": 64,
"GREY_8": 8,
"GREY_16": 16,
"GREY_32": 32,
"GLOBAL_512": 512,
}
for name, size in sizes.items():
assert len(getattr(mcoimg_palettes, name)) == size, name
for size in (8, 16, 32, 64, 128, 256, 512):
table = getattr(mcoimg_palettes, f"DYNAMIC_{size}")
assert len(table) == size
assert len(set(table)) == size
assert all(0 <= index < 512 for index in table)
assert mcoimg_palettes.DYNAMIC_512 == tuple(range(512))
def test_decode_reads_only_version_3() -> None:
"""The decoder gives ``None`` for an image of version 1 or 2 and for words."""
assert decode("im:CC&tj68MFSB") is None
assert decode("hello") is None
assert decode("im3:hello") is None
@pytest.mark.parametrize("vector", _VECTORS, ids=[vector["name"] for vector in _VECTORS])
def test_a_truncated_image_gives_none(vector: dict) -> None:
"""An image with the end of its text cut off gives ``None``."""
text = vector["text"]
for end in range(len("im3:"), len(text) - 1):
assert decode(text[:end]) is None
def test_a_changed_image_does_not_raise() -> None:
"""Change each character of each vector. The result can be an image or ``None``."""
alphabet = mcoimg._ALPHABET
for vector in _VECTORS:
text = vector["text"]
for index in range(len("im3:"), len(text)):
for char in alphabet[::7]:
decode(text[:index] + char + text[index + 1 :])
def test_random_base91_does_not_raise() -> None:
"""Random Base91 text after the prefix gives a picture or ``None``, and never an error."""
chooser = random.Random(1234)
for _ in range(3000):
length = chooser.randint(1, 200)
text = "".join(chooser.choice(mcoimg._ALPHABET) for _ in range(length))
decode("im3:g/" + text)
decode("im3:" + text)
def test_a_very_long_text_gives_none() -> None:
"""The decoder does not read a text that is longer than the limit."""
assert decode("im3:g/" + "A" * 10_000) is None
def _encode_raw_global(width: int, height: int, profile: int, refs: list[int]) -> str:
"""Encode an image as ``compactBlock`` and ``rawGlobal``: the minimal encoder of the spec.
Only square sizes up to 64 are done here (dimension mode 0).
"""
assert width == height <= 64
bits = max(1, (len(_PROFILE_COLOURS_BY_ID[profile]) - 1).bit_length())
fields = [(0, 2), (width - 1, 6), (0x20, 8)] + [(ref, bits) for ref in refs]
value = position = 0
for field, count in fields:
value |= field << position
position += count
data = bytes([0x13, 0x5A, profile]) + value.to_bytes((position + 7) // 8, "little")
return "im3:" + _base91_encode(data)
def _base91_encode(data: bytes) -> str:
"""The basE91 encoder of the spec."""
alphabet = mcoimg._ALPHABET
out = []
queue = count = 0
for byte in data:
queue |= byte << count
count += 8
if count > 13:
value = queue & 8191
if value > 88:
queue >>= 13
count -= 13
else:
value = queue & 16383
queue >>= 14
count -= 14
out += [alphabet[value % 91], alphabet[value // 91]]
if count:
out.append(alphabet[queue % 91])
if count > 7 or queue > 90:
out.append(alphabet[queue // 91])
return "".join(out)
_PROFILE_COLOURS_BY_ID = {5: mcoimg_palettes.MASTER_64}
def test_a_64_by_64_image_decodes_quickly() -> None:
"""A 64 x 64 image of 64 colours decodes in less than 50 ms."""
chooser = random.Random(5)
refs = [chooser.randrange(64) for _ in range(64 * 64)]
text = _encode_raw_global(64, 64, 5, refs)
start = time.perf_counter()
picture = decode(text)
elapsed = time.perf_counter() - start
assert picture is not None
assert picture.pixels == tuple(mcoimg_palettes.MASTER_64[ref] for ref in refs)
assert elapsed < 0.05
def test_a_picture_draws_two_pixels_in_each_cell() -> None:
"""Half blocks: the top pixel is the colour, the bottom pixel is the background."""
from meshterm.ui.pixelart import picture_text
red, blue = (255, 0, 0), (0, 0, 255)
picture = mcoimg.Picture(2, 3, (red, None, blue, red, red, None))
lines = picture_text(picture, 10, indent=2).plain.split("\n")
# Row 0 over row 1: red over blue, nothing over red. Row 2 alone: red, nothing.
assert lines == [" ▀▄", " ▀ "]
def test_a_wide_picture_becomes_smaller_to_fit_the_lane() -> None:
"""An image wider than the lane keeps its proportions and fits the lane."""
from meshterm.ui.pixelart import fit
assert fit(64, 64, 40, 40) == (40, 40)
assert fit(16, 16, 40, 40) == (16, 16) # never larger
assert fit(30, 30, 53, 5) == (10, 10) # five rows hold ten pixels