-
Notifications
You must be signed in to change notification settings - Fork 491
Expand file tree
/
Copy pathConfig.cpp
More file actions
6079 lines (5181 loc) · 206 KB
/
Config.cpp
File metadata and controls
6079 lines (5181 loc) · 206 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
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: BSD-3-Clause
// Copyright Contributors to the OpenColorIO Project.
#include <cstdlib>
#include <cstring>
#include <set>
#include <sstream>
#include <fstream>
#include <utility>
#include <vector>
#include <regex>
#include <functional>
#include <algorithm>
#include <exception>
#include <pystring.h>
#include <OpenColorIO/OpenColorIO.h>
#include "ConfigUtils.h"
#include "ContextVariableUtils.h"
#include "Display.h"
#include "fileformats/FileFormatICC.h"
#include "FileRules.h"
#include "HashUtils.h"
#include "Logging.h"
#include "LookParse.h"
#include "MathUtils.h"
#include "Mutex.h"
#include "OCIOYaml.h"
#include "OCIOZArchive.h"
#include "ParseUtils.h"
#include "PathUtils.h"
#include "Platform.h"
#include "PrivateTypes.h"
#include "Processor.h"
#include "utils/StringUtils.h"
#include "ViewingRules.h"
#include "SystemMonitor.h"
namespace OCIO_NAMESPACE
{
const char * OCIO_CONFIG_ENVVAR = "OCIO";
const char * OCIO_ACTIVE_DISPLAYS_ENVVAR = "OCIO_ACTIVE_DISPLAYS";
const char * OCIO_ACTIVE_VIEWS_ENVVAR = "OCIO_ACTIVE_VIEWS";
const char * OCIO_INACTIVE_COLORSPACES_ENVVAR = "OCIO_INACTIVE_COLORSPACES";
const char * OCIO_OPTIMIZATION_FLAGS_ENVVAR = "OCIO_OPTIMIZATION_FLAGS";
const char * OCIO_USER_CATEGORIES_ENVVAR = "OCIO_USER_CATEGORIES";
// Default filename (with extension) of a config and archived config.
const char * OCIO_CONFIG_DEFAULT_NAME = "config";
const char * OCIO_CONFIG_DEFAULT_FILE_EXT = ".ocio";
const char * OCIO_CONFIG_ARCHIVE_FILE_EXT = ".ocioz";
// A shared view using this for the color space name will use a display color space that
// has the same name as the display the shared view is used by.
const char * OCIO_VIEW_USE_DISPLAY_NAME = "<USE_DISPLAY_NAME>";
const char * OCIO_BUILTIN_URI_PREFIX = "ocio://";
namespace
{
// These are the 709 primaries specified by the ASC.
constexpr double DEFAULT_LUMA_COEFF_R = 0.2126;
constexpr double DEFAULT_LUMA_COEFF_G = 0.7152;
constexpr double DEFAULT_LUMA_COEFF_B = 0.0722;
constexpr char INTERNAL_RAW_PROFILE[] =
"ocio_profile_version: 2\n"
"strictparsing: false\n"
"roles:\n"
" default: raw\n"
"file_rules:\n"
" - !<Rule> {name: Default, colorspace: default}\n"
"displays:\n"
" sRGB:\n"
" - !<View> {name: Raw, colorspace: raw}\n"
"colorspaces:\n"
" - !<ColorSpace>\n"
" name: raw\n"
" family: raw\n"
" equalitygroup:\n"
" bitdepth: 32f\n"
" isdata: true\n"
" allocation: uniform\n"
" description: 'A raw color space. Conversions to and from this space are no-ops.'\n";
} // anon.
///////////////////////////////////////////////////////////////////////////
const char * GetVersion()
{
return OCIO_VERSION_FULL_STR;
}
int GetVersionHex()
{
return OCIO_VERSION_HEX;
}
namespace
{
ConstConfigRcPtr g_currentConfig;
Mutex g_currentConfigLock;
}
ConstConfigRcPtr GetCurrentConfig()
{
AutoMutex lock(g_currentConfigLock);
if(!g_currentConfig)
{
g_currentConfig = Config::CreateFromEnv();
}
return g_currentConfig;
}
void SetCurrentConfig(const ConstConfigRcPtr & config)
{
AutoMutex lock(g_currentConfigLock);
g_currentConfig = config->createEditableCopy();
}
namespace
{
// Environment
const char* LookupEnvironment(const StringMap & env, const std::string & name)
{
StringMap::const_iterator iter = env.find(name);
if(iter == env.end()) return "";
return iter->second.c_str();
}
// Roles
// (lower case role name: colorspace name)
const char* LookupRole(const StringMap & roles, const std::string & rolename)
{
StringMap::const_iterator iter = roles.find(StringUtils::Lower(rolename));
if(iter == roles.end()) return "";
return iter->second.c_str();
}
void GetFileReferences(std::set<std::string> & files, const ConstTransformRcPtr & transform)
{
if(!transform) return;
if(ConstGroupTransformRcPtr groupTransform =
DynamicPtrCast<const GroupTransform>(transform))
{
for(int i=0; i<groupTransform->getNumTransforms(); ++i)
{
GetFileReferences(files, groupTransform->getTransform(i));
}
}
else if(ConstFileTransformRcPtr fileTransform =
DynamicPtrCast<const FileTransform>(transform))
{
files.insert(fileTransform->getSrc());
}
}
// Return the list of all color spaces referenced by the transform (including all sub-transforms in
// a group). All legal context variables are expanded, so if any are remaining, the caller may want
// to throw.
void GetColorSpaceReferences(std::set<std::string> & colorSpaceNames,
const ConstTransformRcPtr & transform,
const ConstContextRcPtr & context)
{
if(!transform) return;
if(ConstGroupTransformRcPtr groupTransform =
DynamicPtrCast<const GroupTransform>(transform))
{
for(int i=0; i<groupTransform->getNumTransforms(); ++i)
{
GetColorSpaceReferences(colorSpaceNames, groupTransform->getTransform(i), context);
}
}
else if(ConstColorSpaceTransformRcPtr colorSpaceTransform =
DynamicPtrCast<const ColorSpaceTransform>(transform))
{
colorSpaceNames.insert(context->resolveStringVar(colorSpaceTransform->getSrc()));
colorSpaceNames.insert(context->resolveStringVar(colorSpaceTransform->getDst()));
}
else if(ConstDisplayViewTransformRcPtr displayViewTransform =
DynamicPtrCast<const DisplayViewTransform>(transform))
{
colorSpaceNames.insert(displayViewTransform->getSrc());
}
else if(ConstLookTransformRcPtr lookTransform =
DynamicPtrCast<const LookTransform>(transform))
{
colorSpaceNames.insert(lookTransform->getSrc());
colorSpaceNames.insert(lookTransform->getDst());
}
}
// Views are stored in two vectors of objects, using pointers to temporarily group them.
typedef std::vector<const View *> ViewPtrVec;
StringUtils::StringVec GetViewNames(const ViewPtrVec & views)
{
StringUtils::StringVec viewNames;
for (const auto & view : views)
{
viewNames.push_back(view->m_name);
}
return viewNames;
}
std::ostringstream GetDisplayViewPrefixErrorMsg(const std::string & display, const View & view)
{
std::ostringstream oss;
oss << "Config failed display view validation. ";
if (display.empty())
{
oss << "Shared ";
}
else
{
oss << "Display '" << display << "' has a ";
}
if (view.m_name.empty())
{
oss << "view with an empty name.";
}
else
{
oss << "view '" << view.m_name << "' ";
}
return oss;
}
static constexpr unsigned FirstSupportedMajorVersion = 1;
static constexpr unsigned LastSupportedMajorVersion = OCIO_VERSION_MAJOR;
// For each major version keep the most recent minor.
static const unsigned int LastSupportedMinorVersion[] = {0, // Version 1
5 // Version 2
};
} // namespace
class Config::Impl
{
public:
enum Validation
{
VALIDATION_UNKNOWN = 0,
VALIDATION_PASSED,
VALIDATION_FAILED
};
unsigned int m_majorVersion;
unsigned int m_minorVersion;
StringMap m_env;
ContextRcPtr m_context;
std::string m_name;
static constexpr char DefaultFamilySeparator = '/';
char m_familySeparator = DefaultFamilySeparator;
std::string m_description;
// The final list of inactive color spaces is built from several inputs.
// An API request (using Config::setInactiveColorSpaces()) always supersedes
// the list from the env. variable (i.e. OCIO_INACTIVE_COLORSPACES_ENVVAR) which
// supersedes the list from the config. file (i.e. inactive_colorspaces).
//
// Refer to Config::Impl::refreshActiveColorSpaces() to have the implementation details.
ColorSpaceSetRcPtr m_allColorSpaces; // All the color spaces (i.e. no filtering).
StringUtils::StringVec m_activeColorSpaceNames; // Active color space names.
StringUtils::StringVec m_inactiveColorSpaceNames; // inactive color space names.
// Inactive color space or named transform filter from API request.
std::string m_inactiveColorSpaceNamesAPI;
// Inactive color space or named transform filter from env. variable.
std::string m_inactiveColorSpaceNamesEnv;
// Inactive color space or named transform filter from config. file.
std::string m_inactiveColorSpaceNamesConf;
StringMap m_roles;
LookVec m_looksList;
DisplayMap m_displays;
StringUtils::StringVec m_activeDisplays;
StringUtils::StringVec m_activeDisplaysEnvOverride;
StringUtils::StringVec m_activeViews;
StringUtils::StringVec m_activeViewsEnvOverride;
ViewVec m_sharedViews;
ViewingRulesRcPtr m_viewingRules;
// List of views attached to the virtual display.
Display m_virtualDisplay;
std::vector<ViewTransformRcPtr> m_viewTransforms;
std::string m_defaultViewTransform;
mutable std::string m_activeDisplaysStr;
mutable std::string m_activeViewsStr;
mutable StringUtils::StringVec m_displayCache;
// All the named transforms(i.e. no filtering).
std::vector<ConstNamedTransformRcPtr> m_allNamedTransforms;
// Active named transform names.
StringUtils::StringVec m_activeNamedTransformNames;
// Inactive named transform names.
StringUtils::StringVec m_inactiveNamedTransformNames;
// Misc
std::vector<double> m_defaultLumaCoefs;
bool m_strictParsing;
mutable Validation m_validation;
mutable std::string m_validationtext;
mutable Mutex m_cacheidMutex;
mutable StringMap m_cacheids;
mutable std::string m_cacheidnocontext;
FileRulesRcPtr m_fileRules;
mutable ProcessorCacheFlags m_cacheFlags { PROCESSOR_CACHE_DEFAULT };
mutable ProcessorCache<std::size_t, ProcessorRcPtr> m_processorCache;
Impl() :
m_majorVersion(LastSupportedMajorVersion),
m_minorVersion(LastSupportedMinorVersion[LastSupportedMajorVersion - 1]),
m_context(Context::Create()),
m_allColorSpaces(ColorSpaceSet::Create()),
m_viewingRules(ViewingRules::Create()),
m_strictParsing(true),
m_validation(VALIDATION_UNKNOWN),
m_fileRules(FileRules::Create())
{
std::string activeDisplays;
Platform::Getenv(OCIO_ACTIVE_DISPLAYS_ENVVAR, activeDisplays);
activeDisplays = StringUtils::Trim(activeDisplays);
if (!activeDisplays.empty())
{
m_activeDisplaysEnvOverride = SplitStringEnvStyle(activeDisplays);
}
std::string activeViews;
Platform::Getenv(OCIO_ACTIVE_VIEWS_ENVVAR, activeViews);
activeViews = StringUtils::Trim(activeViews);
if (!activeViews.empty())
{
m_activeViewsEnvOverride = SplitStringEnvStyle(activeViews);
}
m_defaultLumaCoefs.resize(3);
m_defaultLumaCoefs[0] = DEFAULT_LUMA_COEFF_R;
m_defaultLumaCoefs[1] = DEFAULT_LUMA_COEFF_G;
m_defaultLumaCoefs[2] = DEFAULT_LUMA_COEFF_B;
Platform::Getenv(OCIO_INACTIVE_COLORSPACES_ENVVAR, m_inactiveColorSpaceNamesEnv);
m_inactiveColorSpaceNamesEnv = StringUtils::Trim(m_inactiveColorSpaceNamesEnv);
m_processorCache.enable((m_cacheFlags & PROCESSOR_CACHE_ENABLED) == PROCESSOR_CACHE_ENABLED);
// This is used to allow the YAML writer to not save any virtual displays that were
// instantiated.
m_virtualDisplay.m_temporary = true;
}
~Impl() = default;
Impl(const Impl&) = delete;
Impl& operator= (const Impl & rhs)
{
if(this!=&rhs)
{
m_majorVersion = rhs.m_majorVersion;
m_minorVersion = rhs.m_minorVersion;
m_env = rhs.m_env;
m_context = rhs.m_context->createEditableCopy();
m_name = rhs.m_name;
m_familySeparator = rhs.m_familySeparator;
m_description = rhs.m_description;
// Deep copy the colorspaces.
m_allColorSpaces = rhs.m_allColorSpaces->createEditableCopy();
m_activeColorSpaceNames = rhs.m_activeColorSpaceNames;
m_inactiveColorSpaceNames = rhs.m_inactiveColorSpaceNames;
m_inactiveColorSpaceNamesConf = rhs.m_inactiveColorSpaceNamesConf;
m_inactiveColorSpaceNamesEnv = rhs.m_inactiveColorSpaceNamesEnv;
m_inactiveColorSpaceNamesAPI = rhs.m_inactiveColorSpaceNamesAPI;
// Deep copy the looks.
m_looksList.clear();
m_looksList.reserve(rhs.m_looksList.size());
for (const auto & look : rhs.m_looksList)
{
m_looksList.push_back(look->createEditableCopy());
}
// Assignment operator will suffice for these.
m_roles = rhs.m_roles;
m_allNamedTransforms.clear();
m_allNamedTransforms.reserve(rhs.m_allNamedTransforms.size());
for (const auto & nt : rhs.m_allNamedTransforms)
{
m_allNamedTransforms.push_back(nt->createEditableCopy());
}
m_activeNamedTransformNames = rhs.m_activeNamedTransformNames;
m_inactiveNamedTransformNames = rhs.m_inactiveNamedTransformNames;
m_displays = rhs.m_displays;
m_activeDisplays = rhs.m_activeDisplays;
m_activeViews = rhs.m_activeViews;
m_activeViewsEnvOverride = rhs.m_activeViewsEnvOverride;
m_activeDisplaysEnvOverride = rhs.m_activeDisplaysEnvOverride;
m_activeDisplaysStr = rhs.m_activeDisplaysStr;
m_displayCache = rhs.m_displayCache;
m_viewingRules = rhs.m_viewingRules->createEditableCopy();
m_sharedViews = rhs.m_sharedViews;
m_virtualDisplay = rhs.m_virtualDisplay;
// Deep copy view transforms.
m_viewTransforms.clear();
m_viewTransforms.reserve(rhs.m_viewTransforms.size());
for (const auto & vt : rhs.m_viewTransforms)
{
m_viewTransforms.push_back(vt->createEditableCopy());
}
m_defaultViewTransform = rhs.m_defaultViewTransform;
m_defaultLumaCoefs = rhs.m_defaultLumaCoefs;
m_strictParsing = rhs.m_strictParsing;
m_validation = rhs.m_validation;
m_validationtext = rhs.m_validationtext;
m_cacheids = rhs.m_cacheids;
m_cacheidnocontext = rhs.m_cacheidnocontext;
m_fileRules = rhs.m_fileRules->createEditableCopy();
m_cacheFlags = rhs.m_cacheFlags;
m_processorCache.clear();
m_processorCache.enable((m_cacheFlags & PROCESSOR_CACHE_ENABLED) == PROCESSOR_CACHE_ENABLED);
}
return *this;
}
ConstColorSpaceRcPtr getColorSpace(const char * name) const
{
// Check to see if the name is a color space.
ConstColorSpaceRcPtr cs = m_allColorSpaces->getColorSpace(name);
if (!cs)
{
// Check to see if the name is a role.
const char * csname = LookupRole(m_roles, name);
cs = m_allColorSpaces->getColorSpace(csname);
}
return cs;
}
// Only search for a color space name (i.e. not for a role name).
int getColorSpaceIndex(const char * csname) const
{
return m_allColorSpaces->getColorSpaceIndex(csname);
}
// Only search for a color space name (i.e. not for a role name).
bool hasColorSpace(const char * csname) const
{
return m_allColorSpaces->hasColorSpace(csname);
}
ConstNamedTransformRcPtr getNamedTransform(const char * name) const noexcept
{
size_t index = getNamedTransformIndex(name);
if (index >= m_allNamedTransforms.size())
{
return ConstNamedTransformRcPtr();
}
return m_allNamedTransforms[index];
}
size_t getNamedTransformIndex(const char * name) const noexcept
{
if (name && *name)
{
const std::string str = StringUtils::Lower(name);
for (size_t idx = 0; idx < m_allNamedTransforms.size(); ++idx)
{
if (StringUtils::Lower(m_allNamedTransforms[idx]->getName()) == str)
{
return idx;
}
const auto numAliases = m_allNamedTransforms[idx]->getNumAliases();
for (size_t alias = 0; alias < numAliases; ++alias)
{
if (StringUtils::Lower(m_allNamedTransforms[idx]->getAlias(alias)) == str)
{
return idx;
}
}
}
}
return static_cast<size_t>(-1);
}
enum InactiveType
{
INACTIVE_COLORSPACE =0,
INACTIVE_NAMEDTRANSFORM,
INACTIVE_ALL
};
StringUtils::StringVec buildInactiveNamesList(InactiveType type) const;
void refreshActiveColorSpaces();
ConstViewTransformRcPtr getViewTransform(const char * name) const noexcept
{
const std::string namelower = StringUtils::Lower(name);
for (const auto & vt : m_viewTransforms)
{
if (StringUtils::Lower(vt->getName()) == namelower)
{
return vt;
}
}
return ConstViewTransformRcPtr();
}
ConstLookRcPtr getLook(const char * name) const
{
const std::string namelower = StringUtils::Lower(name);
for (const auto & look : m_looksList)
{
if (StringUtils::Lower(look->getName()) == namelower)
{
return look;
}
}
return ConstLookRcPtr();
}
ViewPtrVec getViews(const Display & display) const
{
ViewPtrVec views;
for (const auto & view : display.m_views)
{
views.push_back(&view);
}
for (const auto & shared : display.m_sharedViews)
{
ViewVec::const_iterator sharedView = FindView(m_sharedViews, shared.c_str());
if (sharedView != m_sharedViews.end())
{
const View * viewPtr = &(*sharedView);
views.push_back(viewPtr);
}
}
return views;
}
// Any time you modify the state of the config, you must call this
// to reset internal cache states. You also should do this in a
// thread safe manner by acquiring the m_cacheidMutex.
void resetCacheIDs();
// Get all internal transforms (to generate cacheIDs, validation, etc).
// This currently crawls colorspaces + looks + view transforms.
void getAllInternalTransforms(ConstTransformVec & transformVec) const;
static ConstConfigRcPtr Read(std::istream & istream, const char * filename);
static ConstConfigRcPtr Read(std::istream & istream, ConfigIOProxyRcPtr ciop);
// Validate view object that can be a config defined shared view or a display-defined view.
void validateView(const std::string & display, const View & view, bool checkUseDisplayName) const
{
if (view.m_name.empty())
{
std::ostringstream os{ GetDisplayViewPrefixErrorMsg(display, view) };
m_validationtext = os.str();
throw Exception(m_validationtext.c_str());
}
const bool sharedViewWithViewTransform = display.empty() && !view.m_viewTransform.empty();
// Validate color space name is not empty.
if (view.m_colorspace.empty())
{
std::ostringstream os{ GetDisplayViewPrefixErrorMsg(display, view) };
os << "does not refer to a color space.";
m_validationtext = os.str();
throw Exception(m_validationtext.c_str());
}
if (checkUseDisplayName)
{
// USE_DISPLAY_NAME can only be used by shared views.
if (!sharedViewWithViewTransform && view.useDisplayNameForColorspace())
{
std::ostringstream os{ GetDisplayViewPrefixErrorMsg(display, view) };
os << "can not use '" << OCIO_VIEW_USE_DISPLAY_NAME;
os << "' keyword for the color space name.";
m_validationtext = os.str();
throw Exception(m_validationtext.c_str());
}
}
// If USE_DISPLAY_NAME is not present, a valid color space must be specified.
if (!view.useDisplayNameForColorspace() &&
!hasColorSpace(view.m_colorspace.c_str()) &&
!getNamedTransform(view.m_colorspace.c_str()))
{
std::ostringstream os{ GetDisplayViewPrefixErrorMsg(display, view) };
os << "that refers to a color space or a named transform, '" << view.m_colorspace;
os << "', which is not defined.";
m_validationtext = os.str();
throw Exception(m_validationtext.c_str());
}
// If there is a view transform, it must exist (or be a named transform) and its color
// space must be a display-referred color space.
if (!view.m_viewTransform.empty())
{
if (!getNamedTransform(view.m_viewTransform.c_str()))
{
if (!getViewTransform(view.m_viewTransform.c_str()))
{
std::ostringstream os{ GetDisplayViewPrefixErrorMsg(display, view) };
os << "that refers to a view transform, '" << view.m_viewTransform << "', ";
os << "which is neither a view transform nor a named transform.";
m_validationtext = os.str();
throw Exception(m_validationtext.c_str());
}
}
const char * displayCS = view.m_colorspace.c_str();
if (view.useDisplayNameForColorspace())
{
displayCS = display.c_str();
}
auto cs = getColorSpace(displayCS);
if (cs && cs->getReferenceSpaceType() != REFERENCE_SPACE_DISPLAY)
{
std::ostringstream os{ GetDisplayViewPrefixErrorMsg(display, view) };
os << "refers to a color space, '" << std::string(displayCS) << "', ";
os << "that is not a display-referred color space.";
m_validationtext = os.str();
throw Exception(m_validationtext.c_str());
}
}
// Confirm looks references exist.
LookParseResult looks;
const LookParseResult::Options & options = looks.parse(view.m_looks);
for (const auto & option : options)
{
for (const auto & token : option)
{
const std::string look = token.name;
if (!look.empty() && !getLook(look.c_str()))
{
std::ostringstream os{ GetDisplayViewPrefixErrorMsg(display, view) };
os << "refers to a look, '" << look << "', ";
os << "which is not defined.";
m_validationtext = os.str();
throw Exception(m_validationtext.c_str());
}
}
}
if (!view.m_rule.empty())
{
size_t ruleIndex{ 0 };
if (!FindRule(m_viewingRules, view.m_rule, ruleIndex))
{
std::ostringstream os{ GetDisplayViewPrefixErrorMsg(display, view) };
os << "refers to a viewing rule, '" << view.m_rule << "', ";
os << "which is not defined.";
m_validationtext = os.str();
throw Exception(m_validationtext.c_str());
}
}
}
// Check a shared view used in a display. View itself has already been checked.
void validateSharedView(const std::string & display,
const ViewVec & viewsOfDisplay,
const std::string & sharedView,
bool checkUseDisplayName) const
{
// Is the name already used for a display-defined view?
// This should never happen because this is checked when adding a view.
const auto viewIt = FindView(viewsOfDisplay, sharedView);
if (viewIt != viewsOfDisplay.end())
{
std::ostringstream os;
os << "Config failed view validation. ";
os << "The display '" << display << "' ";
os << "contains a shared view '" << sharedView;
os << "' that is already defined as a view.";
m_validationtext = os.str();
throw Exception(m_validationtext.c_str());
}
// Is the shared view defined?
const auto sharedViewIt = FindView(m_sharedViews, sharedView);
if (sharedViewIt == m_sharedViews.end())
{
std::ostringstream os;
os << "Config failed view validation. ";
os << "The display '" << display << "' ";
os << "contains a shared view '" << sharedView;
os << "' that is not defined.";
m_validationtext = os.str();
throw Exception(m_validationtext.c_str());
}
else if (checkUseDisplayName)
{
const auto view = *sharedViewIt;
if (!view.m_viewTransform.empty() && view.useDisplayNameForColorspace())
{
// Shared views using a view transform can omit the colorspace, in that case
// the color space to use should be named from the display.
const auto displayCS = getColorSpace(display.c_str());
if (!displayCS)
{
std::ostringstream os;
os << "Config failed view validation. The display '" << display << "' ";
os << "contains a shared view '" << (*sharedViewIt).m_name;
os << "' which does not define a color space and there is "
"no color space that matches the display name.";
m_validationtext = os.str();
throw Exception(m_validationtext.c_str());
}
if (displayCS->getReferenceSpaceType() != REFERENCE_SPACE_DISPLAY)
{
std::ostringstream os;
os << "Config failed view validation. The display '" << display << "' ";
os << "contains a shared view '" << (*sharedViewIt).m_name;
os << "' that refers to a color space, '" << display << "', ";
os << "that is not a display-referred color space.";
m_validationtext = os.str();
throw Exception(m_validationtext.c_str());
}
}
}
}
void setInactiveColorSpaces(const char * inactiveColorSpaces)
{
m_inactiveColorSpaceNamesConf
= StringUtils::Trim(std::string(inactiveColorSpaces ? inactiveColorSpaces : ""));
// An API request must always supersede the two other lists. Filling the
// m_inactiveColorSpaceNamesAPI list highlights the API request precedence.
m_inactiveColorSpaceNamesAPI = m_inactiveColorSpaceNamesConf;
AutoMutex lock(m_cacheidMutex);
resetCacheIDs();
refreshActiveColorSpaces();
}
void checkVersionConsistency(ConstTransformRcPtr & transform) const;
void checkVersionConsistency() const;
const View * getView(const char * display, const char * view) const
{
if (!view || !*view) return nullptr;
bool searchShared = !display || !*display;
DisplayMap::const_iterator iter = m_displays.end();
if (!searchShared)
{
iter = FindDisplay(m_displays, display);
if (iter == m_displays.end()) return nullptr;
const StringUtils::StringVec & sharedViews = iter->second.m_sharedViews;
searchShared = StringUtils::Contain(sharedViews, view);
}
const ViewVec & views = searchShared ? m_sharedViews : iter->second.m_views;
const auto & viewIt = FindView(views, view);
if (viewIt != views.end())
{
return &(*viewIt);
}
return nullptr;
}
// Filter/reorder views using the active views if defined.
StringUtils::StringVec getActiveViews(const StringUtils::StringVec & views) const
{
StringUtils::StringVec activeViews;
if (!m_activeViewsEnvOverride.empty())
{
const StringUtils::StringVec orderedViews
= IntersectStringVecsCaseIgnore(m_activeViewsEnvOverride, views);
if (!orderedViews.empty())
{
activeViews = orderedViews;
}
}
else if (!m_activeViews.empty())
{
const StringUtils::StringVec orderedViews
= IntersectStringVecsCaseIgnore(m_activeViews, views);
if (!orderedViews.empty())
{
activeViews = orderedViews;
}
}
if (activeViews.empty())
{
activeViews = views;
}
return activeViews;
}
// Get all active views from a display with a rule that refers to color space or an encoding
// referred by color space. Note that views that do not have viewing rules are all returned.
// ViewNames, created from views, is returned as parameter.
StringUtils::StringVec getFilteredViews(StringUtils::StringVec & viewNames,
const ViewPtrVec & views,
const char * imageCSName) const
{
ConstColorSpaceRcPtr imageColorSpace = getColorSpace(imageCSName);
if (!imageColorSpace)
{
std::ostringstream os;
os << "Could not find source color space '" << imageCSName << "'.";
throw Exception(os.str().c_str());
}
const std::string viewEncoding{ imageColorSpace->getEncoding() };
viewNames = GetViewNames(views);
StringUtils::StringVec activeViews = getActiveViews(viewNames);
const std::string imageColorSpaceName{ StringUtils::Lower(imageCSName) };
StringUtils::StringVec filteredActiveViews;
for (const auto & view : activeViews)
{
const auto idx = FindInStringVecCaseIgnore(viewNames, view);
const auto & ruleName = views[idx]->m_rule;
size_t ruleIdx{ 0 };
if (ruleName.empty())
{
// Include all views that do not have a rule.
filteredActiveViews.push_back(view);
}
else if (FindRule(m_viewingRules, ruleName, ruleIdx))
{
const size_t numcs = m_viewingRules->getNumColorSpaces(ruleIdx);
bool added = false;
for (size_t csIdx = 0; csIdx < numcs; ++csIdx)
{
// Rule can use role names.
const char * rolename = m_viewingRules->getColorSpace(ruleIdx, csIdx);
const char * csname = LookupRole(m_roles, rolename);
const std::string csName{ *csname ? csname : rolename };
if (StringUtils::Lower(csName) == imageColorSpaceName)
{
// Include a view if its rule contains the image's color space.
filteredActiveViews.push_back(view);
added = true;
break;
}
}
if (!added && !viewEncoding.empty())
{
const size_t numEnc = m_viewingRules->getNumEncodings(ruleIdx);
for (size_t encIdx = 0; encIdx < numEnc; ++encIdx)
{
const std::string encName{ m_viewingRules->getEncoding(ruleIdx, encIdx) };
if (StringUtils::Lower(encName) == viewEncoding)
{
// Include a view if its rule contains the image's color space encoding.
filteredActiveViews.push_back(view);
break;
}
}
}
}
}
return filteredActiveViews;
}
void updateDisplayCache() const
{
if (m_displayCache.empty())
{
ComputeDisplays(m_displayCache,
m_displays,
m_activeDisplays,
m_activeDisplaysEnvOverride);
}
}
ProcessorCacheFlags getProcessorCacheFlags() const noexcept
{
return m_cacheFlags;
}
void setProcessorCacheFlags(ProcessorCacheFlags flags) const noexcept
{
m_cacheFlags = flags;
m_processorCache.enable((m_cacheFlags & PROCESSOR_CACHE_ENABLED) == PROCESSOR_CACHE_ENABLED);
}
ConstProcessorRcPtr getProcessorWithoutCaching(
const Config & config,
const ConstTransformRcPtr & transform,
TransformDirection direction) const
{
if (!transform)
{
throw Exception("Config::GetProcessor failed. Transform is null.");
}
ProcessorRcPtr processor = Processor::Create();
processor->getImpl()->setProcessorCacheFlags(PROCESSOR_CACHE_OFF);
processor->getImpl()->setTransform(config, m_context, transform, direction);
return processor;
}
int instantiateDisplay(const std::string & monitorName,
const std::string & monitorDescription,
const std::string & ICCProfileFilepath)
{
if (ICCProfileFilepath.empty())
{
throw Exception("The ICC Profile filepath cannot be null.");
}
if (monitorDescription.empty())
{
throw Exception("The monitor description cannot be null.");
}
std::string envContent;
if (Platform::Getenv(OCIO_ACTIVE_DISPLAYS_ENVVAR, envContent))
{
std::ostringstream oss;
oss << "Cannot instantiate a virtual display because the list of active displays is "
<< "defined by "
<< OCIO_ACTIVE_DISPLAYS_ENVVAR
<< " = "
<< envContent
<< ".";
throw Exception(oss.str().c_str());
}
std::string colorSpaceName = monitorDescription;
// The monitor name is null if the display is instantiated from an ICC filepath.
if (!monitorName.empty())
{
colorSpaceName += " [" + monitorName + "]";
}
// The $ is forbidden for color space names i.e. reserved for context variables.
StringUtils::ReplaceInPlace(colorSpaceName, "$", "_");
// The % is forbidden for color space names i.e. reserved for context variables.
StringUtils::ReplaceInPlace(colorSpaceName, "%", "_");
// Check if the virtual display definition is present.
if (m_virtualDisplay.m_views.size() == 0
&& m_virtualDisplay.m_sharedViews.size() == 0)
{
throw Exception("The virtual display information to instantiate a display is missing.");
}
int absoluteDisplayIndex = -1;
try
{
// Create the (display, view) pairs with the display color space implemented using