Skip to content

Commit 9dbc6ba

Browse files
committed
Convert texconv to use GNU-style long options
1 parent cea630a commit 9dbc6ba

File tree

1 file changed

+160
-94
lines changed

1 file changed

+160
-94
lines changed

Texconv/texconv.cpp

+160-94
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,9 @@ namespace
166166
OPT_USE_XBOX,
167167
OPT_XGMODE,
168168
#endif
169-
OPT_MAX
169+
OPT_FLAGS_MAX,
170+
OPT_VERSION,
171+
OPT_HELP,
170172
};
171173

172174
enum
@@ -187,7 +189,7 @@ namespace
187189
FORMAT_DXT5_RXGB,
188190
};
189191

190-
static_assert(OPT_MAX <= 64, "dwOptions is a unsigned int bitfield");
192+
static_assert(OPT_FLAGS_MAX <= 64, "dwOptions is a unsigned int bitfield");
191193

192194
const SValue<uint64_t> g_pOptions[] =
193195
{
@@ -210,20 +212,11 @@ namespace
210212
{ L"hflip", OPT_HFLIP },
211213
{ L"vflip", OPT_VFLIP },
212214
{ L"dword", OPT_DDS_DWORD_ALIGN },
213-
{ L"badtails", OPT_DDS_BAD_DXTN_TAILS },
214-
{ L"permissive", OPT_DDS_PERMISSIVE },
215-
{ L"ignoremips", OPT_DDS_IGNORE_MIPS },
216215
{ L"dx10", OPT_USE_DX10 },
217216
{ L"dx9", OPT_USE_DX9 },
218-
{ L"tga20", OPT_TGA20 },
219-
{ L"tgazeroalpha", OPT_TGAZEROALPHA },
220217
{ L"wicq", OPT_WIC_QUALITY },
221-
{ L"wiclossless", OPT_WIC_LOSSLESS },
222-
{ L"wicmulti", OPT_WIC_MULTIFRAME },
223218
{ L"nologo", OPT_NOLOGO },
224-
{ L"timing", OPT_TIMING },
225219
{ L"sepalpha", OPT_SEPALPHA },
226-
{ L"keepcoverage", OPT_PRESERVE_ALPHA_COVERAGE },
227220
{ L"nowic", OPT_NO_WIC },
228221
{ L"tu", OPT_TYPELESS_UNORM },
229222
{ L"tf", OPT_TYPELESS_FLOAT },
@@ -232,7 +225,6 @@ namespace
232225
{ L"xlum", OPT_EXPAND_LUMINANCE },
233226
{ L"wrap", OPT_TA_WRAP },
234227
{ L"mirror", OPT_TA_MIRROR },
235-
{ L"singleproc", OPT_FORCE_SINGLEPROC },
236228
{ L"gpu", OPT_GPU },
237229
{ L"nogpu", OPT_NOGPU },
238230
{ L"fl", OPT_FEATURE_LEVEL },
@@ -243,21 +235,66 @@ namespace
243235
{ L"nmapamp", OPT_NORMAL_MAP_AMPLITUDE },
244236
{ L"bc", OPT_BC_COMPRESS },
245237
{ L"c", OPT_COLORKEY },
246-
{ L"tonemap", OPT_TONEMAP },
247238
{ L"x2bias", OPT_X2_BIAS },
248-
{ L"inverty", OPT_INVERT_Y },
249-
{ L"reconstructz", OPT_RECONSTRUCT_Z },
250-
{ L"rotatecolor", OPT_ROTATE_COLOR },
251239
{ L"nits", OPT_PAPER_WHITE_NITS },
252-
{ L"fixbc4x4", OPT_BCNONMULT4FIX },
253-
{ L"swizzle", OPT_SWIZZLE },
254240
#ifdef USE_XBOX_EXTS
255241
{ L"xbox", OPT_USE_XBOX },
256242
{ L"xgmode", OPT_XGMODE },
257243
#endif
258244
{ nullptr, 0 }
259245
};
260246

247+
const SValue<uint64_t> g_pOptionsLong[] =
248+
{
249+
{ L"alpha-threshold", OPT_ALPHA_THRESHOLD },
250+
{ L"alpha-weight", OPT_ALPHA_WEIGHT },
251+
{ L"bad-tails", OPT_DDS_BAD_DXTN_TAILS },
252+
{ L"block-compress", OPT_BC_COMPRESS },
253+
{ L"color-key", OPT_COLORKEY },
254+
{ L"expand-luminance", OPT_EXPAND_LUMINANCE },
255+
{ L"feature-level", OPT_FEATURE_LEVEL },
256+
{ L"file-list", OPT_FILELIST },
257+
{ L"file-type", OPT_FILETYPE },
258+
{ L"fit-power-of-2", OPT_FIT_POWEROF2 },
259+
{ L"fix-bc-4x4", OPT_BCNONMULT4FIX },
260+
{ L"format", OPT_FORMAT },
261+
{ L"height", OPT_HEIGHT },
262+
{ L"help", OPT_HELP },
263+
{ L"horizontal-flip", OPT_HFLIP },
264+
{ L"ignore-mips", OPT_DDS_IGNORE_MIPS },
265+
{ L"image-filter", OPT_FILTER },
266+
{ L"invert-y", OPT_INVERT_Y },
267+
{ L"keep-coverage", OPT_PRESERVE_ALPHA_COVERAGE },
268+
{ L"mip-levels", OPT_MIPLEVELS },
269+
{ L"normalmap-amplitude", OPT_NORMAL_MAP_AMPLITUDE },
270+
{ L"normalmap", OPT_NORMAL_MAP },
271+
{ L"overwrite", OPT_OVERWRITE },
272+
{ L"paper-white-nits", OPT_PAPER_WHITE_NITS },
273+
{ L"permissive", OPT_DDS_PERMISSIVE },
274+
{ L"prefix", OPT_PREFIX },
275+
{ L"premultiplied-alpha", OPT_PREMUL_ALPHA },
276+
{ L"reconstruct-z", OPT_RECONSTRUCT_Z },
277+
{ L"rotate-color", OPT_ROTATE_COLOR },
278+
{ L"separate-alpha", OPT_SEPALPHA },
279+
{ L"single-proc", OPT_FORCE_SINGLEPROC },
280+
{ L"srgb-in", OPT_SRGBI },
281+
{ L"srgb-out", OPT_SRGBO },
282+
{ L"suffix", OPT_SUFFIX },
283+
{ L"swizzle", OPT_SWIZZLE },
284+
{ L"tga-zero-alpha", OPT_TGAZEROALPHA },
285+
{ L"tga20", OPT_TGA20 },
286+
{ L"timing", OPT_TIMING },
287+
{ L"to-lowercase", OPT_TOLOWER },
288+
{ L"tonemap", OPT_TONEMAP },
289+
{ L"version", OPT_VERSION },
290+
{ L"vertical-flip", OPT_VFLIP },
291+
{ L"wic-lossless", OPT_WIC_LOSSLESS },
292+
{ L"wic-multiframe", OPT_WIC_MULTIFRAME },
293+
{ L"wic-quality", OPT_WIC_QUALITY },
294+
{ L"width", OPT_WIDTH },
295+
{ nullptr, 0 }
296+
};
297+
261298
#define DEFFMT(fmt) { L## #fmt, DXGI_FORMAT_ ## fmt }
262299

263300
const SValue<DXGI_FORMAT> g_pFormats[] =
@@ -693,53 +730,58 @@ namespace
693730
L" -r wildcard filename search is recursive\n"
694731
L" -r:flatten flatten the directory structure (default)\n"
695732
L" -r:keep keep the directory structure\n"
696-
L" -flist <filename> use text file with a list of input files (one per line)\n"
733+
L" -flist <filename>, --file-list <filename>\n"
734+
L" use text file with a list of input files (one per line)\n"
697735
L"\n"
698-
L" -w <n> width\n"
699-
L" -h <n> height\n"
700-
L" -m <n> miplevels\n"
701-
L" -f <format> format\n"
736+
L" -w <n>, --width <n> width for output\n"
737+
L" -h <n>, --height <n> height for output\n"
738+
L" -m <n>, --mip-levels <n> miplevels for output\n"
739+
L" -f <format>, --format <format> pixel format for output\n"
702740
L"\n"
703-
L" -if <filter> image filtering\n"
704-
L" -srgb{i|o} sRGB {input, output}\n"
741+
L" -if <filter>, --image-filter <filter> image filtering\n"
742+
L" -srgb{i|o}, --srgb-in, --srgb-out sRGB {input, output}\n"
705743
L"\n"
706-
L" -px <string> name prefix\n"
707-
L" -sx <string> name suffix\n"
708-
L" -o <directory> output directory\n"
709-
L" -l force output filename to lower case\n"
710-
L" -y overwrite existing output file (if any)\n"
711-
L" -ft <filetype> output file type\n"
744+
L" -px <string>, --prefix <string> name prefix\n"
745+
L" -sx <string>, --suffix <string> name suffix\n"
746+
L" -o <directory> output directory\n"
747+
L" -l, --to-lowercase force output filename to lower case\n"
748+
L" -y, --overwrite overwrite existing output file (if any)\n"
749+
L" -ft <filetype>, --file-type <filetype> output file type\n"
712750
L"\n"
713-
L" -hflip horizonal flip of source image\n"
714-
L" -vflip vertical flip of source image\n"
751+
L" -hflip, --horizontal-flip horizonal flip of source image\n"
752+
L" -vflip, --vertical-flip vertical flip of source image\n"
715753
L"\n"
716-
L" -sepalpha resize/generate mips alpha channel separately\n"
717-
L" from color channels\n"
718-
L" -keepcoverage <ref> Preserve alpha coverage in mips for alpha test ref\n"
754+
L" -sepalpha, --separate-alpha resize/generate mips alpha channel separately from color channels\n"
755+
L" --keep-coverage <ref> Preserve alpha coverage in mips for alpha test ref\n"
719756
L"\n"
720-
L" -nowic Force non-WIC filtering\n"
721-
L" -wrap, -mirror texture addressing mode (wrap, mirror, or clamp)\n"
722-
L" -pmalpha convert final texture to use premultiplied alpha\n"
723-
L" -alpha convert premultiplied alpha to straight alpha\n"
724-
L" -at <threshold> Alpha threshold used for BC1, RGBA5551, and WIC\n"
725-
L" (defaults to 0.5)\n"
757+
L" -nowic Force non-WIC filtering\n"
758+
L" -wrap, -mirror texture addressing mode (wrap, mirror, or clamp)\n"
759+
L" -pmalpha, --premultiplied-alpha convert final texture to use premultiplied alpha\n"
760+
L" -alpha convert premultiplied alpha to straight alpha\n"
761+
L" -at <threshold>, --alpha-threshold <threshold>\n"
762+
L" Alpha threshold used for BC1, RGBA5551, and WIC\n"
763+
L" (defaults to 0.5)\n"
726764
L"\n"
727-
L" -fl <feature-level> Set maximum feature level target (defaults to 11.0)\n"
728-
L" -pow2 resize to fit a power-of-2, respecting aspect ratio\n"
765+
L" -fl <feature-level>, --feature-level <feature-level>\n"
766+
L" Set maximum feature level target (defaults to 11.0)\n"
767+
L" -pow2, --fit-power-of-2\n"
768+
L" resize to fit a power-of-2, respecting aspect ratio\n"
729769
L"\n"
730-
L" -nmap <options> converts height-map to normal-map\n"
770+
L" -nmap <options>, --normal-map <options>\n"
771+
L" converts height-map to normal-map\n"
731772
L" options must be one or more of\n"
732773
L" r, g, b, a, l, m, u, v, i, o\n"
733-
L" -nmapamp <weight> normal map amplitude (defaults to 1.0)\n"
774+
L" -nmapamp <weight>, --normalmap-amplitude <weight>\n"
775+
L" normal map amplitude (defaults to 1.0)\n"
734776
L"\n"
735-
L" (DDS input only)\n"
736-
L" -t{u|f} TYPELESS format is treated as UNORM or FLOAT\n"
737-
L" -dword Use DWORD instead of BYTE alignment\n"
738-
L" -badtails Fix for older DXTn with bad mipchain tails\n"
739-
L" -permissive Allow some DX9 variants with unusual header values\n"
740-
L" -ignoremips Reads just the top-level mip which reads some invalid files\n"
741-
L" -fixbc4x4 Fix for odd-sized BC files that Direct3D can't load\n"
742-
L" -xlum expand legacy L8, L16, and A8P8 formats\n"
777+
L" (DDS input only)\n"
778+
L" -t{u|f} TYPELESS format is treated as UNORM or FLOAT\n"
779+
L" -dword Use DWORD instead of BYTE alignment\n"
780+
L" --bad-tails Fix for older DXTn with bad mipchain tails\n"
781+
L" --permissive Allow some DX9 variants with unusual header values\n"
782+
L" --ignore-mips Reads just the top-level mip which reads some invalid files\n"
783+
L" --fix-bc-4x4 Fix for odd-sized BC files that Direct3D can't load\n"
784+
L" -xlum, --expand-luminance Expand legacy L8, L16, and A8P8 formats\n"
743785
L"\n"
744786
L" (DDS output only)\n"
745787
L" -dx10 Force use of 'DX10' extended header\n"
@@ -750,39 +792,44 @@ namespace
750792
#endif
751793
L"\n"
752794
L" (TGA input only)\n"
753-
L" -tgazeroalpha Allow all zero alpha channel files to be loaded 'as is'\n"
795+
L" --tga-zero-alpha Allow all zero alpha channel files to be loaded 'as is'\n"
754796
L"\n"
755797
L" (TGA output only)\n"
756-
L" -tga20 Write file including TGA 2.0 extension area\n"
798+
L" --tga20 Write file including TGA 2.0 extension area\n"
757799
L"\n"
758800
L" (BMP, PNG, JPG, TIF, WDP output only)\n"
759-
L" -wicq <quality> When writing images with WIC use quality (0.0 to 1.0)\n"
760-
L" -wiclossless When writing images with WIC use lossless mode\n"
761-
L" -wicmulti When writing images with WIC encode multiframe images\n"
801+
L" -wicq <quality>, --wic-quality <quality>\n"
802+
L" When writing images with WIC use quality (0.0 to 1.0)\n"
803+
L" --wic-lossless When writing images with WIC use lossless mode\n"
804+
L" --wic-multiframe When writing images with WIC encode multiframe images\n"
762805
L"\n"
763806
L" -nologo suppress copyright message\n"
764-
L" -timing Display elapsed processing time\n"
807+
L" --timing display elapsed processing time\n"
765808
L"\n"
766809
#ifdef _OPENMP
767-
L" -singleproc Do not use multi-threaded compression\n"
810+
L" --single-proc Do not use multi-threaded compression\n"
768811
#endif
769812
L" -gpu <adapter> Select GPU for DirectCompute-based codecs (0 is default)\n"
770813
L" -nogpu Do not use DirectCompute-based codecs\n"
771814
L"\n"
772-
L" -bc <options> Sets options for BC compression\n"
815+
L" -bc <options>, --block-compress <options>\n"
816+
L" Sets options for BC compression\n"
773817
L" options must be one or more of\n"
774818
L" d, u, q, x\n"
775-
L" -aw <weight> BC7 GPU compressor weighting for alpha error metric\n"
819+
L" -aw <weight>, --alpha-weight <weight>\n"
820+
L" BC7 GPU compressor weighting for alpha error metric\n"
776821
L" (defaults to 1.0)\n"
777822
L"\n"
778-
L" -c <hex-RGB> colorkey (a.k.a. chromakey) transparency\n"
779-
L" -rotatecolor <rot> rotates color primaries and/or applies a curve\n"
780-
L" -nits <value> paper-white value in nits to use for HDR10 (def: 200.0)\n"
781-
L" -tonemap Apply a tonemap operator based on maximum luminance\n"
823+
L" -c <hex-RGB>, --color-key <hex-RGB> colorkey (a.k.a. chromakey) transparency\n"
824+
L" --rotate-color <rot> rotates color primaries and/or applies a curve\n"
825+
L"\n"
826+
L" -nits <value>, --paper-white-nits <value>\n"
827+
L" paper-white value in nits to use for HDR10 (def: 200.0)\n"
828+
L" --tonemap Apply a tonemap operator based on maximum luminance\n"
782829
L" -x2bias Enable *2 - 1 conversion cases for unorm/pos-only-float\n"
783-
L" -inverty Invert Y (i.e. green) channel values\n"
784-
L" -reconstructz Rebuild Z (blue) channel assuming X/Y are normals\n"
785-
L" -swizzle <rgba> Swizzle image channels using HLSL-style mask\n"
830+
L" --invert-y Invert Y (i.e. green) channel values\n"
831+
L" --reconstruct-z Rebuild Z (blue) channel assuming X/Y are normals\n"
832+
L" --swizzle <rgba> Swizzle image channels using HLSL-style mask\n"
786833
L"\n"
787834
L" '-- ' is needed if any input filepath starts with the '-' or '/' character\n";
788835

@@ -1218,46 +1265,65 @@ int __cdecl wmain(_In_ int argc, _In_z_count_(argc) wchar_t* argv[])
12181265
{
12191266
PWSTR pArg = argv[iArg];
12201267

1221-
if (allowOpts
1222-
&& ('-' == pArg[0]) && ('-' == pArg[1]))
1268+
if (allowOpts && (('-' == pArg[0]) || ('/' == pArg[0])))
12231269
{
1224-
if (pArg[2] == 0)
1225-
{
1226-
// "-- " is the POSIX standard for "end of options" marking to escape the '-' and '/' characters at the start of filepaths.
1227-
allowOpts = false;
1228-
}
1229-
else if (!_wcsicmp(pArg,L"--version"))
1270+
uint64_t dwOption = 0;
1271+
1272+
if (('-' == pArg[0]) && ('-' == pArg[1]))
12301273
{
1231-
PrintLogo(true, g_ToolName, g_Description);
1232-
return 0;
1274+
if (pArg[2] == 0)
1275+
{
1276+
// "-- " is the POSIX standard for "end of options" marking to escape the '-' and '/' characters at the start of filepaths.
1277+
allowOpts = false;
1278+
}
1279+
else
1280+
{
1281+
pArg += 2;
1282+
dwOption = LookupByName(pArg, g_pOptionsLong);
1283+
1284+
if (dwOption == OPT_VERSION)
1285+
{
1286+
PrintLogo(true, g_ToolName, g_Description);
1287+
return 0;
1288+
}
1289+
else if (dwOption == OPT_HELP)
1290+
{
1291+
PrintUsage();
1292+
return 0;
1293+
}
1294+
}
12331295
}
1234-
else if (!_wcsicmp(pArg, L"--help"))
1296+
else if (('-' == pArg[0]) || ('/' == pArg[0]))
12351297
{
1236-
PrintUsage();
1237-
return 0;
1298+
pArg++;
1299+
dwOption = LookupByName(pArg, g_pOptions);
1300+
1301+
if (!dwOption)
1302+
{
1303+
if (LookupByName(pArg, g_pOptionsLong))
1304+
{
1305+
wprintf(L"ERROR: did you mean `--%ls` (with two dashes)?\n", pArg);
1306+
return 1;
1307+
}
1308+
}
12381309
}
1239-
else
1310+
1311+
if (!dwOption)
12401312
{
1241-
wprintf(L"Unknown option: %ls\n", pArg);
1313+
wprintf(L"ERROR: Unknown option: `%ls`\n\nUse %ls --help\n", pArg, g_ToolName);
12421314
return 1;
12431315
}
1244-
}
1245-
else if (allowOpts
1246-
&& (('-' == pArg[0]) || ('/' == pArg[0])))
1247-
{
1248-
pArg++;
1316+
12491317
PWSTR pValue;
12501318

12511319
for (pValue = pArg; *pValue && (':' != *pValue); pValue++);
12521320

12531321
if (*pValue)
12541322
*pValue++ = 0;
12551323

1256-
const uint64_t dwOption = LookupByName(pArg, g_pOptions);
1257-
1258-
if (!dwOption || (dwOptions & (uint64_t(1) << dwOption)))
1324+
if (dwOptions & (uint64_t(1) << dwOption))
12591325
{
1260-
PrintUsage();
1326+
wprintf(L"ERROR: Duplicate option: `%ls`\n\n", pArg);
12611327
return 1;
12621328
}
12631329

0 commit comments

Comments
 (0)