Strict Standards: Only variables should be passed by reference in /home/stefanix/stefanix.net/doku.php on line 71

Warning: Cannot modify header information - headers already sent by (output started at /home/stefanix/stefanix.net/doku.php:71) in /home/stefanix/stefanix.net/inc/actions.php on line 154
stefanix :: opengl-raw-image
ABOUT PROJECTS RESUME
opengl-raw-image

This opengl code snippet writes the current color buffer to a raw grayscale image. Photoshop recognizes this format as Photoshop Raw.

int screenshot(char *filename) {
    FILE* imgfile;
    unsigned char* pixels;
    unsigned char* flipped;
    int viewport[4];
    int width, height, i;
 
    glGetIntegerv(GL_VIEWPORT, viewport);
    width = viewport[2];
    height = viewport[3];
 
    pixels =  malloc(width*height);
    glReadPixels(0, 0, width, height, 
                 GL_LUMINANCE, GL_UNSIGNED_BYTE, pixels);
 
    flipped =  malloc(width*height);
    for (i=0; i < height; i++)
        memcpy(flipped + width*i, 
               pixels + width*(height-i-1), width);
    free(pixels);
 
    imgfile = fopen(filename, "wb");
    fwrite(flipped, width*height, 1, imgfile);
 
    free(flipped);
    fclose(imgfile);
 
    return 1;
}


back to → isomotion

Show pagesource Old revisions Backlinks Index Recent changes
opengl-raw-image.txt · Last modified: 2009/04/26 01:30 (external edit)

Strict Standards: Only variables should be passed by reference in /home/stefanix/stefanix.net/doku.php on line 79