bmp.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include "bmp.h"
  2. void bmp_init(void *buf, long width, long height)
  3. {
  4. long pad;
  5. unsigned long size;
  6. unsigned long uw = width;
  7. unsigned long uh = -height;
  8. unsigned char *p = (unsigned char *)buf;
  9. #ifdef BMP_COMPAT
  10. uh = height;
  11. #endif
  12. /* bfType */
  13. *p++ = 0x42;
  14. *p++ = 0x4D;
  15. /* bfSize */
  16. pad = (width * -3UL) & 3;
  17. size = height * (width * 3 + pad) + 14 + 40;
  18. *p++ = size >> 0;
  19. *p++ = size >> 8;
  20. *p++ = size >> 16;
  21. *p++ = size >> 24;
  22. /* bfReserved1 + bfReserved2 */
  23. *p++ = 0x00;
  24. *p++ = 0x00;
  25. *p++ = 0x00;
  26. *p++ = 0x00;
  27. /* bfOffBits */
  28. *p++ = 0x36;
  29. *p++ = 0x00;
  30. *p++ = 0x00;
  31. *p++ = 0x00;
  32. /* biSize */
  33. *p++ = 0x28;
  34. *p++ = 0x00;
  35. *p++ = 0x00;
  36. *p++ = 0x00;
  37. /* biWidth */
  38. *p++ = uw >> 0;
  39. *p++ = uw >> 8;
  40. *p++ = uw >> 16;
  41. *p++ = uw >> 24;
  42. /* biHeight */
  43. *p++ = uh >> 0;
  44. *p++ = uh >> 8;
  45. *p++ = uh >> 16;
  46. *p++ = uh >> 24;
  47. /* biPlanes */
  48. *p++ = 0x01;
  49. *p++ = 0x00;
  50. /* biBitCount */
  51. *p++ = 0x18;
  52. *p++ = 0x00;
  53. /* biCompression */
  54. *p++ = 0x00;
  55. *p++ = 0x00;
  56. *p++ = 0x00;
  57. *p++ = 0x00;
  58. /* biSizeImage */
  59. *p++ = 0x00;
  60. *p++ = 0x00;
  61. *p++ = 0x00;
  62. *p++ = 0x00;
  63. /* biXPelsPerMeter */
  64. *p++ = 0x00;
  65. *p++ = 0x00;
  66. *p++ = 0x00;
  67. *p++ = 0x00;
  68. /* biYPelsPerMeter */
  69. *p++ = 0x00;
  70. *p++ = 0x00;
  71. *p++ = 0x00;
  72. *p++ = 0x00;
  73. /* biClrUsed */
  74. *p++ = 0x00;
  75. *p++ = 0x00;
  76. *p++ = 0x00;
  77. *p++ = 0x00;
  78. /* biClrImportant */
  79. *p++ = 0x00;
  80. *p++ = 0x00;
  81. *p++ = 0x00;
  82. *p = 0x00;
  83. }
  84. void bmp_set(void *buf, long x, long y, unsigned long color)
  85. {
  86. unsigned char *p;
  87. unsigned char *hdr = (unsigned char *)buf;
  88. unsigned long width =
  89. (unsigned long)hdr[18] << 0 |
  90. (unsigned long)hdr[19] << 8 |
  91. (unsigned long)hdr[20] << 16 |
  92. (unsigned long)hdr[21] << 24;
  93. long pad = (width * -3UL) & 3;
  94. #ifdef BMP_COMPAT
  95. unsigned long height =
  96. (unsigned long)hdr[22] << 0 |
  97. (unsigned long)hdr[23] << 8 |
  98. (unsigned long)hdr[24] << 16 |
  99. (unsigned long)hdr[25] << 24;
  100. y = height - y - 1;
  101. #endif
  102. p = hdr + 14 + 40 + y * (width * 3 + pad) + x * 3;
  103. p[0] = color >> 0;
  104. p[1] = color >> 8;
  105. p[2] = color >> 16;
  106. }
  107. unsigned long bmp_encode(unsigned long color_hex)
  108. {
  109. unsigned char r = (color_hex >> 16) & 0xFF;
  110. unsigned char g = (color_hex >> 8) & 0xFF;
  111. unsigned char b = color_hex & 0xFF;
  112. return b | g << 8 | r << 16;
  113. }