glReadPixels save to BMP - Color Dirstortion

2.5k views Asked by At

I'm using OpenGL and PCL_lib right now, And I want to draw a accessory on the background pic.

Here is my output_image on display:

https://www.dropbox.com/s/ocb62klonzxb4yx/IMG_2027%5B1%5D.JPG

Image source is PCD format(PCL lib), drew by OpenGL

and Here is my code where to save the GL_pic:

char filename[100];
sprintf(filename, "path", _mframe_Count);   

// Set Parameter
GLint x, y, width, height;
int i;
unsigned long imageSize;
GLenum lastBuffer
glGetIntegerv( GL_VIEWPORT, mViewPort );
x = mViewPort[0];
y = mViewPort[1];
width = mViewPort[2];
height = mViewPort[3];    

BITMAPFILEHEADER bmfh;
BITMAPINFOHEADER bmih;
bmfh.bfType = 0x4D42;
bmfh.bfReserved1 = 0;
bmfh.bfReserved2 = 0;
bmfh.bfOffBits = sizeof(bmfh) + sizeof(bmih);
imageSize = ((width+((4-(width%4))%4))*height*3)+2;
bmfh.bfSize = imageSize + sizeof(bmfh) + sizeof(bmih);
GLubyte *pImageData = (GLubyte *)malloc(imageSize);


// Allocate apicture buffer
if(pImageData == NULL) 
{
    printf("[Error] : grab_GL(), Cannot create Memory_Sapce for pImageData.\n");
    return false;
}

// Byte alignment (that is, no alignment)
glPixelStorei(GL_PACK_ALIGNMENT, 4);
glPixelStorei(GL_PACK_ROW_LENGTH,0);
glPixelStorei(GL_PACK_SKIP_ROWS,0);
glPixelStorei(GL_PACK_SKIP_PIXELS,0);
glPixelStorei(GL_PACK_SWAP_BYTES,1);
glGetIntegerv(GL_READ_BUFFER, ( GLint *)&lastBuffer);
glReadBuffer(GL_FRONT);

glReadPixels(0, 0, width, height, GL_BGR, GL_UNSIGNED_BYTE, pImageData);
pImageData[imageSize-1] = 0;
pImageData[imageSize-2] = 0;
glReadBuffer(lastBuffer);


FILE *pWritingFile = fopen(filename, "wb");
if (pWritingFile == NULL)
{
    free(pImageData);
    printf("[Error] : grab_GL(), Cannot open %s for writting.\n", filename);
    return false;
}

// BMP Structures   
bmih.biSize = sizeof(bmih);
bmih.biWidth = width;
bmih.biHeight = height;
bmih.biPlanes = 1;
bmih.biBitCount = 24;
bmih.biCompression = 0;
bmih.biSizeImage = imageSize;
bmih.biXPelsPerMeter = 2835;
bmih.biYPelsPerMeter = 2835;
bmih.biClrUsed = 0;
bmih.biClrImportant = 0;    

// BMP Compress
fwrite(&bmfh, 8, 1, pWritingFile);
fwrite(&bmfh.bfReserved2, sizeof(bmfh.bfReserved2), 1, pWritingFile); 
fwrite(&bmfh.bfOffBits, sizeof(bmfh.bfOffBits), 1, pWritingFile); 
fwrite(&bmih, sizeof(bmih), 1, pWritingFile);
fwrite(pImageData, imageSize, 1, pWritingFile);


// End The Process  
fclose(pWritingFile);
free(pImageData);    
return true;

But when save the image to BMP file which shown on the display, it becomes:

https://www.dropbox.com/s/yzus5wiu3shnwm2/IMG_2023%5B1%5D.JPG

1

There are 1 answers

2
Roger Rowland On

It looks like the writing to disk is a little difficult to interpret:

// BMP Compress
fwrite(&bmfh, 8, 1, pWritingFile);
fwrite(&bmfh.bfReserved2, sizeof(bmfh.bfReserved2), 1, pWritingFile); 
fwrite(&bmfh.bfOffBits, sizeof(bmfh.bfOffBits), 1, pWritingFile); 
fwrite(&bmih, sizeof(bmih), 1, pWritingFile);
fwrite(pImageData, imageSize, 1, pWritingFile);

I don't see why you are writing the elements of bmfh separately so just make sure everything is clear by doing this instead:

// BMP Compress
fwrite(&bmfh, sizeof(bmfh), 1, pWritingFile);
fwrite(&bmih, sizeof(bmih), 1, pWritingFile);
fwrite(pImageData, imageSize, 1, pWritingFile);

If that makes no difference, then the appearance of your image is that the colour components are reversed (RGB rather than BGR) - although glPixelStorei(GL_PACK_SWAP_BYTES,1) is not supposed to affect the pixel data itself, it would be worth trying either this:

glPixelStorei(GL_PACK_SWAP_BYTES,1);
glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, pImageData);

or this:

glPixelStorei(GL_PACK_SWAP_BYTES,0);
glReadPixels(0, 0, width, height, GL_BGR, GL_UNSIGNED_BYTE, pImageData);