bool FromBMP(quint8** out, int& outwidth, int& outheight, int& outbytes, const qstring& file) { FILE* infile = fopen(file.c_str(), "rb");; if( !infile ) return false; fseek(infile, 0, SEEK_END); long length = ftell(infile); fseek(infile, 0, SEEK_SET); char* data = (char*)malloc(length); if( !data ) { fclose(infile); return false; } fread(data, 1, length, infile); fclose(infile); size_t size = FromBMP(out, outwidth, outheight, outbytes, (size_t)length, data); free(data); return (0 != size); }
void ImageLoader(ResourceManager& res,Task* t) { //load input file name std::string fileName=GetElement(t,"filename","ImageLoader",res,false,""); //load input file format std::string format=GetElement(t,"format","ImageLoader",res,true,"hlt"); //load file to the buffer if(format=="hlt") res.Add(t->name,FromHLT(fileName)); if(format=="bmp") res.Add(t->name,FromBMP(fileName)); if(format=="raw") { int width=(int)ToFloat(GetElement(t,"width","ImageLoader",res,false,"")); int height=(int)ToFloat(GetElement(t,"height","ImageLoader",res,false,"")); res.Add(t->name,FromRAW(fileName,width,height)); } if(format!="hlt"&&format!="raw"&&format!="bmp") Log::AddMessage("ImageLoader: Unsupported format "+format,Log::ERR); }
int main () { quint8* img1 = 0; quint8* img2 = 0; quint8* img3; int width, height, bytes; if( !FromBMP(&img1, width, height, bytes, "../resources/1.bmp") ) return 1; if( !FromBMP(&img2, width, height, bytes, "../resources/2.bmp") ) return 1; img1 = ConvertTo32BPP(img1, width, height); img2 = ConvertTo32BPP(img2, width, height); img3 = new quint8[width * height * 4]; quint8 r, g, b; double a, x, y, z; quint8 r1, g1, b1; quint8 r2, g2, b2; quint8 c1, c2; int index; for( int i = 0; i < height; ++i ) { for( int j = 0; j < width; ++j ) { index = (i * width + j) * 4; //if( i == height - 126 - 1 && j == 122 ) // ::_CrtDbgBreak(); r1 = img1[index + 0]; g1 = img1[index + 1]; b1 = img1[index + 2]; r2 = img2[index + 0]; g2 = img2[index + 1]; b2 = img2[index + 2]; if( r1 == r2 && g1 == g2 && b1 == b2 ) { // same -> no alpha a = 1.0; r = r1; g = g1; b = b1; } else { c1 = 255; c2 = 0; if( r1 - r2 < 255 ) { c1 = c1; c2 = r2; } if( g1 - g2 < 255 ) { if( g2 > c2 ) { c1 = c1; c2 = g2; } } if( b1 - b2 < 255 ) { if( b2 > c2 ) { c1 = c1; c2 = b2; } } if( c1 - c2 < 255 ) { a = (255.0 - (c1 - c2)) / 255.0; //if( a > 1.0 || a < 0.0 ) // ::_CrtDbgBreak(); x = std::min<double>(r2 / a, 255.0); y = std::min<double>(g2 / a, 255.0); z = std::min<double>(b2 / a, 255.0); r = (quint8)x; g = (quint8)y; b = (quint8)z; } else { // background in both a = 0.0; r = 0; g = 0; b = 0; } } img3[index + 0] = r; img3[index + 1] = g; img3[index + 2] = b; img3[index + 3] = (quint8)(a * 255.0); } } ToTGA(img3, width, height, 4, "../resources/test.tga"); delete[] img1; delete[] img2; delete[] img3; //system("pause"); return 0; }