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[] = {
87 static int logo_type = LINUX_LOGO_CLUT224;
88 static unsigned int logo_width;
89 static unsigned int logo_height;
90 static struct color **logo_data;
91 static struct color logo_clut[MAX_LINUX_LOGO_COLORS];
92 static unsigned int logo_clutsize;
94 static void die(const char *fmt, ...)
95 __attribute__ ((noreturn)) __attribute ((format (printf, 1, 2)));
96 static void usage(void) __attribute ((noreturn));
98 static unsigned int get_number(FILE *fp)
102 /* Skip leading whitespace */
106 die("%s: end of file\n", filename);
108 /* Ignore comments 'till end of line */
112 die("%s: end of file\n", filename);
115 } while (isspace(c));
117 /* Parse decimal number */
123 die("%s: end of file\n", filename);
128 static unsigned int get_number255(FILE *fp, unsigned int maxval)
130 unsigned int val = get_number(fp);
131 return (255*val+maxval/2)/maxval;
134 static void read_image(void)
141 /* open image file */
142 fp = fopen(filename, "r");
144 die("Cannot open file %s: %s\n", filename, strerror(errno));
146 /* check file type and read file header */
149 die("%s is not a PNM file\n", filename);
155 /* Plain PBM/PGM/PPM */
161 /* Binary PBM/PGM/PPM */
162 die("%s: Binary PNM is not supported\n"
163 "Use pnmnoraw(1) to convert it to ASCII PNM\n", filename);
166 die("%s is not a PNM file\n", filename);
168 logo_width = get_number(fp);
169 logo_height = get_number(fp);
172 /* allocate image data */
173 logo_data = (struct color **)malloc(logo_height * sizeof(struct color *));
175 die("%s\n", strerror(errno));
176 for (i = 0; i < logo_height; i++) {
177 logo_data[i] = (struct color *)malloc(logo_width * sizeof(struct color));
179 die("%s\n", strerror(errno));
182 /* read image data */
186 for (i = 0; i < logo_height; i++)
187 for (j = 0; j < logo_width; j++)
188 logo_data[i][j].red = logo_data[i][j].green =
189 logo_data[i][j].blue = 255*(1-get_number(fp));
194 maxval = get_number(fp);
195 for (i = 0; i < logo_height; i++)
196 for (j = 0; j < logo_width; j++)
197 logo_data[i][j].red = logo_data[i][j].green =
198 logo_data[i][j].blue = get_number255(fp, maxval);
203 maxval = get_number(fp);
204 for (i = 0; i < logo_height; i++)
205 for (j = 0; j < logo_width; j++) {
206 logo_data[i][j].red = get_number255(fp, maxval);
207 logo_data[i][j].green = get_number255(fp, maxval);
208 logo_data[i][j].blue = get_number255(fp, maxval);
217 static inline int is_black(struct color c)
219 return c.red == 0 && c.green == 0 && c.blue == 0;
222 static inline int is_white(struct color c)
224 return c.red == 255 && c.green == 255 && c.blue == 255;
227 static inline int is_gray(struct color c)
229 return c.red == c.green && c.red == c.blue;
232 static inline int is_equal(struct color c1, struct color c2)
234 return c1.red == c2.red && c1.green == c2.green && c1.blue == c2.blue;
237 static int write_hex_cnt;
239 static void write_hex(unsigned char byte)
241 if (write_hex_cnt % 12)
242 fprintf(out, ", 0x%02x", byte);
243 else if (write_hex_cnt)
244 fprintf(out, ",\n\t0x%02x", byte);
246 fprintf(out, "\t0x%02x", byte);
250 static void write_header(void)
254 out = fopen(outputname, "w");
256 die("Cannot create file %s: %s\n", outputname, strerror(errno));
262 fputs(" * DO NOT EDIT THIS FILE!\n", out);
264 fprintf(out, " * It was automatically generated from %s\n", filename);
266 fprintf(out, " * Linux logo %s\n", logoname);
267 fputs(" */\n\n", out);
268 fputs("#include <linux/linux_logo.h>\n\n", out);
269 fprintf(out, "static unsigned char %s_data[] __initdata = {\n",
273 static void write_footer(void)
275 fputs("\n};\n\n", out);
276 fprintf(out, "const struct linux_logo %s __initconst = {\n", logoname);
277 fprintf(out, "\t.type\t\t= %s,\n", logo_types[logo_type]);
279 if (logo_type == LINUX_LOGO_CLUT224) {
280 fprintf(out, "\t.clut\t\t= &(%s_clut[%ld]),\n", logoname, sizeof(clut_name));
281 fprintf(out, "\t.data\t\t= &(%s_data[%ld])\n", logoname, sizeof(data_name) + 4);
283 fprintf(out, "\t.width\t\t= %d,\n", logo_width);
284 fprintf(out, "\t.height\t\t= %d,\n", logo_height);
285 if (logo_type == LINUX_LOGO_CLUT224) {
286 fprintf(out, "\t.clutsize\t= %d,\n", logo_clutsize);
287 fprintf(out, "\t.clut\t\t= %s_clut,\n", logoname);
289 fprintf(out, "\t.data\t\t= %s_data\n", logoname);
292 fputs("};\n\n", out);
294 /* close logo file */
299 static void write_logo_mono(void)
302 unsigned char val, bit;
305 for (i = 0; i < logo_height; i++)
306 for (j = 0; j < logo_width; j++)
307 if (!is_black(logo_data[i][j]) && !is_white(logo_data[i][j]))
308 die("Image must be monochrome\n");
310 /* write file header */
313 /* write logo data */
314 for (i = 0; i < logo_height; i++) {
315 for (j = 0; j < logo_width;) {
316 for (val = 0, bit = 0x80; bit && j < logo_width; j++, bit >>= 1)
317 if (logo_data[i][j].red)
324 /* write logo structure and file footer */
328 static void write_logo_vga16(void)
330 unsigned int i, j, k;
334 for (i = 0; i < logo_height; i++)
335 for (j = 0; j < logo_width; j++) {
336 for (k = 0; k < 16; k++)
337 if (is_equal(logo_data[i][j], clut_vga16[k]))
340 die("Image must use the 16 console colors only\n"
341 "Use ppmquant(1) -map clut_vga16.ppm to reduce the number "
345 /* write file header */
348 /* write logo data */
349 for (i = 0; i < logo_height; i++)
350 for (j = 0; j < logo_width; j++) {
351 for (k = 0; k < 16; k++)
352 if (is_equal(logo_data[i][j], clut_vga16[k]))
355 if (++j < logo_width) {
356 for (k = 0; k < 16; k++)
357 if (is_equal(logo_data[i][j], clut_vga16[k]))
364 /* write logo structure and file footer */
368 static void write_logo_clut224(void)
370 unsigned int i, j, k;
375 for (i = 0; i < logo_height; i++)
376 for (j = 0; j < logo_width; j++) {
377 for (k = 0; k < logo_clutsize; k++)
378 if (is_equal(logo_data[i][j], logo_clut[k]))
380 if (k == logo_clutsize) {
381 if (logo_clutsize == MAX_LINUX_LOGO_COLORS)
382 die("Image has more than %d colors\n"
383 "Use ppmquant(1) to reduce the number of colors\n",
384 MAX_LINUX_LOGO_COLORS);
385 logo_clut[logo_clutsize++] = logo_data[i][j];
391 /* write file header */
394 write_hex((unsigned char)(logo_width >> 8));
395 write_hex((unsigned char)logo_width);
396 write_hex((unsigned char)(logo_height >> 8));
397 write_hex((unsigned char)logo_height);
399 for (i = 0; i < sizeof(data_name); i++){
400 write_hex(data_name[i]);
402 write_hex((unsigned char)(logo_width >> 8));
403 write_hex((unsigned char)logo_width);
404 write_hex((unsigned char)(logo_height >> 8));
405 write_hex((unsigned char)logo_height);
407 /* write logo data */
408 for (i = 0; i < logo_height; i++)
409 for (j = 0; j < logo_width; j++) {
410 for (k = 0; k < logo_clutsize; k++)
411 if (is_equal(logo_data[i][j], logo_clut[k]))
416 fputs("\n};\n\n", out);
418 /* write logo clut */
419 fprintf(out, "static unsigned char %s_clut[] __initdata = {\n",
424 for (i = 0; i < sizeof(clut_name); i++){
425 write_hex(clut_name[i]);
427 write_hex(logo_clutsize);
429 for (i = 0; i < logo_clutsize; i++) {
430 write_hex(logo_clut[i].red);
431 write_hex(logo_clut[i].green);
432 write_hex(logo_clut[i].blue);
435 for (i = logo_clutsize; i < (MAX_LINUX_LOGO_COLORS * 3); i++)
440 /* write logo structure and file footer */
444 static void write_logo_gray256(void)
449 for (i = 0; i < logo_height; i++)
450 for (j = 0; j < logo_width; j++)
451 if (!is_gray(logo_data[i][j]))
452 die("Image must be grayscale\n");
454 /* write file header */
457 /* write logo data */
458 for (i = 0; i < logo_height; i++)
459 for (j = 0; j < logo_width; j++)
460 write_hex(logo_data[i][j].red);
462 /* write logo structure and file footer */
466 static void die(const char *fmt, ...)
471 vfprintf(stderr, fmt, ap);
477 static void usage(void)
480 "Usage: %s [options] <filename>\n"
483 " -h : display this usage information\n"
484 " -n <name> : specify logo name (default: linux_logo)\n"
485 " -o <output> : output to file <output> instead of stdout\n"
486 " -t <type> : specify logo type, one of\n"
487 " mono : monochrome black/white\n"
488 " vga16 : 16 colors VGA text palette\n"
489 " clut224 : 224 colors (default)\n"
490 " gray256 : 256 levels grayscale\n"
494 int main(int argc, char *argv[])
498 programname = argv[0];
502 opt = getopt(argc, argv, "hn:o:t:");
520 if (!strcmp(optarg, "mono"))
521 logo_type = LINUX_LOGO_MONO;
522 else if (!strcmp(optarg, "vga16"))
523 logo_type = LINUX_LOGO_VGA16;
524 else if (!strcmp(optarg, "clut224"))
525 logo_type = LINUX_LOGO_CLUT224;
526 else if (!strcmp(optarg, "gray256"))
527 logo_type = LINUX_LOGO_GRAY256;
537 if (optind != argc-1)
540 filename = argv[optind];
544 case LINUX_LOGO_MONO:
548 case LINUX_LOGO_VGA16:
552 case LINUX_LOGO_CLUT224:
553 write_logo_clut224();
556 case LINUX_LOGO_GRAY256:
557 write_logo_gray256();