-
Notifications
You must be signed in to change notification settings - Fork 490
Expand file tree
/
Copy pathmain.cpp
More file actions
752 lines (655 loc) · 20.3 KB
/
main.cpp
File metadata and controls
752 lines (655 loc) · 20.3 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
747
748
749
750
751
752
// SPDX-License-Identifier: BSD-3-Clause
// Copyright Contributors to the OpenColorIO Project.
#include <array>
#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <fstream>
#include <map>
#include <sstream>
#include <utility>
#include <vector>
#include <OpenColorIO/OpenColorIO.h>
namespace OCIO = OCIO_NAMESPACE;
#ifdef __APPLE__
#include <OpenGL/gl.h>
#include <OpenGL/glext.h>
#include <GLUT/glut.h>
#elif _WIN32
#include <GL/glew.h>
#include <GL/glut.h>
#else
#include <GL/glew.h>
#include <GL/gl.h>
#include <GL/glut.h>
#endif
#if __APPLE__
#include "metalapp.h"
#endif
#include "glsl.h"
#include "oglapp.h"
#include "imageio.h"
bool g_verbose = false;
bool g_gpulegacy = false;
bool g_gpuinfo = false;
#if __APPLE__
bool g_useMetal = false;
#endif
std::string g_filename;
float g_imageAspect;
std::string g_inputColorSpace;
std::string g_display;
std::string g_transformName;
std::string g_look;
OCIO::OptimizationFlags g_optimization{OCIO::OPTIMIZATION_DEFAULT};
static const std::array<std::pair<const char *, OCIO::OptimizationFlags>, 5> OptmizationMenu = {{{"None", OCIO::OPTIMIZATION_NONE},
{"Lossless", OCIO::OPTIMIZATION_LOSSLESS},
{"Very good", OCIO::OPTIMIZATION_VERY_GOOD},
{"Good", OCIO::OPTIMIZATION_GOOD},
{"Draft", OCIO::OPTIMIZATION_DRAFT}}};
float g_exposure_fstop{0.0f};
float g_display_gamma{1.0f};
int g_channelHot[4]{1, 1, 1, 1}; // show rgb
int g_viewsMenuID;
OCIO::GraphicalAppRcPtr g_oglApp;
void UpdateOCIOGLState();
static void InitImageTexture(const char * filename)
{
OCIO::ImageIO img;
if (filename && *filename)
{
std::cout << "Loading: " << filename << std::endl;
try
{
img.read(filename, OCIO::BIT_DEPTH_F32);
}
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);
}
}
// If no file is provided, use a default gradient texture
else
{
std::cout << "No image specified, loading gradient." << std::endl;
img.init(512, 512, OCIO::CHANNEL_ORDERING_RGBA, OCIO::BIT_DEPTH_F32);
float * pixels = (float *)img.getData();
const long width = img.getWidth();
const long channels = img.getNumChannels();
for (int y = 0; y < img.getHeight(); ++y)
{
for (int x = 0; x < img.getWidth(); ++x)
{
float c = (float)x / ((float)width - 1.0f);
pixels[channels * (width * y + x) + 0] = c;
pixels[channels * (width * y + x) + 1] = c;
pixels[channels * (width * y + x) + 2] = c;
pixels[channels * (width * y + x) + 3] = 1.0f;
}
}
}
OCIO::GraphicalApp::Components comp = OCIO::GraphicalApp::COMPONENTS_RGBA;
if (img.getNumChannels() == 4)
{
comp = OCIO::GraphicalApp::COMPONENTS_RGBA;
}
else if (img.getNumChannels() == 3)
{
comp = OCIO::GraphicalApp::COMPONENTS_RGB;
}
else
{
std::cerr << "Cannot load image with " << img.getNumChannels()
<< " components." << std::endl;
exit(1);
}
g_imageAspect = 1.0;
if (img.getHeight() != 0)
{
g_imageAspect = (float)img.getWidth() / (float)img.getHeight();
}
if (g_oglApp)
{
g_oglApp->initImage(img.getWidth(),
img.getHeight(),
comp,
(float *)img.getData());
}
}
void InitOCIO(const char * filename)
{
OCIO::ConstConfigRcPtr config = OCIO::GetCurrentConfig();
g_display = config->getDefaultDisplay();
g_transformName = config->getDefaultView(g_display.c_str());
g_look = config->getDisplayViewLooks(g_display.c_str(), g_transformName.c_str());
g_inputColorSpace = OCIO::ROLE_SCENE_LINEAR;
if (filename && *filename)
{
std::string cs = config->getColorSpaceFromFilepath(filename);
if (!cs.empty())
{
g_inputColorSpace = cs;
std::cout << "colorspace: " << cs << std::endl;
}
else
{
std::cout << "colorspace: " << g_inputColorSpace
<< " \t(could not determine from filename, using default)"
<< std::endl;
}
}
}
void Redisplay(void)
{
if (g_oglApp)
{
g_oglApp->redisplay();
}
}
static void Reshape(int width, int height)
{
if (g_oglApp)
{
g_oglApp->reshape(width, height);
}
}
static void CleanUp(void)
{
g_oglApp.reset();
}
static void Key(unsigned char key, int /*x*/, int /*y*/)
{
if (key == 'c' || key == 'C')
{
g_channelHot[0] = 1;
g_channelHot[1] = 1;
g_channelHot[2] = 1;
g_channelHot[3] = 1;
}
else if (key == 'r' || key == 'R')
{
g_channelHot[0] = 1;
g_channelHot[1] = 0;
g_channelHot[2] = 0;
g_channelHot[3] = 0;
}
else if (key == 'g' || key == 'G')
{
g_channelHot[0] = 0;
g_channelHot[1] = 1;
g_channelHot[2] = 0;
g_channelHot[3] = 0;
}
else if (key == 'b' || key == 'B')
{
g_channelHot[0] = 0;
g_channelHot[1] = 0;
g_channelHot[2] = 1;
g_channelHot[3] = 0;
}
else if (key == 'a' || key == 'A')
{
g_channelHot[0] = 0;
g_channelHot[1] = 0;
g_channelHot[2] = 0;
g_channelHot[3] = 1;
}
else if (key == 'l' || key == 'L')
{
g_channelHot[0] = 1;
g_channelHot[1] = 1;
g_channelHot[2] = 1;
g_channelHot[3] = 0;
}
else if (key == 27)
{
CleanUp();
exit(0);
}
UpdateOCIOGLState();
glutPostRedisplay();
}
static void SpecialKey(int key, int x, int y)
{
(void)x;
(void)y;
int mod = glutGetModifiers();
if (key == GLUT_KEY_UP && (mod & GLUT_ACTIVE_CTRL))
{
g_exposure_fstop += 0.25f;
}
else if (key == GLUT_KEY_DOWN && (mod & GLUT_ACTIVE_CTRL))
{
g_exposure_fstop -= 0.25f;
}
else if (key == GLUT_KEY_HOME && (mod & GLUT_ACTIVE_CTRL))
{
g_exposure_fstop = 0.0f;
g_display_gamma = 1.0f;
}
else if (key == GLUT_KEY_UP && (mod & GLUT_ACTIVE_ALT))
{
g_display_gamma *= 1.1f;
}
else if (key == GLUT_KEY_DOWN && (mod & GLUT_ACTIVE_ALT))
{
g_display_gamma /= 1.1f;
}
else if (key == GLUT_KEY_HOME && (mod & GLUT_ACTIVE_ALT))
{
g_exposure_fstop = 0.0f;
g_display_gamma = 1.0f;
}
UpdateOCIOGLState();
glutPostRedisplay();
}
void updateViewsMenu(const char * displayValue)
{
OCIO::ConstConfigRcPtr config = OCIO::GetCurrentConfig();
glutSetMenu(g_viewsMenuID);
int numViewsMenuItems = glutGet(GLUT_MENU_NUM_ITEMS);
for (int i = numViewsMenuItems; i > 0; --i)
{
glutRemoveMenuItem(i);
}
for (int i = 0; i < config->getNumViews(displayValue); ++i)
{
glutAddMenuEntry(config->getView(displayValue, i), i);
}
}
void UpdateOCIOGLState()
{
if (!g_oglApp)
{
return;
}
// Step 0: Get the processor using any of the pipelines mentioned above.
OCIO::ConstConfigRcPtr config = OCIO::GetCurrentConfig();
OCIO::DisplayViewTransformRcPtr transform = OCIO::DisplayViewTransform::Create();
transform->setSrc(g_inputColorSpace.c_str());
transform->setDisplay(g_display.c_str());
transform->setView(g_transformName.c_str());
OCIO::LegacyViewingPipelineRcPtr vp = OCIO::LegacyViewingPipeline::Create();
vp->setDisplayViewTransform(transform);
vp->setLooksOverrideEnabled(true);
vp->setLooksOverride(g_look.c_str());
if (g_verbose)
{
std::cout << std::endl;
std::cout << "Color transformation composed of:" << std::endl;
std::cout << " Image ColorSpace is:\t" << g_inputColorSpace << std::endl;
std::cout << " Views is:\t\t" << g_transformName << std::endl;
std::cout << " Display is:\t\t" << g_display << std::endl;
std::cout << " Looks Override is:\t'" << g_look << "'" << std::endl;
std::cout << " with:" << std::endl;
std::cout << " exposure_fstop = " << g_exposure_fstop << std::endl;
std::cout << " display_gamma = " << g_display_gamma << std::endl;
std::cout << " channels = "
<< (g_channelHot[0] ? "R" : "")
<< (g_channelHot[1] ? "G" : "")
<< (g_channelHot[2] ? "B" : "")
<< (g_channelHot[3] ? "A" : "") << std::endl;
for (const auto &opt : OptmizationMenu)
{
if (opt.second == g_optimization)
{
std::cout << std::endl
<< "Optimization: " << opt.first << std::endl;
}
}
}
// Add optional transforms to create a full-featured, "canonical" display pipeline
// Fstop exposure control (in SCENE_LINEAR)
{
double gain = powf(2.0f, g_exposure_fstop);
const double slope4f[] = {gain, gain, gain, gain};
double m44[16];
double offset4[4];
OCIO::MatrixTransform::Scale(m44, offset4, slope4f);
OCIO::MatrixTransformRcPtr mtx = OCIO::MatrixTransform::Create();
mtx->setMatrix(m44);
mtx->setOffset(offset4);
vp->setLinearCC(mtx);
}
// Channel swizzling
{
double lumacoef[3];
config->getDefaultLumaCoefs(lumacoef);
double m44[16];
double offset[4];
OCIO::MatrixTransform::View(m44, offset, g_channelHot, lumacoef);
OCIO::MatrixTransformRcPtr swizzle = OCIO::MatrixTransform::Create();
swizzle->setMatrix(m44);
swizzle->setOffset(offset);
vp->setChannelView(swizzle);
}
// Post-display transform gamma
{
double exponent = 1.0 / std::max(1e-6, static_cast<double>(g_display_gamma));
const double exponent4f[4] = {exponent, exponent, exponent, exponent};
OCIO::ExponentTransformRcPtr expTransform = OCIO::ExponentTransform::Create();
expTransform->setValue(exponent4f);
vp->setDisplayCC(expTransform);
}
OCIO::ConstProcessorRcPtr processor;
try
{
processor = vp->getProcessor(config, config->getCurrentContext());
}
catch (const OCIO::Exception &e)
{
std::cerr << e.what() << std::endl;
return;
}
catch (...)
{
return;
}
// Set the shader context.
OCIO::GpuShaderDescRcPtr shaderDesc = OCIO::GpuShaderDesc::CreateShaderDesc();
shaderDesc->setLanguage(
#if __APPLE__
g_useMetal ? OCIO::GPU_LANGUAGE_MSL_2_0 :
#endif
OCIO::GPU_LANGUAGE_GLSL_1_2);
shaderDesc->setFunctionName("OCIODisplay");
shaderDesc->setResourcePrefix("ocio_");
// Extract the shader information.
OCIO::ConstGPUProcessorRcPtr gpu = g_gpulegacy ? processor->getOptimizedLegacyGPUProcessor(g_optimization, 32)
: processor->getOptimizedGPUProcessor(g_optimization);
gpu->extractGpuShaderInfo(shaderDesc);
g_oglApp->setShader(shaderDesc);
}
void menuCallback(int /*id*/)
{
glutPostRedisplay();
}
void imageColorSpace_CB(int id)
{
OCIO::ConstConfigRcPtr config = OCIO::GetCurrentConfig();
const char * name = config->getColorSpaceNameByIndex(id);
if (!name)
{
return;
}
g_inputColorSpace = name;
UpdateOCIOGLState();
glutPostRedisplay();
}
void displayDevice_CB(int id)
{
OCIO::ConstConfigRcPtr config = OCIO::GetCurrentConfig();
const char * display = config->getDisplay(id);
if (!display)
{
return;
}
g_display = display;
const char * csname = config->getDisplayViewColorSpaceName(g_display.c_str(), g_transformName.c_str());
if (!csname || !*csname)
{
g_transformName = config->getDefaultView(g_display.c_str());
}
g_look = config->getDisplayViewLooks(g_display.c_str(), g_transformName.c_str());
updateViewsMenu(display);
UpdateOCIOGLState();
glutPostRedisplay();
}
void transform_CB(int id)
{
OCIO::ConstConfigRcPtr config = OCIO::GetCurrentConfig();
const char * transform = config->getView(g_display.c_str(), id);
if (!transform)
{
return;
}
g_transformName = transform;
g_look = config->getDisplayViewLooks(g_display.c_str(), g_transformName.c_str());
UpdateOCIOGLState();
glutPostRedisplay();
}
void look_CB(int id)
{
OCIO::ConstConfigRcPtr config = OCIO::GetCurrentConfig();
const char * look = config->getLookNameByIndex(id);
if (!look || !*look)
{
return;
}
g_look = look;
UpdateOCIOGLState();
glutPostRedisplay();
}
void optimization_CB(int id)
{
g_optimization = OptmizationMenu[id].second;
UpdateOCIOGLState();
glutPostRedisplay();
}
static void PopulateOCIOMenus()
{
OCIO::ConstConfigRcPtr config = OCIO::GetCurrentConfig();
int csMenuID = glutCreateMenu(imageColorSpace_CB);
std::map<std::string, int> families;
for (int i = 0; i < config->getNumColorSpaces(); ++i)
{
const char * csName = config->getColorSpaceNameByIndex(i);
if (csName && *csName)
{
OCIO::ConstColorSpaceRcPtr cs = config->getColorSpace(csName);
if (cs)
{
const char * family = cs->getFamily();
if (family && *family)
{
if (families.find(family) == families.end())
{
families[family] = glutCreateMenu(imageColorSpace_CB);
glutAddMenuEntry(csName, i);
glutSetMenu(csMenuID);
glutAddSubMenu(family, families[family]);
}
else
{
glutSetMenu(families[family]);
glutAddMenuEntry(csName, i);
}
}
else
{
glutSetMenu(csMenuID);
glutAddMenuEntry(csName, i);
}
}
}
}
int displayMenuID = glutCreateMenu(displayDevice_CB);
for (int i = 0; i < config->getNumDisplays(); ++i)
{
glutAddMenuEntry(config->getDisplay(i), i);
}
g_viewsMenuID = glutCreateMenu(transform_CB);
const char * defaultDisplay = config->getDefaultDisplay();
updateViewsMenu(defaultDisplay);
int lookMenuID = glutCreateMenu(look_CB);
for (int i = 0; i < config->getNumLooks(); ++i)
{
glutAddMenuEntry(config->getLookNameByIndex(i), i);
}
int optimizationMenuID = glutCreateMenu(optimization_CB);
for (size_t i = 0; i < OptmizationMenu.size(); ++i)
{
glutAddMenuEntry(OptmizationMenu[i].first, static_cast<int>(i));
}
glutCreateMenu(menuCallback);
glutAddSubMenu("Image ColorSpace", csMenuID);
glutAddSubMenu("Views", g_viewsMenuID);
glutAddSubMenu("Display", displayMenuID);
glutAddSubMenu("Looks Override", lookMenuID);
glutAddSubMenu("Optimization", optimizationMenuID);
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
const char * USAGE_TEXT = "\n"
"Keys:\n"
"\tCtrl+Up: Exposure +1/4 stop (in scene linear)\n"
"\tCtrl+Down: Exposure -1/4 stop (in scene linear)\n"
"\tCtrl+Home: Reset Exposure + Gamma\n"
"\n"
"\tAlt+Up: Gamma up (post display transform)\n"
"\tAlt+Down: Gamma down (post display transform)\n"
"\tAlt+Home: Reset Exposure + Gamma\n"
"\n"
"\tC: View Color\n"
"\tR: View Red\n"
"\tG: View Green\n"
"\tB: View Blue\n"
"\tA: View Alpha\n"
"\tL: View Luma\n"
"\n"
"\tRight-Mouse Button: Configure Display / Transform / ColorSpace / Looks / Optimization\n"
"\n"
"\tEsc: Quit\n";
void parseArguments(int argc, char **argv)
{
for (int i = 1; i < argc; ++i)
{
if (0 == strcmp(argv[i], "-v"))
{
g_verbose = true;
}
else if (0 == strcmp(argv[i], "-gpulegacy"))
{
g_gpulegacy = true;
}
else if (0 == strcmp(argv[i], "-gpuinfo"))
{
g_gpuinfo = true;
}
#if __APPLE__
else if (0 == strcmp(argv[i], "-metal"))
{
g_useMetal = true;
}
#endif
else if (0 == strcmp(argv[i], "-h"))
{
std::cout << std::endl;
std::cout << "help:" << std::endl;
std::cout << " ociodisplay [OPTIONS] [image] where" << std::endl;
std::cout << std::endl;
std::cout << " OPTIONS:" << std::endl;
std::cout << " -h : displays the help and exit" << std::endl;
std::cout << " -v : displays the color space information" << std::endl;
std::cout << " -gpulegacy : use the legacy (i.e. baked) GPU color processing" << std::endl;
std::cout << " -gpuinfo : output the OCIO shader program" << std::endl;
#if __APPLE__
std::cout << " -metal : use metal OCIO shader backend " << std::endl;
#endif
std::cout << std::endl;
exit(0);
}
else
{
g_filename = argv[i];
}
}
}
int main(int argc, char **argv)
{
parseArguments(argc, argv);
try
{
#if __APPLE__
if (g_useMetal)
{
g_oglApp = std::make_shared<OCIO::MetalApp>("ociodisplay", 512, 512);
}
else
#endif
{
g_oglApp = std::make_shared<OCIO::ScreenOglApp>("ociodisplay", 512, 512);
}
}
catch (const OCIO::Exception &e)
{
std::cerr << e.what() << std::endl;
return 1;
}
if (g_verbose)
{
g_oglApp->printGraphicsInfo();
}
g_oglApp->setYMirror();
g_oglApp->setShaderVerbose(g_gpuinfo);
glutReshapeFunc(Reshape);
glutKeyboardFunc(Key);
glutSpecialFunc(SpecialKey);
glutDisplayFunc(Redisplay);
if (g_verbose)
{
if (!g_filename.empty())
{
std::cout << std::endl;
std::cout << "Image: " << g_filename << std::endl;
}
std::cout << std::endl;
std::cout << OCIO::ImageIO::GetVersion() << std::endl;
std::cout << "OCIO Version: " << OCIO::GetVersion() << std::endl;
}
OCIO::ConstConfigRcPtr config;
try
{
config = OCIO::GetCurrentConfig();
}
catch (...)
{
const char * env = OCIO::GetEnvVariable("OCIO");
std::cerr << "Error loading the config file: '" << (env ? env : "") << "'";
exit(1);
}
if (g_verbose)
{
const char * env = OCIO::GetEnvVariable("OCIO");
if (env && *env)
{
std::cout << std::endl;
std::cout << "OCIO Config. file : '" << env << "'" << std::endl;
std::cout << "OCIO Config. version: " << config->getMajorVersion() << "."
<< config->getMinorVersion() << std::endl;
std::cout << "OCIO search_path : " << config->getSearchPath() << std::endl;
}
}
std::cout << std::endl;
std::cout << USAGE_TEXT << std::endl;
InitImageTexture(g_filename.c_str());
try
{
InitOCIO(g_filename.c_str());
}
catch (OCIO::Exception &e)
{
std::cerr << e.what() << std::endl;
exit(1);
}
PopulateOCIOMenus();
try
{
UpdateOCIOGLState();
}
catch (const OCIO::Exception &e)
{
std::cerr << e.what() << std::endl;
exit(1);
}
Redisplay();
glutMainLoop();
return 0;
}