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
43 #define MAX_LINUX_LOGO_WIDTH 1280
44 #define MAX_LINUX_LOGO_HEIGHT 800
52 static const struct color clut_vga16[16] = {
71 unsigned char data_name[] = {
80 unsigned char clut_name[] = {
89 static int logo_type = LINUX_LOGO_CLUT224;
90 static unsigned int logo_width;
91 static unsigned int logo_height;
92 static struct color **logo_data;
93 static struct color logo_clut[MAX_LINUX_LOGO_COLORS];
94 static unsigned int logo_clutsize;
96 static void die(const char *fmt, ...)
97 __attribute__ ((noreturn)) __attribute ((format (printf, 1, 2)));
98 static void usage(void) __attribute ((noreturn));
100 static unsigned int get_number(FILE *fp)
104 /* Skip leading whitespace */
108 die("%s: end of file\n", filename);
110 /* Ignore comments 'till end of line */
114 die("%s: end of file\n", filename);
117 } while (isspace(c));
119 /* Parse decimal number */
125 die("%s: end of file\n", filename);
130 static unsigned int get_number255(FILE *fp, unsigned int maxval)
132 unsigned int val = get_number(fp);
133 return (255*val+maxval/2)/maxval;
136 static void read_image(void)
143 /* open image file */
144 fp = fopen(filename, "r");
146 die("Cannot open file %s: %s\n", filename, strerror(errno));
148 /* check file type and read file header */
151 die("%s is not a PNM file\n", filename);
157 /* Plain PBM/PGM/PPM */
163 /* Binary PBM/PGM/PPM */
164 die("%s: Binary PNM is not supported\n"
165 "Use pnmnoraw(1) to convert it to ASCII PNM\n", filename);
168 die("%s is not a PNM file\n", filename);
170 logo_width = get_number(fp);
171 logo_height = get_number(fp);
174 /* allocate image data */
175 logo_data = (struct color **)malloc(logo_height * sizeof(struct color *));
177 die("%s\n", strerror(errno));
178 for (i = 0; i < logo_height; i++) {
179 logo_data[i] = (struct color *)malloc(logo_width * sizeof(struct color));
181 die("%s\n", strerror(errno));
184 /* read image data */
188 for (i = 0; i < logo_height; i++)
189 for (j = 0; j < logo_width; j++)
190 logo_data[i][j].red = logo_data[i][j].green =
191 logo_data[i][j].blue = 255*(1-get_number(fp));
196 maxval = get_number(fp);
197 for (i = 0; i < logo_height; i++)
198 for (j = 0; j < logo_width; j++)
199 logo_data[i][j].red = logo_data[i][j].green =
200 logo_data[i][j].blue = get_number255(fp, maxval);
205 maxval = get_number(fp);
206 for (i = 0; i < logo_height; i++)
207 for (j = 0; j < logo_width; j++) {
208 logo_data[i][j].red = get_number255(fp, maxval);
209 logo_data[i][j].green = get_number255(fp, maxval);
210 logo_data[i][j].blue = get_number255(fp, maxval);
219 static inline int is_black(struct color c)
221 return c.red == 0 && c.green == 0 && c.blue == 0;
224 static inline int is_white(struct color c)
226 return c.red == 255 && c.green == 255 && c.blue == 255;
229 static inline int is_gray(struct color c)
231 return c.red == c.green && c.red == c.blue;
234 static inline int is_equal(struct color c1, struct color c2)
236 return c1.red == c2.red && c1.green == c2.green && c1.blue == c2.blue;
239 static int write_hex_cnt;
241 static void write_hex(unsigned char byte)
243 if (write_hex_cnt % 12)
244 fprintf(out, ", 0x%02x", byte);
245 else if (write_hex_cnt)
246 fprintf(out, ",\n\t0x%02x", byte);
248 fprintf(out, "\t0x%02x", byte);
252 static void write_header(void)
256 out = fopen(outputname, "w");
258 die("Cannot create file %s: %s\n", outputname, strerror(errno));
264 fputs(" * DO NOT EDIT THIS FILE!\n", out);
266 fprintf(out, " * It was automatically generated from %s\n", filename);
268 fprintf(out, " * Linux logo %s\n", logoname);
269 fputs(" */\n\n", out);
270 fputs("#include <linux/linux_logo.h>\n\n", out);
271 fprintf(out, "static unsigned char %s_data[] __initdata = {\n",
275 static void write_footer(void)
277 fputs("\n};\n\n", out);
278 fprintf(out, "const struct linux_logo %s __initconst = {\n", logoname);
279 fprintf(out, "\t.type\t\t= %s,\n", logo_types[logo_type]);
281 if (logo_type == LINUX_LOGO_CLUT224) {
282 fprintf(out, "\t.clut\t\t= &(%s_clut[%ld]),\n", logoname, sizeof(clut_name));
283 fprintf(out, "\t.data\t\t= &(%s_data[%ld])\n", logoname, sizeof(data_name));
285 fprintf(out, "\t.width\t\t= %d,\n", logo_width);
286 fprintf(out, "\t.height\t\t= %d,\n", logo_height);
287 if (logo_type == LINUX_LOGO_CLUT224) {
288 fprintf(out, "\t.clutsize\t= %d,\n", logo_clutsize);
289 fprintf(out, "\t.clut\t\t= %s_clut,\n", logoname);
291 fprintf(out, "\t.data\t\t= %s_data\n", logoname);
294 fputs("};\n\n", out);
296 /* close logo file */
301 static void write_logo_mono(void)
304 unsigned char val, bit;
307 for (i = 0; i < logo_height; i++)
308 for (j = 0; j < logo_width; j++)
309 if (!is_black(logo_data[i][j]) && !is_white(logo_data[i][j]))
310 die("Image must be monochrome\n");
312 /* write file header */
315 /* write logo data */
316 for (i = 0; i < logo_height; i++) {
317 for (j = 0; j < logo_width;) {
318 for (val = 0, bit = 0x80; bit && j < logo_width; j++, bit >>= 1)
319 if (logo_data[i][j].red)
326 /* write logo structure and file footer */
330 static void write_logo_vga16(void)
332 unsigned int i, j, k;
336 for (i = 0; i < logo_height; i++)
337 for (j = 0; j < logo_width; j++) {
338 for (k = 0; k < 16; k++)
339 if (is_equal(logo_data[i][j], clut_vga16[k]))
342 die("Image must use the 16 console colors only\n"
343 "Use ppmquant(1) -map clut_vga16.ppm to reduce the number "
347 /* write file header */
350 /* write logo data */
351 for (i = 0; i < logo_height; i++)
352 for (j = 0; j < logo_width; j++) {
353 for (k = 0; k < 16; k++)
354 if (is_equal(logo_data[i][j], clut_vga16[k]))
357 if (++j < logo_width) {
358 for (k = 0; k < 16; k++)
359 if (is_equal(logo_data[i][j], clut_vga16[k]))
366 /* write logo structure and file footer */
370 static void write_logo_clut224(void)
372 unsigned int i, j, k;
377 for (i = 0; i < logo_height; i++)
378 for (j = 0; j < logo_width; j++) {
379 for (k = 0; k < logo_clutsize; k++)
380 if (is_equal(logo_data[i][j], logo_clut[k]))
382 if (k == logo_clutsize) {
383 if (logo_clutsize == MAX_LINUX_LOGO_COLORS)
384 die("Image has more than %d colors\n"
385 "Use ppmquant(1) to reduce the number of colors\n",
386 MAX_LINUX_LOGO_COLORS);
387 logo_clut[logo_clutsize++] = logo_data[i][j];
393 /* write file header */
396 for (i = 0; i < sizeof(data_name); i++){
397 write_hex(data_name[i]);
399 write_hex((unsigned char)(logo_width >> 8));
400 write_hex((unsigned char)logo_width);
401 write_hex((unsigned char)(logo_height >> 8));
402 write_hex((unsigned char)logo_height);
404 /* write logo data */
405 for (i = 0; i < logo_height; i++)
406 for (j = 0; j < logo_width; j++) {
407 for (k = 0; k < logo_clutsize; k++)
408 if (is_equal(logo_data[i][j], logo_clut[k]))
413 if (logo_height < MAX_LINUX_LOGO_HEIGHT || logo_width < MAX_LINUX_LOGO_WIDTH) {
414 if (logo_height == MAX_LINUX_LOGO_HEIGHT) {
415 for (i = 0; i < MAX_LINUX_LOGO_HEIGHT; i++)
416 for (j = logo_width; j < MAX_LINUX_LOGO_WIDTH; j++)
418 } else if (logo_width == MAX_LINUX_LOGO_WIDTH) {
419 for (i = logo_height; i < MAX_LINUX_LOGO_HEIGHT; i++)
420 for (j = 0; j < MAX_LINUX_LOGO_WIDTH; j++)
423 for (i = logo_height; i < MAX_LINUX_LOGO_HEIGHT; i++)
424 for (j = 0; j < MAX_LINUX_LOGO_WIDTH; j++)
427 for (i = 0; i < logo_height; i++)
428 for (j = logo_width; j < MAX_LINUX_LOGO_WIDTH; j++)
433 fputs("\n};\n\n", out);
435 /* write logo clut */
436 fprintf(out, "static unsigned char %s_clut[] __initdata = {\n",
441 for (i = 0; i < sizeof(clut_name); i++){
442 write_hex(clut_name[i]);
444 write_hex(logo_clutsize);
446 for (i = 0; i < logo_clutsize; i++) {
447 write_hex(logo_clut[i].red);
448 write_hex(logo_clut[i].green);
449 write_hex(logo_clut[i].blue);
452 /* write logo structure and file footer */
456 static void write_logo_gray256(void)
461 for (i = 0; i < logo_height; i++)
462 for (j = 0; j < logo_width; j++)
463 if (!is_gray(logo_data[i][j]))
464 die("Image must be grayscale\n");
466 /* write file header */
469 /* write logo data */
470 for (i = 0; i < logo_height; i++)
471 for (j = 0; j < logo_width; j++)
472 write_hex(logo_data[i][j].red);
474 /* write logo structure and file footer */
478 static void die(const char *fmt, ...)
483 vfprintf(stderr, fmt, ap);
489 static void usage(void)
492 "Usage: %s [options] <filename>\n"
495 " -h : display this usage information\n"
496 " -n <name> : specify logo name (default: linux_logo)\n"
497 " -o <output> : output to file <output> instead of stdout\n"
498 " -t <type> : specify logo type, one of\n"
499 " mono : monochrome black/white\n"
500 " vga16 : 16 colors VGA text palette\n"
501 " clut224 : 224 colors (default)\n"
502 " gray256 : 256 levels grayscale\n"
506 int main(int argc, char *argv[])
510 programname = argv[0];
514 opt = getopt(argc, argv, "hn:o:t:");
532 if (!strcmp(optarg, "mono"))
533 logo_type = LINUX_LOGO_MONO;
534 else if (!strcmp(optarg, "vga16"))
535 logo_type = LINUX_LOGO_VGA16;
536 else if (!strcmp(optarg, "clut224"))
537 logo_type = LINUX_LOGO_CLUT224;
538 else if (!strcmp(optarg, "gray256"))
539 logo_type = LINUX_LOGO_GRAY256;
549 if (optind != argc-1)
552 filename = argv[optind];
556 case LINUX_LOGO_MONO:
560 case LINUX_LOGO_VGA16:
564 case LINUX_LOGO_CLUT224:
565 write_logo_clut224();
568 case LINUX_LOGO_GRAY256:
569 write_logo_gray256();