#include "targa.h" #include #include TargaFormat::TargaFormat():imageData(NULL){} const unsigned char* TargaFormat::getImageData(){ return imageData; } unsigned char* TargaFormat::loadType2(FILE* targaFile){ if(header.pixelSize != 8 && header.pixelSize != 24 && header.pixelSize != 32){ std::cout << "Pixel size error = " << (int)header.pixelSize << std::endl; return NULL; } if(header.colorMapType!=0){ std::cout << "Color Map Type != 0 - Not supported yet." << std::endl; return NULL; } if(header.colorMapSpecification[0]!=0 || header.colorMapSpecification[1]!=0 || header.colorMapSpecification[2]!=0 || header.colorMapSpecification[3]!=0 || header.colorMapSpecification[4]!=0){ std::cout << "Color Map Specification is different of 0 - Not supported yet." << std::endl; return NULL; } //check descriptor. //check x and y origin. if(header.width==0 || header.height==0){ std::cout << "Size of image is 0, must be wrong" << std::endl; return NULL; } depth = header.pixelSize / 8; imageSize = header.width * header.height * depth; imageData = new unsigned char[(imageSize*sizeof(unsigned char))]; if(header.idLength==0){ if(fread(imageData, imageSize, 1, targaFile) != 1){ std::cout << "Read Error." << std::endl; free(imageData); fclose(targaFile); return NULL; } }else{ std::cout << "IDLenght is different of 0 - not supported yet." << std::endl; return NULL; } fclose(targaFile); return imageData; } const unsigned char* TargaFormat::getImageDataFrom(std::string path){ if(imageData != NULL) delete imageData; FILE* targaFile = fopen(path.c_str(),"rb"); if(targaFile == NULL) { std::cout << "Wrong path = " << path.c_str() << std::endl; return NULL; } fread(&header,sizeof(header),1,targaFile); if(header.imageTypeCode==2) return loadType2(targaFile); else std::cout << "Image Type Code is ("<< (int)header.imageTypeCode << ") it is different of 2 - Not Supported yet." << std::endl; return NULL; } unsigned short TargaFormat::getWidth(){ return header.width; } unsigned short TargaFormat::getHeight(){ return header.height; } unsigned char TargaFormat::getPixelSize(){ return header.pixelSize; } unsigned char TargaFormat::getImageDescriptorByte(){ return header.imageDescriptor; } unsigned char TargaFormat::getIdLenght(){ return header.idLength; } unsigned char TargaFormat::getColorMapType(){ return header.colorMapType; } unsigned char TargaFormat::getImageTypeCode(){ return header.imageTypeCode; } unsigned short TargaFormat::getXOrigin(){ return header.xOrigin; } unsigned short TargaFormat::getYOrigin(){ return header.yOrigin; } long int TargaFormat::getImageSize(){ return imageSize; } short TargaFormat::getDepth(){ return depth; } int TargaFormat::setTransparency(unsigned char r,unsigned char g,unsigned char b,unsigned char a){ if(imageData!=NULL){ for(int i = 0 ; i < imageSize ;){ if(imageData[i++]!=r){ i+=3;continue;} if(imageData[i++]!=g){ i+=2;continue;} if(imageData[i++]!=b){ i+=1;continue;} imageData[i++]=a; } return true; }return false; } TargaFormat::~TargaFormat(){ if(imageData!=NULL) delete imageData; }