next question on BMP screenshots

hi guys
could you please tell me what could be the problem with this function…

this one i’ve almost completely rewrote from one of threads some time ago (think it was originally written by rts)…tried also another code from some tutorial - both were very similar but they build BMP files i can’t read then - every windows application says it’s not valid BMP format or starts to read it but doesn’t finish

in the code here is GL_RGB - i know i should replace R and B, but this shouldn’t make my file invalid…

so here’s the code

void screen_c::screenshot()
{
char fileName[256];
FILE *fBMP;

sprintf(fileName, “save/screenshots/shot%i.bmp”, numScreenshots++);
if (!(fBMP = fopen(fileName, “wb”))) return;

unsigned char image = (unsigned char) malloc(sizeof(unsigned char) * width * height * 3);
glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, image);

struct BMPHeader
{
unsigned short type;
unsigned int size;
unsigned short res1;
unsigned short res2;
unsigned int offset;
} header;

struct BMPInfo
{
unsigned int size;
unsigned int width;
unsigned int height;
unsigned short planes;
unsigned short bit_count;
unsigned int comp;
unsigned int sizeImage;
unsigned int x_pels_per_meter;
unsigned int y_pels_per_meter;
unsigned int clr_used;
unsigned int clr_important;
} info;

header.type = ‘B’ | ‘M’ << 8;
header.size = sizeof(header) + sizeof(info) + width * height * 3;
header.res1 = header.res2 = 0;
header.offset = sizeof(header) + sizeof(info);
info.size = sizeof(info);
info.width = width;
info.height = height;
info.planes = 1;
info.bit_count = 24;
info.comp = 0;
info.sizeImage = width * height * 3;
info.x_pels_per_meter = info.y_pels_per_meter = 0;
info.clr_used = 0;
info.clr_important = 0;
fwrite(&header, sizeof(header), 1, fBMP);
fwrite(&info, sizeof(info), 1, fBMP);
fwrite(image, sizeof(unsigned char), width * height * 3, fBMP);

fclose(fBMP);
}

thx for any suggestions

Can be that your compiler align those short-members on 4 byte boundary. The sizeof operator will then return the size of the structure including the pad bytes, which is not correct. Same with fwrite, which will write those pad bytes to the file.

In MSVC, you can tell the compiler to store members on byte boudary like this.

#pragma pack(push, 1) // push old alignmnent and set new to 1
struct
{

};
#pragma pack(pop) // go back to previous alignment

Maybe u must set the correct opengl parameters to say glReadPixels how to read the buffer.

I’m codind a windows aplication and i save bmp’s without problem.
Try with
glPixelStorei(GL_PACK_ALIGNMENT, 4); glPixelStorei(GL_PACK_ROW_LENGTH, 0);
glPixelStorei(GL_PACK_SKIP_ROWS, 0);
glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
before glReadPixels

If you want, i could post the code or send it to u.

[This message has been edited by pifaf (edited 05-24-2002).]

thank you for your replies, yes Bob that was the reason, i added pragma’s before and after the BMP code and my(not my) both codes work ok…great thanks!