bmp.c 2.5 KB

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