好的,回答我自己的问题。显然,答案是“我看起来不够努力”:D。
在关于 BMP 图像文件格式的Wikipedia article 中,有一个 4x2 ARGB 图像的示例,其所有字段都清楚地解释了。
谢谢@Weather Vane 的提示。
但是,我确实发现其中的数据存在问题。 MacOS 的预览版不喜欢具有特定格式的特定图像。我对文件进行了一些更改,并成功生成了一个 4x2 ARGB 位图,可以在 MacOS 和 Windows 上查看和使用。
以下是我用来生成BMP文件的代码,供以后参考:
#include <iostream>
#include <fstream>
unsigned char bmpData[] = // All values are little-endian
{
0x42, 0x4D, // Signature 'BM'
0xaa, 0x00, 0x00, 0x00, // Size: 170 bytes
0x00, 0x00, // Unused
0x00, 0x00, // Unused
0x8a, 0x00, 0x00, 0x00, // Offset to image data
0x7c, 0x00, 0x00, 0x00, // DIB header size (124 bytes)
0x04, 0x00, 0x00, 0x00, // Width (4px)
0x02, 0x00, 0x00, 0x00, // Height (2px)
0x01, 0x00, // Planes (1)
0x20, 0x00, // Bits per pixel (32)
0x03, 0x00, 0x00, 0x00, // Format (bitfield = use bitfields | no compression)
0x20, 0x00, 0x00, 0x00, // Image raw size (32 bytes)
0x13, 0x0B, 0x00, 0x00, // Horizontal print resolution (2835 = 72dpi * 39.3701)
0x13, 0x0B, 0x00, 0x00, // Vertical print resolution (2835 = 72dpi * 39.3701)
0x00, 0x00, 0x00, 0x00, // Colors in palette (none)
0x00, 0x00, 0x00, 0x00, // Important colors (0 = all)
0x00, 0x00, 0xFF, 0x00, // R bitmask (00FF0000)
0x00, 0xFF, 0x00, 0x00, // G bitmask (0000FF00)
0xFF, 0x00, 0x00, 0x00, // B bitmask (000000FF)
0x00, 0x00, 0x00, 0xFF, // A bitmask (FF000000)
0x42, 0x47, 0x52, 0x73, // sRGB color space
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Unused R, G, B entries for color space
0x00, 0x00, 0x00, 0x00, // Unused Gamma X entry for color space
0x00, 0x00, 0x00, 0x00, // Unused Gamma Y entry for color space
0x00, 0x00, 0x00, 0x00, // Unused Gamma Z entry for color space
0x00, 0x00, 0x00, 0x00, // Unknown
0x00, 0x00, 0x00, 0x00, // Unknown
0x00, 0x00, 0x00, 0x00, // Unknown
0x00, 0x00, 0x00, 0x00, // Unknown
// Image data:
0xFF, 0x00, 0x00, 0x7F, // Bottom left pixel
0x00, 0xFF, 0x00, 0x7F,
0x00, 0x00, 0xFF, 0x7F,
0xFF, 0xFF, 0xFF, 0x7F, // Bottom right pixel
0xFF, 0x00, 0x00, 0xFF, // Top left pixel
0x00, 0xFF, 0x00, 0xFF,
0x00, 0x00, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF // Top right pixel
};
int main(int argc, const char * argv[])
{
std::fstream fs("test.bmp", std::ios_base::out | std::ios_base::binary);
fs.write((const char *)bmpData, sizeof(bmpData));
fs.close();
std::cout << "The BMP has been written.\n";
return 0;
}