Free games for graphing calculators: NumWorks, TI-Nspire CX, Casio fx-CG50 and soon TI-84 Plus CE. Balatro, Celeste, Hollow Knight, Minecraft and more.
C calculatorcalculator-gamescasiochesscrossy-roaddrive-madfx-cg50gamesgeometry-dashgraphing-calculatorndlessnumworksnumworks-appsnumworks-calculatornumworks-gamenumworks-gamestetristi-84-plus-ceti-nspire
Cookie Clicker: its pictures 300 KB smaller, drawn the same
Mason3636a17a80514a2
diff --git a/games/cookieclicker/src/data.bin b/games/cookieclicker/src/data.binindex 7c19b584..1d6677df 100644Binary files a/games/cookieclicker/src/data.bin and b/games/cookieclicker/src/data.bin differdiff --git a/games/cookieclicker/src/data.h b/games/cookieclicker/src/data.hindex 64a16a27..8308c0d8 100644--- a/games/cookieclicker/src/data.h+++ b/games/cookieclicker/src/data.h@@ -5,7 +5,7 @@#define MILK_TILE 178#define ICON_COLS 36#define ICON_ROWS 37-#define OFF_ICONS 1421904+#define OFF_ICONS 1118216#define F_TITLE 0#define F_NAME 1#define F_NAME_SMALL 2@@ -15,8 +15,8 @@#define F_ITALIC 6#define F_SMALL 7#define F_KAVOON 8-#define OFF_FONTS 1472488-#define OFF_BUILDINGS 1472528+#define OFF_FONTS 1168800+#define OFF_BUILDINGS 1168840#define N_BUILDINGS 20#define B_CURSOR 0#define B_GRANDMA 1@@ -38,7 +38,7 @@#define B_IDLEVERSE 17#define B_CORTEX_BAKER 18#define B_YOU 19-#define OFF_TIERS 1473648+#define OFF_TIERS 1169960#define N_TIERS 18#define TIER__1 0#define TIER__2 1@@ -58,7 +58,7 @@#define TIER_SYNERGY1 15#define TIER_SYNERGY2 16#define TIER_FORTUNE 17-#define OFF_UPGRADES 1474080+#define OFF_UPGRADES 1170392#define N_UPGRADES 876#define U_REINFORCED_INDEX_FINGER 0#define U_CARPAL_TUNNEL_PREVENTION_CREAM 1@@ -936,7 +936,7 @@#define U_UNDECILLION_FINGERS 873#define U_OMNIPLAST_MOUSE 874#define U_WRINKLER_AMBERGRIS 875-#define OFF_ACHIEVS 1530144+#define OFF_ACHIEVS 1226456#define N_ACHIEVS 643#define A_WAKE_AND_BAKE 0#define A_MAKING_SOME_DOUGH 1@@ -1582,47 +1582,47 @@#define A_ALL_ON_DECK 641#define A_A_ROUND_OF_APPLAUSE 642#define N_MILKS 25-#define OFF_MILKS 1543004-#define OFF_BUILDING_LINKS 1543208-#define OFF_TIER_UNSHACKLE 1545768+#define OFF_MILKS 1239316+#define OFF_BUILDING_LINKS 1239520+#define OFF_TIER_UNSHACKLE 1242080#define N_TIER_UNSHACKLE 18-#define OFF_COOKIE_UPGRADES 1545804+#define OFF_COOKIE_UPGRADES 1242116#define N_COOKIE_UPGRADES 248-#define OFF_GRANDMA_SYNERGIES 1546300+#define OFF_GRANDMA_SYNERGIES 1242612#define N_GRANDMA_SYNERGIES 18-#define OFF_KITTENS 1546336+#define OFF_KITTENS 1242648#define N_KITTENS 18-#define OFF_GOLDEN_UPGRADES 1546372+#define OFF_GOLDEN_UPGRADES 1242684#define N_GOLDEN_UPGRADES 10-#define OFF_LASTING 1546392+#define OFF_LASTING 1242704#define N_LASTING 8-#define OFF_NO_PERM 1546408+#define OFF_NO_PERM 1242720#define N_NO_PERM 6-#define OFF_HEAVENLY 1546420+#define OFF_HEAVENLY 1242732#define N_HEAVENLY 129-#define OFF_EASTER_EGGS 1546680+#define OFF_EASTER_EGGS 1242992#define N_EASTER_EGGS 20-#define OFF_HALLOWEEN_DROPS 1546720+#define OFF_HALLOWEEN_DROPS 1243032#define N_HALLOWEEN_DROPS 7-#define OFF_HEART_DROPS 1546736+#define OFF_HEART_DROPS 1243048#define N_HEART_DROPS 7-#define OFF_SANTA_DROPS 1546752+#define OFF_SANTA_DROPS 1243064#define N_SANTA_DROPS 14-#define OFF_REINDEER_DROPS 1546780+#define OFF_REINDEER_DROPS 1243092#define N_REINDEER_DROPS 7-#define OFF_FORTUNES 1546796+#define OFF_FORTUNES 1243108#define N_FORTUNES 25-#define OFF_SHOW_IF 1546848+#define OFF_SHOW_IF 1243160#define N_SHOW_IF 3-#define OFF_CONFIRM 1546856+#define OFF_CONFIRM 1243168#define N_CONFIRM 6-#define OFF_BANK_ACHIEVS 1546872+#define OFF_BANK_ACHIEVS 1243184#define N_BANK_ACHIEVS 48-#define OFF_CPS_ACHIEVS 1547640+#define OFF_CPS_ACHIEVS 1243952#define N_CPS_ACHIEVS 48-#define OFF_UNLOCK_AT 1548408+#define OFF_UNLOCK_AT 1244720#define N_UNLOCK_AT 213-#define OFF_NEWS 1553520+#define OFF_NEWS 1249832#define N_NEWS 128#define NK_GRANDMA 0#define NK_GRANDMA_THREAT 1@@ -1643,8 +1643,8 @@#define NK_FOOLS_RARE 16#define NK_FOOLS_BUILDING 17#define NK_FOOLS_LORE 18-#define OFF_TEXT 1554544-#define OFF_IMAGES 1782036+#define OFF_TEXT 1250856+#define OFF_IMAGES 1478348#define N_IMAGES 1556#define IM_COOKIE 0#define IM_COOKIE_SHADOW 1@@ -1932,6 +1932,6 @@#define IM_FAMILIAR1_1 283#define IM_FAMILIAR1_2 284#define IM_FAMILIAR1_3 285-#define DATA_SIZE 1806932+#define DATA_SIZE 1503244#endifdiff --git a/games/cookieclicker/src/gfx.c b/games/cookieclicker/src/gfx.cindex 06618abc..d5bfab06 100644--- a/games/cookieclicker/src/gfx.c+++ b/games/cookieclicker/src/gfx.c@@ -174,37 +174,39 @@ typedef struct {const uint8_t *px;const uint16_t *col;const uint8_t *opa;- int w, h, fmt;+ int w, h;+ int x, y, cw, ch, bits, stride; /* the part kept: outside it, clear */} Img;static Img img(int im) {const ImageRec *r = image_rec(im);Img m;- m.w = r->w, m.h = r->h, m.fmt = r->fmt;+ m.w = r->w, m.h = r->h, m.x = r->x, m.y = r->y, m.cw = r->cw, m.ch = r->ch, m.bits = r->bits;+ m.stride = (m.cw * m.bits + 7) >> 3;m.px = cc_data + r->px;- if (r->fmt == FMT_RGB) {- m.col = (const uint16_t *)(const void *)m.px, m.opa = NULL;- } else {- m.col = (const uint16_t *)(const void *)(cc_data + r->pal);- m.opa = cc_data + r->pal + 2 * (r->ncolors_1 + 1);- }+ m.col = (const uint16_t *)(const void *)(cc_data + r->pal);+ m.opa = cc_data + r->pal + 2 * (r->ncolors_1 + 1);return m;}int gfx_image_w(int im) { return image_rec(im)->w; }int gfx_image_h(int im) { return image_rec(im)->h; }+/* the palette index of pixel (x, y) of the picture, -1 if it is outside the part kept (clear) */+static inline int index_at(const Img *m, int x, int y) {+ x -= m->x, y -= m->y;+ if ((unsigned)x >= (unsigned)m->cw || (unsigned)y >= (unsigned)m->ch) return -1;+ if (m->bits == FMT_P8) return m->px[y * m->stride + x];+ if (m->bits == FMT_P0) return 0;+ uint8_t b = m->px[y * m->stride + (x >> 1)];+ return x & 1 ? b >> 4 : b & 15;+}+/* the colour and opacity (0..255) of pixel (x, y) */static inline uint16_t texel(const Img *m, int x, int y, int *a) {- if (m->fmt == FMT_RGB) {- *a = 255;- return m->col[y * m->w + x];- }- int i;- if (m->fmt == FMT_P8)- i = m->px[y * m->w + x];- else {- uint8_t b = m->px[y * ((m->w + 1) >> 1) + (x >> 1)];- i = x & 1 ? b >> 4 : b & 15;+ int i = index_at(m, x, y);+ if (i < 0) {+ *a = 0;+ return 0;}*a = m->opa[i];return m->col[i];@@ -212,29 +214,36 @@ static inline uint16_t texel(const Img *m, int x, int y, int *a) {/* a row of n pixels of the picture from (sx, sy), on p: each format its own loop (most of what is drawn) */static void row(const Img *m, int sx, int sy, int n, uint16_t *p, int alpha) {+ sx -= m->x, sy -= m->y;+ if ((unsigned)sy >= (unsigned)m->ch) return;+ if (sx < 0) p -= sx, n += sx, sx = 0;+ if (sx + n > m->cw) n = m->cw - sx;+ if (n <= 0) return;bool plain = alpha >= 256 && blend_mode == BLEND_NORMAL;- if (m->fmt == FMT_RGB) {- const uint16_t *src = m->col + sy * m->w + sx;- if (plain)- for (int i = 0; i < n; i++) p[i] = src[i];- else- for (int i = 0; i < n; i++) put(p + i, src[i], alpha);- } else if (m->fmt == FMT_P8) {- const uint8_t *src = m->px + sy * m->w + sx;+ const uint8_t *src = m->px + sy * m->stride;+ if (m->bits == FMT_P8) {+ src += sx;for (int i = 0; i < n; i++) {int k = src[i], a = m->opa[k];if (!a) continue;if (a == 255 && plain) p[i] = m->col[k];else put(p + i, m->col[k], (a + (a >> 7)) * alpha >> 8);}- } else {- const uint8_t *src = m->px + sy * ((m->w + 1) >> 1);+ } else if (m->bits == FMT_P4) {for (int i = 0; i < n; i++) {int x = sx + i, k = x & 1 ? src[x >> 1] >> 4 : src[x >> 1] & 15, a = m->opa[k];if (!a) continue;if (a == 255 && plain) p[i] = m->col[k];else put(p + i, m->col[k], (a + (a >> 7)) * alpha >> 8);}+ } else { /* (one colour) */+ int a = m->opa[0];+ uint16_t c = m->col[0];+ if (!a) return;+ if (a == 255 && plain)+ for (int i = 0; i < n; i++) p[i] = c;+ else+ for (int i = 0; i < n; i++) put(p + i, c, (a + (a >> 7)) * alpha >> 8);}}@@ -261,13 +270,6 @@ void gfx_image_dark(int im, int x, int y, int alpha) {}}-/* the index of pixel (x, y) of a paletted picture (inside it) */-static inline int index_at(const Img *m, int x, int y) {- if (m->fmt == FMT_P8) return m->px[y * m->w + x];- uint8_t b = m->px[y * ((m->w + 1) >> 1) + (x >> 1)];- return x & 1 ? b >> 4 : b & 15;-}-/* the colour at (u, v), in 1/256ths of a pixel of the picture, between its four nearest pixels;* opacity 0..255. wrap: tiled, else clear outside. (Flat areas and opaque ones take shortcuts: this* runs for most pixels drawn.) */@@ -275,34 +277,22 @@ static uint16_t bilinear(const Img *m, int u, int v, bool wrap, int *a_out) {u -= 128, v -= 128;int x0 = u >> 8, y0 = v >> 8, fx = (u & 255) >> 4, fy = (v & 255) >> 4;int x1 = x0 + 1, y1 = y0 + 1;- bool in0x = true, in1x = true, in0y = true, in1y = true;if (wrap) {if ((unsigned)x0 >= (unsigned)m->w) x0 %= m->w, x0 += x0 < 0 ? m->w : 0;if ((unsigned)y0 >= (unsigned)m->h) y0 %= m->h, y0 += y0 < 0 ? m->h : 0;x1 = x0 + 1 == m->w ? 0 : x0 + 1, y1 = y0 + 1 == m->h ? 0 : y0 + 1;- } else {- in0x = (unsigned)x0 < (unsigned)m->w, in1x = (unsigned)x1 < (unsigned)m->w;- in0y = (unsigned)y0 < (unsigned)m->h, in1y = (unsigned)y1 < (unsigned)m->h;}uint16_t c[4];int a[4];- if (m->fmt == FMT_RGB) {- c[0] = in0x && in0y ? m->col[y0 * m->w + x0] : 0, a[0] = in0x && in0y ? 255 : 0;- c[1] = in1x && in0y ? m->col[y0 * m->w + x1] : 0, a[1] = in1x && in0y ? 255 : 0;- c[2] = in0x && in1y ? m->col[y1 * m->w + x0] : 0, a[2] = in0x && in1y ? 255 : 0;- c[3] = in1x && in1y ? m->col[y1 * m->w + x1] : 0, a[3] = in1x && in1y ? 255 : 0;- } else {- int i0 = in0x && in0y ? index_at(m, x0, y0) : -1, i1 = in1x && in0y ? index_at(m, x1, y0) : -1;- int i2 = in0x && in1y ? index_at(m, x0, y1) : -1, i3 = in1x && in1y ? index_at(m, x1, y1) : -1;- if (i0 == i1 && i0 == i2 && i0 == i3) { /* (a flat area) */- *a_out = i0 < 0 ? 0 : m->opa[i0];- return i0 < 0 ? 0 : m->col[i0];- }- c[0] = i0 < 0 ? 0 : m->col[i0], a[0] = i0 < 0 ? 0 : m->opa[i0];- c[1] = i1 < 0 ? 0 : m->col[i1], a[1] = i1 < 0 ? 0 : m->opa[i1];- c[2] = i2 < 0 ? 0 : m->col[i2], a[2] = i2 < 0 ? 0 : m->opa[i2];- c[3] = i3 < 0 ? 0 : m->col[i3], a[3] = i3 < 0 ? 0 : m->opa[i3];+ int i0 = index_at(m, x0, y0), i1 = index_at(m, x1, y0), i2 = index_at(m, x0, y1), i3 = index_at(m, x1, y1);+ if (i0 == i1 && i0 == i2 && i0 == i3) { /* (a flat area) */+ *a_out = i0 < 0 ? 0 : m->opa[i0];+ return i0 < 0 ? 0 : m->col[i0];}+ c[0] = i0 < 0 ? 0 : m->col[i0], a[0] = i0 < 0 ? 0 : m->opa[i0];+ c[1] = i1 < 0 ? 0 : m->col[i1], a[1] = i1 < 0 ? 0 : m->opa[i1];+ c[2] = i2 < 0 ? 0 : m->col[i2], a[2] = i2 < 0 ? 0 : m->opa[i2];+ c[3] = i3 < 0 ? 0 : m->col[i3], a[3] = i3 < 0 ? 0 : m->opa[i3];int w[4] = {(16 - fx) * (16 - fy), fx * (16 - fy), (16 - fx) * fy, fx * fy}; /* (sum 256) */if ((a[0] & a[1] & a[2] & a[3]) == 255) { /* (opaque: no weighting by opacity) */uint32_t R = 0, G = 0, B = 0;diff --git a/games/cookieclicker/src/res.h b/games/cookieclicker/src/res.hindex d7188079..06ee632a 100644--- a/games/cookieclicker/src/res.h+++ b/games/cookieclicker/src/res.h@@ -1,7 +1,8 @@/* Cookie Clicker: data.bin (tools/pack.py), read where it lies in flash.*- * Pictures (IM_*) are a byte or half a byte a pixel with a palette of colours and- * opacities, or RGB565; the icons are the game's icon sheet at 24 pixels; fonts (F_*)+ * Pictures (IM_*) are the part of them that isn't clear, a byte, half a byte or nothing a+ * pixel (tools/pictures.py), with a palette of colours and opacities; the icons are the+ * game's icon sheet at 24 pixels; fonts (F_*)* are 4-bit glyphs; texts are the game's own, in a small markup (see text.c); and* the game's tables: buildings, tiers, upgrades, achievements, milks. */#ifndef CC_RES_H@@ -13,12 +14,13 @@extern const uint8_t cc_data[];typedef struct {- uint16_t w, h;- uint8_t fmt, ncolors_1; /* fmt: 0 RGB565, 1 a byte a pixel, 2 half a byte a pixel; colours - 1 */- uint16_t pad;- uint32_t px, pal; /* offsets: the pixels, the palette (colours RGB565, then their opacities) */+ uint8_t w, h;+ uint8_t x, y, cw, ch; /* the part kept (outside it, clear) */+ uint8_t bits, ncolors_1; /* bits a pixel (FMT_*); colours - 1 */+ uint32_t px, pal; /* offsets: the part's pixels (each row a whole number of bytes, the first pixel+ lowest), the palette (colours RGB565, then their opacities) */} ImageRec;-enum { FMT_RGB, FMT_P8, FMT_P4 };+enum { FMT_P0 = 0, FMT_P4 = 4, FMT_P8 = 8 }; /* (P0: all of one colour) */typedef struct {double price, cps;diff --git a/games/cookieclicker/tools/pack.py b/games/cookieclicker/tools/pack.pyindex 380433af..b36edf1f 100644--- a/games/cookieclicker/tools/pack.py+++ b/games/cookieclicker/tools/pack.py@@ -7,9 +7,7 @@The game's left column (about 384 pixels wide in a browser) is drawn at 0.37 of its size, its store at about half.data.bin, read where it lies in flash (offsets from its start; data.h names them):-- IMAGES: 16 bytes each (IM_*): w, h (u16), format (u8: 0 RGB565, 1 a byte a pixel, 2 half a byte a pixel, the- first pixel lowest), colours - 1 (u8), 0 (u16), the pixels' offset, the palette's (u32): its colours (RGB565),- then their opacities (u8 each)+- IMAGES: 16 bytes each (IM_*), and the pictures' palettes and pixels: see tools/pictures.py- ICONS: the game's icon sheet (icons.png, 36 x 37 icons of 48 pixels) at 24 pixels: an image index (u16, 0xFFFFnone) for each icon, row by row- FONTS: the offset (u32) of each (F_*): first character, count, ascent, line height (u8 x 4), then 8 bytes a glyph@@ -31,6 +29,9 @@ import sysimport numpy as npfrom PIL import Image, ImageDraw, ImageFont+sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))+import pictures # noqa: E402+HERE = os.path.dirname(os.path.abspath(__file__))OUT_BIN = os.path.join(HERE, "..", "src", "data.bin")OUT_H = os.path.join(HERE, "..", "src", "data.h")@@ -86,11 +87,12 @@ def quantize(img, n):class Pack:def __init__(self):- self.blob = bytearray(b"CCK1")- self.images = [] # (name, w, h, fmt, ncolors, px_off, pal_off)+ self.blob = bytearray(pictures.MAGIC)+ self.images = [] # the pictures, as drawn (pictures.plane)+ self.groups = [] # (the pictures whose pixels are shared: pictures.lay_out)self.names = {}+ self.recs = Noneself.defines = []- self.sizes = {} # bytes by group of pictures, for the reportdef align(self, n=4):while len(self.blob) % n:@@ -102,38 +104,29 @@ class Pack:self.blob += datareturn off- def image(self, name, img, fmt="p8", ncolors=256):- """adds a picture; returns its index"""+ def image(self, name, img, fmt="p8", ncolors=256, group=None):+ """adds a picture (RGB565, or quantized to a palette of ncolors, 16 for p4); returns its index"""w, h = img.size- start = len(self.blob)if fmt == "rgb":arr = np.asarray(img.convert("RGB")).astype(np.uint16)- px = ((arr[..., 0] >> 3) << 11 | (arr[..., 1] >> 2) << 5 | arr[..., 2] >> 3).astype("<u2")- off = self.put(px.tobytes())- rec = (name, w, h, 0, 1, off, 0)+ px = (arr[..., 0] >> 3) << 11 | (arr[..., 1] >> 2) << 5 | arr[..., 2] >> 3+ self.images.append(pictures.plane(px, np.full((h, w), 255)))else:- n = 16 if fmt == "p4" else ncolors- pal, idx = quantize(img, n)- pal_bytes = b"".join(struct.pack("<H", rgb565(*c[:3])) for c in pal) + bytes(c[3] for c in pal)- pal_off = self.put(pal_bytes)- if fmt == "p4":- if w % 2:- idx = np.concatenate([idx, np.zeros((h, 1), np.uint8)], 1)- packed = (idx[:, 0::2] | idx[:, 1::2] << 4).astype(np.uint8)- off = self.put(packed.tobytes())- rec = (name, w, h, 2, len(pal), off, pal_off)- else:- off = self.put(idx.astype(np.uint8).tobytes())- rec = (name, w, h, 1, len(pal), off, pal_off)+ pal, idx = quantize(img, 16 if fmt == "p4" else ncolors)+ cols = np.array([rgb565(*c[:3]) for c in pal])+ opas = np.array([c[3] for c in pal])+ self.images.append(pictures.plane(cols[idx], opas[idx]))if name:if name in self.names:raise SystemExit(f"pack: two pictures named {name}")- self.names[name] = len(self.images)- group = re.sub(r"(_.*|\d+)$", "", name) if name else "ICONS"- self.sizes[group] = self.sizes.get(group, 0) + len(self.blob) - start- self.images.append(rec)+ self.names[name] = len(self.images) - 1+ self.groups.append(group or pictures.group(name))return len(self.images) - 1+ def lay_out(self):+ """the pictures into data.bin"""+ self.recs = pictures.lay_out(self.images, self.groups, self.put)+def load(ref, rel):return Image.open(os.path.join(ref, "img", rel)).convert("RGBA")@@ -765,7 +758,8 @@ def main(ref, game_json):if np.asarray(cell)[..., 3].max() < 8:icon_index.append(0xFFFF)continue- icon_index.append(P.image(None, premul_resize(cell, (24, 24)), "p4"))+ icon_index.append(P.image(None, premul_resize(cell, (24, 24)), "p4", group=f"icons{y}"))+ P.lay_out()icons_off = P.put(struct.pack(f"<{len(icon_index)}H", *icon_index))defs += [f"#define ICON_COLS {cols}", f"#define ICON_ROWS {rows}", f"#define OFF_ICONS {icons_off}"]@@ -943,11 +937,7 @@ def main(ref, game_json):# ---- the texts, the pictures' tabletext_off = P.put(bytes(T.blob))defs.append(f"#define OFF_TEXT {text_off}")- P.align(4)- table = bytearray()- for name, w, h, fmt, n, px, pal in P.images:- table += struct.pack("<HHBBHII", w, h, fmt, n - 1, 0, px, pal)- img_off = P.put(bytes(table))+ img_off = P.put(pictures.records(P.recs))defs.append(f"#define OFF_IMAGES {img_off}")defs.append(f"#define N_IMAGES {len(P.images)}")for name, i in P.names.items():@@ -977,9 +967,13 @@ def main(ref, game_json):f.write("\n".join(lines))if UNKNOWN:print("pack: characters left out:", "".join(sorted(UNKNOWN)))+ pictures.check(P.images, bytes(P.blob), dict(N_IMAGES=len(P.images), OFF_IMAGES=img_off))sizes = {}+ for g, (w, h, x, y, cw, ch, bits, n, px, pal) in zip(P.groups, P.recs):+ g = re.sub(r"\d+$", "", g)+ sizes[g] = sizes.get(g, 0) + (cw * bits + 7) // 8 * ch + 3 * nprint(f"data.bin: {len(P.blob)} bytes, {len(P.images)} pictures, texts {len(T.blob)}")- print(" " + ", ".join(f"{k} {v // 1024} KB" for k, v in sorted(P.sizes.items(), key=lambda kv: -kv[1])))+ print(" pictures (before sharing): " + ", ".join(f"{k} {v // 1024} KB" for k, v in sorted(sizes.items(), key=lambda kv: -kv[1])))if __name__ == "__main__":diff --git a/games/cookieclicker/tools/pictures.py b/games/cookieclicker/tools/pictures.pynew file mode 100644index 00000000..d39eb35d--- /dev/null+++ b/games/cookieclicker/tools/pictures.py@@ -0,0 +1,202 @@+#!/usr/bin/env python3+"""How data.bin keeps its pictures (tools/pack.py lays them out with this), and the pictures of src/data.bin laid+out again in place, every pixel drawn the same:++ python3 -I tools/pictures.py++A picture is its colours (RGB565) and opacities, pixel by pixel; a clear pixel's colour does not matter. Kept:+- only the part of it that isn't clear (outside it, clear);+- a palette of its colours and opacities, each once (a palette seen before is not stored again);+- its pixels as indices to it: none (one colour), half a byte a pixel (16 colours or fewer, the first pixel+ lowest) or a byte, each row a whole number of bytes. The pixels of each group of pictures (by name, ROW_*,+ MILK_*..., the icons by rows of the sheet) are laid out with tools/share.py: a string seen before, or inside+ another, is not stored again; tails overlap heads.++Its record (IMAGES, 16 bytes; res.h's ImageRec): w, h, then the part kept: x, y, w, h (u8 each), the bits a pixel+(0, 4, 8), colours - 1 (u8 each), the pixels' offset, the palette's (u32): its colours (RGB565), then their+opacities (u8 each)."""+import os+import re+import struct+import sys++import numpy as np++HERE = os.path.dirname(os.path.abspath(__file__))+sys.path.insert(0, os.path.join(HERE, "..", "..", "..", "tools"))+from share import share # noqa: E402++MAGIC = b"CCK2"+++def plane(colours, opacities):+ """a picture as one array: RGB565 << 8 | opacity, the clear pixels 0"""+ c = np.asarray(colours, np.uint32)+ a = np.asarray(opacities, np.uint32)+ return np.where(a > 0, c << 8 | a, 0).astype(np.uint32)+++def lay_out(pictures, groups, put):+ """pictures: (h x w) planes (plane()); groups: a name for each, the pictures that share their pixels+ (tools/share.py, slow on many); put(bytes, align) adds bytes to data.bin, giving their offset.+ Returns the records (IMAGES), in order."""+ kept, pals, recs = [], {}, []+ for p in pictures:+ h, w = p.shape+ if not (0 < w < 256 and 0 < h < 256):+ raise SystemExit(f"pictures: {w} x {h}: past 255 pixels")+ ys, xs = np.nonzero(p)+ if not len(xs): # (all clear: nothing to read)+ kept.append(None)+ continue+ x0, x1, y0, y1 = int(xs.min()), int(xs.max()) + 1, int(ys.min()), int(ys.max()) + 1+ part = p[y0:y1, x0:x1]+ flat = part.ravel()+ _, first, inv = np.unique(flat, return_index=True, return_inverse=True)+ order = np.argsort(first) # (the colours in the order they come)+ rank = np.empty(len(order), np.int64)+ rank[order] = np.arange(len(order))+ idx = rank[inv].reshape(part.shape).astype(np.uint8)+ colours = flat[first[order]]+ n = len(colours)+ if n > 256:+ raise SystemExit(f"pictures: a picture of {n} colours")+ bits = 0 if n == 1 else 4 if n <= 16 else 8+ if bits == 4:+ if idx.shape[1] % 2:+ idx = np.concatenate([idx, np.zeros((idx.shape[0], 1), np.uint8)], 1)+ px = (idx[:, 0::2] | idx[:, 1::2] << 4).astype(np.uint8).tobytes()+ else:+ px = idx.tobytes() if bits else b""+ pal = b"".join(struct.pack("<H", int(c) >> 8) for c in colours) + bytes(int(c) & 255 for c in colours)+ if pal not in pals:+ pals[pal] = put(pal, 2)+ kept.append((w, h, x0, y0, x1 - x0, y1 - y0, bits, n, px, pals[pal]))+ at = {} # picture -> its pixels' offset+ for g in dict.fromkeys(groups):+ mine = [i for i, k in enumerate(kept) if k and k[8] and groups[i] == g]+ if mine:+ blob, offs = share([kept[i][8] for i in mine])+ base = put(blob, 1)+ at.update((i, base + o) for i, o in zip(mine, offs))+ for i, (p, k) in enumerate(zip(pictures, kept)):+ if k is None:+ recs.append((p.shape[1], p.shape[0], 0, 0, 0, 0, 0, 1, 0, 0))+ else:+ recs.append(k[:8] + (at.get(i, 0), k[9]))+ return recs+++def group(name):+ """the group a picture's pixels are shared in: by its name (ROW_FARM: ROW), or icons by their row"""+ return re.sub(r"(_.*|\d+)$", "", name)+++def groups_of(data, h):+ g = ["?"] * h["N_IMAGES"]+ cols = h["ICON_COLS"]+ for k, i in enumerate(struct.unpack_from(f"<{cols * h['ICON_ROWS']}H", data, h["OFF_ICONS"])):+ if i != 0xFFFF:+ g[i] = f"icons{k // cols}"+ for k, v in h.items():+ if k.startswith("IM_"):+ g[v] = group(k[3:])+ return g+++def records(recs):+ return b"".join(struct.pack("<BBBBBBBBII", w, h, x, y, cw, ch, bits, n - 1, px, pal)+ for w, h, x, y, cw, ch, bits, n, px, pal in recs)+++def pictures_of(data, h):+ """the pictures of a data.bin (and its data.h's numbers), as planes"""+ out = []+ old = data[:4] == b"CCK1" # (before: no part, pictures of a byte, half a byte or RGB565 a pixel)+ for i in range(h["N_IMAGES"]):+ at = h["OFF_IMAGES"] + 16 * i+ if old:+ w, hh, fmt, n1, _, px, pal = struct.unpack_from("<HHBBHII", data, at)+ if fmt == 0:+ c = np.frombuffer(data, "<u2", w * hh, px).reshape(hh, w)+ out.append(plane(c, np.full((hh, w), 255)))+ continue+ x, y, cw, ch, bits = 0, 0, w, hh, 8 if fmt == 1 else 4+ else:+ w, hh, x, y, cw, ch, bits, n1, px, pal = struct.unpack_from("<BBBBBBBBII", data, at)+ p = np.zeros((hh, w), np.uint32)+ if cw:+ n = n1 + 1+ cols = np.frombuffer(data, "<u2", n, pal).astype(np.uint32)+ opa = np.frombuffer(data, np.uint8, n, pal + 2 * n).astype(np.uint32)+ if bits == 8:+ idx = np.frombuffer(data, np.uint8, cw * ch, px).reshape(ch, cw)+ elif bits == 4:+ b = np.frombuffer(data, np.uint8, (cw + 1) // 2 * ch, px).reshape(ch, (cw + 1) // 2)+ idx = np.stack([b & 15, b >> 4], 2).reshape(ch, -1)[:, :cw]+ else:+ idx = np.zeros((ch, cw), np.uint8)+ p[y:y + ch, x:x + cw] = plane(cols[idx], opa[idx])+ out.append(p)+ return out+++def check(pictures, data, h):+ for i, (a, b) in enumerate(zip(pictures, pictures_of(data, h))):+ if a.shape != b.shape or (a != b).any():+ raise SystemExit(f"pictures: picture {i} not read back as it was")+++def numbers(path):+ h = {}+ for line in open(path):+ m = re.match(r"#define (\w+) (-?\d+)$", line.strip())+ if m:+ h[m.group(1)] = int(m.group(2))+ return h+++def main():+ """src/data.bin's pictures laid out again: the pictures first, then the rest (from the icons' table to the+ texts) moved as it is, then the pictures' table; data.h's offsets moved with them"""+ bin_path, h_path = os.path.join(HERE, "..", "src", "data.bin"), os.path.join(HERE, "..", "src", "data.h")+ data, h = open(bin_path, "rb").read(), numbers(h_path)+ pics = pictures_of(data, h)+ out = bytearray(MAGIC)++ def put(b, align):+ while len(out) % align:+ out.append(0)+ out.extend(b)+ return len(out) - len(b)+ recs = lay_out(pics, groups_of(data, h), put)+ start, end = h["OFF_ICONS"], h["OFF_IMAGES"] # (the rest: what isn't pictures)+ while (len(out) - start) % 8: # (its tables of doubles stay 8-aligned)+ out.append(0)+ moved = len(out) - start+ rest = bytearray(data[start:end])+ fonts = h["OFF_FONTS"] - start # (the fonts' offsets, from data.bin's start)+ for f in range(len([k for k in h if k.startswith("F_")])):+ o = struct.unpack_from("<I", rest, fonts + 4 * f)[0]+ struct.pack_into("<I", rest, fonts + 4 * f, o + moved)+ out += rest+ while len(out) % 4:+ out.append(0)+ images = len(out)+ out += records(recs)+ check(pics, bytes(out), dict(h, OFF_IMAGES=images))+ lines = []+ for line in open(h_path).read().split("\n"):+ m = re.match(r"#define (OFF_\w+|DATA_SIZE) (\d+)$", line)+ if m:+ v = int(m.group(2))+ v = images if m.group(1) == "OFF_IMAGES" else len(out) if m.group(1) == "DATA_SIZE" else v + moved+ line = f"#define {m.group(1)} {v}"+ lines.append(line)+ open(bin_path, "wb").write(out)+ open(h_path, "w").write("\n".join(lines))+ print(f"data.bin: {len(data)} -> {len(out)} bytes, pictures {start} -> {start + moved}")+++if __name__ == "__main__":+ main()