forked from AcademySoftwareFoundation/OpenColorIO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OCIOCDLTransform.cpp
379 lines (305 loc) · 11.3 KB
/
OCIOCDLTransform.cpp
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
/**
* OpenColorIO conversion Iop.
*/
#include "OCIOCDLTransform.h"
namespace OCIO = OCIO_NAMESPACE;
#include <string>
#include <sstream>
#include <stdexcept>
#include <DDImage/Channel.h>
#include <DDImage/PixelIop.h>
#include <DDImage/NukeWrapper.h>
#include <DDImage/Row.h>
#include <DDImage/Knobs.h>
const char* OCIOCDLTransform::dirs[] = { "forward", "inverse", 0 };
OCIOCDLTransform::OCIOCDLTransform(Node *n) : DD::Image::PixelIop(n)
{
for (int i = 0; i < 3; i++){
m_slope[i] = 1.0;
m_offset[i] = 0.0;
m_power[i] = 1.0;
}
m_saturation = 1.0;
m_readFromFile = false;
m_dirindex = 0;
m_file = NULL;
m_reload_version = 1;
m_slopeKnob = NULL;
m_offsetKnob = NULL;
m_powerKnob = NULL;
m_saturationKnob = NULL;
m_fileKnob = NULL;
m_cccidKnob = NULL;
m_firstLoad = true;
}
OCIOCDLTransform::~OCIOCDLTransform()
{
}
void OCIOCDLTransform::knobs(DD::Image::Knob_Callback f)
{
// ASC CDL grade numbers
m_slopeKnob = DD::Image::Color_knob(f, m_slope, DD::Image::IRange(0, 4.0), "slope");
m_offsetKnob = DD::Image::Color_knob(f, m_offset, DD::Image::IRange(-0.2, 0.2), "offset");
m_powerKnob = DD::Image::Color_knob(f, m_power, DD::Image::IRange(0.0, 4.0), "power");
m_saturationKnob = DD::Image::Float_knob(f, &m_saturation, DD::Image::IRange(0, 4.0), "saturation");
Enumeration_knob(f, &m_dirindex, dirs, "direction", "direction");
DD::Image::Tooltip(f, "Specify the transform direction.");
DD::Image::Divider(f);
DD::Image::Bool_knob(f, &m_readFromFile, "read_from_file", "read from file");
DD::Image::SetFlags(f, DD::Image::Knob::EARLY_STORE);
DD::Image::Tooltip(f, "Load color correction information from the .cc or .ccc file.");
m_fileKnob = File_knob(f, &m_file, "file", "file");
const char * filehelp = "Specify the src ASC CDL file, on disk, to use for this transform. "
"This can be either a .cc or .ccc file. If .ccc is specified, the cccid is required.";
DD::Image::Tooltip(f, filehelp);
// Reload button, and hidden "version" knob to invalidate cache on reload
Button(f, "reload", "reload");
DD::Image::Tooltip(f, "Reloads specified files");
Int_knob(f, &m_reload_version, "version");
DD::Image::SetFlags(f, DD::Image::Knob::HIDDEN);
DD::Image::SetFlags(f, DD::Image::Knob::ENDLINE);
m_cccidKnob = String_knob(f, &m_cccid, "cccid");
const char * ccchelp = "If the source file is an ASC CDL CCC (color correction collection), "
"this specifies the id to lookup. OpenColorIO::Contexts (envvars) are obeyed.";
DD::Image::Tooltip(f, ccchelp);
/* TODO:
These scripts should be updated to make use of native OCIO APIs, rather than convenience functions
exposed in ocionuke. Ideally, ocionuke would be a minimal module (essentially only ui code) that makes
use of OCIO for all heavy lifting.
*/
DD::Image::PyScript_knob(f, "import ocionuke.cdl; ocionuke.cdl.select_cccid_for_filetransform()", "select_cccid", "select cccid");
// Import/export buttons
DD::Image::PyScript_knob(f, "import ocionuke.cdl; ocionuke.cdl.export_as_cc()", "export_cc", "export grade as .cc");
DD::Image::Tooltip(f, "Export this grade as a ColorCorrection XML file, which can be loaded with the OCIOFileTransform, or using a FileTransform in an OCIO config");
DD::Image::PyScript_knob(f, "import ocionuke.cdl; ocionuke.cdl.import_cc_from_xml()", "import_cc", "import from .cc");
DD::Image::Tooltip(f, "Import grade from a ColorCorrection XML file");
DD::Image::Divider(f);
/*
TODO: One thing thats sucks is that we dont apparently have a mechansism to call refreshKnobEnabledState
after the knobs have been loaded, but before scripts have a chance to run. I'd love to have a post-knob
finalize callback opportunity to reload the cdl from file, if needed. The current system will only do the
initial file refresh when either the ui is loaded, or a render is triggered.
*/
if(!f.makeKnobs() && m_firstLoad)
{
m_firstLoad = false;
refreshKnobEnabledState();
if(m_readFromFile) loadCDLFromFile();
}
}
void OCIOCDLTransform::refreshKnobEnabledState()
{
if(m_readFromFile)
{
m_slopeKnob->disable();
m_offsetKnob->disable();
m_powerKnob->disable();
m_saturationKnob->disable();
// We leave these active to allow knob re-use with the import/export buttons
//m_fileKnob->enable();
//m_cccidKnob->enable();
loadCDLFromFile();
}
else
{
m_slopeKnob->enable();
m_offsetKnob->enable();
m_powerKnob->enable();
m_saturationKnob->enable();
// We leave these active to allow knob re-use with the import/export buttons
//m_fileKnob->disable();
//m_cccidKnob->disable();
}
}
void OCIOCDLTransform::append(DD::Image::Hash& nodehash)
{
// There is a bug where in Nuke <6.3 the String_knob (used for
// cccid) is not included in the node's hash. Include it manually
// so the node correctly redraws. Appears fixed in in 6.3
nodehash.append(m_cccid.c_str());
// Incremented to force reloading after rereading the LUT file
nodehash.append(m_reload_version);
}
int OCIOCDLTransform::knob_changed(DD::Image::Knob* k)
{
// return true if you want to continue to receive changes for this knob
std::string knobname = k->name();
if(knobname == "read_from_file" || knobname == "file" || knobname == "cccid")
{
refreshKnobEnabledState();
if(m_readFromFile)
{
loadCDLFromFile();
}
return true;
}
else if(knobname == "reload")
{
knob("version")->set_value(m_reload_version+1);
OCIO::ClearAllCaches();
m_firstLoad = true;
return true; // ensure callback is triggered again
}
return false;
}
// Check to see if file exists
// read from file
// set knob values
// throw an error if anything goes wrong
void OCIOCDLTransform::loadCDLFromFile()
{
OCIO::CDLTransformRcPtr transform;
try
{
// This is inexpensive to call multiple times, as OCIO caches results
// internally.
transform = OCIO::CDLTransform::CreateFromFile(m_file, m_cccid.c_str());
}
catch(OCIO::Exception &e)
{
error(e.what());
return;
}
float sop[9];
transform->getSOP(sop);
m_slopeKnob->clear_animated(-1);
m_slopeKnob->set_value(sop[0], 0);
m_slopeKnob->set_value(sop[1], 1);
m_slopeKnob->set_value(sop[2], 2);
m_offsetKnob->clear_animated(-1);
m_offsetKnob->set_value(sop[3], 0);
m_offsetKnob->set_value(sop[4], 1);
m_offsetKnob->set_value(sop[5], 2);
m_powerKnob->clear_animated(-1);
m_powerKnob->set_value(sop[6], 0);
m_powerKnob->set_value(sop[7], 1);
m_powerKnob->set_value(sop[8], 2);
m_saturationKnob->clear_animated(-1);
m_saturationKnob->set_value(transform->getSat());
}
void OCIOCDLTransform::_validate(bool for_real)
{
if(m_firstLoad)
{
m_firstLoad = false;
if(m_readFromFile) loadCDLFromFile();
}
// We must explicitly refresh the enable state here as well,
// as there are some changes (such as expressioned knob updates)
// that wont trigger the knob_changed callback. This allows
// us to catch these here.
refreshKnobEnabledState();
try
{
OCIO::ConstConfigRcPtr config = OCIO::GetCurrentConfig();
OCIO::CDLTransformRcPtr cc = OCIO::CDLTransform::Create();
cc->setSlope(m_slope);
cc->setOffset(m_offset);
cc->setPower(m_power);
cc->setSat(m_saturation);
if(m_dirindex == 0) cc->setDirection(OCIO::TRANSFORM_DIR_FORWARD);
else cc->setDirection(OCIO::TRANSFORM_DIR_INVERSE);
m_processor = config->getProcessor(cc);
}
catch(OCIO::Exception &e)
{
error(e.what());
return;
}
if(m_processor->isNoOp())
{
set_out_channels(DD::Image::Mask_None); // prevents engine() from being called
} else {
set_out_channels(DD::Image::Mask_All);
}
DD::Image::PixelIop::_validate(for_real);
}
// Note that this is copied by others (OCIODisplay)
void OCIOCDLTransform::in_channels(int /* n unused */, DD::Image::ChannelSet& mask) const
{
DD::Image::ChannelSet done;
foreach(c, mask)
{
if (DD::Image::colourIndex(c) < 3 && !(done & c))
{
done.addBrothers(c, 3);
}
}
mask += done;
}
// See Saturation::pixel_engine for a well-commented example.
// Note that this is copied by others (OCIODisplay)
void OCIOCDLTransform::pixel_engine(
const DD::Image::Row& in,
int /* rowY unused */, int rowX, int rowXBound,
DD::Image::ChannelMask outputChannels,
DD::Image::Row& out)
{
int rowWidth = rowXBound - rowX;
DD::Image::ChannelSet done;
foreach (requestedChannel, outputChannels)
{
// Skip channels which had their trios processed already,
if (done & requestedChannel)
{
continue;
}
// Pass through channels which are not selected for processing
// and non-rgb channels.
if (colourIndex(requestedChannel) >= 3)
{
out.copy(in, requestedChannel, rowX, rowXBound);
continue;
}
DD::Image::Channel rChannel = DD::Image::brother(requestedChannel, 0);
DD::Image::Channel gChannel = DD::Image::brother(requestedChannel, 1);
DD::Image::Channel bChannel = DD::Image::brother(requestedChannel, 2);
done += rChannel;
done += gChannel;
done += bChannel;
const float *rIn = in[rChannel] + rowX;
const float *gIn = in[gChannel] + rowX;
const float *bIn = in[bChannel] + rowX;
float *rOut = out.writable(rChannel) + rowX;
float *gOut = out.writable(gChannel) + rowX;
float *bOut = out.writable(bChannel) + rowX;
// OCIO modifies in-place
// Note: xOut can equal xIn in some circumstances, such as when the
// 'Black' (throwaway) scanline is uses. We thus must guard memcpy,
// which does not allow for overlapping regions.
if (rOut != rIn) memcpy(rOut, rIn, sizeof(float)*rowWidth);
if (gOut != gIn) memcpy(gOut, gIn, sizeof(float)*rowWidth);
if (bOut != bIn) memcpy(bOut, bIn, sizeof(float)*rowWidth);
try
{
OCIO::PlanarImageDesc img(rOut, gOut, bOut, NULL, rowWidth, /*height*/ 1);
m_processor->apply(img);
}
catch(OCIO::Exception &e)
{
error(e.what());
}
}
}
const DD::Image::Op::Description OCIOCDLTransform::description("OCIOCDLTransform", build);
const char* OCIOCDLTransform::Class() const
{
return description.name;
}
const char* OCIOCDLTransform::displayName() const
{
return description.name;
}
const char* OCIOCDLTransform::node_help() const
{
// TODO more detailed help text
return "Use OpenColorIO to apply an ASC CDL grade. Applied using:\n\n"\
"out = (i * s + o)^p\n\nWhere i is the input value, s is slope, "\
"o is offset and p is power";
}
DD::Image::Op* build(Node *node)
{
DD::Image::NukeWrapper *op = new DD::Image::NukeWrapper(new OCIOCDLTransform(node));
op->channels(DD::Image::Mask_RGB);
return op;
}