我在尝试加载图像文件时遇到很多问题(PIXELIMAGEFORMAT)。代码没有通过比较魔术标头值与PIXELIMAGEFORMAT
(没有字符串结束字符)
我的图片格式为:
Bytes 0-15: PIXELIMAGEFORMAT (The Magic Header Value)
Bytes 16-17: Width (Formatted as 0000 xxxx xxxx xxxx)
Bytes 18-19: Height (Formatted as 0000 xxxx xxxx xxxx)
Bytes 20-23: Bits Per Pixel (Formatted as 1000 1000 1000 1000)
Bytes 24-31: NULL (All 0's)
Bytes 32-END: 32-Bit RGBA (8 Bit Red, 8 Bit Green, 8 Bit Blue, 8 Bit Alpha)
我的图片加载代码是:
char* vimg_LoadPIXELIMAGE(char* filePath) {
FILE* file;
file = fopen(filePath, "rb");
if (file == NULL) return "a";
char* header = (char*)malloc(32);
fread(header, sizeof(char), 32, file);
char* magicHeader = (char*)malloc(16);
const char magic[] = {
'P', 'I', 'X', 'E', 'L',
'I', 'M', 'A', 'G', 'E',
'F', 'O', 'R', 'M', 'A', 'T'
};
strncpy(magicHeader, header, 16);
if (magicHeader != magic) return "b";
unsigned short width;
unsigned short height;
memcpy(&width, header + 16, 2);
memcpy(&height, header + 18, 2);
unsigned int fileSize = width * height;
char* fullbuffer = (char*)malloc(fileSize+32);
char* buffer = (char*)malloc(fileSize);
fread(fullbuffer, 1, fileSize + 32, file);
memcpy(buffer, fullbuffer + 32, fileSize);
return buffer;
}
我的主要职能是:
void main(int argc, char* argv) {
char* imgSRC;
imgSRC = vimg_LoadPIXELIMAGE("img.pfi");
if (imgSRC == "a")
printf("File Is Null!\n");
else if (imgSRC == "b")
printf("File Is Not a PIXELIMAGE!\n");
else if (imgSRC == NULL)
printf("SEVERE ERROR!!!\n");
else
printf(imgSRC);
system("pause");
}
目前应该做的是打印出每个二进制像素的char值。
如果您愿意,我也可以发布当前的图像文件。
谢谢!
答案 0 :(得分:1)
您要比较缓冲区的地址,而不是缓冲区本身,您应该使用memcmp
:
if (memcmp(magicHeader, magic, 16) != 0) return "b";
这不是答案,但也应该考虑:
您比较地址,而您应该比较值:
if (*imgSRC == 'a')
此外,由于可能会返回NULL,我会更改检查的顺序:
if (imgSRC == NULL)
printf("SEVERE ERROR!!!\n");
else if (*imgSRC == 'a')
printf("File Is Null!\n");
else if (*imgSRC == 'a')
printf("File Is Not a PIXELIMAGE!\n");
else
printf(imgSRC);