After doing this, the .bmp file is created. However, when I want to open and view it, this error message appears:
"Paint cannot read this file. This is not a valid bitmap file, or its format is not currently supported"
Well, you need to do a loop to swap every 3rd byte with the one 2 bytes before it. Which will convert it from RGB to BGR.
Like --
Code:
BYTE tmp;
for ( int i = 3; i < array_size; i+=3 ) {
tmp = bmp[i-2];
bmp[i-2] = bmp[i];
bmp[i] = tmp;
}
Or you can use memmove()...
I'm really sick right now so my thinking may be a little off, hopefully that code is actually right ..
[edit:] Assuming I'm actually right ( are the colors wrong on the bitmap? Inverted? ) ...
Each pixel has a color index right, each color is an RGB value.
Bitmaps should be in the BGR format I think, at least I remember when I wrote a bitmap loader for opengl textures that it had to be transformed from BGR to RGB to use it. Which is why I believe it needs to be save in BGR...
Anyways, the BYTE array is really just and array of colors. .. So for example -
BYTE[0] = Red value
BYTE[1] = Green value
BYTE[2] = Blue value
RGB!
But, assuming my memory serves me correctly and you need to swap these bytes every set of 3 in the array would be in the format BGR
BYTE[0] = Blue value
BYTE[1] = Green value
BYTE[2] = Red value
And it just repeats like that, with BYTE[3] being blue value and BYTE[5] being red value and so on..
I hope I haven't confused you. This better be right or I'll look like a fool..
__________________ Omnis mico antequam dominus Spookster!
Yes the distortion is just bcoz of junk bytes. You have to take care of junk bytes while reading or writting a 24-bit bitmap image. Since in each scan line the number of bytes should b multiple of 4, there may be some extra bytes. At the time of reading u must jump over those bytes in each scan line. And while creating a 24-bit bitmap u've to write the junk bytes into the output file.
To find out junk bytes in each scan line u can try the following line of code in c :--
int junkBytes=(fileSize - (imageSize*3 + 54)/width) / height;
where fileSize = total disk size of the file
imageSize = width * height;
another thing u must notice --- in ur code u've written "MB" as BMP identifier, but it should be "BM".
Last edited by s.basu9; 01-02-2007 at 10:04 AM..
Reason: bmp identifier