source: GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/vmi/src/WriteTGA.cpp @ 983

Revision 983, 3.0 KB checked in by gumbau, 18 years ago (diff)
Line 
1// WriteTGA.c
2// Captures the current screen/window from the OpenGL context
3// and writes the pixels out as a targa formatted file.
4// Richard S. Wright Jr.
5// OpenGL SuperBible
6
7#include <stdio.h>
8#include <stdlib.h>
9
10#include "../include/GL/glew.h"
11#include "../include/TGA.h"
12
13////////////////////////////////////////////////////////////////////
14// Capture the current viewport and save it as a targa file.
15// Be sure and call SwapBuffers for double buffered contexts or
16// glFinish for single buffered contexts before calling this function.
17// Returns 0 if an error occurs, or 1 on success.
18GLint gltWriteTGA(const char *szFileName)
19    {
20    FILE *pFile;                // File pointer
21    TGAHEADER tgaHeader;                // TGA file header
22    unsigned long lImageSize;   // Size in bytes of image
23    GLbyte      *pBits = NULL;      // Pointer to bits
24    GLint iViewport[4];         // Viewport in pixels
25    GLint lastBuffer;          // Storage for the current read buffer setting
26   
27        // Get the viewport dimensions
28        glGetIntegerv(GL_VIEWPORT, iViewport);
29
30    // How big is the image going to be (targas are tightly packed)
31        lImageSize = iViewport[2] * 3 * iViewport[3];   
32
33    // Allocate block. If this doesn't work, go home
34    pBits = (GLbyte *)malloc(lImageSize);
35    if(pBits == NULL)
36        return 0;
37
38    // Read bits from color buffer
39    glPixelStorei(GL_PACK_ALIGNMENT, 1);
40        glPixelStorei(GL_PACK_ROW_LENGTH, 0);
41        glPixelStorei(GL_PACK_SKIP_ROWS, 0);
42        glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
43   
44    // Get the current read buffer setting and save it. Switch to
45    // the front buffer and do the read operation. Finally, restore
46    // the read buffer state
47    glGetIntegerv(GL_READ_BUFFER, &lastBuffer);
48    glReadBuffer(GL_FRONT);
49    glReadPixels(0, 0, iViewport[2], iViewport[3], GL_BGR_EXT, GL_UNSIGNED_BYTE, pBits);
50    glReadBuffer(lastBuffer);
51   
52    // Initialize the Targa header
53    tgaHeader.identsize = 0;
54    tgaHeader.colorMapType = 0;
55    tgaHeader.imageType = 2;
56    tgaHeader.colorMapStart = 0;
57    tgaHeader.colorMapLength = 0;
58    tgaHeader.colorMapBits = 0;
59    tgaHeader.xstart = 0;
60    tgaHeader.ystart = 0;
61    tgaHeader.width = iViewport[2];
62    tgaHeader.height = iViewport[3];
63    tgaHeader.bits = 24;
64    tgaHeader.descriptor = 0;
65   
66    // Do byte swap for big vs little endian
67#ifdef __APPLE__
68    BYTE_SWAP(tgaHeader.colorMapStart);
69    BYTE_SWAP(tgaHeader.colorMapLength);
70    BYTE_SWAP(tgaHeader.xstart);
71    BYTE_SWAP(tgaHeader.ystart);
72    BYTE_SWAP(tgaHeader.width);
73    BYTE_SWAP(tgaHeader.height);
74#endif
75   
76    // Attempt to open the file
77    pFile = fopen(szFileName, "wb");
78    if(pFile == NULL)
79        {
80        free(pBits);    // Free buffer and return error
81        return 0;
82        }
83     
84    // Write the header
85    fwrite(&tgaHeader, sizeof(TGAHEADER), 1, pFile);
86   
87    // Write the image data
88    fwrite(pBits, lImageSize, 1, pFile);
89             
90    // Free temporary buffer and close the file
91    free(pBits);   
92    fclose(pFile);
93   
94    // Success!
95    return 1;
96    }
Note: See TracBrowser for help on using the repository browser.