Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/lib/graphics/importer/texture.cc @ 3660

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

orxonox/trunk: now ResourceManager has the ability to unload resources by priority

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