Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- typedef unsigned char BYTE;
- inline int bmpEncoder(const std::string &location,
- const unsigned __int32 &width, const unsigned __int32 &height,
- const std::vector<BYTE> &buffer,
- const bool &hasAlphaChannel = true) {
- std::ofstream fout(location, std::ios::out | std::ios::binary);
- if (fout.fail()) {
- return 0;
- }
- //Padding
- const unsigned __int8 padding = hasAlphaChannel ? 0 : (4 - (width * 3) % 4) % 4;
- //Bitmap file header.
- const char signature[2] = { 'B', 'M' };
- const unsigned __int32 fileSize = buffer.size() * sizeof(BYTE) + padding * (height - 1) + 14 + 124;
- const unsigned __int32 offset = 14 + 124;
- //Bitmap information header file
- const unsigned __int32 DIBSize = 124;
- const __int32 bitmapWidth = width;
- const __int32 bitmapHeight = height;
- const unsigned __int16 numPlanes = 1;
- const unsigned __int16 bitsPerPixel = (hasAlphaChannel) ? 32 : 24;
- const unsigned __int32 compressionMethod = (hasAlphaChannel) ? 3 : 0; //BI_RGB = 0, BI_BITFIELDS = 3
- const unsigned __int32 bitmapSize = buffer.size() * sizeof(BYTE);
- const __int32 horizontalResolution = 2834;
- const __int32 verticalResolution = 2834;
- const unsigned __int32 numColors = 0;
- const unsigned __int32 impColorCount = 0;
- const unsigned __int32 redBitmask = (hasAlphaChannel) ? 0x0000FF00 : 0; //ARGB32 pixel format
- const unsigned __int32 greenBitmask = (hasAlphaChannel) ? 0x00FF0000 : 0;
- const unsigned __int32 blueBitmask = (hasAlphaChannel) ? 0xFF000000 : 0;
- const unsigned __int32 alphaBitmask = (hasAlphaChannel) ? 0x000000FF : 0;
- //Writing the file header and information header to the file
- std::vector<BYTE> header(offset, 0);
- header[0] = signature[0];
- header[1] = signature[1];
- #define HEADERS(i, variableName) header[i] = variableName; header[i+1] = variableName >> 8; header[i+2] = variableName >> 16; header[i+3] = variableName >> 24;
- HEADERS(2, fileSize);
- HEADERS(6, 0);
- HEADERS(10, offset);
- HEADERS(14, DIBSize);
- HEADERS(18, bitmapWidth);
- HEADERS(22, bitmapHeight);
- header[26] = (BYTE)numPlanes;
- header[27] = (BYTE)(numPlanes >> 8);
- header[28] = (BYTE)bitsPerPixel;
- header[29] = (BYTE)(bitsPerPixel >> 8);
- HEADERS(30, compressionMethod);
- HEADERS(34, bitmapSize);
- HEADERS(38, horizontalResolution);
- HEADERS(42, verticalResolution);
- HEADERS(46, numColors);
- HEADERS(50, impColorCount);
- HEADERS(54, redBitmask);
- HEADERS(58, greenBitmask);
- HEADERS(62, blueBitmask);
- HEADERS(66, alphaBitmask);
- #undef HEADERS
- fout.write((char *)header.data(), sizeof(BYTE) * header.size());
- //Writing the pixel array
- const unsigned __int32 bWidth = bitsPerPixel / 8 * width;
- for (int i = height - 1; i >= 0; i--) {
- std::vector<BYTE> row(buffer.begin() + i * bWidth, buffer.begin() + i * bWidth + bWidth);
- fout.write((char *)row.data(), row.size() * sizeof(BYTE));
- fout.seekp(padding * sizeof(BYTE), std::ios::cur);
- }
- fout.close();
- return 1;
- }
Add Comment
Please, Sign In to add comment