#include <ExCImageLoader.h>
Membres publics | |
ExCImageLoader (void) | |
ExCImageLoader (std::string FileName) | |
~ExCImageLoader (void) | |
bool | LoadImage (std::string FileName) |
bool | LoadBMP (const char *strFileName) |
bool | LoadBMP (std::string strFileName) |
bool | LoadTGA (std::string strFileName) |
bool | LoadJPG (std::string strFileName) |
bool | LoadPCX (std::string strFileName) |
bool | LoadRAW (std::string strFileName) |
bool | LoadRGB (std::string strFileName) |
bool | LoadPPM (std::string strFileName) |
bool | LoadDDS (std::string strFileName) |
bool | SaveImage (std::string FileName, int width, int height) |
bool | SaveBMP (std::string strFileName, int width, int height, unsigned char *ImageData) |
bool | SaveTGA (std::string strFileName, int width, int height, unsigned char *ImageData) |
bool | SaveJPG (std::string strFileName, int width, int height, unsigned char *ImageData) |
bool | SavePCX (std::string strFileName, int width, int height, unsigned char *ImageData) |
bool | SaveRAW (std::string strFileName, int width, int height, unsigned char *ImageData) |
bool | SaveRGB (std::string strFileName, int width, int height, unsigned char *ImageData) |
bool | SavePPM (std::string strFileName, int width, int height, unsigned char *ImageData) |
Attributs Publics | |
TextureType | m_TextureType |
int | m_channels |
int | m_sizeX |
int | m_sizeY |
long int | m_scaledWidth |
long int | m_scaledHeight |
unsigned int | m_type |
unsigned char * | m_data |
unsigned char * | m_palette |
int | m_ddsBufsize |
int | m_ddsNumMipmaps |
int | m_ddsFormat |
int | m_ddsComponent |
Types Privés | |
typedef ExCImageLoader::tagBITMAPINFOHEADER | BITMAPINFOHEADER |
Membres privés | |
unsigned char * | LoadPCXFile (std::string filename, PCXHEADER *pcxHeader) |
TextureType | FindTextureType (std::string FileName) |
|
|
|
Définition à la ligne 4 du fichier ExCImageLoader.cpp.
00005 { 00006 } |
|
Définition à la ligne 8 du fichier ExCImageLoader.cpp. Références LoadImage().
00009 { 00010 LoadImage(FileName); 00011 } |
|
Définition à la ligne 13 du fichier ExCImageLoader.cpp. Références m_data, et m_palette.
|
|
Définition à la ligne 19 du fichier ExCImageLoader.cpp. Références BMP, DDS, FindTextureType(), Guard, PCX, PPM, TextureType, TGA, UnGuard, et UNKNOWN. Référencé par FindTextureType(), LoadImage(), et SaveImage().
00020 { 00021 Guard(TextureType ExCImageLoader::FindTextureType(std::string FileName)) 00022 //std::cout<<FileName<<std::endl; 00023 //Extension in string 00024 if(FileName.find(".")) 00025 { 00026 if(FileName.find(".bmp")==(FileName.length()-4))return BMP; 00027 if(FileName.find(".tga")==(FileName.length()-4))return TGA; 00028 if(FileName.find(".pcx")==(FileName.length()-4))return PCX; 00029 //if(FileName.find(".jpg")==(FileName.length()-4))return JPG; 00030 //if(FileName.find(".rgb")==(FileName.length()-4))return RGB; 00031 //if(FileName.find(".raw")==(FileName.length()-4))return RAW; 00032 if(FileName.find(".dds")==(FileName.length()-4))return DDS; 00033 if(FileName.find(".ppm")==(FileName.length()-4))return PPM; 00034 if(FileName.find(".")<FileName.length())return UNKNOWN; 00035 } 00036 //No extension in string 00037 //std::cout<<"File without extension"<<std::endl; 00038 FILE *file; 00039 std::ifstream filetest; 00040 std::string strFileName; 00041 00042 //try with bmp 00043 strFileName=FileName+".bmp"; 00044 if ((file = fopen(strFileName.data(), "r"))!=NULL){fclose(file);return BMP;} 00045 //try with pcx 00046 strFileName=FileName+".pcx"; 00047 if ((file = fopen(strFileName.data(), "r"))!=NULL){fclose(file);return PCX;} 00048 //try with tga 00049 strFileName=FileName+".tga"; 00050 if ((file = fopen(strFileName.data(), "r"))!=NULL){fclose(file);return TGA;} 00051 //try with ppm 00052 strFileName=FileName+".ppm"; 00053 if ((file = fopen(strFileName.data(), "r"))!=NULL){fclose(file);return PPM;} 00054 //try with dds 00055 strFileName=FileName+".dds"; 00056 if ((file = fopen(strFileName.data(), "r"))!=NULL){fclose(file);return DDS;} 00057 /* 00058 //try with jpg 00059 strFileName=FileName+".jpg"; 00060 if ((file = fopen(strFileName.data(), "r"))!=NULL){fclose(file);return JPG;} 00061 //try with rgb 00062 strFileName=FileName+".rgb"; 00063 if ((file = fopen(strFileName.data(), "r"))!=NULL){fclose(file);return RGB;} 00064 //try with raw 00065 strFileName=FileName+".raw"; 00066 if ((file = fopen(strFileName.data(), "r"))!=NULL){fclose(file);return RAW;} 00067 */ 00068 throw ExCExpFileNotFound(); 00069 return UNKNOWN; 00070 UnGuard 00071 } |
|
Définition à la ligne 442 du fichier ExCImageLoader.cpp. Références Guard, LoadBMP(), et UnGuard.
|
|
Définition à la ligne 530 du fichier ExCImageLoader.cpp. Références Guard, LoadBMP(), m_channels, m_data, m_sizeX, m_sizeY, m_type, et UnGuard. Référencé par LoadBMP(), et LoadImage().
00531 { 00532 Guard(bool ExCImageLoader::LoadBMP(const char *strFileName)) 00533 //--------------------------------------------- 00534 FILE *file; 00535 unsigned long size; // size of the image in uchars. 00536 unsigned long i; // standard counter. 00537 unsigned short int planes; // number of planes in image (must be 1) 00538 unsigned short int channels; // number of bits per pixel (must be 24) 00539 char temp; // temporary color storage for bgr-rgb conversion. 00540 00541 // make sure the file is there. 00542 if ((file = fopen(strFileName, "rb"))==NULL) 00543 { 00544 printf("ExCImageLoader::LoadBMP ==>File Not Found : %s\n",strFileName); 00545 throw ExCExpFileNotFound(); 00546 return false; 00547 } 00548 #ifdef UNIX_SRC 00549 unsigned short int is_bmp; 00550 if (! fread (&is_bmp, sizeof (short int), 1, file)) 00551 { 00552 printf("cannot read %s.\n", strFileName); 00553 return false; 00554 } 00555 00556 /* check if file is a bitmap */ 00557 if (is_bmp != 19778) 00558 { 00559 printf("%s is not a valid bmp file.\n", strFileName); 00560 return false; 00561 } 00562 00563 fseek (file, 8, SEEK_CUR); 00564 /* get the position of the actual bitmap data */ 00565 long int bfOffBits; 00566 if (! fread (&bfOffBits, sizeof (long int), 1, file)) 00567 { 00568 printf("Error reading %s.\n", strFileName); 00569 return false; 00570 } 00571 /*sprintf(buffer,"Data at offset: %ld.", bfOffBits); 00572 WriteToConsol(buffer);*/ 00573 00574 // seek through the bmp header, up to the width/height: 00575 fseek(file, 4, SEEK_CUR); 00576 #else 00577 fseek(file, 18, SEEK_SET); 00578 #endif 00579 // read the width 00580 if ((i = fread(&m_sizeX, 4, 1, file)) != 1) 00581 { 00582 printf("Error reading width from %s.\n", strFileName); 00583 return false; 00584 } 00585 00586 // read the height 00587 if ((i = fread(&m_sizeY, 4, 1, file)) != 1) 00588 { 00589 printf("Error reading height from %s.\n", strFileName); 00590 return false; 00591 } 00592 00593 00594 // calculate the size (assuming 24 bits or 3 uchars per pixel). 00595 size = m_sizeX * m_sizeY * 3; 00596 00597 // read the planes 00598 if ((fread(&planes, 2, 1, file)) != 1) 00599 { 00600 printf("Error reading planes from %s.\n", strFileName); 00601 return false; 00602 } 00603 00604 if (planes != 1) 00605 { 00606 printf("Planes from %s is not 1: %u.\n", strFileName, planes); 00607 return false; 00608 } 00609 00610 // read the channels 00611 if ((i = fread(&channels, 2, 1, file)) != 1) 00612 { 00613 printf("Error reading channels from %s.\n", strFileName); 00614 return false; 00615 } 00616 if (channels != 24) 00617 { 00618 printf("channels from %s is not 24: %u\n", strFileName, channels); 00619 return false; 00620 } 00621 00622 // seek past the rest of the bitmap header. 00623 fseek(file, 24, SEEK_CUR); 00624 // read the data. 00625 m_data = new unsigned char[size]; 00626 //char *foo = new char[size]; 00627 //m_data = foo; 00628 00629 if (m_data == NULL) 00630 { 00631 printf("Error allocating memory for color-corrected image data"); 00632 return false; 00633 } 00634 00635 if ((i = fread(m_data, size, 1, file)) != 1) 00636 { 00637 printf("Error reading image data from %s.\n", strFileName); 00638 return false; 00639 } 00640 00641 for (i=0;i<size;i+=3) 00642 { 00643 // reverse all of the colors. (bgr -> rgb) 00644 temp = m_data[i]; 00645 m_data[i] = m_data[i+2]; 00646 m_data[i+2] = temp; 00647 } 00648 //--------------------------------------------- 00649 m_type=GL_RGB; 00650 m_channels=3; 00651 fclose(file); 00652 return true; 00653 UnGuard 00654 } |
|
Définition à la ligne 116 du fichier ExCImageLoader.cpp. Références DDS, Guard, LoadDDS(), m_data, m_ddsBufsize, m_ddsComponent, m_ddsFormat, m_ddsNumMipmaps, m_sizeX, m_sizeY, m_TextureType, et UnGuard. Référencé par LoadDDS(), et LoadImage().
00117 { 00118 Guard(bool ExCImageLoader::LoadDDS(std::string strFileName)) 00119 m_TextureType = DDS; 00120 DDSURFACEDESC2 ddsd; 00121 char filecode[4]; 00122 FILE *fp; 00123 int factor; 00124 00125 strFileName=strFileName+".dds"; 00126 fp = fopen(strFileName.data(), "rb"); 00127 if (!fp) 00128 { 00129 throw ExCExpFileNotFound(); 00130 return NULL; 00131 } 00132 00133 /* verify the type of file */ 00134 fread(filecode, 1, 4, fp); 00135 if (strncmp(filecode, "DDS ", 4) != 0) 00136 { 00137 fclose(fp); 00138 return NULL; 00139 } 00140 00141 /* get the surface desc */ 00142 fread(&ddsd, sizeof(ddsd), 1, fp); 00143 00144 switch(ddsd.ddpfPixelFormat.dwFourCC) 00145 { 00146 case FOURCC_DXT1: 00147 m_ddsFormat = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; 00148 factor = 2; 00149 break; 00150 case FOURCC_DXT3: 00151 m_ddsFormat = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; 00152 factor = 4; 00153 break; 00154 case FOURCC_DXT5: 00155 m_ddsFormat = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; 00156 factor = 4; 00157 break; 00158 default: 00159 return NULL; 00160 } 00161 00162 /* how big is it going to be including all mipmaps? */ 00163 m_ddsBufsize = ddsd.dwMipMapCount > 1 ? ddsd.dwLinearSize * factor : ddsd.dwLinearSize; 00164 // unsigned char buffinverse; 00165 // buffinverse = (unsigned char*)malloc(m_ddsBufsize * sizeof(unsigned char)); 00166 m_data = (unsigned char*)malloc(m_ddsBufsize * sizeof(unsigned char)); 00167 00168 fread(m_data, 1, m_ddsBufsize, fp); 00169 /* close the file pointer */ 00170 fclose(fp); 00171 00172 00173 //inverse pixel 00174 00175 00176 00177 m_sizeX = ddsd.dwWidth; 00178 m_sizeY = ddsd.dwHeight; 00179 m_ddsComponent = (ddsd.ddpfPixelFormat.dwFourCC == FOURCC_DXT1) ? 3 : 4; 00180 m_ddsNumMipmaps = ddsd.dwMipMapCount; 00181 return true; 00182 UnGuard 00183 } |
|
Définition à la ligne 73 du fichier ExCImageLoader.cpp. Références BMP, DDS, FindTextureType(), Guard, JPG, LoadBMP(), LoadDDS(), LoadImage(), LoadJPG(), LoadPCX(), LoadPPM(), LoadTGA(), m_TextureType, PCX, PPM, TGA, UnGuard, et UNKNOWN. Référencé par ExCImageLoader(), ExCTexture::LoadFile(), et LoadImage().
00074 { 00075 Guard(bool ExCImageLoader::LoadImage(std::string FileName)) 00076 m_TextureType=FindTextureType(FileName); 00077 //std::cout<<m_TextureType; 00078 switch(m_TextureType) 00079 { 00080 case BMP:return LoadBMP(FileName); 00081 case TGA:return LoadTGA(FileName); 00082 case PCX:return LoadPCX(FileName); 00083 case JPG:return LoadJPG(FileName); 00084 //case RGB:return LoadRGB(FileName); 00085 //case RAW:return LoadRAW(FileName); 00086 case DDS:return LoadDDS(FileName); 00087 case PPM:return LoadPPM(FileName); 00088 case UNKNOWN:return false; 00089 default:return false; 00090 } 00091 UnGuard 00092 } |
|
Définition à la ligne 110 du fichier ExCImageLoader.cpp. Référencé par LoadImage().
00111 { 00112 00113 return false; 00114 } |
|
Définition à la ligne 450 du fichier ExCImageLoader.cpp. Références Guard, LoadPCX(), LoadPCXFile(), m_data, m_palette, m_scaledHeight, m_scaledWidth, m_sizeX, m_sizeY, ExCImageLoader::PCXHEADER::palette, UnGuard, ExCImageLoader::PCXHEADER::xMax, ExCImageLoader::PCXHEADER::xMin, ExCImageLoader::PCXHEADER::yMax, et ExCImageLoader::PCXHEADER::yMin. Référencé par LoadImage(), et LoadPCX().
00451 { 00452 Guard(bool ExCImageLoader::LoadPCX(std::string strFileName)) 00453 PCXHEADER texInfo; // header of texture 00454 //texture_t *thisTexture; // the texture 00455 unsigned char *unscaledData;// used to calculate pcx 00456 int i; // index counter 00457 int j; // index counter 00458 int width; // width of texture 00459 int height; // height of texture 00460 00461 00462 // load the PCX file into the texture struct 00463 m_data=LoadPCXFile(strFileName, &texInfo); 00464 if (m_data == NULL) 00465 { 00466 free(m_data); 00467 throw ExCExpNullPointer(); 00468 return false; 00469 } 00470 00471 // store the texture information 00472 m_palette = texInfo.palette; 00473 m_sizeX = texInfo.xMax - texInfo.xMin + 1; 00474 m_sizeY = texInfo.yMax - texInfo.yMin + 1; 00475 00476 // allocate memory for the unscaled data 00477 unscaledData = (unsigned char*)malloc(m_sizeX*m_sizeY*4); 00478 00479 // store the unscaled data via the palette 00480 for (j = 0; j < m_sizeY; j++) 00481 { 00482 for (i = 0; i < m_sizeX; i++) 00483 { 00484 unscaledData[4*(j*m_sizeX+i)+0] = (unsigned char)m_palette[3*m_data[j*m_sizeX+i]+0]; 00485 unscaledData[4*(j*m_sizeX+i)+1] = (unsigned char)m_palette[3*m_data[j*m_sizeX+i]+1]; 00486 unscaledData[4*(j*m_sizeX+i)+2] = (unsigned char)m_palette[3*m_data[j*m_sizeX+i]+2]; 00487 unscaledData[4*(j*m_sizeX+i)+3] = (unsigned char)255; 00488 } 00489 } 00490 00491 // find width and height's nearest greater power of 2 00492 width = m_sizeX; 00493 height = m_sizeY; 00494 00495 // find width's 00496 i = 0; 00497 while (width) 00498 { 00499 width /= 2; 00500 i++; 00501 } 00502 m_scaledHeight = (long)pow(2, i-1); 00503 00504 // find height's 00505 i = 0; 00506 while (height) 00507 { 00508 height /= 2; 00509 i++; 00510 } 00511 m_scaledWidth = (long)pow(2, i-1); 00512 00513 // clear the texture data 00514 if (m_data != NULL) 00515 { 00516 free(m_data); 00517 m_data = NULL; 00518 } 00519 00520 // reallocate memory for the texture data 00521 m_data = (unsigned char*)malloc(m_scaledWidth*m_scaledHeight*4); 00522 00523 // use the GL utility library to scale the texture to the unscaled dimensions 00524 gluScaleImage (GL_RGBA, m_sizeX, m_sizeY, GL_UNSIGNED_BYTE, unscaledData, m_scaledWidth, m_scaledHeight, GL_UNSIGNED_BYTE, m_data); 00525 00526 return true; 00527 UnGuard 00528 } |
|
Définition à la ligne 656 du fichier ExCImageLoader.cpp. Références Guard, LoadPCXFile(), ExCImageLoader::PCXHEADER::palette, UnGuard, ExCImageLoader::PCXHEADER::xMax, ExCImageLoader::PCXHEADER::xMin, ExCImageLoader::PCXHEADER::yMax, et ExCImageLoader::PCXHEADER::yMin. Référencé par LoadPCX(), et LoadPCXFile().
00657 { 00658 Guard(unsigned char * ExCImageLoader::LoadPCXFile(std::string filename, PCXHEADER *pcxHeader)) 00659 int idx = 0; // counter index 00660 int c; // used to retrieve a char from the file 00661 int i; // counter index 00662 int numRepeat; 00663 FILE *filePtr; // file handle 00664 int width; // pcx width 00665 int height; // pcx height 00666 unsigned char *pixelData; // pcx image data 00667 unsigned char *paletteData; // pcx palette data 00668 00669 filename=filename+".pcx"; 00670 // open PCX file 00671 filePtr = fopen(filename.data(), "rb"); 00672 if (filePtr == NULL) 00673 { 00674 throw ExCExpFileNotFound(); 00675 return NULL; 00676 } 00677 // retrieve first character; should be equal to 10 00678 c = getc(filePtr); 00679 if (c != 10) 00680 { 00681 fclose(filePtr); 00682 return NULL; 00683 } 00684 // retrieve next character; should be equal to 5 00685 c = getc(filePtr); 00686 if (c != 5) 00687 { 00688 fclose(filePtr); 00689 return NULL; 00690 } 00691 // reposition file pointer to beginning of file 00692 rewind(filePtr); 00693 00694 // read 4 characters of data to skip 00695 fgetc(filePtr); 00696 fgetc(filePtr); 00697 fgetc(filePtr); 00698 fgetc(filePtr); 00699 00700 // retrieve leftmost x value of PCX 00701 pcxHeader->xMin = fgetc(filePtr); // loword 00702 pcxHeader->xMin |= fgetc(filePtr) << 8; // hiword 00703 00704 // retrieve bottom-most y value of PCX 00705 pcxHeader->yMin = fgetc(filePtr); // loword 00706 pcxHeader->yMin |= fgetc(filePtr) << 8; // hiword 00707 00708 // retrieve rightmost x value of PCX 00709 pcxHeader->xMax = fgetc(filePtr); // loword 00710 pcxHeader->xMax |= fgetc(filePtr) << 8; // hiword 00711 00712 // retrieve topmost y value of PCX 00713 pcxHeader->yMax = fgetc(filePtr); // loword 00714 pcxHeader->yMax |= fgetc(filePtr) << 8; // hiword 00715 00716 // calculate the width and height of the PCX 00717 width = pcxHeader->xMax - pcxHeader->xMin + 1; 00718 height = pcxHeader->yMax - pcxHeader->yMin + 1; 00719 00720 // allocate memory for PCX image data 00721 pixelData = (unsigned char*)malloc(width*height); 00722 00723 // set file pointer to 128th byte of file, where the PCX image data starts 00724 fseek(filePtr, 128, SEEK_SET); 00725 00726 // decode the pixel data and store 00727 while (idx < (width*height)) 00728 { 00729 c = getc(filePtr); 00730 if (c > 0xbf) 00731 { 00732 numRepeat = 0x3f & c; 00733 c = getc(filePtr); 00734 00735 for (i = 0; i < numRepeat; i++) 00736 { 00737 pixelData[idx++] = c; 00738 } 00739 } 00740 else 00741 pixelData[idx++] = c; 00742 00743 fflush(stdout); 00744 } 00745 00746 // allocate memory for the PCX image palette 00747 paletteData = (unsigned char*)malloc(768); 00748 00749 // palette is the last 769 bytes of the PCX file 00750 fseek(filePtr, -769, SEEK_END); 00751 00752 // verify palette; first character should be 12 00753 c = getc(filePtr); 00754 if (c != 12) 00755 { 00756 fclose(filePtr); 00757 return NULL; 00758 } 00759 // read and store all of palette 00760 for (i = 0; i < 768; i++) 00761 { 00762 c = getc(filePtr); 00763 paletteData[i] = c; 00764 } 00765 00766 // close file and store palette in header 00767 fclose(filePtr); 00768 pcxHeader->palette = paletteData; 00769 00770 // return the pixel image data 00771 return pixelData; 00772 UnGuard 00773 } |
|
Définition à la ligne 185 du fichier ExCImageLoader.cpp. Références Guard, LoadPPM(), m_data, m_sizeX, m_sizeY, et UnGuard. Référencé par LoadImage(), et LoadPPM().
00186 { 00187 Guard(bool ExCImageLoader::LoadPPM(std::string strFileName)) 00188 FILE* fp; 00189 int i, w, h, d; 00190 char head[70]; /* max line <= 70 in PPM (per spec). */ 00191 00192 strFileName=strFileName+".ppm"; 00193 00194 fp = fopen(strFileName.data(), "rb"); 00195 if (!fp) 00196 { 00197 std::cout<<strFileName<<std::endl; 00198 throw ExCExpFileNotFound(); 00199 return NULL; 00200 } 00201 00202 /* grab first two chars of the file and make sure that it has the 00203 correct magic cookie for a raw PPM file. */ 00204 fgets(head, 70, fp); 00205 if (strncmp(head, "P6", 2)) 00206 { 00207 printf("%s: Not a raw PPM file\n", strFileName.data()); 00208 return NULL; 00209 } 00210 00211 /* grab the three elements in the header (width, height, maxval). */ 00212 i = 0; 00213 while(i < 3) { 00214 fgets(head, 70, fp); 00215 if (head[0] == '#') /* skip comments. */ 00216 continue; 00217 if (i == 0) 00218 i += sscanf(head, "%d %d %d", &w, &h, &d); 00219 else if (i == 1) 00220 i += sscanf(head, "%d %d", &h, &d); 00221 else if (i == 2) 00222 i += sscanf(head, "%d", &d); 00223 } 00224 00225 /* grab all the image data in one fell swoop. */ 00226 m_data = (unsigned char*)malloc(sizeof(unsigned char)*w*h*3); 00227 fread(m_data, sizeof(unsigned char), w*h*3, fp); 00228 fclose(fp); 00229 00230 m_sizeX = w; 00231 m_sizeY = h; 00232 00233 00234 return true; 00235 UnGuard 00236 } |
|
Définition à la ligne 100 du fichier ExCImageLoader.cpp.
00101 { 00102 return false; 00103 } |
|
Définition à la ligne 105 du fichier ExCImageLoader.cpp.
00106 { 00107 return false; 00108 } |
|
Définition à la ligne 238 du fichier ExCImageLoader.cpp. Références byte, Guard, LoadTGA(), m_channels, m_data, m_sizeX, m_sizeY, m_TextureType, TGA, TGA_RLE, et UnGuard. Référencé par LoadImage(), et LoadTGA().
00239 { 00240 Guard(bool ExCImageLoader::LoadTGA(std::string strFileName)) 00241 WORD width = 0, height = 0; // The dimensions of the image 00242 byte length = 0; // The length in bytes to the pixels 00243 byte imageType = 0; // The image type (RLE, RGB, Alpha...) 00244 byte bits = 0; // The bits per pixel for the image (16, 24, 32) 00245 FILE *pFile = NULL; // The file pointer 00246 int channels = 0; // The channels of the image (3 = RGA : 4 = RGBA) 00247 int stride = 0; // The stride (channels * width) 00248 int i = 0; // A counter 00249 00250 strFileName=strFileName+".tga"; 00251 // Open a file pointer to the targa file and check if it was found and opened 00252 if((pFile = fopen(strFileName.data(), "rb")) == NULL) 00253 { 00254 // Display an error message saying the file was not found, then return NULL 00255 std::cout<<strFileName<<std::endl; 00256 throw ExCExpFileNotFound(); 00257 return NULL; 00258 } 00259 00260 // Read in the length in bytes from the header to the pixel data 00261 fread(&length, sizeof(byte), 1, pFile); 00262 00263 // Jump over one byte 00264 fseek(pFile,1,SEEK_CUR); 00265 00266 // Read in the imageType (RLE, RGB, etc...) 00267 fread(&imageType, sizeof(byte), 1, pFile); 00268 00269 // Skip past general information we don't care about 00270 fseek(pFile, 9, SEEK_CUR); 00271 00272 // Read the width, height and bits per pixel (16, 24 or 32) 00273 fread(&width, sizeof(WORD), 1, pFile); 00274 fread(&height, sizeof(WORD), 1, pFile); 00275 fread(&bits, sizeof(byte), 1, pFile); 00276 00277 // Now we move the file pointer to the pixel data 00278 fseek(pFile, length + 1, SEEK_CUR); 00279 00280 // Check if the image is RLE compressed or not 00281 if(imageType != TGA_RLE) 00282 { 00283 // Check if the image is a 24 or 32-bit image 00284 if(bits == 24 || bits == 32) 00285 { 00286 // Calculate the channels (3 or 4) - (use bits >> 3 for more speed). 00287 // Next, we calculate the stride and allocate enough memory for the pixels. 00288 channels = bits / 8; 00289 stride = channels * width; 00290 m_data = new unsigned char[stride * height]; 00291 00292 // Load in all the pixel data line by line 00293 for(int y = 0; y < height; y++) 00294 { 00295 // Store a pointer to the current line of pixels 00296 unsigned char *pLine = &(m_data[stride * y]); 00297 00298 // Read in the current line of pixels 00299 fread(pLine, stride, 1, pFile); 00300 00301 // Go through all of the pixels and swap the B and R values since TGA 00302 // files are stored as BGR instead of RGB (or use GL_BGR_EXT verses GL_RGB) 00303 for(i = 0; i < stride; i += channels) 00304 { 00305 int temp = pLine[i]; 00306 pLine[i] = pLine[i + 2]; 00307 pLine[i + 2] = temp; 00308 } 00309 } 00310 } 00311 // Check if the image is a 16 bit image (RGB stored in 1 unsigned short) 00312 else if(bits == 16) 00313 { 00314 unsigned short pixels = 0; 00315 int r=0, g=0, b=0; 00316 00317 // Since we convert 16-bit images to 24 bit, we hardcode the channels to 3. 00318 // We then calculate the stride and allocate memory for the pixels. 00319 channels = 3; 00320 stride = channels * width; 00321 m_data = new unsigned char[stride * height]; 00322 00323 // Load in all the pixel data pixel by pixel 00324 for(int i = 0; i < width*height; i++) 00325 { 00326 // Read in the current pixel 00327 fread(&pixels, sizeof(unsigned short), 1, pFile); 00328 00329 // To convert a 16-bit pixel into an R, G, B, we need to 00330 // do some masking and such to isolate each color value. 00331 // 0x1f = 11111 in binary, so since 5 bits are reserved in 00332 // each unsigned short for the R, G and B, we bit shift and mask 00333 // to find each value. We then bit shift up by 3 to get the full color. 00334 b = (pixels & 0x1f) << 3; 00335 g = ((pixels >> 5) & 0x1f) << 3; 00336 r = ((pixels >> 10) & 0x1f) << 3; 00337 00338 // This essentially assigns the color to our array and swaps the 00339 // B and R values at the same time. 00340 m_data[i * 3 + 0] = r; 00341 m_data[i * 3 + 1] = g; 00342 m_data[i * 3 + 2] = b; 00343 } 00344 } 00345 // Else return a NULL for a bad or unsupported pixel format 00346 else 00347 return NULL; 00348 } 00349 // Else, it must be Run-Length Encoded (RLE) 00350 else 00351 { 00352 // Create some variables to hold the rleID, current colors read, channels, & stride. 00353 byte rleID = 0; 00354 int colorsRead = 0; 00355 channels = bits / 8; 00356 stride = channels * width; 00357 00358 // Next we want to allocate the memory for the pixels and create an array, 00359 // depending on the channel count, to read in for each pixel. 00360 m_data = new unsigned char[stride * height]; 00361 byte *pColors = new byte [channels]; 00362 00363 // Load in all the pixel data 00364 while(i < width*height) 00365 { 00366 // Read in the current color count + 1 00367 fread(&rleID, sizeof(byte), 1, pFile); 00368 00369 // Check if we don't have an encoded string of colors 00370 if(rleID < 128) 00371 { 00372 // Increase the count by 1 00373 rleID++; 00374 00375 // Go through and read all the unique colors found 00376 while(rleID) 00377 { 00378 // Read in the current color 00379 fread(pColors, sizeof(byte) * channels, 1, pFile); 00380 00381 // Store the current pixel in our image array 00382 m_data[colorsRead + 0] = pColors[2]; 00383 m_data[colorsRead + 1] = pColors[1]; 00384 m_data[colorsRead + 2] = pColors[0]; 00385 00386 // If we have a 4 channel 32-bit image, assign one more for the alpha 00387 if(bits == 32) 00388 m_data[colorsRead + 3] = pColors[3]; 00389 00390 // Increase the current pixels read, decrease the amount 00391 // of pixels left, and increase the starting index for the next pixel. 00392 i++; 00393 rleID--; 00394 colorsRead += channels; 00395 } 00396 } 00397 // Else, let's read in a string of the same character 00398 else 00399 { 00400 // Minus the 128 ID + 1 (127) to get the color count that needs to be read 00401 rleID -= 127; 00402 00403 // Read in the current color, which is the same for a while 00404 fread(pColors, sizeof(byte) * channels, 1, pFile); 00405 00406 // Go and read as many pixels as are the same 00407 while(rleID) 00408 { 00409 // Assign the current pixel to the current index in our pixel array 00410 m_data[colorsRead + 0] = pColors[2]; 00411 m_data[colorsRead + 1] = pColors[1]; 00412 m_data[colorsRead + 2] = pColors[0]; 00413 00414 // If we have a 4 channel 32-bit image, assign one more for the alpha 00415 if(bits == 32) 00416 m_data[colorsRead + 3] = pColors[3]; 00417 00418 // Increase the current pixels read, decrease the amount 00419 // of pixels left, and increase the starting index for the next pixel. 00420 i++; 00421 rleID--; 00422 colorsRead += channels; 00423 } 00424 00425 } 00426 00427 } 00428 } 00429 00430 // Close the file pointer that opened the file 00431 fclose(pFile); 00432 00433 // Fill in our tImageTGA structure to pass back 00434 m_channels = channels; 00435 m_sizeX = width; 00436 m_sizeY = height; 00437 m_TextureType = TGA; 00438 return true; 00439 UnGuard 00440 } |
|
Définition à la ligne 840 du fichier ExCImageLoader.cpp. Références Guard, SaveBMP(), et UnGuard. Référencé par SaveBMP(), et SaveImage().
|
|
Définition à la ligne 775 du fichier ExCImageLoader.cpp. Références BMP, FindTextureType(), Guard, JPG, m_TextureType, PCX, PPM, RAW, RGB, SaveBMP(), SaveImage(), SaveJPG(), SavePCX(), SavePPM(), SaveRAW(), SaveRGB(), SaveTGA(), TGA, UnGuard, et UNKNOWN. Référencé par ExCTexture::SaveFile(), et SaveImage().
00776 { 00777 Guard(bool ExCImageLoader::SaveImage(std::string FileName,int width,int height)) 00778 m_TextureType=FindTextureType(FileName); 00779 00780 unsigned char * imageData; 00781 imageData= (unsigned char*) malloc(width*height*3); 00782 memset(imageData,255, width* height* 3); 00783 00784 glReadBuffer(GL_FRONT); 00785 glReadPixels(0, 0, width- 1, height- 1, GL_RGB, GL_UNSIGNED_BYTE, imageData); 00786 00787 00788 switch(m_TextureType) 00789 { 00790 case BMP:return SaveBMP(FileName,width,height,imageData); 00791 case TGA:return SaveTGA(FileName,width,height,imageData); 00792 case PCX:return SavePCX(FileName,width,height,imageData); 00793 case JPG:return SaveJPG(FileName,width,height,imageData); 00794 case RGB:return SaveRGB(FileName,width,height,imageData); 00795 case RAW:return SaveRAW(FileName,width,height,imageData); 00796 case PPM:return SavePPM(FileName,width,height,imageData); 00797 case UNKNOWN:return false; 00798 default:return false; 00799 } 00800 UnGuard 00801 } |
|
Définition à la ligne 853 du fichier ExCImageLoader.cpp. Références Guard, SaveJPG(), et UnGuard. Référencé par SaveImage(), et SaveJPG().
|
|
Définition à la ligne 846 du fichier ExCImageLoader.cpp. Références Guard, SavePCX(), et UnGuard. Référencé par SaveImage(), et SavePCX().
|
|
Définition à la ligne 874 du fichier ExCImageLoader.cpp. Références Guard, SavePPM(), et UnGuard. Référencé par SaveImage(), et SavePPM().
|
|
Définition à la ligne 867 du fichier ExCImageLoader.cpp. Références Guard, SaveRAW(), et UnGuard. Référencé par SaveImage(), et SaveRAW().
|
|
Définition à la ligne 860 du fichier ExCImageLoader.cpp. Références Guard, SaveRGB(), et UnGuard. Référencé par SaveImage(), et SaveRGB().
|
|
Définition à la ligne 803 du fichier ExCImageLoader.cpp. Références Guard, SaveTGA(), et UnGuard. Référencé par SaveImage(), et SaveTGA().
00804 { 00805 Guard(bool ExCImageLoader::SaveTGA(std::string strFileName,int width,int height,unsigned char * ImageData)) 00806 FILE *pFile; // The file pointer. 00807 unsigned char uselessChar=0; // used for useless char. 00808 short int uselessInt=0; // used for useless int. 00809 unsigned char imageType=2; // Type of image we are saving. 00810 int index=0; // used with the for loop. 00811 unsigned char bits=24; // Bit depth. 00812 int colorMode=3; 00813 long Size=width * height * colorMode; // Size of the picture. 00814 // Open file 00815 if(pFile= fopen(strFileName.data(), "wb")) 00816 { 00817 fwrite(&uselessChar, sizeof(unsigned char), 1, pFile);// Write useless data. 00818 fwrite(&uselessChar, sizeof(unsigned char), 1, pFile);// Write useless data. 00819 fwrite(&imageType, sizeof(unsigned char), 1, pFile);// Write image type. 00820 fwrite(&uselessInt, sizeof(short int), 1, pFile);// Write useless data. 00821 fwrite(&uselessInt, sizeof(short int), 1, pFile);// Write useless data. 00822 fwrite(&uselessChar, sizeof(unsigned char), 1, pFile);// Write useless data. 00823 fwrite(&uselessInt, sizeof(short int), 1, pFile);// Write useless data. 00824 fwrite(&uselessInt, sizeof(short int), 1, pFile);// Write useless data. 00825 fwrite(&width, sizeof(short int), 1, pFile);// Write width. 00826 fwrite(&height, sizeof(short int), 1, pFile);// Write height. 00827 fwrite(&bits, sizeof(unsigned char), 1, pFile);//Write bits 00828 fwrite(&uselessChar, sizeof(unsigned char), 1, pFile);// Write useless data. 00829 for(index = 0; index < Size; index += colorMode)// Transform data RGB to BGR. 00830 ImageData[index]^= ImageData[index + 2]^=ImageData[index]^=ImageData[index + 2]; 00831 fwrite(ImageData, sizeof(unsigned char), Size, pFile);//Write image data. 00832 fclose(pFile); 00833 return true; 00834 } 00835 fclose(pFile); 00836 return false; 00837 UnGuard 00838 } |
|
Définition à la ligne 124 du fichier ExCImageLoader.h. Référencé par LoadBMP(), ExCTexture::LoadFile(), et LoadTGA(). |
|
Définition à la ligne 130 du fichier ExCImageLoader.h. Référencé par LoadBMP(), LoadDDS(), ExCTexture::LoadFile(), LoadPCX(), LoadPPM(), LoadTGA(), et ~ExCImageLoader(). |
|
Définition à la ligne 134 du fichier ExCImageLoader.h. Référencé par LoadDDS(). |
|
Définition à la ligne 137 du fichier ExCImageLoader.h. Référencé par LoadDDS(). |
|
Définition à la ligne 136 du fichier ExCImageLoader.h. Référencé par LoadDDS(), et ExCTexture::LoadFile(). |
|
Définition à la ligne 135 du fichier ExCImageLoader.h. Référencé par LoadDDS(), et ExCTexture::LoadFile(). |
|
Définition à la ligne 131 du fichier ExCImageLoader.h. Référencé par LoadPCX(), et ~ExCImageLoader(). |
|
Définition à la ligne 128 du fichier ExCImageLoader.h. Référencé par LoadPCX(). |
|
Définition à la ligne 127 du fichier ExCImageLoader.h. Référencé par LoadPCX(). |
|
Définition à la ligne 125 du fichier ExCImageLoader.h. Référencé par LoadBMP(), LoadDDS(), ExCTexture::LoadFile(), LoadPCX(), LoadPPM(), et LoadTGA(). |
|
Définition à la ligne 126 du fichier ExCImageLoader.h. Référencé par LoadBMP(), LoadDDS(), ExCTexture::LoadFile(), LoadPCX(), LoadPPM(), et LoadTGA(). |
|
Définition à la ligne 123 du fichier ExCImageLoader.h. Référencé par LoadDDS(), ExCTexture::LoadFile(), LoadImage(), LoadTGA(), et SaveImage(). |
|
Définition à la ligne 129 du fichier ExCImageLoader.h. Référencé par LoadBMP(). |