Tu masz poprawiony kod:
#include <cstdint>
#include <cstring>
#include <fstream>
#include <iostream>
#include <vector>
#pragma pack(push)
#pragma pack(1)
struct BmpHeader {
char bitmapSignatureBytes[2];
uint32_t sizeOfBitmapFile;
uint32_t reservedBytes;
uint32_t pixelDataOffset;
};
struct BmpInfoHeader {
uint32_t sizeOfThisHeader;
int32_t width;
int32_t height;
uint16_t numberOfColorPlanes;
uint16_t colorDepth;
uint32_t compressionMethod;
uint32_t rawBitmapDataSize;
int32_t horizontalResolution;
int32_t verticalResolution;
uint32_t colorTableEntries;
uint32_t importantColors;
};
struct Pixel {
uint8_t blue;
uint8_t green;
uint8_t red;
};
#pragma pack(pop)
void save24bitsBitmap(std::ostream &os, size_t w, size_t h, const Pixel* data)
{
BmpHeader bmpHdr;
BmpInfoHeader bmpInfo;
bmpInfo.sizeOfThisHeader = sizeof(BmpInfoHeader);
bmpInfo.width = w;
bmpInfo.height = h;
bmpInfo.numberOfColorPlanes = 1;
bmpInfo.colorDepth = 24;
bmpInfo.compressionMethod = 0;
bmpInfo.rawBitmapDataSize = 0;
bmpInfo.horizontalResolution = 3780;
bmpInfo.verticalResolution = 3780;
bmpInfo.colorTableEntries = 0;
bmpInfo.importantColors = 0;
size_t scanLineSize = 4 * ((24 * w + 31) / 32);
std::memcpy(bmpHdr.bitmapSignatureBytes, "BM", 2);
bmpHdr.pixelDataOffset = sizeof(bmpHdr) + sizeof(bmpInfo);
bmpHdr.sizeOfBitmapFile = bmpHdr.pixelDataOffset + scanLineSize * h;
bmpHdr.reservedBytes = 0;
os.write(reinterpret_cast<char*>(&bmpHdr), sizeof(BmpHeader));
os.write(reinterpret_cast<char*>(&bmpInfo), sizeof(BmpInfoHeader));
size_t widthInBytes = sizeof(Pixel) * w;
size_t paddingSize = scanLineSize - widthInBytes;
char pad[3] = { 0 };
while (h--) {
os.write(reinterpret_cast<const char*>(data), widthInBytes);
os.write(pad, paddingSize);
data += w;
}
}
int main(int argc, char** argv)
{
std::ofstream os("output.bmp", std::ios::binary);
Pixel v[100][100] = { 0, 0, 0 };
for(int i = 0; i < 100; ++i) {
v[i][50] = v[50][i] = { 255, 255, 255 };
}
save24bitsBitmap(os, 100, 100, v[0]);
return 0;
}
Nie wiem, czy dyrektywy #pragma będą działać na twoim kompilatorze, ale na g++ i VC działają, więc problemów być nie powinno.