Changeset 3090 in orxonox.OLD for orxonox/branches/images
- Timestamp:
- Dec 5, 2004, 12:10:08 AM (20 years ago)
- Location:
- orxonox/branches/images/importer
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
orxonox/branches/images/importer/material.cc
r3089 r3090 391 391 bool Material::loadBMP (char* bmpName, GLuint* texture) 392 392 { 393 SDL_Surface* map; 394 if (map = SDL_LoadBMP(bmpName)) 395 { 396 loadTexToGL (map->w, map->h, map->pixels, texture); 397 if ( map ) 398 SDL_FreeSurface( map ); 399 400 return true; 401 } 402 else 403 return false; 404 } 405 406 bool Material::loadJPG (char* jpgName, GLuint* texture) 407 { 408 struct jpeg_decompress_struct cinfo; 409 tImageJPG *pImage = NULL; 410 FILE *pFile; 411 412 // Open a file pointer to the jpeg file and check if it was found and opened 413 if((pFile = fopen(jpgName, "rb")) == NULL) 414 { 415 // Display an error message saying the file was not found, then return NULL 416 printf("Unable to load JPG File %s.\n", jpgName); 417 return false; 418 } 419 420 // Create an error handler 421 jpeg_error_mgr jerr; 422 423 // Have our compression info object point to the error handler address 424 cinfo.err = jpeg_std_error(&jerr); 425 426 // Initialize the decompression object 427 jpeg_create_decompress(&cinfo); 428 429 // Specify the data source (Our file pointer) 430 jpeg_stdio_src(&cinfo, pFile); 431 432 // Allocate the structure that will hold our eventual jpeg data (must free it!) 433 pImage = (tImageJPG*)malloc(sizeof(tImageJPG)); 434 435 // Decode the jpeg file and fill in the image data structure to pass back 436 decodeJPG(&cinfo, pImage); 437 438 // This releases all the stored memory for reading and decoding the jpeg 439 jpeg_destroy_decompress(&cinfo); 440 441 // Close the file pointer that opened the file 442 fclose(pFile); 443 444 445 if(pImage == NULL) 446 exit(0); 447 393 Image* pImage = new Image; 394 FILE *file; 395 unsigned long size; // size of the image in bytes. 396 unsigned long i; // standard counter. 397 unsigned short int planes; // number of planes in image (must be 1) 398 unsigned short int bpp; // number of bits per pixel (must be 24) 399 GLuint temp; // temporary color storage for bgr-rgb conversion. 400 401 // make sure the file is there. 402 if ((file = fopen(bmpName, "rb"))==NULL) 403 { 404 if (verbose >=1) 405 printf("File Not Found : %s\n",bmpName); 406 return false; 407 } 408 // seek through the bmp header, up to the width/height: 409 fseek(file, 18, SEEK_CUR); 410 411 // read the width 412 if ((i = fread(&pImage->sizeX, 4, 1, file)) != 1) 413 { 414 if (verbose >=1) 415 printf("Error reading width from %s.\n", bmpName); 416 return false; 417 } 418 // read the height 419 if ((i = fread(&pImage->sizeY, 4, 1, file)) != 1) 420 { 421 if (verbose>=1) 422 printf("Error reading height from %s.\n", bmpName); 423 return false; 424 } 425 426 // calculate the size (assuming 24 bits or 3 bytes per pixel). 427 size = pImage->sizeX * pImage->sizeY * 3; 428 429 // read the planes 430 if ((fread(&planes, 2, 1, file)) != 1) 431 { 432 if (verbose>=1) 433 printf("Error reading planes from %s.\n", bmpName); 434 return false; 435 } 436 if (planes != 1) 437 { 438 if (verbose>=1) 439 printf("Planes from %s is not 1: %u\n", bmpName, planes); 440 return false; 441 } 442 443 // read the bpp 444 if ((i = fread(&bpp, 2, 1, file)) != 1) 445 { 446 if (verbose>=1) 447 printf("Error reading bpp from %s.\n", bmpName); 448 return false; 449 } 450 if (bpp != 24) 451 { 452 if (verbose>=1) 453 printf("Bpp from %s is not 24: %u\n", bmpName, bpp); 454 return false; 455 } 456 457 // seek past the rest of the bitmap header. 458 fseek(file, 24, SEEK_CUR); 459 460 // read the data. 461 pImage->data = (GLubyte *) malloc(size); 462 if (pImage->data == NULL) 463 { 464 if (verbose>=1) 465 printf("Error allocating memory for color-corrected image data"); 466 return false; 467 } 468 469 if ((i = fread(pImage->data, size, 1, file)) != 1) 470 { 471 if (verbose>=1) 472 printf("Error reading image data from %s.\n", bmpName); 473 return false; 474 } 475 // reverse all of the colors. (bgr -> rgb) 476 for (i=0;i<size;i+=3) 477 { 478 temp = pImage->data[i]; 479 pImage->data[i] = pImage->data[i+2]; 480 pImage->data[i+2] = temp; 481 } 448 482 loadTexToGL (pImage->sizeX, pImage->sizeY, pImage->data, texture); 483 484 return true; 485 449 486 if (pImage) 450 487 { … … 456 493 free(pImage); 457 494 } 495 496 } 497 498 bool Material::loadJPG (char* jpgName, GLuint* texture) 499 { 500 struct jpeg_decompress_struct cinfo; 501 Image *pImage = NULL; 502 FILE *pFile; 503 504 // Open a file pointer to the jpeg file and check if it was found and opened 505 if((pFile = fopen(jpgName, "rb")) == NULL) 506 { 507 // Display an error message saying the file was not found, then return NULL 508 printf("Unable to load JPG File %s.\n", jpgName); 509 return false; 510 } 511 512 // Create an error handler 513 jpeg_error_mgr jerr; 514 515 // Have our compression info object point to the error handler address 516 cinfo.err = jpeg_std_error(&jerr); 517 518 // Initialize the decompression object 519 jpeg_create_decompress(&cinfo); 520 521 // Specify the data source (Our file pointer) 522 jpeg_stdio_src(&cinfo, pFile); 523 524 // Allocate the structure that will hold our eventual jpeg data (must free it!) 525 pImage = (Image*)malloc(sizeof(Image)); 526 527 // Decode the jpeg file and fill in the image data structure to pass back 528 decodeJPG(&cinfo, pImage); 529 530 // This releases all the stored memory for reading and decoding the jpeg 531 jpeg_destroy_decompress(&cinfo); 532 533 // Close the file pointer that opened the file 534 fclose(pFile); 535 536 537 if(pImage == NULL) 538 exit(0); 539 540 loadTexToGL (pImage->sizeX, pImage->sizeY, pImage->data, texture); 541 if (pImage) 542 { 543 if (pImage->data) 544 { 545 free(pImage->data); 546 } 547 548 free(pImage); 549 } 458 550 return true; 459 551 } 460 552 461 void Material::decodeJPG(jpeg_decompress_struct* cinfo, tImageJPG *pImageData)553 void Material::decodeJPG(jpeg_decompress_struct* cinfo, Image* pImageData) 462 554 { 463 555 // Read in the header of the jpeg file -
orxonox/branches/images/importer/material.h
r3089 r3090 66 66 67 67 private: 68 struct tImageJPG68 struct Image 69 69 { 70 70 int rowSpan; 71 intsizeX;72 intsizeY;71 unsigned long sizeX; 72 unsigned long sizeY; 73 73 GLubyte *data; 74 74 }; … … 90 90 91 91 bool loadJPG (char* jpgName, GLuint* texture); 92 void decodeJPG(jpeg_decompress_struct* cinfo, tImageJPG*pImageData);92 void decodeJPG(jpeg_decompress_struct* cinfo, Image *pImageData); 93 93 94 94 bool loadTexToGL (int hight, int width, void* rawImage, GLuint* texture);
Note: See TracChangeset
for help on using the changeset viewer.