hockeyman
hockeyman

Reputation: 1183

glReadPixels to ImageMagick

I want to save OpenGL view as image with ImageMagick. I searched the internet and found some info about that. So I now know that I need to use glReadPixels. Thats OK, but how then make image from these readed pixels?


Now code looks like this:

char *buffer = (char*) calloc(viewHeight * viewWidth * 4, sizeof(char));
glReadPixels( 0, 0, viewWidth, viewHeight, GL_RGBA, GL_BYTE, buffer );
Blob b( buffer, 4 * viewWidth * viewHeight );
Image saveimage(b);
saveimage.write("subimageGcrop.png");

Upvotes: 0

Views: 400

Answers (1)

BЈовић
BЈовић

Reputation: 64283

When you read pixels using glReadPixels, you need to copy them to a blob object. Then you can create Image object, and write to a file.

Something like this :

    char *buffer; // needs to be big enough
    glReadPixels( 0, 0, width, length, GL_BGR, GL_BYTE, buffer );
    Blob b( buffer, 3 * width * length );
    Image i( b, 3 * width * length, 3 );
    i.write( "img.jpg" );

You might need to adjust the parameters.

Upvotes: 1

Related Questions