Main Page   Namespace List   Class Hierarchy   Alphabetical List   Data Structures   File List   Data Fields   Globals  

/cygdrive/d/Eigene Dateien/!DProcs/code_gen/tga.cpp

Go to the documentation of this file.
00001 /*
00002    This program is free software; you can redistribute it and/or
00003    modify it under the terms of the GNU General Public License
00004    as published by the Free Software Foundation; either version 2
00005    of the License, or (at your option) any later version.
00006 
00007    This program is distributed in the hope that it will be useful,
00008    but WITHOUT ANY WARRANTY; without even the implied warranty of
00009    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010    GNU General Public License for more details.
00011 
00012    You should have received a copy of the GNU General Public License
00013    along with this program; if not, write to the Free Software
00014    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00015 
00016    See gpl.txt for more information regarding the GNU General Public License.
00017 
00018 
00019     File    : tga.c
00020     Date    : 05/05/1999
00021     Author  : Nate Miller
00022     Contact : vandals1@home.com
00023 
00024     Change Log
00025     **********
00026     8/07/99 - made the code portable
00027     7/26/99 - added code to fclose () a file when there is an error
00028     6/11/99 - added support for 8bit images, changed commenting
00029 
00030     2/04/04 - added createTexture(), modified getRGBA and getGray
00031 */
00032 #include "tga.h"
00033 #include <stdio.h>
00034 #include <stdlib.h>
00035 #include <string.h>
00036 #ifdef _WIN32
00037 #include <windows.h>
00038 #endif
00039 #include <GL/gl.h>
00040 #include <GL/glu.h>
00041 
00042 #include <iostream>
00043 using namespace std;
00044 
00045 GLenum texFormat;
00046 
00047 typedef unsigned char BYTE ;
00048 
00049 /*
00050 =============
00051 checkSize
00052 
00053 Make sure its a power of 2.
00054 =============
00055 */
00056 int checkSize (int x)
00057 {
00058     if (x == 2   || x == 4 || 
00059         x == 8   || x == 16 || 
00060         x == 32  || x == 64 ||
00061         x == 128 || x == 256 || x == 512)
00062         return 1;
00063     else return 0;
00064 }
00065 /*
00066 =============
00067 getRGBA
00068 
00069 Reads in RGBA data for a 32bit image. 
00070 =============
00071 */
00072 unsigned char *getRGBA (FILE *s, int size)
00073 {
00074     BYTE *rgba;
00075     BYTE temp;
00076     int bread;
00077     int i;
00078 
00079     rgba = new BYTE[size*4]; //malloc (size * 4);
00080 
00081     if (rgba == NULL)
00082         return 0;
00083 
00084     bread = fread (rgba, sizeof (unsigned char), size * 4, s); 
00085 
00086     if (bread != size * 4)
00087     {
00088         delete[] rgba; //free (rgba);
00089         return 0;
00090     }
00091 
00092     /* TGA is stored in BGRA, make it RGBA */
00093     /* transparent color is magenta (1,0,1), which is set to (1,1,1,1) */
00094     for (i = 0; i < size * 4; i += 4 )
00095     {
00096         temp = rgba[i];
00097         rgba[i] = rgba[i + 2];
00098         rgba[i + 2] = temp;
00099         if ((rgba[i] == 255) && (rgba[i+1] == 0) && (rgba[i+2] == 255))
00100         {
00101            // set alpha to 0
00102            rgba[i+3] = 0;
00103            rgba[i+1] = 255;
00104         }
00105         else
00106         {
00107            // else alpha is set to full opaque
00108            rgba[i+3] = 255;
00109         }
00110 
00111     }
00112 
00113     texFormat = GL_RGBA;
00114     return rgba;
00115 }
00116 /*
00117 =============
00118 getRGB
00119 
00120 Reads in RGB data for a 24bit image. 
00121 =============
00122 */
00123 unsigned char *getRGB (FILE *s, int size)
00124 {
00125     BYTE *rgb;
00126     BYTE temp;
00127     int bread;
00128     int i;
00129 
00130     rgb = new BYTE[size*3]; // malloc (size * 3);
00131 
00132     if (rgb == NULL)
00133         return 0;
00134 
00135     bread = fread (rgb, sizeof (unsigned char), size * 3, s);
00136 
00137     if (bread != size * 3)
00138     {
00139         delete[] rgb; //free (rgb);
00140         return 0;
00141     }
00142 
00143     /* TGA is stored in BGR, make it RGB */
00144     for (i = 0; i < size * 3; i += 3)
00145     {
00146         temp = rgb[i];
00147         rgb[i] = rgb[i + 2];
00148         rgb[i + 2] = temp;
00149     }
00150 
00151     texFormat = GL_RGB;
00152 
00153     return rgb;
00154 }
00155 /*
00156 =============
00157 getGray
00158 
00159 Gets the grayscale image data.  Used as an alpha channel.
00160 =============
00161 */
00162 unsigned char *getGray (FILE *s, int size)
00163 {
00164     BYTE *grayData;
00165     int bread;
00166 
00167     grayData = new BYTE[size]; //malloc (size);
00168 
00169     if (grayData == NULL)
00170         return 0;
00171 
00172     bread = fread (grayData, sizeof (unsigned char), size, s);
00173 
00174     if (bread != size)
00175     {
00176         delete[] grayData; //free (grayData);
00177         return 0;
00178     }
00179 
00180     //texFormat = GL_ALPHA;
00181      texFormat = GL_LUMINANCE;
00182     //texFormat = GL_INTENSITY8;
00183 
00184     return grayData;
00185 }
00186 /*
00187 =============
00188 getData
00189 
00190 Gets the image data for the specified bit depth.
00191 =============
00192 */
00193 unsigned char *getData (FILE *s, int sz, int iBits)
00194 {
00195     if (iBits == 32)
00196         return getRGBA (s, sz);
00197     else if (iBits == 24)
00198         return getRGB (s, sz);  
00199     else if (iBits == 8)
00200         return getGray (s, sz);
00201     return NULL;
00202 }
00203 /*
00204 =============
00205 returnError
00206 =============
00207 Called when there is an error loading the .tga file.
00208 */
00209 int returnError (FILE *s, int error)
00210 {
00211     fclose (s);
00212     return error;
00213 }
00214 /*
00215 =============
00216 loadTGA
00217 
00218 Loads up a targa file.  Supported types are 8,24 and 32 uncompressed images.  
00219 id is the texture ID to bind too.
00220 =============
00221 */
00222 int loadTGA (char *name, int id)
00223 {
00224     unsigned char type[4];
00225     unsigned char info[7];
00226     unsigned char *imageData = NULL;
00227     int imageWidth, imageHeight;
00228     int imageBits, size;
00229     FILE *s;
00230 
00231     if (!(s = fopen (name, "r+bt")))
00232         return TGA_FILE_NOT_FOUND;
00233 
00234     fread (&type, sizeof (char), 3, s); // read in colormap info and image type, byte 0 ignored
00235     fseek (s, 12, SEEK_SET);            // seek past the header and useless info
00236     fread (&info, sizeof (char), 6, s);
00237 
00238     if (type[1] != 0 || (type[2] != 2 && type[2] != 3))
00239         returnError (s, TGA_BAD_IMAGE_TYPE);
00240 
00241     imageWidth = info[0] + info[1] * 256; 
00242     imageHeight = info[2] + info[3] * 256;
00243     imageBits = info[4]; 
00244 
00245     size = imageWidth * imageHeight; 
00246 
00247     /* make sure dimension is a power of 2 */
00248     if (!checkSize (imageWidth) || !checkSize (imageHeight))
00249         returnError (s, TGA_BAD_DIMENSION);
00250 
00251     /* make sure we are loading a supported type */
00252     if (imageBits != 32 && imageBits != 24 && imageBits != 8)
00253         returnError (s, TGA_BAD_BITS);
00254 
00255     imageData = getData (s, size, imageBits);
00256 
00257     /* no image data */
00258     if (imageData == NULL)
00259         returnError (s, TGA_BAD_DATA);
00260 
00261     fclose (s);
00262 
00263     glBindTexture (GL_TEXTURE_2D, id);
00264     glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
00265     glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
00266     glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
00267     /* glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); */
00268     glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00269     /* glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); */
00270     glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00271 
00272 
00273     if (texFormat == GL_LUMINANCE)
00274     {
00275          glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
00276          glTexImage2D (GL_TEXTURE_2D, 0, GL_INTENSITY, imageWidth, imageHeight, 0,
00277                   GL_LUMINANCE, GL_UNSIGNED_BYTE, imageData);
00278 
00279     }
00280     else
00281     {
00282            glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
00283            glTexImage2D (GL_TEXTURE_2D, 0, texFormat, imageWidth, imageHeight, 0,
00284                   texFormat, GL_UNSIGNED_BYTE, imageData);
00285    }
00286 
00287 
00288     /* release data, its been uploaded */
00289     free (imageData);
00290 
00291     return 1;
00292 }
00293 
00294 
00295 
00296 
00297 unsigned int createTexture( char  *  filename )
00298 {
00299   #define CHECK_GL_ERROR(msg) {const GLint e = glGetError(); if ((e != 0) && (e != GL_NO_ERROR)) {cerr\
00300   << endl << "OpenGL-ERROR <0x" << e <<  "> in " __FILE__ "(" << __LINE__ << ")!" << endl << msg << endl;cerr << gluErrorString(e) << endl;}}
00301 
00302   GLuint texid = 0;
00303   glGenTextures(1,&texid);
00304   CHECK_GL_ERROR("glGenTextures failed");
00305   if (loadTGA(filename,texid) != 1)
00306    {
00307      cout << "Error occured while tried to load TGA texture from " << filename << endl;
00308    }
00309 
00310   CHECK_GL_ERROR(filename);
00311 
00312   return texid;
00313 
00314   #undef CHECK_GL_ERROR
00315 } // end of createTexture()
00316 

Generated on Thu Mar 18 18:33:48 2004 for miniQT by doxygen1.2.18