I’m having many problems trying to load an image file (PIXELIMAGEFORMAT). The code does not get past comparing the magic header value to PIXELIMAGEFORMAT (without string ending character)
My Image Format is:
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)
My Image Loading Code is:
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;
}
My Main Function is:
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");
}
Currently what it SHOULD do is print out the char values of each binary pixel.
If you want, I can post my current image file as well.
Thanks!
- Adrian Collado
You compare the address of the buffers, and not the buffers themselves, you should use
memcmp:This is not the answer, but should also be considered:
You compare addresses, while you should compare the value:
Also, since NULL may be returned , I would change the order of checks: