-
Notifications
You must be signed in to change notification settings - Fork 489
Expand file tree
/
Copy pathmain.cpp
More file actions
746 lines (661 loc) · 23.8 KB
/
main.cpp
File metadata and controls
746 lines (661 loc) · 23.8 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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
// SPDX-License-Identifier: BSD-3-Clause
// Copyright Contributors to the OpenColorIO Project.
#include <chrono>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <utility>
#include <vector>
#include <OpenColorIO/OpenColorIO.h>
namespace OCIO = OCIO_NAMESPACE;
#include "apputils/argparse.h"
#ifdef OCIO_GPU_ENABLED
#include "oglapp.h"
#endif // OCIO_GPU_ENABLED
#include "imageio.h"
// Array of non OpenColorIO arguments.
static std::vector<std::string> args;
// Fill 'args' array with OpenColorIO arguments.
static int parse_end_args(int argc, const char *argv[])
{
while (argc>0)
{
args.push_back(argv[0]);
argc--;
argv++;
}
return 0;
}
bool ParseNameValuePair(std::string& name, std::string& value, const std::string& input);
bool StringToFloat(float * fval, const char * str);
bool StringToInt(int * ival, const char * str);
int main(int argc, const char **argv)
{
ArgParse ap;
std::vector<std::string> floatAttrs;
std::vector<std::string> intAttrs;
std::vector<std::string> stringAttrs;
std::string outputDepth;
std::string inputconfig;
bool usegpu = false;
bool usegpuLegacy = false;
bool outputgpuInfo = false;
bool verbose = false;
bool help = false;
bool useLut = false;
bool useDisplayView = false;
bool useInvertView = false;
bool useNamedTransform = false;
bool useInvNamedTransform = false;
ap.options("ocioconvert -- apply colorspace transform to an image \n\n"
"usage: ocioconvert [options] inputimage inputcolorspace outputimage outputcolorspace\n"
" or: ocioconvert [options] --lut lutfile inputimage outputimage\n"
" or: ocioconvert [options] --view inputimage inputcolorspace outputimage displayname viewname\n"
" or: ocioconvert [options] --invertview inputimage displayname viewname outputimage outputcolorspace\n"
" or: ocioconvert [options] --namedtransform transformname inputimage outputimage\n"
" or: ocioconvert [options] --invnamedtransform transformname inputimage outputimage\n\n",
"%*", parse_end_args, "",
"<SEPARATOR>", "Options:",
"--lut", &useLut, "Convert using a LUT rather than a config file",
"--view", &useDisplayView, "Convert to a (display,view) pair rather than to "
"an output color space",
"--invertview", &useInvertView, "Convert from a (display,view) pair rather than "
"from a color space",
"--namedtransform", &useNamedTransform, "Convert using a named transform in the forward direction",
"--invnamedtransform", &useInvNamedTransform, "Convert using a named transform in the inverse direction",
"--gpu", &usegpu, "Use GPU color processing instead of CPU (CPU is the default)",
"--gpulegacy", &usegpuLegacy, "Use the legacy (i.e. baked) GPU color processing "
"instead of the CPU one (--gpu is ignored)",
"--gpuinfo", &outputgpuInfo, "Output the OCIO shader program",
"--h", &help, "Display the help and exit",
"--help", &help, "Display the help and exit",
"-v" , &verbose, "Display general information",
"--iconfig %s", &inputconfig, "Input .ocio configuration file (default: $OCIO)",
"<SEPARATOR>", "\nOpenImageIO or OpenEXR options:",
"--bitdepth %s", &outputDepth, "Output image bitdepth",
"--float-attribute %L", &floatAttrs, "\"name=float\" pair defining OIIO float attribute "
"for outputimage",
"--int-attribute %L", &intAttrs, "\"name=int\" pair defining an int attribute "
"for outputimage",
"--string-attribute %L", &stringAttrs, "\"name=string\" pair defining a string attribute "
"for outputimage",
NULL
);
if (ap.parse (argc, argv) < 0)
{
std::cerr << ap.geterror() << std::endl;
ap.usage ();
exit(1);
}
if (help)
{
ap.usage();
return 0;
}
#ifndef OCIO_GPU_ENABLED
if (usegpu || outputgpuInfo || usegpuLegacy)
{
std::cerr << "Compiled without OpenGL support, GPU options are not available.";
std::cerr << std::endl;
exit(1);
}
#endif // OCIO_GPU_ENABLED
OCIO::BitDepth userOutputBitDepth = OCIO::BIT_DEPTH_UNKNOWN;
if (!outputDepth.empty())
{
if (outputDepth == "uint8")
{
userOutputBitDepth = OCIO::BIT_DEPTH_UINT8;
}
else if (outputDepth == "uint16")
{
userOutputBitDepth = OCIO::BIT_DEPTH_UINT16;
}
else if (outputDepth == "half")
{
userOutputBitDepth = OCIO::BIT_DEPTH_F16;
}
else if (outputDepth == "float")
{
userOutputBitDepth = OCIO::BIT_DEPTH_F32;
}
else
{
throw OCIO::Exception("Unsupported output bitdepth, must be uint8, uint16, half or float.");
}
}
const char * inputimage = nullptr;
const char * inputcolorspace = nullptr;
const char * outputimage = nullptr;
const char * outputcolorspace = nullptr;
const char * lutFile = nullptr;
const char * display = nullptr;
const char * view = nullptr;
const char * namedtransform = nullptr;
if (!useLut && !useDisplayView && !useInvertView && !useNamedTransform && !useInvNamedTransform)
{
if (args.size() != 4)
{
std::cerr << "ERROR: Expecting 4 arguments, found "
<< args.size() << "." << std::endl;
ap.usage();
exit(1);
}
inputimage = args[0].c_str();
inputcolorspace = args[1].c_str();
outputimage = args[2].c_str();
outputcolorspace = args[3].c_str();
}
else if (useLut && useDisplayView)
{
std::cerr << "ERROR: Options lut & view can't be used at the same time." << std::endl;
ap.usage();
exit(1);
}
else if (useLut)
{
if (args.size() != 3)
{
std::cerr << "ERROR: Expecting 3 arguments for --lut option, found "
<< args.size() << "." << std::endl;
ap.usage();
exit(1);
}
lutFile = args[0].c_str();
inputimage = args[1].c_str();
outputimage = args[2].c_str();
}
else if (useDisplayView)
{
if (args.size() != 5)
{
std::cerr << "ERROR: Expecting 5 arguments for --view option, found "
<< args.size() << "." << std::endl;
ap.usage();
exit(1);
}
inputimage = args[0].c_str();
inputcolorspace = args[1].c_str();
outputimage = args[2].c_str();
display = args[3].c_str();
view = args[4].c_str();
}
else if (useDisplayView && useInvertView)
{
std::cerr << "ERROR: Options view & invertview can't be used at the same time." << std::endl;
ap.usage();
exit(1);
}
else if (useInvertView)
{
if (args.size() != 5)
{
std::cerr << "ERROR: Expecting 5 arguments for --invertview option, found "
<< args.size() << "." << std::endl;
ap.usage();
exit(1);
}
inputimage = args[0].c_str();
display = args[1].c_str();
view = args[2].c_str();
outputimage = args[3].c_str();
outputcolorspace = args[4].c_str();
}
else if (useNamedTransform)
{
if (useLut || useDisplayView || useInvertView || useInvNamedTransform)
{
std::cerr << "ERROR: Option namedtransform can't be used with lut, view, invertview, \
or invnamedtransform at the same time." << std::endl;
ap.usage();
exit(1);
}
if (args.size() != 3)
{
std::cerr << "ERROR: Expecting 3 arguments for --namedtransform option, found "
<< args.size() << "." << std::endl;
ap.usage();
exit(1);
}
namedtransform = args[0].c_str();
inputimage = args[1].c_str();
outputimage = args[2].c_str();
}
else if (useInvNamedTransform)
{
if (useLut || useDisplayView || useInvertView || useNamedTransform)
{
std::cerr << "ERROR: Option invnamedtransform can't be used with lut, view, invertview, \
or namedtransform at the same time." << std::endl;
ap.usage();
exit(1);
}
if (args.size() != 3)
{
std::cerr << "ERROR: Expecting 3 arguments for --invnamedtransform option, found "
<< args.size() << "." << std::endl;
ap.usage();
exit(1);
}
namedtransform = args[0].c_str();
inputimage = args[1].c_str();
outputimage = args[2].c_str();
}
// Load the current config.
OCIO::ConstConfigRcPtr config;
try
{
if (useLut)
{
config = OCIO::Config::CreateRaw();
}
else if (!inputconfig.empty() )
{
config = OCIO::Config::CreateFromFile(inputconfig.c_str());
}
else
{
const char * env = OCIO::GetEnvVariable("OCIO");
if (env && *env)
inputconfig = env;
config = OCIO::GetCurrentConfig();
}
}
catch (const OCIO::Exception & e)
{
std::cout << "ERROR loading config file: " << e.what() << std::endl;
exit(1);
}
catch (...)
{
std::cerr << "ERROR loading config file: '" << inputconfig << "'" << std::endl;
exit(1);
}
if (verbose)
{
std::cout << std::endl;
std::cout << OCIO::ImageIO::GetVersion() << std::endl;
std::cout << "OCIO Version: " << OCIO::GetVersion() << std::endl;
if (!useLut)
{
std::cout << std::endl;
std::cout << "OCIO Config. file: '" << inputconfig << "'" << std::endl;
std::cout << "OCIO Config. version: " << config->getMajorVersion() << "."
<< config->getMinorVersion() << std::endl;
std::cout << "OCIO search_path: " << config->getSearchPath() << std::endl;
}
}
if (usegpuLegacy)
{
std::cout << std::endl;
std::cout << "Using legacy OCIO v1 GPU color processing." << std::endl;
}
else if (usegpu)
{
std::cout << std::endl;
std::cout << "Using GPU color processing." << std::endl;
}
OCIO::ImageIO imgInput;
OCIO::ImageIO imgOutputCPU;
// Default is to perform in-place conversion.
OCIO::ImageIO *imgOutput = &imgInput;
// Load the image.
std::cout << std::endl;
std::cout << "Loading " << inputimage << std::endl;
try
{
if (usegpu || usegpuLegacy)
{
imgInput.read(inputimage, OCIO::BIT_DEPTH_F32);
}
else
{
imgInput.read(inputimage);
}
std::cout << imgInput.getImageDescStr() << std::endl;
}
catch (const std::exception & e)
{
std::cerr << "ERROR: Loading file failed: " << e.what() << std::endl;
exit(1);
}
catch (...)
{
std::cerr << "ERROR: Loading file failed." << std::endl;
exit(1);
}
#ifdef OCIO_GPU_ENABLED
// Initialize GPU.
OCIO::GraphicalAppRcPtr oglApp;
if (usegpu || usegpuLegacy)
{
OCIO::GraphicalApp::Components comp = OCIO::GraphicalApp::COMPONENTS_RGBA;
if (imgInput.getNumChannels() == 4)
{
comp = OCIO::GraphicalApp::COMPONENTS_RGBA;
}
else if (imgInput.getNumChannels() == 3)
{
comp = OCIO::GraphicalApp::COMPONENTS_RGB;
}
else
{
std::cerr << "Cannot convert image with " << imgInput.getNumChannels()
<< " components." << std::endl;
exit(1);
}
try
{
oglApp = OCIO::GraphicalApp::CreateApp("ocioconvert", 256, 20);
}
catch (const OCIO::Exception & e)
{
std::cerr << std::endl << e.what() << std::endl;
exit(1);
}
if (verbose)
{
oglApp->printGraphicsInfo();
}
oglApp->setShaderVerbose(outputgpuInfo);
oglApp->initImage(imgInput.getWidth(), imgInput.getHeight(), comp, (float *)imgInput.getData());
oglApp->createBuffers();
}
#endif // OCIO_GPU_ENABLED
// Process the image.
try
{
// Get the processor.
OCIO::ConstProcessorRcPtr processor;
try
{
if (useLut)
{
// Create the OCIO processor for the specified transform.
OCIO::FileTransformRcPtr t = OCIO::FileTransform::Create();
t->setSrc(lutFile);
t->setInterpolation(OCIO::INTERP_BEST);
processor = config->getProcessor(t);
}
else if (useDisplayView)
{
OCIO::DisplayViewTransformRcPtr t = OCIO::DisplayViewTransform::Create();
t->setSrc(inputcolorspace);
t->setDisplay(display);
t->setView(view);
processor = config->getProcessor(t);
}
else if (useInvertView)
{
OCIO::DisplayViewTransformRcPtr t = OCIO::DisplayViewTransform::Create();
t->setSrc(outputcolorspace);
t->setDisplay(display);
t->setView(view);
processor = config->getProcessor(t, OCIO::TRANSFORM_DIR_INVERSE);
}
else if (useNamedTransform)
{
auto nt = config->getNamedTransform(namedtransform);
if (nt)
{
processor = config->getProcessor(nt, OCIO::TRANSFORM_DIR_FORWARD);
}
else
{
std::cout << "ERROR: Could not get NamedTransform " << namedtransform << std::endl;
exit(1);
}
}
else if (useInvNamedTransform)
{
auto nt = config->getNamedTransform(namedtransform);
if (nt)
{
processor = config->getProcessor(nt, OCIO::TRANSFORM_DIR_INVERSE);
}
else
{
std::cout << "ERROR: Could not get NamedTransform " << namedtransform << std::endl;
exit(1);
}
}
else
{
processor = config->getProcessor(inputcolorspace, outputcolorspace);
}
}
catch (const OCIO::Exception & e)
{
std::cout << "ERROR: OCIO failed with: " << e.what() << std::endl;
exit(1);
}
catch (...)
{
std::cout << "ERROR: Creating processor unknown failure." << std::endl;
exit(1);
}
#ifdef OCIO_GPU_ENABLED
if (usegpu || usegpuLegacy)
{
// Get the GPU shader program from the processor and set oglApp to use it.
OCIO::GpuShaderDescRcPtr shaderDesc = OCIO::GpuShaderDesc::CreateShaderDesc();
shaderDesc->setLanguage(OCIO::GPU_LANGUAGE_GLSL_1_2);
OCIO::ConstGPUProcessorRcPtr gpu
= usegpuLegacy ? processor->getOptimizedLegacyGPUProcessor(OCIO::OPTIMIZATION_DEFAULT, 32)
: processor->getDefaultGPUProcessor();
gpu->extractGpuShaderInfo(shaderDesc);
oglApp->setShader(shaderDesc);
oglApp->reshape(imgInput.getWidth(), imgInput.getHeight());
oglApp->redisplay();
oglApp->readImage((float *)imgInput.getData());
}
else
#endif // OCIO_GPU_ENABLED
{
/*
Set the bit-depth of the output buffer.
Whereas the GPU processor always work on float data, the CPU processor
can be optimised for a specific input and output bit-depth.
The converted image may require more bits than the source image.
For example, converting a log image to linear requires at least a half-float
output format. For most cases, half-float strikes a good balance between
precision and storage space. But if the input depth would lose precision
when converted to half-float, use float for the output depth instead.
Note that when using OpenImageIO, the actual output bit-depth may be overrided
if the file format doesn't support it. OCIO is not trying to analyze the filename
to emulate OpenImageIO's decision making process.
*/
const OCIO::BitDepth inputBitDepth = imgInput.getBitDepth();
OCIO::BitDepth outputBitDepth;
if (userOutputBitDepth != OCIO::BIT_DEPTH_UNKNOWN)
{
outputBitDepth = userOutputBitDepth;
}
else
{
if (inputBitDepth == OCIO::BIT_DEPTH_UINT16 || inputBitDepth == OCIO::BIT_DEPTH_F32)
{
outputBitDepth = OCIO::BIT_DEPTH_F32;
}
else if (inputBitDepth == OCIO::BIT_DEPTH_UINT8 || inputBitDepth == OCIO::BIT_DEPTH_F16)
{
outputBitDepth = OCIO::BIT_DEPTH_F16;
}
else
{
throw OCIO::Exception("Unsupported input bitdepth, must be uint8, uint16, half or float.");
}
}
OCIO::ConstCPUProcessorRcPtr cpuProcessor
= processor->getOptimizedCPUProcessor(inputBitDepth,
outputBitDepth,
OCIO::OPTIMIZATION_DEFAULT);
const bool useOutputBuffer = inputBitDepth != outputBitDepth;
if (useOutputBuffer)
{
imgOutputCPU.init(imgInput, outputBitDepth);
imgOutput = &imgOutputCPU;
}
const std::chrono::high_resolution_clock::time_point start
= std::chrono::high_resolution_clock::now();
if (useOutputBuffer)
{
OCIO::ImageDescRcPtr srcImgDesc = imgInput.getImageDesc();
OCIO::ImageDescRcPtr dstImgDesc = imgOutputCPU.getImageDesc();
cpuProcessor->apply(*srcImgDesc, *dstImgDesc);
}
else
{
OCIO::ImageDescRcPtr imgDesc = imgInput.getImageDesc();
cpuProcessor->apply(*imgDesc);
}
if (verbose)
{
const std::chrono::high_resolution_clock::time_point end
= std::chrono::high_resolution_clock::now();
std::chrono::duration<float, std::milli> duration = end - start;
std::cout << std::endl;
std::cout << "CPU processing took: "
<< duration.count()
<< " ms" << std::endl;
}
}
}
catch (const OCIO::Exception & exception)
{
std::cerr << "ERROR: OCIO failed with: " << exception.what() << std::endl;
exit(1);
}
catch (...)
{
std::cerr << "ERROR: Unknown error processing the image." << std::endl;
exit(1);
}
// Set the provided image attributes.
bool parseError = false;
for (unsigned int i=0; i<floatAttrs.size(); ++i)
{
std::string name, value;
float fval = 0.0f;
if (!ParseNameValuePair(name, value, floatAttrs[i]) ||
!StringToFloat(&fval,value.c_str()))
{
std::cerr << "ERROR: Attribute string '" << floatAttrs[i]
<< "' should be in the form name=floatvalue." << std::endl;
parseError = true;
continue;
}
imgOutput->attribute(name, fval);
}
for (unsigned int i=0; i<intAttrs.size(); ++i)
{
std::string name, value;
int ival = 0;
if (!ParseNameValuePair(name, value, intAttrs[i]) ||
!StringToInt(&ival,value.c_str()))
{
std::cerr << "ERROR: Attribute string '" << intAttrs[i]
<< "' should be in the form name=intvalue." << std::endl;
parseError = true;
continue;
}
imgOutput->attribute(name, ival);
}
for (unsigned int i=0; i<stringAttrs.size(); ++i)
{
std::string name, value;
if (!ParseNameValuePair(name, value, stringAttrs[i]))
{
std::cerr << "ERROR: Attribute string '" << stringAttrs[i]
<< "' should be in the form name=value." << std::endl;
parseError = true;
continue;
}
imgOutput->attribute(name, value);
}
if (parseError)
{
exit(1);
}
// Write out the result.
try
{
if (useDisplayView)
{
outputcolorspace = config->getDisplayViewColorSpaceName(display, view);
}
if (outputcolorspace)
{
imgOutput->attribute("oiio:ColorSpace", outputcolorspace);
// Set the color space interopID if available.
auto cs = config->getColorSpace(outputcolorspace);
const char* interopID = cs ? cs->getInteropID() : nullptr;
if(interopID && *interopID)
{
imgOutput->attribute("colorInteropID", interopID);
}
}
imgOutput->write(outputimage, userOutputBitDepth);
}
catch (...)
{
std::cerr << "ERROR: Writing file \"" << outputimage << "\"." << std::endl;
exit(1);
}
std::cout << "Wrote " << outputimage << std::endl;
std::cout << imgOutput->getImageDescStr() << std::endl;
return 0;
}
// Parse name=value parts.
// return true on success.
bool ParseNameValuePair(std::string& name,
std::string& value,
const std::string& input)
{
// split string into name=value.
size_t pos = input.find('=');
if (pos==std::string::npos)
{
return false;
}
name = input.substr(0,pos);
value = input.substr(pos+1);
return true;
}
// return true on success.
bool StringToFloat(float * fval, const char * str)
{
if (!str)
{
return false;
}
std::istringstream inputStringstream(str);
float x;
if (!(inputStringstream >> x))
{
return false;
}
if (fval)
{
*fval = x;
}
return true;
}
bool StringToInt(int * ival, const char * str)
{
if (!str)
{
return false;
}
std::istringstream inputStringstream(str);
int x;
if (!(inputStringstream >> x))
{
return false;
}
if (ival)
{
*ival = x;
}
return true;
}