From f8ceec9e3d31bf06d9849cdfde3db045eaab5139 Mon Sep 17 00:00:00 2001 From: kervala Date: Sat, 23 Jan 2016 10:28:00 +0100 Subject: [PATCH] Merge with develop --- code/nel/tools/3d/tga_2_dds/tga2dds.cpp | 783 +++++++++++------------- 1 file changed, 370 insertions(+), 413 deletions(-) diff --git a/code/nel/tools/3d/tga_2_dds/tga2dds.cpp b/code/nel/tools/3d/tga_2_dds/tga2dds.cpp index de8b0b602..17ce786bc 100644 --- a/code/nel/tools/3d/tga_2_dds/tga2dds.cpp +++ b/code/nel/tools/3d/tga_2_dds/tga2dds.cpp @@ -21,6 +21,8 @@ #include "nel/misc/bitmap.h" #include "nel/misc/path.h" #include "nel/misc/debug.h" +#include "nel/misc/cmd_args.h" + #include #include "../s3tc_compressor_lib/s3tc_compressor.h" @@ -40,7 +42,6 @@ using namespace std; bool sameType(const std::string &sFileNameDest, uint8 algo); bool dataCheck(const std::string &sFileNameSrc, const std::string &FileNameDest, uint8 algo); std::string getOutputFileName(const std::string &inputFileName); -void writeInstructions(); @@ -197,39 +198,6 @@ bool dataCheck(const std::string &sFileNameSrc, const std::string &sFileNameDest return true; } -void writeInstructions() -{ - cout< [-o ] [-a ] [-m]"<=argc) return false; - OptOutputFileName= argv[i]; - } - // Algo. - else if(!strcmp(argv[i], "-a")) - { - i++; - if(i>=argc) return false; - if(!strcmp(argv[i],"1")) OptAlgo = DXT1; - else - if(!strcmp(argv[i],"1A")) OptAlgo = DXT1A; - else - if(!strcmp(argv[i],"1a")) OptAlgo = DXT1A; - else - if(!strcmp(argv[i],"3")) OptAlgo = DXT3; - else - if(!strcmp(argv[i],"5")) OptAlgo = DXT5; - else - if(!strcmp(argv[i],"tga16")) OptAlgo = TGA16; - else - if(!strcmp(argv[i],"tga8")) OptAlgo = TGA8; - else - if(!strcmp(argv[i],"png16")) OptAlgo = PNG16; - else - if(!strcmp(argv[i],"png8")) OptAlgo = PNG8; - else - { - cerr<<"Algorithm unknown : "< RGBASrc = picTga.getPixels(); - CObjectVector RGBASrc2; - CObjectVector RGBADest; - RGBADest.resize(height*width*4); - uint dstRGBADestId= 0; - - // UserColor - //=========== - /* - // Checking if option "usercolor" has been used - std::string userColorFileName; - if(argc>4) - { - if(strcmp("-usercolor",argv[4])==0) { - if(argc!=6) + cerr << "Unknown algorithm: " << strAlgo << endl; + return 1; + } + } + + if (args.haveArg("r")) + { + std::string strReduce = args.getArg("r").front(); + + // Reduce size of the bitmap + if (fromString(strReduce, Reduce)) + { + if (Reduce > 8) Reduce = 8; + } + } + + std::vector inputFileNames = args.getAdditionalArg("input"); + + for(uint i = 0; i < inputFileNames.size(); ++i) + { + uint8 algo; + + // Reading TGA or PNG and converting to RGBA + //==================================== + CBitmap picTga; + CBitmap picTga2; + CBitmap picSrc; + + std::string inputFileName = inputFileNames[i]; + + if(inputFileName.find("_usercolor") 1) + { + cerr<<"WARNING! Several files to convert to the same output filename! Use an output directory instead."< RGBASrc = picTga.getPixels(); + CObjectVector RGBASrc2; + CObjectVector RGBADest; + RGBADest.resize(height*width*4); + uint dstRGBADestId= 0; + + // UserColor + //=========== + /* + // Checking if option "usercolor" has been used + std::string userColorFileName; + if(argc>4) + { + if(strcmp("-usercolor",argv[4])==0) + { + if(argc!=6) + { + writeInstructions(); + return; + } + userColorFileName = argv[5]; + } + else { writeInstructions(); return; } - userColorFileName = argv[5]; + } + */ + // Checking if associate usercolor file exists + std::string userColorFileName; + std::string::size_type pos = inputFileName.rfind("."); + if (pos == std::string::npos) + { + // name without extension + userColorFileName = inputFileName + "_usercolor"; } else { - writeInstructions(); - return; + // append input filename extension + userColorFileName = inputFileName.substr(0,pos) + "_usercolor" + inputFileName.substr(pos); } - } - */ - // Checking if associate usercolor file exists - std::string userColorFileName; - std::string::size_type pos = inputFileName.rfind("."); - if (pos == std::string::npos) - { - // name without extension - userColorFileName = inputFileName + "_usercolor"; - } - else - { - // append input filename extension - userColorFileName = inputFileName.substr(0,pos) + "_usercolor" + inputFileName.substr(pos); - } - // Reading second Tga for user color, don't complain if _usercolor is missing - NLMISC::CIFile input2; - if (CPath::exists(userColorFileName) && input2.open(userColorFileName)) - { - picTga2.load(input2); - uint32 height2 = picTga2.getHeight(); - uint32 width2 = picTga2.getWidth(); - nlassert(width2==width); - nlassert(height2==height); - picTga2.convertToType (CBitmap::RGBA); - - RGBASrc2 = picTga2.getPixels(); - - NLMISC::CRGBA *pRGBASrc = (NLMISC::CRGBA*)&RGBASrc[0]; - NLMISC::CRGBA *pRGBASrc2 = (NLMISC::CRGBA*)&RGBASrc2[0]; - - for(uint32 i = 0; i0) + // If no UserColor, must take same RGB, and keep same Alpha from src1 !!! So texture can have both alpha + // userColor and other alpha usage. + if(pRGBASrc2[i].A==255) { - Rtm= Rt*At / Atm; - Gtm= Gt*At / Atm; - Btm= Bt*At / Atm; + RGBADest[dstRGBADestId++]= pRGBASrc[i].R; + RGBADest[dstRGBADestId++]= pRGBASrc[i].G; + RGBADest[dstRGBADestId++]= pRGBASrc[i].B; + RGBADest[dstRGBADestId++]= pRGBASrc[i].A; } - // Else special case: At==0, and Lt==1. else { - Rtm= Gtm= Btm= 0; + // Old code. + /*uint8 F = (uint8) ((float)pRGBASrc[i].R*0.3 + (float)pRGBASrc[i].G*0.56 + (float)pRGBASrc[i].B*0.14); + uint8 Frgb; + if((F*pRGBASrc2[i].A/255)==255) + Frgb = 0; + else + Frgb = (255-pRGBASrc2[i].A)/(255-F*pRGBASrc2[i].A/255); + RGBADest[dstRGBADestId++]= Frgb*pRGBASrc[i].R/255; + RGBADest[dstRGBADestId++]= Frgb*pRGBASrc[i].G/255; + RGBADest[dstRGBADestId++]= Frgb*pRGBASrc[i].B/255; + RGBADest[dstRGBADestId++]= F*pRGBASrc[i].A/255;*/ + + // New code: use new restrictions from IDriver. + float Rt, Gt, Bt, At; + float Lt; + float Rtm, Gtm, Btm, Atm; + + // read 0-1 RGB pixel. + Rt= (float)pRGBASrc[i].R/255; + Gt= (float)pRGBASrc[i].G/255; + Bt= (float)pRGBASrc[i].B/255; + Lt= Rt*0.3f + Gt*0.56f + Bt*0.14f; + + // take Alpha from userColor src. + At= (float)pRGBASrc2[i].A/255; + Atm= 1-Lt*(1-At); + + // If normal case. + if(Atm>0) + { + Rtm= Rt*At / Atm; + Gtm= Gt*At / Atm; + Btm= Bt*At / Atm; + } + // Else special case: At==0, and Lt==1. + else + { + Rtm= Gtm= Btm= 0; + } + + // copy to buffer. + sint r,g,b,a; + r= (sint)floor(Rtm*255+0.5f); + g= (sint)floor(Gtm*255+0.5f); + b= (sint)floor(Btm*255+0.5f); + a= (sint)floor(Atm*255+0.5f); + clamp(r, 0,255); + clamp(g, 0,255); + clamp(b, 0,255); + clamp(a, 0,255); + RGBADest[dstRGBADestId++]= r; + RGBADest[dstRGBADestId++]= g; + RGBADest[dstRGBADestId++]= b; + RGBADest[dstRGBADestId++]= a; } - - // copy to buffer. - sint r,g,b,a; - r= (sint)floor(Rtm*255+0.5f); - g= (sint)floor(Gtm*255+0.5f); - b= (sint)floor(Btm*255+0.5f); - a= (sint)floor(Atm*255+0.5f); - clamp(r, 0,255); - clamp(g, 0,255); - clamp(b, 0,255); - clamp(a, 0,255); - RGBADest[dstRGBADestId++]= r; - RGBADest[dstRGBADestId++]= g; - RGBADest[dstRGBADestId++]= b; - RGBADest[dstRGBADestId++]= a; } } - } - else - RGBADest = RGBASrc; + else + RGBADest = RGBASrc; - // Copy to the dest bitmap. - picSrc.resize(width, height, CBitmap::RGBA); - picSrc.getPixels(0)= RGBADest; + // Copy to the dest bitmap. + picSrc.resize(width, height, CBitmap::RGBA); + picSrc.getPixels(0)= RGBADest; - // Resize the destination bitmap ? - while (Reduce != 0) - { - dividSize (picSrc); - Reduce--; - } - - if (algo == TGA16) - { - // Apply bayer dither - CObjectVector &rgba = picSrc.getPixels(0); - const uint32 w = picSrc.getWidth(0); - uint32 x = 0; - uint32 y = 0; - for (uint32 i = 0; i < rgba.size(); i += 4) + // Resize the destination bitmap ? + while (Reduce != 0) { - NLMISC::CRGBA &c = reinterpret_cast(rgba[i]); - c.R = (uint8)std::min(255, (int)c.R + bayerDiv8R[x % 4][y % 4]); - c.G = (uint8)std::min(255, (int)c.G + bayerDiv8G[x % 4][y % 4]); - c.B = (uint8)std::min(255, (int)c.B + bayerDiv8B[x % 4][y % 4]); - ++x; - x %= w; - if (x == 0) - ++y; + dividSize (picSrc); + Reduce--; } - } - // 8 or 16 bits TGA or PNG ? - if ((algo == TGA16) || (algo == TGA8) || (algo == PNG16) || (algo == PNG8)) - { - // Saving TGA or PNG file - //================= - NLMISC::COFile output; - if(!output.open(outputFileName)) + if (algo == TGA16) { - cerr<<"Can't open output file "< &rgba = picSrc.getPixels(0); + const uint32 w = picSrc.getWidth(0); + uint32 x = 0; + uint32 y = 0; + for (uint32 i = 0; i < rgba.size(); i += 4) { - picSrc.writeTGA (output, 16); + NLMISC::CRGBA &c = reinterpret_cast(rgba[i]); + c.R = (uint8)std::min(255, (int)c.R + bayerDiv8R[x % 4][y % 4]); + c.G = (uint8)std::min(255, (int)c.G + bayerDiv8G[x % 4][y % 4]); + c.B = (uint8)std::min(255, (int)c.B + bayerDiv8B[x % 4][y % 4]); + ++x; + x %= w; + if (x == 0) + ++y; } - else if (algo == TGA8) + } + + // 8 or 16 bits TGA or PNG ? + if ((algo == TGA16) || (algo == TGA8) || (algo == PNG16) || (algo == PNG8)) + { + // Saving TGA or PNG file + //================= + NLMISC::COFile output; + if(!output.open(outputFileName)) { - picSrc.convertToType(CBitmap::Luminance); - picSrc.writeTGA (output, 8); + cerr<<"Can't open output file "<