bmp_logo.c 3.6 KB

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