2
0

CmBitmapWriter.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include "CmBitmapWriter.h"
  2. namespace CamelotFramework
  3. {
  4. #pragma pack(push, 2) // Align to 2byte boundary so we don't get extra 2 bytes for this struct
  5. struct BMP_HEADER
  6. {
  7. UINT16 BM;
  8. UINT32 size_of_file;
  9. UINT32 reserve;
  10. UINT32 offset_of_pixel_data;
  11. UINT32 size_of_header;
  12. UINT32 width;
  13. UINT32 hight;
  14. UINT16 num_of_color_plane;
  15. UINT16 num_of_bit_per_pix;
  16. UINT32 compression;
  17. UINT32 size_of_pix_data;
  18. UINT32 h_resolution;
  19. UINT32 v_resolution;
  20. UINT32 num_of_color_in_palette;
  21. UINT32 important_colors;
  22. };
  23. #pragma pack(pop)
  24. void BitmapWriter::rawPixelsToBMP(const UINT8* input, UINT8* output, UINT32 width, UINT32 height, UINT32 bytesPerPixel)
  25. {
  26. UINT16 bmpBytesPerPixel = 3;
  27. if(bytesPerPixel >= 4)
  28. bmpBytesPerPixel = 4;
  29. UINT32 padding = (width * bmpBytesPerPixel) % 4;
  30. if(padding != 0)
  31. padding = 4 - padding;
  32. UINT32 rowPitch = (width * bmpBytesPerPixel) + padding;
  33. UINT32 dataSize = height * rowPitch;
  34. BMP_HEADER header;
  35. header.BM = 0x4d42;
  36. header.size_of_file = sizeof(header) + dataSize;
  37. header.reserve = 0000;
  38. header.offset_of_pixel_data = 54;
  39. header.size_of_header = 40;
  40. header.width = width;
  41. header.hight = height;
  42. header.num_of_color_plane = 1;
  43. header.num_of_bit_per_pix = bmpBytesPerPixel * 8;
  44. header.compression = 0;
  45. header.size_of_pix_data = dataSize;
  46. header.h_resolution = 2835;
  47. header.v_resolution = 2835;
  48. header.num_of_color_in_palette = 0;
  49. header.important_colors = 0;
  50. // Write header
  51. memcpy(output, &header, sizeof(header));
  52. output += sizeof(header);
  53. // Write bytes
  54. UINT32 widthBytes = width * bytesPerPixel;
  55. // BPP matches so we can just copy directly
  56. if(bmpBytesPerPixel == bytesPerPixel)
  57. {
  58. for(INT32 y = height - 1; y >= 0 ; y--)
  59. {
  60. UINT8* outputPtr = output + y * rowPitch;
  61. memcpy(outputPtr, input, widthBytes);
  62. memset(outputPtr + widthBytes, 0, padding);
  63. input += widthBytes;
  64. }
  65. }
  66. else if(bmpBytesPerPixel < bytesPerPixel) // More bytes in source than supported in BMP, just truncate excess data
  67. {
  68. for(INT32 y = height - 1; y >= 0 ; y--)
  69. {
  70. UINT8* outputPtr = output + y * rowPitch;
  71. for(UINT32 x = 0; x < width; x++)
  72. {
  73. memcpy(outputPtr, input, bmpBytesPerPixel);
  74. outputPtr += bmpBytesPerPixel;
  75. input += bytesPerPixel;
  76. }
  77. memset(outputPtr, 0, padding);
  78. }
  79. }
  80. else // More bytes in BMP than in source (BMP must be 24bit minimum)
  81. {
  82. for(INT32 y = height - 1; y >= 0 ; y--)
  83. {
  84. UINT8* outputPtr = output + y * rowPitch;
  85. for(UINT32 x = 0; x < width; x++)
  86. {
  87. memcpy(outputPtr, input, bytesPerPixel);
  88. // Fill the empty bytes with the last available byte from input
  89. UINT32 remainingBytes = bmpBytesPerPixel - bytesPerPixel;
  90. while(remainingBytes > 0)
  91. {
  92. memcpy(outputPtr + (bmpBytesPerPixel - remainingBytes), input, 1);
  93. remainingBytes--;
  94. }
  95. outputPtr += bmpBytesPerPixel;
  96. input += bytesPerPixel;
  97. }
  98. memset(outputPtr, 0, padding);
  99. }
  100. }
  101. }
  102. UINT32 BitmapWriter::getBMPSize(UINT32 width, UINT32 height, UINT32 bytesPerPixel)
  103. {
  104. UINT16 bmpBytesPerPixel = 3;
  105. if(bytesPerPixel >= 4)
  106. bmpBytesPerPixel = 4;
  107. UINT32 padding = (width * bmpBytesPerPixel) % 4;
  108. if(padding != 0)
  109. padding = 4 - padding;
  110. UINT32 rowPitch = (width * bmpBytesPerPixel) + padding;
  111. UINT32 dataSize = height * rowPitch;
  112. return sizeof(BMP_HEADER) + dataSize;
  113. }
  114. }