9 typedef struct bitmap_s { /* bitmap description */
12 uint8_t palette[256*3];
16 #define DEFAULT_CMAP_SIZE 16 /* size of default color map */
18 void usage(const char *prog)
20 fprintf(stderr, "Usage: %s [--gen-info|--gen-data|--gen-bmp] file\n",
25 * Neutralize little endians.
27 uint16_t le_short(uint16_t x)
30 uint8_t *p = (uint8_t *)(&x);
32 val = (*p++ & 0xff) << 0;
33 val |= (*p & 0xff) << 8;
38 void skip_bytes (FILE *fp, int n)
44 __attribute__ ((__noreturn__))
45 int error (char * msg, FILE *fp)
47 fprintf (stderr, "ERROR: %s\n", msg);
54 void gen_info(bitmap_t *b, uint16_t n_colors)
57 " * Automatically generated by \"tools/bmp_logo\"\n"
62 "#ifndef __BMP_LOGO_H__\n"
63 "#define __BMP_LOGO_H__\n\n"
64 "#define BMP_LOGO_WIDTH\t\t%d\n"
65 "#define BMP_LOGO_HEIGHT\t\t%d\n"
66 "#define BMP_LOGO_COLORS\t\t%d\n"
67 "#define BMP_LOGO_OFFSET\t\t%d\n\n"
68 "extern unsigned short bmp_logo_palette[];\n"
69 "extern unsigned char bmp_logo_bitmap[];\n\n"
70 "#endif /* __BMP_LOGO_H__ */\n",
71 b->width, b->height, n_colors,
75 int main (int argc, char *argv[])
82 uint16_t data_offset, n_colors, hdr_size;
89 if (!strcmp(argv[1], "--gen-info"))
91 else if (!strcmp(argv[1], "--gen-data"))
93 else if (!strcmp(argv[1], "--gen-bmp"))
100 fp = fopen(argv[2], "rb");
106 if (fgetc (fp) != 'B' || fgetc (fp) != 'M')
107 error ("Input file is not a bitmap", fp);
110 * read width and height of the image, and the number of colors used;
114 if (fread (&data_offset, sizeof (uint16_t), 1, fp) != 1)
115 error ("Couldn't read bitmap data offset", fp);
117 if (fread(&hdr_size, sizeof(uint16_t), 1, fp) != 1)
118 error("Couldn't read bitmap header size", fp);
120 error("Invalid bitmap header", fp);
122 if (fread (&b->width, sizeof (uint16_t), 1, fp) != 1)
123 error ("Couldn't read bitmap width", fp);
125 if (fread (&b->height, sizeof (uint16_t), 1, fp) != 1)
126 error ("Couldn't read bitmap height", fp);
128 if (fread (&n_colors, sizeof (uint16_t), 1, fp) != 1)
129 error ("Couldn't read bitmap colors", fp);
130 skip_bytes(fp, hdr_size - 34);
135 data_offset = le_short(data_offset);
136 b->width = le_short(b->width);
137 b->height = le_short(b->height);
138 n_colors = le_short(n_colors);
139 size = b->width * b->height;
141 /* assume we are working with an 8-bit file */
142 if ((n_colors == 0) || (n_colors > 256 - DEFAULT_CMAP_SIZE)) {
143 /* reserve DEFAULT_CMAP_SIZE color map entries for default map */
144 n_colors = 256 - DEFAULT_CMAP_SIZE;
147 if (mode == MODE_GEN_INFO) {
148 gen_info(b, n_colors);
153 " * Automatically generated by \"tools/bmp_logo\"\n"
158 "#ifndef __BMP_LOGO_DATA_H__\n"
159 "#define __BMP_LOGO_DATA_H__\n\n");
161 /* read and print the palette information */
162 printf("unsigned short bmp_logo_palette[] = {\n");
164 for (i=0; i<n_colors; ++i) {
165 b->palette[(int)(i*3+2)] = fgetc(fp);
166 b->palette[(int)(i*3+1)] = fgetc(fp);
167 b->palette[(int)(i*3+0)] = fgetc(fp);
170 printf ("%s0x0%X%X%X,%s",
171 ((i%8) == 0) ? "\t" : " ",
172 (b->palette[(int)(i*3+0)] >> 4) & 0x0F,
173 (b->palette[(int)(i*3+1)] >> 4) & 0x0F,
174 (b->palette[(int)(i*3+2)] >> 4) & 0x0F,
175 ((i%8) == 7) ? "\n" : ""
179 /* seek to offset indicated by file header */
180 if (mode == MODE_GEN_BMP) {
181 /* copy full bmp file */
182 fseek(fp, 0L, SEEK_END);
184 fseek(fp, 0L, SEEK_SET);
186 fseek(fp, (long)data_offset, SEEK_SET);
189 /* allocate memory */
190 b->data = (uint8_t *)malloc(size);
192 error("Error allocating memory for file", fp);
194 /* read the bitmap; leave room for default color map */
198 printf("unsigned char bmp_logo_bitmap[] = {\n");
199 if (mode == MODE_GEN_BMP) {
201 for (i = 0; i < size; i++)
202 b->data[i] = (uint8_t)fgetc(fp);
204 for (i = (b->height - 1) * b->width; i >= 0; i -= b->width) {
205 for (x = 0; x < b->width; x++) {
206 b->data[i + x] = (uint8_t)fgetc(fp)
212 for (i = 0; i < size; ++i) {
217 ((i%8) == 7) ? '\n' : ' '
222 "#endif /* __BMP_LOGO_DATA_H__ */\n"