src/image.h File Reference

#include "GLee.h"

Include dependency graph for image.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  TGAFILE

Defines

#define _IMAGE_H_

Functions

bool BuildTexture (const char *filename, GLuint *texId, GLint param, bool genMips, bool tryCompress, bool tryAlpha)
int LoadTGAFile (const char *filename, TGAFILE *tgaFile)
int WriteTGAFile (const char *filename, short int width, short int height, unsigned char *imageData)
void SaveScreenshot (const char *filename, int x, int y)


Define Documentation

#define _IMAGE_H_

Definition at line 8 of file image.h.


Function Documentation

bool BuildTexture ( const char *  filename,
GLuint *  texId,
GLint  param,
bool  genMips,
bool  tryCompress,
bool  tryAlpha 
)

Definition at line 17 of file image.cpp.

References Config::CheckCompressTextures(), Log::Message(), Config::s_config, and Log::s_log.

Referenced by TextureResource::Load().

00017                                                                                                                    {
00018   // True if configured for compression and tryCompress is true
00019   bool shouldCompress;
00020 
00021   // Create storage space for the texture
00022   SDL_Surface *TextureImage;
00023 
00024   // Format to pass to texture generation function
00025   GLint textureFormat;
00026   GLint imageFormat;
00027 
00028   // Load the image, check for errors, if image is not found quit
00029   TextureImage = IMG_Load(filename);
00030 
00031   if (!TextureImage) {
00032     // Error loading image
00033     // Perhaps this should throw an exception
00034 
00035     // Log the error
00036     Log::s_log->Message("Warning: Could not load %s", filename);
00037 
00038     // Return false
00039     return false;
00040   }
00041 
00042   // Create the texture
00043   glGenTextures(1, texId);
00044 
00045   // Typical texture generation using data from the bitmap
00046   glBindTexture(GL_TEXTURE_2D, *texId);
00047 
00048   // Setup filtering
00049   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, param);
00050   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, param);
00051   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00052 
00053   shouldCompress = tryCompress && Config::s_config->CheckCompressTextures();
00054 
00055   switch (TextureImage->format->BytesPerPixel) {
00056     case 3:
00057       imageFormat = GL_RGB;
00058       break;
00059     case 4:
00060       imageFormat = GL_RGBA;
00061       break;
00062     default:
00063       Log::s_log->Message("Warning: Invalid texture format in %s", filename);
00064       SDL_FreeSurface(TextureImage);
00065       return false;
00066   }
00067 
00068   if (shouldCompress) {
00069     if ((tryAlpha) && (imageFormat == GL_RGBA)) {
00070       textureFormat = GL_COMPRESSED_RGBA;
00071     } else {
00072       textureFormat = GL_COMPRESSED_RGB;
00073     }
00074   } else {
00075     if ((tryAlpha) && (imageFormat == GL_RGBA)) {
00076       textureFormat = GL_RGBA;
00077     } else {
00078       textureFormat = GL_RGB;
00079     }
00080   }
00081 
00082   if (genMips) {
00083     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
00084         GL_LINEAR_MIPMAP_LINEAR);
00085     // Generate the texture and mipmaps
00086     gluBuild2DMipmaps(GL_TEXTURE_2D, textureFormat, TextureImage->w,
00087           TextureImage->h, imageFormat, GL_UNSIGNED_BYTE,
00088           TextureImage->pixels);
00089   } else {
00090     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
00091         GL_LINEAR);
00092     glTexImage2D(GL_TEXTURE_2D, 0, textureFormat, TextureImage->w,
00093          TextureImage->h, 0, imageFormat, GL_UNSIGNED_BYTE,
00094          TextureImage->pixels);
00095   }
00096 
00097   // Free up memory we used
00098   SDL_FreeSurface(TextureImage);
00099 
00100   return true;
00101 }

int LoadTGAFile ( const char *  filename,
TGAFILE tgaFile 
)

Definition at line 105 of file image.cpp.

References TGAFILE::bitCount, TGAFILE::imageData, TGAFILE::imageHeight, TGAFILE::imageTypeCode, and TGAFILE::imageWidth.

00105                                                         {
00106   FILE *filePtr;
00107   unsigned char ucharBad;   // garbage unsigned char data
00108   short int   sintBad;  // garbage short int data
00109   long      imageSize;  // size of the TGA image
00110   int     colorMode;  // 4 for RGBA or 3 for RGB
00111   long      imageIdx; // counter variable
00112   unsigned char colorSwap;  // swap variable
00113 
00114   // open the TGA file
00115   filePtr = fopen(filename, "rb");
00116   if (!filePtr)
00117     return 0;
00118 
00119   // read first two bytes of garbage
00120   fread(&ucharBad, sizeof(unsigned char), 1, filePtr);
00121   fread(&ucharBad, sizeof(unsigned char), 1, filePtr);
00122 
00123   // read in the image type
00124   fread(&tgaFile->imageTypeCode, sizeof(unsigned char), 1, filePtr);
00125 
00126   // for our purposes, the image type should be either a 2 (color) or a 3 (greyscale)
00127   if ((tgaFile->imageTypeCode != 2) && (tgaFile->imageTypeCode != 3))
00128     {
00129       fclose(filePtr);
00130       return 0;
00131     }
00132 
00133   // read 13 bytes of garbage data
00134   fread(&sintBad, sizeof(short int), 1, filePtr);
00135   fread(&sintBad, sizeof(short int), 1, filePtr);
00136   fread(&ucharBad, sizeof(unsigned char), 1, filePtr);
00137   fread(&sintBad, sizeof(short int), 1, filePtr);
00138   fread(&sintBad, sizeof(short int), 1, filePtr);
00139 
00140   // read image dimensions
00141   fread(&tgaFile->imageWidth, sizeof(short int), 1, filePtr);
00142   fread(&tgaFile->imageHeight, sizeof(short int), 1, filePtr);
00143 
00144   // read image bit depth
00145   fread(&tgaFile->bitCount, sizeof(unsigned char), 1, filePtr);
00146 
00147   // read 1 byte of garbage data
00148   fread(&ucharBad, sizeof(unsigned char), 1, filePtr);
00149 
00150   // colorMode -> 3 = BGR, 4 = BGRA
00151   colorMode = tgaFile->bitCount / 8;
00152   imageSize = tgaFile->imageWidth * tgaFile->imageHeight * colorMode;
00153 
00154   // allocate memory for image data
00155   tgaFile->imageData = (unsigned char*)malloc(sizeof(unsigned char)*imageSize);
00156 
00157   // read in image data
00158   fread(tgaFile->imageData, sizeof(unsigned char), imageSize, filePtr);
00159 
00160   // change BGR to RGB so OpenGL can read the image data
00161   for (imageIdx = 0; imageIdx < imageSize; imageIdx += colorMode)
00162     {
00163       colorSwap = tgaFile->imageData[imageIdx];
00164       tgaFile->imageData[imageIdx] = tgaFile->imageData[imageIdx + 2];
00165       tgaFile->imageData[imageIdx + 2] = colorSwap;
00166     }
00167 
00168   // close the file
00169   fclose(filePtr);
00170 
00171   return 1;
00172 }

void SaveScreenshot ( const char *  filename,
int  x,
int  y 
)

Definition at line 245 of file image.cpp.

References WriteTGAFile().

Referenced by IGameState::TakeScreenshot().

00245                                                         {
00246   void *imageData;
00247 
00248   imageData = malloc(x*y*3);    // allocate memory for the imageData
00249   memset(imageData, 0, x*y*3);  // clear imageData memory contents
00250 
00251   // read the image data from the window
00252   glReadPixels(0, 0, x-1, y-1, GL_RGB, GL_UNSIGNED_BYTE, imageData);
00253 
00254   // write the image data to a file
00255   WriteTGAFile(filename, x, y, (unsigned char*)imageData);
00256 
00257   // free the image data memory
00258   free(imageData);
00259 }

int WriteTGAFile ( const char *  filename,
short int  width,
short int  height,
unsigned char *  imageData 
)

Definition at line 175 of file image.cpp.

Referenced by SaveScreenshot().

00175                                                                                                     {
00176   unsigned char byteSkip;               // used to fill in the data fields that we don't care about
00177   short int     shortSkip;
00178   unsigned char imageType;  // type of image we're writing to file
00179   int           colorMode;
00180   unsigned char colorSwap;
00181   int       imageIdx;
00182   unsigned char bitDepth;
00183   long      imageSize;
00184   FILE *filePtr;
00185 
00186   // create file for writing binary mode
00187   filePtr = fopen(filename, "wb");
00188   if (!filePtr)
00189     {
00190       fclose(filePtr);
00191       return 0;
00192     }
00193 
00194   imageType = 2;    // RGB, uncompressed
00195   bitDepth = 24;    // 24-bitdepth
00196   colorMode = 3;    // RGB color mode
00197 
00198   byteSkip = 0;
00199   shortSkip = 0;
00200 
00201   // write 2 bytes of blank data
00202   fwrite(&byteSkip, sizeof(unsigned char), 1, filePtr);
00203   fwrite(&byteSkip, sizeof(unsigned char), 1, filePtr);
00204 
00205   // write imageType
00206   fwrite(&imageType, sizeof(unsigned char), 1, filePtr);
00207 
00208   fwrite(&shortSkip, sizeof(short int), 1, filePtr);
00209   fwrite(&shortSkip, sizeof(short int), 1, filePtr);
00210   fwrite(&byteSkip, sizeof(unsigned char), 1, filePtr);
00211   fwrite(&shortSkip, sizeof(short int), 1, filePtr);
00212   fwrite(&shortSkip, sizeof(short int), 1, filePtr);
00213 
00214   // write image dimensions
00215   fwrite(&width, sizeof(short int), 1, filePtr);
00216   fwrite(&height, sizeof(short int), 1, filePtr);
00217   fwrite(&bitDepth, sizeof(unsigned char), 1, filePtr);
00218 
00219   // write 1 byte of blank data
00220   fwrite(&byteSkip, sizeof(unsigned char), 1, filePtr);
00221 
00222   // calculate the image size
00223   imageSize = width * height * colorMode;
00224 
00225   // change image data from RGB to BGR
00226   for (imageIdx = 0; imageIdx < imageSize ; imageIdx += colorMode)
00227     {
00228       colorSwap = imageData[imageIdx];
00229       imageData[imageIdx] = imageData[imageIdx + 2];
00230       imageData[imageIdx + 2] = colorSwap;
00231     }
00232 
00233   // write the image data
00234   fwrite(imageData, sizeof(unsigned char), imageSize, filePtr);
00235 
00236   // close the file
00237   fclose(filePtr);
00238 
00239   return 1;
00240 }


Generated on Mon Jan 8 22:34:13 2007 for CrownandCutlass by  doxygen 1.4.7