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.
18 #include <sys/types.h>
31 #include <sys/types.h>
34 static const char *programname;
35 static const char *filename;
36 static const char *logoname = "linux_logo";
37 static const char *outputname;
41 #define LINUX_LOGO_MONO 1 /* monochrome black/white */
42 #define LINUX_LOGO_VGA16 2 /* 16 colors VGA text palette */
43 #define LINUX_LOGO_CLUT224 3 /* 224 colors */
44 #define LINUX_LOGO_GRAY256 4 /* 256 levels grayscale */
45 #define LINUX_LOGO_bmp 5 /* truecolours*/
47 static const char *logo_types[LINUX_LOGO_bmp+1] = {
48 [LINUX_LOGO_MONO] = "LINUX_LOGO_MONO",
49 [LINUX_LOGO_VGA16] = "LINUX_LOGO_VGA16",
50 [LINUX_LOGO_CLUT224] = "LINUX_LOGO_CLUT224",
51 [LINUX_LOGO_GRAY256] = "LINUX_LOGO_GRAY256",
52 [LINUX_LOGO_bmp] = "LINUX_LOGO_bmp"
55 #define MAX_LINUX_LOGO_COLORS 224
63 static const struct color clut_vga16[16] = {
82 unsigned char data_name[] = {
91 unsigned char clut_name[] = {
101 static int logo_type = LINUX_LOGO_CLUT224;
102 static unsigned long logo_width;
103 static unsigned long logo_height;
104 static unsigned long data_long;
105 static unsigned long data_start;
106 static unsigned char *logo_data;
107 static struct color logo_clut[MAX_LINUX_LOGO_COLORS];
108 static unsigned int logo_clutsize;
110 static void die(const char *fmt, ...)
111 __attribute__ ((noreturn)) __attribute ((format (printf, 1, 2)));
112 static void usage(void) __attribute ((noreturn));
114 static unsigned int get_number(FILE *fp)
118 /* Skip leading whitespace */
122 die("%s: end of file\n", filename);
124 /* Ignore comments 'till end of line */
128 die("%s: end of file\n", filename);
131 } while (isspace(c));
133 /* Parse decimal number */
139 die("%s: end of file\n", filename);
144 static unsigned int get_number255(FILE *fp, unsigned int maxval)
146 unsigned int val = get_number(fp);
147 return (255*val+maxval/2)/maxval;
150 static void read_image(void)
162 /* open image file */
163 fp = open(filename, O_RDONLY);
165 die("Cannot open file isll.. %s: %s\n", filename, strerror(errno));
167 if (fstat(fp, &s) < 0) {
168 die("Cannot stat file isll.. %s: %s\n", filename, strerror(errno));
171 ret = fread(read_buf,1,0x26,fp);
173 die("read file %s: error read_buf=%ld\n", filename,ret);
175 logo_height = (read_buf[0x19]<<24) + (read_buf[0x18]<<16) +(read_buf[0x17]<<8) +(read_buf[0x16]);
176 logo_width = (read_buf[0x15]<<24) + (read_buf[0x14]<<16) +(read_buf[0x13]<<8) +(read_buf[0x12]);
177 data_start = (read_buf[0x0d]<<24) + (read_buf[0x0c]<<16) +(read_buf[0x0b]<<8) +(read_buf[0x0a]);
178 data_long = (read_buf[0x25]<<24) + (read_buf[0x24]<<16) +(read_buf[0x023]<<8) +(read_buf[0x22]);
180 /* allocate image data */
181 //logo_data = (char *)malloc(logo_height * logo_width * 3);
182 //data_long = logo_height * logo_width * 3;
185 die("%s..logo_height=%ld,logo_width=%ld,data_start=%ld,data_long=%ld,sizeof(struct color)=%d, \
186 read_buf[0x17]=%d read_buf[0x13]=%d\n\n",filename,logo_height,logo_width,data_start, \
187 data_long,sizeof(struct color),read_buf[0x17],read_buf[0x13]);
188 if ((logo_width*logo_height*3) != data_long)
189 die("something is wront in scripts/bmptologo.c\n");
193 fseek(fp,data_start,SEEK_SET);
194 ret = fread(logo_data,1,data_long,fp);
195 if (ret != data_long)
196 die("read file %s: error logo_data=%ld\n", filename,ret);
198 data = mmap(0, s.st_size, PROT_READ, MAP_SHARED, fp, 0);
199 if (data == MAP_FAILED)
200 die("read file %s: error logo_data\n", filename);
201 logo_data = data + 54;
202 logo_height = (data[0x19]<<24) + (data[0x18]<<16) +(data[0x17]<<8) +(data[0x16]);
203 logo_width = (data[0x15]<<24) + (data[0x14]<<16) +(data[0x13]<<8) +(data[0x12]);
204 data_start = (data[0x0d]<<24) + (data[0x0c]<<16) +(data[0x0b]<<8) +(data[0x0a]);
205 data_long = (data[0x25]<<24) + (data[0x24]<<16) +(data[0x023]<<8) +(data[0x22]);
206 data_long = logo_height * logo_width * 3;
208 die("%s..logo_height=%ld,logo_width=%ld,data_start=%ld,data_long=%ld,sizeof(struct color)=%d, \
209 read_buf[0x17]=%d read_buf[0x13]=%d\n\n",filename,logo_height,logo_width,data_start, \
210 data_long,sizeof(struct color),read_buf[0x17],read_buf[0x13]);
211 if ((logo_width*logo_height*3) != data_long)
212 die("something is wront in scripts/bmptologo.c\n");
216 die("logo_data is:%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x:over\n", \
217 logo_data[0],logo_data[1],logo_data[2],logo_data[3],logo_data[4],logo_data[5],logo_data[6],logo_data[7],logo_data[8], \
218 logo_data[9],logo_data[10],logo_data[11]);
225 static inline int is_black(struct color c)
227 return c.red == 0 && c.green == 0 && c.blue == 0;
230 static inline int is_white(struct color c)
232 return c.red == 255 && c.green == 255 && c.blue == 255;
235 static inline int is_gray(struct color c)
237 return c.red == c.green && c.red == c.blue;
240 static inline int is_equal(struct color c1, struct color c2)
242 return c1.red == c2.red && c1.green == c2.green && c1.blue == c2.blue;
245 static int write_hex_cnt;
247 static void write_hex(unsigned char byte)
249 if (write_hex_cnt % 12)
250 fprintf(out, ", 0x%02x", byte);
251 else if (write_hex_cnt)
252 fprintf(out, ",\n\t0x%02x", byte);
254 fprintf(out, "\t0x%02x", byte);
258 static void write_header(void)
262 out = fopen(outputname, "w");
264 die("Cannot create file %s: %s\n", outputname, strerror(errno));
270 fputs(" * DO NOT EDIT THIS FILE!\n", out);
272 fprintf(out, " * It was automatically generated from %s\n", filename);
274 fprintf(out, " * Linux logo %s\n", logoname);
275 fputs(" */\n\n", out);
276 fputs("#include <linux/linux_logo.h>\n\n", out);
277 fprintf(out, "static unsigned char %s_data[] __initdata = {\n",
281 static void write_footer(void)
283 fputs("\n};\n\n", out);
284 fprintf(out, "const struct linux_logo %s __initconst = {\n", logoname);
285 fprintf(out, "\t.type\t\t= %s,\n", logo_types[logo_type]);
287 if (logo_type == LINUX_LOGO_bmp) {
288 fprintf(out, "\t.width\t\t= %ld,\n", logo_width);
289 fprintf(out, "\t.height\t\t= %ld,\n", logo_height);
290 //fprintf(out, "\t.data\t\t= %s_data,\n", logoname);
291 fprintf(out, "\t.data\t\t= &(%s_data[%ld]),\n", logoname,sizeof(data_name) + 8);
292 fprintf(out, "\t.clut\t\t= %s_clut\n", logoname);
295 fputs("};\n\n", out);
297 /* close logo file */
303 static void write_logo_bmp(void)
305 unsigned long i=0, j=0;
306 unsigned char *position ;
309 /*statistics how many colours ,and if have over 224
311 for (i = 0; i < logo_height; i++)
312 for (j = 0; j < logo_width; j++) {
313 for (k = 0; k < logo_clutsize; k++)
314 if (is_equal(logo_data[i][j], logo_clut[k]))
316 if (k == logo_clutsize) {
317 if (logo_clutsize == MAX_LINUX_LOGO_COLORS)
318 die("Image has more than %d colors\n"
319 "Use ppmquant(1) to reduce the number of colors\n",
320 MAX_LINUX_LOGO_COLORS);
321 logo_clut[logo_clutsize++] = logo_data[i][j];
329 /* write file header */
332 write_hex((unsigned char)(logo_width >> 8));
333 write_hex((unsigned char)logo_width);
334 write_hex((unsigned char)(logo_height >> 8));
335 write_hex((unsigned char)logo_height);
337 for (i = 0; i < sizeof(data_name); i++){
338 write_hex(data_name[i]);
340 write_hex((unsigned char)(logo_width >> 8));
341 write_hex((unsigned char)logo_width);
342 write_hex((unsigned char)(logo_height >> 8));
343 write_hex((unsigned char)logo_height);
347 /* write logo data */
348 for (i = 0; i < logo_height; i++)
349 for (j = 0; j < logo_width; j++) {
350 for (k = 0; k < logo_clutsize; k++)
351 if (is_equal(logo_data[i][j], logo_clut[k]))
355 fputs("\n};\n\n", out);
359 /* write logo clut */
360 fprintf(out, "static unsigned char %s_clut[] __initdata = {\n",
365 for (i = 0; i < sizeof(clut_name); i++){
366 write_hex(clut_name[i]);
368 write_hex(logo_clutsize);
370 for (i = 0; i < logo_clutsize; i++) {
371 write_hex(logo_clut[i].red);
372 write_hex(logo_clut[i].green);
373 write_hex(logo_clut[i].blue);
376 for (i = logo_clutsize; i < (MAX_LINUX_LOGO_COLORS * 3); i++)
381 /* write logo structure and file footer */
385 for (i=logo_height; i>0; i--)
387 for (j=0; j<logo_width; j++)
389 position = logo_data + (i-1)* logo_width * 3 + 3 * j;
391 write_hex(*(position));
392 write_hex(*(position+1));
393 write_hex(*(position+2));
395 write_hex(*(position));
396 write_hex(*(position+1));
397 write_hex(*(position+2));
404 fputs("\n};\n\n", out);
405 /* write logo clut */
406 fprintf(out, "static unsigned char %s_clut[] __initdata = {\n",
410 for (i = 0; i < sizeof(clut_name); i++){
411 write_hex(clut_name[i]);
417 static void die(const char *fmt, ...)
422 vfprintf(stderr, fmt, ap);
428 static void usage(void)
431 "Usage: %s [options] <filename>\n"
434 " -h : display this usage information\n"
435 " -n <name> : specify logo name (default: linux_logo)\n"
436 " -o <output> : output to file <output> instead of stdout\n"
437 " -t <type> : specify logo type, one of\n"
438 " bmp : truecolour\n"
442 int main(int argc, char *argv[])
446 programname = argv[0];
450 opt = getopt(argc, argv, "hn:o:t:");
468 if (!strcmp(optarg, "bmp"))
469 logo_type = LINUX_LOGO_bmp;
471 die("logo_type is wrong without bmp\n");
479 if (optind != argc-1)
482 filename = argv[optind];
490 die("logo_type is wrong\n");