arm64: dts: rk3368: add isp config
[firefly-linux-kernel-4.4.55.git] / scripts / bmptologo.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 <sys/stat.h>
18 #include <sys/types.h>
19 #include <stdarg.h>
20 #include <stdio.h>
21 #include <sys/mman.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <sys/mman.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32
33
34 static const char *programname;
35 static const char *filename;
36 static const char *logoname = "linux_logo";
37 static const char *outputname;
38 static FILE *out;
39
40 //#define debug 0
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*/
46
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"
53 };
54
55 #define MAX_LINUX_LOGO_COLORS   224
56
57 struct color {
58     char blue;
59     char green;
60     char red;
61 };
62
63 static const struct color clut_vga16[16] = {
64     { 0x00, 0x00, 0x00 },
65     { 0x00, 0x00, 0xaa },
66     { 0x00, 0xaa, 0x00 },
67     { 0x00, 0xaa, 0xaa },
68     { 0xaa, 0x00, 0x00 },
69     { 0xaa, 0x00, 0xaa },
70     { 0xaa, 0x55, 0x00 },
71     { 0xaa, 0xaa, 0xaa },
72     { 0x55, 0x55, 0x55 },
73     { 0x55, 0x55, 0xff },
74     { 0x55, 0xff, 0x55 },
75     { 0x55, 0xff, 0xff },
76     { 0xff, 0x55, 0x55 },
77     { 0xff, 0x55, 0xff },
78     { 0xff, 0xff, 0x55 },
79     { 0xff, 0xff, 0xff },
80 };
81
82 unsigned char data_name[] = {
83         0x6C, 0x6F, 0x67,
84         0x6F, 0x5F, 0x52,
85         0x4B, 0x6C, 0x6F,
86         0x67, 0x6F, 0x5F,
87         0x64, 0x61, 0x74,
88         0x61
89 };
90
91 unsigned char clut_name[] = {
92         0x62, 0x6D, 0x70,
93         0x6C, 0x6F, 0x67,
94         0x6F, 0x5F, 0x52,
95         0x4B, 0x6C, 0x6F,
96         0x67, 0x6F, 0x5F,
97         0x63, 0x6C, 0x75,
98         0x74, 0x00
99 };
100
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
108 static void die(const char *fmt, ...)
109     __attribute__ ((noreturn)) __attribute ((format (printf, 1, 2)));
110 static void usage(void) __attribute ((noreturn));
111
112 static void read_image(void)
113 {
114         int fd;
115         struct stat s;
116         unsigned char *data;
117         
118         /* open image file */
119         fd = open(filename, O_RDONLY);
120         if (fd < 0)
121                 die("Cannot open file isll.. %s: %s\n", filename, strerror(errno));
122
123         if (fstat(fd, &s) < 0)
124                 die("Cannot stat file isll.. %s: %s\n", filename, strerror(errno));
125
126 #if 0
127         ret = fread(read_buf,1,0x26,fp);
128         if (ret != 0x26)
129                 die("read file %s: error read_buf=%ld\n", filename,ret);
130
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]);
135 #endif  
136         /* allocate image data */
137         //logo_data = (char *)malloc(logo_height * logo_width * 3);
138         //data_long = logo_height * logo_width * 3;
139 //#ifdef debug
140 #if 0
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");
146
147 #endif
148 #if 0
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);
153 #else
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;
163 #if 0
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");
169 #endif  
170 #endif
171 #ifdef  debug
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]);
175 #endif
176     /* close file */
177     close(fd);
178 }
179
180
181 static inline int is_black(struct color c)
182 {
183     return c.red == 0 && c.green == 0 && c.blue == 0;
184 }
185
186 static inline int is_white(struct color c)
187 {
188     return c.red == 255 && c.green == 255 && c.blue == 255;
189 }
190
191 static inline int is_gray(struct color c)
192 {
193     return c.red == c.green && c.red == c.blue;
194 }
195
196 static inline int is_equal(struct color c1, struct color c2)
197 {
198     return c1.red == c2.red && c1.green == c2.green && c1.blue == c2.blue;
199 }
200
201 static int write_hex_cnt;
202
203 static void write_hex(unsigned char byte)
204 {
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);
209     else
210         fprintf(out, "\t0x%02x", byte);
211     write_hex_cnt++;
212 }
213
214 static void write_header(void)
215 {
216         /* open logo file */
217         if (outputname) {
218                 out = fopen(outputname, "w");
219                 if (!out)
220                         die("Cannot create file %s: %s\n", outputname, strerror(errno));
221         } else {
222                 out = stdout;
223         }
224
225         fputs("/*\n", out);
226         fputs(" *  DO NOT EDIT THIS FILE!\n", out);
227         fputs(" *\n", out);
228         fprintf(out, " *  It was automatically generated from %s\n", filename);
229         fputs(" *\n", out);
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",
234                 logoname);
235 }
236
237 static void write_footer(void)
238 {
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]);
242
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);
249         }  
250
251         fputs("};\n\n", out);
252
253         /* close logo file */
254         if (outputname)
255                 fclose(out);
256 }
257
258
259 static void write_logo_bmp(void)
260 {
261         unsigned long  i=0, j=0;
262         unsigned char *position ;
263         
264         /* validate image */
265 /*statistics how many colours ,and if have over 224
266         logo_clutsize = 0;
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]))
271                                         break;
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];
278                         }
279                 }
280
281 */
282         write_hex_cnt = 0;
283
284         
285         /* write file header */
286         write_header();
287 #if 1
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);
292
293         for (i = 0; i < sizeof(data_name); i++){
294                 write_hex(data_name[i]);
295         }
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);
300 #endif
301         
302 #if 0
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]))
308                                         break;
309                         write_hex(k+32);
310                 }
311         fputs("\n};\n\n", out);
312
313         
314
315         /* write logo clut */
316         fprintf(out, "static unsigned char %s_clut[] __initdata = {\n",
317                 logoname);
318
319         write_hex_cnt = 0;
320
321         for (i = 0; i < sizeof(clut_name); i++){
322                 write_hex(clut_name[i]);
323         }
324         write_hex(logo_clutsize);
325
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);
330         }
331
332         for (i = logo_clutsize; i < (MAX_LINUX_LOGO_COLORS * 3); i++)
333         {
334                 write_hex(32);
335         }
336
337         /* write logo structure and file footer */
338 #endif
339
340 #if 1
341         for (i=logo_height; i>0; i--)
342         {
343                 for (j=0; j<logo_width; j++)
344                 {       
345                                 position = logo_data + (i-1)* logo_width * 3 + 3 * j;
346 #if 0
347                         write_hex(*(position));
348                         write_hex(*(position+1));
349                         write_hex(*(position+2));
350 #else
351                         write_hex(*(position));
352                         write_hex(*(position+1));
353                         write_hex(*(position+2));               
354                         write_hex(0);
355 #endif
356                 }
357         }
358 #endif
359
360         fputs("\n};\n\n", out);
361         /* write logo clut */
362         fprintf(out, "static unsigned char %s_clut[] __initdata = {\n",
363                 logoname);
364
365         write_hex_cnt = 0;
366         for (i = 0; i < sizeof(clut_name); i++){
367                 write_hex(clut_name[i]);
368         }
369         
370         write_footer();
371 }
372
373 static void die(const char *fmt, ...)
374 {
375     va_list ap;
376
377     va_start(ap, fmt);
378     vfprintf(stderr, fmt, ap);
379     va_end(ap);
380
381     exit(1);
382 }
383
384 static void usage(void)
385 {
386     die("\n"
387         "Usage: %s [options] <filename>\n"
388         "\n"
389         "Valid options:\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"
395         "\n", programname);
396 }
397
398 int main(int argc, char *argv[])
399 {
400     int opt;
401
402     programname = argv[0];
403
404     opterr = 0;
405     while (1) {
406         opt = getopt(argc, argv, "hn:o:t:");
407         if (opt == -1)
408             break;
409
410         switch (opt) {
411             case 'h':
412                 usage();
413                 break;
414
415             case 'n':
416                 logoname = optarg;
417                 break;
418
419             case 'o':
420                 outputname = optarg;
421                 break;
422
423             case 't':
424                 if (!strcmp(optarg, "bmp"))
425                     logo_type = LINUX_LOGO_bmp;         
426                 else
427                         die("logo_type is wrong without bmp\n");
428                 break;
429
430             default:
431                 usage();
432                 break;
433         }
434     }
435     if (optind != argc-1)
436         usage();
437
438     filename = argv[optind];
439
440         read_image();
441     switch (logo_type) {
442         case LINUX_LOGO_bmp:
443                 write_logo_bmp();
444             break;
445         default :
446                 die("logo_type is wrong\n");
447     }
448     exit(0);
449 }
450