bmp_logo.c 4.9 KB

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