Guest User

Untitled

a guest
May 24th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. typedef unsigned char BYTE;
  2.  
  3. inline int bmpEncoder(const std::string &location,
  4. const unsigned __int32 &width, const unsigned __int32 &height,
  5. const std::vector<BYTE> &buffer,
  6. const bool &hasAlphaChannel = true) {
  7.  
  8. std::ofstream fout(location, std::ios::out | std::ios::binary);
  9.  
  10. if (fout.fail()) {
  11. return 0;
  12. }
  13.  
  14. //Padding
  15. const unsigned __int8 padding = hasAlphaChannel ? 0 : (4 - (width * 3) % 4) % 4;
  16.  
  17. //Bitmap file header.
  18. const char signature[2] = { 'B', 'M' };
  19. const unsigned __int32 fileSize = buffer.size() * sizeof(BYTE) + padding * (height - 1) + 14 + 124;
  20. const unsigned __int32 offset = 14 + 124;
  21.  
  22. //Bitmap information header file
  23. const unsigned __int32 DIBSize = 124;
  24. const __int32 bitmapWidth = width;
  25. const __int32 bitmapHeight = height;
  26. const unsigned __int16 numPlanes = 1;
  27. const unsigned __int16 bitsPerPixel = (hasAlphaChannel) ? 32 : 24;
  28. const unsigned __int32 compressionMethod = (hasAlphaChannel) ? 3 : 0; //BI_RGB = 0, BI_BITFIELDS = 3
  29. const unsigned __int32 bitmapSize = buffer.size() * sizeof(BYTE);
  30. const __int32 horizontalResolution = 2834;
  31. const __int32 verticalResolution = 2834;
  32. const unsigned __int32 numColors = 0;
  33. const unsigned __int32 impColorCount = 0;
  34. const unsigned __int32 redBitmask = (hasAlphaChannel) ? 0x0000FF00 : 0; //ARGB32 pixel format
  35. const unsigned __int32 greenBitmask = (hasAlphaChannel) ? 0x00FF0000 : 0;
  36. const unsigned __int32 blueBitmask = (hasAlphaChannel) ? 0xFF000000 : 0;
  37. const unsigned __int32 alphaBitmask = (hasAlphaChannel) ? 0x000000FF : 0;
  38.  
  39. //Writing the file header and information header to the file
  40. std::vector<BYTE> header(offset, 0);
  41. header[0] = signature[0];
  42. header[1] = signature[1];
  43.  
  44. #define HEADERS(i, variableName) header[i] = variableName; header[i+1] = variableName >> 8; header[i+2] = variableName >> 16; header[i+3] = variableName >> 24;
  45.  
  46. HEADERS(2, fileSize);
  47. HEADERS(6, 0);
  48. HEADERS(10, offset);
  49. HEADERS(14, DIBSize);
  50. HEADERS(18, bitmapWidth);
  51. HEADERS(22, bitmapHeight);
  52.  
  53. header[26] = (BYTE)numPlanes;
  54. header[27] = (BYTE)(numPlanes >> 8);
  55. header[28] = (BYTE)bitsPerPixel;
  56. header[29] = (BYTE)(bitsPerPixel >> 8);
  57.  
  58. HEADERS(30, compressionMethod);
  59. HEADERS(34, bitmapSize);
  60. HEADERS(38, horizontalResolution);
  61. HEADERS(42, verticalResolution);
  62. HEADERS(46, numColors);
  63. HEADERS(50, impColorCount);
  64. HEADERS(54, redBitmask);
  65. HEADERS(58, greenBitmask);
  66. HEADERS(62, blueBitmask);
  67. HEADERS(66, alphaBitmask);
  68.  
  69. #undef HEADERS
  70.  
  71. fout.write((char *)header.data(), sizeof(BYTE) * header.size());
  72.  
  73. //Writing the pixel array
  74. const unsigned __int32 bWidth = bitsPerPixel / 8 * width;
  75.  
  76. for (int i = height - 1; i >= 0; i--) {
  77. std::vector<BYTE> row(buffer.begin() + i * bWidth, buffer.begin() + i * bWidth + bWidth);
  78. fout.write((char *)row.data(), row.size() * sizeof(BYTE));
  79. fout.seekp(padding * sizeof(BYTE), std::ios::cur);
  80. }
  81.  
  82. fout.close();
  83. return 1;
  84. }
Add Comment
Please, Sign In to add comment