14 typedef struct bitmap_s { /* bitmap description */
17 uint8_t palette[256*3];
21 #define DEFAULT_CMAP_SIZE 16 /* size of default color map */
24 * Neutralize little endians.
26 uint16_t le_short(uint16_t x)
29 uint8_t *p = (uint8_t *)(&x);
31 val = (*p++ & 0xff) << 0;
32 val |= (*p & 0xff) << 8;
37 void skip_bytes (FILE *fp, int n)
43 __attribute__ ((__noreturn__))
44 int error (char * msg, FILE *fp)
46 fprintf (stderr, "ERROR: %s\n", msg);
53 int main (int argc, char *argv[])
59 uint16_t data_offset, n_colors;
62 fprintf (stderr, "Usage: %s file\n", argv[0]);
66 if ((fp = fopen (argv[1], "rb")) == NULL) {
71 if (fgetc (fp) != 'B' || fgetc (fp) != 'M')
72 error ("Input file is not a bitmap", fp);
75 * read width and height of the image, and the number of colors used;
79 if (fread (&data_offset, sizeof (uint16_t), 1, fp) != 1)
80 error ("Couldn't read bitmap data offset", fp);
82 if (fread (&b->width, sizeof (uint16_t), 1, fp) != 1)
83 error ("Couldn't read bitmap width", fp);
85 if (fread (&b->height, sizeof (uint16_t), 1, fp) != 1)
86 error ("Couldn't read bitmap height", fp);
88 if (fread (&n_colors, sizeof (uint16_t), 1, fp) != 1)
89 error ("Couldn't read bitmap colors", fp);
95 data_offset = le_short(data_offset);
96 b->width = le_short(b->width);
97 b->height = le_short(b->height);
98 n_colors = le_short(n_colors);
100 /* assume we are working with an 8-bit file */
101 if ((n_colors == 0) || (n_colors > 256 - DEFAULT_CMAP_SIZE)) {
102 /* reserve DEFAULT_CMAP_SIZE color map entries for default map */
103 n_colors = 256 - DEFAULT_CMAP_SIZE;
107 " * Automatically generated by \"tools/bmp_logo\"\n"
112 "#ifndef __BMP_LOGO_H__\n"
113 "#define __BMP_LOGO_H__\n\n"
114 "#define BMP_LOGO_WIDTH\t\t%d\n"
115 "#define BMP_LOGO_HEIGHT\t\t%d\n"
116 "#define BMP_LOGO_COLORS\t\t%d\n"
117 "#define BMP_LOGO_OFFSET\t\t%d\n"
119 b->width, b->height, n_colors,
122 /* allocate memory */
123 if ((b->data = (uint8_t *)malloc(b->width * b->height)) == NULL)
124 error ("Error allocating memory for file", fp);
126 /* read and print the palette information */
127 printf ("unsigned short bmp_logo_palette[] = {\n");
129 for (i=0; i<n_colors; ++i) {
130 b->palette[(int)(i*3+2)] = fgetc(fp);
131 b->palette[(int)(i*3+1)] = fgetc(fp);
132 b->palette[(int)(i*3+0)] = fgetc(fp);
135 printf ("%s0x0%X%X%X,%s",
136 ((i%8) == 0) ? "\t" : " ",
137 (b->palette[(int)(i*3+0)] >> 4) & 0x0F,
138 (b->palette[(int)(i*3+1)] >> 4) & 0x0F,
139 (b->palette[(int)(i*3+2)] >> 4) & 0x0F,
140 ((i%8) == 7) ? "\n" : ""
144 /* seek to offset indicated by file header */
145 fseek(fp, (long)data_offset, SEEK_SET);
147 /* read the bitmap; leave room for default color map */
151 printf ("unsigned char bmp_logo_bitmap[] = {\n");
152 for (i=(b->height-1)*b->width; i>=0; i-=b->width) {
153 for (x = 0; x < b->width; x++) {
154 b->data[(uint16_t) i + x] = (uint8_t) fgetc (fp) \
160 for (i=0; i<(b->height*b->width); ++i) {
165 ((i%8) == 7) ? '\n' : ' '
170 "#endif /* __BMP_LOGO_H__ */\n"