Index: orxonox/branches/images/importer/material.cc
===================================================================
--- orxonox/branches/images/importer/material.cc	(revision 3089)
+++ orxonox/branches/images/importer/material.cc	(revision 3090)
@@ -391,60 +391,97 @@
 bool Material::loadBMP (char* bmpName, GLuint* texture)
 {
-  SDL_Surface* map;
-  if (map = SDL_LoadBMP(bmpName))
-    {
-      loadTexToGL (map->w, map->h, map->pixels, texture);
-      if ( map )
-	SDL_FreeSurface( map );
-
-      return true;
-    }
-  else
-    return false;
-}
-
-bool Material::loadJPG (char* jpgName, GLuint* texture)
-{
-  struct jpeg_decompress_struct cinfo;
-  tImageJPG *pImage = NULL;
-  FILE *pFile;
-  
-  // Open a file pointer to the jpeg file and check if it was found and opened 
-  if((pFile = fopen(jpgName, "rb")) == NULL) 
-    {
-      // Display an error message saying the file was not found, then return NULL
-      printf("Unable to load JPG File %s.\n", jpgName);
-      return false;
-    }
-  
-  // Create an error handler
-  jpeg_error_mgr jerr;
-  
-  // Have our compression info object point to the error handler address
-  cinfo.err = jpeg_std_error(&jerr);
-  
-  // Initialize the decompression object
-  jpeg_create_decompress(&cinfo);
-  
-  // Specify the data source (Our file pointer)	
-  jpeg_stdio_src(&cinfo, pFile);
-  
-  // Allocate the structure that will hold our eventual jpeg data (must free it!)
-  pImage = (tImageJPG*)malloc(sizeof(tImageJPG));
-  
-  // Decode the jpeg file and fill in the image data structure to pass back
-  decodeJPG(&cinfo, pImage);
-  
-  // This releases all the stored memory for reading and decoding the jpeg
-  jpeg_destroy_decompress(&cinfo);
-  
-  // Close the file pointer that opened the file
-  fclose(pFile);
-  
-
-  if(pImage == NULL)
-    exit(0);
-  
+  Image* pImage = new Image;
+  FILE *file;
+  unsigned long size;                 // size of the image in bytes.
+  unsigned long i;                    // standard counter.
+  unsigned short int planes;          // number of planes in image (must be 1) 
+  unsigned short int bpp;             // number of bits per pixel (must be 24)
+  GLuint temp;                          // temporary color storage for bgr-rgb conversion.
+
+  // make sure the file is there.
+  if ((file = fopen(bmpName, "rb"))==NULL)
+    {
+      if (verbose >=1)
+	printf("File Not Found : %s\n",bmpName);
+      return false;
+    }
+  // seek through the bmp header, up to the width/height:
+  fseek(file, 18, SEEK_CUR);
+  
+  // read the width
+  if ((i = fread(&pImage->sizeX, 4, 1, file)) != 1) 
+    {
+      if (verbose >=1)
+	printf("Error reading width from %s.\n", bmpName);
+      return false;
+    }
+  // read the height 
+  if ((i = fread(&pImage->sizeY, 4, 1, file)) != 1) 
+    {
+      if (verbose>=1)
+	printf("Error reading height from %s.\n", bmpName);
+      return false;
+    }
+  
+  // calculate the size (assuming 24 bits or 3 bytes per pixel).
+  size = pImage->sizeX * pImage->sizeY * 3;
+  
+  // read the planes
+  if ((fread(&planes, 2, 1, file)) != 1) 
+    {
+      if (verbose>=1)
+	printf("Error reading planes from %s.\n", bmpName);
+      return false;
+    }
+  if (planes != 1) 
+    {
+      if (verbose>=1)
+	printf("Planes from %s is not 1: %u\n", bmpName, planes);
+      return false;
+    }
+  
+  // read the bpp
+  if ((i = fread(&bpp, 2, 1, file)) != 1) 
+    {
+      if (verbose>=1)
+	printf("Error reading bpp from %s.\n", bmpName);
+      return false;
+    }
+  if (bpp != 24) 
+    {
+      if (verbose>=1)
+	printf("Bpp from %s is not 24: %u\n", bmpName, bpp);
+      return false;
+    }
+  
+  // seek past the rest of the bitmap header.
+  fseek(file, 24, SEEK_CUR);
+  
+  // read the data. 
+  pImage->data = (GLubyte *) malloc(size);
+  if (pImage->data == NULL) 
+    {
+      if (verbose>=1)
+	printf("Error allocating memory for color-corrected image data");
+      return false;	
+    }
+  
+  if ((i = fread(pImage->data, size, 1, file)) != 1) 
+    {
+      if (verbose>=1)
+	printf("Error reading image data from %s.\n", bmpName);
+      return false;
+    }
+  // reverse all of the colors. (bgr -> rgb)
+  for (i=0;i<size;i+=3) 
+    { 
+      temp = pImage->data[i];
+      pImage->data[i] = pImage->data[i+2];
+      pImage->data[i+2] = temp;
+    }
   loadTexToGL (pImage->sizeX, pImage->sizeY, pImage->data, texture);
+  
+  return true;
+
   if (pImage)
     {
@@ -456,8 +493,63 @@
       free(pImage);
     }
+
+}
+
+bool Material::loadJPG (char* jpgName, GLuint* texture)
+{
+  struct jpeg_decompress_struct cinfo;
+  Image *pImage = NULL;
+  FILE *pFile;
+  
+  // Open a file pointer to the jpeg file and check if it was found and opened 
+  if((pFile = fopen(jpgName, "rb")) == NULL) 
+    {
+      // Display an error message saying the file was not found, then return NULL
+      printf("Unable to load JPG File %s.\n", jpgName);
+      return false;
+    }
+  
+  // Create an error handler
+  jpeg_error_mgr jerr;
+  
+  // Have our compression info object point to the error handler address
+  cinfo.err = jpeg_std_error(&jerr);
+  
+  // Initialize the decompression object
+  jpeg_create_decompress(&cinfo);
+  
+  // Specify the data source (Our file pointer)	
+  jpeg_stdio_src(&cinfo, pFile);
+  
+  // Allocate the structure that will hold our eventual jpeg data (must free it!)
+  pImage = (Image*)malloc(sizeof(Image));
+  
+  // Decode the jpeg file and fill in the image data structure to pass back
+  decodeJPG(&cinfo, pImage);
+  
+  // This releases all the stored memory for reading and decoding the jpeg
+  jpeg_destroy_decompress(&cinfo);
+  
+  // Close the file pointer that opened the file
+  fclose(pFile);
+  
+
+  if(pImage == NULL)
+    exit(0);
+  
+  loadTexToGL (pImage->sizeX, pImage->sizeY, pImage->data, texture);
+  if (pImage)
+    {
+      if (pImage->data)
+	{
+	  free(pImage->data);
+	}
+      
+      free(pImage);
+    }
   return true;
 }
 
-void Material::decodeJPG(jpeg_decompress_struct* cinfo, tImageJPG *pImageData)
+void Material::decodeJPG(jpeg_decompress_struct* cinfo, Image* pImageData)
 {
   // Read in the header of the jpeg file
Index: orxonox/branches/images/importer/material.h
===================================================================
--- orxonox/branches/images/importer/material.h	(revision 3089)
+++ orxonox/branches/images/importer/material.h	(revision 3090)
@@ -66,9 +66,9 @@
 
  private:
-  struct tImageJPG
+  struct Image
   {
     int rowSpan;
-    int sizeX;
-    int sizeY;
+    unsigned long sizeX;
+    unsigned long sizeY;
     GLubyte *data;
   };
@@ -90,5 +90,5 @@
 
   bool loadJPG (char* jpgName, GLuint* texture);
-  void decodeJPG(jpeg_decompress_struct* cinfo, tImageJPG *pImageData);
+  void decodeJPG(jpeg_decompress_struct* cinfo, Image *pImageData);
 
   bool loadTexToGL (int hight, int width, void* rawImage, GLuint* texture);
