My code as follows is giving me issues trying to write a bitmap image with an odd size 250x250 pixels. When I write it with an evenly divisibly by 3 area, eg. 160x160 everything is fine. I have spent a few hours now trying to get the 250x250 and other odd area's to write correctly but they just won't, the image is diagonal and half of it with the wrong color bits.
a busy cat http://oi40.tinypic.com/2zxuidw.jpg
int screen_save_part(string filename,unsigned x,unsigned y,unsigned w,unsigned h) //Assumes native integers are little endian
{
unsigned sz = w * h;
FILE *bmp=fopen(filename.c_str(), "wb");
if (!bmp) return -1;
fwrite("BM", 2, 1, bmp);
std::vector<unsigned char> rgbdata(3*sz);
glReadPixels(x,window_get_region_height_scaled()-h-y,w,h,GL_BGR, GL_UNSIGNED_BYTE, &rgbdata[0]);
//glBindFramebuffer(GL_DRAW_FRAMEBUFFER, prevFbo);
sz <<= 2;
fwrite(&sz,4,1,bmp);
fwrite("\0\0\0\0\x36\0\0\0\x28\0\0",12,1,bmp);
fwrite(&w,4,1,bmp);
fwrite(&h,4,1,bmp);
fwrite("\1\0\x18\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",28,1,bmp);
if (w & 3) {
w *= 3;
size_t pad = 3 - (w % 3);
sz -= sz >> 2;
for(unsigned i = 0; i <= sz; i += w) {
fwrite(&rgbdata[0] + i,sizeof(char),w,bmp);
fwrite("\0\0\0",sizeof(char),pad,bmp);
}
} else { fwrite(&rgbdata[0], w*3, h, bmp); }
fclose(bmp);
rgbdata.clear();
return 0;
}
Ok, but yah I am about to start pulling my hair out here I have gone through literally everything the closest I got was a for loop after that one without any padding and just added an extra row of null data but that gave me a black line on the very top 1px scanline of the image, but it had no stretching issue.