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