Mike Frysinger | 4ad8e9f | 2009-07-02 19:23:25 -0400 | [diff] [blame] | 1 | #include "compiler.h" |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 2 | |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 3 | typedef 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 | */ |
| 15 | uint16_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 | |
| 26 | void skip_bytes (FILE *fp, int n) |
| 27 | { |
| 28 | while (n-- > 0) |
| 29 | fgetc (fp); |
| 30 | } |
| 31 | |
Peter Tyser | f4a193b | 2009-04-24 15:59:56 -0500 | [diff] [blame] | 32 | __attribute__ ((__noreturn__)) |
| 33 | int error (char * msg, FILE *fp) |
| 34 | { |
| 35 | fprintf (stderr, "ERROR: %s\n", msg); |
| 36 | |
| 37 | fclose (fp); |
| 38 | |
| 39 | exit (EXIT_FAILURE); |
| 40 | } |
| 41 | |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 42 | int main (int argc, char *argv[]) |
| 43 | { |
| 44 | int i, x; |
| 45 | FILE *fp; |
| 46 | bitmap_t bmp; |
| 47 | bitmap_t *b = &bmp; |
Wolfgang Denk | 375a89f | 2005-09-25 15:39:00 +0200 | [diff] [blame] | 48 | uint16_t data_offset, n_colors; |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 49 | |
| 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 Tyser | f4a193b | 2009-04-24 15:59:56 -0500 | [diff] [blame] | 60 | if (fgetc (fp) != 'B' || fgetc (fp) != 'M') |
| 61 | error ("Input file is not a bitmap", fp); |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 62 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 63 | /* |
| 64 | * read width and height of the image, and the number of colors used; |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 65 | * ignore the rest |
| 66 | */ |
Wolfgang Denk | 375a89f | 2005-09-25 15:39:00 +0200 | [diff] [blame] | 67 | skip_bytes (fp, 8); |
Peter Tyser | f4a193b | 2009-04-24 15:59:56 -0500 | [diff] [blame] | 68 | if (fread (&data_offset, sizeof (uint16_t), 1, fp) != 1) |
| 69 | error ("Couldn't read bitmap data offset", fp); |
Wolfgang Denk | 375a89f | 2005-09-25 15:39:00 +0200 | [diff] [blame] | 70 | skip_bytes (fp, 6); |
Peter Tyser | f4a193b | 2009-04-24 15:59:56 -0500 | [diff] [blame] | 71 | if (fread (&b->width, sizeof (uint16_t), 1, fp) != 1) |
| 72 | error ("Couldn't read bitmap width", fp); |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 73 | skip_bytes (fp, 2); |
Peter Tyser | f4a193b | 2009-04-24 15:59:56 -0500 | [diff] [blame] | 74 | if (fread (&b->height, sizeof (uint16_t), 1, fp) != 1) |
| 75 | error ("Couldn't read bitmap height", fp); |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 76 | skip_bytes (fp, 22); |
Peter Tyser | f4a193b | 2009-04-24 15:59:56 -0500 | [diff] [blame] | 77 | if (fread (&n_colors, sizeof (uint16_t), 1, fp) != 1) |
| 78 | error ("Couldn't read bitmap colors", fp); |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 79 | skip_bytes (fp, 6); |
| 80 | |
| 81 | /* |
| 82 | * Repair endianess. |
| 83 | */ |
Wolfgang Denk | 375a89f | 2005-09-25 15:39:00 +0200 | [diff] [blame] | 84 | data_offset = le_short(data_offset); |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 85 | 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 Tyser | f4a193b | 2009-04-24 15:59:56 -0500 | [diff] [blame] | 112 | if ((b->data = (uint8_t *)malloc(b->width * b->height)) == NULL) |
| 113 | error ("Error allocating memory for file", fp); |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 114 | |
| 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 | |
wdenk | 9ca7bbc | 2004-10-09 23:25:58 +0000 | [diff] [blame] | 124 | printf ("%s0x0%X%X%X,%s", |
| 125 | ((i%8) == 0) ? "\t" : " ", |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 126 | (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, |
wdenk | 9ca7bbc | 2004-10-09 23:25:58 +0000 | [diff] [blame] | 129 | ((i%8) == 7) ? "\n" : "" |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 130 | ); |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Wolfgang Denk | 375a89f | 2005-09-25 15:39:00 +0200 | [diff] [blame] | 133 | /* seek to offset indicated by file header */ |
| 134 | fseek(fp, (long)data_offset, SEEK_SET); |
| 135 | |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 136 | /* 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 | } |