/* mkappicons: turn the pre-rendered GNOME app icon PNGs into an NBAPP1 * bundle of premultiplied RGB565+alpha images. * * ./mkappicons [-v] * * Every APP_ICON_LIST entry (include/app_icon_abi.h) names * /.png. The PNGs are committed next to the upstream SVGs * so the normal build needs neither rsvg-convert nor an SVG rasterizer. * tools/render_appicons.sh regenerates them from the SVGs. * * The in-tree image library (user/lib/img) does the PNG decode; the output * is written straight into the bundle consumed by uwm_lib. */ #include #include #include #include "app_icon_abi.h" #include "lib/img/img.h" #define APP_ICON_MAX_FILE (4u * 1024u * 1024u) struct icon_def { const char *name; const char *file; }; #define ICON_DEF_(e, file) { #e, file }, static const struct icon_def icons[] = { APP_ICON_LIST(ICON_DEF_) }; #undef ICON_DEF_ #define NICONS ((int)(sizeof(icons) / sizeof(icons[0]))) static const char *cur_file; static void die(const char *msg, const char *detail) { fprintf(stderr, "mkappicons: %s: %s%s%s\n", cur_file ? cur_file : "-", msg, detail ? ": " : "", detail ? detail : ""); exit(1); } static unsigned char *read_file(const char *path, unsigned int *out_len) { FILE *f = fopen(path, "rb"); long n; unsigned char *buf; if (!f) return NULL; if (fseek(f, 0, SEEK_END) != 0) { fclose(f); return NULL; } n = ftell(f); if (n <= 0 || n > (long)APP_ICON_MAX_FILE) { fclose(f); return NULL; } if (fseek(f, 0, SEEK_SET) != 0) { fclose(f); return NULL; } buf = malloc((size_t)n); if (!buf || fread(buf, 1, (size_t)n, f) != (size_t)n) { fclose(f); free(buf); return NULL; } fclose(f); *out_len = (unsigned int)n; return buf; } static void write_u16(FILE *f, unsigned int v) { fputc((int)(v & 0xFF), f); fputc((int)((v >> 8) & 0xFF), f); } int main(int argc, char **argv) { const char *dir, *out_path; unsigned int size, i; int verbose = 0; FILE *out; unsigned short count; if (argc < 4) { fprintf(stderr, "usage: %s [-v]\n", argv[0]); return 2; } dir = argv[1]; size = (unsigned int)strtoul(argv[2], NULL, 0); out_path = argv[3]; if (argc > 4 && !strcmp(argv[4], "-v")) verbose = 1; if (size != APP_ICON_PX) { fprintf(stderr, "mkappicons: size must be %d (got %u)\n", APP_ICON_PX, size); return 2; } if (NICONS > 65535) { fprintf(stderr, "mkappicons: too many icons\n"); return 2; } out = fopen(out_path, "wb"); if (!out) { fprintf(stderr, "mkappicons: cannot create %s\n", out_path); return 1; } count = (unsigned short)NICONS; fwrite(APP_ICON_FILE_MAGIC, 1, 8, out); write_u16(out, count); write_u16(out, size); write_u16(out, 0); write_u16(out, 0); for (i = 0; i < (unsigned int)NICONS; i++) { char path[1024]; unsigned char *data; unsigned int len = 0, j, opaque = 0; struct img_info info; struct img_options opt; struct img_mem mem; struct img_image img; unsigned int arena_bytes; unsigned char *arena; int r; snprintf(path, sizeof(path), "%s/%s.png", dir, icons[i].file); cur_file = path; data = read_file(path, &len); if (!data) die("cannot read PNG", NULL); memset(&info, 0, sizeof(info)); r = img_sniff(data, len, &info); if (r != 0) die("not an image", img_error_string(r)); if (info.format != IMG_FMT_PNG) die("not a PNG", NULL); if (info.width != size || info.height != size) { char buf[64]; snprintf(buf, sizeof(buf), "%ux%u, expected %ux%u", info.width, info.height, size, size); die("wrong dimensions", buf); } arena_bytes = img_estimate(&info); if (arena_bytes < 65536) arena_bytes = 65536; arena = malloc(arena_bytes); if (!arena) die("out of memory", NULL); img_mem_init(&mem, arena, arena_bytes); memset(&opt, 0, sizeof(opt)); opt.max_pixels = size * size; memset(&img, 0, sizeof(img)); r = img_decode(data, len, &opt, &mem, &img); if (r != 0) die("decode failed", img_error_string(r)); if (img.w != size || img.h != size) die("decoder size mismatch", NULL); for (j = 0; j < size * size; j++) { unsigned int v = img.px[j]; unsigned int a = img.alpha ? img.alpha[j] : 255u; unsigned int r5, g6, b5, rr, gg, bb; if (!a) { write_u16(out, 0); fputc(0, out); continue; } opaque++; r5 = (v >> 11) & 0x1F; g6 = (v >> 5) & 0x3F; b5 = v & 0x1F; rr = (r5 << 3) | (r5 >> 2); gg = (g6 << 2) | (g6 >> 4); bb = (b5 << 3) | (b5 >> 2); if (a != 255) { rr = (rr * a + 127) / 255; gg = (gg * a + 127) / 255; bb = (bb * a + 127) / 255; } v = ((rr >> 3) << 11) | ((gg >> 2) << 5) | (bb >> 3); write_u16(out, v); fputc((int)a, out); } if (!opaque) die("image is fully transparent", NULL); if (verbose) printf("%-9s %-38s %7u opaque px\n", icons[i].name, icons[i].file, opaque); free(arena); free(data); cur_file = NULL; } if (fclose(out) != 0) { fprintf(stderr, "mkappicons: write error\n"); return 1; } if (verbose) printf("wrote %s: %u icons at %ux%u\n", out_path, (unsigned int)count, size, size); return 0; }