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;
108 static void die(const char *fmt, ...)
109 __attribute__ ((noreturn)) __attribute ((format (printf, 1, 2)));
110 static void usage(void) __attribute ((noreturn));
112 static void read_image(void)
118 /* open image file */
119 fd = open(filename, O_RDONLY);
121 die("Cannot open file isll.. %s: %s\n", filename, strerror(errno));
123 if (fstat(fd, &s) < 0)
124 die("Cannot stat file isll.. %s: %s\n", filename, strerror(errno));
127 ret = fread(read_buf,1,0x26,fp);
129 die("read file %s: error read_buf=%ld\n", filename,ret);
131 logo_height = (read_buf[0x19]<<24) + (read_buf[0x18]<<16) +(read_buf[0x17]<<8) +(read_buf[0x16]);
132 logo_width = (read_buf[0x15]<<24) + (read_buf[0x14]<<16) +(read_buf[0x13]<<8) +(read_buf[0x12]);
133 data_start = (read_buf[0x0d]<<24) + (read_buf[0x0c]<<16) +(read_buf[0x0b]<<8) +(read_buf[0x0a]);
134 data_long = (read_buf[0x25]<<24) + (read_buf[0x24]<<16) +(read_buf[0x023]<<8) +(read_buf[0x22]);
136 /* allocate image data */
137 //logo_data = (char *)malloc(logo_height * logo_width * 3);
138 //data_long = logo_height * logo_width * 3;
141 die("%s..logo_height=%ld,logo_width=%ld,data_start=%ld,data_long=%ld,sizeof(struct color)=%d, \
142 read_buf[0x17]=%d read_buf[0x13]=%d\n\n",filename,logo_height,logo_width,data_start, \
143 data_long,sizeof(struct color),read_buf[0x17],read_buf[0x13]);
144 if ((logo_width*logo_height*3) != data_long)
145 die("something is wront in scripts/bmptologo.c\n");
149 fseek(fp,data_start,SEEK_SET);
150 ret = fread(logo_data,1,data_long,fp);
151 if (ret != data_long)
152 die("read file %s: error logo_data=%ld\n", filename,ret);
154 data = mmap(0, s.st_size, PROT_READ, MAP_SHARED, fd, 0);
155 if (data == MAP_FAILED)
156 die("read file %s: error logo_data\n", filename);
157 logo_data = data + 54;
158 logo_height = (data[0x19]<<24) + (data[0x18]<<16) +(data[0x17]<<8) +(data[0x16]);
159 logo_width = (data[0x15]<<24) + (data[0x14]<<16) +(data[0x13]<<8) +(data[0x12]);
160 data_start = (data[0x0d]<<24) + (data[0x0c]<<16) +(data[0x0b]<<8) +(data[0x0a]);
161 data_long = (data[0x25]<<24) + (data[0x24]<<16) +(data[0x023]<<8) +(data[0x22]);
162 data_long = logo_height * logo_width * 3;
164 die("%s..logo_height=%ld,logo_width=%ld,data_start=%ld,data_long=%ld,sizeof(struct color)=%d, \
165 read_buf[0x17]=%d read_buf[0x13]=%d\n\n",filename,logo_height,logo_width,data_start, \
166 data_long,sizeof(struct color),read_buf[0x17],read_buf[0x13]);
167 if ((logo_width*logo_height*3) != data_long)
168 die("something is wront in scripts/bmptologo.c\n");
172 die("logo_data is:%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x:over\n", \
173 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], \
174 logo_data[9],logo_data[10],logo_data[11]);
181 static inline int is_black(struct color c)
183 return c.red == 0 && c.green == 0 && c.blue == 0;
186 static inline int is_white(struct color c)
188 return c.red == 255 && c.green == 255 && c.blue == 255;
191 static inline int is_gray(struct color c)
193 return c.red == c.green && c.red == c.blue;
196 static inline int is_equal(struct color c1, struct color c2)
198 return c1.red == c2.red && c1.green == c2.green && c1.blue == c2.blue;
201 static int write_hex_cnt;
203 static void write_hex(unsigned char byte)
205 if (write_hex_cnt % 12)
206 fprintf(out, ", 0x%02x", byte);
207 else if (write_hex_cnt)
208 fprintf(out, ",\n\t0x%02x", byte);
210 fprintf(out, "\t0x%02x", byte);
214 static void write_header(void)
218 out = fopen(outputname, "w");
220 die("Cannot create file %s: %s\n", outputname, strerror(errno));
226 fputs(" * DO NOT EDIT THIS FILE!\n", out);
228 fprintf(out, " * It was automatically generated from %s\n", filename);
230 fprintf(out, " * Linux logo %s\n", logoname);
231 fputs(" */\n\n", out);
232 fputs("#include <linux/linux_logo.h>\n\n", out);
233 fprintf(out, "static unsigned char %s_data[] __initdata = {\n",
237 static void write_footer(void)
239 fputs("\n};\n\n", out);
240 fprintf(out, "const struct linux_logo %s __initconst = {\n", logoname);
241 fprintf(out, "\t.type\t\t= %s,\n", logo_types[logo_type]);
243 if (logo_type == LINUX_LOGO_bmp) {
244 fprintf(out, "\t.width\t\t= %ld,\n", logo_width);
245 fprintf(out, "\t.height\t\t= %ld,\n", logo_height);
246 //fprintf(out, "\t.data\t\t= %s_data,\n", logoname);
247 fprintf(out, "\t.data\t\t= &(%s_data[%ld]),\n", logoname,sizeof(data_name) + 8);
248 fprintf(out, "\t.clut\t\t= %s_clut\n", logoname);
251 fputs("};\n\n", out);
253 /* close logo file */
259 static void write_logo_bmp(void)
261 unsigned long i=0, j=0;
262 unsigned char *position ;
265 /*statistics how many colours ,and if have over 224
267 for (i = 0; i < logo_height; i++)
268 for (j = 0; j < logo_width; j++) {
269 for (k = 0; k < logo_clutsize; k++)
270 if (is_equal(logo_data[i][j], logo_clut[k]))
272 if (k == logo_clutsize) {
273 if (logo_clutsize == MAX_LINUX_LOGO_COLORS)
274 die("Image has more than %d colors\n"
275 "Use ppmquant(1) to reduce the number of colors\n",
276 MAX_LINUX_LOGO_COLORS);
277 logo_clut[logo_clutsize++] = logo_data[i][j];
285 /* write file header */
288 write_hex((unsigned char)(logo_width >> 8));
289 write_hex((unsigned char)logo_width);
290 write_hex((unsigned char)(logo_height >> 8));
291 write_hex((unsigned char)logo_height);
293 for (i = 0; i < sizeof(data_name); i++){
294 write_hex(data_name[i]);
296 write_hex((unsigned char)(logo_width >> 8));
297 write_hex((unsigned char)logo_width);
298 write_hex((unsigned char)(logo_height >> 8));
299 write_hex((unsigned char)logo_height);
303 /* write logo data */
304 for (i = 0; i < logo_height; i++)
305 for (j = 0; j < logo_width; j++) {
306 for (k = 0; k < logo_clutsize; k++)
307 if (is_equal(logo_data[i][j], logo_clut[k]))
311 fputs("\n};\n\n", out);
315 /* write logo clut */
316 fprintf(out, "static unsigned char %s_clut[] __initdata = {\n",
321 for (i = 0; i < sizeof(clut_name); i++){
322 write_hex(clut_name[i]);
324 write_hex(logo_clutsize);
326 for (i = 0; i < logo_clutsize; i++) {
327 write_hex(logo_clut[i].red);
328 write_hex(logo_clut[i].green);
329 write_hex(logo_clut[i].blue);
332 for (i = logo_clutsize; i < (MAX_LINUX_LOGO_COLORS * 3); i++)
337 /* write logo structure and file footer */
341 for (i=logo_height; i>0; i--)
343 for (j=0; j<logo_width; j++)
345 position = logo_data + (i-1)* logo_width * 3 + 3 * j;
347 write_hex(*(position));
348 write_hex(*(position+1));
349 write_hex(*(position+2));
351 write_hex(*(position));
352 write_hex(*(position+1));
353 write_hex(*(position+2));
360 fputs("\n};\n\n", out);
361 /* write logo clut */
362 fprintf(out, "static unsigned char %s_clut[] __initdata = {\n",
366 for (i = 0; i < sizeof(clut_name); i++){
367 write_hex(clut_name[i]);
373 static void die(const char *fmt, ...)
378 vfprintf(stderr, fmt, ap);
384 static void usage(void)
387 "Usage: %s [options] <filename>\n"
390 " -h : display this usage information\n"
391 " -n <name> : specify logo name (default: linux_logo)\n"
392 " -o <output> : output to file <output> instead of stdout\n"
393 " -t <type> : specify logo type, one of\n"
394 " bmp : truecolour\n"
398 int main(int argc, char *argv[])
402 programname = argv[0];
406 opt = getopt(argc, argv, "hn:o:t:");
424 if (!strcmp(optarg, "bmp"))
425 logo_type = LINUX_LOGO_bmp;
427 die("logo_type is wrong without bmp\n");
435 if (optind != argc-1)
438 filename = argv[optind];
446 die("logo_type is wrong\n");