bmp_logo.c 3.3 KB

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