CmBitmapWriter.cpp 3.5 KB

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