使用 Big-Endian 问题在 C++ 中加载位图
Loading Bitmap in C++ with Big-Endian issues
我目前正在使用以下代码读取 Visual Studio 中的位图:
unsigned char* imageIO::readBMP(char* filename) {
FILE* f = fopen(filename, "rb");
if (f == NULL)
{
printf("%s \n", "File Not Loaded");
}
unsigned char info[54];
fread(info, sizeof(unsigned char), 54, f); //read the 54-byte header
//extract image height and width from header
imageWidth = *(int*)&info[18];
imageHeight = *(int*)&info[22];
imageBytes = 3;
size = imageWidth * imageHeight * imageBytes;
unsigned char* readData = new unsigned char[size]; //allocate 3 byte per pixel
fread(readData, sizeof(unsigned char), size, f); //read the rest of the data at once
fclose(f);
return readData;
}
然而,我试图让这段代码在 PowerPC 上运行,但它从位图中提取了错误的宽度和高度 header。我认为这与 Little Endian(普通 PC)和 Big Endian(PowerPC)有关。
我应该如何让位图在 Big Endian 机器上读取?
我可以只翻转小端数据吗?
你可以这样做(它应该适用于大端或小端架构):
unsigned int getIntLE(const unsigned char *p) {
#if LITTLE_ENDIAN
return *(unsigned int *)p;
#else
return ((unsigned int)p[3] << 24) |
((unsigned int)p[2] << 16) |
((unsigned int)p[1] << 8) |
p[0];
#endif
}
// ...
imageWidth = getIntLE(&info[18]);
imageHeight = getIntLE(&info[22]);
请注意,您需要定义 LITTLE_ENDIAN
或使用 Visual Studio 预定义的内容。我手边没有 Windows 开发环境,无法了解那里使用的是什么。
我目前正在使用以下代码读取 Visual Studio 中的位图:
unsigned char* imageIO::readBMP(char* filename) {
FILE* f = fopen(filename, "rb");
if (f == NULL)
{
printf("%s \n", "File Not Loaded");
}
unsigned char info[54];
fread(info, sizeof(unsigned char), 54, f); //read the 54-byte header
//extract image height and width from header
imageWidth = *(int*)&info[18];
imageHeight = *(int*)&info[22];
imageBytes = 3;
size = imageWidth * imageHeight * imageBytes;
unsigned char* readData = new unsigned char[size]; //allocate 3 byte per pixel
fread(readData, sizeof(unsigned char), size, f); //read the rest of the data at once
fclose(f);
return readData;
}
然而,我试图让这段代码在 PowerPC 上运行,但它从位图中提取了错误的宽度和高度 header。我认为这与 Little Endian(普通 PC)和 Big Endian(PowerPC)有关。
我应该如何让位图在 Big Endian 机器上读取?
我可以只翻转小端数据吗?
你可以这样做(它应该适用于大端或小端架构):
unsigned int getIntLE(const unsigned char *p) {
#if LITTLE_ENDIAN
return *(unsigned int *)p;
#else
return ((unsigned int)p[3] << 24) |
((unsigned int)p[2] << 16) |
((unsigned int)p[1] << 8) |
p[0];
#endif
}
// ...
imageWidth = getIntLE(&info[18]);
imageHeight = getIntLE(&info[22]);
请注意,您需要定义 LITTLE_ENDIAN
或使用 Visual Studio 预定义的内容。我手边没有 Windows 开发环境,无法了解那里使用的是什么。