Merge remote-tracking branch 'stable/linux-3.0.y' into develop-3.0-jb
[firefly-linux-kernel-4.4.55.git] / scripts / pnmtologo.c
1
2 /*
3  *  Convert a logo in ASCII PNM format to C source suitable for inclusion in
4  *  the Linux kernel
5  *
6  *  (C) Copyright 2001-2003 by Geert Uytterhoeven <geert@linux-m68k.org>
7  *
8  *  --------------------------------------------------------------------------
9  *
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.
13  */
14
15 #include <ctype.h>
16 #include <errno.h>
17 #include <stdarg.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22
23
24 static const char *programname;
25 static const char *filename;
26 static const char *logoname = "linux_logo";
27 static const char *outputname;
28 static FILE *out;
29
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 */
34
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"
40 };
41
42 #define MAX_LINUX_LOGO_COLORS   224
43
44 struct color {
45     unsigned char red;
46     unsigned char green;
47     unsigned char blue;
48 };
49
50 static const struct color clut_vga16[16] = {
51     { 0x00, 0x00, 0x00 },
52     { 0x00, 0x00, 0xaa },
53     { 0x00, 0xaa, 0x00 },
54     { 0x00, 0xaa, 0xaa },
55     { 0xaa, 0x00, 0x00 },
56     { 0xaa, 0x00, 0xaa },
57     { 0xaa, 0x55, 0x00 },
58     { 0xaa, 0xaa, 0xaa },
59     { 0x55, 0x55, 0x55 },
60     { 0x55, 0x55, 0xff },
61     { 0x55, 0xff, 0x55 },
62     { 0x55, 0xff, 0xff },
63     { 0xff, 0x55, 0x55 },
64     { 0xff, 0x55, 0xff },
65     { 0xff, 0xff, 0x55 },
66     { 0xff, 0xff, 0xff },
67 };
68
69 unsigned char data_name[] = {
70         0x6C, 0x6F, 0x67,
71         0x6F, 0x5F, 0x52,
72         0x4B, 0x6C, 0x6F,
73         0x67, 0x6F, 0x5F,
74         0x64, 0x61, 0x74,
75         0x61
76 };
77
78 unsigned char clut_name[] = {
79         0x6C, 0x6F, 0x67,
80         0x6F, 0x5F, 0x52,
81         0x4B, 0x6C, 0x6F,
82         0x67, 0x6F, 0x5F,
83         0x63, 0x6C, 0x75,
84         0x74
85 };
86
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;
93
94 static void die(const char *fmt, ...)
95     __attribute__ ((noreturn)) __attribute ((format (printf, 1, 2)));
96 static void usage(void) __attribute ((noreturn));
97
98 static unsigned int get_number(FILE *fp)
99 {
100     int c, val;
101
102     /* Skip leading whitespace */
103     do {
104         c = fgetc(fp);
105         if (c == EOF)
106             die("%s: end of file\n", filename);
107         if (c == '#') {
108             /* Ignore comments 'till end of line */
109             do {
110                 c = fgetc(fp);
111                 if (c == EOF)
112                     die("%s: end of file\n", filename);
113             } while (c != '\n');
114         }
115     } while (isspace(c));
116
117     /* Parse decimal number */
118     val = 0;
119     while (isdigit(c)) {
120         val = 10*val+c-'0';
121         c = fgetc(fp);
122         if (c == EOF)
123             die("%s: end of file\n", filename);
124     }
125     return val;
126 }
127
128 static unsigned int get_number255(FILE *fp, unsigned int maxval)
129 {
130     unsigned int val = get_number(fp);
131     return (255*val+maxval/2)/maxval;
132 }
133
134 static void read_image(void)
135 {
136         FILE *fp;
137         unsigned int i, j;
138         int magic;
139         unsigned int maxval;
140
141         /* open image file */
142         fp = fopen(filename, "r");
143         if (!fp)
144                 die("Cannot open file %s: %s\n", filename, strerror(errno));
145
146     /* check file type and read file header */
147     magic = fgetc(fp);
148     if (magic != 'P')
149         die("%s is not a PNM file\n", filename);
150     magic = fgetc(fp);
151     switch (magic) {
152         case '1':
153         case '2':
154         case '3':
155             /* Plain PBM/PGM/PPM */
156             break;
157
158         case '4':
159         case '5':
160         case '6':
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);
164
165         default:
166             die("%s is not a PNM file\n", filename);
167     }
168         logo_width = get_number(fp);
169         logo_height = get_number(fp);
170
171
172         /* allocate image data */
173         logo_data = (struct color **)malloc(logo_height * sizeof(struct color *));
174         if (!logo_data)
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));
178                 if (!logo_data[i])
179                 die("%s\n", strerror(errno));
180         }
181
182     /* read image data */
183     switch (magic) {
184         case '1':
185             /* Plain PBM */
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));
190             break;
191
192         case '2':
193             /* Plain PGM */
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);
199             break;
200
201         case '3':
202             /* Plain PPM */
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);
209                 }
210             break;
211     }
212
213     /* close file */
214     fclose(fp);
215 }
216
217 static inline int is_black(struct color c)
218 {
219     return c.red == 0 && c.green == 0 && c.blue == 0;
220 }
221
222 static inline int is_white(struct color c)
223 {
224     return c.red == 255 && c.green == 255 && c.blue == 255;
225 }
226
227 static inline int is_gray(struct color c)
228 {
229     return c.red == c.green && c.red == c.blue;
230 }
231
232 static inline int is_equal(struct color c1, struct color c2)
233 {
234     return c1.red == c2.red && c1.green == c2.green && c1.blue == c2.blue;
235 }
236
237 static int write_hex_cnt;
238
239 static void write_hex(unsigned char byte)
240 {
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);
245     else
246         fprintf(out, "\t0x%02x", byte);
247     write_hex_cnt++;
248 }
249
250 static void write_header(void)
251 {
252         /* open logo file */
253         if (outputname) {
254                 out = fopen(outputname, "w");
255                 if (!out)
256                         die("Cannot create file %s: %s\n", outputname, strerror(errno));
257         } else {
258                 out = stdout;
259         }
260
261         fputs("/*\n", out);
262         fputs(" *  DO NOT EDIT THIS FILE!\n", out);
263         fputs(" *\n", out);
264         fprintf(out, " *  It was automatically generated from %s\n", filename);
265         fputs(" *\n", out);
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",
270                 logoname);
271 }
272
273 static void write_footer(void)
274 {
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]);
278
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);
282         } else {
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);
288                 }
289                 fprintf(out, "\t.data\t\t= %s_data\n", logoname);
290         }
291
292         fputs("};\n\n", out);
293
294         /* close logo file */
295         if (outputname)
296                 fclose(out);
297 }
298
299 static void write_logo_mono(void)
300 {
301     unsigned int i, j;
302     unsigned char val, bit;
303
304     /* validate image */
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");
309
310     /* write file header */
311     write_header();
312
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)
318                     val |= bit;
319             write_hex(val);
320         }
321     }
322
323
324     /* write logo structure and file footer */
325     write_footer();
326 }
327
328 static void write_logo_vga16(void)
329 {
330     unsigned int i, j, k;
331     unsigned char val;
332
333     /* validate image */
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]))
338                     break;
339             if (k == 16)
340                 die("Image must use the 16 console colors only\n"
341                     "Use ppmquant(1) -map clut_vga16.ppm to reduce the number "
342                     "of colors\n");
343         }
344
345     /* write file header */
346     write_header();
347
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]))
353                     break;
354             val = k<<4;
355             if (++j < logo_width) {
356                 for (k = 0; k < 16; k++)
357                     if (is_equal(logo_data[i][j], clut_vga16[k]))
358                         break;
359                 val |= k;
360             }
361             write_hex(val);
362         }
363
364     /* write logo structure and file footer */
365     write_footer();
366 }
367
368 static void write_logo_clut224(void)
369 {
370         unsigned int i, j, k;
371
372         logo_clutsize = 0;
373
374         /* validate image */
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]))
379                                         break;
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];
386                         }
387                 }
388
389         write_hex_cnt = 0;
390
391         /* write file header */
392         write_header();
393
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);
398
399         for (i = 0; i < sizeof(data_name); i++){
400                 write_hex(data_name[i]);
401         }
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);
406
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]))
412                                         break;
413                         write_hex(k+32);
414                 }
415
416         fputs("\n};\n\n", out);
417
418         /* write logo clut */
419         fprintf(out, "static unsigned char %s_clut[] __initdata = {\n",
420                 logoname);
421
422         write_hex_cnt = 0;
423
424         for (i = 0; i < sizeof(clut_name); i++){
425                 write_hex(clut_name[i]);
426         }
427         write_hex(logo_clutsize);
428
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);
433         }
434
435         for (i = logo_clutsize; i < (MAX_LINUX_LOGO_COLORS * 3); i++)
436         {
437                 write_hex(32);
438         }
439
440         /* write logo structure and file footer */
441         write_footer();
442 }
443
444 static void write_logo_gray256(void)
445 {
446     unsigned int i, j;
447
448     /* validate image */
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");
453
454     /* write file header */
455     write_header();
456
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);
461
462     /* write logo structure and file footer */
463     write_footer();
464 }
465
466 static void die(const char *fmt, ...)
467 {
468     va_list ap;
469
470     va_start(ap, fmt);
471     vfprintf(stderr, fmt, ap);
472     va_end(ap);
473
474     exit(1);
475 }
476
477 static void usage(void)
478 {
479     die("\n"
480         "Usage: %s [options] <filename>\n"
481         "\n"
482         "Valid options:\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"
491         "\n", programname);
492 }
493
494 int main(int argc, char *argv[])
495 {
496     int opt;
497
498     programname = argv[0];
499
500     opterr = 0;
501     while (1) {
502         opt = getopt(argc, argv, "hn:o:t:");
503         if (opt == -1)
504             break;
505
506         switch (opt) {
507             case 'h':
508                 usage();
509                 break;
510
511             case 'n':
512                 logoname = optarg;
513                 break;
514
515             case 'o':
516                 outputname = optarg;
517                 break;
518
519             case '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;
528                 else
529                     usage();
530                 break;
531
532             default:
533                 usage();
534                 break;
535         }
536     }
537     if (optind != argc-1)
538         usage();
539
540     filename = argv[optind];
541
542     read_image();
543     switch (logo_type) {
544         case LINUX_LOGO_MONO:
545             write_logo_mono();
546             break;
547
548         case LINUX_LOGO_VGA16:
549             write_logo_vga16();
550             break;
551
552         case LINUX_LOGO_CLUT224:
553             write_logo_clut224();
554             break;
555
556         case LINUX_LOGO_GRAY256:
557             write_logo_gray256();
558             break;
559     }
560     exit(0);
561 }
562