blob: e8dd8c80046c2513f1dabfde62df86dfc9b76911 [file] [log] [blame]
wdenk0bbe2002002-03-14 10:00:31 +00001#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
wdenk0bbe2002002-03-14 10:00:31 +000014typedef 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 */
26uint16_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
37void skip_bytes (FILE *fp, int n)
38{
39 while (n-- > 0)
40 fgetc (fp);
41}
42
Peter Tyser65351a82009-04-24 15:59:56 -050043__attribute__ ((__noreturn__))
44int error (char * msg, FILE *fp)
45{
46 fprintf (stderr, "ERROR: %s\n", msg);
47
48 fclose (fp);
49
50 exit (EXIT_FAILURE);
51}
52
wdenk0bbe2002002-03-14 10:00:31 +000053int main (int argc, char *argv[])
54{
55 int i, x;
56 FILE *fp;
57 bitmap_t bmp;
58 bitmap_t *b = &bmp;
Wolfgang Denkfd4bb672005-09-25 15:39:00 +020059 uint16_t data_offset, n_colors;
wdenk0bbe2002002-03-14 10:00:31 +000060
61 if (argc < 2) {
62 fprintf (stderr, "Usage: %s file\n", argv[0]);
63 exit (EXIT_FAILURE);
64 }
65
66 if ((fp = fopen (argv[1], "rb")) == NULL) {
67 perror (argv[1]);
68 exit (EXIT_FAILURE);
69 }
70
Peter Tyser65351a82009-04-24 15:59:56 -050071 if (fgetc (fp) != 'B' || fgetc (fp) != 'M')
72 error ("Input file is not a bitmap", fp);
wdenk0bbe2002002-03-14 10:00:31 +000073
wdenk8bde7f72003-06-27 21:31:46 +000074 /*
75 * read width and height of the image, and the number of colors used;
wdenk0bbe2002002-03-14 10:00:31 +000076 * ignore the rest
77 */
Wolfgang Denkfd4bb672005-09-25 15:39:00 +020078 skip_bytes (fp, 8);
Peter Tyser65351a82009-04-24 15:59:56 -050079 if (fread (&data_offset, sizeof (uint16_t), 1, fp) != 1)
80 error ("Couldn't read bitmap data offset", fp);
Wolfgang Denkfd4bb672005-09-25 15:39:00 +020081 skip_bytes (fp, 6);
Peter Tyser65351a82009-04-24 15:59:56 -050082 if (fread (&b->width, sizeof (uint16_t), 1, fp) != 1)
83 error ("Couldn't read bitmap width", fp);
wdenk0bbe2002002-03-14 10:00:31 +000084 skip_bytes (fp, 2);
Peter Tyser65351a82009-04-24 15:59:56 -050085 if (fread (&b->height, sizeof (uint16_t), 1, fp) != 1)
86 error ("Couldn't read bitmap height", fp);
wdenk0bbe2002002-03-14 10:00:31 +000087 skip_bytes (fp, 22);
Peter Tyser65351a82009-04-24 15:59:56 -050088 if (fread (&n_colors, sizeof (uint16_t), 1, fp) != 1)
89 error ("Couldn't read bitmap colors", fp);
wdenk0bbe2002002-03-14 10:00:31 +000090 skip_bytes (fp, 6);
91
92 /*
93 * Repair endianess.
94 */
Wolfgang Denkfd4bb672005-09-25 15:39:00 +020095 data_offset = le_short(data_offset);
wdenk0bbe2002002-03-14 10:00:31 +000096 b->width = le_short(b->width);
97 b->height = le_short(b->height);
98 n_colors = le_short(n_colors);
99
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;
104 }
105
106 printf ("/*\n"
107 " * Automatically generated by \"tools/bmp_logo\"\n"
108 " *\n"
109 " * DO NOT EDIT\n"
110 " *\n"
111 " */\n\n\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"
118 "\n",
119 b->width, b->height, n_colors,
120 DEFAULT_CMAP_SIZE);
121
122 /* allocate memory */
Peter Tyser65351a82009-04-24 15:59:56 -0500123 if ((b->data = (uint8_t *)malloc(b->width * b->height)) == NULL)
124 error ("Error allocating memory for file", fp);
wdenk0bbe2002002-03-14 10:00:31 +0000125
126 /* read and print the palette information */
127 printf ("unsigned short bmp_logo_palette[] = {\n");
128
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);
133 x=fgetc(fp);
134
wdenk8655b6f2004-10-09 23:25:58 +0000135 printf ("%s0x0%X%X%X,%s",
136 ((i%8) == 0) ? "\t" : " ",
wdenk0bbe2002002-03-14 10:00:31 +0000137 (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,
wdenk8655b6f2004-10-09 23:25:58 +0000140 ((i%8) == 7) ? "\n" : ""
wdenk0bbe2002002-03-14 10:00:31 +0000141 );
wdenk0bbe2002002-03-14 10:00:31 +0000142 }
143
Wolfgang Denkfd4bb672005-09-25 15:39:00 +0200144 /* seek to offset indicated by file header */
145 fseek(fp, (long)data_offset, SEEK_SET);
146
wdenk0bbe2002002-03-14 10:00:31 +0000147 /* read the bitmap; leave room for default color map */
148 printf ("\n");
149 printf ("};\n");
150 printf ("\n");
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) \
155 + DEFAULT_CMAP_SIZE;
156 }
157 }
158 fclose (fp);
159
160 for (i=0; i<(b->height*b->width); ++i) {
161 if ((i%8) == 0)
162 putchar ('\t');
163 printf ("0x%02X,%c",
164 b->data[i],
165 ((i%8) == 7) ? '\n' : ' '
166 );
167 }
168 printf ("\n"
169 "};\n\n"
170 "#endif /* __BMP_LOGO_H__ */\n"
171 );
172
173 return (0);
174}