tga.cpp tga.h datatypes.h OpenArena #include"tga.h" #include"datatypes.h" #ifdefWIN32 #pragmawarning(disable:4996) #endif namespaceOpenArena{ TextureImage*LoadTGA(constchar*filename) { TGAHeadertgaheader; TextureImage*image; std::stringerrmsg; FILE*file=fopen(filename,"rb"); if(file==NULL) { errmsg="Couldnotopentexturefile"; errmsg=errmsg+filename; //Thisneedstobeabstractedsomehow #ifdefWIN32 MessageBox(NULL,errmsg.c_str(),"ERROR",MB_OK); #endif returnNULL; } if(fread(&tgaheader,sizeof(TGAHeader),1,file)==0) { //Thisneedstobeabstractedsomehow #ifdefWIN32 MessageBox(NULL,"Couldnotreadfileheader","ERROR",MB_OK); #endif if(file!=NULL) fclose(file); returnNULL; } if(memcmp(uTGAcompare,&tgaheader,sizeof(tgaheader))==0) image=LoadUncompressedTGA(file); elseif(memcmp(cTGAcompare,&tgaheader,sizeof(tgaheader))==0) image=LoadCompressedTGA(file); else { //Thisneedstobeabstractedsomehow #ifdefWIN32 MessageBox(NULL,"TGAfilemustbetype2ortype10","InvalidImage",MB_OK); #endif fclose(file); returnNULL; } returnimage; } TextureImage*LoadUncompressedTGA(FILE*fTGA) { TGAtga; TextureImage*image=newTextureImage; if(fread(tga.header,sizeof(tga.header),1,fTGA)==0) { //Thisneedstobeabstractedsomehow #ifdefWIN32 MessageBox(NULL,"Couldnotreadinfoheader","ERROR",MB_OK); #endif if(fTGA!=NULL) { fclose(fTGA); } returnNULL; } image->sizeY=tga.header[1]*256+tga.header[0]; image->sizeX=tga.header[3]*256+tga.header[2]; image->bpp=tga.header[4]; tga.Width=image->sizeX; tga.Height=image->sizeY; tga.Bpp=image->bpp; if((image->sizeX<=0)||(image->sizeY<=0)||((image->bpp!=24)&&(image->bpp!=32))) { //Thisneedstobeabstractedsomehow #ifdefWIN32 MessageBox(NULL,"Invalidtextureinformation","ERROR",MB_OK); #endif if(fTGA!=NULL) { fclose(fTGA); } returnNULL; } if(image->bpp==24) { image->type=GL_RGB; } else { image->type=GL_RGBA; } tga.bytesPerPixel=(tga.Bpp/8); tga.imageSize=(tga.bytesPerPixel*tga.Width*tga.Height); image->data=(GLubyte*)malloc(tga.imageSize); if(image->data==NULL) { //Thisneedstobeabstractedsomehow #ifdefWIN32 MessageBox(NULL,"Couldnotallocatememoryforimage","ERROR",MB_OK); #endif fclose(fTGA); returnNULL; } if(fread(image->data,1,tga.imageSize,fTGA)!=tga.imageSize) { //Thisneedstobeabstractedsomehow #ifdefWIN32 MessageBox(NULL,"Couldnotreadimagedata","ERROR",MB_OK); #endif if(image->data!=NULL) { free(image->data); } fclose(fTGA); returnNULL; } for(GLuintcswap=0;cswap<(int)tga.imageSize;cswap+=tga.bytesPerPixel) { image->data[cswap]^=image->data[cswap+2]^= image->data[cswap]^=image->data[cswap+2]; } fclose(fTGA); returnimage; } TextureImage*LoadCompressedTGA(FILE*fTGA) { TextureImage*image=newTextureImage; TGAtga; if(fread(tga.header,sizeof(tga.header),1,fTGA)==0) { //Thisneedstobeabstractedsomehow #ifdefWIN32 MessageBox(NULL,"Couldnotreadinfoheader","ERROR",MB_OK); #endif if(fTGA!=NULL) { fclose(fTGA); } returnNULL; } image->sizeX=tga.header[1]*256+tga.header[0]; image->sizeY=tga.header[3]*256+tga.header[2]; image->bpp=tga.header[4]; tga.Width=image->sizeX; tga.Height=image->sizeY; tga.Bpp=image->bpp; if((image->sizeX<=0)||(image->sizeY<=0)||((image->bpp!=24)&&(image->bpp!=32))) { //Thisneedstobeabstractedsomehow #ifdefWIN32 MessageBox(NULL,"Invalidtextureinformation","ERROR",MB_OK); #endif if(fTGA!=NULL) { fclose(fTGA); } returnNULL; } tga.bytesPerPixel=(tga.Bpp/8); tga.imageSize=(tga.bytesPerPixel*tga.Width*tga.Height); image->data=(GLubyte*)malloc(tga.imageSize); if(image->data==NULL) { //Thisneedstobeabstractedsomehow #ifdefWIN32 MessageBox(NULL,"Couldnotallocatememoryforimage","ERROR",MB_OK); #endif fclose(fTGA); returnNULL; } GLuintpixelcount=tga.Height*tga.Width; GLuintcurrentpixel=0; GLuintcurrentbyte=0; GLubyte*colorbuffer=(GLubyte*)malloc(tga.bytesPerPixel); do { GLubytechunkheader=0; if(fread(&chunkheader,sizeof(GLubyte),1,fTGA)==0) { //Thisneedstobeabstractedsomehow #ifdefWIN32 MessageBox(NULL,"CouldnotreadRLEheader","ERROR",MB_OK); #endif if(fTGA!=NULL) { fclose(fTGA); } if(image->data!=NULL) { free(image->data); } returnNULL; } if(chunkheader<128) { chunkheader++; for(shortcounter=0;counter<chunkheader;counter++) { if(fread(colorbuffer,1,tga.bytesPerPixel,fTGA)!=tga.bytesPerPixel) { //Thisneedstobeabstractedsomehow #ifdefWIN32 MessageBox(NULL,"Couldnotreadimagedata","ERROR",MB_OK); #endif if(fTGA!=NULL) { fclose(fTGA); } if(colorbuffer!=NULL) { free(colorbuffer); } if(image->data!=NULL) { free(image->data); } returnNULL; } image->data[currentbyte]=colorbuffer[2]; image->data[currentbyte+1]=colorbuffer[1]; image->data[currentbyte+2]=colorbuffer[0]; if(tga.bytesPerPixel==4) { image->data[currentbyte+3]=colorbuffer[3]; } currentbyte+=tga.bytesPerPixel; currentpixel++; if(currentpixel>pixelcount) { //Thisneedstobeabstractedsomehow #ifdefWIN32 MessageBox(NULL,"Toomanypixelsread","ERROR",NULL); #endif if(fTGA!=NULL) { fclose(fTGA); } if(colorbuffer!=NULL) { free(colorbuffer); } if(image->data!=NULL) { free(image->data); } returnNULL; } } } else { chunkheader-=127; if(fread(colorbuffer,1,tga.bytesPerPixel,fTGA)!=tga.bytesPerPixel) { //Thisneedstobeabstractedsomehow #ifdefWIN32 MessageBox(NULL,"Couldnotreadfromfile","ERROR",MB_OK); #endif if(fTGA!=NULL) { fclose(fTGA); } if(colorbuffer!=NULL) { free(colorbuffer); } if(image->data!=NULL) { free(image->data); } returnNULL; } for(shortcounter=0;counter<chunkheader;counter++) { image->data[currentbyte]=colorbuffer[2]; image->data[currentbyte+1]=colorbuffer[1]; image->data[currentbyte+2]=colorbuffer[0]; if(tga.bytesPerPixel==4) { image->data[currentbyte+3]=colorbuffer[3]; } currentbyte+=tga.bytesPerPixel; currentpixel++; if(currentpixel>pixelcount) { //Thisneedstobeabstractedsomehow #ifdefWIN32 MessageBox(NULL,"Toomanypixelsread","ERROR",NULL); #endif if(fTGA!=NULL) { fclose(fTGA); } if(colorbuffer!=NULL) { free(colorbuffer); } if(image->data!=NULL) { free(image->data); } returnNULL; } } } } while(currentpixel<pixelcount); fclose(fTGA); returnimage; } /* TextureImage*LoadTGA(constchar*filename) { TGAHeadertgaheader; TextureImage*image; std::stringerrmsg; FILE*file=fopen(filename,"rb"); if(file==NULL) { errmsg="Couldnotopentexturefile"; errmsg=errmsg+filename; MessageBox(NULL,errmsg.c_str(),"ERROR",MB_OK); returnNULL; } if(fread(&tgaheader,sizeof(TGAHeader),1,file)==0) { MessageBox(NULL,"Couldnotreadfileheader","ERROR",MB_OK); if(file!=NULL) fclose(file); returnNULL; } if(memcmp(uTGAcompare,&tgaheader,sizeof(tgaheader))==0) image=LoadUncompressedTGA(file); elseif(memcmp(cTGAcompare,&tgaheader,sizeof(tgaheader))==0) image=LoadCompressedTGA(file); else { MessageBox(NULL,"TGAfilemustbetype2ortype10","InvalidImage",MB_OK); fclose(file); returnNULL; } returnimage; } TextureImage*LoadUncompressedTGA(FILE*fTGA) { TGAtga; TextureImage*image=newTextureImage; if(fread(tga.header,sizeof(tga.header),1,fTGA)==0) { MessageBox(NULL,"Couldnotreadinfoheader","ERROR",MB_OK); if(fTGA!=NULL) { fclose(fTGA); } returnNULL; } image->sizeY=tga.header[1]*256+tga.header[0]; image->sizeX=tga.header[3]*256+tga.header[2]; image->bpp=tga.header[4]; tga.Width=image->sizeX; tga.Height=image->sizeY; tga.Bpp=image->bpp; if((image->sizeX<=0)||(image->sizeY<=0)||((image->bpp!=24)&&(image->bpp!=32))) { MessageBox(NULL,"Invalidtextureinformation","ERROR",MB_OK); if(fTGA!=NULL) { fclose(fTGA); } returnNULL; } if(image->bpp==24) { image->type=GL_RGB; } else { image->type=GL_RGBA; } tga.bytesPerPixel=(tga.Bpp/8); tga.imageSize=(tga.bytesPerPixel*tga.Width*tga.Height); image->data=(GLubyte*)malloc(tga.imageSize); if(image->data==NULL) { MessageBox(NULL,"Couldnotallocatememoryforimage","ERROR",MB_OK); fclose(fTGA); returnNULL; } if(fread(image->data,1,tga.imageSize,fTGA)!=tga.imageSize) { MessageBox(NULL,"Couldnotreadimagedata","ERROR",MB_OK); if(image->data!=NULL) { free(image->data); } fclose(fTGA); returnNULL; } for(GLuintcswap=0;cswap<(int)tga.imageSize;cswap+=tga.bytesPerPixel) { image->data[cswap]^=image->data[cswap+2]^= image->data[cswap]^=image->data[cswap+2]; } fclose(fTGA); returnimage; } TextureImage*LoadCompressedTGA(FILE*fTGA) { TextureImage*image=newTextureImage; TGAtga; if(fread(tga.header,sizeof(tga.header),1,fTGA)==0) { MessageBox(NULL,"Couldnotreadinfoheader","ERROR",MB_OK); if(fTGA!=NULL) { fclose(fTGA); } returnNULL; } image->sizeX=tga.header[1]*256+tga.header[0]; image->sizeY=tga.header[3]*256+tga.header[2]; image->bpp=tga.header[4]; tga.Width=image->sizeX; tga.Height=image->sizeY; tga.Bpp=image->bpp; if((image->sizeX<=0)||(image->sizeY<=0)||((image->bpp!=24)&&(image->bpp!=32))) { MessageBox(NULL,"Invalidtextureinformation","ERROR",MB_OK); if(fTGA!=NULL) { fclose(fTGA); } returnNULL; } tga.bytesPerPixel=(tga.Bpp/8); tga.imageSize=(tga.bytesPerPixel*tga.Width*tga.Height); image->data=(GLubyte*)malloc(tga.imageSize); if(image->data==NULL) { MessageBox(NULL,"Couldnotallocatememoryforimage","ERROR",MB_OK); fclose(fTGA); returnNULL; } GLuintpixelcount=tga.Height*tga.Width; GLuintcurrentpixel=0; GLuintcurrentbyte=0; GLubyte*colorbuffer=(GLubyte*)malloc(tga.bytesPerPixel); do { GLubytechunkheader=0; if(fread(&chunkheader,sizeof(GLubyte),1,fTGA)==0) { MessageBox(NULL,"CouldnotreadRLEheader","ERROR",MB_OK); if(fTGA!=NULL) { fclose(fTGA); } if(image->data!=NULL) { free(image->data); } returnNULL; } if(chunkheader<128) { chunkheader++; for(shortcounter=0;counter<chunkheader;counter++) { if(fread(colorbuffer,1,tga.bytesPerPixel,fTGA)!=tga.bytesPerPixel) { MessageBox(NULL,"Couldnotreadimagedata","ERROR",MB_OK); if(fTGA!=NULL) { fclose(fTGA); } if(colorbuffer!=NULL) { free(colorbuffer); } if(image->data!=NULL) { free(image->data); } returnNULL; } image->data[currentbyte]=colorbuffer[2]; image->data[currentbyte+1]=colorbuffer[1]; image->data[currentbyte+2]=colorbuffer[0]; if(tga.bytesPerPixel==4) { image->data[currentbyte+3]=colorbuffer[3]; } currentbyte+=tga.bytesPerPixel; currentpixel++; if(currentpixel>pixelcount) { MessageBox(NULL,"Toomanypixelsread","ERROR",NULL); if(fTGA!=NULL) { fclose(fTGA); } if(colorbuffer!=NULL) { free(colorbuffer); } if(image->data!=NULL) { free(image->data); } returnNULL; } } } else { chunkheader-=127; if(fread(colorbuffer,1,tga.bytesPerPixel,fTGA)!=tga.bytesPerPixel) { MessageBox(NULL,"Couldnotreadfromfile","ERROR",MB_OK); if(fTGA!=NULL) { fclose(fTGA); } if(colorbuffer!=NULL) { free(colorbuffer); } if(image->data!=NULL) { free(image->data); } returnNULL; } for(shortcounter=0;counter<chunkheader;counter++) { image->data[currentbyte]=colorbuffer[2]; image->data[currentbyte+1]=colorbuffer[1]; image->data[currentbyte+2]=colorbuffer[0]; if(tga.bytesPerPixel==4) { image->data[currentbyte+3]=colorbuffer[3]; } currentbyte+=tga.bytesPerPixel; currentpixel++; if(currentpixel>pixelcount) { MessageBox(NULL,"Toomanypixelsread","ERROR",NULL); if(fTGA!=NULL) { fclose(fTGA); } if(colorbuffer!=NULL) { free(colorbuffer); } if(image->data!=NULL) { free(image->data); } returnNULL; } } } } while(currentpixel<pixelcount); fclose(fTGA); returnimage; } */ };