BsBitmapWriter.cpp 3.7 KB

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