mkappicons.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* mkappicons: turn the pre-rendered GNOME app icon PNGs into an NBAPP1
  2. * bundle of premultiplied RGB565+alpha images.
  3. *
  4. * ./mkappicons <asset-dir> <size> <out.icn> [-v]
  5. *
  6. * Every APP_ICON_LIST entry (include/app_icon_abi.h) names
  7. * <asset-dir>/<file>.png. The PNGs are committed next to the upstream SVGs
  8. * so the normal build needs neither rsvg-convert nor an SVG rasterizer.
  9. * tools/render_appicons.sh regenerates them from the SVGs.
  10. *
  11. * The in-tree image library (user/lib/img) does the PNG decode; the output
  12. * is written straight into the bundle consumed by uwm_lib. */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include "app_icon_abi.h"
  17. #include "lib/img/img.h"
  18. #define APP_ICON_MAX_FILE (4u * 1024u * 1024u)
  19. struct icon_def {
  20. const char *name;
  21. const char *file;
  22. };
  23. #define ICON_DEF_(e, file) { #e, file },
  24. static const struct icon_def icons[] = { APP_ICON_LIST(ICON_DEF_) };
  25. #undef ICON_DEF_
  26. #define NICONS ((int)(sizeof(icons) / sizeof(icons[0])))
  27. static const char *cur_file;
  28. static void die(const char *msg, const char *detail)
  29. {
  30. fprintf(stderr, "mkappicons: %s: %s%s%s\n",
  31. cur_file ? cur_file : "-", msg,
  32. detail ? ": " : "", detail ? detail : "");
  33. exit(1);
  34. }
  35. static unsigned char *read_file(const char *path, unsigned int *out_len)
  36. {
  37. FILE *f = fopen(path, "rb");
  38. long n;
  39. unsigned char *buf;
  40. if (!f)
  41. return NULL;
  42. if (fseek(f, 0, SEEK_END) != 0) {
  43. fclose(f);
  44. return NULL;
  45. }
  46. n = ftell(f);
  47. if (n <= 0 || n > (long)APP_ICON_MAX_FILE) {
  48. fclose(f);
  49. return NULL;
  50. }
  51. if (fseek(f, 0, SEEK_SET) != 0) {
  52. fclose(f);
  53. return NULL;
  54. }
  55. buf = malloc((size_t)n);
  56. if (!buf || fread(buf, 1, (size_t)n, f) != (size_t)n) {
  57. fclose(f);
  58. free(buf);
  59. return NULL;
  60. }
  61. fclose(f);
  62. *out_len = (unsigned int)n;
  63. return buf;
  64. }
  65. static void write_u16(FILE *f, unsigned int v)
  66. {
  67. fputc((int)(v & 0xFF), f);
  68. fputc((int)((v >> 8) & 0xFF), f);
  69. }
  70. int main(int argc, char **argv)
  71. {
  72. const char *dir, *out_path;
  73. unsigned int size, i;
  74. int verbose = 0;
  75. FILE *out;
  76. unsigned short count;
  77. if (argc < 4) {
  78. fprintf(stderr,
  79. "usage: %s <asset-dir> <size> <out.icn> [-v]\n",
  80. argv[0]);
  81. return 2;
  82. }
  83. dir = argv[1];
  84. size = (unsigned int)strtoul(argv[2], NULL, 0);
  85. out_path = argv[3];
  86. if (argc > 4 && !strcmp(argv[4], "-v"))
  87. verbose = 1;
  88. if (size != APP_ICON_PX) {
  89. fprintf(stderr, "mkappicons: size must be %d (got %u)\n",
  90. APP_ICON_PX, size);
  91. return 2;
  92. }
  93. if (NICONS > 65535) {
  94. fprintf(stderr, "mkappicons: too many icons\n");
  95. return 2;
  96. }
  97. out = fopen(out_path, "wb");
  98. if (!out) {
  99. fprintf(stderr, "mkappicons: cannot create %s\n", out_path);
  100. return 1;
  101. }
  102. count = (unsigned short)NICONS;
  103. fwrite(APP_ICON_FILE_MAGIC, 1, 8, out);
  104. write_u16(out, count);
  105. write_u16(out, size);
  106. write_u16(out, 0);
  107. write_u16(out, 0);
  108. for (i = 0; i < (unsigned int)NICONS; i++) {
  109. char path[1024];
  110. unsigned char *data;
  111. unsigned int len = 0, j, opaque = 0;
  112. struct img_info info;
  113. struct img_options opt;
  114. struct img_mem mem;
  115. struct img_image img;
  116. unsigned int arena_bytes;
  117. unsigned char *arena;
  118. int r;
  119. snprintf(path, sizeof(path), "%s/%s.png", dir, icons[i].file);
  120. cur_file = path;
  121. data = read_file(path, &len);
  122. if (!data)
  123. die("cannot read PNG", NULL);
  124. memset(&info, 0, sizeof(info));
  125. r = img_sniff(data, len, &info);
  126. if (r != 0)
  127. die("not an image", img_error_string(r));
  128. if (info.format != IMG_FMT_PNG)
  129. die("not a PNG", NULL);
  130. if (info.width != size || info.height != size) {
  131. char buf[64];
  132. snprintf(buf, sizeof(buf), "%ux%u, expected %ux%u",
  133. info.width, info.height, size, size);
  134. die("wrong dimensions", buf);
  135. }
  136. arena_bytes = img_estimate(&info);
  137. if (arena_bytes < 65536)
  138. arena_bytes = 65536;
  139. arena = malloc(arena_bytes);
  140. if (!arena)
  141. die("out of memory", NULL);
  142. img_mem_init(&mem, arena, arena_bytes);
  143. memset(&opt, 0, sizeof(opt));
  144. opt.max_pixels = size * size;
  145. memset(&img, 0, sizeof(img));
  146. r = img_decode(data, len, &opt, &mem, &img);
  147. if (r != 0)
  148. die("decode failed", img_error_string(r));
  149. if (img.w != size || img.h != size)
  150. die("decoder size mismatch", NULL);
  151. for (j = 0; j < size * size; j++) {
  152. unsigned int v = img.px[j];
  153. unsigned int a = img.alpha ? img.alpha[j] : 255u;
  154. unsigned int r5, g6, b5, rr, gg, bb;
  155. if (!a) {
  156. write_u16(out, 0);
  157. fputc(0, out);
  158. continue;
  159. }
  160. opaque++;
  161. r5 = (v >> 11) & 0x1F;
  162. g6 = (v >> 5) & 0x3F;
  163. b5 = v & 0x1F;
  164. rr = (r5 << 3) | (r5 >> 2);
  165. gg = (g6 << 2) | (g6 >> 4);
  166. bb = (b5 << 3) | (b5 >> 2);
  167. if (a != 255) {
  168. rr = (rr * a + 127) / 255;
  169. gg = (gg * a + 127) / 255;
  170. bb = (bb * a + 127) / 255;
  171. }
  172. v = ((rr >> 3) << 11) | ((gg >> 2) << 5) | (bb >> 3);
  173. write_u16(out, v);
  174. fputc((int)a, out);
  175. }
  176. if (!opaque)
  177. die("image is fully transparent", NULL);
  178. if (verbose)
  179. printf("%-9s %-38s %7u opaque px\n",
  180. icons[i].name, icons[i].file, opaque);
  181. free(arena);
  182. free(data);
  183. cur_file = NULL;
  184. }
  185. if (fclose(out) != 0) {
  186. fprintf(stderr, "mkappicons: write error\n");
  187. return 1;
  188. }
  189. if (verbose)
  190. printf("wrote %s: %u icons at %ux%u\n",
  191. out_path, (unsigned int)count, size, size);
  192. return 0;
  193. }