forked from KhronosGroup/KTX-Software
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathktx2ktx2.cpp
434 lines (380 loc) · 14.2 KB
/
ktx2ktx2.cpp
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
// -*- tab-width: 4; -*-
// vi: set sw=2 ts=4 sts=4 expandtab:
// Copyright 2019-2020 The Khronos Group, Inc.
// SPDX-License-Identifier: Apache-2.0
// To use, download from http://www.billbaxter.com/projects/imdebug/
// Put imdebug.dll in %SYSTEMROOT% (usually C:\WINDOWS), imdebug.h in
// ../../include, imdebug.lib in ../../build/msvs/<platform>/vs<ver> &
// add ..\imdebug.lib to the libraries list in the project properties.
#define IMAGE_DEBUG 0
#include "ktxapp.h"
#include <cstdlib>
#include <errno.h>
#include <iostream>
#include <sstream>
#include <vector>
#include <ktx.h>
#include "argparser.h"
#include "version.h"
#if (IMAGE_DEBUG) && defined(_DEBUG) && defined(_WIN32) && !defined(_WIN32_WCE)
# include "imdebug.h"
#elif defined(IMAGE_DEBUG) && IMAGE_DEBUG
# undef IMAGE_DEBUG
# define IMAGE_DEBUG 0
#endif
#if defined(_MSC_VER)
#define strncasecmp _strnicmp
#define fileno _fileno
#define isatty _isatty
#endif
#if IMAGE_DEBUG
static void dumpImage(char* name, int width, int height, int components,
int componentSize, bool isLuminance,
unsigned char* srcImage);
#endif
using namespace std;
/** @page ktx2ktx2 ktx2ktx2
@~English
Create a KTX 2 file from a KTX file.
@section ktx2ktx2_synopsis SYNOPSIS
ktx2ktx2 [options] [@e infile ...]
@section ktx2ktx2_description DESCRIPTION
@b ktx2ktx2 creates Khronos texture format version 2 files (KTX2) from
Khronos texture format version 1 files. @b ktx2ktx2 reads each named
@e infile. Output files have the same name as the input but with the
extension changed to @c .ktx2. When @b infile is not specified, a single
file will be read from stdin and the output written to standard out.
If unrecognized metadata with keys beginning "KTX" or "ktx" is found in
the input file, it is dropped and a warning is written to standard error.
The following options are available:
<dl>
<dt>-b, \--rewritebado</dt>
<dd>Rewrite bad orientation metadata. Some in-the-wild KTX files
have orientation metadata with the key "KTXOrientation"
instead of KTXorientaion. This option will rewrite such
bad metadata instead of dropping it.
<dt>-o outfile, --output=outfile</dt>
<dd>Name the output file @e outfile. If @e outfile is 'stdout', output will
be written to stdout. If there is more than 1 input file, the command
prints its usage message and exits.</dd>
<dt>-f, \--force</dt>
<dd>If the destination file already exists, remove it and create a
new file, without prompting for confirmation regardless of its
permissions.</dd>
</dl>
@snippet{doc} ktxapp.h ktxApp options
@section ktx2ktx2_exitstatus EXIT STATUS
@b ktx2ktx2 exits 0 on success, 1 on command line errors and 2 on
functional errors.
@section ktx2ktx2_history HISTORY
@par Version 4.0
- Initial version.
@section ktx2ktx2_author AUTHOR
Mark Callow, Edgewise Consulting www.edgewise-consulting.com
*/
#define QUOTE(x) #x
#define STR(x) QUOTE(x)
std::string myversion(STR(KTX2KTX2_VERSION));
std::string mydefversion(STR(KTX2KTX2_DEFAULT_VERSION));
class ktxUpgrader : public ktxApp {
public:
ktxUpgrader();
virtual int main(int argc, char* argv[]);
virtual void usage();
protected:
virtual bool processOption(argparser& parser, int opt);
void validateOptions();
struct commandOptions : public ktxApp::commandOptions {
bool useStdout;
bool force;
bool rewriteBadOrientation;
commandOptions() {
useStdout = false;
force = false;
rewriteBadOrientation = false;
}
} options;
};
ktxUpgrader::ktxUpgrader() : ktxApp(myversion, mydefversion, options)
{
argparser::option my_option_list[] = {
{ "force", argparser::option::no_argument, NULL, 'f' },
{ "outfile", argparser::option::required_argument, NULL, 'o' },
{ "rewritebado", argparser::option::no_argument, NULL, 'b' },
};
const int lastOptionIndex = sizeof(my_option_list)
/ sizeof(argparser::option);
option_list.insert(option_list.begin(), my_option_list,
my_option_list + lastOptionIndex);
short_opts += "bd:fo:";
}
void
ktxUpgrader::usage()
{
cerr <<
"Usage: " << name << " [options] [<infile> ...]\n"
"\n"
" infile The source ktx file. The output is written to a file of the\n"
" same name with the extension changed to '.ktx2'. If it is not\n"
" specified input will be read from stdin and the converted texture\n"
" written to stdout.\n"
"\n"
" Options are:\n"
"\n"
" -b, --rewritebado\n"
" Rewrite bad orientation metadata. Some in-the-wild KTX files\n"
" have orientation metadata with the key \"KTXOrientation\"\n"
" instead of \"KTXorientaion\". This option will rewrite such\n"
" bad metadata instead of dropping it.\n"
" -o outfile, --output=outfile\n"
" Name the output file outfile. If @e outfile is 'stdout', output\n"
" will be written to stdout. If there is more than 1 infile,\n"
" the command prints its usage message and exits.\n"
" -f, --force If the output file already exists, remove it and create a\n"
" new file, without prompting for confirmation regardless of\n"
" its permissions.\n";
ktxApp::usage();
}
static ktxUpgrader ktx2ktx2;
ktxApp& theApp = ktx2ktx2;
int
ktxUpgrader::main(int argc, char* argv[])
{
FILE *inf, *outf = nullptr;
KTX_error_code result;
ktxTexture1* texture = 0;
int exitCode = 0;
processCommandLine(argc, argv);
validateOptions();
std::vector<string>::const_iterator it;
for (it = options.infiles.begin(); it < options.infiles.end(); it++) {
string infile = *it;
string outfile;
if (infile.compare("-") == 0) {
inf = stdin;
#if defined(_WIN32)
/* Set "stdin" to have binary mode */
(void)_setmode( _fileno( stdin ), _O_BINARY );
#endif
} else {
inf = fopenUTF8(infile, "rb");
}
if (inf) {
if (infile.compare("-")
&& !options.useStdout && !options.outfile.length())
{
size_t dot;
outfile = infile;
dot = outfile.find_last_of('.');
if (dot !=string::npos) {
outfile.erase(dot,string::npos);
}
outfile += ".ktx2";
} else if (options.outfile.length()) {
outfile = options.outfile;
}
if (options.useStdout) {
outf = stdout;
#if defined(_WIN32)
/* Set "stdout" to have binary mode */
(void)_setmode( _fileno( stdout ), _O_BINARY );
#endif
} else if (outfile.length()) {
outf = fopen_write_if_not_exists(outfile);
}
if (!outf && errno == EEXIST) {
bool force = options.force;
if (!force) {
if (isatty(fileno(stdin))) {
char answer;
cout << "Output file " << options.outfile
<< " exists. Overwrite? [Y or n] ";
cin >> answer;
if (answer == 'Y') {
force = true;
}
}
}
if (force) {
outf = fopenUTF8(outfile, "wb");
}
}
if (outf) {
result = ktxTexture1_CreateFromStdioStream(inf,
KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT,
&texture);
if (result != KTX_SUCCESS) {
if (result == KTX_UNKNOWN_FILE_FORMAT) {
cerr << infile << " is not a KTX v1 file." << endl;
} else if (result != KTX_SUCCESS) {
cerr << name
<< " failed to create ktxTexture from " << infile
<< ": " << ktxErrorString(result) << endl;
}
(void)fclose(inf);
(void)fclose(outf);
(void)unlinkUTF8(options.outfile.c_str());
exitCode = 2;
goto cleanup;
}
// Some in-the-wild KTX files have incorrect KTXOrientation
// Warn about dropping invalid metadata.
ktxHashListEntry* pEntry;
for (pEntry = texture->kvDataHead;
pEntry != NULL;
pEntry = ktxHashList_Next(pEntry)) {
unsigned int keyLen;
char* key;
ktxHashListEntry_GetKey(pEntry, &keyLen, &key);
if (strncasecmp(key, "KTX", 3) == 0) {
if (strcmp(key, KTX_ORIENTATION_KEY)
&& strcmp(key, KTX_WRITER_KEY)) {
if (strcmp(key, "KTXOrientation") == 0
&& options.rewriteBadOrientation) {
unsigned int orientLen;
char* orientation;
ktxHashListEntry_GetValue(pEntry,
&orientLen,
(void**)&orientation);
ktxHashList_AddKVPair(&texture->kvDataHead,
KTX_ORIENTATION_KEY,
orientLen,
orientation);
} else {
cerr << name
<< ": Warning: Dropping unrecognized "
<< "metadata \"" << key << "\""
<< std::endl;
}
ktxHashList_DeleteEntry(&texture->kvDataHead,
pEntry);
}
}
}
// Add required writer metadata.
std::stringstream writer;
writeId(writer, options.test != 0);
ktxHashList_AddKVPair(&texture->kvDataHead, KTX_WRITER_KEY,
(ktx_uint32_t)writer.str().length() + 1,
writer.str().c_str());
result = ktxTexture1_WriteKTX2ToStdioStream(texture, outf);
ktxTexture_Destroy(ktxTexture(texture));
(void)fclose(inf);
(void)fclose(outf);
if (result != KTX_SUCCESS) {
cerr << name
<< " failed to write KTX2 file; "
<< ktxErrorString(result) << endl;
(void)unlinkUTF8(options.outfile.c_str());
exitCode = 2;
goto cleanup;
}
} else {
cerr << name
<< " could not open output file \""
<< (options.outfile.length() ? options.outfile.c_str()
: "stdout")
<< "\". " << strerror(errno) << endl;
exitCode = 2;
goto cleanup;
}
} else {
cerr << name
<< " could not open input file \""
<< (infile.compare("-") ? infile : "stdin") << "\". "
<< strerror(errno) << endl;
exitCode = 2;
goto cleanup;
}
}
cleanup:
return exitCode;
}
void
ktxUpgrader::validateOptions()
{
if (options.infiles.size() > 1 && options.outfile.length()) {
cerr << "Can't use -o when there are multiple infiles." << endl;
usage();
exit(1);
}
}
/*
* @brief process a command line option
*
* @return true if option processed, false otherwise.
*
* @param[in] parser, an @c argparser holding the options to process.
* @param[in,out] options commandOptions struct in which option information
* is set.
*/
bool
ktxUpgrader::processOption(argparser& parser, int opt)
{
switch (opt) {
case 'b':
options.rewriteBadOrientation = true;
break;
case 'f':
options.force = true;
break;
case 'o':
options.outfile = parser.optarg;
if (!options.outfile.compare("stdout")) {
options.useStdout = true;
} else {
size_t dot;
dot = options.outfile.find_last_of('.');
if (dot ==string::npos) {
options.outfile += ".ktx2";
}
}
break;
default:
return false;
}
return true;
}
#if IMAGE_DEBUG
static void
dumpImage(char* name, int width, int height, int components, int componentSize,
bool isLuminance, unsigned char* srcImage)
{
char formatstr[2048];
char *imagefmt;
char *fmtname;
int bitsPerComponent = componentSize == 2 ? 16 : 8;
switch (components) {
case 1:
if (isLuminance) {
imagefmt = "lum b=";
fmtname = "LUMINANCE";
} else {
imagefmt = "a b=";
fmtname = "ALPHA";
}
break;
case 2:
imagefmt = "luma b=";
fmtname = "LUMINANCE_ALPHA";
break;
case 3:
imagefmt = "rgb b=";
fmtname = "RGB";
break;
case 4:
imagefmt = "rgba b=";
fmtname = "RGBA";
break;
default:
assert(0);
}
sprintf(formatstr, "%s%d w=%%d h=%%d t=\'%s %s%d\' %%p",
imagefmt,
bitsPerComponent,
name,
fmtname,
bitsPerComponent);
imdebug(formatstr, width, height, srcImage);
}
#endif