-
Notifications
You must be signed in to change notification settings - Fork 492
Expand file tree
/
Copy pathOp.cpp
More file actions
executable file
·621 lines (538 loc) · 15.7 KB
/
Op.cpp
File metadata and controls
executable file
·621 lines (538 loc) · 15.7 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
// SPDX-License-Identifier: BSD-3-Clause
// Copyright Contributors to the OpenColorIO Project.
#include <cstring>
#include <sstream>
#include <OpenColorIO/OpenColorIO.h>
#include "Logging.h"
#include "Op.h"
#include "ops/cdl/CDLOp.h"
#include "ops/exponent/ExponentOp.h"
#include "ops/exposurecontrast/ExposureContrastOp.h"
#include "ops/fixedfunction/FixedFunctionOp.h"
#include "ops/gamma/GammaOp.h"
#include "ops/gradingprimary/GradingPrimaryOp.h"
#include "ops/gradingrgbcurve/GradingRGBCurveOp.h"
#include "ops/gradinghuecurve/GradingHueCurveOp.h"
#include "ops/gradingtone/GradingToneOp.h"
#include "ops/log/LogOp.h"
#include "ops/lut1d/Lut1DOp.h"
#include "ops/lut3d/Lut3DOp.h"
#include "ops/range/RangeOp.h"
#include "utils/StringUtils.h"
namespace OCIO_NAMESPACE
{
bool OpCPU::isDynamic() const
{
return false;
}
bool OpCPU::hasDynamicProperty(DynamicPropertyType /* type */) const
{
return false;
}
DynamicPropertyRcPtr OpCPU::getDynamicProperty(DynamicPropertyType /* type */) const
{
throw Exception("Op does not implement dynamic property.");
}
OpData::OpData()
: m_metadata()
{ }
OpData::OpData(const OpData & rhs)
: m_metadata()
{
*this = rhs;
}
OpData & OpData::operator=(const OpData & rhs)
{
if (this != &rhs)
{
m_metadata = rhs.m_metadata;
}
return *this;
}
OpDataRcPtr OpData::getIdentityReplacement() const
{
return std::make_shared<MatrixOpData>();
}
void OpData::getSimplerReplacement(OpDataVec & /* ops */) const
{
}
bool OpData::equals(const OpData & other) const
{
if (this == &other) return true;
// Ignore metadata.
return getType() == other.getType();
}
const std::string & OpData::getID() const
{
return m_metadata.getAttributeValueString(METADATA_ID);
}
void OpData::setID(const std::string & id)
{
return m_metadata.setID(id.c_str());
}
const std::string & OpData::getName() const
{
return m_metadata.getAttributeValueString(METADATA_NAME);
}
void OpData::setName(const std::string & name)
{
return m_metadata.setName(name.c_str());
}
bool operator==(const OpData & lhs, const OpData & rhs)
{
return lhs.equals(rhs);
}
const char * GetTypeName(OpData::Type type)
{
switch (type)
{
case OpData::CDLType:
return "CDL";
case OpData::ExponentType:
return "Exponent";
case OpData::ExposureContrastType:
return "ExposureContrast";
case OpData::FixedFunctionType:
return "FixedFunction";
case OpData::GammaType:
return "Gamma";
case OpData::GradingPrimaryType:
return "GradingPrimary";
case OpData::GradingRGBCurveType:
return "GradingRGBCurve";
case OpData::GradingHueCurveType :
return "GradingHueCurve";
case OpData::GradingToneType:
return "GradingTone";
case OpData::LogType:
return "Log";
case OpData::Lut1DType:
return "LUT1D";
case OpData::Lut3DType:
return "LUT3D";
case OpData::MatrixType:
return "Matrix";
case OpData::RangeType:
return "Range";
case OpData::ReferenceType:
case OpData::NoOpType:
break;
}
throw Exception("Unexpected op type.");
}
bool Op::canCombineWith(ConstOpRcPtr & /*op*/) const
{
return false;
}
void Op::combineWith(OpRcPtrVec & /*ops*/, ConstOpRcPtr & /*secondOp*/) const
{
std::ostringstream os;
os << "Op: " << getInfo() << " cannot be combined. ";
os << "A type-specific combining function is not defined.";
throw Exception(os.str().c_str());
}
void Op::validate() const
{
m_data->validate();
}
bool Op::isDynamic() const
{
return false;
}
bool Op::hasDynamicProperty(DynamicPropertyType /* type */) const
{
return false;
}
DynamicPropertyRcPtr Op::getDynamicProperty(DynamicPropertyType /* type */) const
{
throw Exception("Op does not implement dynamic property.");
}
OpRcPtr Op::getIdentityReplacement() const
{
auto opData = m_data->getIdentityReplacement();
OpRcPtrVec ops;
if (opData->getType() == OpData::MatrixType)
{
// No-op that will be optimized.
auto mat = std::dynamic_pointer_cast<MatrixOpData>(opData);
CreateMatrixOp(ops, mat, TRANSFORM_DIR_FORWARD);
}
else if (opData->getType() == OpData::RangeType)
{
// Clamping op.
auto range = std::dynamic_pointer_cast<RangeOpData>(opData);
CreateRangeOp(ops, range, TRANSFORM_DIR_FORWARD);
}
else
{
std::ostringstream oss;
oss << "Unexpected type in getIdentityReplacement. Expecting Matrix or Range, got :"
<< std::string(GetTypeName(opData->getType())) << ".";
throw Exception(oss.str().c_str());
}
return ops[0];
}
void Op::getSimplerReplacement(OpRcPtrVec & ops) const
{
OpDataVec opDataVec;
m_data->getSimplerReplacement(opDataVec);
for (const auto & opData : opDataVec)
{
CreateOpVecFromOpData(ops, opData, TRANSFORM_DIR_FORWARD);
}
}
OpRcPtrVec::OpRcPtrVec()
: m_metadata()
{
}
OpRcPtrVec::OpRcPtrVec(const OpRcPtrVec & v)
: OpRcPtrVec()
{
*this = v;
}
OpRcPtrVec & OpRcPtrVec::operator=(const OpRcPtrVec & v)
{
if(this!=&v)
{
m_ops = v.m_ops;
m_metadata = v.m_metadata;
}
return *this;
}
OpRcPtrVec & OpRcPtrVec::operator+=(const OpRcPtrVec & v)
{
if (this != &v)
{
m_ops.insert(end(), v.begin(), v.end());
m_metadata.combine(v.m_metadata);
return *this;
}
else
{
OpRcPtrVec other = v;
return operator+=(other);
}
}
OpRcPtrVec::iterator OpRcPtrVec::erase(OpRcPtrVec::const_iterator position)
{
return m_ops.erase(position);
}
OpRcPtrVec::iterator OpRcPtrVec::erase(OpRcPtrVec::const_iterator first,
OpRcPtrVec::const_iterator last)
{
return m_ops.erase(first, last);
}
void OpRcPtrVec::insert(OpRcPtrVec::const_iterator position,
OpRcPtrVec::const_iterator first,
OpRcPtrVec::const_iterator last)
{
m_ops.insert(position, first, last);
}
void OpRcPtrVec::push_back(const OpRcPtrVec::value_type & val)
{
m_ops.push_back(val);
}
OpRcPtrVec::const_reference OpRcPtrVec::back() const
{
return m_ops.back();
}
OpRcPtrVec::const_reference OpRcPtrVec::front() const
{
return m_ops.front();
}
bool OpRcPtrVec::isNoOp() const noexcept
{
for (const auto & op : m_ops)
{
if(!op->isNoOp()) return false;
}
return true;
}
bool OpRcPtrVec::hasChannelCrosstalk() const noexcept
{
return m_ops.end() != std::find_if(m_ops.begin(),
m_ops.end(),
[](const OpRcPtr & op) { return op->hasChannelCrosstalk(); } );
}
bool OpRcPtrVec::isDynamic() const noexcept
{
return m_ops.end() != std::find_if(m_ops.begin(),
m_ops.end(),
[](const OpRcPtr & op) { return op->isDynamic(); } );
}
bool OpRcPtrVec::hasDynamicProperty(DynamicPropertyType type) const noexcept
{
return m_ops.end() != std::find_if(m_ops.begin(),
m_ops.end(),
[type](const OpRcPtr & op) { return op->hasDynamicProperty(type); } );
}
DynamicPropertyRcPtr OpRcPtrVec::getDynamicProperty(DynamicPropertyType type) const
{
for (const auto & op : m_ops)
{
if (op->hasDynamicProperty(type))
{
return op->getDynamicProperty(type);
}
}
throw Exception("Cannot find dynamic property.");
}
OpRcPtrVec OpRcPtrVec::clone() const
{
OpRcPtrVec cloned;
for (const auto & op : m_ops)
{
cloned.push_back(op->clone());
}
return cloned;
}
OpRcPtrVec OpRcPtrVec::invert() const
{
OpRcPtrVec inverted;
OpRcPtrVec::const_reverse_iterator iter = m_ops.rbegin();
OpRcPtrVec::const_reverse_iterator end = m_ops.rend();
for (; iter!=end; ++iter)
{
ConstOpRcPtr op = *iter;
if (op->isNoOpType())
{
// Keep track of the information.
inverted.push_back(op->clone());
}
else
{
ConstOpDataRcPtr data = op->data();
CreateOpVecFromOpData(inverted, data, TRANSFORM_DIR_INVERSE);
}
}
return inverted;
}
void OpRcPtrVec::validate() const
{
for (auto & op : m_ops)
{
op->validate();
}
}
namespace
{
template<typename T>
void ValidateDynamicProperty(OpRcPtr op, std::shared_ptr<T> & prop, DynamicPropertyType type)
{
if (op->hasDynamicProperty(type))
{
if (!prop)
{
// Initialize property.
DynamicPropertyRcPtr dp = op->getDynamicProperty(type);
prop = OCIO_DYNAMIC_POINTER_CAST<T>(dp);
}
else
{
// If the property is already initialized, it means that it is already being used
// elsewhere in this OpVec.
std::ostringstream os;
switch (type)
{
case DYNAMIC_PROPERTY_EXPOSURE:
os << "Exposure";
break;
case DYNAMIC_PROPERTY_CONTRAST:
os << "Contrast";
break;
case DYNAMIC_PROPERTY_GAMMA:
os << "Gamma";
break;
case DYNAMIC_PROPERTY_GRADING_PRIMARY:
os << "Grading primary";
break;
case DYNAMIC_PROPERTY_GRADING_RGBCURVE:
os << "Grading RGB curve";
break;
case DYNAMIC_PROPERTY_GRADING_TONE:
os << "Grading tone";
break;
case DYNAMIC_PROPERTY_GRADING_HUECURVE:
os << "Grading hue curve";
break;
}
os << " dynamic property can only be there once.";
LogWarning(os.str());
}
}
}
}
// Warn if there is more than one property of a given type that is currently dynamic. There may
// be more than one property of a given type, but only one will respond to parameter updates, the
// others will use their original parameter values.
void OpRcPtrVec::validateDynamicProperties()
{
// Empty shared pointers.
DynamicPropertyDoubleImplRcPtr dpExposure;
DynamicPropertyDoubleImplRcPtr dpContrast;
DynamicPropertyDoubleImplRcPtr dpGamma;
DynamicPropertyGradingPrimaryImplRcPtr dpGradingPrimary;
DynamicPropertyGradingRGBCurveImplRcPtr dpGradingRGBCurve;
DynamicPropertyGradingHueCurveImplRcPtr dpGradingHueCurve;
DynamicPropertyGradingToneImplRcPtr dpGradingTone;
for (auto op : m_ops)
{
// Each property can only be there once.
ValidateDynamicProperty(op, dpExposure, DYNAMIC_PROPERTY_EXPOSURE);
ValidateDynamicProperty(op, dpContrast, DYNAMIC_PROPERTY_CONTRAST);
ValidateDynamicProperty(op, dpGamma, DYNAMIC_PROPERTY_GAMMA);
ValidateDynamicProperty(op, dpGradingPrimary, DYNAMIC_PROPERTY_GRADING_PRIMARY);
ValidateDynamicProperty(op, dpGradingRGBCurve, DYNAMIC_PROPERTY_GRADING_RGBCURVE);
ValidateDynamicProperty(op, dpGradingHueCurve, DYNAMIC_PROPERTY_GRADING_HUECURVE);
ValidateDynamicProperty(op, dpGradingTone, DYNAMIC_PROPERTY_GRADING_TONE);
}
}
std::string OpRcPtrVec::getCacheID() const
{
std::ostringstream stream;
for (const auto & op : m_ops)
{
if (!op->isNoOpType())
{
const std::string id = op->getCacheID();
if (!id.empty())
{
stream << " " << id;
}
}
}
return stream.str();
}
std::ostream& operator<< (std::ostream & os, const Op & op)
{
os << op.getInfo();
return os;
}
std::string SerializeOpVec(const OpRcPtrVec & ops, size_t indent)
{
std::ostringstream oss;
for (OpRcPtrVec::size_type idx = 0, size = ops.size(); idx < size; ++idx)
{
const OpRcPtr & op = ops[idx];
oss << StringUtils::Repeat(" ", indent);
oss << "Op " << idx << ": " << *op << " ";
oss << op->getCacheID();
oss << "\n";
}
return oss.str();
}
void CreateOpVecFromOpData(OpRcPtrVec & ops,
const ConstOpDataRcPtr & opData,
TransformDirection dir)
{
switch (opData->getType())
{
case OpData::CDLType:
{
auto cdlSrc = std::dynamic_pointer_cast<const CDLOpData>(opData);
auto cdl = std::make_shared<CDLOpData>(*cdlSrc);
CreateCDLOp(ops, cdl, dir);
break;
}
case OpData::ExponentType:
{
auto expSrc = std::dynamic_pointer_cast<const ExponentOpData>(opData);
auto exp = std::make_shared<ExponentOpData>(*expSrc);
CreateExponentOp(ops, exp, dir);
break;
}
case OpData::ExposureContrastType:
{
auto ecSrc = std::dynamic_pointer_cast<const ExposureContrastOpData>(opData);
auto ec = ecSrc->clone();
CreateExposureContrastOp(ops, ec, dir);
break;
}
case OpData::FixedFunctionType:
{
auto ffSrc = std::dynamic_pointer_cast<const FixedFunctionOpData>(opData);
auto ff = std::make_shared<FixedFunctionOpData>(*ffSrc);
CreateFixedFunctionOp(ops, ff, dir);
break;
}
case OpData::GammaType:
{
auto gammaSrc = std::dynamic_pointer_cast<const GammaOpData>(opData);
auto gamma = std::make_shared<GammaOpData>(*gammaSrc);
CreateGammaOp(ops, gamma, dir);
break;
}
case OpData::GradingPrimaryType:
{
auto primarySrc = std::dynamic_pointer_cast<const GradingPrimaryOpData>(opData);
auto primary = std::make_shared<GradingPrimaryOpData>(*primarySrc);
CreateGradingPrimaryOp(ops, primary, dir);
break;
}
case OpData::GradingRGBCurveType:
{
auto rgbSrc = std::dynamic_pointer_cast<const GradingRGBCurveOpData>(opData);
auto rgb = std::make_shared<GradingRGBCurveOpData>(*rgbSrc);
CreateGradingRGBCurveOp(ops, rgb, dir);
break;
}
case OpData::GradingHueCurveType:
{
auto hueSrc = std::dynamic_pointer_cast<const GradingHueCurveOpData>(opData);
auto hue = std::make_shared<GradingHueCurveOpData>(*hueSrc);
CreateGradingHueCurveOp(ops, hue, dir);
break;
}
case OpData::GradingToneType:
{
auto toneSrc = std::dynamic_pointer_cast<const GradingToneOpData>(opData);
auto tone = std::make_shared<GradingToneOpData>(*toneSrc);
CreateGradingToneOp(ops, tone, dir);
break;
}
case OpData::LogType:
{
auto logSrc = std::dynamic_pointer_cast<const LogOpData>(opData);
auto log = std::make_shared<LogOpData>(*logSrc);
CreateLogOp(ops, log, dir);
break;
}
case OpData::Lut1DType:
{
auto lutSrc = std::dynamic_pointer_cast<const Lut1DOpData>(opData);
auto lut = std::make_shared<Lut1DOpData>(*lutSrc);
CreateLut1DOp(ops, lut, dir);
break;
}
case OpData::Lut3DType:
{
auto lutSrc = std::dynamic_pointer_cast<const Lut3DOpData>(opData);
auto lut = std::make_shared<Lut3DOpData>(*lutSrc);
CreateLut3DOp(ops, lut, dir);
break;
}
case OpData::MatrixType:
{
auto matrixSrc = std::dynamic_pointer_cast<const MatrixOpData>(opData);
auto matrix = std::make_shared<MatrixOpData>(*matrixSrc);
CreateMatrixOp(ops, matrix, dir);
break;
}
case OpData::RangeType:
{
auto rangeSrc = std::dynamic_pointer_cast<const RangeOpData>(opData);
auto range = std::make_shared<RangeOpData>(*rangeSrc);
CreateRangeOp(ops, range, dir);
break;
}
case OpData::ReferenceType:
{
throw Exception("ReferenceOpData should have been replaced by referenced ops");
}
case OpData::NoOpType:
{
throw Exception("OpData is not supported");
}
}
}
} // namespace OCIO_NAMESPACE