1
glViewport ( 0, 0, 320, 480 );
byte* memory 
glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 1 );
glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, memory);

I want to convert the byte array, which is read from the frame buffer using glreadpixel(), to any image of PNG/JPEG:

The printf statement shows that glreadpixel() is reading the memory correctly.
In what way can I write the image into to a file, e.g: "C:\image.jpg"?

1
  • @EitanT Thank you for edit. I found the solution. its like acces each frame from effect and the output is rgb which can be converted to png using its library Commented Jun 26, 2012 at 14:12

1 Answer 1

1

OpenGL (ES included) accepts only the RAW texture data (that is, RGB triples, RGBA quads or some other predefined format) or some data with s3tc (S3 Texture Compression), but not the file formats. Same with the output.

EDIT:

To be sure that the elements actually appear in the framebuffer, glFlush() call must be done right before the glReadPixels() call. It will force the update of the buffer.

Addition:

If all you need is to dump the buffer contents to the file, I would suggest you to use the .bmp file format. It's only a 54-byte header at the beginning and then the data which is read directly from the OpenGL (watch for GL_UNPACK_ALIGNMENT though).

Here's a WriteBMP() function for you:

void WriteBMP(const char *fname, int w,int h,unsigned char *img)
{
    FILE *f = fopen(fname,"wb");

    unsigned char bfh[54] = {0x42, 0x4d,
    /* bfSize [2]*/ 54, 0, 0, 0, /**/
    /* reserved [6]*/ 0, 0, 0, 0, /**/
    /* biOffBits [10]*/ 54, 0, 0, 0, /**/
    /* biSize [14]*/ 40, 0, 0, 0, /**/
    /* width [18]*/ 0, 0, 0, 0, /**/
    /* height [22]*/ 0, 0, 0, 0, /**/
    /* planes [26]*/ 1, 0, /**/
    /* bitcount [28]*/ 24, 0,/**/
    /* compression [30]*/ 0, 0, 0, 0, /**/
    /* size image [34]*/ 0, 0, 0, 0, /**/
    /* xpermeter [38]*/ 0, 0, 0, 0, /**/
    /* ypermeter [42]*/ 0, 0, 0, 0, /**/
    /* clrused [46]*/ 0, 0, 0, 0, /**/
    /* clrimportant [50]*/ 0, 0, 0, 0 /**/};
    int realw = w * 3, rem = w % 4, isz = (realw + rem) * h, fsz = isz + 54;
    //bfh.bfSize = fsz;
    bfh[2] = (fsz & 0xFF); bfh[3] = (fsz >> 8) & 0xFF; bfh[4] = (fsz >> 16) & 0xFF; bfh[5] = (fsz >> 24) & 0xFF;
    //bfh.biSize = isz
    bfh[34] = (isz & 0xFF); bfh[35] = (isz >> 8) & 0xFF; bfh[36] = (isz >> 16) & 0xFF; bfh[37] = (isz >> 24) & 0xFF;
    //bfh.biWidth = w;
    bfh[18] = (w & 0xFF); bfh[19] = (w >> 8) & 0xFF; bfh[20] = (w >> 16) & 0xFF; bfh[21] = (w >> 24) & 0xFF;
    //bfh.biHeight = h;
    bfh[22] = (h & 0xFF); bfh[23] = (h >> 8) & 0xFF; bfh[24] = (h >> 16) & 0xFF; bfh[25] = (h >> 24) & 0xFF;

    // xpels/ypels
    // bfh[38] = 19; bfh[39] = 11;
    // bfh[42] = 19; bfh[43] = 11;

    fwrite((void*)bfh, 54, 1, f);

    unsigned char* bstr = new unsigned char[realw], *remstr = 0; 
    if(rem != 0) { remstr = new unsigned char[rem]; memset(remstr,0,rem); }

    for(int j = h-1 ; j > -1 ; j--){
            for(int i = 0 ; i < w ; i++)
                    for(int k = 0 ; k < 3 ; k++) { bstr[i*3+k] = img[(j*realw+i*3)+(2-k)]; }
            fwrite(bstr,realw,1,f); if (rem != 0) { fwrite(remstr,rem,1,f); }
    }

    delete [] bstr; if(remstr) delete [] remstr;

    fclose(f);
}

Then just do the call:

WriteBMP("C:\\image.bmp", 320, 480, memory);

If you really need the .jpg/.png file, you might convert the resulting .bmp file or use the FreeImage library or the libjpng/libpng themselves. They all deal with specific file formats.

Sign up to request clarification or add additional context in comments.

6 Comments

I am able to access it and and storing output as .rgb. but when the glReadpixel() access the framebuffer and i see the output.rgb file i dont find any thing in it. The frame buffer is cleared after eglswapbuffer. I tried accessing it before eglswapbuffer and the result is same. I dont find the image, can you help me with it.
Yes, try the glFlush() call right before the glReadPixels() call. It will force the update of the buffer.
Its a good idea :) I tried it but its the same result. Can we guess What the reason could be. i tried accessing the fb before and after the swap but when i get the rgb output file and read it from rgb viewer also i am getting same result. just a grey screen. But when i display it the result is accurate and perfect.
I forgot, call the glFinish() after the glReadPixels() call. It's essential. The reason is obvious - something is cached and we have to wait for the result.
Still same problem. :( i am using glreadpixel just before eglswapbuffer. I tried changing it after and before swapbuffer. But i dont find any changes. my pyuv player has same result displayed ` glFlush(); glReadPixels(0, 0, esContext->width, esContext->height, GL_RGB, GL_UNSIGNED_BYTE, memory); glFinish(); `
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.