1 | // SPDX-License-Identifier: GPL-2.0-only |
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 | #include <ctype.h> |
10 | #include <errno.h> |
11 | #include <stdarg.h> |
12 | #include <stdio.h> |
13 | #include <stdlib.h> |
14 | #include <string.h> |
15 | #include <unistd.h> |
16 | |
17 | |
18 | static const char *programname; |
19 | static const char *filename; |
20 | static const char *logoname = "linux_logo" ; |
21 | static const char *outputname; |
22 | static FILE *out; |
23 | |
24 | |
25 | #define LINUX_LOGO_MONO 1 /* monochrome black/white */ |
26 | #define LINUX_LOGO_VGA16 2 /* 16 colors VGA text palette */ |
27 | #define LINUX_LOGO_CLUT224 3 /* 224 colors */ |
28 | #define LINUX_LOGO_GRAY256 4 /* 256 levels grayscale */ |
29 | |
30 | static const char *logo_types[LINUX_LOGO_GRAY256+1] = { |
31 | [LINUX_LOGO_MONO] = "LINUX_LOGO_MONO" , |
32 | [LINUX_LOGO_VGA16] = "LINUX_LOGO_VGA16" , |
33 | [LINUX_LOGO_CLUT224] = "LINUX_LOGO_CLUT224" , |
34 | [LINUX_LOGO_GRAY256] = "LINUX_LOGO_GRAY256" |
35 | }; |
36 | |
37 | #define MAX_LINUX_LOGO_COLORS 224 |
38 | |
39 | struct color { |
40 | unsigned char red; |
41 | unsigned char green; |
42 | unsigned char blue; |
43 | }; |
44 | |
45 | static const struct color clut_vga16[16] = { |
46 | { 0x00, 0x00, 0x00 }, |
47 | { 0x00, 0x00, 0xaa }, |
48 | { 0x00, 0xaa, 0x00 }, |
49 | { 0x00, 0xaa, 0xaa }, |
50 | { 0xaa, 0x00, 0x00 }, |
51 | { 0xaa, 0x00, 0xaa }, |
52 | { 0xaa, 0x55, 0x00 }, |
53 | { 0xaa, 0xaa, 0xaa }, |
54 | { 0x55, 0x55, 0x55 }, |
55 | { 0x55, 0x55, 0xff }, |
56 | { 0x55, 0xff, 0x55 }, |
57 | { 0x55, 0xff, 0xff }, |
58 | { 0xff, 0x55, 0x55 }, |
59 | { 0xff, 0x55, 0xff }, |
60 | { 0xff, 0xff, 0x55 }, |
61 | { 0xff, 0xff, 0xff }, |
62 | }; |
63 | |
64 | |
65 | static int logo_type = LINUX_LOGO_CLUT224; |
66 | static unsigned int logo_width; |
67 | static unsigned int logo_height; |
68 | static struct color **logo_data; |
69 | static struct color logo_clut[MAX_LINUX_LOGO_COLORS]; |
70 | static unsigned int logo_clutsize; |
71 | static int is_plain_pbm = 0; |
72 | |
73 | static void die(const char *fmt, ...) |
74 | __attribute__((noreturn)) __attribute((format (printf, 1, 2))); |
75 | static void usage(void) __attribute((noreturn)); |
76 | |
77 | |
78 | static unsigned int get_number(FILE *fp) |
79 | { |
80 | int c, val; |
81 | |
82 | /* Skip leading whitespace */ |
83 | do { |
84 | c = fgetc(stream: fp); |
85 | if (c == EOF) |
86 | die(fmt: "%s: end of file\n" , filename); |
87 | if (c == '#') { |
88 | /* Ignore comments 'till end of line */ |
89 | do { |
90 | c = fgetc(stream: fp); |
91 | if (c == EOF) |
92 | die(fmt: "%s: end of file\n" , filename); |
93 | } while (c != '\n'); |
94 | } |
95 | } while (isspace(c)); |
96 | |
97 | /* Parse decimal number */ |
98 | val = 0; |
99 | while (isdigit(c)) { |
100 | val = 10*val+c-'0'; |
101 | /* some PBM are 'broken'; GiMP for example exports a PBM without space |
102 | * between the digits. This is Ok cause we know a PBM can only have a '1' |
103 | * or a '0' for the digit. |
104 | */ |
105 | if (is_plain_pbm) |
106 | break; |
107 | c = fgetc(stream: fp); |
108 | if (c == EOF) |
109 | die(fmt: "%s: end of file\n" , filename); |
110 | } |
111 | return val; |
112 | } |
113 | |
114 | static unsigned int get_number255(FILE *fp, unsigned int maxval) |
115 | { |
116 | unsigned int val = get_number(fp); |
117 | |
118 | return (255*val+maxval/2)/maxval; |
119 | } |
120 | |
121 | static void read_image(void) |
122 | { |
123 | FILE *fp; |
124 | unsigned int i, j; |
125 | int magic; |
126 | unsigned int maxval; |
127 | |
128 | /* open image file */ |
129 | fp = fopen(filename: filename, modes: "r" ); |
130 | if (!fp) |
131 | die(fmt: "Cannot open file %s: %s\n" , filename, strerror(errno)); |
132 | |
133 | /* check file type and read file header */ |
134 | magic = fgetc(stream: fp); |
135 | if (magic != 'P') |
136 | die(fmt: "%s is not a PNM file\n" , filename); |
137 | magic = fgetc(stream: fp); |
138 | switch (magic) { |
139 | case '1': |
140 | case '2': |
141 | case '3': |
142 | /* Plain PBM/PGM/PPM */ |
143 | break; |
144 | |
145 | case '4': |
146 | case '5': |
147 | case '6': |
148 | /* Binary PBM/PGM/PPM */ |
149 | die(fmt: "%s: Binary PNM is not supported\n" |
150 | "Use pnmnoraw(1) to convert it to ASCII PNM\n" , filename); |
151 | |
152 | default: |
153 | die(fmt: "%s is not a PNM file\n" , filename); |
154 | } |
155 | logo_width = get_number(fp); |
156 | logo_height = get_number(fp); |
157 | |
158 | /* allocate image data */ |
159 | logo_data = (struct color **)malloc(size: logo_height*sizeof(struct color *)); |
160 | if (!logo_data) |
161 | die(fmt: "%s\n" , strerror(errno)); |
162 | for (i = 0; i < logo_height; i++) { |
163 | logo_data[i] = malloc(size: logo_width*sizeof(struct color)); |
164 | if (!logo_data[i]) |
165 | die(fmt: "%s\n" , strerror(errno)); |
166 | } |
167 | |
168 | /* read image data */ |
169 | switch (magic) { |
170 | case '1': |
171 | /* Plain PBM */ |
172 | is_plain_pbm = 1; |
173 | for (i = 0; i < logo_height; i++) |
174 | for (j = 0; j < logo_width; j++) |
175 | logo_data[i][j].red = logo_data[i][j].green = |
176 | logo_data[i][j].blue = 255*(1-get_number(fp)); |
177 | break; |
178 | |
179 | case '2': |
180 | /* Plain PGM */ |
181 | maxval = get_number(fp); |
182 | for (i = 0; i < logo_height; i++) |
183 | for (j = 0; j < logo_width; j++) |
184 | logo_data[i][j].red = logo_data[i][j].green = |
185 | logo_data[i][j].blue = get_number255(fp, maxval); |
186 | break; |
187 | |
188 | case '3': |
189 | /* Plain PPM */ |
190 | maxval = get_number(fp); |
191 | for (i = 0; i < logo_height; i++) |
192 | for (j = 0; j < logo_width; j++) { |
193 | logo_data[i][j].red = get_number255(fp, maxval); |
194 | logo_data[i][j].green = get_number255(fp, maxval); |
195 | logo_data[i][j].blue = get_number255(fp, maxval); |
196 | } |
197 | break; |
198 | } |
199 | |
200 | /* close file */ |
201 | fclose(stream: fp); |
202 | } |
203 | |
204 | static inline int is_black(struct color c) |
205 | { |
206 | return c.red == 0 && c.green == 0 && c.blue == 0; |
207 | } |
208 | |
209 | static inline int is_white(struct color c) |
210 | { |
211 | return c.red == 255 && c.green == 255 && c.blue == 255; |
212 | } |
213 | |
214 | static inline int is_gray(struct color c) |
215 | { |
216 | return c.red == c.green && c.red == c.blue; |
217 | } |
218 | |
219 | static inline int is_equal(struct color c1, struct color c2) |
220 | { |
221 | return c1.red == c2.red && c1.green == c2.green && c1.blue == c2.blue; |
222 | } |
223 | |
224 | static void (void) |
225 | { |
226 | /* open logo file */ |
227 | if (outputname) { |
228 | out = fopen(filename: outputname, modes: "w" ); |
229 | if (!out) |
230 | die(fmt: "Cannot create file %s: %s\n" , outputname, strerror(errno)); |
231 | } else { |
232 | out = stdout; |
233 | } |
234 | |
235 | fputs(s: "/*\n" , stream: out); |
236 | fputs(s: " * DO NOT EDIT THIS FILE!\n" , stream: out); |
237 | fputs(s: " *\n" , stream: out); |
238 | fprintf(stream: out, format: " * It was automatically generated from %s\n" , filename); |
239 | fputs(s: " *\n" , stream: out); |
240 | fprintf(stream: out, format: " * Linux logo %s\n" , logoname); |
241 | fputs(s: " */\n\n" , stream: out); |
242 | fputs(s: "#include <linux/linux_logo.h>\n\n" , stream: out); |
243 | fprintf(stream: out, format: "static unsigned char %s_data[] __initdata = {\n" , |
244 | logoname); |
245 | } |
246 | |
247 | static void (void) |
248 | { |
249 | fputs(s: "\n};\n\n" , stream: out); |
250 | fprintf(stream: out, format: "const struct linux_logo %s __initconst = {\n" , logoname); |
251 | fprintf(stream: out, format: "\t.type\t\t= %s,\n" , logo_types[logo_type]); |
252 | fprintf(stream: out, format: "\t.width\t\t= %u,\n" , logo_width); |
253 | fprintf(stream: out, format: "\t.height\t\t= %u,\n" , logo_height); |
254 | if (logo_type == LINUX_LOGO_CLUT224) { |
255 | fprintf(stream: out, format: "\t.clutsize\t= %u,\n" , logo_clutsize); |
256 | fprintf(stream: out, format: "\t.clut\t\t= %s_clut,\n" , logoname); |
257 | } |
258 | fprintf(stream: out, format: "\t.data\t\t= %s_data\n" , logoname); |
259 | fputs(s: "};\n\n" , stream: out); |
260 | |
261 | /* close logo file */ |
262 | if (outputname) |
263 | fclose(stream: out); |
264 | } |
265 | |
266 | static int write_hex_cnt; |
267 | |
268 | static void write_hex(unsigned char byte) |
269 | { |
270 | if (write_hex_cnt % 12) |
271 | fprintf(stream: out, format: ", 0x%02x" , byte); |
272 | else if (write_hex_cnt) |
273 | fprintf(stream: out, format: ",\n\t0x%02x" , byte); |
274 | else |
275 | fprintf(stream: out, format: "\t0x%02x" , byte); |
276 | write_hex_cnt++; |
277 | } |
278 | |
279 | static void write_logo_mono(void) |
280 | { |
281 | unsigned int i, j; |
282 | unsigned char val, bit; |
283 | |
284 | /* validate image */ |
285 | for (i = 0; i < logo_height; i++) |
286 | for (j = 0; j < logo_width; j++) |
287 | if (!is_black(c: logo_data[i][j]) && !is_white(c: logo_data[i][j])) |
288 | die(fmt: "Image must be monochrome\n" ); |
289 | |
290 | /* write file header */ |
291 | write_header(); |
292 | |
293 | /* write logo data */ |
294 | for (i = 0; i < logo_height; i++) { |
295 | for (j = 0; j < logo_width;) { |
296 | for (val = 0, bit = 0x80; bit && j < logo_width; j++, bit >>= 1) |
297 | if (logo_data[i][j].red) |
298 | val |= bit; |
299 | write_hex(byte: val); |
300 | } |
301 | } |
302 | |
303 | /* write logo structure and file footer */ |
304 | write_footer(); |
305 | } |
306 | |
307 | static void write_logo_vga16(void) |
308 | { |
309 | unsigned int i, j, k; |
310 | unsigned char val; |
311 | |
312 | /* validate image */ |
313 | for (i = 0; i < logo_height; i++) |
314 | for (j = 0; j < logo_width; j++) { |
315 | for (k = 0; k < 16; k++) |
316 | if (is_equal(c1: logo_data[i][j], c2: clut_vga16[k])) |
317 | break; |
318 | if (k == 16) |
319 | die(fmt: "Image must use the 16 console colors only\n" |
320 | "Use ppmquant(1) -map clut_vga16.ppm to reduce the number " |
321 | "of colors\n" ); |
322 | } |
323 | |
324 | /* write file header */ |
325 | write_header(); |
326 | |
327 | /* write logo data */ |
328 | for (i = 0; i < logo_height; i++) |
329 | for (j = 0; j < logo_width; j++) { |
330 | for (k = 0; k < 16; k++) |
331 | if (is_equal(c1: logo_data[i][j], c2: clut_vga16[k])) |
332 | break; |
333 | val = k<<4; |
334 | if (++j < logo_width) { |
335 | for (k = 0; k < 16; k++) |
336 | if (is_equal(c1: logo_data[i][j], c2: clut_vga16[k])) |
337 | break; |
338 | val |= k; |
339 | } |
340 | write_hex(byte: val); |
341 | } |
342 | |
343 | /* write logo structure and file footer */ |
344 | write_footer(); |
345 | } |
346 | |
347 | static void write_logo_clut224(void) |
348 | { |
349 | unsigned int i, j, k; |
350 | |
351 | /* validate image */ |
352 | for (i = 0; i < logo_height; i++) |
353 | for (j = 0; j < logo_width; j++) { |
354 | for (k = 0; k < logo_clutsize; k++) |
355 | if (is_equal(c1: logo_data[i][j], c2: logo_clut[k])) |
356 | break; |
357 | if (k == logo_clutsize) { |
358 | if (logo_clutsize == MAX_LINUX_LOGO_COLORS) |
359 | die(fmt: "Image has more than %d colors\n" |
360 | "Use ppmquant(1) to reduce the number of colors\n" , |
361 | MAX_LINUX_LOGO_COLORS); |
362 | logo_clut[logo_clutsize++] = logo_data[i][j]; |
363 | } |
364 | } |
365 | |
366 | /* write file header */ |
367 | write_header(); |
368 | |
369 | /* write logo data */ |
370 | for (i = 0; i < logo_height; i++) |
371 | for (j = 0; j < logo_width; j++) { |
372 | for (k = 0; k < logo_clutsize; k++) |
373 | if (is_equal(c1: logo_data[i][j], c2: logo_clut[k])) |
374 | break; |
375 | write_hex(byte: k+32); |
376 | } |
377 | fputs(s: "\n};\n\n" , stream: out); |
378 | |
379 | /* write logo clut */ |
380 | fprintf(stream: out, format: "static unsigned char %s_clut[] __initdata = {\n" , |
381 | logoname); |
382 | write_hex_cnt = 0; |
383 | for (i = 0; i < logo_clutsize; i++) { |
384 | write_hex(byte: logo_clut[i].red); |
385 | write_hex(byte: logo_clut[i].green); |
386 | write_hex(byte: logo_clut[i].blue); |
387 | } |
388 | |
389 | /* write logo structure and file footer */ |
390 | write_footer(); |
391 | } |
392 | |
393 | static void write_logo_gray256(void) |
394 | { |
395 | unsigned int i, j; |
396 | |
397 | /* validate image */ |
398 | for (i = 0; i < logo_height; i++) |
399 | for (j = 0; j < logo_width; j++) |
400 | if (!is_gray(c: logo_data[i][j])) |
401 | die(fmt: "Image must be grayscale\n" ); |
402 | |
403 | /* write file header */ |
404 | write_header(); |
405 | |
406 | /* write logo data */ |
407 | for (i = 0; i < logo_height; i++) |
408 | for (j = 0; j < logo_width; j++) |
409 | write_hex(byte: logo_data[i][j].red); |
410 | |
411 | /* write logo structure and file footer */ |
412 | write_footer(); |
413 | } |
414 | |
415 | static void die(const char *fmt, ...) |
416 | { |
417 | va_list ap; |
418 | |
419 | va_start(ap, fmt); |
420 | vfprintf(stderr, format: fmt, arg: ap); |
421 | va_end(ap); |
422 | |
423 | exit(status: 1); |
424 | } |
425 | |
426 | static void usage(void) |
427 | { |
428 | die(fmt: "\n" |
429 | "Usage: %s [options] <filename>\n" |
430 | "\n" |
431 | "Valid options:\n" |
432 | " -h : display this usage information\n" |
433 | " -n <name> : specify logo name (default: linux_logo)\n" |
434 | " -o <output> : output to file <output> instead of stdout\n" |
435 | " -t <type> : specify logo type, one of\n" |
436 | " mono : monochrome black/white\n" |
437 | " vga16 : 16 colors VGA text palette\n" |
438 | " clut224 : 224 colors (default)\n" |
439 | " gray256 : 256 levels grayscale\n" |
440 | "\n" , programname); |
441 | } |
442 | |
443 | int main(int argc, char *argv[]) |
444 | { |
445 | int opt; |
446 | |
447 | programname = argv[0]; |
448 | |
449 | opterr = 0; |
450 | while (1) { |
451 | opt = getopt(argc: argc, argv: argv, shortopts: "hn:o:t:" ); |
452 | if (opt == -1) |
453 | break; |
454 | |
455 | switch (opt) { |
456 | case 'h': |
457 | usage(); |
458 | break; |
459 | |
460 | case 'n': |
461 | logoname = optarg; |
462 | break; |
463 | |
464 | case 'o': |
465 | outputname = optarg; |
466 | break; |
467 | |
468 | case 't': |
469 | if (!strcmp(s1: optarg, s2: "mono" )) |
470 | logo_type = LINUX_LOGO_MONO; |
471 | else if (!strcmp(s1: optarg, s2: "vga16" )) |
472 | logo_type = LINUX_LOGO_VGA16; |
473 | else if (!strcmp(s1: optarg, s2: "clut224" )) |
474 | logo_type = LINUX_LOGO_CLUT224; |
475 | else if (!strcmp(s1: optarg, s2: "gray256" )) |
476 | logo_type = LINUX_LOGO_GRAY256; |
477 | else |
478 | usage(); |
479 | break; |
480 | |
481 | default: |
482 | usage(); |
483 | break; |
484 | } |
485 | } |
486 | if (optind != argc-1) |
487 | usage(); |
488 | |
489 | filename = argv[optind]; |
490 | |
491 | read_image(); |
492 | switch (logo_type) { |
493 | case LINUX_LOGO_MONO: |
494 | write_logo_mono(); |
495 | break; |
496 | |
497 | case LINUX_LOGO_VGA16: |
498 | write_logo_vga16(); |
499 | break; |
500 | |
501 | case LINUX_LOGO_CLUT224: |
502 | write_logo_clut224(); |
503 | break; |
504 | |
505 | case LINUX_LOGO_GRAY256: |
506 | write_logo_gray256(); |
507 | break; |
508 | } |
509 | exit(status: 0); |
510 | } |
511 | |