Changed: Use CCmdArgs for tga2dds

This commit is contained in:
kervala 2016-01-23 10:27:34 +01:00
parent 59e3f0b9ef
commit 87f63145ba

View file

@ -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 <math.h>
#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<<endl;
cout<<"TGA2DDS"<<endl;
cout<<" Convert TGA or PNG image file (24bits or 32 bits) to DDS compressed file using"<<endl;
cout<<"DXTC compression (DXTC1, DXTC1 with alpha, DXTC3, or DXTC5). "<<endl;
cout<<" The program looks for possible user color files and load them automatically,"<<endl;
cout<<"a user color file must have the same name that the original tga file, plus the"<<endl;
cout<<"extension \"_usercolor\""<<endl;
cout<<"ex : pic.tga, the associated user color file must be : pic_usercolor.tga"<<endl;
cout<<endl;
cout<<"syntax : tga2dds <input> [-o <output.dds>] [-a <algo>] [-m]"<<endl;
cout<<endl;
cout<<"with"<<endl;
cout<<"algo : 1 for DXTC1 (no alpha)"<<endl;
cout<<" 1A for DXTC1 with alpha"<<endl;
cout<<" 3 for DXTC3"<<endl;
cout<<" 5 for DXTC5"<<endl;
cout<<" tga16 for 16 bits TGA"<<endl;
cout<<" tga8 for 8 bits TGA"<<endl;
cout<<" png16 for 16 bits PNG"<<endl;
cout<<" png8 for 8 bits PNG"<<endl;
cout<<"-m : Create MipMap"<<endl;
cout<<"-rFACTOR : Reduce the bitmap size before compressing"<<endl;
cout<<" FACTOR is 0, 1, 2, 3, 4, 5, 6, 7 or 8"<<endl;
cout<<endl;
cout<<"default : DXTC1 if 24b, DXTC5 if 32b."<<endl;
cout<<endl;
cout<<"/? for this help"<<endl;
cout<<endl;
}
std::string getOutputFileName(const std::string &inputFileName)
{
std::string::size_type pos = inputFileName.rfind(".");
@ -244,85 +212,6 @@ std::string getOutputFileName(const std::string &inputFileName)
}
}
// ***************************************************************************
string OptOutputFileName;
uint8 OptAlgo = NOT_DEFINED;
bool OptMipMap = false;
uint Reduce = 0;
bool parseOptions(int argc, char **argv)
{
for(sint i=2;i<argc;i++)
{
// OutputFileName.
if(!strcmp(argv[i], "-o"))
{
i++;
if(i>=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 : "<<argv[i]<<endl;
return 1;
}
}
// MipMap.
else if(!strcmp(argv[i], "-m"))
{
OptMipMap= true;
}
// Reduce size of the bitmap
else if(!strcmp(argv[i], "-r0"))
Reduce = 0;
else if(!strcmp(argv[i], "-r1"))
Reduce = 1;
else if(!strcmp(argv[i], "-r2"))
Reduce = 2;
else if(!strcmp(argv[i], "-r3"))
Reduce = 3;
else if(!strcmp(argv[i], "-r4"))
Reduce = 4;
else if(!strcmp(argv[i], "-r5"))
Reduce = 5;
else if(!strcmp(argv[i], "-r6"))
Reduce = 6;
else if(!strcmp(argv[i], "-r7"))
Reduce = 7;
else if(!strcmp(argv[i], "-r8"))
Reduce = 8;
// What is this option?
else
{
return false;
}
}
return true;
}
// ***************************************************************************
void dividSize (CBitmap &bitmap)
{
@ -385,30 +274,77 @@ int main(int argc, char **argv)
{
CApplicationContext applicationContext;
uint8 algo;
// Parse Command Line.
//====================
if(argc<2)
NLMISC::CCmdArgs args;
args.setDescription(
"Convert TGA or PNG image file to DDS compressed file using DXTC compression (DXTC1, DXTC1 with alpha, DXTC3, or DXTC5).\n"
" The program looks for possible user color files and load them automatically, a user color file must have the same name that the original tga file, plus the extension \"_usercolor\"\n"
"Eg.: pic.tga, the associated user color file must be: pic_usercolor.tga\n"
);
args.addArg("o", "output", "output.dds", "Output DDS filename or directory");
args.addArg("a", "algo", "algo", "Conversion algorithm to use\n"
" 1 for DXTC1 (no alpha)\n"
" 1A for DXTC1 with alpha\n"
" 3 for DXTC3\n"
" 5 for DXTC5\n"
" tga16 for 16 bits TGA\n"
" tga8 for 8 bits TGA\n"
" png16 for 16 bits PNG\n"
" png8 for 8 bits PNG\n"
"\n"
" default : DXTC1 if 24 bits, DXTC5 if 32 bits."
);
args.addArg("m", "mipmap", "", "Create MipMap");
args.addArg("r", "reduce", "FACTOR", "Reduce the bitmap size before compressing\n\t\t\tFACTOR is 0, 1, 2, 3, 4, 5, 6, 7 or 8");
args.addAdditionalArg("input", "PNG or TGA files to convert", false);
if (!args.parse(argc, argv)) return 1;
string OptOutputFileName;
uint8 OptAlgo = NOT_DEFINED;
bool OptMipMap = false;
uint Reduce = 0;
if (args.haveArg("o"))
OptOutputFileName = args.getArg("o").front();
if (args.haveArg("a"))
{
writeInstructions();
return 0;
}
if(!strcmp(argv[1],"/?"))
std::string strAlgo = args.getArg("a").front();
if (strAlgo == "1") OptAlgo = DXT1;
else if (toLower(strAlgo) == "1a") OptAlgo = DXT1A;
else if (strAlgo == "3") OptAlgo = DXT3;
else if (strAlgo == "5") OptAlgo = DXT5;
else if (strAlgo == "tga8") OptAlgo = TGA8;
else if (strAlgo == "tga16") OptAlgo = TGA16;
else if (strAlgo == "png8") OptAlgo = PNG8;
else if (strAlgo == "png16") OptAlgo = PNG16;
else
{
writeInstructions();
return 0;
cerr << "Unknown algorithm: " << strAlgo << endl;
return 1;
}
if(!strcmp(argv[1],"-?"))
}
if (args.haveArg("r"))
{
writeInstructions();
return 0;
}
if(!parseOptions(argc, argv))
std::string strReduce = args.getArg("r").front();
// Reduce size of the bitmap
if (fromString(strReduce, Reduce))
{
writeInstructions();
return 0;
if (Reduce > 8) Reduce = 8;
}
}
std::vector<std::string> inputFileNames = args.getAdditionalArg("input");
for(uint i = 0; i < inputFileNames.size(); ++i)
{
uint8 algo;
// Reading TGA or PNG and converting to RGBA
//====================================
@ -416,7 +352,8 @@ int main(int argc, char **argv)
CBitmap picTga2;
CBitmap picSrc;
std::string inputFileName(argv[1]);
std::string inputFileName = inputFileNames[i];
if(inputFileName.find("_usercolor")<inputFileName.length())
{
return 0;
@ -424,18 +361,18 @@ int main(int argc, char **argv)
NLMISC::CIFile input;
if(!input.open(inputFileName))
{
cerr<<"Can't open input file "<<inputFileName<<endl;
cerr<<"Can't open input file " << inputFileName << endl;
return 1;
}
uint8 imageDepth = picTga.load(input);
if(imageDepth==0)
{
cerr<<"Can't load file : "<<inputFileName<<endl;
cerr<<"Can't load file: "<<inputFileName<<endl;
return 1;
}
if(imageDepth!=16 && imageDepth!=24 && imageDepth!=32 && imageDepth!=8)
{
cerr<<"Image not supported : "<<imageDepth<<endl;
cerr<<"Image not supported: "<<imageDepth<<endl;
return 1;
}
input.close();
@ -447,10 +384,29 @@ int main(int argc, char **argv)
// Output file name and algo.
//===========================
std::string outputFileName;
if (!OptOutputFileName.empty())
outputFileName = OptOutputFileName;
{
// if OptOutputFileName is a directory, append the original filename
if (CFile::isDirectory(OptOutputFileName))
{
outputFileName = CPath::standardizePath(outputFileName) + getOutputFileName(inputFileName);
}
else
{
outputFileName = OptOutputFileName;
if (inputFileNames.size() > 1)
{
cerr<<"WARNING! Several files to convert to the same output filename! Use an output directory instead."<<endl;
return 1;
}
}
}
else
{
outputFileName = getOutputFileName(inputFileName);
}
// Check dest algo
if (OptAlgo==NOT_DEFINED)
@ -729,6 +685,7 @@ int main(int argc, char **argv)
output.close();
}
}
return 0;
}