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