wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 1 | #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 | |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 14 | typedef 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 | */ |
| 26 | uint16_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 | |
| 37 | void skip_bytes (FILE *fp, int n) |
| 38 | { |
| 39 | while (n-- > 0) |
| 40 | fgetc (fp); |
| 41 | } |
| 42 | |
| 43 | int main (int argc, char *argv[]) |
| 44 | { |
| 45 | int i, x; |
| 46 | FILE *fp; |
| 47 | bitmap_t bmp; |
| 48 | bitmap_t *b = &bmp; |
Wolfgang Denk | 375a89f | 2005-09-25 15:39:00 +0200 | [diff] [blame] | 49 | uint16_t data_offset, n_colors; |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 50 | |
| 51 | if (argc < 2) { |
| 52 | fprintf (stderr, "Usage: %s file\n", argv[0]); |
| 53 | exit (EXIT_FAILURE); |
| 54 | } |
| 55 | |
| 56 | if ((fp = fopen (argv[1], "rb")) == NULL) { |
| 57 | perror (argv[1]); |
| 58 | exit (EXIT_FAILURE); |
| 59 | } |
| 60 | |
| 61 | if (fgetc (fp) != 'B' || fgetc (fp) != 'M') { |
| 62 | fprintf (stderr, "%s is not a bitmap file.\n", argv[1]); |
| 63 | exit (EXIT_FAILURE); |
| 64 | } |
| 65 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 66 | /* |
| 67 | * read width and height of the image, and the number of colors used; |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 68 | * ignore the rest |
| 69 | */ |
Wolfgang Denk | 375a89f | 2005-09-25 15:39:00 +0200 | [diff] [blame] | 70 | skip_bytes (fp, 8); |
| 71 | fread (&data_offset, sizeof (uint16_t), 1, fp); |
| 72 | skip_bytes (fp, 6); |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 73 | fread (&b->width, sizeof (uint16_t), 1, fp); |
| 74 | skip_bytes (fp, 2); |
| 75 | fread (&b->height, sizeof (uint16_t), 1, fp); |
| 76 | skip_bytes (fp, 22); |
| 77 | fread (&n_colors, sizeof (uint16_t), 1, fp); |
| 78 | skip_bytes (fp, 6); |
| 79 | |
| 80 | /* |
| 81 | * Repair endianess. |
| 82 | */ |
Wolfgang Denk | 375a89f | 2005-09-25 15:39:00 +0200 | [diff] [blame] | 83 | data_offset = le_short(data_offset); |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 84 | b->width = le_short(b->width); |
| 85 | b->height = le_short(b->height); |
| 86 | n_colors = le_short(n_colors); |
| 87 | |
| 88 | /* assume we are working with an 8-bit file */ |
| 89 | if ((n_colors == 0) || (n_colors > 256 - DEFAULT_CMAP_SIZE)) { |
| 90 | /* reserve DEFAULT_CMAP_SIZE color map entries for default map */ |
| 91 | n_colors = 256 - DEFAULT_CMAP_SIZE; |
| 92 | } |
| 93 | |
| 94 | printf ("/*\n" |
| 95 | " * Automatically generated by \"tools/bmp_logo\"\n" |
| 96 | " *\n" |
| 97 | " * DO NOT EDIT\n" |
| 98 | " *\n" |
| 99 | " */\n\n\n" |
| 100 | "#ifndef __BMP_LOGO_H__\n" |
| 101 | "#define __BMP_LOGO_H__\n\n" |
| 102 | "#define BMP_LOGO_WIDTH\t\t%d\n" |
| 103 | "#define BMP_LOGO_HEIGHT\t\t%d\n" |
| 104 | "#define BMP_LOGO_COLORS\t\t%d\n" |
| 105 | "#define BMP_LOGO_OFFSET\t\t%d\n" |
| 106 | "\n", |
| 107 | b->width, b->height, n_colors, |
| 108 | DEFAULT_CMAP_SIZE); |
| 109 | |
| 110 | /* allocate memory */ |
| 111 | if ((b->data = (uint8_t *)malloc(b->width * b->height)) == NULL) { |
| 112 | fclose (fp); |
| 113 | printf ("Error allocating memory for file %s.\n", argv[1]); |
| 114 | exit (EXIT_FAILURE); |
| 115 | } |
| 116 | |
| 117 | /* read and print the palette information */ |
| 118 | printf ("unsigned short bmp_logo_palette[] = {\n"); |
| 119 | |
| 120 | for (i=0; i<n_colors; ++i) { |
| 121 | b->palette[(int)(i*3+2)] = fgetc(fp); |
| 122 | b->palette[(int)(i*3+1)] = fgetc(fp); |
| 123 | b->palette[(int)(i*3+0)] = fgetc(fp); |
| 124 | x=fgetc(fp); |
| 125 | |
wdenk | 9ca7bbc | 2004-10-09 23:25:58 +0000 | [diff] [blame] | 126 | printf ("%s0x0%X%X%X,%s", |
| 127 | ((i%8) == 0) ? "\t" : " ", |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 128 | (b->palette[(int)(i*3+0)] >> 4) & 0x0F, |
| 129 | (b->palette[(int)(i*3+1)] >> 4) & 0x0F, |
| 130 | (b->palette[(int)(i*3+2)] >> 4) & 0x0F, |
wdenk | 9ca7bbc | 2004-10-09 23:25:58 +0000 | [diff] [blame] | 131 | ((i%8) == 7) ? "\n" : "" |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 132 | ); |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Wolfgang Denk | 375a89f | 2005-09-25 15:39:00 +0200 | [diff] [blame] | 135 | /* seek to offset indicated by file header */ |
| 136 | fseek(fp, (long)data_offset, SEEK_SET); |
| 137 | |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 138 | /* read the bitmap; leave room for default color map */ |
| 139 | printf ("\n"); |
| 140 | printf ("};\n"); |
| 141 | printf ("\n"); |
| 142 | printf ("unsigned char bmp_logo_bitmap[] = {\n"); |
| 143 | for (i=(b->height-1)*b->width; i>=0; i-=b->width) { |
| 144 | for (x = 0; x < b->width; x++) { |
| 145 | b->data[(uint16_t) i + x] = (uint8_t) fgetc (fp) \ |
| 146 | + DEFAULT_CMAP_SIZE; |
| 147 | } |
| 148 | } |
| 149 | fclose (fp); |
| 150 | |
| 151 | for (i=0; i<(b->height*b->width); ++i) { |
| 152 | if ((i%8) == 0) |
| 153 | putchar ('\t'); |
| 154 | printf ("0x%02X,%c", |
| 155 | b->data[i], |
| 156 | ((i%8) == 7) ? '\n' : ' ' |
| 157 | ); |
| 158 | } |
| 159 | printf ("\n" |
| 160 | "};\n\n" |
| 161 | "#endif /* __BMP_LOGO_H__ */\n" |
| 162 | ); |
| 163 | |
| 164 | return (0); |
| 165 | } |