forked from NatronGitHub/Natron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImagePlaneDesc.cpp
529 lines (443 loc) · 16.5 KB
/
ImagePlaneDesc.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
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
/* ***** BEGIN LICENSE BLOCK *****
* This file is part of Natron <https://natrongithub.github.io/>,
* (C) 2018-2021 The Natron developers
* (C) 2013-2018 INRIA and Alexandre Gauthier-Foichat
*
* Natron is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Natron is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Natron. If not, see <http://www.gnu.org/licenses/gpl-2.0.html>
* ***** END LICENSE BLOCK ***** */
// ***** BEGIN PYTHON BLOCK *****
// from <https://docs.python.org/3/c-api/intro.html#include-files>:
// "Since Python may define some pre-processor definitions which affect the standard headers on some systems, you must include Python.h before any standard headers are included."
#include <Python.h>
// ***** END PYTHON BLOCK *****
#include "ImagePlaneDesc.h"
#include <ofxNatron.h>
#include <cassert>
#include <stdexcept>
#include <cstring>
#include <sstream>
NATRON_NAMESPACE_ENTER
static const char* rgbaComps[4] = {"R", "G", "B", "A"};
static const char* rgbComps[3] = {"R", "G", "B"};
static const char* alphaComps[1] = {"A"};
static const char* motionComps[2] = {"U", "V"};
static const char* disparityComps[2] = {"X", "Y"};
static const char* xyComps[2] = {"X", "Y"};
ImagePlaneDesc::ImagePlaneDesc()
: _planeID("none")
, _planeLabel("none")
, _channels()
, _channelsLabel("none")
{
}
ImagePlaneDesc::ImagePlaneDesc(const std::string& planeID,
const std::string& planeLabel,
const std::string& channelsLabel,
const std::vector<std::string>& channels)
: _planeID(planeID)
, _planeLabel(planeLabel)
, _channels(channels)
, _channelsLabel(channelsLabel)
{
if (planeLabel.empty()) {
// Plane label is the ID if empty
_planeLabel = _planeID;
}
if ( channelsLabel.empty() ) {
// Channels label is the concatenation of all channels
for (std::size_t i = 0; i < channels.size(); ++i) {
_channelsLabel.append(channels[i]);
}
}
}
ImagePlaneDesc::ImagePlaneDesc(const std::string& planeName,
const std::string& planeLabel,
const std::string& channelsLabel,
const char** channels,
int count)
: _planeID(planeName)
, _planeLabel(planeLabel)
, _channels()
, _channelsLabel(channelsLabel)
{
_channels.resize(count);
for (int i = 0; i < count; ++i) {
_channels[i] = channels[i];
}
if (planeLabel.empty()) {
// Plane label is the ID if empty
_planeLabel = _planeID;
}
if ( channelsLabel.empty() ) {
// Channels label is the concatenation of all channels
for (std::size_t i = 0; i < _channels.size(); ++i) {
_channelsLabel.append(channels[i]);
}
}
}
ImagePlaneDesc::ImagePlaneDesc(const ImagePlaneDesc& other)
{
*this = other;
}
ImagePlaneDesc&
ImagePlaneDesc::operator=(const ImagePlaneDesc& other)
{
_planeID = other._planeID;
_planeLabel = other._planeLabel;
_channels = other._channels;
_channelsLabel = other._channelsLabel;
return *this;
}
ImagePlaneDesc::~ImagePlaneDesc()
{
}
bool
ImagePlaneDesc::isColorPlane(const std::string& planeID)
{
return planeID == kNatronColorPlaneID;
}
bool
ImagePlaneDesc::isColorPlane() const
{
return ImagePlaneDesc::isColorPlane(_planeID);
}
bool
ImagePlaneDesc::operator==(const ImagePlaneDesc& other) const
{
if ( _channels.size() != other._channels.size() ) {
return false;
}
return _planeID == other._planeID;
}
bool
ImagePlaneDesc::operator<(const ImagePlaneDesc& other) const
{
return _planeID < other._planeID;
}
int
ImagePlaneDesc::getNumComponents() const
{
return (int)_channels.size();
}
const std::string&
ImagePlaneDesc::getPlaneID() const
{
return _planeID;
}
const std::string&
ImagePlaneDesc::getPlaneLabel() const
{
return _planeLabel;
}
const std::string&
ImagePlaneDesc::getChannelsLabel() const
{
return _channelsLabel;
}
const std::vector<std::string>&
ImagePlaneDesc::getChannels() const
{
return _channels;
}
const ImagePlaneDesc&
ImagePlaneDesc::getNoneComponents()
{
static const ImagePlaneDesc comp;
return comp;
}
const ImagePlaneDesc&
ImagePlaneDesc::getRGBAComponents()
{
static const ImagePlaneDesc comp(kNatronColorPlaneID, kNatronColorPlaneLabel, "", rgbaComps, 4);
return comp;
}
const ImagePlaneDesc&
ImagePlaneDesc::getRGBComponents()
{
static const ImagePlaneDesc comp(kNatronColorPlaneID, kNatronColorPlaneLabel, "", rgbComps, 3);
return comp;
}
const ImagePlaneDesc&
ImagePlaneDesc::getXYComponents()
{
static const ImagePlaneDesc comp(kNatronColorPlaneID, kNatronColorPlaneLabel, "XY", xyComps, 2);
return comp;
}
const ImagePlaneDesc&
ImagePlaneDesc::getAlphaComponents()
{
static const ImagePlaneDesc comp(kNatronColorPlaneID, kNatronColorPlaneLabel, "Alpha", alphaComps, 1);
return comp;
}
const ImagePlaneDesc&
ImagePlaneDesc::getBackwardMotionComponents()
{
static const ImagePlaneDesc comp(kNatronBackwardMotionVectorsPlaneID, kNatronBackwardMotionVectorsPlaneLabel, kNatronMotionComponentsLabel, motionComps, 2);
return comp;
}
const ImagePlaneDesc&
ImagePlaneDesc::getForwardMotionComponents()
{
static const ImagePlaneDesc comp(kNatronForwardMotionVectorsPlaneID, kNatronForwardMotionVectorsPlaneLabel, kNatronMotionComponentsLabel, motionComps, 2);
return comp;
}
const ImagePlaneDesc&
ImagePlaneDesc::getDisparityLeftComponents()
{
static const ImagePlaneDesc comp(kNatronDisparityLeftPlaneID, kNatronDisparityLeftPlaneLabel, kNatronDisparityComponentsLabel, disparityComps, 2);
return comp;
}
const ImagePlaneDesc&
ImagePlaneDesc::getDisparityRightComponents()
{
static const ImagePlaneDesc comp(kNatronDisparityRightPlaneID, kNatronDisparityRightPlaneLabel, kNatronDisparityComponentsLabel, disparityComps, 2);
return comp;
}
ChoiceOption
ImagePlaneDesc::getChannelOption(int channelIndex) const
{
if (channelIndex < 0 || channelIndex >= (int)_channels.size()) {
assert(false);
return ChoiceOption("","","");
}
std::string optionID, optionLabel;
optionLabel += _planeLabel;
optionID += _planeID;
if ( !optionLabel.empty() ) {
optionLabel += '.';
}
if (!optionID.empty()) {
optionID += '.';
}
// For the option label, append the name of the channel
optionLabel += _channels[channelIndex];
optionID += _channels[channelIndex];
return ChoiceOption(optionID, optionLabel, "");
}
ChoiceOption
ImagePlaneDesc::getPlaneOption() const
{
std::string optionLabel = _planeLabel + "." + _channelsLabel;
// The option ID is always the name of the layer, this ensures for the Color plane that even if the components type changes, the choice stays
// the same in the parameter.
return ChoiceOption(_planeID, optionLabel, "");
}
const ImagePlaneDesc&
ImagePlaneDesc::mapNCompsToColorPlane(int nComps)
{
switch (nComps) {
case 1:
return ImagePlaneDesc::getAlphaComponents();
case 2:
return ImagePlaneDesc::getXYComponents();
case 3:
return ImagePlaneDesc::getRGBComponents();
case 4:
return ImagePlaneDesc::getRGBAComponents();
default:
return ImagePlaneDesc::getNoneComponents();
}
}
static bool
extractOFXEncodedCustomPlane(const std::string& comp, std::string* layerName, std::string* layerLabel, std::string* channelsLabel, std::vector<std::string>* channels)
{
// Find the plane unique identifier
const std::size_t foundPlaneLen = std::strlen(kNatronOfxImageComponentsPlaneName);
std::size_t foundPlane = comp.find(kNatronOfxImageComponentsPlaneName);
if (foundPlane == std::string::npos) {
return false;
}
const std::size_t planeNameStartIdx = foundPlane + foundPlaneLen;
// Find the optional plane label
// If planeLabelStartIdx = 0, there's no plane label.
std::size_t planeLabelStartIdx = 0;
const std::size_t foundPlaneLabelLen = std::strlen(kNatronOfxImageComponentsPlaneLabel);
std::size_t foundPlaneLabel = comp.find(kNatronOfxImageComponentsPlaneLabel, planeNameStartIdx);
if (foundPlaneLabel != std::string::npos) {
planeLabelStartIdx = foundPlaneLabel + foundPlaneLabelLen;
}
// Find the optional channels label
// If channelsLabelStartIdx = 0, there's no channels label.
std::size_t channelsLabelStartIdx = 0;
const std::size_t foundChannelsLabelLen = std::strlen(kNatronOfxImageComponentsPlaneChannelsLabel);
// If there was a plane label before, pick from there otherwise pick from the name
std::size_t findChannelsLabelStart = planeLabelStartIdx > 0 ? planeLabelStartIdx : planeNameStartIdx;
std::size_t foundChannelsLabel = comp.find(kNatronOfxImageComponentsPlaneChannelsLabel, findChannelsLabelStart);
if (foundChannelsLabel != std::string::npos) {
channelsLabelStartIdx = foundChannelsLabel + foundChannelsLabelLen;
}
// Find the first channel
// If there was a channels label before, find from there, otherwise if there was a plane label before
// find from there, otherwise find from the name.
std::size_t findChannelStart = 0;
if (channelsLabelStartIdx > 0) {
findChannelStart = channelsLabelStartIdx;
} else if (planeLabelStartIdx > 0) {
findChannelStart = planeLabelStartIdx;
} else {
findChannelStart = planeNameStartIdx;
}
const std::size_t foundChannelLen = std::strlen(kNatronOfxImageComponentsPlaneChannel);
std::size_t foundChannel = comp.find(kNatronOfxImageComponentsPlaneChannel, findChannelStart);
if (foundChannel == std::string::npos) {
// There needs to be at least one channel.
return false;
}
// Extract channels label
if (channelsLabelStartIdx > 0) {
*channelsLabel = comp.substr(channelsLabelStartIdx, foundChannel - channelsLabelStartIdx);
}
// Extract plane label
if (planeLabelStartIdx > 0) {
std::size_t endIndex = (foundChannelsLabel != std::string::npos) ? foundChannelsLabel : foundChannel;
*layerLabel = comp.substr(planeLabelStartIdx, endIndex - planeLabelStartIdx);
}
// Extract plane name
{
std::size_t endIndex;
if (foundPlaneLabel != std::string::npos) {
// There's a plane label
endIndex = foundPlaneLabel;
} else if (foundChannelsLabel != std::string::npos) {
// There's no plane label but a channels label
endIndex = foundChannelsLabel;
} else {
// No plane label and no channels label
endIndex = foundChannel;
}
*layerName = comp.substr(planeNameStartIdx, endIndex - planeNameStartIdx);
}
while (foundChannel != std::string::npos) {
if (channels->size() >= 4) {
// A plane must have between 1 and 4 channels.
return false;
}
findChannelStart = foundChannel + foundChannelLen;
std::size_t nextChannel = comp.find(kNatronOfxImageComponentsPlaneChannel, findChannelStart);
std::string chan = comp.substr(findChannelStart, nextChannel - findChannelStart);
channels->push_back(chan);
foundChannel = nextChannel;
}
return true;
} // extractOFXEncodedCustomPlane
static ImagePlaneDesc
ofxCustomCompToNatronComp(const std::string& comp)
{
std::string planeID, planeLabel, channelsLabel;
std::vector<std::string> channels;
if (!extractOFXEncodedCustomPlane(comp, &planeID, &planeLabel, &channelsLabel, &channels)) {
return ImagePlaneDesc::getNoneComponents();
}
return ImagePlaneDesc(planeID, planeLabel, channelsLabel, channels);
}
ImagePlaneDesc
ImagePlaneDesc::mapOFXPlaneStringToPlane(const std::string& ofxPlane)
{
assert(ofxPlane != kFnOfxImagePlaneColour);
if (ofxPlane == kFnOfxImagePlaneBackwardMotionVector) {
return ImagePlaneDesc::getBackwardMotionComponents();
} else if (ofxPlane == kFnOfxImagePlaneForwardMotionVector) {
return ImagePlaneDesc::getForwardMotionComponents();
} else if (ofxPlane == kFnOfxImagePlaneStereoDisparityLeft) {
return ImagePlaneDesc::getDisparityLeftComponents();
} else if (ofxPlane == kFnOfxImagePlaneStereoDisparityRight) {
return ImagePlaneDesc::getDisparityRightComponents();
} else {
return ofxCustomCompToNatronComp(ofxPlane);
}
}
void
ImagePlaneDesc::mapOFXComponentsTypeStringToPlanes(const std::string& ofxComponents, ImagePlaneDesc* plane, ImagePlaneDesc* pairedPlane)
{
if (ofxComponents == kOfxImageComponentRGBA) {
*plane = ImagePlaneDesc::getRGBAComponents();
} else if (ofxComponents == kOfxImageComponentAlpha) {
*plane = ImagePlaneDesc::getAlphaComponents();
} else if (ofxComponents == kOfxImageComponentRGB) {
*plane = ImagePlaneDesc::getRGBComponents();
}else if (ofxComponents == kNatronOfxImageComponentXY) {
*plane = ImagePlaneDesc::getXYComponents();
} else if (ofxComponents == kOfxImageComponentNone) {
*plane = ImagePlaneDesc::getNoneComponents();
} else if (ofxComponents == kFnOfxImageComponentMotionVectors) {
*plane = ImagePlaneDesc::getBackwardMotionComponents();
*pairedPlane = ImagePlaneDesc::getForwardMotionComponents();
} else if (ofxComponents == kFnOfxImageComponentStereoDisparity) {
*plane = ImagePlaneDesc::getDisparityLeftComponents();
*pairedPlane = ImagePlaneDesc::getDisparityRightComponents();
} else {
*plane = ofxCustomCompToNatronComp(ofxComponents);
}
} // mapOFXComponentsTypeStringToPlanes
static std::string
natronCustomCompToOfxComp(const ImagePlaneDesc &comp)
{
std::stringstream ss;
const std::vector<std::string>& channels = comp.getChannels();
const std::string& planeID = comp.getPlaneID();
const std::string& planeLabel = comp.getPlaneLabel();
const std::string& channelsLabel = comp.getChannelsLabel();
ss << kNatronOfxImageComponentsPlaneName << planeID;
if (!planeLabel.empty()) {
ss << kNatronOfxImageComponentsPlaneLabel << planeLabel;
}
if (!channelsLabel.empty()) {
ss << kNatronOfxImageComponentsPlaneChannelsLabel << channelsLabel;
}
for (std::size_t i = 0; i < channels.size(); ++i) {
ss << kNatronOfxImageComponentsPlaneChannel << channels[i];
}
return ss.str();
} // natronCustomCompToOfxComp
std::string
ImagePlaneDesc::mapPlaneToOFXPlaneString(const ImagePlaneDesc& plane)
{
if (plane.isColorPlane()) {
return kFnOfxImagePlaneColour;
} else if ( plane == ImagePlaneDesc::getBackwardMotionComponents() ) {
return kFnOfxImagePlaneBackwardMotionVector;
} else if ( plane == ImagePlaneDesc::getForwardMotionComponents()) {
return kFnOfxImagePlaneForwardMotionVector;
} else if ( plane == ImagePlaneDesc::getDisparityLeftComponents()) {
return kFnOfxImagePlaneStereoDisparityLeft;
} else if ( plane == ImagePlaneDesc::getDisparityRightComponents() ) {
return kFnOfxImagePlaneStereoDisparityRight;
} else {
return natronCustomCompToOfxComp(plane);
}
}
std::string
ImagePlaneDesc::mapPlaneToOFXComponentsTypeString(const ImagePlaneDesc& plane)
{
if ( plane == ImagePlaneDesc::getNoneComponents() ) {
return kOfxImageComponentNone;
} else if ( plane == ImagePlaneDesc::getAlphaComponents() ) {
return kOfxImageComponentAlpha;
} else if ( plane == ImagePlaneDesc::getRGBComponents() ) {
return kOfxImageComponentRGB;
} else if ( plane == ImagePlaneDesc::getRGBAComponents() ) {
return kOfxImageComponentRGBA;
} else if ( plane == ImagePlaneDesc::getXYComponents() ) {
return kNatronOfxImageComponentXY;
} else if ( plane == ImagePlaneDesc::getBackwardMotionComponents() ||
plane == ImagePlaneDesc::getForwardMotionComponents()) {
return kFnOfxImageComponentMotionVectors;
} else if ( plane == ImagePlaneDesc::getDisparityLeftComponents() ||
plane == ImagePlaneDesc::getDisparityRightComponents()) {
return kFnOfxImageComponentStereoDisparity;
} else {
return natronCustomCompToOfxComp(plane);
}
}
NATRON_NAMESPACE_EXIT