bmp_logo.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #include "compiler.h"
  2. typedef struct bitmap_s { /* bitmap description */
  3. uint16_t width;
  4. uint16_t height;
  5. uint8_t palette[256*3];
  6. uint8_t *data;
  7. } bitmap_t;
  8. #define DEFAULT_CMAP_SIZE 16 /* size of default color map */
  9. /*
  10. * Neutralize little endians.
  11. */
  12. uint16_t le_short(uint16_t x)
  13. {
  14. uint16_t val;
  15. uint8_t *p = (uint8_t *)(&x);
  16. val = (*p++ & 0xff) << 0;
  17. val |= (*p & 0xff) << 8;
  18. return val;
  19. }
  20. void skip_bytes (FILE *fp, int n)
  21. {
  22. while (n-- > 0)
  23. fgetc (fp);
  24. }
  25. __attribute__ ((__noreturn__))
  26. int error (char * msg, FILE *fp)
  27. {
  28. fprintf (stderr, "ERROR: %s\n", msg);
  29. fclose (fp);
  30. exit (EXIT_FAILURE);
  31. }
  32. int main (int argc, char *argv[])
  33. {
  34. int i, x;
  35. FILE *fp;
  36. bitmap_t bmp;
  37. bitmap_t *b = &bmp;
  38. uint16_t data_offset, n_colors;
  39. if (argc < 2) {
  40. fprintf (stderr, "Usage: %s file\n", argv[0]);
  41. exit (EXIT_FAILURE);
  42. }
  43. if ((fp = fopen (argv[1], "rb")) == NULL) {
  44. perror (argv[1]);
  45. exit (EXIT_FAILURE);
  46. }
  47. if (fgetc (fp) != 'B' || fgetc (fp) != 'M')
  48. error ("Input file is not a bitmap", fp);
  49. /*
  50. * read width and height of the image, and the number of colors used;
  51. * ignore the rest
  52. */
  53. skip_bytes (fp, 8);
  54. if (fread (&data_offset, sizeof (uint16_t), 1, fp) != 1)
  55. error ("Couldn't read bitmap data offset", fp);
  56. skip_bytes (fp, 6);
  57. if (fread (&b->width, sizeof (uint16_t), 1, fp) != 1)
  58. error ("Couldn't read bitmap width", fp);
  59. skip_bytes (fp, 2);
  60. if (fread (&b->height, sizeof (uint16_t), 1, fp) != 1)
  61. error ("Couldn't read bitmap height", fp);
  62. skip_bytes (fp, 22);
  63. if (fread (&n_colors, sizeof (uint16_t), 1, fp) != 1)
  64. error ("Couldn't read bitmap colors", fp);
  65. skip_bytes (fp, 6);
  66. /*
  67. * Repair endianess.
  68. */
  69. data_offset = le_short(data_offset);
  70. b->width = le_short(b->width);
  71. b->height = le_short(b->height);
  72. n_colors = le_short(n_colors);
  73. /* assume we are working with an 8-bit file */
  74. if ((n_colors == 0) || (n_colors > 256 - DEFAULT_CMAP_SIZE)) {
  75. /* reserve DEFAULT_CMAP_SIZE color map entries for default map */
  76. n_colors = 256 - DEFAULT_CMAP_SIZE;
  77. }
  78. printf ("/*\n"
  79. " * Automatically generated by \"tools/bmp_logo\"\n"
  80. " *\n"
  81. " * DO NOT EDIT\n"
  82. " *\n"
  83. " */\n\n\n"
  84. "#ifndef __BMP_LOGO_H__\n"
  85. "#define __BMP_LOGO_H__\n\n"
  86. "#define BMP_LOGO_WIDTH\t\t%d\n"
  87. "#define BMP_LOGO_HEIGHT\t\t%d\n"
  88. "#define BMP_LOGO_COLORS\t\t%d\n"
  89. "#define BMP_LOGO_OFFSET\t\t%d\n"
  90. "\n",
  91. b->width, b->height, n_colors,
  92. DEFAULT_CMAP_SIZE);
  93. /* allocate memory */
  94. if ((b->data = (uint8_t *)malloc(b->width * b->height)) == NULL)
  95. error ("Error allocating memory for file", fp);
  96. /* read and print the palette information */
  97. printf ("unsigned short bmp_logo_palette[] = {\n");
  98. for (i=0; i<n_colors; ++i) {
  99. b->palette[(int)(i*3+2)] = fgetc(fp);
  100. b->palette[(int)(i*3+1)] = fgetc(fp);
  101. b->palette[(int)(i*3+0)] = fgetc(fp);
  102. x=fgetc(fp);
  103. printf ("%s0x0%X%X%X,%s",
  104. ((i%8) == 0) ? "\t" : " ",
  105. (b->palette[(int)(i*3+0)] >> 4) & 0x0F,
  106. (b->palette[(int)(i*3+1)] >> 4) & 0x0F,
  107. (b->palette[(int)(i*3+2)] >> 4) & 0x0F,
  108. ((i%8) == 7) ? "\n" : ""
  109. );
  110. }
  111. /* seek to offset indicated by file header */
  112. fseek(fp, (long)data_offset, SEEK_SET);
  113. /* read the bitmap; leave room for default color map */
  114. printf ("\n");
  115. printf ("};\n");
  116. printf ("\n");
  117. printf ("unsigned char bmp_logo_bitmap[] = {\n");
  118. for (i=(b->height-1)*b->width; i>=0; i-=b->width) {
  119. for (x = 0; x < b->width; x++) {
  120. b->data[(uint16_t) i + x] = (uint8_t) fgetc (fp) \
  121. + DEFAULT_CMAP_SIZE;
  122. }
  123. }
  124. fclose (fp);
  125. for (i=0; i<(b->height*b->width); ++i) {
  126. if ((i%8) == 0)
  127. putchar ('\t');
  128. printf ("0x%02X,%c",
  129. b->data[i],
  130. ((i%8) == 7) ? '\n' : ' '
  131. );
  132. }
  133. printf ("\n"
  134. "};\n\n"
  135. "#endif /* __BMP_LOGO_H__ */\n"
  136. );
  137. return (0);
  138. }