Mike Frysinger | 3756609 | 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 | |
Che-Liang Chiou | c270730 | 2011-10-20 23:04:20 +0000 | [diff] [blame] | 3 | enum { |
| 4 | MODE_GEN_INFO, |
| 5 | MODE_GEN_DATA |
| 6 | }; |
| 7 | |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 8 | typedef struct bitmap_s { /* bitmap description */ |
| 9 | uint16_t width; |
| 10 | uint16_t height; |
| 11 | uint8_t palette[256*3]; |
| 12 | uint8_t *data; |
| 13 | } bitmap_t; |
| 14 | |
| 15 | #define DEFAULT_CMAP_SIZE 16 /* size of default color map */ |
| 16 | |
Che-Liang Chiou | c270730 | 2011-10-20 23:04:20 +0000 | [diff] [blame] | 17 | void usage(const char *prog) |
| 18 | { |
| 19 | fprintf(stderr, "Usage: %s [--gen-info|--gen-data] file\n", prog); |
| 20 | } |
| 21 | |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 22 | /* |
| 23 | * Neutralize little endians. |
| 24 | */ |
| 25 | uint16_t le_short(uint16_t x) |
| 26 | { |
| 27 | uint16_t val; |
| 28 | uint8_t *p = (uint8_t *)(&x); |
| 29 | |
| 30 | val = (*p++ & 0xff) << 0; |
| 31 | val |= (*p & 0xff) << 8; |
| 32 | |
| 33 | return val; |
| 34 | } |
| 35 | |
| 36 | void skip_bytes (FILE *fp, int n) |
| 37 | { |
| 38 | while (n-- > 0) |
| 39 | fgetc (fp); |
| 40 | } |
| 41 | |
Peter Tyser | 65351a8 | 2009-04-24 15:59:56 -0500 | [diff] [blame] | 42 | __attribute__ ((__noreturn__)) |
| 43 | int error (char * msg, FILE *fp) |
| 44 | { |
| 45 | fprintf (stderr, "ERROR: %s\n", msg); |
| 46 | |
| 47 | fclose (fp); |
| 48 | |
| 49 | exit (EXIT_FAILURE); |
| 50 | } |
| 51 | |
Che-Liang Chiou | c270730 | 2011-10-20 23:04:20 +0000 | [diff] [blame] | 52 | void gen_info(bitmap_t *b, uint16_t n_colors) |
| 53 | { |
| 54 | printf("/*\n" |
| 55 | " * Automatically generated by \"tools/bmp_logo\"\n" |
| 56 | " *\n" |
| 57 | " * DO NOT EDIT\n" |
| 58 | " *\n" |
| 59 | " */\n\n\n" |
| 60 | "#ifndef __BMP_LOGO_H__\n" |
| 61 | "#define __BMP_LOGO_H__\n\n" |
| 62 | "#define BMP_LOGO_WIDTH\t\t%d\n" |
| 63 | "#define BMP_LOGO_HEIGHT\t\t%d\n" |
| 64 | "#define BMP_LOGO_COLORS\t\t%d\n" |
| 65 | "#define BMP_LOGO_OFFSET\t\t%d\n\n" |
| 66 | "extern unsigned short bmp_logo_palette[];\n" |
| 67 | "extern unsigned char bmp_logo_bitmap[];\n\n" |
| 68 | "#endif /* __BMP_LOGO_H__ */\n", |
| 69 | b->width, b->height, n_colors, |
| 70 | DEFAULT_CMAP_SIZE); |
| 71 | } |
| 72 | |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 73 | int main (int argc, char *argv[]) |
| 74 | { |
Che-Liang Chiou | c270730 | 2011-10-20 23:04:20 +0000 | [diff] [blame] | 75 | int mode, i, x; |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 76 | FILE *fp; |
| 77 | bitmap_t bmp; |
| 78 | bitmap_t *b = &bmp; |
Wolfgang Denk | fd4bb67 | 2005-09-25 15:39:00 +0200 | [diff] [blame] | 79 | uint16_t data_offset, n_colors; |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 80 | |
Che-Liang Chiou | c270730 | 2011-10-20 23:04:20 +0000 | [diff] [blame] | 81 | if (argc < 3) { |
| 82 | usage(argv[0]); |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 83 | exit (EXIT_FAILURE); |
| 84 | } |
| 85 | |
Che-Liang Chiou | c270730 | 2011-10-20 23:04:20 +0000 | [diff] [blame] | 86 | if (!strcmp(argv[1], "--gen-info")) |
| 87 | mode = MODE_GEN_INFO; |
| 88 | else if (!strcmp(argv[1], "--gen-data")) |
| 89 | mode = MODE_GEN_DATA; |
| 90 | else { |
| 91 | usage(argv[0]); |
| 92 | exit(EXIT_FAILURE); |
| 93 | } |
| 94 | |
| 95 | fp = fopen(argv[2], "rb"); |
| 96 | if (!fp) { |
| 97 | perror(argv[2]); |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 98 | exit (EXIT_FAILURE); |
| 99 | } |
| 100 | |
Peter Tyser | 65351a8 | 2009-04-24 15:59:56 -0500 | [diff] [blame] | 101 | if (fgetc (fp) != 'B' || fgetc (fp) != 'M') |
| 102 | error ("Input file is not a bitmap", fp); |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 103 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 104 | /* |
| 105 | * read width and height of the image, and the number of colors used; |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 106 | * ignore the rest |
| 107 | */ |
Wolfgang Denk | fd4bb67 | 2005-09-25 15:39:00 +0200 | [diff] [blame] | 108 | skip_bytes (fp, 8); |
Peter Tyser | 65351a8 | 2009-04-24 15:59:56 -0500 | [diff] [blame] | 109 | if (fread (&data_offset, sizeof (uint16_t), 1, fp) != 1) |
| 110 | error ("Couldn't read bitmap data offset", fp); |
Wolfgang Denk | fd4bb67 | 2005-09-25 15:39:00 +0200 | [diff] [blame] | 111 | skip_bytes (fp, 6); |
Peter Tyser | 65351a8 | 2009-04-24 15:59:56 -0500 | [diff] [blame] | 112 | if (fread (&b->width, sizeof (uint16_t), 1, fp) != 1) |
| 113 | error ("Couldn't read bitmap width", fp); |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 114 | skip_bytes (fp, 2); |
Peter Tyser | 65351a8 | 2009-04-24 15:59:56 -0500 | [diff] [blame] | 115 | if (fread (&b->height, sizeof (uint16_t), 1, fp) != 1) |
| 116 | error ("Couldn't read bitmap height", fp); |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 117 | skip_bytes (fp, 22); |
Peter Tyser | 65351a8 | 2009-04-24 15:59:56 -0500 | [diff] [blame] | 118 | if (fread (&n_colors, sizeof (uint16_t), 1, fp) != 1) |
| 119 | error ("Couldn't read bitmap colors", fp); |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 120 | skip_bytes (fp, 6); |
| 121 | |
| 122 | /* |
| 123 | * Repair endianess. |
| 124 | */ |
Wolfgang Denk | fd4bb67 | 2005-09-25 15:39:00 +0200 | [diff] [blame] | 125 | data_offset = le_short(data_offset); |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 126 | b->width = le_short(b->width); |
| 127 | b->height = le_short(b->height); |
| 128 | n_colors = le_short(n_colors); |
| 129 | |
| 130 | /* assume we are working with an 8-bit file */ |
| 131 | if ((n_colors == 0) || (n_colors > 256 - DEFAULT_CMAP_SIZE)) { |
| 132 | /* reserve DEFAULT_CMAP_SIZE color map entries for default map */ |
| 133 | n_colors = 256 - DEFAULT_CMAP_SIZE; |
| 134 | } |
| 135 | |
Che-Liang Chiou | c270730 | 2011-10-20 23:04:20 +0000 | [diff] [blame] | 136 | if (mode == MODE_GEN_INFO) { |
| 137 | gen_info(b, n_colors); |
| 138 | goto out; |
| 139 | } |
| 140 | |
| 141 | printf("/*\n" |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 142 | " * Automatically generated by \"tools/bmp_logo\"\n" |
| 143 | " *\n" |
| 144 | " * DO NOT EDIT\n" |
| 145 | " *\n" |
| 146 | " */\n\n\n" |
Che-Liang Chiou | c270730 | 2011-10-20 23:04:20 +0000 | [diff] [blame] | 147 | "#ifndef __BMP_LOGO_DATA_H__\n" |
| 148 | "#define __BMP_LOGO_DATA_H__\n\n"); |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 149 | |
| 150 | /* allocate memory */ |
Peter Tyser | 65351a8 | 2009-04-24 15:59:56 -0500 | [diff] [blame] | 151 | if ((b->data = (uint8_t *)malloc(b->width * b->height)) == NULL) |
| 152 | error ("Error allocating memory for file", fp); |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 153 | |
| 154 | /* read and print the palette information */ |
Che-Liang Chiou | c270730 | 2011-10-20 23:04:20 +0000 | [diff] [blame] | 155 | printf("unsigned short bmp_logo_palette[] = {\n"); |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 156 | |
| 157 | for (i=0; i<n_colors; ++i) { |
| 158 | b->palette[(int)(i*3+2)] = fgetc(fp); |
| 159 | b->palette[(int)(i*3+1)] = fgetc(fp); |
| 160 | b->palette[(int)(i*3+0)] = fgetc(fp); |
| 161 | x=fgetc(fp); |
| 162 | |
wdenk | 8655b6f | 2004-10-09 23:25:58 +0000 | [diff] [blame] | 163 | printf ("%s0x0%X%X%X,%s", |
| 164 | ((i%8) == 0) ? "\t" : " ", |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 165 | (b->palette[(int)(i*3+0)] >> 4) & 0x0F, |
| 166 | (b->palette[(int)(i*3+1)] >> 4) & 0x0F, |
| 167 | (b->palette[(int)(i*3+2)] >> 4) & 0x0F, |
wdenk | 8655b6f | 2004-10-09 23:25:58 +0000 | [diff] [blame] | 168 | ((i%8) == 7) ? "\n" : "" |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 169 | ); |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Wolfgang Denk | fd4bb67 | 2005-09-25 15:39:00 +0200 | [diff] [blame] | 172 | /* seek to offset indicated by file header */ |
| 173 | fseek(fp, (long)data_offset, SEEK_SET); |
| 174 | |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 175 | /* read the bitmap; leave room for default color map */ |
| 176 | printf ("\n"); |
| 177 | printf ("};\n"); |
| 178 | printf ("\n"); |
Che-Liang Chiou | c270730 | 2011-10-20 23:04:20 +0000 | [diff] [blame] | 179 | printf("unsigned char bmp_logo_bitmap[] = {\n"); |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 180 | for (i=(b->height-1)*b->width; i>=0; i-=b->width) { |
| 181 | for (x = 0; x < b->width; x++) { |
| 182 | b->data[(uint16_t) i + x] = (uint8_t) fgetc (fp) \ |
| 183 | + DEFAULT_CMAP_SIZE; |
| 184 | } |
| 185 | } |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 186 | |
| 187 | for (i=0; i<(b->height*b->width); ++i) { |
| 188 | if ((i%8) == 0) |
| 189 | putchar ('\t'); |
| 190 | printf ("0x%02X,%c", |
| 191 | b->data[i], |
| 192 | ((i%8) == 7) ? '\n' : ' ' |
| 193 | ); |
| 194 | } |
| 195 | printf ("\n" |
| 196 | "};\n\n" |
Che-Liang Chiou | c270730 | 2011-10-20 23:04:20 +0000 | [diff] [blame] | 197 | "#endif /* __BMP_LOGO_DATA_H__ */\n" |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 198 | ); |
| 199 | |
Che-Liang Chiou | c270730 | 2011-10-20 23:04:20 +0000 | [diff] [blame] | 200 | out: |
| 201 | fclose(fp); |
| 202 | return 0; |
wdenk | 0bbe200 | 2002-03-14 10:00:31 +0000 | [diff] [blame] | 203 | } |