3 * Convert a logo in ASCII PNM format to C source suitable for inclusion in
6 * (C) Copyright 2001-2003 by Geert Uytterhoeven <geert@linux-m68k.org>
8 * --------------------------------------------------------------------------
10 * This file is subject to the terms and conditions of the GNU General Public
11 * License. See the file COPYING in the main directory of the Linux
12 * distribution for more details.
24 static const char *programname;
25 static const char *filename;
26 static const char *logoname = "linux_logo";
27 static const char *outputname;
30 #define LINUX_LOGO_MONO 1 /* monochrome black/white */
31 #define LINUX_LOGO_VGA16 2 /* 16 colors VGA text palette */
32 #define LINUX_LOGO_CLUT224 3 /* 224 colors */
33 #define LINUX_LOGO_GRAY256 4 /* 256 levels grayscale */
35 static const char *logo_types[LINUX_LOGO_GRAY256+1] = {
36 [LINUX_LOGO_MONO] = "LINUX_LOGO_MONO",
37 [LINUX_LOGO_VGA16] = "LINUX_LOGO_VGA16",
38 [LINUX_LOGO_CLUT224] = "LINUX_LOGO_CLUT224",
39 [LINUX_LOGO_GRAY256] = "LINUX_LOGO_GRAY256"
42 #define MAX_LINUX_LOGO_COLORS 224
50 static const struct color clut_vga16[16] = {
69 unsigned char data_name[] = {
78 unsigned char clut_name[] = {
88 static int logo_type = LINUX_LOGO_CLUT224;
89 static unsigned int logo_width;
90 static unsigned int logo_height;
91 static struct color **logo_data;
92 static struct color logo_clut[MAX_LINUX_LOGO_COLORS];
93 static unsigned int logo_clutsize;
95 static void die(const char *fmt, ...)
96 __attribute__ ((noreturn)) __attribute ((format (printf, 1, 2)));
97 static void usage(void) __attribute ((noreturn));
99 static unsigned int get_number(FILE *fp)
103 /* Skip leading whitespace */
107 die("%s: end of file\n", filename);
109 /* Ignore comments 'till end of line */
113 die("%s: end of file\n", filename);
116 } while (isspace(c));
118 /* Parse decimal number */
124 die("%s: end of file\n", filename);
129 static unsigned int get_number255(FILE *fp, unsigned int maxval)
131 unsigned int val = get_number(fp);
132 return (255*val+maxval/2)/maxval;
135 static void read_image(void)
142 /* open image file */
143 fp = fopen(filename, "r");
145 die("Cannot open file %s: %s\n", filename, strerror(errno));
147 /* check file type and read file header */
150 die("%s is not a PNM file\n", filename);
156 /* Plain PBM/PGM/PPM */
162 /* Binary PBM/PGM/PPM */
163 die("%s: Binary PNM is not supported\n"
164 "Use pnmnoraw(1) to convert it to ASCII PNM\n", filename);
167 die("%s is not a PNM file\n", filename);
169 logo_width = get_number(fp);
170 logo_height = get_number(fp);
173 /* allocate image data */
174 logo_data = (struct color **)malloc(logo_height * sizeof(struct color *));
176 die("%s\n", strerror(errno));
177 for (i = 0; i < logo_height; i++) {
178 logo_data[i] = (struct color *)malloc(logo_width * sizeof(struct color));
180 die("%s\n", strerror(errno));
183 /* read image data */
187 for (i = 0; i < logo_height; i++)
188 for (j = 0; j < logo_width; j++)
189 logo_data[i][j].red = logo_data[i][j].green =
190 logo_data[i][j].blue = 255*(1-get_number(fp));
195 maxval = get_number(fp);
196 for (i = 0; i < logo_height; i++)
197 for (j = 0; j < logo_width; j++)
198 logo_data[i][j].red = logo_data[i][j].green =
199 logo_data[i][j].blue = get_number255(fp, maxval);
204 maxval = get_number(fp);
205 for (i = 0; i < logo_height; i++)
206 for (j = 0; j < logo_width; j++) {
207 logo_data[i][j].red = get_number255(fp, maxval);
208 logo_data[i][j].green = get_number255(fp, maxval);
209 logo_data[i][j].blue = get_number255(fp, maxval);
218 static inline int is_black(struct color c)
220 return c.red == 0 && c.green == 0 && c.blue == 0;
223 static inline int is_white(struct color c)
225 return c.red == 255 && c.green == 255 && c.blue == 255;
228 static inline int is_gray(struct color c)
230 return c.red == c.green && c.red == c.blue;
233 static inline int is_equal(struct color c1, struct color c2)
235 return c1.red == c2.red && c1.green == c2.green && c1.blue == c2.blue;
238 static int write_hex_cnt;
240 static void write_hex(unsigned char byte)
242 if (write_hex_cnt % 12)
243 fprintf(out, ", 0x%02x", byte);
244 else if (write_hex_cnt)
245 fprintf(out, ",\n\t0x%02x", byte);
247 fprintf(out, "\t0x%02x", byte);
251 static void write_header(void)
255 out = fopen(outputname, "w");
257 die("Cannot create file %s: %s\n", outputname, strerror(errno));
263 fputs(" * DO NOT EDIT THIS FILE!\n", out);
265 fprintf(out, " * It was automatically generated from %s\n", filename);
267 fprintf(out, " * Linux logo %s\n", logoname);
268 fputs(" */\n\n", out);
269 fputs("#include <linux/linux_logo.h>\n\n", out);
270 fprintf(out, "static unsigned char %s_data[] __initdata = {\n",
274 static void write_footer(void)
276 fputs("\n};\n\n", out);
277 fprintf(out, "const struct linux_logo %s __initconst = {\n", logoname);
278 fprintf(out, "\t.type\t\t= %s,\n", logo_types[logo_type]);
280 if (logo_type == LINUX_LOGO_CLUT224) {
281 fprintf(out, "\t.clut\t\t= &(%s_clut[%ld]),\n", logoname, sizeof(clut_name));
282 fprintf(out, "\t.data\t\t= &(%s_data[%ld])\n", logoname, sizeof(data_name) + 4);
284 fprintf(out, "\t.width\t\t= %d,\n", logo_width);
285 fprintf(out, "\t.height\t\t= %d,\n", logo_height);
286 if (logo_type == LINUX_LOGO_CLUT224) {
287 fprintf(out, "\t.clutsize\t= %d,\n", logo_clutsize);
288 fprintf(out, "\t.clut\t\t= %s_clut,\n", logoname);
290 fprintf(out, "\t.data\t\t= %s_data\n", logoname);
293 fputs("};\n\n", out);
295 /* close logo file */
300 static void write_logo_mono(void)
303 unsigned char val, bit;
306 for (i = 0; i < logo_height; i++)
307 for (j = 0; j < logo_width; j++)
308 if (!is_black(logo_data[i][j]) && !is_white(logo_data[i][j]))
309 die("Image must be monochrome\n");
311 /* write file header */
314 /* write logo data */
315 for (i = 0; i < logo_height; i++) {
316 for (j = 0; j < logo_width;) {
317 for (val = 0, bit = 0x80; bit && j < logo_width; j++, bit >>= 1)
318 if (logo_data[i][j].red)
325 /* write logo structure and file footer */
329 static void write_logo_vga16(void)
331 unsigned int i, j, k;
335 for (i = 0; i < logo_height; i++)
336 for (j = 0; j < logo_width; j++) {
337 for (k = 0; k < 16; k++)
338 if (is_equal(logo_data[i][j], clut_vga16[k]))
341 die("Image must use the 16 console colors only\n"
342 "Use ppmquant(1) -map clut_vga16.ppm to reduce the number "
346 /* write file header */
349 /* write logo data */
350 for (i = 0; i < logo_height; i++)
351 for (j = 0; j < logo_width; j++) {
352 for (k = 0; k < 16; k++)
353 if (is_equal(logo_data[i][j], clut_vga16[k]))
356 if (++j < logo_width) {
357 for (k = 0; k < 16; k++)
358 if (is_equal(logo_data[i][j], clut_vga16[k]))
365 /* write logo structure and file footer */
369 static void write_logo_clut224(void)
371 unsigned int i, j, k;
376 for (i = 0; i < logo_height; i++)
377 for (j = 0; j < logo_width; j++) {
378 for (k = 0; k < logo_clutsize; k++)
379 if (is_equal(logo_data[i][j], logo_clut[k]))
381 if (k == logo_clutsize) {
382 if (logo_clutsize == MAX_LINUX_LOGO_COLORS)
383 die("Image has more than %d colors\n"
384 "Use ppmquant(1) to reduce the number of colors\n",
385 MAX_LINUX_LOGO_COLORS);
386 logo_clut[logo_clutsize++] = logo_data[i][j];
392 /* write file header */
395 write_hex((unsigned char)(logo_width >> 8));
396 write_hex((unsigned char)logo_width);
397 write_hex((unsigned char)(logo_height >> 8));
398 write_hex((unsigned char)logo_height);
400 for (i = 0; i < sizeof(data_name); i++){
401 write_hex(data_name[i]);
403 write_hex((unsigned char)(logo_width >> 8));
404 write_hex((unsigned char)logo_width);
405 write_hex((unsigned char)(logo_height >> 8));
406 write_hex((unsigned char)logo_height);
408 /* write logo data */
409 for (i = 0; i < logo_height; i++)
410 for (j = 0; j < logo_width; j++) {
411 for (k = 0; k < logo_clutsize; k++)
412 if (is_equal(logo_data[i][j], logo_clut[k]))
417 fputs("\n};\n\n", out);
419 /* write logo clut */
420 fprintf(out, "static unsigned char %s_clut[] __initdata = {\n",
425 for (i = 0; i < sizeof(clut_name); i++){
426 write_hex(clut_name[i]);
428 write_hex(logo_clutsize);
430 for (i = 0; i < logo_clutsize; i++) {
431 write_hex(logo_clut[i].red);
432 write_hex(logo_clut[i].green);
433 write_hex(logo_clut[i].blue);
436 for (i = logo_clutsize; i < (MAX_LINUX_LOGO_COLORS * 3); i++)
441 /* write logo structure and file footer */
445 static void write_logo_gray256(void)
450 for (i = 0; i < logo_height; i++)
451 for (j = 0; j < logo_width; j++)
452 if (!is_gray(logo_data[i][j]))
453 die("Image must be grayscale\n");
455 /* write file header */
458 /* write logo data */
459 for (i = 0; i < logo_height; i++)
460 for (j = 0; j < logo_width; j++)
461 write_hex(logo_data[i][j].red);
463 /* write logo structure and file footer */
467 static void die(const char *fmt, ...)
472 vfprintf(stderr, fmt, ap);
478 static void usage(void)
481 "Usage: %s [options] <filename>\n"
484 " -h : display this usage information\n"
485 " -n <name> : specify logo name (default: linux_logo)\n"
486 " -o <output> : output to file <output> instead of stdout\n"
487 " -t <type> : specify logo type, one of\n"
488 " mono : monochrome black/white\n"
489 " vga16 : 16 colors VGA text palette\n"
490 " clut224 : 224 colors (default)\n"
491 " gray256 : 256 levels grayscale\n"
495 int main(int argc, char *argv[])
499 programname = argv[0];
503 opt = getopt(argc, argv, "hn:o:t:");
521 if (!strcmp(optarg, "mono"))
522 logo_type = LINUX_LOGO_MONO;
523 else if (!strcmp(optarg, "vga16"))
524 logo_type = LINUX_LOGO_VGA16;
525 else if (!strcmp(optarg, "clut224"))
526 logo_type = LINUX_LOGO_CLUT224;
527 else if (!strcmp(optarg, "gray256"))
528 logo_type = LINUX_LOGO_GRAY256;
538 if (optind != argc-1)
541 filename = argv[optind];
545 case LINUX_LOGO_MONO:
549 case LINUX_LOGO_VGA16:
553 case LINUX_LOGO_CLUT224:
554 write_logo_clut224();
557 case LINUX_LOGO_GRAY256:
558 write_logo_gray256();