forked from AcademySoftwareFoundation/OpenImageIO
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiconvert.cpp
More file actions
551 lines (496 loc) · 20.1 KB
/
Copy pathiconvert.cpp
File metadata and controls
551 lines (496 loc) · 20.1 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
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
// Copyright Contributors to the OpenImageIO project.
// SPDX-License-Identifier: Apache-2.0
// https://github.com/AcademySoftwareFoundation/OpenImageIO
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iterator>
#include <string>
#include <vector>
#include <OpenImageIO/argparse.h>
#include <OpenImageIO/filesystem.h>
#include <OpenImageIO/imagebuf.h>
#include <OpenImageIO/imagecache.h>
#include <OpenImageIO/imageio.h>
#include <OpenImageIO/strutil.h>
#include <OpenImageIO/sysutil.h>
using namespace OIIO;
static std::string uninitialized = "uninitialized \001 HHRU dfvAS: efjl";
static std::string dataformatname = "";
static float gammaval = 1.0f;
//static bool depth = false;
static bool verbose = false;
static int nthreads = 0; // default: use #cores threads if available
static std::vector<std::string> filenames;
static std::string out_filename;
static int tile[3] = { 0, 0, 1 };
static bool scanline = false;
//static bool zfile = false;
//static std::string channellist;
static std::string compression;
static bool no_copy_image = false;
static int quality = -1;
static bool adjust_time = false;
static std::string caption = uninitialized;
static std::vector<std::string> keywords;
static bool clear_keywords = false;
static std::vector<std::string> attribnames, attribvals;
static bool inplace = false;
static int orientation = 0;
static bool rotcw = false, rotccw = false, rot180 = false;
static bool sRGB = false;
static bool separate = false, contig = false;
static bool noclobber = false;
static int return_code = EXIT_SUCCESS;
static ArgParse ap;
static int
parse_files(int argc, const char* argv[])
{
for (int i = 0; i < argc; i++)
filenames.emplace_back(argv[i]);
return 0;
}
static void
getargs(int argc, char* argv[])
{
bool help = false;
// clang-format off
ap.options ("iconvert -- copy images with format conversions and other alterations\n"
OIIO_INTRO_STRING "\n"
"Usage: iconvert [options] inputfile outputfile\n"
" or: iconvert [options] inputfile -o outputfile\n"
" or: iconvert --inplace [options] file...\n",
"%*", parse_files, "",
"--help", &help, "Print help message",
"-v", &verbose, "Verbose status messages",
"-o %s:FILENAME", &out_filename, "Output filename",
"--threads %d:NTHREADS", &nthreads, "Number of threads (default 0 = #cores)",
"-d %s:TYPE", &dataformatname, "Set the output data format to one of:"
"uint8, sint8, uint10, uint12, uint16, sint16, half, float, double",
"-g %f:GAMMA", &gammaval, "Set gamma correction (default = 1.0)",
"--tile %d:WIDTH %d:HEIGHT", &tile[0], &tile[1], "Output as a tiled image",
"--scanline", &scanline, "Output as a scanline image",
"--compression %s:METHOD", &compression, "Set the compression method (default = same as input)."
" Note: may be in the form \"name:quality\"",
"--quality %d", &quality, "", // DEPRECATED(2.1)
"--no-copy-image", &no_copy_image, "Do not use ImageOutput copy_image functionality (dbg)",
"--adjust-time", &adjust_time, "Adjust file times to match DateTime metadata",
"--caption %s:TEXT", &caption, "Set caption (ImageDescription)",
"--keyword %L:NAME", &keywords, "Add a keyword",
"--clear-keywords", &clear_keywords, "Clear keywords",
"--attrib %L:NAME %L:VALUE", &attribnames, &attribvals, "Set a string attribute",
"--orientation %d:ORIENT", &orientation, "Set the orientation",
"--rotcw", &rotcw, "Rotate 90 deg clockwise",
"--rotccw", &rotccw, "Rotate 90 deg counter-clockwise",
"--rot180", &rot180, "Rotate 180 deg",
"--inplace", &inplace, "Do operations in place on images",
"--sRGB", &sRGB, "This file is in sRGB color space",
"--separate", &separate, "Force planarconfig separate",
"--contig", &contig, "Force planarconfig contig",
"--no-clobber", &noclobber, "Do not overwrite existing files",
//FIXME "-z", &zfile, "Treat input as a depth file",
//FIXME "-c %s", &channellist, "Restrict/shuffle channels",
nullptr);
// clang-format on
if (ap.parse(argc, (const char**)argv) < 0) {
OIIO::print(stderr, "{}\n", ap.geterror());
ap.usage();
ap.abort();
return_code = EXIT_FAILURE;
return;
}
if (help) {
ap.usage();
ap.abort();
return_code = EXIT_SUCCESS;
return;
}
if (!out_filename.empty() && filenames.size() == 1) {
// -o mode: one positional arg is input, -o specifies output
filenames.push_back(out_filename);
} else if (!out_filename.empty() && filenames.size() != 1) {
OIIO::print(stderr,
"iconvert: -o requires exactly one input filename.\n");
ap.usage();
ap.abort();
return_code = EXIT_FAILURE;
return;
}
if (filenames.size() != 2 && !inplace) {
OIIO::print(
stderr,
"iconvert: Must have both an input and output filename specified.\n");
ap.usage();
ap.abort();
return_code = EXIT_FAILURE;
return;
}
if (filenames.size() == 0 && inplace) {
OIIO::print(stderr, "iconvert: Must have at least one filename\n");
ap.usage();
ap.abort();
return_code = EXIT_FAILURE;
return;
}
if (((int)rotcw + (int)rotccw + (int)rot180 + (orientation > 0)) > 1) {
OIIO::print(
stderr,
"iconvert: more than one of --rotcw, --rotccw, --rot180, --orientation\n");
ap.usage();
ap.abort();
return_code = EXIT_FAILURE;
return;
}
}
static bool
DateTime_to_time_t(string_view datetime, time_t& timet)
{
int year, month, day, hour, min, sec;
if (!Strutil::scan_datetime(datetime, year, month, day, hour, min, sec))
return false;
// OIIO::print("{}:{}:{} {}:{}:{}\n", year, month, day, hour, min, sec);
struct tm tmtime;
time_t now = time(nullptr);
Sysutil::get_local_time(&now, &tmtime); // fill in defaults
tmtime.tm_sec = sec;
tmtime.tm_min = min;
tmtime.tm_hour = hour;
tmtime.tm_mday = day;
tmtime.tm_mon = month - 1;
tmtime.tm_year = year - 1900;
timet = mktime(&tmtime);
return true;
}
// Adjust the output spec based on the command-line arguments.
// Return whether the specifics preclude using copy_image.
static bool
adjust_spec(ImageInput* in, ImageOutput* out, const ImageSpec& inspec,
ImageSpec& outspec)
{
bool nocopy = no_copy_image;
// Copy the spec, with possible change in format
outspec.format = inspec.format;
if (inspec.channelformats.size()) {
// Input file has mixed channels
if (out->supports("channelformats")) {
// Output supports mixed formats -- so request it
outspec.format = TypeDesc::UNKNOWN;
} else {
// Input had mixed formats, output did not, so just use a fixed
// format and forget the per-channel formats for output.
outspec.channelformats.clear();
}
}
if (!dataformatname.empty()) {
// make sure there isn't a stray BPS that will screw us up
outspec.erase_attribute("oiio:BitsPerSample");
if (dataformatname == "uint8")
outspec.set_format(TypeDesc::UINT8);
else if (dataformatname == "int8")
outspec.set_format(TypeDesc::INT8);
else if (dataformatname == "uint10") {
outspec.attribute("oiio:BitsPerSample", 10);
outspec.set_format(TypeDesc::UINT16);
} else if (dataformatname == "uint12") {
outspec.attribute("oiio:BitsPerSample", 12);
outspec.set_format(TypeDesc::UINT16);
} else if (dataformatname == "uint16")
outspec.set_format(TypeDesc::UINT16);
else if (dataformatname == "int16")
outspec.set_format(TypeDesc::INT16);
else if (dataformatname == "uint32" || dataformatname == "uint")
outspec.set_format(TypeDesc::UINT32);
else if (dataformatname == "int32" || dataformatname == "int")
outspec.set_format(TypeDesc::INT32);
else if (dataformatname == "half")
outspec.set_format(TypeDesc::HALF);
else if (dataformatname == "float")
outspec.set_format(TypeDesc::FLOAT);
else if (dataformatname == "double")
outspec.set_format(TypeDesc::DOUBLE);
outspec.channelformats.clear();
}
if (outspec.format != inspec.format || inspec.channelformats.size())
nocopy = true;
if (outspec.nchannels != inspec.nchannels)
nocopy = true;
if (gammaval != 1.0f)
outspec.attribute("oiio:Gamma", gammaval);
if (sRGB) {
outspec.set_colorspace("srgb_rec709_scene");
if (!strcmp(in->format_name(), "jpeg")
|| outspec.find_attribute("Exif:ColorSpace"))
outspec.attribute("Exif:ColorSpace", 1);
}
if (tile[0]) {
outspec.tile_width = tile[0];
outspec.tile_height = tile[1];
outspec.tile_depth = tile[2];
}
if (scanline) {
outspec.tile_width = 0;
outspec.tile_height = 0;
outspec.tile_depth = 0;
}
if (outspec.tile_width != inspec.tile_width
|| outspec.tile_height != inspec.tile_height
|| outspec.tile_depth != inspec.tile_depth)
nocopy = true;
if (!compression.empty()) {
outspec.attribute("compression", compression);
if (compression != inspec.get_string_attribute("compression"))
nocopy = true;
}
if (quality > 0) {
outspec.attribute("CompressionQuality", quality);
if (quality != inspec.get_int_attribute("CompressionQuality"))
nocopy = true;
}
if (contig)
outspec.attribute("planarconfig", "contig");
if (separate)
outspec.attribute("planarconfig", "separate");
if (orientation >= 1)
outspec.attribute("Orientation", orientation);
else {
orientation = outspec.get_int_attribute("Orientation", 1);
if (orientation >= 1 && orientation <= 8) {
static int cw[] = { 0, 6, 7, 8, 5, 2, 3, 4, 1 };
if (rotcw || rotccw || rot180)
orientation = cw[orientation];
if (rotccw || rot180)
orientation = cw[orientation];
if (rotccw)
orientation = cw[orientation];
outspec.attribute("Orientation", orientation);
}
}
if (caption != uninitialized)
outspec.attribute("ImageDescription", caption);
if (clear_keywords)
outspec.attribute("Keywords", "");
if (keywords.size()) {
std::string oldkw = outspec.get_string_attribute("Keywords");
std::vector<std::string> oldkwlist;
if (!oldkw.empty()) {
Strutil::split(oldkw, oldkwlist, ";");
for (auto& kw : oldkwlist)
kw = Strutil::strip(kw);
}
for (auto&& nk : keywords) {
bool dup = false;
for (auto&& ok : oldkwlist)
dup |= (ok == nk);
if (!dup)
oldkwlist.push_back(nk);
}
outspec.attribute("Keywords", Strutil::join(oldkwlist, "; "));
}
for (size_t i = 0; i < attribnames.size(); ++i) {
outspec.attribute(attribnames[i].c_str(), attribvals[i].c_str());
}
return nocopy;
}
static bool
convert_file(const std::string& in_filename, const std::string& out_filename)
{
if (noclobber && Filesystem::exists(out_filename)) {
OIIO::print(stderr,
"iconvert ERROR: Output file already exists \"{}\"\n",
out_filename);
return false;
}
if (verbose) {
OIIO::print("Converting {} to {}\n", in_filename, out_filename);
fflush(stdout);
}
std::string tempname = out_filename;
if (tempname == in_filename) {
tempname = out_filename + ".tmp" + Filesystem::extension(out_filename);
}
// Find an ImageIO plugin that can open the input file, and open it.
auto in = ImageInput::open(in_filename);
if (!in) {
std::string err = geterror();
OIIO::print(stderr, "iconvert ERROR: {}\n",
(err.length()
? err
: Strutil::fmt::format("Could not open \"{}\"",
in_filename)));
return false;
}
ImageSpec inspec = in->spec();
std::string metadatatime = inspec.get_string_attribute("DateTime");
// Find an ImageIO plugin that can open the output file, and open it
auto out = ImageOutput::create(tempname);
if (!out) {
OIIO::print(
stderr,
"iconvert ERROR: Could not find an ImageIO plugin to write \"{}\": {}\n",
out_filename, geterror());
return false;
}
// In order to deal with formats that support subimages, but not
// subimage appending, we gather them all first.
std::vector<ImageSpec> subimagespecs;
if (out->supports("multiimage") && !out->supports("appendsubimage")) {
auto imagecache = ImageCache::create();
int nsubimages = 0;
ustring ufilename(in_filename);
imagecache->get_image_info(ufilename, 0, 0, ustring("subimages"),
TypeInt, &nsubimages);
if (nsubimages > 1) {
subimagespecs.resize(nsubimages);
for (int i = 0; i < nsubimages; ++i) {
ImageSpec inspec = *imagecache->imagespec(ufilename, i);
subimagespecs[i] = inspec;
adjust_spec(in.get(), out.get(), inspec, subimagespecs[i]);
}
}
}
bool ok = true;
bool mip_to_subimage_warning = false;
for (int subimage = 0; ok && in->seek_subimage(subimage, 0); ++subimage) {
if (subimage > 0 && !out->supports("multiimage")) {
OIIO::print(
stderr,
"iconvert WARNING: {} does not support multiple subimages.\n"
"\tOnly the first subimage has been copied.\n",
out->format_name());
break; // we're done
}
int miplevel = 0;
do {
// Copy the spec, with possible change in format
inspec = in->spec(subimage, miplevel);
ImageSpec outspec = inspec;
bool nocopy = adjust_spec(in.get(), out.get(), inspec, outspec);
if (miplevel > 0) {
// Moving to next MIP level
ImageOutput::OpenMode mode;
if (out->supports("mipmap"))
mode = ImageOutput::AppendMIPLevel;
else if (out->supports("multiimage")
&& out->supports("appendsubimage")) {
mode = ImageOutput::AppendSubimage; // use if we must
if (!mip_to_subimage_warning
&& strcmp(out->format_name(), "tiff")) {
OIIO::print(
stderr,
"iconvert WARNING: {} does not support MIPmaps.\n"
"\tStoring the MIPmap levels in subimages.\n",
out->format_name());
}
mip_to_subimage_warning = true;
} else {
OIIO::print(stderr,
"iconvert WARNING: {} does not support MIPmaps.\n"
"\tOnly the first level has been copied.\n",
out->format_name());
break; // on to the next subimage
}
ok = out->open(tempname.c_str(), outspec, mode);
} else if (subimage > 0) {
// Moving to next subimage
ok = out->open(tempname.c_str(), outspec,
ImageOutput::AppendSubimage);
} else {
// First time opening
if (subimagespecs.size())
ok = out->open(tempname.c_str(), int(subimagespecs.size()),
&subimagespecs[0]);
else
ok = out->open(tempname.c_str(), outspec,
ImageOutput::Create);
}
if (!ok) {
std::string err = out->geterror();
OIIO::print(stderr, "iconvert ERROR: {}\n",
(err.length()
? err
: Strutil::fmt::format("Could not open \"{}\"",
out_filename)));
ok = false;
break;
}
// Copy thumbnail, if there is one.
if (miplevel == 0 && in->supports("thumbnail")
&& out->supports("thumbnail")) {
ImageBuf thumb;
in->get_thumbnail(thumb, subimage);
if (thumb.initialized())
out->set_thumbnail(thumb);
}
if (in->spec().nchannels != out->spec().nchannels)
nocopy = true;
if (!nocopy) {
ok = out->copy_image(in.get());
if (!ok)
OIIO::print(
stderr,
"iconvert ERROR copying \"{}\" to \"{}\" :\n\t{}\n",
in_filename, out_filename, out->geterror());
} else {
// Need to do it by hand for some reason. Future expansion in which
// only a subset of channels are copied, or some such.
std::vector<char> pixels((size_t)outspec.image_bytes(true));
ok = in->read_image(subimage, miplevel, 0, outspec.nchannels,
outspec.format, &pixels[0]);
if (!ok) {
OIIO::print(stderr, "iconvert ERROR reading \"{}\": {}\n",
in_filename, in->geterror());
} else {
ok = out->write_image(outspec.format, &pixels[0]);
if (!ok)
OIIO::print(stderr,
"iconvert ERROR writing \"{}\": {}\n",
out_filename, out->geterror());
}
}
++miplevel;
} while (ok && in->seek_subimage(subimage, miplevel));
}
if (!out->close()) {
errorfmt("Error closing \"{}\" : {}", out_filename, out->geterror());
}
in->close();
// Figure out a time for the input file -- either one supplied by
// the metadata, or the actual time stamp of the input file.
std::time_t in_time;
if (metadatatime.empty()
|| !DateTime_to_time_t(metadatatime.c_str(), in_time))
in_time = Filesystem::last_write_time(in_filename);
if (out_filename != tempname) {
if (ok) {
Filesystem::remove(out_filename);
Filesystem::rename(tempname, out_filename);
} else
Filesystem::remove(tempname);
}
// If user requested, try to adjust the file's modification time to
// the creation time indicated by the file's DateTime metadata.
if (ok && adjust_time)
Filesystem::last_write_time(out_filename, in_time);
return ok;
}
int
main(int argc, char* argv[])
{
// Helpful for debugging to make sure that any crashes dump a stack
// trace.
Sysutil::setup_crash_stacktrace("stdout");
Filesystem::convert_native_arguments(argc, (const char**)argv);
getargs(argc, argv);
if (ap.aborted())
return return_code;
OIIO::attribute("threads", nthreads);
bool ok = true;
if (inplace) {
for (auto&& s : filenames)
ok &= convert_file(s, s);
} else {
ok = convert_file(filenames[0], filenames[1]);
}
shutdown();
return ok ? EXIT_SUCCESS : EXIT_FAILURE;
}