Hi!
I've posted earlier about reading and writing to bitmap image files and I've been able to successfully work with the 32x32 bitmap files that I was interested in.
Now, I want to deal with different sizes and ran into an issue with the padding from bitmap files.
I created a 33x32 image using MS Paint and tried to read it. I can't seem to get the right image... it seems distorted and I'm unable to figure out where the bug is. Any idea on what I need to fix, people?
Best Regards,
Tin
PS: I could post the whole code, if anyone wants that... I've traced the error to this section so I've only posted this bit.
This code fragment reads the file and removes the padding.
Code:
//Code fragment from trying to read a 33x32 image in 24-bit BMP format
//The approach works perfectly when there's no padding... I tried 32x32 and some other sizes with no padding
//I did use a #pragma pack(1)
struct rgbQuad {
unsigned char blue;
unsigned char green;
unsigned char red;
unsigned char reserved;
};
struct rgbQuad* bmpImgData2;
void* tempImgData;
padding = (width*3) % 4;
tempImgData = malloc((3*width*height)+(padding*height));
//the header has previously been removed so the reading is fine too.
//I also tried a rewind and fseek to the offset from the header - I get the same result
fread((void*) tempImgData, (3*width*height), 1, file1);
int n=0;
bmpImgData2 = (struct rgbQuad*) malloc(3*width*height);
char* tempDestPtr = (char*) bmpImgData2;
char* tempSrcPtr = (char*) tempImgData;
for (int p=0; p<height; p++) {
memcpy(tempDestPtr, tempSrcPtr, sizeof(struct rgbQuad)*width);
printf("(R: %u G: %u B: %u)\n", ((struct rgbQuad*) tempDestPtr)->red, ((struct rgbQuad*) tempDestPtr)->green, ((struct rgbQuad*) tempDestPtr)->blue);
tempDestPtr += (sizeof(struct rgbQuad)*width);
tempSrcPtr += (sizeof(struct rgbQuad)*width+padding);
}