Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/parenting/src/importer/texture.cc @ 3345

Last change on this file since 3345 was 3345, checked in by bensch, 19 years ago

orxonox/branches/parenting: :loadscreen: now uses texture-class

File size: 23.9 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Benjamin Grauer
13   co-programmer: ...
14
15   TGA-code: borrowed from nehe-Tutorials
16
17*/
18
19
20#include "texture.h"
21
22// headers only for PathList
23#include <unistd.h>
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <stdlib.h>
27#include <fstream>
28
29/**
30   \brief creates a ned PathList.
31   
32   It is a good idea to use this as an initial List,
33   because if you give on a name the Path will not be checked for its existence.
34*/
35PathList::PathList()
36{
37  this->pathName = NULL;
38  this->next = NULL;
39}
40
41/**
42   \brief Creates a new PathList with a Name.
43   \param pName the Name of The Path.
44
45   This function just adds the Path without checking if it exists.
46*/
47PathList::PathList(char* pName)
48{
49  this->pathName = new char [strlen(pName)+1];
50  strcpy (this->pathName, pName);
51  this->next = NULL;
52}
53
54/**
55   \brief destroys a PathList
56
57   It does this by deleting the Name and then delete its preceding PathList.
58*/
59PathList::~PathList()
60{
61  if (this->pathName)
62    delete []this->pathName;
63  if (this->next)
64    delete this->next;
65}
66
67PathList* PathList::firstPath = NULL;
68
69/**
70   \returns A Pointer to the first Path of the Pathlist
71*/
72PathList* PathList::getInstance(void)
73{
74  if (firstPath)
75    return firstPath;
76  firstPath = new PathList();
77}
78/**
79   \brief Adds a new Pathlist Element.
80   \param pName
81   
82   Adding a Path automatically checks if the Path exists,
83   and if it does not it will not add it to the List.
84*/
85void PathList::addPath (char* pName)
86{
87  if (pName[0] == '\0')
88    {
89      PRINTF(3)("not Adding empty Path to the List.\n");
90      return;
91    }
92  char* tmpPName = new char[strlen(pName)];
93  strncpy(tmpPName, pName, strlen(pName)-1);
94  tmpPName[strlen(pName)-1] = '\0';
95  if (access (tmpPName, F_OK) == 0)
96    {
97      struct stat status;
98      stat(tmpPName, &status);
99      if (status.st_mode & S_IFDIR)
100        {
101          PRINTF(2)("Adding Path %s to the PathList.\n", pName);
102          PathList* tmpPathList = this;
103          while (tmpPathList->next)
104            tmpPathList = tmpPathList->next;
105          tmpPathList->next = new PathList(pName);
106        }
107      else
108        PRINTF(2)("You tried to add non-folder %s to a PathList.\n", tmpPName);
109    }
110  else
111      PRINTF(2)("You tried to add non-existing folder %s to a PathList.\n", tmpPName);
112  delete []tmpPName;
113}
114
115
116
117/**
118   \brief Constructor for a Texture
119*/
120Texture::Texture(void)
121{
122  this->pImage = new Image;
123  this->pImage->data = NULL;
124  this->texture = 0;
125}
126
127/**
128   \brief Destructor of a Texture
129   
130   Frees Data, and deletes the textures from GL
131*/
132Texture::~Texture(void)
133{
134  if (this->pImage->data)
135    delete []this->pImage->data;
136  delete pImage;
137  if (this->texture)
138    glDeleteTextures(1, &this->texture);
139}
140
141/**
142   \brief Searches for a Texture inside one of the defined Paths
143   \param texName The name of the texture o search for.
144   \returns pathName+texName if texName was found in the pathList. NULL if the Texture is not found.
145*/
146char* Texture::searchTextureInPaths(char* texName) const
147{
148  char* tmpName = NULL;
149  PathList* pList = PathList::getInstance();
150  while (pList)
151    {
152      if (pList->pathName)
153        {
154          tmpName = new char [strlen(pList->pathName)+strlen(texName)+1];
155          strcpy(tmpName, pList->pathName);
156        }
157      else
158        {
159          tmpName = new char [strlen(texName)+1];
160          tmpName[0]='\0';
161        }
162      strcat(tmpName, texName);
163      if (access (tmpName, F_OK) == 0)
164        return tmpName;
165     
166      if (tmpName)
167        delete []tmpName;
168      tmpName = NULL;
169      pList = pList->next;
170    }
171  return NULL;
172}
173
174/**
175   \brief a Simple function that switches two char values
176   \param a The first value
177   \param b The second value
178*/
179inline void Texture::swap (unsigned char &a, unsigned char &b)
180{
181  unsigned char temp;
182  temp = a;
183  a    = b;
184  b    = temp;
185}
186
187
188/**
189   \brief Loads a Texture to the openGL-environment.
190   \param pImage The Image to load to openGL
191   \param texture The Texture to apply it to.
192*/
193bool Texture::loadTexToGL (Image* pImage)
194{
195  PRINTF(2)("Loading texture to OpenGL-Environment.\n");
196  glGenTextures(1, &this->texture);
197  glBindTexture(GL_TEXTURE_2D, this->texture);
198  /* not Working, and not needed.
199  glTexImage2D( GL_TEXTURE_2D, 0, 3, width,
200                height, 0, GL_BGR,
201                GL_UNSIGNED_BYTE, map->pixels );
202  */ 
203  gluBuild2DMipmaps(GL_TEXTURE_2D, 3, pImage->width, pImage->height, GL_RGB, GL_UNSIGNED_BYTE, pImage->data);
204 
205  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
206  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR); 
207}
208
209
210#ifdef HAVE_SDL_SDL_IMAGE_H
211bool Texture::loadImage(char* imageName)
212{
213  char* imgNameWithPath = searchTextureInPaths(imageName);
214  if (imgNameWithPath)
215    {
216      SDL_Surface* map;
217      map=IMG_Load(imgNameWithPath);
218      if(!map)
219        {
220          PRINTF(1)("IMG_Load: %s\n", IMG_GetError());
221          return false;
222        }
223      pImage->height = map->h;
224      pImage->width  = map->w;
225      pImage->data   = (GLubyte*)map->pixels;
226      pImage->bpp    = 3;//map->BytesPerPixel;
227      if( !IMG_isPNG(SDL_RWFromFile(imgNameWithPath, "rb")) && !IMG_isJPG(SDL_RWFromFile(imgNameWithPath, "rb")))
228        for (int i=0;i<map->h * map->w *3;i+=3)
229          { 
230            GLuint temp = pImage->data[i];
231            pImage->data[i] = pImage->data[i+2];
232            pImage->data[i+2] = temp;
233          }
234      /* this is the real swapping algorithm */
235      for( int i = 0 ; i < (pImage->height / 2) ; ++i )
236        for( int j = 0 ; j < pImage->width * pImage->bpp; j += pImage->bpp )
237          for(int k = 0; k < pImage->bpp; ++k)
238            swap( pImage->data[ (i * pImage->width * pImage->bpp) + j + k], pImage->data[ ( (pImage->height - i - 1) * pImage->width * pImage->bpp ) + j + k]);
239 
240      this->loadTexToGL (this->pImage);
241    }
242  else
243    {
244      PRINTF(1)("Image not Found: %s\n", imgNameWithPath);
245      return false;
246    }
247}
248
249
250#else /* HAVE_SDL_SDL_IMAGE_H */
251/**
252   \brief Makes the Programm ready to Read-in a texture-File
253   1. Checks what type of Image should be imported
254   2. ToDO: Checks where to find the Image
255*/
256bool Texture::loadImage(char* imageName)
257{
258  char* imgNameWithPath = searchTextureInPaths(imageName);
259  if (imgNameWithPath)
260    {
261      if (!strncmp(imgNameWithPath+strlen(imgNameWithPath)-4, ".bmp", 4))
262        {
263          PRINTF(3)("Requested bmp-image. Trying to Import.\n");
264          return this->loadBMP(imgNameWithPath);
265        }
266     
267      else if (!strncmp(imgNameWithPath+strlen(imgNameWithPath)-4, ".jpg", 4) || !strncmp(imgNameWithPath+strlen(imgNameWithPath)-5, ".jpg", 5))
268        {
269          PRINTF(3)("Requested jpeg-image. Trying to Import\n");
270          return this->loadJPG(imgNameWithPath);
271        }
272      else if (!strncmp(imgNameWithPath+strlen(imgNameWithPath)-4, ".tga", 4))
273        {
274          PRINTF(3)("Requested tga-image. Trying to Import\n");
275          return this->loadTGA(imgNameWithPath);
276        }
277      else if (!strncmp(imgNameWithPath+strlen(imgNameWithPath)-4, ".png", 4))
278        {
279          PRINTF(3)("Requested png-image. Trying to Import\n");
280          return this->loadPNG(imgNameWithPath);
281        }
282      else
283        {
284          PRINTF(1)("Requested Image was not recognized in its type. (Maybe a type-Cast-error.)\n FileName: %s", imgNameWithPath);
285          return false;
286        }
287    }
288  else
289    {
290      PRINTF(1)("Image not Found: %s\n", imgNameWithPath);
291      return false;
292    }
293}
294
295/**
296   \brief reads in a Windows BMP-file, and imports it to openGL.
297   \param bmpName The name of the Image to load.
298   \param texture A pointer to the Texture which should be read to.
299*/
300bool Texture::loadBMP (char* bmpName)
301{
302  FILE *file;
303  unsigned long size;                 // size of the image in bytes.
304  unsigned long i;                    // standard counter.
305  unsigned short int planes;          // number of planes in image (must be 1)
306  unsigned short int bpp;             // number of bits per pixel (must be 24)
307  GLuint temp;                          // temporary color storage for bgr-rgb conversion.
308
309  // make sure the file is there.
310  if ((file = fopen(bmpName, "rb"))==NULL)
311    {
312      PRINTF(1)("File Not Found : %s\n",bmpName);
313      return false;
314    }
315  // seek through the bmp header, up to the width/height:
316  fseek(file, 18, SEEK_CUR);
317 
318  // read the width
319  if ((i = fread(&pImage->width, 4, 1, file)) != 1) 
320    {
321      PRINTF(1)("Error reading width from %s.\n", bmpName);
322      return false;
323    }
324  // read the height
325  if ((i = fread(&pImage->height, 4, 1, file)) != 1) 
326    {
327      PRINTF(1)("Error reading height from %s.\n", bmpName);
328      return false;
329    }
330 
331  // calculate the size (assuming 24 bits or 3 bytes per pixel).
332  size = pImage->width * pImage->height * 3;
333 
334  // read the planes
335  if ((fread(&planes, 2, 1, file)) != 1) 
336    {
337      PRINTF(1)("Error reading planes from %s.\n", bmpName);
338      return false;
339    }
340  if (planes != 1) 
341    {
342      PRINTF(1)("Planes from %s is not 1: %u\n", bmpName, planes);
343      return false;
344    }
345 
346  // read the bpp
347  if ((i = fread(&bpp, 2, 1, file)) != 1) 
348    {
349      PRINTF(1)("Error reading bpp from %s.\n", bmpName);
350      return false;
351    }
352  if (bpp != 24) 
353    {
354      PRINTF(1)("Bpp from %s is not 24: %u\n", bmpName, bpp);
355      return false;
356    }
357 
358  // seek past the rest of the bitmap header.
359  fseek(file, 24, SEEK_CUR);
360 
361  // read the data.
362  pImage->data = (GLubyte *) malloc(size);
363  if (pImage->data == NULL) 
364    {
365      PRINTF(1)("Error allocating memory for color-corrected image data");
366      return false;     
367    }
368 
369  if ((i = fread(pImage->data, size, 1, file)) != 1) 
370    {
371      PRINTF(1)("Error reading image data from %s.\n", bmpName);
372      return false;
373    }
374  fclose(file);
375
376  // reverse all of the colors. (bgr -> rgb)
377  for (i=0;i<size;i+=3) 
378    { 
379      temp = pImage->data[i];
380      pImage->data[i] = pImage->data[i+2];
381      pImage->data[i+2] = temp;
382    }
383  this->loadTexToGL (pImage);
384 
385
386  if (pImage)
387    {
388      if (pImage->data)
389        {
390          free(pImage->data);
391        }
392     
393      free(pImage);
394    }
395  return true;
396
397}
398
399/**
400   \brief reads in a jpg-file
401   \param jpgName the Name of the Image to load
402   \param texture a reference to the Texture to write the image to
403*/
404bool Texture::loadJPG (char* jpgName)
405{
406#ifdef HAVE_JPEGLIB_H
407  struct jpeg_decompress_struct cinfo;
408  Image *pImage = NULL;
409  FILE *pFile;
410 
411  // Open a file pointer to the jpeg file and check if it was found and opened
412  if((pFile = fopen(jpgName, "rb")) == NULL) 
413    {
414      // Display an error message saying the file was not found, then return NULL
415      PRINTF(1)("Unable to load JPG File %s.\n", jpgName);
416      return false;
417    }
418 
419  // Create an error handler
420  jpeg_error_mgr jerr;
421 
422  // Have our compression info object point to the error handler address
423  cinfo.err = jpeg_std_error(&jerr);
424 
425  // Initialize the decompression object
426  jpeg_create_decompress(&cinfo);
427 
428  // Specify the data source (Our file pointer)
429  jpeg_stdio_src(&cinfo, pFile);
430 
431  // Allocate the structure that will hold our eventual jpeg data (must free it!)
432  pImage = (Image*)malloc(sizeof(Image));
433 
434  // DECOFING
435  // Read in the header of the jpeg file
436  jpeg_read_header(&cinfo, TRUE);
437 
438  // Start to decompress the jpeg file with our compression info
439  jpeg_start_decompress(&cinfo);
440 
441  // Get the image dimensions and row span to read in the pixel data
442  pImage->rowSpan = cinfo.image_width * cinfo.num_components;
443  pImage->width   = cinfo.image_width;
444  pImage->height   = cinfo.image_height;
445 
446  // Allocate memory for the pixel buffer
447  pImage->data = new unsigned char[pImage->rowSpan * pImage->height];
448 
449  // Here we use the library's state variable cinfo.output_scanline as the
450  // loop counter, so that we don't have to keep track ourselves.
451 
452  // Create an array of row pointers
453  unsigned char** rowPtr = new unsigned char*[pImage->height];
454  for (int i = 0; i < pImage->height; i++)
455    rowPtr[i] = &(pImage->data[i*pImage->rowSpan]);
456 
457  // Now comes the juice of our work, here we extract all the pixel data
458  int rowsRead = 0;
459  while (cinfo.output_scanline < cinfo.output_height) 
460    {
461      // Read in the current row of pixels and increase the rowsRead count
462      rowsRead += jpeg_read_scanlines(&cinfo, &rowPtr[rowsRead], cinfo.output_height - rowsRead);
463    }
464 
465  // Delete the temporary row pointers
466  delete [] rowPtr;
467 
468  // Finish decompressing the data
469  jpeg_finish_decompress(&cinfo);//  decodeJPG(&cinfo, pImage);
470 
471  // This releases all the stored memory for reading and decoding the jpeg
472  jpeg_destroy_decompress(&cinfo);
473 
474  // Close the file pointer that opened the file
475  fclose(pFile);
476 
477
478  if(pImage == NULL)
479    exit(0);
480 
481  this->loadTexToGL (pImage);
482  if (pImage)
483    {
484      if (pImage->data)
485        {
486          free(pImage->data);
487        }
488     
489      free(pImage);
490    }
491  return true;
492#else /* HAVE_JPEGLIB_H */
493  PRINTF(1)("sorry, but you did not compile with jpeg-support.\nEither install SDL_image or jpeglib, and recompile to see the image\n");
494  return false;
495#endif /* HAVE_JPEGLIB_H */
496
497}
498
499/**
500   \brief reads in a tga-file
501   \param tgaName the Name of the Image to load
502   \param texture a reference to the Texture to write the image to
503*/
504bool Texture::loadTGA(const char * tgaName)
505{
506  typedef struct
507  {
508    GLubyte Header[12];
509  } TGAHeader;
510  TGAHeader tgaHeader;                 
511 
512  GLubyte uTGAcompare[12] = {0,0,2, 0,0,0,0,0,0,0,0,0}; // Uncompressed TGA Header
513  GLubyte cTGAcompare[12] = {0,0,10,0,0,0,0,0,0,0,0,0}; // Compressed TGA Header
514  FILE * fTGA;
515  fTGA = fopen(tgaName, "rb");
516
517  if(fTGA == NULL)
518    {
519      PRINTF(1)("Error could not open texture file: %s\n", tgaName);
520      return false;
521    }
522 
523  if(fread(&tgaHeader, sizeof(TGAHeader), 1, fTGA) == 0)
524    {
525      PRINTF(1)("Error could not read file header of %s\n", tgaName);
526      if(fTGA != NULL)
527        {
528          fclose(fTGA);
529        }
530      return false;
531    }
532 
533  if(memcmp(uTGAcompare, &tgaHeader, sizeof(TGAHeader)) == 0)
534    {
535      loadUncompressedTGA(tgaName, fTGA);
536      if (fTGA)
537        fclose (fTGA);
538    }
539  else if(memcmp(cTGAcompare, &tgaHeader, sizeof(TGAHeader)) == 0)
540    {
541      loadCompressedTGA(tgaName, fTGA);
542        if (fTGA)
543          fclose (fTGA);
544    }
545  else
546    {
547      PRINTF(1)("Error TGA file be type 2 or type 10\n");
548      if (fTGA)
549        fclose(fTGA);
550      return false;
551    }
552  return true;
553}
554
555/**
556   \brief reads in an uncompressed tga-file
557   \param filename the Name of the Image to load
558   \param fTGA a Pointer to a File, that should be read
559   \param texture a reference to the Texture to write the image to
560*/
561bool Texture::loadUncompressedTGA(const char * filename, FILE * fTGA)
562{
563  GLubyte header[6];      // First 6 Useful Bytes From The Header
564  GLuint  bytesPerPixel;  // Holds Number Of Bytes Per Pixel Used In The TGA File
565  GLuint  imageSize;      // Used To Store The Image Size When Setting Aside Ram
566  GLuint  temp;           // Temporary Variable
567  GLuint  type;
568  GLuint  Height;         // Height of Image
569  GLuint  Width;          // Width of Image
570  GLuint  Bpp;            // Bits Per Pixel
571
572  GLuint cswap;
573  if(fread(header, sizeof(header), 1, fTGA) == 0)
574    {
575      PRINTF(1)("Error could not read info header\n");
576      return false;
577    }
578 
579  Width = pImage->width  = header[1] * 256 + header[0];
580  Height =  pImage->height = header[3] * 256 + header[2];
581  Bpp = pImage->bpp = header[4];
582  // Make sure all information is valid
583  if((pImage->width <= 0) || (pImage->height <= 0) || ((pImage->bpp != 24) && (pImage->bpp !=32)))
584    {
585      PRINTF(1)("Error invalid texture information\n");
586      return false;
587    }
588 
589  if(pImage->bpp == 24) 
590    {
591      pImage->type = GL_RGB;
592    }
593  else
594    {
595      pImage->type = GL_RGBA;
596    }
597 
598  bytesPerPixel = (Bpp / 8);
599  imageSize = (bytesPerPixel * Width * Height);
600  pImage->data = (GLubyte*) malloc(imageSize);
601 
602  if(pImage->data == NULL)
603    {
604      PRINTF(1)("Error could not allocate memory for image\n");
605      return false;
606    }
607 
608  if(fread(pImage->data, 1, imageSize, fTGA) != imageSize)
609    {
610      PRINTF(1)("Error could not read image data\n");
611      if(pImage->data != NULL)
612        {
613          free(pImage->data);
614        }
615      return false;
616    }
617 
618  for(cswap = 0; cswap < (int)imageSize; cswap += bytesPerPixel)
619    {
620      pImage->data[cswap] ^= pImage->data[cswap+2] ^=
621        pImage->data[cswap] ^= pImage->data[cswap+2];
622    }
623 
624  this->loadTexToGL (pImage);
625
626  return true;
627}
628
629/**
630   \brief reads in a compressed tga-file
631   \param filename the Name of the Image to load
632   \param fTGA a Pointer to a File, that should be read
633   \param texture a reference to the Texture to write the image to
634*/
635bool Texture::loadCompressedTGA(const char * filename, FILE * fTGA)
636{
637  GLubyte header[6];      // First 6 Useful Bytes From The Header
638  GLuint  bytesPerPixel;  // Holds Number Of Bytes Per Pixel Used In The TGA File
639  GLuint  imageSize;      // Used To Store The Image Size When Setting Aside Ram
640  GLuint  temp;           // Temporary Variable
641  GLuint  type;
642  GLuint  Height;         // Height of Image
643  GLuint  Width;          // Width of Image
644  GLuint  Bpp;            // Bits Per Pixel
645
646  if(fread(header, sizeof(header), 1, fTGA) == 0)
647    {
648      PRINTF(1)("Error could not read info header\n");
649      return false;
650    }
651 
652  Width = pImage->width  = header[1] * 256 + header[0];
653  Height = pImage->height = header[3] * 256 + header[2];
654  Bpp = pImage->bpp     = header[4];
655
656  GLuint pixelcount     = Height * Width;
657  GLuint currentpixel   = 0;
658  GLuint currentbyte    = 0;
659  GLubyte * colorbuffer = (GLubyte *)malloc(bytesPerPixel);
660
661  //Make sure all pImage info is ok
662  if((pImage->width <= 0) || (pImage->height <= 0) || ((pImage->bpp != 24) && (pImage->bpp !=32)))
663    {
664      PRINTF(1)("Error Invalid pImage information\n");
665      return false;
666    }
667 
668  bytesPerPixel = (Bpp / 8);
669  imageSize             = (bytesPerPixel * Width * Height);
670  pImage->data  = (GLubyte*) malloc(imageSize);
671 
672  if(pImage->data == NULL)
673    {
674      PRINTF(1)("Error could not allocate memory for image\n");
675      return false;
676    }
677 
678  do
679    {
680      GLubyte chunkheader = 0;
681     
682      if(fread(&chunkheader, sizeof(GLubyte), 1, fTGA) == 0)
683        {
684          PRINTF(1)("Error could not read RLE header\n");
685          if(pImage->data != NULL)
686            {
687              free(pImage->data);
688            }
689          return false;
690        }
691      // If the ehader is < 128, it means the that is the number of RAW color packets minus 1
692      if(chunkheader < 128)
693        {
694          short counter;
695          chunkheader++;
696          // Read RAW color values
697          for(counter = 0; counter < chunkheader; counter++)
698            { 
699              // Try to read 1 pixel
700              if(fread(colorbuffer, 1, bytesPerPixel, fTGA) != bytesPerPixel)
701                {
702                  PRINTF(1)("Error could not read image data\n");
703                  if(colorbuffer != NULL)
704                    {
705                      free(colorbuffer);
706                    }
707                 
708                  if(pImage->data != NULL)
709                    {
710                      free(pImage->data);
711                    }
712                 
713                  return false; 
714                }
715              // write to memory
716              // Flip R and B vcolor values around in the process
717              pImage->data[currentbyte    ] = colorbuffer[2];                               
718              pImage->data[currentbyte + 1] = colorbuffer[1];
719              pImage->data[currentbyte + 2] = colorbuffer[0];
720             
721              if(bytesPerPixel == 4) // if its a 32 bpp image
722                {
723                  pImage->data[currentbyte + 3] = colorbuffer[3];// copy the 4th byte
724                }
725             
726              currentbyte += bytesPerPixel;
727              currentpixel++;
728
729              // Make sure we haven't read too many pixels
730              if(currentpixel > pixelcount)     
731                {
732                  PRINTF(1)("Error too many pixels read\n");
733                  if(colorbuffer != NULL)
734                    {
735                      free(colorbuffer);
736                    }
737                 
738                  if(pImage->data != NULL)
739                    {
740                      free(pImage->data);
741                    }
742                 
743                  return false;
744                }
745            }
746        }
747      // chunkheader > 128 RLE data, next color  reapeated chunkheader - 127 times
748      else
749        {
750          short counter;
751          chunkheader -= 127;   // Subteact 127 to get rid of the ID bit
752          if(fread(colorbuffer, 1, bytesPerPixel, fTGA) != bytesPerPixel) // Attempt to read following color values
753            {
754              PRINTF(1)("Error could not read from file");
755              if(colorbuffer != NULL)
756                {
757                  free(colorbuffer);
758                }
759             
760              if(pImage->data != NULL)
761                {
762                  free(pImage->data);
763                }
764             
765              return false;
766            }
767         
768          for(counter = 0; counter < chunkheader; counter++) //copy the color into the image data as many times as dictated
769            {                                                   
770              // switch R and B bytes areound while copying
771              pImage->data[currentbyte    ] = colorbuffer[2];
772              pImage->data[currentbyte + 1] = colorbuffer[1];
773              pImage->data[currentbyte + 2] = colorbuffer[0];
774             
775              if(bytesPerPixel == 4)
776                {
777                  pImage->data[currentbyte + 3] = colorbuffer[3];
778                }
779             
780              currentbyte += bytesPerPixel;
781              currentpixel++;
782             
783              if(currentpixel > pixelcount)
784                {
785                  PRINTF(1)("Error too many pixels read\n");
786                  if(colorbuffer != NULL)
787                    {
788                      free(colorbuffer);
789                    }
790                 
791                  if(pImage->data != NULL)
792                    {
793                      free(pImage->data);
794                    }
795                 
796                  return false;
797                }
798            }
799        }
800    }
801 
802  while(currentpixel < pixelcount);     // Loop while there are still pixels left
803
804  this->loadTexToGL (pImage);
805
806  return true;
807}
808
809
810/*
811static int ST_is_power_of_two(unsigned int number)
812{
813  return (number & (number - 1)) == 0;
814}
815*/
816
817/**
818   \brief reads in a png-file
819   \param pngName the Name of the Image to load
820   \param texture a reference to the Texture to write the image to
821*/
822bool Texture::loadPNG(const char* pngName)
823{
824#ifdef HAVE_PNG_H
825
826  FILE *PNG_file = fopen(pngName, "rb");
827  if (PNG_file == NULL)
828    {
829      return 0;
830    }
831 
832  GLubyte PNG_header[8];
833 
834  fread(PNG_header, 1, 8, PNG_file);
835  if (png_sig_cmp(PNG_header, 0, 8) != 0)
836    {
837      PRINTF(2)("Not Recognized as a pngFile\n");
838      fclose (PNG_file);
839      return 0;
840    }
841 
842  png_structp PNG_reader = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
843  if (PNG_reader == NULL)
844    {
845      fclose(PNG_file);
846      return 0;
847    }
848 
849  png_infop PNG_info = png_create_info_struct(PNG_reader);
850  if (PNG_info == NULL)
851    {
852      png_destroy_read_struct(&PNG_reader, NULL, NULL);
853      fclose(PNG_file);
854      return 0;
855    }
856 
857  png_infop PNG_end_info = png_create_info_struct(PNG_reader);
858  if (PNG_end_info == NULL)
859    {
860      png_destroy_read_struct(&PNG_reader, &PNG_info, NULL);
861      fclose(PNG_file);
862      return 0;
863    }
864 
865  if (setjmp(png_jmpbuf(PNG_reader)))
866    {
867      png_destroy_read_struct(&PNG_reader, &PNG_info, &PNG_end_info);
868      fclose(PNG_file);
869      return (0);
870    }
871 
872  png_init_io(PNG_reader, PNG_file);
873  png_set_sig_bytes(PNG_reader, 8);
874 
875  png_read_info(PNG_reader, PNG_info);
876 
877  pImage->width = png_get_image_width(PNG_reader, PNG_info);
878  pImage->height = png_get_image_height(PNG_reader, PNG_info);
879 
880  png_uint_32 bit_depth, color_type;
881  bit_depth = png_get_bit_depth(PNG_reader, PNG_info);
882  color_type = png_get_color_type(PNG_reader, PNG_info);
883 
884  if (color_type == PNG_COLOR_TYPE_PALETTE)
885    {
886      png_set_palette_to_rgb(PNG_reader);
887    }
888 
889  if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
890    {
891      png_set_gray_1_2_4_to_8(PNG_reader);
892    }
893 
894  if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
895    {
896      png_set_gray_to_rgb(PNG_reader);
897    }
898 
899  if (png_get_valid(PNG_reader, PNG_info, PNG_INFO_tRNS))
900    {
901      png_set_tRNS_to_alpha(PNG_reader);
902    }
903  else
904    {
905      png_set_filler(PNG_reader, 0xff, PNG_FILLER_AFTER);
906    }
907 
908  if (bit_depth == 16)
909    {
910      png_set_strip_16(PNG_reader);
911    }
912 
913  png_read_update_info(PNG_reader, PNG_info);
914 
915  pImage->data = (png_byte*)malloc(4 * pImage->width * pImage->height);
916  png_byte** PNG_rows = (png_byte**)malloc(pImage->height * sizeof(png_byte*));
917 
918  unsigned int row;
919  for (row = 0; row < pImage->height; ++row)
920    {
921      PNG_rows[pImage->height - 1 - row] = pImage->data + (row * 4 * pImage->width);
922    }
923 
924  png_read_image(PNG_reader, PNG_rows);
925 
926  free(PNG_rows);
927 
928  png_destroy_read_struct(&PNG_reader, &PNG_info, &PNG_end_info);
929  fclose(PNG_file);
930 
931  /*  if (!ST_is_power_of_two(pImage->width) || !ST_is_power_of_two(pImage->height))
932    {
933      free(pImage->data);
934      return 0;
935    }
936  */
937  this->loadTexToGL (pImage); 
938 
939  free(pImage->data);
940 
941  return true;
942#else /* HAVE_PNG_H */
943  PRINTF(1)("sorry, but you did not compile with png-support.\nEither install SDL_image or libpng, and recompile to see the image\n");
944  return false;
945#endif /* HAVE_PNG_H */
946
947}
948#endif /* HAVE_SDL_SDL_IMAGE_H */
Note: See TracBrowser for help on using the repository browser.