forked from KhronosGroup/KTX-Software
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathktx_main.cpp
More file actions
247 lines (209 loc) · 8.74 KB
/
ktx_main.cpp
File metadata and controls
247 lines (209 loc) · 8.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Copyright 2022-2023 The Khronos Group Inc.
// Copyright 2022-2023 RasterGrid Kft.
// Copyright 2026 Binomial LLC: basisu v2.10 integration. Disabling -validation command completely for this fork.
// SPDX-License-Identifier: Apache-2.0
#include "command.h"
#include "platform_utils.h"
#include "stdafx.h"
#include <iostream>
#include <string>
#include <unordered_map>
#include <cxxopts.hpp>
#include <fmt/ostream.h>
#include <fmt/printf.h>
// -------------------------------------------------------------------------------------------------
namespace ktx {
/** @page ktx ktx
@~English
Unified CLI frontend for the KTX-Software library.
@section ktx_synopsis SYNOPSIS
ktx <command> [command-option...]<br>
ktx [option...]
@section ktx_description DESCRIPTION
Unified CLI frontend for the KTX-Software library with sub-commands for specific operations
for the KTX File Format Specification
https://registry.khronos.org/KTX/specs/2.0/ktxspec.v2.html.
The following commands are available:
<dl>
<dt>@ref ktx_convert "convert"</dt>
<dd>
Convert a file in another texture format to a KTX2 file
</dd>
<dt>@ref ktx_create "create"</dt>
<dd>
Create a KTX2 file from various input files.
</dd>
<dt>@ref ktx_deflate "deflate"</dt>
<dd>
Deflate (supercompress) a KTX2 file.
</dd>
<dt>@ref ktx_encode "encode"</dt>
<dd>
Encode a KTX2 file.
</dd>
<dt>@ref ktx_extract "extract"</dt>
<dd>
Extract selected images from a KTX2 file.
</dd>
<dt>@ref ktx_transcode "transcode"</dt>
<dd>
Transcode a KTX2 file.
</dd>
<dt>@ref ktx_info "info"</dt>
<dd>
Print information about a KTX2 file.
</dd>
<dt>@ref ktx_validate "validate"</dt>
<dd>
Validate a KTX2 file.
</dd>
<dt>@ref ktx_compare "compare"</dt>
<dd>
Compare two KTX2 files.
</dd>
<dt>@ref ktx_help "help"</dt>
<dd>
Display help information about the ktx tool.
</dd>
</dl>
@section ktx\_main\_options OPTIONS
The following options are also available without a command:
@snippet{doc} ktx/command.h command options_generic
@section ktx_exitstatus EXIT STATUS
@snippet{doc} ktx/command.h command exitstatus
@section ktx_history HISTORY
@par Version 4.0
- Initial version
@section ktx_author AUTHOR
- Mátyás Császár [Vader], RasterGrid www.rastergrid.com
- Daniel Rákos, RasterGrid www.rastergrid.com
*/
class Tools : public Command {
bool testrun = false; /// Indicates test run. If enabled ktx tools will only include the default version information in any output
public:
using Command::Command;
virtual ~Tools() {};
public:
virtual int main(int argc, char* argv[]) override;
void printUsage(std::ostream& os, const cxxopts::Options& options);
};
// -------------------------------------------------------------------------------------------------
int Tools::main(int argc, char* argv[]) {
cxxopts::Options options("btx", ""); // Richard Geldreich, Binomial LLC 3/8/2026
options.custom_help("[<command>] [OPTION...]");
options.set_width(CONSOLE_USAGE_WIDTH);
options.add_options()
("h,help", "Print this usage message and exit")
("v,version", "Print the version number of this program and exit")
("testrun", "Indicates test run. If enabled the tool will produce deterministic output whenever possible");
options.allow_unrecognised_options();
cxxopts::ParseResult args;
try {
args = options.parse(argc, argv);
} catch (const std::exception& ex) {
fmt::print(std::cerr, "{}: {}\n", options.program(), ex.what());
printUsage(std::cerr, options);
return +rc::INVALID_ARGUMENTS;
}
testrun = args["testrun"].as<bool>();
if (args.count("help")) {
fmt::print(std::cout, "{}: Unified CLI frontend for the KTX-Software [BINOMIAL FORK] library with sub-commands for specific operations.\n", options.program()); // change made by Richard Geldreich, Binomial LLC, 3/8/2026
printUsage(std::cout, options);
return +rc::SUCCESS;
}
if (args.count("version")) {
fmt::print("{} version: {}\n", options.program(), version(testrun));
return +rc::SUCCESS;
}
if (args.unmatched().empty()) {
fmt::print(std::cerr, "{}: Missing command.\n", options.program());
printUsage(std::cerr, options);
} else {
if (argv[1][0] != '-') {
fmt::print(std::cerr, "{}: Unrecognized command: \"{}\"\n", options.program(), argv[1]);
} else {
fmt::print(std::cerr, "{}: Unrecognized argument: \"{}\"\n", options.program(), args.unmatched()[0]);
}
printUsage(std::cerr, options);
}
return +rc::INVALID_ARGUMENTS;
}
void Tools::printUsage(std::ostream& os, const cxxopts::Options& options) {
fmt::print(os, "{}", options.help());
fmt::print(os, "\n");
fmt::print(os, "Available commands:\n");
fmt::print(os, " convert Convert another texture file type to a KTX2 file\n");
fmt::print(os, " create Create a KTX2 file from various input files\n");
fmt::print(os, " deflate Deflate (supercompress) a KTX2 file\n");
fmt::print(os, " encode Encode a KTX2 file\n");
fmt::print(os, " extract Extract selected images from a KTX2 file\n");
fmt::print(os, " transcode Transcode a KTX2 file\n");
fmt::print(os, " info Print information about a KTX2 file\n");
//fmt::print(os, " check Checking a KTX2 file\n");
fmt::print(os, " compare Compare two KTX2 files\n");
fmt::print(os, " help Display help information about the btx tool\n");
#if KTX_DEVELOPER_FEATURE_PATCH
fmt::print(os, " patch Apply certain patch operations to a KTX2 file.");
#endif
fmt::print(os, "\n");
fmt::print(os, "For detailed usage and description of each subcommand use 'btx help <command>'\n"
"or 'btx <command> --help'\n"); // modified by Richard Geldreich, Binomial LLC, 3/8/2026
}
} // namespace ktx ---------------------------------------------------------------------------------
KTX_COMMAND_BUILTIN(ktxConvert)
KTX_COMMAND_BUILTIN(ktxCreate)
KTX_COMMAND_BUILTIN(ktxDeflate)
KTX_COMMAND_BUILTIN(ktxEncode)
KTX_COMMAND_BUILTIN(ktxExtract)
KTX_COMMAND_BUILTIN(ktxTranscode)
KTX_COMMAND_BUILTIN(ktxInfo)
KTX_COMMAND_BUILTIN(ktxValidate)
KTX_COMMAND_BUILTIN(ktxCompare)
KTX_COMMAND_BUILTIN(ktxHelp)
#if KTX_DEVELOPER_FEATURE_PATCH
KTX_COMMAND_BUILTIN(ktxPatch)
#endif
std::unordered_map<std::string, ktx::pfnBuiltinCommand> builtinCommands = {
{ "convert", ktxConvert },
{ "create", ktxCreate },
{ "deflate", ktxDeflate },
{ "encode", ktxEncode },
{ "extract", ktxExtract },
{ "transcode", ktxTranscode },
{ "info", ktxInfo },
{ "check", ktxValidate },
{ "compare", ktxCompare },
{ "help", ktxHelp },
#if KTX_DEVELOPER_FEATURE_PATCH
{ "patch", ktxPatch }
#endif
};
int main(int argc, char* argv[]) {
// If -NSDocumentRevisionsDebugMode YES ever causes any problem it should be discarded here
// by creating a new argc and argv pair and excluding the problematic arguments from them.
// This way downstream tools will not have to deal with this issue
// // -NSDocumentRevisionsDebugMode YES is appended to the end
// // of the command by Xcode when debugging and "Allow debugging when
// // using document Versions Browser" is checked in the scheme. It
// // defaults to checked and is saved in a user-specific file not the
// // pbxproj file, so it can't be disabled in a generated project.
// // Remove these from the arguments under consideration.
printf("btx v0.10 [BINOMIAL FORK].\n");
InitUTF8CLI(argc, argv);
if (argc >= 2) {
// Has a subcommand, attempt to lookup
const auto it = builtinCommands.find(argv[1]);
if (it != builtinCommands.end()) {
// Call built-in subcommand, trimming the first parameter.
return it->second(argc - 1, argv + 1);
} else {
// In the future it is possible to add further logic here to allow loading command plugins
// from shared libraries or call external commands. There is no defined configuration
// mechanism to do so, but the command framework has been designed to be able to build
// subcommands as separate executables or shared libraries.
}
}
// If no sub-command was specified or if it was not found, call the main command's entry point.
ktx::Tools cmd;
return cmd.main(argc, argv);
}