Changeset 3863 in orxonox.OLD for orxonox/trunk/src/lib
- Timestamp:
- Apr 18, 2005, 10:10:26 AM (20 years ago)
- Location:
- orxonox/trunk/src/lib/graphics/importer
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
orxonox/trunk/src/lib/graphics/importer/texture.cc
r3790 r3863 12 12 main-programmer: Benjamin Grauer 13 13 co-programmer: ... 14 15 TGA-code: borrowed from nehe-Tutorials16 17 14 */ 18 19 15 20 16 #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_IMPORTER … … 66 62 \param b The second value 67 63 */ 68 inlinevoid Texture::swap (unsigned char &a, unsigned char &b)64 void Texture::swap (unsigned char &a, unsigned char &b) 69 65 { 70 66 unsigned char temp; … … 98 94 } 99 95 100 #ifdef HAVE_SDL_IMAGE_H 96 /** 97 \brief loads an Image from a file to a Texture 98 \param imageName The image to load 99 */ 101 100 bool Texture::loadImage(const char* imageName) 102 101 { … … 138 137 this->loadTexToGL (this->pImage); 139 138 SDL_FreeSurface(map); 140 this->pImage->data = NULL;139 pImage->data = NULL; 141 140 } 142 141 else … … 147 146 } 148 147 } 149 150 151 #else /* HAVE_SDL_IMAGE_H */152 /**153 \brief Makes the Programm ready to Read-in a texture-File154 1. Checks what type of Image should be imported155 \todo Checks where to find the Image156 */157 bool Texture::loadImage(const char* imageName)158 {159 if (GraphicsEngine::texturesEnabled)160 {161 if (imageName)162 {163 if (!strncmp(imageName+strlen(imageName)-4, ".bmp", 4))164 {165 PRINTF(4)("Requested bmp-image. Trying to Import.\n");166 return this->loadBMP(imageName);167 }168 169 else if (!strncmp(imageName+strlen(imageName)-4, ".jpg", 4) || !strncmp(imageName+strlen(imageName)-5, ".jpg", 5))170 {171 PRINTF(4)("Requested jpeg-image. Trying to Import\n");172 return this->loadJPG(imageName);173 }174 else if (!strncmp(imageName+strlen(imageName)-4, ".tga", 4))175 {176 PRINTF(4)("Requested tga-image. Trying to Import\n");177 return this->loadTGA(imageName);178 }179 else if (!strncmp(imageName+strlen(imageName)-4, ".png", 4))180 {181 PRINTF(4)("Requested png-image. Trying to Import\n");182 return this->loadPNG(imageName);183 }184 else185 {186 PRINTF(2)("Requested Image was not recognized in its type. (Maybe a type-Cast-error.)\n FileName: %s", imageName);187 return false;188 }189 }190 else191 {192 PRINTF(2)("Image not Found: %s\n", imageName);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 */201 bool Texture::loadBMP (char* bmpName)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 {213 PRINTF(2)("File Not Found : %s\n",bmpName);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 width220 if ((i = fread(&pImage->width, 4, 1, file)) != 1)221 {222 PRINTF(2)("Error reading width from %s.\n", bmpName);223 return false;224 }225 // read the height226 if ((i = fread(&pImage->height, 4, 1, file)) != 1)227 {228 PRINTF(2)("Error reading height from %s.\n", bmpName);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 planes236 if ((fread(&planes, 2, 1, file)) != 1)237 {238 PRINTF(2)("Error reading planes from %s.\n", bmpName);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 bpp248 if ((i = fread(&bpp, 2, 1, file)) != 1)249 {250 PRINTF(2)("Error reading bpp from %s.\n", bmpName);251 return false;252 }253 if (bpp != 24)254 {255 PRINTF(2)("Bpp from %s is not 24: %u\n", bmpName, bpp);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 {266 PRINTF(2)("Error allocating memory for color-corrected image data");267 return false;268 }269 270 if ((i = fread(pImage->data, size, 1, file)) != 1)271 {272 PRINTF(2)("Error reading image data from %s.\n", bmpName);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 }284 this->loadTexToGL (pImage);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-file302 \param jpgName the Name of the Image to load303 */304 bool Texture::loadJPG (char* jpgName)305 {306 #ifdef HAVE_JPEGLIB_H307 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 opened312 if((pFile = fopen(jpgName, "rb")) == NULL)313 {314 // Display an error message saying the file was not found, then return NULL315 PRINTF(2)("Unable to load JPG File %s.\n", jpgName);316 return false;317 }318 319 // Create an error handler320 jpeg_error_mgr jerr;321 322 // Have our compression info object point to the error handler address323 cinfo.err = jpeg_std_error(&jerr);324 325 // Initialize the decompression object326 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 // DECOFING335 // Read in the header of the jpeg file336 jpeg_read_header(&cinfo, TRUE);337 338 // Start to decompress the jpeg file with our compression info339 jpeg_start_decompress(&cinfo);340 341 // Get the image dimensions and row span to read in the pixel data342 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 buffer347 pImage->data = new unsigned char[pImage->rowSpan * pImage->height];348 349 // Here we use the library's state variable cinfo.output_scanline as the350 // loop counter, so that we don't have to keep track ourselves.351 352 // Create an array of row pointers353 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 data358 int rowsRead = 0;359 while (cinfo.output_scanline < cinfo.output_height)360 {361 // Read in the current row of pixels and increase the rowsRead count362 rowsRead += jpeg_read_scanlines(&cinfo, &rowPtr[rowsRead], cinfo.output_height - rowsRead);363 }364 365 // Delete the temporary row pointers366 delete [] rowPtr;367 368 // Finish decompressing the data369 jpeg_finish_decompress(&cinfo);// decodeJPG(&cinfo, pImage);370 371 // This releases all the stored memory for reading and decoding the jpeg372 jpeg_destroy_decompress(&cinfo);373 374 // Close the file pointer that opened the file375 fclose(pFile);376 377 378 if(pImage == NULL)379 exit(0);380 381 this->loadTexToGL (pImage);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-file401 \param tgaName the Name of the Image to load402 */403 bool Texture::loadTGA(const char * tgaName)404 {405 typedef struct406 {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 Header412 GLubyte cTGAcompare[12] = {0,0,10,0,0,0,0,0,0,0,0,0}; // Compressed TGA Header413 FILE * fTGA;414 fTGA = fopen(tgaName, "rb");415 416 if(fTGA == NULL)417 {418 PRINTF(2)("Error could not open texture file: %s\n", tgaName);419 return false;420 }421 422 if(fread(&tgaHeader, sizeof(TGAHeader), 1, fTGA) == 0)423 {424 PRINTF(2)("Error could not read file header of %s\n", tgaName);425 if(fTGA != NULL)426 {427 fclose(fTGA);428 }429 return false;430 }431 432 if(memcmp(uTGAcompare, &tgaHeader, sizeof(TGAHeader)) == 0)433 {434 loadUncompressedTGA(tgaName, fTGA);435 if (fTGA)436 fclose (fTGA);437 }438 else if(memcmp(cTGAcompare, &tgaHeader, sizeof(TGAHeader)) == 0)439 {440 loadCompressedTGA(tgaName, fTGA);441 if (fTGA)442 fclose (fTGA);443 }444 else445 {446 PRINTF(2)("Error TGA file be type 2 or type 10\n");447 if (fTGA)448 fclose(fTGA);449 return false;450 }451 return true;452 }453 454 /**455 \brief reads in an uncompressed tga-file456 \param filename the Name of the Image to load457 \param fTGA a Pointer to a File, that should be read458 */459 bool Texture::loadUncompressedTGA(const char * filename, FILE * fTGA)460 {461 GLubyte header[6]; // First 6 Useful Bytes From The Header462 GLuint bytesPerPixel; // Holds Number Of Bytes Per Pixel Used In The TGA File463 GLuint imageSize; // Used To Store The Image Size When Setting Aside Ram464 GLuint temp; // Temporary Variable465 GLuint type;466 GLuint Height; // Height of Image467 GLuint Width; // Width of Image468 GLuint Bpp; // Bits Per Pixel469 470 GLuint cswap;471 if(fread(header, sizeof(header), 1, fTGA) == 0)472 {473 PRINTF(2)("Error could not read info header\n");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 valid481 if((pImage->width <= 0) || (pImage->height <= 0) || ((pImage->bpp != 24) && (pImage->bpp !=32)))482 {483 PRINTF(2)("Error invalid texture information\n");484 return false;485 }486 487 if(pImage->bpp == 24)488 {489 pImage->type = GL_RGB;490 }491 else492 {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 {502 PRINTF(2)("Error could not allocate memory for image\n");503 return false;504 }505 506 if(fread(pImage->data, 1, imageSize, fTGA) != imageSize)507 {508 PRINTF(2)("Error could not read image data\n");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 522 this->loadTexToGL (pImage);523 524 return true;525 }526 527 /**528 \brief reads in a compressed tga-file529 \param filename the Name of the Image to load530 \param fTGA a Pointer to a File, that should be read531 */532 bool Texture::loadCompressedTGA(const char * filename, FILE * fTGA)533 {534 GLubyte header[6]; // First 6 Useful Bytes From The Header535 GLuint bytesPerPixel; // Holds Number Of Bytes Per Pixel Used In The TGA File536 GLuint imageSize; // Used To Store The Image Size When Setting Aside Ram537 GLuint temp; // Temporary Variable538 GLuint type;539 GLuint Height; // Height of Image540 GLuint Width; // Width of Image541 GLuint Bpp; // Bits Per Pixel542 543 if(fread(header, sizeof(header), 1, fTGA) == 0)544 {545 PRINTF(2)("Error could not read info header\n");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 ok559 if((pImage->width <= 0) || (pImage->height <= 0) || ((pImage->bpp != 24) && (pImage->bpp !=32)))560 {561 PRINTF(2)("Error Invalid pImage information\n");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 {571 PRINTF(2)("Error could not allocate memory for image\n");572 return false;573 }574 575 do576 {577 GLubyte chunkheader = 0;578 579 if(fread(&chunkheader, sizeof(GLubyte), 1, fTGA) == 0)580 {581 PRINTF(2)("Error could not read RLE header\n");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 1589 if(chunkheader < 128)590 {591 short counter;592 chunkheader++;593 // Read RAW color values594 for(counter = 0; counter < chunkheader; counter++)595 {596 // Try to read 1 pixel597 if(fread(colorbuffer, 1, bytesPerPixel, fTGA) != bytesPerPixel)598 {599 PRINTF(2)("Error could not read image data\n");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 memory613 // Flip R and B vcolor values around in the process614 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 image619 {620 pImage->data[currentbyte + 3] = colorbuffer[3];// copy the 4th byte621 }622 623 currentbyte += bytesPerPixel;624 currentpixel++;625 626 // Make sure we haven't read too many pixels627 if(currentpixel > pixelcount)628 {629 PRINTF(2)("Error too many pixels read\n");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 times645 else646 {647 short counter;648 chunkheader -= 127; // Subteact 127 to get rid of the ID bit649 if(fread(colorbuffer, 1, bytesPerPixel, fTGA) != bytesPerPixel) // Attempt to read following color values650 {651 PRINTF(2)("Error could not read from file");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 dictated666 {667 // switch R and B bytes areound while copying668 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 {682 PRINTF(2)("Error too many pixels read\n");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 left700 701 this->loadTexToGL (pImage);702 703 return true;704 }705 706 707 /**708 \brief reads in a png-file709 \param pngName the Name of the Image to load710 */711 bool Texture::loadPNG(const char* pngName)712 {713 #ifdef HAVE_PNG_H714 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 else793 {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 */826 this->loadTexToGL (pImage);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 }837 #endif /* HAVE_SDL_IMAGE_H */ -
orxonox/trunk/src/lib/graphics/importer/texture.h
r3790 r3863 14 14 #include "debug.h" 15 15 16 #ifdef HAVE_SDL_IMAGE_H17 16 #include <SDL_image.h> 18 #else19 // IMAGE LIBS //20 #ifdef HAVE_JPEGLIB_H21 extern "C"{ // This has to be done, because not a c++ lib22 #include <jpeglib.h>23 }24 #endif /* HAVE_JPEGLIB_H */25 #ifdef HAVE_PNG_H26 #include <png.h>27 #endif /* HAVE_PNG_H */28 #endif /* HAVE_SDL_IMAGE_H */29 17 30 18 //! A Class, that reads in Textures from different fileformats. … … 47 35 SDL_Surface* map; //!< The map SDL initializes for this element. 48 36 char* searchTextureInPaths(const char* texName) const; 49 inlinevoid swap(unsigned char &a, unsigned char &b);37 void swap(unsigned char &a, unsigned char &b); 50 38 public: 51 39 Texture(void); … … 57 45 58 46 bool loadImage(const char* imageName); 59 #ifndef HAVE_SDL_IMAGE_H 47 }; 60 48 61 bool loadBMP (char* bmpName);62 63 bool loadJPG (char* jpgName);64 65 /// TGA ///66 67 bool loadTGA(const char * tgaName);68 bool loadUncompressedTGA(const char * filename, FILE * fTGA);69 bool loadCompressedTGA(const char * filename, FILE * fTGA);70 71 bool loadPNG(const char* pngName);72 #endif73 74 75 };76 49 #endif /* _TEXTURE_H */
Note: See TracChangeset
for help on using the changeset viewer.