xref: /openbmc/u-boot/tools/bmp_logo.c (revision c83bf6a2)
1 #include <stdio.h>
2 #include <stdlib.h>
3 
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
13 
14 typedef struct bitmap_s {		/* bitmap description */
15 	uint16_t width;
16 	uint16_t height;
17 	uint8_t	palette[256*3];
18 	uint8_t	*data;
19 } bitmap_t;
20 
21 #define DEFAULT_CMAP_SIZE	16	/* size of default color map	*/
22 
23 /*
24  * Neutralize little endians.
25  */
26 uint16_t le_short(uint16_t x)
27 {
28     uint16_t val;
29     uint8_t *p = (uint8_t *)(&x);
30 
31     val =  (*p++ & 0xff) << 0;
32     val |= (*p & 0xff) << 8;
33 
34     return val;
35 }
36 
37 void skip_bytes (FILE *fp, int n)
38 {
39 	while (n-- > 0)
40 		fgetc (fp);
41 }
42 
43 int main (int argc, char *argv[])
44 {
45 	int	i, x;
46 	FILE	*fp;
47 	bitmap_t bmp;
48 	bitmap_t *b = &bmp;
49 	uint16_t n_colors;
50 
51 	if (argc < 2) {
52 		fprintf (stderr, "Usage: %s file\n", argv[0]);
53 		exit (EXIT_FAILURE);
54 	}
55 
56 	if ((fp = fopen (argv[1], "rb")) == NULL) {
57 		perror (argv[1]);
58 		exit (EXIT_FAILURE);
59 	}
60 
61 	if (fgetc (fp) != 'B' || fgetc (fp) != 'M') {
62 		fprintf (stderr, "%s is not a bitmap file.\n", argv[1]);
63 		exit (EXIT_FAILURE);
64 	}
65 
66 	/*
67 	 * read width and height of the image, and the number of colors used;
68 	 * ignore the rest
69 	 */
70 	skip_bytes (fp, 16);
71 	fread (&b->width,   sizeof (uint16_t), 1, fp);
72 	skip_bytes (fp, 2);
73 	fread (&b->height,  sizeof (uint16_t), 1, fp);
74 	skip_bytes (fp, 22);
75 	fread (&n_colors, sizeof (uint16_t), 1, fp);
76 	skip_bytes (fp, 6);
77 
78 	/*
79 	 * Repair endianess.
80 	 */
81 	b->width = le_short(b->width);
82 	b->height = le_short(b->height);
83 	n_colors = le_short(n_colors);
84 
85 	/* assume we are working with an 8-bit file */
86 	if ((n_colors == 0) || (n_colors > 256 - DEFAULT_CMAP_SIZE)) {
87 		/* reserve DEFAULT_CMAP_SIZE color map entries for default map */
88 		n_colors = 256 - DEFAULT_CMAP_SIZE;
89 	}
90 
91 	printf ("/*\n"
92 		" * Automatically generated by \"tools/bmp_logo\"\n"
93 		" *\n"
94 		" * DO NOT EDIT\n"
95 		" *\n"
96 		" */\n\n\n"
97 		"#ifndef __BMP_LOGO_H__\n"
98 		"#define __BMP_LOGO_H__\n\n"
99 		"#define BMP_LOGO_WIDTH\t\t%d\n"
100 		"#define BMP_LOGO_HEIGHT\t\t%d\n"
101 		"#define BMP_LOGO_COLORS\t\t%d\n"
102 		"#define BMP_LOGO_OFFSET\t\t%d\n"
103 		"\n",
104 		b->width, b->height, n_colors,
105 		DEFAULT_CMAP_SIZE);
106 
107 	/* allocate memory */
108 	if ((b->data = (uint8_t *)malloc(b->width * b->height)) == NULL) {
109 		fclose (fp);
110 		printf ("Error allocating memory for file %s.\n", argv[1]);
111 		exit (EXIT_FAILURE);
112 	}
113 
114 	/* read and print the palette information */
115 	printf ("unsigned short bmp_logo_palette[] = {\n");
116 
117 	for (i=0; i<n_colors; ++i) {
118 		b->palette[(int)(i*3+2)] = fgetc(fp);
119 		b->palette[(int)(i*3+1)] = fgetc(fp);
120 		b->palette[(int)(i*3+0)] = fgetc(fp);
121 		x=fgetc(fp);
122 
123 #if 0
124 		if ((i%4) == 0)
125 			putchar ('\t');
126 		printf ("0x%02X, 0x%02X, 0x%02X,%s",
127 			b->palette[(int)(i*3+0)],
128 			b->palette[(int)(i*3+1)],
129 			b->palette[(int)(i*3+2)],
130 			((i%4) == 3) ? "\n" : "	   "
131 		);
132 #else
133 		if ((i%8) == 0)
134 			putchar ('\t');
135 		printf ("0x0%X%X%X,%s",
136 			(b->palette[(int)(i*3+0)] >> 4) & 0x0F,
137 			(b->palette[(int)(i*3+1)] >> 4) & 0x0F,
138 			(b->palette[(int)(i*3+2)] >> 4) & 0x0F,
139 			((i%8) == 7) ? "\n" : "  "
140 		);
141 #endif
142 	}
143 
144 	/* read the bitmap; leave room for default color map */
145 	printf ("\n");
146 	printf ("};\n");
147 	printf ("\n");
148 	printf ("unsigned char bmp_logo_bitmap[] = {\n");
149 	for (i=(b->height-1)*b->width; i>=0; i-=b->width) {
150 		for (x = 0; x < b->width; x++) {
151 			b->data[(uint16_t) i + x] = (uint8_t) fgetc (fp) \
152 						+ DEFAULT_CMAP_SIZE;
153 		}
154 	}
155 	fclose (fp);
156 
157 	for (i=0; i<(b->height*b->width); ++i) {
158 		if ((i%8) == 0)
159 			putchar ('\t');
160 		printf ("0x%02X,%c",
161 			b->data[i],
162 			((i%8) == 7) ? '\n' : ' '
163 		);
164 	}
165 	printf ("\n"
166 		"};\n\n"
167 		"#endif /* __BMP_LOGO_H__ */\n"
168 	);
169 
170 	return (0);
171 }
172