/* This code saves the contents of an OpenGL window into a TIFF file. It requires libtiff! */ void DumpWindowTIFF(int win_width, int win_height, char *basename, int step) { TIFF *file; GLubyte *image, *p; int i; char filename[MAX_BUFFER_SIZE]; /* Compose the name of the TIFF file: basename_step#.tif */ sprintf(filename,"%s_%.4d.tif", basename, step); /* Open TIFF file */ file = TIFFOpen(filename, "w"); if (file == NULL) { return; } /* Allocate space for the image */ image = (GLubyte *) malloc(win_width * win_height * sizeof(GLubyte) * 3); /* OpenGL's default 4 byte pack alignment would leave extra bytes at the end of each image row so that each full row contained a number of bytes divisible by 4. Ie, an RGB row with 3 pixels and 8-bit componets would be laid out like "RGBRGBRGBxxx" where the last three "xxx" bytes exist just to pad the row out to 12 bytes (12 is divisible by 4). To make sure the rows are packed as tight as possible (no row padding), set the pack alignment to 1. */ glPixelStorei(GL_PACK_ALIGNMENT, 1); /* Read framebuffer into image */ glReadPixels(0, 0, win_width, win_height, GL_RGB, GL_UNSIGNED_BYTE, image); /* Set TIFF tags */ TIFFSetField(file, TIFFTAG_IMAGEWIDTH, (uint32) win_width); TIFFSetField(file, TIFFTAG_IMAGELENGTH, (uint32) win_height); TIFFSetField(file, TIFFTAG_BITSPERSAMPLE, 8); /* This uses LZW compression */ TIFFSetField(file, TIFFTAG_COMPRESSION, COMPRESSION_LZW); /* This uses no compression */ /* TIFFSetField(file, TIFFTAG_COMPRESSION, COMPRESSION_NONE); */ TIFFSetField(file, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB); TIFFSetField(file, TIFFTAG_SAMPLESPERPIXEL, 3); TIFFSetField(file, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); TIFFSetField(file, TIFFTAG_ROWSPERSTRIP, 1); /* Write image to TIFF file scanline by scanline */ p = image; for (i = win_height - 1; i >= 0; i--) { if (TIFFWriteScanline(file, p, i, 0) < 0) { free(image); TIFFClose(file); return; } p += win_width * sizeof(GLubyte) * 3; } /* Close TIFF file */ TIFFClose(file); }