Index: orxonox/branches/images/importer/material.cc
===================================================================
--- orxonox/branches/images/importer/material.cc	(revision 3093)
+++ orxonox/branches/images/importer/material.cc	(revision 3094)
@@ -12,4 +12,6 @@
    main-programmer: Benjamin Grauer
    co-programmer: ...
+
+   TGA-code: borrowed from nehe-Tutorials
 */
 
@@ -357,4 +359,21 @@
 }
 
+bool Material::loadTexToGL (Image* pImage, GLuint* texture)
+{
+  glGenTextures(1, texture);
+  glBindTexture(GL_TEXTURE_2D, *texture);
+  /* not Working, and not needed. 
+  glTexImage2D( GL_TEXTURE_2D, 0, 3, width,
+		height, 0, GL_BGR,
+		GL_UNSIGNED_BYTE, map->pixels );
+  */ 
+  gluBuild2DMipmaps(GL_TEXTURE_2D, 3, pImage->width, pImage->height, GL_RGB, GL_UNSIGNED_BYTE, pImage->data);
+  
+  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
+  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);	
+}
+
+
+
 /**
    \brief Makes the Programm ready to Read-in a texture-File
@@ -376,4 +395,10 @@
 	printf ("Requested jpeg-image. Trying to Import\n");
       return loadJPG(imageName, texture);
+    }
+  else if (!strncmp(imageName+strlen(imageName)-4, ".tga", 4))
+    {
+      if (verbose >=2)
+	printf ("Requested tga-image. Trying to Import\n");
+      return loadTGA(imageName, texture);
     }
   else
@@ -412,5 +437,5 @@
   
   // read the width
-  if ((i = fread(&pImage->sizeX, 4, 1, file)) != 1) 
+  if ((i = fread(&pImage->width, 4, 1, file)) != 1) 
     {
       if (verbose >=1)
@@ -419,5 +444,5 @@
     }
   // read the height 
-  if ((i = fread(&pImage->sizeY, 4, 1, file)) != 1) 
+  if ((i = fread(&pImage->height, 4, 1, file)) != 1) 
     {
       if (verbose>=1)
@@ -427,5 +452,5 @@
   
   // calculate the size (assuming 24 bits or 3 bytes per pixel).
-  size = pImage->sizeX * pImage->sizeY * 3;
+  size = pImage->width * pImage->height * 3;
   
   // read the planes
@@ -565,9 +590,9 @@
   // Get the image dimensions and row span to read in the pixel data
   pImageData->rowSpan = cinfo->image_width * cinfo->num_components;
-  pImageData->sizeX   = cinfo->image_width;
-  pImageData->sizeY   = cinfo->image_height;
+  pImageData->width   = cinfo->image_width;
+  pImageData->height   = cinfo->image_height;
   
   // Allocate memory for the pixel buffer
-  pImageData->data = new unsigned char[pImageData->rowSpan * pImageData->sizeY];
+  pImageData->data = new unsigned char[pImageData->rowSpan * pImageData->height];
   
   // Here we use the library's state variable cinfo.output_scanline as the
@@ -575,6 +600,6 @@
   
   // Create an array of row pointers
-  unsigned char** rowPtr = new unsigned char*[pImageData->sizeY];
-  for (int i = 0; i < pImageData->sizeY; i++)
+  unsigned char** rowPtr = new unsigned char*[pImageData->height];
+  for (int i = 0; i < pImageData->height; i++)
     rowPtr[i] = &(pImageData->data[i*pImageData->rowSpan]);
   
@@ -594,18 +619,324 @@
 }
 
-bool Material::loadTexToGL (Image* pImage, GLuint* texture)
-{
-  glGenTextures(1, texture);
-  glBindTexture(GL_TEXTURE_2D, *texture);
-  /* not Working, and not needed. 
-  glTexImage2D( GL_TEXTURE_2D, 0, 3, width,
-		height, 0, GL_BGR,
-		GL_UNSIGNED_BYTE, map->pixels );
-  */ 
-  gluBuild2DMipmaps(GL_TEXTURE_2D, 3, pImage->sizeX, pImage->sizeY, GL_RGB, GL_UNSIGNED_BYTE, pImage->data);
-  
-  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
-  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);	
-  
-
-}
+
+
+
+bool Material::loadTGA(const char * tgaName, GLuint* texture)
+{
+  typedef struct
+  {
+    GLubyte Header[12];									/* TGA File Header */
+  } TGAHeader;
+ TGAHeader tgaHeader;			
+
+  GLubyte uTGAcompare[12] = {0,0,2, 0,0,0,0,0,0,0,0,0};	/* Uncompressed TGA Header */
+  GLubyte cTGAcompare[12] = {0,0,10,0,0,0,0,0,0,0,0,0};	/* Compressed TGA Header */
+  FILE * fTGA;
+  fTGA = fopen(tgaName, "rb");
+
+  if(fTGA == NULL)
+    {
+      printf("Error could not open texture file: %s\n", tgaName);
+      return false;
+    }
+  
+  if(fread(&tgaHeader, sizeof(TGAHeader), 1, fTGA) == 0) /// hmm... don't know if this is io
+    {
+      printf("Error could not read file header of %s\n", tgaName);
+      if(fTGA != NULL)
+	{
+	  fclose(fTGA);
+	}
+      return false;
+    }
+  
+  if(memcmp(uTGAcompare, &tgaHeader, sizeof(TGAHeader)) == 0)
+    {
+      loadUncompressedTGA(tgaName, fTGA, texture);
+      if (fTGA)
+	fclose (fTGA);
+    }
+  else if(memcmp(cTGAcompare, &tgaHeader, sizeof(TGAHeader)) == 0)
+    {
+      //loadCompressedTGA(tgaName, fTGA, texture);
+	if (fTGA)
+	  fclose (fTGA);
+    }
+  else
+    {
+      printf("Error TGA file be type 2 or type 10\n");
+      if (fTGA)
+	fclose(fTGA);
+      return false;
+    }
+  return true;
+}
+
+bool Material::loadUncompressedTGA(const char * filename, FILE * fTGA, GLuint* texture)
+{
+  GLubyte header[6];      // First 6 Useful Bytes From The Header 
+  GLuint  bytesPerPixel;  // Holds Number Of Bytes Per Pixel Used In The TGA File 
+  GLuint  imageSize;	  // Used To Store The Image Size When Setting Aside Ram 
+  GLuint  temp;		  // Temporary Variable
+  GLuint  type;
+  GLuint  Height;	  // Height of Image 
+  GLuint  Width;	  // Width of Image 
+  GLuint  Bpp;		  // Bits Per Pixel 
+
+  Image* pImage = new Image;
+  GLuint cswap;
+  if(fread(header, sizeof(header), 1, fTGA) == 0)
+    {
+      printf("Error could not read info header\n");
+      return false;
+    }
+  
+  Width = pImage->width  = header[1] * 256 + header[0];
+  Height =  pImage->height = header[3] * 256 + header[2];
+  Bpp = pImage->bpp = header[4];
+  // Make sure all information is valid
+  if((pImage->width <= 0) || (pImage->height <= 0) || ((pImage->bpp != 24) && (pImage->bpp !=32)))
+    {
+      printf("Error invalid texture information\n");
+      return false;
+    }
+  
+  if(pImage->bpp == 24)	
+    {
+      pImage->type = GL_RGB;
+    }
+  else
+    {
+      pImage->type = GL_RGBA;
+    }
+  
+  bytesPerPixel	= (Bpp / 8);
+  imageSize = (bytesPerPixel * Width * Height);
+  pImage->data = (GLubyte*) malloc(imageSize);
+  
+  if(pImage->data == NULL)
+    {
+      printf("Error could not allocate memory for image\n");
+      return false;
+    }
+  
+  if(fread(pImage->data, 1, imageSize, fTGA) != imageSize)
+    {
+      printf("Error could not read image data\n");
+      if(pImage->data != NULL)
+	{
+	  free(pImage->data);
+	}
+      return false;
+    }
+  
+  /* Byte Swapping Optimized By Steve Thomas */
+  for(cswap = 0; cswap < (int)imageSize; cswap += bytesPerPixel)
+    {
+      pImage->data[cswap] ^= pImage->data[cswap+2] ^=
+	pImage->data[cswap] ^= pImage->data[cswap+2];
+    }
+  
+  loadTexToGL (pImage, texture);
+
+  return true;
+}
+
+#ifdef __auskomentiert__
+bool Material::LoadCompressedTGA(Texture * texture,const char * filename, FILE * fTGA)
+{
+  GLuint pixelcount	= Height * Width;							/* Nuber of pixels in the image */
+  GLuint currentpixel	= 0;												/* Current pixel being read */
+  GLuint currentbyte	= 0;												/* Current byte */
+  GLubyte * colorbuffer = (GLubyte *)malloc(bytesPerPixel);			/* Storage for 1 pixel */
+  
+  if(fread(header, sizeof(header), 1, fTGA) == 0)				/* Attempt to read header */
+    {
+      printf("Error could not read info header");							/* Display Error */
+      if(fTGA != NULL)													/* If file is open */
+	{
+	  fclose(fTGA);													/* Close it */
+	}
+      return false;														/* Return failed */
+    }
+  
+  pImage->width  = header[1] * 256 + header[0];					/* Determine The TGA Width	(highbyte*256+lowbyte) */
+  pImage->height = header[3] * 256 + header[2];					/* Determine The TGA Height	(highbyte*256+lowbyte) */
+  pImage->bpp	= header[4];										/* Determine Bits Per Pixel */
+  Width		= pImage->width;										/* Copy width to local structure */
+  Height		= pImage->height;										/* Copy width to local structure */
+  Bpp			= pImage->bpp;											/* Copy width to local structure */
+  
+  if((pImage->width <= 0) || (pImage->height <= 0) || ((pImage->bpp != 24) && (pImage->bpp !=32)))	/*Make sure all pImage info is ok */
+    {
+      printf("Error Invalid pImage information");						/* If it isnt...Display error */
+      if(fTGA != NULL)													/* Check if file is open */
+	{
+	  fclose(fTGA);													/* Ifit is, close it */
+	}
+      return false;														/* Return failed */
+    }
+  
+  bytesPerPixel	= (Bpp / 8);									/* Compute BYTES per pixel */
+  imageSize		= (bytesPerPixel * Width * Height);		/* Compute amout of memory needed to store image */
+  pImage->data	= (GLubyte *)malloc(imageSize);					/* Allocate that much memory */
+  
+  if(pImage->data == NULL)											/* If it wasnt allocated correctly.. */
+    {
+      printf("Error could not allocate memory for image");				/* Display Error */
+      fclose(fTGA);														/* Close file */
+      return false;														/* Return failed */
+    }
+  
+  do
+    {
+      GLubyte chunkheader = 0;											/* Storage for "chunk" header */
+      
+      if(fread(&chunkheader, sizeof(GLubyte), 1, fTGA) == 0)				/* Read in the 1 byte header */
+	{
+	  printf("Error could not read RLE header");						/*Display Error */
+	  if(fTGA != NULL)												/* If file is open */
+	    {
+	      fclose(fTGA);												/* Close file */
+	    }
+	  if(pImage->data != NULL)									/* If there is stored image data */
+	    {
+	      free(pImage->data);									/* Delete image data */
+	    }
+	  return false;													/* Return failed */
+	}
+      
+      if(chunkheader < 128)												/* If the ehader is < 128, it means the that is the number of RAW color packets minus 1 */
+	{
+	  short counter;													/* that follow the header */
+	  chunkheader++;													/* add 1 to get number of following color values */
+	  for(counter = 0; counter < chunkheader; counter++)				/* Read RAW color values */
+	    {
+	      if(fread(colorbuffer, 1, bytesPerPixel, fTGA) != bytesPerPixel) /* Try to read 1 pixel */
+		{
+		  printf("Error could not read image data");				/* IF we cant, display an error */
+		  
+		  if(fTGA != NULL)										/* See if file is open */
+		    {
+		      fclose(fTGA);										/* If so, close file */
+		    }
+		  
+		  if(colorbuffer != NULL)									/* See if colorbuffer has data in it */
+		    {
+		      free(colorbuffer);									/* If so, delete it */
+		    }
+		  
+		  if(pImage->data != NULL)										/* See if there is stored Image data */
+		    {
+		      free(pImage->data);										/* If so, delete it too */
+		    }
+		  
+		  return false;														/* Return failed */
+		}
+	      /* write to memory */
+	      pImage->data[currentbyte		] = colorbuffer[2];				    /* Flip R and B vcolor values around in the process */
+	      pImage->data[currentbyte + 1	] = colorbuffer[1];
+	      pImage->data[currentbyte + 2	] = colorbuffer[0];
+	      
+	      if(bytesPerPixel == 4)												/* if its a 32 bpp image */
+		{
+		  pImage->data[currentbyte + 3] = colorbuffer[3];				/* copy the 4th byte */
+		}
+	      
+	      currentbyte += bytesPerPixel;										/* Increase thecurrent byte by the number of bytes per pixel */
+	      currentpixel++;															/* Increase current pixel by 1 */
+	      
+	      if(currentpixel > pixelcount)											/* Make sure we havent read too many pixels */
+		{
+		  printf("Error too many pixels read");								/* if there is too many... Display an error! */
+		  
+		  if(fTGA != NULL)													/* If there is a file open */
+		    {
+		      fclose(fTGA);													/* Close file */
+		    }
+		  
+		  if(colorbuffer != NULL)												/* If there is data in colorbuffer */
+		    {
+		      free(colorbuffer);												/* Delete it */
+		    }
+		  
+		  if(pImage->data != NULL)										/* If there is Image data */
+		    {
+		      free(pImage->data);										/* delete it */
+		    }
+		  
+		  return false;														/* Return failed */
+		}
+	    }
+	}
+      else																			/* chunkheader > 128 RLE data, next color  reapeated chunkheader - 127 times */
+	{
+	  short counter;
+	  chunkheader -= 127;															/* Subteact 127 to get rid of the ID bit */
+	  if(fread(colorbuffer, 1, bytesPerPixel, fTGA) != bytesPerPixel)		/* Attempt to read following color values */
+	    {
+	      printf("Error could not read from file");			/* If attempt fails.. Display error (again) */
+	      
+	      if(fTGA != NULL)														/* If thereis a file open */
+		{
+		  fclose(fTGA);														/* Close it */
+		}
+	      
+	      if(colorbuffer != NULL)													/* If there is data in the colorbuffer */
+		{
+		  free(colorbuffer);													/* delete it */
+		}
+	      
+	      if(pImage->data != NULL)											/* If thereis image data */
+		{
+		  free(pImage->data);											/* delete it */
+		}
+	      
+	      return false;															/* return failed */
+	    }
+	  
+	  for(counter = 0; counter < chunkheader; counter++)					/* copy the color into the image data as many times as dictated */
+	    {																			/* by the header */
+	      pImage->data[currentbyte		] = colorbuffer[2];					/* switch R and B bytes areound while copying */
+	      pImage->data[currentbyte + 1	] = colorbuffer[1];
+	      pImage->data[currentbyte + 2	] = colorbuffer[0];
+	      
+	      if(bytesPerPixel == 4)												/* If TGA images is 32 bpp */
+		{
+		  pImage->data[currentbyte + 3] = colorbuffer[3];				/* Copy 4th byte */
+		}
+	      
+	      currentbyte += bytesPerPixel;										/* Increase current byte by the number of bytes per pixel */
+	      currentpixel++;															/* Increase pixel count by 1 */
+	      
+	      if(currentpixel > pixelcount)											/* Make sure we havent written too many pixels */
+		{
+		  printf("Error too many pixels read");								/* if there is too many... Display an error! */
+		  
+		  if(fTGA != NULL)													/* If there is a file open */
+		    {
+		      fclose(fTGA);													/* Close file */
+		    }
+		  
+		  if(colorbuffer != NULL)												/* If there is data in colorbuffer */
+		    {
+		      free(colorbuffer);												/* Delete it */
+		    }
+		  
+		  if(pImage->data != NULL)										/* If there is Image data */
+		    {
+		      free(pImage->data);										/* delete it */
+		    }
+		  
+		  return false;														/* Return failed */
+		}
+	    }
+	}
+    }
+  
+  while(currentpixel < pixelcount);													/* Loop while there are still pixels left */
+  fclose(fTGA);																		/* Close the file */
+  return true;																		/* return success */
+}
+
+#endif
Index: orxonox/branches/images/importer/material.h
===================================================================
--- orxonox/branches/images/importer/material.h	(revision 3093)
+++ orxonox/branches/images/importer/material.h	(revision 3094)
@@ -61,6 +61,8 @@
   {
     int rowSpan;
-    unsigned long sizeX;
-    unsigned long sizeY;
+    GLuint width;
+    GLuint height;
+    GLuint bpp;
+    GLuint type;
     GLubyte *data;
   };
@@ -86,4 +88,6 @@
 
   // TEXTURING
+  bool loadTexToGL (Image* pImage, GLuint* texture);
+
   bool loadImage(char* imageName, GLuint* texture);
 
@@ -93,5 +97,10 @@
   void decodeJPG(jpeg_decompress_struct* cinfo, Image *pImageData);
 
-  bool loadTexToGL (Image* pImage, GLuint* texture);
+  /// TGA ///
+
+  bool loadTGA(const char * tgaName, GLuint* texture);
+  bool loadUncompressedTGA(const char * filename, FILE * fTGA, GLuint* texture);
+  //  bool LoadCompressedTGA(const char * filename, FILE * fTGA, GLuint* texture);
+
 };
 #endif
