pnmtologo.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /*
  2. * Convert a logo in ASCII PNM format to C source suitable for inclusion in
  3. * the Linux kernel
  4. *
  5. * (C) Copyright 2001-2003 by Geert Uytterhoeven <geert@linux-m68k.org>
  6. *
  7. * --------------------------------------------------------------------------
  8. *
  9. * This file is subject to the terms and conditions of the GNU General Public
  10. * License. See the file COPYING in the main directory of the Linux
  11. * distribution for more details.
  12. */
  13. #include <ctype.h>
  14. #include <errno.h>
  15. #include <stdarg.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. static const char *programname;
  21. static const char *filename;
  22. static const char *logoname = "linux_logo";
  23. static const char *outputname;
  24. static FILE *out;
  25. #define LINUX_LOGO_MONO 1 /* monochrome black/white */
  26. #define LINUX_LOGO_VGA16 2 /* 16 colors VGA text palette */
  27. #define LINUX_LOGO_CLUT224 3 /* 224 colors */
  28. #define LINUX_LOGO_GRAY256 4 /* 256 levels grayscale */
  29. static const char *logo_types[LINUX_LOGO_GRAY256+1] = {
  30. [LINUX_LOGO_MONO] = "LINUX_LOGO_MONO",
  31. [LINUX_LOGO_VGA16] = "LINUX_LOGO_VGA16",
  32. [LINUX_LOGO_CLUT224] = "LINUX_LOGO_CLUT224",
  33. [LINUX_LOGO_GRAY256] = "LINUX_LOGO_GRAY256"
  34. };
  35. #define MAX_LINUX_LOGO_COLORS 224
  36. struct color {
  37. unsigned char red;
  38. unsigned char green;
  39. unsigned char blue;
  40. };
  41. static const struct color clut_vga16[16] = {
  42. { 0x00, 0x00, 0x00 },
  43. { 0x00, 0x00, 0xaa },
  44. { 0x00, 0xaa, 0x00 },
  45. { 0x00, 0xaa, 0xaa },
  46. { 0xaa, 0x00, 0x00 },
  47. { 0xaa, 0x00, 0xaa },
  48. { 0xaa, 0x55, 0x00 },
  49. { 0xaa, 0xaa, 0xaa },
  50. { 0x55, 0x55, 0x55 },
  51. { 0x55, 0x55, 0xff },
  52. { 0x55, 0xff, 0x55 },
  53. { 0x55, 0xff, 0xff },
  54. { 0xff, 0x55, 0x55 },
  55. { 0xff, 0x55, 0xff },
  56. { 0xff, 0xff, 0x55 },
  57. { 0xff, 0xff, 0xff },
  58. };
  59. static int logo_type = LINUX_LOGO_CLUT224;
  60. static unsigned int logo_width;
  61. static unsigned int logo_height;
  62. static struct color **logo_data;
  63. static struct color logo_clut[MAX_LINUX_LOGO_COLORS];
  64. static unsigned int logo_clutsize;
  65. static int is_plain_pbm = 0;
  66. static void die(const char *fmt, ...)
  67. __attribute__ ((noreturn)) __attribute ((format (printf, 1, 2)));
  68. static void usage(void) __attribute ((noreturn));
  69. static unsigned int get_number(FILE *fp)
  70. {
  71. int c, val;
  72. /* Skip leading whitespace */
  73. do {
  74. c = fgetc(fp);
  75. if (c == EOF)
  76. die("%s: end of file\n", filename);
  77. if (c == '#') {
  78. /* Ignore comments 'till end of line */
  79. do {
  80. c = fgetc(fp);
  81. if (c == EOF)
  82. die("%s: end of file\n", filename);
  83. } while (c != '\n');
  84. }
  85. } while (isspace(c));
  86. /* Parse decimal number */
  87. val = 0;
  88. while (isdigit(c)) {
  89. val = 10*val+c-'0';
  90. /* some PBM are 'broken'; GiMP for example exports a PBM without space
  91. * between the digits. This is Ok cause we know a PBM can only have a '1'
  92. * or a '0' for the digit. */
  93. if (is_plain_pbm)
  94. break;
  95. c = fgetc(fp);
  96. if (c == EOF)
  97. die("%s: end of file\n", filename);
  98. }
  99. return val;
  100. }
  101. static unsigned int get_number255(FILE *fp, unsigned int maxval)
  102. {
  103. unsigned int val = get_number(fp);
  104. return (255*val+maxval/2)/maxval;
  105. }
  106. static void read_image(void)
  107. {
  108. FILE *fp;
  109. unsigned int i, j;
  110. int magic;
  111. unsigned int maxval;
  112. /* open image file */
  113. fp = fopen(filename, "r");
  114. if (!fp)
  115. die("Cannot open file %s: %s\n", filename, strerror(errno));
  116. /* check file type and read file header */
  117. magic = fgetc(fp);
  118. if (magic != 'P')
  119. die("%s is not a PNM file\n", filename);
  120. magic = fgetc(fp);
  121. switch (magic) {
  122. case '1':
  123. case '2':
  124. case '3':
  125. /* Plain PBM/PGM/PPM */
  126. break;
  127. case '4':
  128. case '5':
  129. case '6':
  130. /* Binary PBM/PGM/PPM */
  131. die("%s: Binary PNM is not supported\n"
  132. "Use pnmnoraw(1) to convert it to ASCII PNM\n", filename);
  133. default:
  134. die("%s is not a PNM file\n", filename);
  135. }
  136. logo_width = get_number(fp);
  137. logo_height = get_number(fp);
  138. /* allocate image data */
  139. logo_data = (struct color **)malloc(logo_height*sizeof(struct color *));
  140. if (!logo_data)
  141. die("%s\n", strerror(errno));
  142. for (i = 0; i < logo_height; i++) {
  143. logo_data[i] = malloc(logo_width*sizeof(struct color));
  144. if (!logo_data[i])
  145. die("%s\n", strerror(errno));
  146. }
  147. /* read image data */
  148. switch (magic) {
  149. case '1':
  150. /* Plain PBM */
  151. is_plain_pbm = 1;
  152. for (i = 0; i < logo_height; i++)
  153. for (j = 0; j < logo_width; j++)
  154. logo_data[i][j].red = logo_data[i][j].green =
  155. logo_data[i][j].blue = 255*(1-get_number(fp));
  156. break;
  157. case '2':
  158. /* Plain PGM */
  159. maxval = get_number(fp);
  160. for (i = 0; i < logo_height; i++)
  161. for (j = 0; j < logo_width; j++)
  162. logo_data[i][j].red = logo_data[i][j].green =
  163. logo_data[i][j].blue = get_number255(fp, maxval);
  164. break;
  165. case '3':
  166. /* Plain PPM */
  167. maxval = get_number(fp);
  168. for (i = 0; i < logo_height; i++)
  169. for (j = 0; j < logo_width; j++) {
  170. logo_data[i][j].red = get_number255(fp, maxval);
  171. logo_data[i][j].green = get_number255(fp, maxval);
  172. logo_data[i][j].blue = get_number255(fp, maxval);
  173. }
  174. break;
  175. }
  176. /* close file */
  177. fclose(fp);
  178. }
  179. static inline int is_black(struct color c)
  180. {
  181. return c.red == 0 && c.green == 0 && c.blue == 0;
  182. }
  183. static inline int is_white(struct color c)
  184. {
  185. return c.red == 255 && c.green == 255 && c.blue == 255;
  186. }
  187. static inline int is_gray(struct color c)
  188. {
  189. return c.red == c.green && c.red == c.blue;
  190. }
  191. static inline int is_equal(struct color c1, struct color c2)
  192. {
  193. return c1.red == c2.red && c1.green == c2.green && c1.blue == c2.blue;
  194. }
  195. static void write_header(void)
  196. {
  197. /* open logo file */
  198. if (outputname) {
  199. out = fopen(outputname, "w");
  200. if (!out)
  201. die("Cannot create file %s: %s\n", outputname, strerror(errno));
  202. } else {
  203. out = stdout;
  204. }
  205. fputs("/*\n", out);
  206. fputs(" * DO NOT EDIT THIS FILE!\n", out);
  207. fputs(" *\n", out);
  208. fprintf(out, " * It was automatically generated from %s\n", filename);
  209. fputs(" *\n", out);
  210. fprintf(out, " * Linux logo %s\n", logoname);
  211. fputs(" */\n\n", out);
  212. fputs("#include <linux/linux_logo.h>\n\n", out);
  213. fprintf(out, "static unsigned char %s_data[] __initdata = {\n",
  214. logoname);
  215. }
  216. static void write_footer(void)
  217. {
  218. fputs("\n};\n\n", out);
  219. fprintf(out, "const struct linux_logo %s __initconst = {\n", logoname);
  220. fprintf(out, "\t.type\t\t= %s,\n", logo_types[logo_type]);
  221. fprintf(out, "\t.width\t\t= %d,\n", logo_width);
  222. fprintf(out, "\t.height\t\t= %d,\n", logo_height);
  223. if (logo_type == LINUX_LOGO_CLUT224) {
  224. fprintf(out, "\t.clutsize\t= %d,\n", logo_clutsize);
  225. fprintf(out, "\t.clut\t\t= %s_clut,\n", logoname);
  226. }
  227. fprintf(out, "\t.data\t\t= %s_data\n", logoname);
  228. fputs("};\n\n", out);
  229. /* close logo file */
  230. if (outputname)
  231. fclose(out);
  232. }
  233. static int write_hex_cnt;
  234. static void write_hex(unsigned char byte)
  235. {
  236. if (write_hex_cnt % 12)
  237. fprintf(out, ", 0x%02x", byte);
  238. else if (write_hex_cnt)
  239. fprintf(out, ",\n\t0x%02x", byte);
  240. else
  241. fprintf(out, "\t0x%02x", byte);
  242. write_hex_cnt++;
  243. }
  244. static void write_logo_mono(void)
  245. {
  246. unsigned int i, j;
  247. unsigned char val, bit;
  248. /* validate image */
  249. for (i = 0; i < logo_height; i++)
  250. for (j = 0; j < logo_width; j++)
  251. if (!is_black(logo_data[i][j]) && !is_white(logo_data[i][j]))
  252. die("Image must be monochrome\n");
  253. /* write file header */
  254. write_header();
  255. /* write logo data */
  256. for (i = 0; i < logo_height; i++) {
  257. for (j = 0; j < logo_width;) {
  258. for (val = 0, bit = 0x80; bit && j < logo_width; j++, bit >>= 1)
  259. if (logo_data[i][j].red)
  260. val |= bit;
  261. write_hex(val);
  262. }
  263. }
  264. /* write logo structure and file footer */
  265. write_footer();
  266. }
  267. static void write_logo_vga16(void)
  268. {
  269. unsigned int i, j, k;
  270. unsigned char val;
  271. /* validate image */
  272. for (i = 0; i < logo_height; i++)
  273. for (j = 0; j < logo_width; j++) {
  274. for (k = 0; k < 16; k++)
  275. if (is_equal(logo_data[i][j], clut_vga16[k]))
  276. break;
  277. if (k == 16)
  278. die("Image must use the 16 console colors only\n"
  279. "Use ppmquant(1) -map clut_vga16.ppm to reduce the number "
  280. "of colors\n");
  281. }
  282. /* write file header */
  283. write_header();
  284. /* write logo data */
  285. for (i = 0; i < logo_height; i++)
  286. for (j = 0; j < logo_width; j++) {
  287. for (k = 0; k < 16; k++)
  288. if (is_equal(logo_data[i][j], clut_vga16[k]))
  289. break;
  290. val = k<<4;
  291. if (++j < logo_width) {
  292. for (k = 0; k < 16; k++)
  293. if (is_equal(logo_data[i][j], clut_vga16[k]))
  294. break;
  295. val |= k;
  296. }
  297. write_hex(val);
  298. }
  299. /* write logo structure and file footer */
  300. write_footer();
  301. }
  302. static void write_logo_clut224(void)
  303. {
  304. unsigned int i, j, k;
  305. /* validate image */
  306. for (i = 0; i < logo_height; i++)
  307. for (j = 0; j < logo_width; j++) {
  308. for (k = 0; k < logo_clutsize; k++)
  309. if (is_equal(logo_data[i][j], logo_clut[k]))
  310. break;
  311. if (k == logo_clutsize) {
  312. if (logo_clutsize == MAX_LINUX_LOGO_COLORS)
  313. die("Image has more than %d colors\n"
  314. "Use ppmquant(1) to reduce the number of colors\n",
  315. MAX_LINUX_LOGO_COLORS);
  316. logo_clut[logo_clutsize++] = logo_data[i][j];
  317. }
  318. }
  319. /* write file header */
  320. write_header();
  321. /* write logo data */
  322. for (i = 0; i < logo_height; i++)
  323. for (j = 0; j < logo_width; j++) {
  324. for (k = 0; k < logo_clutsize; k++)
  325. if (is_equal(logo_data[i][j], logo_clut[k]))
  326. break;
  327. write_hex(k+32);
  328. }
  329. fputs("\n};\n\n", out);
  330. /* write logo clut */
  331. fprintf(out, "static unsigned char %s_clut[] __initdata = {\n",
  332. logoname);
  333. write_hex_cnt = 0;
  334. for (i = 0; i < logo_clutsize; i++) {
  335. write_hex(logo_clut[i].red);
  336. write_hex(logo_clut[i].green);
  337. write_hex(logo_clut[i].blue);
  338. }
  339. /* write logo structure and file footer */
  340. write_footer();
  341. }
  342. static void write_logo_gray256(void)
  343. {
  344. unsigned int i, j;
  345. /* validate image */
  346. for (i = 0; i < logo_height; i++)
  347. for (j = 0; j < logo_width; j++)
  348. if (!is_gray(logo_data[i][j]))
  349. die("Image must be grayscale\n");
  350. /* write file header */
  351. write_header();
  352. /* write logo data */
  353. for (i = 0; i < logo_height; i++)
  354. for (j = 0; j < logo_width; j++)
  355. write_hex(logo_data[i][j].red);
  356. /* write logo structure and file footer */
  357. write_footer();
  358. }
  359. static void die(const char *fmt, ...)
  360. {
  361. va_list ap;
  362. va_start(ap, fmt);
  363. vfprintf(stderr, fmt, ap);
  364. va_end(ap);
  365. exit(1);
  366. }
  367. static void usage(void)
  368. {
  369. die("\n"
  370. "Usage: %s [options] <filename>\n"
  371. "\n"
  372. "Valid options:\n"
  373. " -h : display this usage information\n"
  374. " -n <name> : specify logo name (default: linux_logo)\n"
  375. " -o <output> : output to file <output> instead of stdout\n"
  376. " -t <type> : specify logo type, one of\n"
  377. " mono : monochrome black/white\n"
  378. " vga16 : 16 colors VGA text palette\n"
  379. " clut224 : 224 colors (default)\n"
  380. " gray256 : 256 levels grayscale\n"
  381. "\n", programname);
  382. }
  383. int main(int argc, char *argv[])
  384. {
  385. int opt;
  386. programname = argv[0];
  387. opterr = 0;
  388. while (1) {
  389. opt = getopt(argc, argv, "hn:o:t:");
  390. if (opt == -1)
  391. break;
  392. switch (opt) {
  393. case 'h':
  394. usage();
  395. break;
  396. case 'n':
  397. logoname = optarg;
  398. break;
  399. case 'o':
  400. outputname = optarg;
  401. break;
  402. case 't':
  403. if (!strcmp(optarg, "mono"))
  404. logo_type = LINUX_LOGO_MONO;
  405. else if (!strcmp(optarg, "vga16"))
  406. logo_type = LINUX_LOGO_VGA16;
  407. else if (!strcmp(optarg, "clut224"))
  408. logo_type = LINUX_LOGO_CLUT224;
  409. else if (!strcmp(optarg, "gray256"))
  410. logo_type = LINUX_LOGO_GRAY256;
  411. else
  412. usage();
  413. break;
  414. default:
  415. usage();
  416. break;
  417. }
  418. }
  419. if (optind != argc-1)
  420. usage();
  421. filename = argv[optind];
  422. read_image();
  423. switch (logo_type) {
  424. case LINUX_LOGO_MONO:
  425. write_logo_mono();
  426. break;
  427. case LINUX_LOGO_VGA16:
  428. write_logo_vga16();
  429. break;
  430. case LINUX_LOGO_CLUT224:
  431. write_logo_clut224();
  432. break;
  433. case LINUX_LOGO_GRAY256:
  434. write_logo_gray256();
  435. break;
  436. }
  437. exit(0);
  438. }