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;
31 #define LINUX_LOGO_MONO 1 /* monochrome black/white */
32 #define LINUX_LOGO_VGA16 2 /* 16 colors VGA text palette */
33 #define LINUX_LOGO_CLUT224 3 /* 224 colors */
34 #define LINUX_LOGO_GRAY256 4 /* 256 levels grayscale */
36 static const char *logo_types[LINUX_LOGO_GRAY256+1] = {
37 [LINUX_LOGO_MONO] = "LINUX_LOGO_MONO",
38 [LINUX_LOGO_VGA16] = "LINUX_LOGO_VGA16",
39 [LINUX_LOGO_CLUT224] = "LINUX_LOGO_CLUT224",
40 [LINUX_LOGO_GRAY256] = "LINUX_LOGO_GRAY256"
43 #define MAX_LINUX_LOGO_COLORS 224
51 static const struct color clut_vga16[16] = {
71 static int logo_type = LINUX_LOGO_CLUT224;
72 static unsigned int logo_width;
73 static unsigned int logo_height;
74 static struct color **logo_data;
75 static struct color logo_clut[MAX_LINUX_LOGO_COLORS];
76 static unsigned int logo_clutsize;
77 static int is_plain_pbm = 0;
79 static void die(const char *fmt, ...)
80 __attribute__ ((noreturn)) __attribute ((format (printf, 1, 2)));
81 static void usage(void) __attribute ((noreturn));
84 static unsigned int get_number(FILE *fp)
88 /* Skip leading whitespace */
92 die("%s: end of file\n", filename);
94 /* Ignore comments 'till end of line */
98 die("%s: end of file\n", filename);
101 } while (isspace(c));
103 /* Parse decimal number */
107 /* some PBM are 'broken'; GiMP for example exports a PBM without space
108 * between the digits. This is Ok cause we know a PBM can only have a '1'
109 * or a '0' for the digit. */
114 die("%s: end of file\n", filename);
119 static unsigned int get_number255(FILE *fp, unsigned int maxval)
121 unsigned int val = get_number(fp);
122 return (255*val+maxval/2)/maxval;
125 static void read_image(void)
132 /* open image file */
133 fp = fopen(filename, "r");
135 die("Cannot open file %s: %s\n", filename, strerror(errno));
137 /* check file type and read file header */
140 die("%s is not a PNM file\n", filename);
146 /* Plain PBM/PGM/PPM */
152 /* Binary PBM/PGM/PPM */
153 die("%s: Binary PNM is not supported\n"
154 "Use pnmnoraw(1) to convert it to ASCII PNM\n", filename);
157 die("%s is not a PNM file\n", filename);
159 logo_width = get_number(fp);
160 logo_height = get_number(fp);
162 /* allocate image data */
163 logo_data = (struct color **)malloc(logo_height*sizeof(struct color *));
165 die("%s\n", strerror(errno));
166 for (i = 0; i < logo_height; i++) {
167 logo_data[i] = malloc(logo_width*sizeof(struct color));
169 die("%s\n", strerror(errno));
172 /* read image data */
177 for (i = 0; i < logo_height; i++)
178 for (j = 0; j < logo_width; j++)
179 logo_data[i][j].red = logo_data[i][j].green =
180 logo_data[i][j].blue = 255*(1-get_number(fp));
185 maxval = get_number(fp);
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 = get_number255(fp, maxval);
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 = get_number255(fp, maxval);
198 logo_data[i][j].green = get_number255(fp, maxval);
199 logo_data[i][j].blue = get_number255(fp, maxval);
208 static inline int is_black(struct color c)
210 return c.red == 0 && c.green == 0 && c.blue == 0;
213 static inline int is_white(struct color c)
215 return c.red == 255 && c.green == 255 && c.blue == 255;
218 static inline int is_gray(struct color c)
220 return c.red == c.green && c.red == c.blue;
223 static inline int is_equal(struct color c1, struct color c2)
225 return c1.red == c2.red && c1.green == c2.green && c1.blue == c2.blue;
228 static void write_header(void)
232 out = fopen(outputname, "w");
234 die("Cannot create file %s: %s\n", outputname, strerror(errno));
240 fputs(" * DO NOT EDIT THIS FILE!\n", out);
242 fprintf(out, " * It was automatically generated from %s\n", filename);
244 fprintf(out, " * Linux logo %s\n", logoname);
245 fputs(" */\n\n", out);
246 fputs("#include <linux/linux_logo.h>\n\n", out);
247 fprintf(out, "static unsigned char %s_data[] __initdata = {\n",
251 static void write_footer(void)
253 fputs("\n};\n\n", out);
254 fprintf(out, "const struct linux_logo %s __initconst = {\n", logoname);
255 fprintf(out, "\t.type\t\t= %s,\n", logo_types[logo_type]);
256 fprintf(out, "\t.width\t\t= %d,\n", logo_width);
257 fprintf(out, "\t.height\t\t= %d,\n", logo_height);
258 if (logo_type == LINUX_LOGO_CLUT224) {
259 fprintf(out, "\t.clutsize\t= %d,\n", logo_clutsize);
260 fprintf(out, "\t.clut\t\t= %s_clut,\n", logoname);
262 fprintf(out, "\t.data\t\t= %s_data\n", logoname);
263 fputs("};\n\n", out);
265 /* close logo file */
270 static int write_hex_cnt;
272 static void write_hex(unsigned char byte)
274 if (write_hex_cnt % 12)
275 fprintf(out, ", 0x%02x", byte);
276 else if (write_hex_cnt)
277 fprintf(out, ",\n\t0x%02x", byte);
279 fprintf(out, "\t0x%02x", byte);
283 static void write_logo_mono(void)
286 unsigned char val, bit;
289 for (i = 0; i < logo_height; i++)
290 for (j = 0; j < logo_width; j++)
291 if (!is_black(logo_data[i][j]) && !is_white(logo_data[i][j]))
292 die("Image must be monochrome\n");
294 /* write file header */
297 /* write logo data */
298 for (i = 0; i < logo_height; i++) {
299 for (j = 0; j < logo_width;) {
300 for (val = 0, bit = 0x80; bit && j < logo_width; j++, bit >>= 1)
301 if (logo_data[i][j].red)
307 /* write logo structure and file footer */
311 static void write_logo_vga16(void)
313 unsigned int i, j, k;
317 for (i = 0; i < logo_height; i++)
318 for (j = 0; j < logo_width; j++) {
319 for (k = 0; k < 16; k++)
320 if (is_equal(logo_data[i][j], clut_vga16[k]))
323 die("Image must use the 16 console colors only\n"
324 "Use ppmquant(1) -map clut_vga16.ppm to reduce the number "
328 /* write file header */
331 /* write logo data */
332 for (i = 0; i < logo_height; i++)
333 for (j = 0; j < logo_width; j++) {
334 for (k = 0; k < 16; k++)
335 if (is_equal(logo_data[i][j], clut_vga16[k]))
338 if (++j < logo_width) {
339 for (k = 0; k < 16; k++)
340 if (is_equal(logo_data[i][j], clut_vga16[k]))
347 /* write logo structure and file footer */
351 static void write_logo_clut224(void)
353 unsigned int i, j, k;
356 for (i = 0; i < logo_height; i++)
357 for (j = 0; j < logo_width; j++) {
358 for (k = 0; k < logo_clutsize; k++)
359 if (is_equal(logo_data[i][j], logo_clut[k]))
361 if (k == logo_clutsize) {
362 if (logo_clutsize == MAX_LINUX_LOGO_COLORS)
363 die("Image has more than %d colors\n"
364 "Use ppmquant(1) to reduce the number of colors\n",
365 MAX_LINUX_LOGO_COLORS);
366 logo_clut[logo_clutsize++] = logo_data[i][j];
370 /* write file header */
373 /* write logo data */
374 for (i = 0; i < logo_height; i++)
375 for (j = 0; j < logo_width; j++) {
376 for (k = 0; k < logo_clutsize; k++)
377 if (is_equal(logo_data[i][j], logo_clut[k]))
381 fputs("\n};\n\n", out);
383 /* write logo clut */
384 fprintf(out, "static unsigned char %s_clut[] __initdata = {\n",
387 for (i = 0; i < logo_clutsize; i++) {
388 write_hex(logo_clut[i].red);
389 write_hex(logo_clut[i].green);
390 write_hex(logo_clut[i].blue);
393 /* write logo structure and file footer */
397 static void write_logo_gray256(void)
402 for (i = 0; i < logo_height; i++)
403 for (j = 0; j < logo_width; j++)
404 if (!is_gray(logo_data[i][j]))
405 die("Image must be grayscale\n");
407 /* write file header */
410 /* write logo data */
411 for (i = 0; i < logo_height; i++)
412 for (j = 0; j < logo_width; j++)
413 write_hex(logo_data[i][j].red);
415 /* write logo structure and file footer */
419 static void die(const char *fmt, ...)
424 vfprintf(stderr, fmt, ap);
430 static void usage(void)
433 "Usage: %s [options] <filename>\n"
436 " -h : display this usage information\n"
437 " -n <name> : specify logo name (default: linux_logo)\n"
438 " -o <output> : output to file <output> instead of stdout\n"
439 " -t <type> : specify logo type, one of\n"
440 " mono : monochrome black/white\n"
441 " vga16 : 16 colors VGA text palette\n"
442 " clut224 : 224 colors (default)\n"
443 " gray256 : 256 levels grayscale\n"
447 int main(int argc, char *argv[])
451 programname = argv[0];
455 opt = getopt(argc, argv, "hn:o:t:");
473 if (!strcmp(optarg, "mono"))
474 logo_type = LINUX_LOGO_MONO;
475 else if (!strcmp(optarg, "vga16"))
476 logo_type = LINUX_LOGO_VGA16;
477 else if (!strcmp(optarg, "clut224"))
478 logo_type = LINUX_LOGO_CLUT224;
479 else if (!strcmp(optarg, "gray256"))
480 logo_type = LINUX_LOGO_GRAY256;
490 if (optind != argc-1)
493 filename = argv[optind];
497 case LINUX_LOGO_MONO:
501 case LINUX_LOGO_VGA16:
505 case LINUX_LOGO_CLUT224:
506 write_logo_clut224();
509 case LINUX_LOGO_GRAY256:
510 write_logo_gray256();