@@ -202,6 +202,27 @@ Oiiotool::clear_options()
202
202
203
203
204
204
205
+ ColorConfig&
206
+ Oiiotool::colorconfig ()
207
+ {
208
+ // It's safe to check the pointer and if it exists, return it, since
209
+ // once it's created, it will never be changed.
210
+ if (ColorConfig* cc = m_colorconfig.get ())
211
+ return *cc;
212
+
213
+ // Otherwise, we need to create it. But we need to be thread-safe.
214
+ static std::mutex colorconfig_mutex;
215
+ std::lock_guard lock (colorconfig_mutex);
216
+ if (!m_colorconfig) {
217
+ if (debug)
218
+ print (" oiiotool Creating ColorConfig\n " );
219
+ m_colorconfig.reset (new ColorConfig);
220
+ }
221
+ return *m_colorconfig.get ();
222
+ }
223
+
224
+
225
+
205
226
void
206
227
Oiiotool::clear_input_config ()
207
228
{
@@ -2192,9 +2213,9 @@ static void
2192
2213
set_colorconfig (Oiiotool& ot, cspan<const char *> argv)
2193
2214
{
2194
2215
OIIO_DASSERT (argv.size () == 2 );
2195
- ot.colorconfig .reset (argv[1 ]);
2196
- if (ot.colorconfig .has_error ()) {
2197
- ot.errorfmt (" --colorconfig" , " {}" , ot.colorconfig .geterror ());
2216
+ ot.colorconfig () .reset (argv[1 ]);
2217
+ if (ot.colorconfig () .has_error ()) {
2218
+ ot.errorfmt (" --colorconfig" , " {}" , ot.colorconfig () .geterror ());
2198
2219
}
2199
2220
}
2200
2221
@@ -2279,7 +2300,7 @@ class OpColorConvert final : public OiiotoolOp {
2279
2300
}
2280
2301
bool ok = ImageBufAlgo::colorconvert (*img[0 ], *img[1 ], fromspace,
2281
2302
tospace, unpremult, contextkey,
2282
- contextvalue, &ot.colorconfig );
2303
+ contextvalue, &ot.colorconfig () );
2283
2304
if (!ok && !strict) {
2284
2305
// The color transform failed, but we were told not to be
2285
2306
// strict, so ignore the error and just copy destination to
@@ -2354,7 +2375,7 @@ OIIOTOOL_OP(ociolook, 1, [&](OiiotoolOp& op, span<ImageBuf*> img) {
2354
2375
tospace = img[1 ]->spec ().get_string_attribute (" oiio:Colorspace" );
2355
2376
return ImageBufAlgo::ociolook (*img[0 ], *img[1 ], lookname, fromspace,
2356
2377
tospace, unpremult, inverse, contextkey,
2357
- contextvalue, &ot.colorconfig );
2378
+ contextvalue, &ot.colorconfig () );
2358
2379
});
2359
2380
2360
2381
@@ -2373,7 +2394,8 @@ OIIOTOOL_OP(ociodisplay, 1, [&](OiiotoolOp& op, span<ImageBuf*> img) {
2373
2394
fromspace = img[1 ]->spec ().get_string_attribute (" oiio:Colorspace" );
2374
2395
return ImageBufAlgo::ociodisplay (*img[0 ], *img[1 ], displayname, viewname,
2375
2396
fromspace, looks, unpremult, inverse,
2376
- contextkey, contextvalue, &ot.colorconfig );
2397
+ contextkey, contextvalue,
2398
+ &ot.colorconfig ());
2377
2399
});
2378
2400
2379
2401
@@ -2384,7 +2406,7 @@ OIIOTOOL_OP(ociofiletransform, 1, [&](OiiotoolOp& op, span<ImageBuf*> img) {
2384
2406
bool inverse = op.options ().get_int (" inverse" );
2385
2407
bool unpremult = op.options ().get_int (" unpremult" );
2386
2408
return ImageBufAlgo::ociofiletransform (*img[0 ], *img[1 ], name, unpremult,
2387
- inverse, &ot.colorconfig );
2409
+ inverse, &ot.colorconfig () );
2388
2410
});
2389
2411
2390
2412
@@ -2398,7 +2420,7 @@ OIIOTOOL_OP(ocionamedtransform, 1, [&](OiiotoolOp& op, span<ImageBuf*> img) {
2398
2420
bool inverse = op.options ().get_int (" inverse" );
2399
2421
return ImageBufAlgo::ocionamedtransform (*img[0 ], *img[1 ], name, unpremult,
2400
2422
inverse, contextkey, contextvalue,
2401
- &ot.colorconfig );
2423
+ &ot.colorconfig () );
2402
2424
});
2403
2425
2404
2426
@@ -5161,7 +5183,7 @@ input_file(Oiiotool& ot, cspan<const char*> argv)
5161
5183
if (autocc) {
5162
5184
// Try to deduce the color space it's in
5163
5185
std::string colorspace (
5164
- ot.colorconfig .getColorSpaceFromFilepath (filename));
5186
+ ot.colorconfig () .getColorSpaceFromFilepath (filename));
5165
5187
if (colorspace.size () && ot.debug )
5166
5188
print (" From {}, we deduce color space \" {}\"\n " , filename,
5167
5189
colorspace);
@@ -5173,9 +5195,9 @@ input_file(Oiiotool& ot, cspan<const char*> argv)
5173
5195
print (" Metadata of {} indicates color space \" {}\"\n " ,
5174
5196
colorspace, filename);
5175
5197
}
5176
- std::string linearspace = ot.colorconfig .resolve (" linear" );
5198
+ std::string linearspace = ot.colorconfig () .resolve (" linear" );
5177
5199
if (colorspace.size ()
5178
- && !ot.colorconfig .equivalent (colorspace, linearspace)) {
5200
+ && !ot.colorconfig () .equivalent (colorspace, linearspace)) {
5179
5201
std::string cmd = " colorconvert:strict=0" ;
5180
5202
if (autoccunpremult)
5181
5203
cmd += " :unpremult=1" ;
@@ -5459,12 +5481,12 @@ output_file(Oiiotool& ot, cspan<const char*> argv)
5459
5481
// automatically set -d based on the name if --autocc is used.
5460
5482
bool autocc = fileoptions.get_int (" autocc" , ot.autocc );
5461
5483
bool autoccunpremult = fileoptions.get_int (" unpremult" , ot.autoccunpremult );
5462
- std::string outcolorspace = ot.colorconfig .getColorSpaceFromFilepath (
5484
+ std::string outcolorspace = ot.colorconfig () .getColorSpaceFromFilepath (
5463
5485
filename);
5464
5486
if (autocc && outcolorspace.size ()) {
5465
5487
TypeDesc type;
5466
5488
int bits;
5467
- type = ot.colorconfig .getColorSpaceDataType (outcolorspace, &bits);
5489
+ type = ot.colorconfig () .getColorSpaceDataType (outcolorspace, &bits);
5468
5490
if (type.basetype != TypeDesc::UNKNOWN) {
5469
5491
if (ot.debug )
5470
5492
std::cout << " Deduced data type " << type << " (" << bits
@@ -5482,7 +5504,7 @@ output_file(Oiiotool& ot, cspan<const char*> argv)
5482
5504
}
5483
5505
}
5484
5506
if (autocc) {
5485
- string_view linearspace = ot.colorconfig .resolve (" linear" );
5507
+ string_view linearspace = ot.colorconfig () .resolve (" linear" );
5486
5508
std::string currentspace
5487
5509
= ir->spec ()->get_string_attribute (" oiio:ColorSpace" , linearspace);
5488
5510
// Special cases where we know formats should be particular color
@@ -5978,66 +6000,66 @@ print_ocio_info(Oiiotool& ot, std::ostream& out)
5978
6000
using Strutil::print;
5979
6001
int columns = Sysutil::terminal_columns () - 1 ;
5980
6002
5981
- int ociover = ot.colorconfig . OpenColorIO_version_hex ();
5982
- if (ociover)
6003
+ ColorConfig& colorconfig = ot.colorconfig ();
6004
+ if (int ociover = colorconfig. OpenColorIO_version_hex () )
5983
6005
out << " OpenColorIO " << (ociover >> 24 ) << ' .'
5984
6006
<< ((ociover >> 16 ) & 0xff ) << ' .' << ((ociover >> 8 ) & 0xff );
5985
6007
else
5986
6008
out << " No OpenColorIO" ;
5987
- out << " \n Color config: " << ot. colorconfig .configname () << " \n " ;
6009
+ out << " \n Color config: " << colorconfig.configname () << " \n " ;
5988
6010
out << " Known color spaces: \n " ;
5989
- const char * linear = ot. colorconfig .getColorSpaceNameByRole (" linear" );
5990
- for (int i = 0 , e = ot. colorconfig .getNumColorSpaces (); i < e; ++i) {
5991
- const char * n = ot. colorconfig .getColorSpaceNameByIndex (i);
6011
+ const char * linear = colorconfig.getColorSpaceNameByRole (" linear" );
6012
+ for (int i = 0 , e = colorconfig.getNumColorSpaces (); i < e; ++i) {
6013
+ const char * n = colorconfig.getColorSpaceNameByIndex (i);
5992
6014
out << " - " << quote_if_spaces (n);
5993
- if ((linear && !ot. colorconfig .equivalent (n, " linear" )
5994
- && ot. colorconfig .equivalent (n, linear))
5995
- || ot. colorconfig .isColorSpaceLinear (n))
6015
+ if ((linear && !colorconfig.equivalent (n, " linear" )
6016
+ && colorconfig.equivalent (n, linear))
6017
+ || colorconfig.isColorSpaceLinear (n))
5996
6018
out << " (linear)" ;
5997
6019
out << " \n " ;
5998
- auto aliases = ot. colorconfig .getAliases (n);
6020
+ auto aliases = colorconfig.getAliases (n);
5999
6021
if (aliases.size ()) {
6000
6022
std::stringstream s;
6001
6023
s << " aliases: " << join_with_quotes (aliases, " , " );
6002
6024
out << Strutil::wordwrap (s.str (), columns, 6 ) << " \n " ;
6003
6025
}
6004
6026
}
6005
6027
6006
- int roles = ot. colorconfig .getNumRoles ();
6028
+ int roles = colorconfig.getNumRoles ();
6007
6029
if (roles) {
6008
6030
print (out, " Known roles:\n " );
6009
6031
for (int i = 0 ; i < roles; ++i) {
6010
- const char * r = ot. colorconfig .getRoleByIndex (i);
6032
+ const char * r = colorconfig.getRoleByIndex (i);
6011
6033
print (out, " - {} -> {}\n " , quote_if_spaces (r),
6012
- quote_if_spaces (ot. colorconfig .getColorSpaceNameByRole (r)));
6034
+ quote_if_spaces (colorconfig.getColorSpaceNameByRole (r)));
6013
6035
}
6014
6036
}
6015
6037
6016
- int nlooks = ot. colorconfig .getNumLooks ();
6038
+ int nlooks = colorconfig.getNumLooks ();
6017
6039
if (nlooks) {
6018
6040
print (out, " Known looks:\n " );
6019
6041
for (int i = 0 ; i < nlooks; ++i)
6020
6042
print (out, " - {}\n " ,
6021
- quote_if_spaces (ot. colorconfig .getLookNameByIndex (i)));
6043
+ quote_if_spaces (colorconfig.getLookNameByIndex (i)));
6022
6044
}
6023
6045
6024
- const char * default_display = ot. colorconfig .getDefaultDisplayName ();
6025
- int ndisplays = ot. colorconfig .getNumDisplays ();
6046
+ const char * default_display = colorconfig.getDefaultDisplayName ();
6047
+ int ndisplays = colorconfig.getNumDisplays ();
6026
6048
if (ndisplays) {
6027
6049
out << " Known displays: (* indicates default)\n " ;
6028
6050
for (int i = 0 ; i < ndisplays; ++i) {
6029
- const char * d = ot. colorconfig .getDisplayNameByIndex (i);
6051
+ const char * d = colorconfig.getDisplayNameByIndex (i);
6030
6052
out << " - " << quote_if_spaces (d);
6031
6053
if (!strcmp (d, default_display))
6032
6054
out << " (*)" ;
6033
- const char * default_view = ot. colorconfig .getDefaultViewName (d);
6034
- int nviews = ot. colorconfig .getNumViews (d);
6055
+ const char * default_view = colorconfig.getDefaultViewName (d);
6056
+ int nviews = colorconfig.getNumViews (d);
6035
6057
if (nviews) {
6036
6058
out << " \n " ;
6037
6059
std::stringstream s;
6038
6060
s << " views: " ;
6039
6061
for (int i = 0 ; i < nviews; ++i) {
6040
- const char * v = ot. colorconfig .getViewNameByIndex (d, i);
6062
+ const char * v = colorconfig.getViewNameByIndex (d, i);
6041
6063
s << quote_if_spaces (v);
6042
6064
if (!strcmp (v, default_view))
6043
6065
s << " (*)" ;
@@ -6050,15 +6072,15 @@ print_ocio_info(Oiiotool& ot, std::ostream& out)
6050
6072
}
6051
6073
}
6052
6074
6053
- int nnamed_transforms = ot. colorconfig .getNumNamedTransforms ();
6075
+ int nnamed_transforms = colorconfig.getNumNamedTransforms ();
6054
6076
if (nnamed_transforms) {
6055
6077
out << " Named transforms:\n " ;
6056
6078
for (int i = 0 ; i < nnamed_transforms; ++i) {
6057
- const char * x = ot. colorconfig .getNamedTransformNameByIndex (i);
6079
+ const char * x = colorconfig.getNamedTransformNameByIndex (i);
6058
6080
out << " - " << quote_if_spaces (x) << " \n " ;
6059
6081
}
6060
6082
}
6061
- if (!ot. colorconfig .supportsOpenColorIO ())
6083
+ if (!colorconfig.supportsOpenColorIO ())
6062
6084
out << " No OpenColorIO support was enabled at build time.\n " ;
6063
6085
}
6064
6086
@@ -6115,12 +6137,12 @@ print_help_end(Oiiotool& ot, std::ostream& out)
6115
6137
out << formatted_format_list (" Input" , " input_format_list" ) << " \n " ;
6116
6138
out << formatted_format_list (" Output" , " output_format_list" ) << " \n " ;
6117
6139
6118
- if (int ociover = ot.colorconfig .OpenColorIO_version_hex ())
6140
+ if (int ociover = ot.colorconfig () .OpenColorIO_version_hex ())
6119
6141
print (out, " OpenColorIO {}.{}.{}\n " , (ociover >> 24 ),
6120
6142
((ociover >> 16 ) & 0xff ), ((ociover >> 8 ) & 0xff ));
6121
6143
else
6122
6144
print (out, " No OpenColorIO\n " );
6123
- print (out, " Color config: {}\n " , ot.colorconfig .configname ());
6145
+ print (out, " Color config: {}\n " , ot.colorconfig () .configname ());
6124
6146
print (out, " Run `oiiotool --colorconfiginfo` for a "
6125
6147
" full color management inventory.\n " );
6126
6148
0 commit comments