C:/Projekte/InfoVIS/InfoVIS/Texture.cpp

00001 #include "Texture.h"
00002 
00003 Texture::Texture()
00004 {
00005         fileNames.push_back("galaxie.png");
00006         fileNames.push_back("cantonales.png");
00007         fileNames.push_back("treemap.png");
00008         fileNames.push_back("ctreemap.png");
00009 }
00010 
00011 Texture::~Texture(void)
00012 {
00013 }
00014 
00015 void Texture::GenerateTexture(int fileNumber)
00016 {
00017         bool result = LoadPNG (fileNames[fileNumber].c_str());
00018 
00019         if(!result)
00020         {
00021                 printf("Texture file not found ... exiting\n");
00022                 exit(-1);
00023         }
00024 
00025         glDeleteTextures(1, &textureId);
00026 
00027         glGenTextures(1, &textureId);
00028         glBindTexture(GL_TEXTURE_2D, textureId);
00029         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
00030 
00031         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
00032         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
00033 
00034         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00035         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00036 
00037         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
00038 
00039         GLsizei w = (GLsizei)this->textureWidth;
00040         GLsizei h = (GLsizei)this->textureHeight;
00041 
00042         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, this->pngImage);
00043         free(pngImage);
00044 }
00045 
00046 GLuint Texture::GetTextureID () const
00047 {
00048         return textureId;
00049 }
00050 
00051 bool Texture::LoadPNG(const char *filename)
00052 {
00053         //char *fname = "cantonales.png";
00054         
00055         // PNG Structures
00056         unsigned char header[8];
00057         png_structp pngPtr;
00058         png_infop pngInfoPtr;
00059         png_bytepp rowPointers = NULL;
00060 
00061         int bitDepth;
00062         int colorType;
00063 
00064         unsigned int rowbytes;
00065         char* imageData = NULL;
00066 
00067         // End of PNG Structures
00068         FILE *fptr = fopen(filename, "rb");
00069 
00070         if(!fptr)
00071         {
00072                 return false;
00073         }
00074 
00075         fread(header, 1, 8, fptr);
00076         if(!png_check_sig((unsigned char*) header, 8))
00077         {
00078                 fclose (fptr);
00079                 return false;
00080         }
00081 
00082         pngPtr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
00083 
00084         if (!pngPtr)
00085         {
00086                 fclose(fptr);
00087                 return false;
00088         }
00089 
00090         pngInfoPtr = png_create_info_struct(pngPtr);
00091 
00092         if (!pngInfoPtr)
00093         {
00094                 png_destroy_read_struct(&pngPtr, (png_infopp) NULL, (png_infopp) NULL);
00095                 fclose (fptr);
00096                 return false;
00097         }
00098 
00099         if (setjmp(png_jmpbuf(pngPtr)))
00100         {
00101                 png_destroy_read_struct (&pngPtr, &pngInfoPtr, NULL);
00102                 fclose(fptr);
00103                 return false;
00104         }
00105 
00106         png_init_io(pngPtr, fptr);
00107         png_set_sig_bytes(pngPtr, 8);
00108         png_read_info(pngPtr, pngInfoPtr);
00109         png_get_IHDR(pngPtr, pngInfoPtr, &textureWidth, &textureHeight, &bitDepth, &colorType, NULL, NULL, NULL);
00110 
00111         if(colorType & PNG_COLOR_MASK_ALPHA)
00112         {
00113                 png_set_strip_alpha(pngPtr);
00114         }
00115         
00116         if (bitDepth > 8)
00117         {
00118                 png_set_strip_16(pngPtr);
00119         }
00120 
00121         if (colorType == PNG_COLOR_TYPE_GRAY || colorType == PNG_COLOR_TYPE_GRAY_ALPHA)
00122         {
00123                 png_set_gray_to_rgb(pngPtr);
00124         }
00125 
00126         if (colorType == PNG_COLOR_TYPE_PALETTE)
00127         {
00128                 png_set_palette_to_rgb(pngPtr);
00129         }
00130 
00131         png_read_update_info(pngPtr, pngInfoPtr);
00132         rowbytes = png_get_rowbytes(pngPtr, pngInfoPtr);
00133 
00134         if((imageData = (char*)malloc(rowbytes * textureHeight)) == NULL)
00135         {
00136                 png_destroy_read_struct(&pngPtr, &pngInfoPtr, NULL);
00137                 free(imageData);
00138                 imageData=NULL;
00139                 return false;
00140         }
00141 
00142         if((rowPointers = (png_bytepp)malloc(textureHeight*sizeof(png_bytep))) == NULL)
00143         {
00144                 png_destroy_read_struct(&pngPtr, &pngInfoPtr, NULL);
00145                 free(imageData);
00146                 imageData = NULL;
00147                 return false;
00148         }
00149 
00150         for (unsigned int i = 0; i < textureHeight; ++i)
00151         {
00152                 //HACK
00153                 rowPointers[textureHeight - 1 - i] = (png_bytep)(imageData + i*rowbytes);
00154         }
00155 
00156         png_read_image(pngPtr, rowPointers);
00157         free(rowPointers);
00158         png_destroy_read_struct(&pngPtr, &pngInfoPtr, NULL);
00159         fclose(fptr);
00160 
00161         pngImage = imageData;
00162 
00163         return true;
00164 }

Generated on Wed Jun 27 10:14:23 2007 for CLV by  doxygen 1.5.2