to make u-boot work for fat32 filesystem
[jz_uboot.git] / tools / bmp_logo.c
blob0941bc1667b481b2851d3a696cec34a21be929cd
1 #include <stdio.h>
2 #include <stdlib.h>
4 #if defined(__linux__)
5 #include <stdint.h>
6 #else
7 #ifdef __CYGWIN__
8 #include "elf.h"
9 #else
10 #include <inttypes.h>
11 #endif
12 #endif
14 #include <config.h>
16 typedef struct bitmap_s { /* bitmap description */
17 uint16_t width;
18 uint16_t height;
19 uint8_t palette[256*3];
20 uint8_t *data;
21 } bitmap_t;
25 #define DEFAULT_CMAP_SIZE 16 /* size of default color map */
28 * Neutralize little endians.
30 uint16_t le_short(uint16_t x)
32 uint16_t val;
33 uint8_t *p = (uint8_t *)(&x);
35 val = (*p++ & 0xff) << 0;
36 val |= (*p & 0xff) << 8;
38 return val;
41 uint32_t le_int(uint32_t x)
43 uint32_t val;
44 uint8_t *p = (uint8_t *)(&x);
46 val = (*p++ & 0xff) << 0;
47 val = (*p++ & 0xff) << 8;
48 val = (*p++ & 0xff) << 16;
49 val |= (*p & 0xff) << 24;
51 return val;
54 void skip_bytes (FILE *fp, int n)
56 while (n-- > 0)
57 fgetc (fp);
60 int main (int argc, char *argv[])
62 int i, x;
63 FILE *fp;
64 bitmap_t bmp;
65 bitmap_t *b = &bmp;
66 uint16_t data_offset, n_colors;
67 #if defined(CONFIG_LCD)
68 #if LCD_BPP==4
69 short val;
70 #elif LCD_BPP==5
71 int val;
72 #endif
73 #endif
75 if (argc < 2) {
76 fprintf (stderr, "Usage: %s file\n", argv[0]);
77 exit (EXIT_FAILURE);
80 if ((fp = fopen (argv[1], "rb")) == NULL) {
81 perror (argv[1]);
82 exit (EXIT_FAILURE);
85 if (fgetc (fp) != 'B' || fgetc (fp) != 'M') {
86 fprintf (stderr, "%s is not a bitmap file.\n", argv[1]);
87 exit (EXIT_FAILURE);
91 * read width and height of the image, and the number of colors used;
92 * ignore the rest
94 skip_bytes (fp, 8);
95 fread (&data_offset, sizeof (uint16_t), 1, fp);
96 skip_bytes (fp, 6);
97 fread (&b->width, sizeof (uint16_t), 1, fp);
98 skip_bytes (fp, 2);
99 fread (&b->height, sizeof (uint16_t), 1, fp);
100 skip_bytes (fp, 22);
101 fread (&n_colors, sizeof (uint16_t), 1, fp);
102 skip_bytes (fp, 6);
105 * Repair endianess.
107 data_offset = le_short(data_offset);
108 b->width = le_short(b->width);
109 b->height = le_short(b->height);
110 n_colors = le_short(n_colors);
112 /* assume we are working with an 8-bit file */
113 #if defined(CONFIG_LCD)
114 if ( n_colors > 256 ) {
115 n_colors = 256 ;
117 #else
118 if ((n_colors == 0) || (n_colors > 256 - DEFAULT_CMAP_SIZE)) {
119 /* reserve DEFAULT_CMAP_SIZE color map entries for default map */
120 n_colors = 256 - DEFAULT_CMAP_SIZE;
122 #endif
124 printf ("/*\n"
125 " * Automatically generated by \"tools/bmp_logo\"\n"
126 " *\n"
127 " * DO NOT EDIT\n"
128 " *\n"
129 " */\n\n\n"
130 "#ifndef __BMP_LOGO_H__\n"
131 "#define __BMP_LOGO_H__\n\n"
132 "#define BMP_LOGO_WIDTH\t\t%d\n"
133 "#define BMP_LOGO_HEIGHT\t\t%d\n"
134 "#define BMP_LOGO_COLORS\t\t%d\n"
135 "#define BMP_LOGO_OFFSET\t\t%d\n"
136 "\n",
137 b->width, b->height, n_colors,
138 DEFAULT_CMAP_SIZE);
140 /* allocate memory */
141 if ((b->data = (uint8_t *)malloc(b->width * b->height)) == NULL) {
142 fclose (fp);
143 printf ("Error allocating memory for file %s.\n", argv[1]);
144 exit (EXIT_FAILURE);
147 /* read and print the palette information */
148 printf ("unsigned int bmp_logo_palette[] = {\n");
150 for (i=0; i<n_colors; ++i) {
151 b->palette[(int)(i*3+2)] = fgetc(fp);
152 b->palette[(int)(i*3+1)] = fgetc(fp);
153 b->palette[(int)(i*3+0)] = fgetc(fp);
154 x=fgetc(fp);
156 #if defined(CONFIG_LCD)
157 val = 0;
159 #if LCD_BPP==4
160 /* RGB565(16bits) */
161 val = ((b->palette[(int)(i*3+0)] >> 3) & 0x1F )<< 11 | \
162 ((b->palette[(int)(i*3+1)] >> 2) & 0x3F )<< 5 | \
163 ((b->palette[(int)(i*3+2)] >> 3) & 0x1F );
164 #elif LCD_BPP==5
165 /* RGB666(18bits) */
166 val = b->palette[(int)(i*3+0)] << 16 | \
167 b->palette[(int)(i*3+1)] << 8 | \
168 b->palette[(int)(i*3+2)] << 0 ;
169 #endif
170 printf ("%s0x%X,%s",
171 ((i%8) == 0) ? "\t" : " ",
172 val,
173 ((i%8) == 7) ? "\n" : ""
176 #else
177 printf ("%s0x0%X%X%X,%s",
178 ((i%8) == 0) ? "\t" : " ",
179 (b->palette[(int)(i*3+0)] >> 4) & 0x0F,
180 (b->palette[(int)(i*3+1)] >> 4) & 0x0F,
181 (b->palette[(int)(i*3+2)] >> 4) & 0x0F,
182 ((i%8) == 7) ? "\n" : ""
184 #endif
187 /* seek to offset indicated by file header */
188 fseek(fp, (long)data_offset, SEEK_SET);
190 /* read the bitmap; leave room for default color map */
191 printf ("\n");
192 printf ("};\n");
193 printf ("\n");
194 printf ("unsigned char bmp_logo_bitmap[] = {\n");
195 for (i=(b->height-1)*b->width; i>=0; i-=b->width) {
196 for (x = 0; x < b->width; x++) {
197 #if defined(CONFIG_LCD)
198 b->data[ i + x] = (uint8_t) fgetc (fp) ;
199 #else
200 b->data[(uint16_t) i + x] = (uint8_t) fgetc (fp) \
201 + DEFAULT_CMAP_SIZE;
202 #endif
205 fclose (fp);
207 for (i=0; i<(b->height*b->width); ++i) {
208 if ((i%8) == 0)
209 putchar ('\t');
210 printf ("0x%02X,%c",
211 b->data[i],
212 ((i%8) == 7) ? '\n' : ' '
215 printf ("\n"
216 "};\n\n"
217 "#endif /* __BMP_LOGO_H__ */\n"
220 return (0);