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 | |
Peter Tyser | f4a193b | 2009-04-24 15:59:56 -0500 | [diff] [blame] | 43 | __attribute__ ((__noreturn__)) |
| 44 | int error (char * msg, FILE *fp) |
| 45 | { |
| 46 | fprintf (stderr, "ERROR: %s\n", msg); |
| 47 | |
| 48 | fclose (fp); |
| 49 | |
| 50 | exit (EXIT_FAILURE); |
| 51 | } |
| 52 | |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 53 | int main (int argc, char *argv[]) |
| 54 | { |
| 55 | int i, x; |
| 56 | FILE *fp; |
| 57 | bitmap_t bmp; |
| 58 | bitmap_t *b = &bmp; |
Wolfgang Denk | 375a89f | 2005-09-25 15:39:00 +0200 | [diff] [blame] | 59 | uint16_t data_offset, n_colors; |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 60 | |
| 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 Tyser | f4a193b | 2009-04-24 15:59:56 -0500 | [diff] [blame] | 71 | if (fgetc (fp) != 'B' || fgetc (fp) != 'M') |
| 72 | error ("Input file is not a bitmap", fp); |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 73 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 74 | /* |
| 75 | * read width and height of the image, and the number of colors used; |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 76 | * ignore the rest |
| 77 | */ |
Wolfgang Denk | 375a89f | 2005-09-25 15:39:00 +0200 | [diff] [blame] | 78 | skip_bytes (fp, 8); |
Peter Tyser | f4a193b | 2009-04-24 15:59:56 -0500 | [diff] [blame] | 79 | if (fread (&data_offset, sizeof (uint16_t), 1, fp) != 1) |
| 80 | error ("Couldn't read bitmap data offset", fp); |
Wolfgang Denk | 375a89f | 2005-09-25 15:39:00 +0200 | [diff] [blame] | 81 | skip_bytes (fp, 6); |
Peter Tyser | f4a193b | 2009-04-24 15:59:56 -0500 | [diff] [blame] | 82 | if (fread (&b->width, sizeof (uint16_t), 1, fp) != 1) |
| 83 | error ("Couldn't read bitmap width", fp); |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 84 | skip_bytes (fp, 2); |
Peter Tyser | f4a193b | 2009-04-24 15:59:56 -0500 | [diff] [blame] | 85 | if (fread (&b->height, sizeof (uint16_t), 1, fp) != 1) |
| 86 | error ("Couldn't read bitmap height", fp); |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 87 | skip_bytes (fp, 22); |
Peter Tyser | f4a193b | 2009-04-24 15:59:56 -0500 | [diff] [blame] | 88 | if (fread (&n_colors, sizeof (uint16_t), 1, fp) != 1) |
| 89 | error ("Couldn't read bitmap colors", fp); |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 90 | skip_bytes (fp, 6); |
| 91 | |
| 92 | /* |
| 93 | * Repair endianess. |
| 94 | */ |
Wolfgang Denk | 375a89f | 2005-09-25 15:39:00 +0200 | [diff] [blame] | 95 | data_offset = le_short(data_offset); |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 96 | 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 Tyser | f4a193b | 2009-04-24 15:59:56 -0500 | [diff] [blame] | 123 | if ((b->data = (uint8_t *)malloc(b->width * b->height)) == NULL) |
| 124 | error ("Error allocating memory for file", fp); |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 125 | |
| 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 | |
wdenk | 9ca7bbc | 2004-10-09 23:25:58 +0000 | [diff] [blame] | 135 | printf ("%s0x0%X%X%X,%s", |
| 136 | ((i%8) == 0) ? "\t" : " ", |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 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, |
wdenk | 9ca7bbc | 2004-10-09 23:25:58 +0000 | [diff] [blame] | 140 | ((i%8) == 7) ? "\n" : "" |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 141 | ); |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Wolfgang Denk | 375a89f | 2005-09-25 15:39:00 +0200 | [diff] [blame] | 144 | /* seek to offset indicated by file header */ |
| 145 | fseek(fp, (long)data_offset, SEEK_SET); |
| 146 | |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 147 | /* 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 | } |