Problems with getting screenshot

I have problem with getting my screenshot function to work.
The screenshot I should get is:
http://lapas.dau.lv/salitis/1.bmp
But instead I receive:
http://lapas.dau.lv/salitis/2.bmp
First shot is taken via PrtScr, second via function posted below.
I’ve checked file output routine with regular (selfmade) arrays & it worked as it should. Firsty I noticed that problem arrised when there were pixels affected by Cg vp/fp on screen, but now it seems that it’s allways apparent. Walls are drawn with bumpmap shader applied.
Maybe problem is with glReadPixels, as I noticed that some people uses w+1 and h+1 to get data, but after experimenting with these values nothing changed. I have no idea what could be wrong
The function I use to make screenshot:

void screenshot()
{
int w, h;
Camera.getres(w,h);
unsigned char data;
data = (unsigned char
)malloc(sizeof(unsigned char)3w*h);
glReadPixels(0,0,w,h,GL_BGR,GL_UNSIGNED_BYTE,data);
BITMAPFILEHEADER bfh;
BITMAPINFOHEADER bih;
bih.biSize = sizeof(BITMAPINFOHEADER);
bih.biPlanes = 1;
bih.biBitCount = 24;
bih.biCompression = BI_RGB;
bih.biSizeImage = w * h * 3;
bih.biXPelsPerMeter = 0;
bih.biYPelsPerMeter = 0;
bih.biClrUsed = 0;
bih.biClrImportant = 0;
bih.biWidth = w;
bih.biHeight = h;
bfh.bfSize = sizeof(BITMAPFILEHEADER);
bfh.bfType = BITMAP_ID;
bfh.bfReserved1 = 0;
bfh.bfReserved2 = 0;
bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

fstream file;
file.open(“out.bmp”,ios: ut);
if(file.is_open())
{
file.write((char*)&bfh,sizeof(BITMAPFILEHEADER));
file.write((char*)&bih,sizeof(BITMAPINFOHEADER));
file.write(data,sizeof(unsigned char)3w*h);
file.close();
}
free(data);
}

file.open("out.bmp",ios::out[b]|ios::binary[/b]);

Thanks, but I don’t actually understand why… Even more 60% of image is outputted correctly. And BMP is is based on bytes, so that every pixel consists of 3 bytes, not 2 or 1/2 byte (GL_RGB8 -> 8bits per color chanel intensity of pixel). BTW, texture reading function without binary is working OK. I’m confused…

EDIT:
I noticed your comment about pack aligment in other thread, but if that’s the problem, how ios::binary can solve it?

http://www.opengl.org/resources/features/KilgardTechniques/oglpitfall/

The question is, do this pack aligment influences on performance?

[This message has been edited by M/\dm/
(edited 02-04-2004).]

Hmm, strange I added glPixelStores, and nothing changed, added ios::binary & everything is OK, why thats so? What’s the difference between ios::binary and ordinary, I’m writing chars anyway :?

Originally posted by M/\dm/
:
Hmm, strange I added glPixelStores, and nothing changed, added ios::binary & everything is OK, why thats so? What’s the difference between ios::binary and ordinary, I’m writing chars anyway :?
Windows massages line feeds. If you write the string "
" to a file, it will get converted to two bytes, a line feed and a carriage return character. This is only useful for text files - if it can be considered useful at all -, which is the assumed default when opening any file. There’s a statistical 1 in 256 chance that a byte in your binary data happens to be
. That’s the cause of your problems, and that’s why you need to specify ios::binary. It will turn these line feed transformations off.

PS: AFAIK this doesn’t apply to *nix, only to Windows (and DOS).

[This message has been edited by zeckensack (edited 02-04-2004).]

Thanks, now I got it