bmp_logo.c 3.8 KB

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