1 | // LoadTGA.c |
---|
2 | // Loads a Targa file for OpenGL |
---|
3 | // Richard S. Wright Jr. |
---|
4 | // OpenGL SuperBible |
---|
5 | // This really only works with 24/32/8 bit targas |
---|
6 | |
---|
7 | #include <stdio.h> |
---|
8 | #include <stdlib.h> |
---|
9 | |
---|
10 | #include "../include/GL/glew.h" |
---|
11 | #include "../include/TGA.h" |
---|
12 | |
---|
13 | //////////////////////////////////////////////////////////////////// |
---|
14 | // Allocate memory and load targa bits. Returns pointer to new buffer, |
---|
15 | // height, and width of texture, and the OpenGL format of data. |
---|
16 | // Call free() on buffer when finished! |
---|
17 | // This only works on pretty vanilla targas... 8, 24, or 32 bit color |
---|
18 | // only, no palettes, no RLE encoding. |
---|
19 | GLbyte *gltLoadTGA(const char *szFileName, GLint *iWidth, GLint *iHeight, GLint *iComponents, GLenum *eFormat) |
---|
20 | { |
---|
21 | FILE *pFile; // File pointer |
---|
22 | TGAHEADER tgaHeader; // TGA file header |
---|
23 | unsigned long lImageSize; // Size in bytes of image |
---|
24 | short sDepth; // Pixel depth; |
---|
25 | GLbyte *pBits = NULL; // Pointer to bits |
---|
26 | |
---|
27 | // Default/Failed values |
---|
28 | *iWidth = 0; |
---|
29 | *iHeight = 0; |
---|
30 | *eFormat = GL_BGR_EXT; |
---|
31 | *iComponents = GL_RGB8; |
---|
32 | |
---|
33 | // Attempt to open the fil |
---|
34 | pFile = fopen(szFileName, "rb"); |
---|
35 | if(pFile == NULL) |
---|
36 | return NULL; |
---|
37 | |
---|
38 | // Read in header (binary) |
---|
39 | fread(&tgaHeader, 18/* sizeof(TGAHEADER)*/, 1, pFile); |
---|
40 | |
---|
41 | // Do byte swap for big vs little endian |
---|
42 | #ifdef __APPLE__ |
---|
43 | BYTE_SWAP(tgaHeader.colorMapStart); |
---|
44 | BYTE_SWAP(tgaHeader.colorMapLength); |
---|
45 | BYTE_SWAP(tgaHeader.xstart); |
---|
46 | BYTE_SWAP(tgaHeader.ystart); |
---|
47 | BYTE_SWAP(tgaHeader.width); |
---|
48 | BYTE_SWAP(tgaHeader.height); |
---|
49 | #endif |
---|
50 | |
---|
51 | |
---|
52 | // Get width, height, and depth of texture |
---|
53 | *iWidth = tgaHeader.width; |
---|
54 | *iHeight = tgaHeader.height; |
---|
55 | sDepth = tgaHeader.bits / 8; |
---|
56 | |
---|
57 | // Put some validity checks here. Very simply, I only understand |
---|
58 | // or care about 8, 24, or 32 bit targa's. |
---|
59 | if(tgaHeader.bits != 8 && tgaHeader.bits != 24 && tgaHeader.bits != 32) |
---|
60 | return NULL; |
---|
61 | |
---|
62 | // Calculate size of image buffer |
---|
63 | lImageSize = tgaHeader.width * tgaHeader.height * sDepth; |
---|
64 | |
---|
65 | // Allocate memory and check for success |
---|
66 | pBits = (GLbyte *)malloc(lImageSize * sizeof(GLbyte)); |
---|
67 | if(pBits == NULL) |
---|
68 | return NULL; |
---|
69 | |
---|
70 | // Read in the bits |
---|
71 | // Check for read error. This should catch RLE or other |
---|
72 | // weird formats that I don't want to recognize |
---|
73 | if(fread(pBits, lImageSize, 1, pFile) != 1) |
---|
74 | { |
---|
75 | free(pBits); |
---|
76 | return NULL; |
---|
77 | } |
---|
78 | |
---|
79 | // Set OpenGL format expected |
---|
80 | switch(sDepth) |
---|
81 | { |
---|
82 | case 3: // Most likely case |
---|
83 | *eFormat = GL_BGR_EXT; |
---|
84 | *iComponents = GL_RGB8; |
---|
85 | break; |
---|
86 | case 4: |
---|
87 | *eFormat = GL_BGRA_EXT; |
---|
88 | *iComponents = GL_RGBA8; |
---|
89 | break; |
---|
90 | case 1: |
---|
91 | *eFormat = GL_LUMINANCE; |
---|
92 | *iComponents = GL_LUMINANCE8; |
---|
93 | break; |
---|
94 | }; |
---|
95 | |
---|
96 | |
---|
97 | // Done with File |
---|
98 | fclose(pFile); |
---|
99 | |
---|
100 | // Return pointer to image data |
---|
101 | return pBits; |
---|
102 | } |
---|
103 | |
---|