PageRenderTime 820ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/scripts/pnmtologo.c

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