blob: 47228d255b344a755790f7cfdcbe6e27246cea86 [file] [log] [blame]
Mike Frysinger4ad8e9f2009-07-02 19:23:25 -04001#include "compiler.h"
wdenk0bbe2002002-03-14 10:00:31 +00002
wdenk0bbe2002002-03-14 10:00:31 +00003typedef struct bitmap_s { /* bitmap description */
4 uint16_t width;
5 uint16_t height;
6 uint8_t palette[256*3];
7 uint8_t *data;
8} bitmap_t;
9
10#define DEFAULT_CMAP_SIZE 16 /* size of default color map */
11
12/*
13 * Neutralize little endians.
14 */
15uint16_t le_short(uint16_t x)
16{
17 uint16_t val;
18 uint8_t *p = (uint8_t *)(&x);
19
20 val = (*p++ & 0xff) << 0;
21 val |= (*p & 0xff) << 8;
22
23 return val;
24}
25
26void skip_bytes (FILE *fp, int n)
27{
28 while (n-- > 0)
29 fgetc (fp);
30}
31
Peter Tyserf4a193b2009-04-24 15:59:56 -050032__attribute__ ((__noreturn__))
33int error (char * msg, FILE *fp)
34{
35 fprintf (stderr, "ERROR: %s\n", msg);
36
37 fclose (fp);
38
39 exit (EXIT_FAILURE);
40}
41
wdenk0bbe2002002-03-14 10:00:31 +000042int main (int argc, char *argv[])
43{
44 int i, x;
45 FILE *fp;
46 bitmap_t bmp;
47 bitmap_t *b = &bmp;
Wolfgang Denk375a89f2005-09-25 15:39:00 +020048 uint16_t data_offset, n_colors;
wdenk0bbe2002002-03-14 10:00:31 +000049
50 if (argc < 2) {
51 fprintf (stderr, "Usage: %s file\n", argv[0]);
52 exit (EXIT_FAILURE);
53 }
54
55 if ((fp = fopen (argv[1], "rb")) == NULL) {
56 perror (argv[1]);
57 exit (EXIT_FAILURE);
58 }
59
Peter Tyserf4a193b2009-04-24 15:59:56 -050060 if (fgetc (fp) != 'B' || fgetc (fp) != 'M')
61 error ("Input file is not a bitmap", fp);
wdenk0bbe2002002-03-14 10:00:31 +000062
wdenk57b2d802003-06-27 21:31:46 +000063 /*
64 * read width and height of the image, and the number of colors used;
wdenk0bbe2002002-03-14 10:00:31 +000065 * ignore the rest
66 */
Wolfgang Denk375a89f2005-09-25 15:39:00 +020067 skip_bytes (fp, 8);
Peter Tyserf4a193b2009-04-24 15:59:56 -050068 if (fread (&data_offset, sizeof (uint16_t), 1, fp) != 1)
69 error ("Couldn't read bitmap data offset", fp);
Wolfgang Denk375a89f2005-09-25 15:39:00 +020070 skip_bytes (fp, 6);
Peter Tyserf4a193b2009-04-24 15:59:56 -050071 if (fread (&b->width, sizeof (uint16_t), 1, fp) != 1)
72 error ("Couldn't read bitmap width", fp);
wdenk0bbe2002002-03-14 10:00:31 +000073 skip_bytes (fp, 2);
Peter Tyserf4a193b2009-04-24 15:59:56 -050074 if (fread (&b->height, sizeof (uint16_t), 1, fp) != 1)
75 error ("Couldn't read bitmap height", fp);
wdenk0bbe2002002-03-14 10:00:31 +000076 skip_bytes (fp, 22);
Peter Tyserf4a193b2009-04-24 15:59:56 -050077 if (fread (&n_colors, sizeof (uint16_t), 1, fp) != 1)
78 error ("Couldn't read bitmap colors", fp);
wdenk0bbe2002002-03-14 10:00:31 +000079 skip_bytes (fp, 6);
80
81 /*
82 * Repair endianess.
83 */
Wolfgang Denk375a89f2005-09-25 15:39:00 +020084 data_offset = le_short(data_offset);
wdenk0bbe2002002-03-14 10:00:31 +000085 b->width = le_short(b->width);
86 b->height = le_short(b->height);
87 n_colors = le_short(n_colors);
88
89 /* assume we are working with an 8-bit file */
90 if ((n_colors == 0) || (n_colors > 256 - DEFAULT_CMAP_SIZE)) {
91 /* reserve DEFAULT_CMAP_SIZE color map entries for default map */
92 n_colors = 256 - DEFAULT_CMAP_SIZE;
93 }
94
95 printf ("/*\n"
96 " * Automatically generated by \"tools/bmp_logo\"\n"
97 " *\n"
98 " * DO NOT EDIT\n"
99 " *\n"
100 " */\n\n\n"
101 "#ifndef __BMP_LOGO_H__\n"
102 "#define __BMP_LOGO_H__\n\n"
103 "#define BMP_LOGO_WIDTH\t\t%d\n"
104 "#define BMP_LOGO_HEIGHT\t\t%d\n"
105 "#define BMP_LOGO_COLORS\t\t%d\n"
106 "#define BMP_LOGO_OFFSET\t\t%d\n"
107 "\n",
108 b->width, b->height, n_colors,
109 DEFAULT_CMAP_SIZE);
110
111 /* allocate memory */
Peter Tyserf4a193b2009-04-24 15:59:56 -0500112 if ((b->data = (uint8_t *)malloc(b->width * b->height)) == NULL)
113 error ("Error allocating memory for file", fp);
wdenk0bbe2002002-03-14 10:00:31 +0000114
115 /* read and print the palette information */
116 printf ("unsigned short bmp_logo_palette[] = {\n");
117
118 for (i=0; i<n_colors; ++i) {
119 b->palette[(int)(i*3+2)] = fgetc(fp);
120 b->palette[(int)(i*3+1)] = fgetc(fp);
121 b->palette[(int)(i*3+0)] = fgetc(fp);
122 x=fgetc(fp);
123
wdenk9ca7bbc2004-10-09 23:25:58 +0000124 printf ("%s0x0%X%X%X,%s",
125 ((i%8) == 0) ? "\t" : " ",
wdenk0bbe2002002-03-14 10:00:31 +0000126 (b->palette[(int)(i*3+0)] >> 4) & 0x0F,
127 (b->palette[(int)(i*3+1)] >> 4) & 0x0F,
128 (b->palette[(int)(i*3+2)] >> 4) & 0x0F,
wdenk9ca7bbc2004-10-09 23:25:58 +0000129 ((i%8) == 7) ? "\n" : ""
wdenk0bbe2002002-03-14 10:00:31 +0000130 );
wdenk0bbe2002002-03-14 10:00:31 +0000131 }
132
Wolfgang Denk375a89f2005-09-25 15:39:00 +0200133 /* seek to offset indicated by file header */
134 fseek(fp, (long)data_offset, SEEK_SET);
135
wdenk0bbe2002002-03-14 10:00:31 +0000136 /* read the bitmap; leave room for default color map */
137 printf ("\n");
138 printf ("};\n");
139 printf ("\n");
140 printf ("unsigned char bmp_logo_bitmap[] = {\n");
141 for (i=(b->height-1)*b->width; i>=0; i-=b->width) {
142 for (x = 0; x < b->width; x++) {
143 b->data[(uint16_t) i + x] = (uint8_t) fgetc (fp) \
144 + DEFAULT_CMAP_SIZE;
145 }
146 }
147 fclose (fp);
148
149 for (i=0; i<(b->height*b->width); ++i) {
150 if ((i%8) == 0)
151 putchar ('\t');
152 printf ("0x%02X,%c",
153 b->data[i],
154 ((i%8) == 7) ? '\n' : ' '
155 );
156 }
157 printf ("\n"
158 "};\n\n"
159 "#endif /* __BMP_LOGO_H__ */\n"
160 );
161
162 return (0);
163}