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