blob: 55f833fb9b8bbcb11c58e29430b193ff327e7ab8 [file] [log] [blame]
Mike Frysinger37566092009-07-02 19:23:25 -04001#include "compiler.h"
wdenk0bbe2002002-03-14 10:00:31 +00002
Che-Liang Chiouc2707302011-10-20 23:04:20 +00003enum {
4 MODE_GEN_INFO,
5 MODE_GEN_DATA
6};
7
wdenk0bbe2002002-03-14 10:00:31 +00008typedef struct bitmap_s { /* bitmap description */
9 uint16_t width;
10 uint16_t height;
11 uint8_t palette[256*3];
12 uint8_t *data;
13} bitmap_t;
14
15#define DEFAULT_CMAP_SIZE 16 /* size of default color map */
16
Che-Liang Chiouc2707302011-10-20 23:04:20 +000017void usage(const char *prog)
18{
19 fprintf(stderr, "Usage: %s [--gen-info|--gen-data] file\n", prog);
20}
21
wdenk0bbe2002002-03-14 10:00:31 +000022/*
23 * Neutralize little endians.
24 */
25uint16_t le_short(uint16_t x)
26{
27 uint16_t val;
28 uint8_t *p = (uint8_t *)(&x);
29
30 val = (*p++ & 0xff) << 0;
31 val |= (*p & 0xff) << 8;
32
33 return val;
34}
35
36void skip_bytes (FILE *fp, int n)
37{
38 while (n-- > 0)
39 fgetc (fp);
40}
41
Peter Tyser65351a82009-04-24 15:59:56 -050042__attribute__ ((__noreturn__))
43int error (char * msg, FILE *fp)
44{
45 fprintf (stderr, "ERROR: %s\n", msg);
46
47 fclose (fp);
48
49 exit (EXIT_FAILURE);
50}
51
Che-Liang Chiouc2707302011-10-20 23:04:20 +000052void gen_info(bitmap_t *b, uint16_t n_colors)
53{
54 printf("/*\n"
55 " * Automatically generated by \"tools/bmp_logo\"\n"
56 " *\n"
57 " * DO NOT EDIT\n"
58 " *\n"
59 " */\n\n\n"
60 "#ifndef __BMP_LOGO_H__\n"
61 "#define __BMP_LOGO_H__\n\n"
62 "#define BMP_LOGO_WIDTH\t\t%d\n"
63 "#define BMP_LOGO_HEIGHT\t\t%d\n"
64 "#define BMP_LOGO_COLORS\t\t%d\n"
65 "#define BMP_LOGO_OFFSET\t\t%d\n\n"
66 "extern unsigned short bmp_logo_palette[];\n"
67 "extern unsigned char bmp_logo_bitmap[];\n\n"
68 "#endif /* __BMP_LOGO_H__ */\n",
69 b->width, b->height, n_colors,
70 DEFAULT_CMAP_SIZE);
71}
72
wdenk0bbe2002002-03-14 10:00:31 +000073int main (int argc, char *argv[])
74{
Che-Liang Chiouc2707302011-10-20 23:04:20 +000075 int mode, i, x;
wdenk0bbe2002002-03-14 10:00:31 +000076 FILE *fp;
77 bitmap_t bmp;
78 bitmap_t *b = &bmp;
Jon Smith06d326d2017-10-05 08:35:02 -040079 uint16_t data_offset, n_colors, hdr_size;
wdenk0bbe2002002-03-14 10:00:31 +000080
Che-Liang Chiouc2707302011-10-20 23:04:20 +000081 if (argc < 3) {
82 usage(argv[0]);
wdenk0bbe2002002-03-14 10:00:31 +000083 exit (EXIT_FAILURE);
84 }
85
Che-Liang Chiouc2707302011-10-20 23:04:20 +000086 if (!strcmp(argv[1], "--gen-info"))
87 mode = MODE_GEN_INFO;
88 else if (!strcmp(argv[1], "--gen-data"))
89 mode = MODE_GEN_DATA;
90 else {
91 usage(argv[0]);
92 exit(EXIT_FAILURE);
93 }
94
95 fp = fopen(argv[2], "rb");
96 if (!fp) {
97 perror(argv[2]);
wdenk0bbe2002002-03-14 10:00:31 +000098 exit (EXIT_FAILURE);
99 }
100
Peter Tyser65351a82009-04-24 15:59:56 -0500101 if (fgetc (fp) != 'B' || fgetc (fp) != 'M')
102 error ("Input file is not a bitmap", fp);
wdenk0bbe2002002-03-14 10:00:31 +0000103
wdenk8bde7f72003-06-27 21:31:46 +0000104 /*
105 * read width and height of the image, and the number of colors used;
wdenk0bbe2002002-03-14 10:00:31 +0000106 * ignore the rest
107 */
Wolfgang Denkfd4bb672005-09-25 15:39:00 +0200108 skip_bytes (fp, 8);
Peter Tyser65351a82009-04-24 15:59:56 -0500109 if (fread (&data_offset, sizeof (uint16_t), 1, fp) != 1)
110 error ("Couldn't read bitmap data offset", fp);
Jon Smith06d326d2017-10-05 08:35:02 -0400111 skip_bytes(fp, 2);
112 if (fread(&hdr_size, sizeof(uint16_t), 1, fp) != 1)
113 error("Couldn't read bitmap header size", fp);
114 if (hdr_size < 40)
115 error("Invalid bitmap header", fp);
116 skip_bytes(fp, 2);
Peter Tyser65351a82009-04-24 15:59:56 -0500117 if (fread (&b->width, sizeof (uint16_t), 1, fp) != 1)
118 error ("Couldn't read bitmap width", fp);
wdenk0bbe2002002-03-14 10:00:31 +0000119 skip_bytes (fp, 2);
Peter Tyser65351a82009-04-24 15:59:56 -0500120 if (fread (&b->height, sizeof (uint16_t), 1, fp) != 1)
121 error ("Couldn't read bitmap height", fp);
wdenk0bbe2002002-03-14 10:00:31 +0000122 skip_bytes (fp, 22);
Peter Tyser65351a82009-04-24 15:59:56 -0500123 if (fread (&n_colors, sizeof (uint16_t), 1, fp) != 1)
124 error ("Couldn't read bitmap colors", fp);
Jon Smith06d326d2017-10-05 08:35:02 -0400125 skip_bytes(fp, hdr_size - 34);
wdenk0bbe2002002-03-14 10:00:31 +0000126
127 /*
128 * Repair endianess.
129 */
Wolfgang Denkfd4bb672005-09-25 15:39:00 +0200130 data_offset = le_short(data_offset);
wdenk0bbe2002002-03-14 10:00:31 +0000131 b->width = le_short(b->width);
132 b->height = le_short(b->height);
133 n_colors = le_short(n_colors);
134
135 /* assume we are working with an 8-bit file */
136 if ((n_colors == 0) || (n_colors > 256 - DEFAULT_CMAP_SIZE)) {
137 /* reserve DEFAULT_CMAP_SIZE color map entries for default map */
138 n_colors = 256 - DEFAULT_CMAP_SIZE;
139 }
140
Che-Liang Chiouc2707302011-10-20 23:04:20 +0000141 if (mode == MODE_GEN_INFO) {
142 gen_info(b, n_colors);
143 goto out;
144 }
145
146 printf("/*\n"
wdenk0bbe2002002-03-14 10:00:31 +0000147 " * Automatically generated by \"tools/bmp_logo\"\n"
148 " *\n"
149 " * DO NOT EDIT\n"
150 " *\n"
151 " */\n\n\n"
Che-Liang Chiouc2707302011-10-20 23:04:20 +0000152 "#ifndef __BMP_LOGO_DATA_H__\n"
153 "#define __BMP_LOGO_DATA_H__\n\n");
wdenk0bbe2002002-03-14 10:00:31 +0000154
155 /* allocate memory */
Peter Tyser65351a82009-04-24 15:59:56 -0500156 if ((b->data = (uint8_t *)malloc(b->width * b->height)) == NULL)
157 error ("Error allocating memory for file", fp);
wdenk0bbe2002002-03-14 10:00:31 +0000158
159 /* read and print the palette information */
Che-Liang Chiouc2707302011-10-20 23:04:20 +0000160 printf("unsigned short bmp_logo_palette[] = {\n");
wdenk0bbe2002002-03-14 10:00:31 +0000161
162 for (i=0; i<n_colors; ++i) {
163 b->palette[(int)(i*3+2)] = fgetc(fp);
164 b->palette[(int)(i*3+1)] = fgetc(fp);
165 b->palette[(int)(i*3+0)] = fgetc(fp);
166 x=fgetc(fp);
167
wdenk8655b6f2004-10-09 23:25:58 +0000168 printf ("%s0x0%X%X%X,%s",
169 ((i%8) == 0) ? "\t" : " ",
wdenk0bbe2002002-03-14 10:00:31 +0000170 (b->palette[(int)(i*3+0)] >> 4) & 0x0F,
171 (b->palette[(int)(i*3+1)] >> 4) & 0x0F,
172 (b->palette[(int)(i*3+2)] >> 4) & 0x0F,
wdenk8655b6f2004-10-09 23:25:58 +0000173 ((i%8) == 7) ? "\n" : ""
wdenk0bbe2002002-03-14 10:00:31 +0000174 );
wdenk0bbe2002002-03-14 10:00:31 +0000175 }
176
Wolfgang Denkfd4bb672005-09-25 15:39:00 +0200177 /* seek to offset indicated by file header */
178 fseek(fp, (long)data_offset, SEEK_SET);
179
wdenk0bbe2002002-03-14 10:00:31 +0000180 /* read the bitmap; leave room for default color map */
181 printf ("\n");
182 printf ("};\n");
183 printf ("\n");
Che-Liang Chiouc2707302011-10-20 23:04:20 +0000184 printf("unsigned char bmp_logo_bitmap[] = {\n");
wdenk0bbe2002002-03-14 10:00:31 +0000185 for (i=(b->height-1)*b->width; i>=0; i-=b->width) {
186 for (x = 0; x < b->width; x++) {
Heiko Schocher3d192be2013-08-03 07:22:52 +0200187 b->data[i + x] = (uint8_t) fgetc(fp)
wdenk0bbe2002002-03-14 10:00:31 +0000188 + DEFAULT_CMAP_SIZE;
189 }
190 }
wdenk0bbe2002002-03-14 10:00:31 +0000191
192 for (i=0; i<(b->height*b->width); ++i) {
193 if ((i%8) == 0)
194 putchar ('\t');
195 printf ("0x%02X,%c",
196 b->data[i],
197 ((i%8) == 7) ? '\n' : ' '
198 );
199 }
200 printf ("\n"
201 "};\n\n"
Che-Liang Chiouc2707302011-10-20 23:04:20 +0000202 "#endif /* __BMP_LOGO_DATA_H__ */\n"
wdenk0bbe2002002-03-14 10:00:31 +0000203 );
204
Che-Liang Chiouc2707302011-10-20 23:04:20 +0000205out:
206 fclose(fp);
207 return 0;
wdenk0bbe2002002-03-14 10:00:31 +0000208}