-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathofxsMultiPlane.cpp
1508 lines (1263 loc) · 52.3 KB
/
ofxsMultiPlane.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
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
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; -*- */
/* ***** BEGIN LICENSE BLOCK *****
* This file is part of openfx-supportext <https://github.com/NatronGitHub/openfx-supportext>,
* (C) 2018-2021 The Natron Developers
* (C) 2013-2018 INRIA
*
* openfx-supportext 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.
*
* openfx-supportext 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 openfx-supportext. If not, see <http://www.gnu.org/licenses/gpl-2.0.html>
* ***** END LICENSE BLOCK ***** */
/*
* Helper functions to implement plug-ins that support kFnOfxImageEffectPlaneSuite v2
* In order to use these functions the following condition must be met:
*#if defined(OFX_EXTENSIONS_NUKE) && defined(OFX_EXTENSIONS_NATRON)
if (fetchSuite(kFnOfxImageEffectPlaneSuite, 2) && // for clipGetImagePlane
getImageEffectHostDescription()->supportsDynamicChoices && // for dynamic layer choices
getImageEffectHostDescription()->isMultiPlanar) // for clipGetImagePlane
... this is ok...
*#endif
*/
#include "ofxsMultiPlane.h"
#include <algorithm>
#include <set>
using namespace OFX;
using std::vector;
using std::string;
using std::map;
using std::set;
static bool gHostSupportsMultiPlaneV1 = false;
static bool gHostSupportsMultiPlaneV2 = false;
static bool gHostSupportsDynamicChoices = false;
static bool gHostIsNatron3OrGreater = false;
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"};
namespace OFX {
namespace MultiPlane {
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 == kOfxMultiplaneColorPlaneID;
}
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(kOfxMultiplaneColorPlaneID, kOfxMultiplaneColorPlaneLabel, "", rgbaComps, 4);
return comp;
}
const ImagePlaneDesc&
ImagePlaneDesc::getRGBComponents()
{
static const ImagePlaneDesc comp(kOfxMultiplaneColorPlaneID, kOfxMultiplaneColorPlaneLabel, "", rgbComps, 3);
return comp;
}
const ImagePlaneDesc&
ImagePlaneDesc::getXYComponents()
{
static const ImagePlaneDesc comp(kOfxMultiplaneColorPlaneID, kOfxMultiplaneColorPlaneLabel, "XY", xyComps, 2);
return comp;
}
const ImagePlaneDesc&
ImagePlaneDesc::getAlphaComponents()
{
static const ImagePlaneDesc comp(kOfxMultiplaneColorPlaneID, kOfxMultiplaneColorPlaneLabel, "Alpha", alphaComps, 1);
return comp;
}
const ImagePlaneDesc&
ImagePlaneDesc::getBackwardMotionComponents()
{
static const ImagePlaneDesc comp(kOfxMultiplaneBackwardMotionVectorsPlaneID, kOfxMultiplaneBackwardMotionVectorsPlaneLabel, kOfxMultiplaneMotionComponentsLabel, motionComps, 2);
return comp;
}
const ImagePlaneDesc&
ImagePlaneDesc::getForwardMotionComponents()
{
static const ImagePlaneDesc comp(kOfxMultiplaneForwardMotionVectorsPlaneID, kOfxMultiplaneForwardMotionVectorsPlaneLabel, kOfxMultiplaneMotionComponentsLabel, motionComps, 2);
return comp;
}
const ImagePlaneDesc&
ImagePlaneDesc::getDisparityLeftComponents()
{
static const ImagePlaneDesc comp(kOfxMultiplaneDisparityLeftPlaneID, kOfxMultiplaneDisparityLeftPlaneLabel, kOfxMultiplaneDisparityComponentsLabel, disparityComps, 2);
return comp;
}
const ImagePlaneDesc&
ImagePlaneDesc::getDisparityRightComponents()
{
static const ImagePlaneDesc comp(kOfxMultiplaneDisparityRightPlaneID, kOfxMultiplaneDisparityRightPlaneLabel, kOfxMultiplaneDisparityComponentsLabel, disparityComps, 2);
return comp;
}
void
ImagePlaneDesc::getChannelOption(int channelIndex, std::string* optionID, std::string* optionLabel) const
{
if (channelIndex < 0 || channelIndex >= (int)_channels.size()) {
assert(false);
return;
}
*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];
}
void
ImagePlaneDesc::getPlaneOption(std::string* optionID, std::string* optionLabel) const
{
// 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.
*optionLabel = _planeLabel + "." + _channelsLabel;
*optionID = _planeID;
}
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 ImagePlaneDesc
ofxCustomCompToNatronComp(const std::string& comp)
{
std::string planeID, planeLabel, channelsLabel;
std::vector<std::string> channels;
if (!extractCustomPlane(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);
}
}
} // namespace MultiPlane
} // namespace OFX
namespace {
void
getHardCodedPlanes(bool onlyColorPlane, std::vector<const MultiPlane::ImagePlaneDesc*>* planesToAdd)
{
const MultiPlane::ImagePlaneDesc& rgbaPlane = MultiPlane::ImagePlaneDesc::getRGBAComponents();
const MultiPlane::ImagePlaneDesc& disparityLeftPlane = MultiPlane::ImagePlaneDesc::getDisparityLeftComponents();
const MultiPlane::ImagePlaneDesc& disparityRightPlane = MultiPlane::ImagePlaneDesc::getDisparityRightComponents();
const MultiPlane::ImagePlaneDesc& motionBwPlane = MultiPlane::ImagePlaneDesc::getBackwardMotionComponents();
const MultiPlane::ImagePlaneDesc& motionFwPlane = MultiPlane::ImagePlaneDesc::getForwardMotionComponents();
planesToAdd->push_back(&rgbaPlane);
if (!onlyColorPlane) {
planesToAdd->push_back(&disparityLeftPlane);
planesToAdd->push_back(&disparityRightPlane);
planesToAdd->push_back(&motionBwPlane);
planesToAdd->push_back(&motionFwPlane);
}
}
struct ChoiceOption
{
string name, label, hint;
};
struct ChoiceOption_Compare
{
bool operator() (const ChoiceOption& lhs, const ChoiceOption& rhs) const
{
return lhs.name < rhs.name;
}
};
void
getHardCodedPlaneOptions(const vector<string>& clips,
bool addConstants,
bool onlyColorPlane,
vector<ChoiceOption>* options)
{
std::vector<const MultiPlane::ImagePlaneDesc*> planesToAdd;
getHardCodedPlanes(onlyColorPlane, &planesToAdd);
for (std::size_t c = 0; c < clips.size(); ++c) {
const string& clipName = clips[c];
for (std::size_t p = 0; p < planesToAdd.size(); ++p) {
const std::string& planeLabel = planesToAdd[p]->getPlaneLabel();
const std::vector<std::string>& planeChannels = planesToAdd[p]->getChannels();
for (std::size_t i = 0; i < planeChannels.size(); ++i) {
ChoiceOption option;
// Prefix the clip name if there are multiple clip channels to read from
if (clips.size() > 1) {
option.name.append(clipName);
option.name.push_back('.');
option.label.append(clipName);
option.label.push_back('.');
}
// Prefix the plane name
option.name.append(planesToAdd[p]->getPlaneID());
option.name.push_back('.');
option.label.append(planeLabel);
option.label.push_back('.');
option.name.append(planeChannels[i]);
option.label.append(planeChannels[i]);
// Make up some tooltip
option.hint.append(planeChannels[i]);
option.hint.append(" channel from input ");
option.hint.append(clipName);
options->push_back(option);
}
}
if ( addConstants && (c == 0) ) {
{
string opt, hint;
opt.append(kMultiPlaneChannelParamOption0);
hint.append(kMultiPlaneChannelParamOption0Hint);
ChoiceOption choice = {opt, opt, hint};
options->push_back(choice);
}
{
string opt, hint;
opt.append(kMultiPlaneChannelParamOption1);
hint.append(kMultiPlaneChannelParamOption1Hint);
ChoiceOption choice = {opt, opt, hint};
options->push_back(choice);
}
}
}
} // getHardCodedPlanes
template <typename T>
void
addInputChannelOptionsRGBAInternal(T* param,
const vector<string>& clips,
bool addConstants,
bool onlyColorPlane,
vector<ChoiceOption>* optionsParam)
{
vector<ChoiceOption> options;
getHardCodedPlaneOptions(clips, addConstants, onlyColorPlane, &options);
if (optionsParam) {
*optionsParam = options;
}
if (param) {
for (std::size_t i = 0; i < options.size(); ++i) {
param->appendOption(options[i].label, options[i].hint, options[i].name);
}
}
} // addInputChannelOptionsRGBAInternal
} // anonymous namespace
namespace OFX {
namespace MultiPlane {
namespace Factory {
void
addInputChannelOptionsRGBA(ChoiceParamDescriptor* param,
const vector<string>& clips,
bool addConstants,
bool onlyColorPlane)
{
addInputChannelOptionsRGBAInternal<ChoiceParamDescriptor>(param, clips, addConstants, onlyColorPlane, 0);
}
void
addInputChannelOptionsRGBA(const vector<string>& clips,
bool addConstants,
bool onlyColorPlane)
{
addInputChannelOptionsRGBAInternal<ChoiceParam>(0, clips, addConstants, onlyColorPlane, 0);
}
} // factory
/**
* @brief For each choice param, the list of clips it "depends on" (that is the clip available planes that will be visible in the choice)
**/
struct ChoiceParamClips
{
// The choice parameter containing the planes or channels.
ChoiceParam* param;
// True if the menu should contain any entry for each channel of each plane
bool splitPlanesIntoChannels;
// True if we should add a "None" option
bool addNoneOption;
// True if we should add 0 and 1 options
bool addConstantOptions;
bool isOutput;
bool hideIfClipDisconnected;
vector<Clip*> clips;
vector<string> clipNames;
ChoiceParamClips()
: param(NULL)
, splitPlanesIntoChannels(false)
, addNoneOption(false)
, addConstantOptions(false)
, isOutput(false)
, hideIfClipDisconnected(false)
, clips()
, clipNames()
{
}
};
struct MultiPlaneEffectPrivate
{
// Pointer to the public interface
MultiPlaneEffect* _publicInterface;
// A map of each dynamic choice parameters containing planes/channels
map<string, ChoiceParamClips> params;
// If true, all planes have to be processed
BooleanParam* allPlanesCheckbox;
Clip* dstClip;
// Stores for each clip its available planes
// This is to avoid a recursion when calling getPlanesPresent
// on the output clip.
std::map<Clip*, std::list<ImagePlaneDesc> > perClipPlanesAvailable;
MultiPlaneEffectPrivate(MultiPlaneEffect* publicInterface)
: _publicInterface(publicInterface)
, params()
, allPlanesCheckbox(NULL)
, dstClip(publicInterface->fetchClip(kOfxImageEffectOutputClipName))
, perClipPlanesAvailable()
{
}
/**
* @brief The instanceChanged handler for the "All Planes" checkbox if the parameter was defined with
**/
void handleAllPlanesCheckboxParamChanged();
/**
* @brief To be called in createInstance and clipChanged to refresh visibility of input channel/plane selectors.
**/
void refreshSelectorsVisibility();
/**
* @brief Rebuild all choice parameters depending on the clips planes present.
* This function is supposed to be called in the clipChanged action on the output clip.
**/
void buildChannelMenus();
};
MultiPlaneEffect::MultiPlaneEffect(OfxImageEffectHandle handle)
: ImageEffect(handle)
, _imp( new MultiPlaneEffectPrivate(this) )
{
}
MultiPlaneEffect::~MultiPlaneEffect()
{
}
void
MultiPlaneEffect::fetchDynamicMultiplaneChoiceParameter(const string& paramName,
const FetchChoiceParamOptions& args)
{
ChoiceParamClips& paramData = _imp->params[paramName];
paramData.param = fetchChoiceParam(paramName);
paramData.splitPlanesIntoChannels = args.splitPlanesIntoChannelOptions;
paramData.addNoneOption = args.addNoneOption;
paramData.addConstantOptions = args.addConstantOptions;
paramData.clips = args.dependsClips;
for (std::size_t i = 0; i < args.dependsClips.size(); ++i) {
// A choice menu cannot depend on the planes present on an output clip, since we actually may need the value
// of the choice to return the planes present in output!
assert(args.dependsClips[i]->name() != kOfxImageEffectOutputClipName);
paramData.clipNames.push_back( args.dependsClips[i]->name() );
}
paramData.isOutput = args.isOutputPlaneChoice;
paramData.hideIfClipDisconnected = args.hideIfClipDisconnected;
if (args.isOutputPlaneChoice && !_imp->allPlanesCheckbox && paramExists(kMultiPlaneProcessAllPlanesParam)) {
_imp->allPlanesCheckbox = fetchBooleanParam(kMultiPlaneProcessAllPlanesParam);
}
if (_imp->allPlanesCheckbox) {
bool allPlanesSelected = _imp->allPlanesCheckbox->getValue();
paramData.param->setIsSecretAndDisabled(allPlanesSelected);
}
}
void
MultiPlaneEffectPrivate::buildChannelMenus()
{
perClipPlanesAvailable.clear();
// If no dynamic choices support, only add built-in planes.
if (!gHostSupportsDynamicChoices) {
vector<const MultiPlane::ImagePlaneDesc*> planesToAdd;
getHardCodedPlanes(!gHostSupportsMultiPlaneV1, &planesToAdd);
for (map<string, ChoiceParamClips>::iterator it = params.begin(); it != params.end(); ++it) {
for (std::size_t c = 0; c < it->second.clips.size(); ++c) {
// For the output plane selector, map the clip planes against the output clip even though the user provided a
// source clip as pass-through clip
Clip* clip = it->second.isOutput ? dstClip : it->second.clips[c];
map<Clip*, std::list<ImagePlaneDesc> >::iterator foundClip = perClipPlanesAvailable.find(clip);
if (foundClip != perClipPlanesAvailable.end()) {
continue;
} else {
std::list<ImagePlaneDesc>& clipPlanes = perClipPlanesAvailable[clip];
for (vector<const MultiPlane::ImagePlaneDesc*>::const_iterator it2 = planesToAdd.begin(); it2 != planesToAdd.end(); ++it2) {
clipPlanes.push_back(**it2);
}
}
}
}
return;
}
// This code requires dynamic choice parameters support.
// For each parameter to refresh
std::vector<std::pair<ChoiceParam*,std::vector<ChoiceOption> > > perParamOptions;
for (map<string, ChoiceParamClips>::iterator it = params.begin(); it != params.end(); ++it) {
vector<ChoiceOption> options;
set<ChoiceOption, ChoiceOption_Compare> optionsSorted;
if (it->second.splitPlanesIntoChannels) {
// Add built-in hard-coded options A.R, A.G, ... 0, 1, B.R, B.G ...
getHardCodedPlaneOptions(it->second.clipNames, it->second.addConstantOptions, true /*onlyColorPlane*/, &options);
optionsSorted.insert(options.begin(), options.end());
} else {
// For plane selectors, we might want a "None" option to select an input plane.
if (it->second.addNoneOption) {
ChoiceOption opt = {kMultiPlanePlaneParamOptionNone, kMultiPlanePlaneParamOptionNoneLabel, ""};
options.push_back(opt);
optionsSorted.insert(opt);
}
}
// We don't use a map here to keep the clips in the order of what the user passed them in fetchDynamicMultiplaneChoiceParameter
std::list<std::pair<Clip*, std::list<ImagePlaneDesc>* > > perClipPlanes;
for (std::size_t c = 0; c < it->second.clips.size(); ++c) {
Clip* clip = it->second.clips[c];
// Did we fetch the clip available planes already ? This speeds it up in the case where we have multiple choice parameters
// accessing the same clip.
std::list<ImagePlaneDesc>* availableClipPlanes = 0;
// For the output plane selector, map the clip planes against the output clip even though the user provided a
// source clip as pass-through clip so the extraneous planes returned by getExtraneousPlanesCreated are not added for
// the available planes on the source clip
Clip* clipToMap = it->second.isOutput ? dstClip : clip;
map<Clip*, std::list<ImagePlaneDesc> >::iterator foundClip = perClipPlanesAvailable.find(clipToMap);
if (foundClip != perClipPlanesAvailable.end()) {
availableClipPlanes = &foundClip->second;
} else {
availableClipPlanes = &(perClipPlanesAvailable)[clipToMap];
// Fetch planes presents from the clip and map them to ImagePlaneDesc
// Note that the clip cannot bethe output clip: the host may call recursively the getClipComponents() action during the call to getPlanesPresent()
// to find out the components present in output of this effect.
//
// Instead the plug-in should read planes from the pass-through clip (the same that is set in the implementation of getClipComponents)
// to populate the output menu.
vector<string> clipPlaneStrings;
clip->getPlanesPresent(&clipPlaneStrings);
// If this is the output menu, add user created planes from a user interface
if (it->second.isOutput) {
vector<string> extraPlanes;
_publicInterface->getExtraneousPlanesCreated(&extraPlanes);
clipPlaneStrings.insert(clipPlaneStrings.end(), extraPlanes.begin(), extraPlanes.end());
}
for (std::size_t i = 0; i < clipPlaneStrings.size(); ++i) {
ImagePlaneDesc plane;
if (clipPlaneStrings[i] == kOfxMultiplaneColorPlaneID) {
plane = ImagePlaneDesc::mapNCompsToColorPlane(clip->getPixelComponentCount());
} else {
plane = ImagePlaneDesc::mapOFXPlaneStringToPlane(clipPlaneStrings[i]);
}
availableClipPlanes->push_back(plane);
}
}
perClipPlanes.push_back(std::make_pair(clip, availableClipPlanes));
} // for each clip
for (std::list<std::pair<Clip*, std::list<ImagePlaneDesc>* > >::const_iterator it2 = perClipPlanes.begin(); it2 != perClipPlanes.end(); ++it2) {
const std::list<ImagePlaneDesc>* planes = it2->second;
for (std::list<ImagePlaneDesc>::const_iterator it3 = planes->begin(); it3 != planes->end(); ++it3) {
if (it->second.splitPlanesIntoChannels) {
// User wants per-channel options
int nChannels = it3->getNumComponents();
for (int k = 0; k < nChannels; ++k) {
ChoiceOption opt;
it3->getChannelOption(k, &opt.name, &opt.label);
// Prefix the clip name if there are multiple clip channels to read from
if (it->second.clips.size() > 1) {
opt.name = it2->first->name() + '.' + opt.name;
opt.label = it2->first->name() + '.' + opt.label;
}
if (optionsSorted.find(opt) == optionsSorted.end()) {
options.push_back(opt);
optionsSorted.insert(opt);
}
}
} else {
// User wants planes in options
ChoiceOption opt;
it3->getPlaneOption(&opt.name, &opt.label);
// Prefix the clip name if there are multiple clip channels to read from
if (it->second.clips.size() > 1) {
opt.name = it2->first->name() + '.' + opt.name;
opt.label = it2->first->name() + '.' + opt.label;
}
if (optionsSorted.find(opt) == optionsSorted.end()) {
options.push_back(opt);
optionsSorted.insert(opt);
}
}
} // for each plane
} // for each clip planes available
// Set the new choice menu
perParamOptions.push_back(std::make_pair(it->second.param,options));
} // for all choice parameters
// Reset all choice options in the same pass, once the perClipPlanesAvailable is full, because the resetOptions call may recursively call
// getClipComponents and thus getPlaneNeeded which relies on it.
for (std::vector<std::pair<ChoiceParam*,std::vector<ChoiceOption> > >::const_iterator it = perParamOptions.begin(); it != perParamOptions.end(); ++it) {
vector<string> labels(it->second.size()), hints(it->second.size()), enums(it->second.size());
for (std::size_t i = 0; i < it->second.size(); ++i) {
labels[i] = it->second[i].label;
hints[i] = it->second[i].hint;
enums[i] = it->second[i].name;
}
it->first->resetOptions(labels, hints, enums);
}
} // buildChannelMenus
void
MultiPlaneEffectPrivate::handleAllPlanesCheckboxParamChanged()
{
bool allPlanesSelected = allPlanesCheckbox->getValue();
for (map<string, ChoiceParamClips>::const_iterator it = params.begin(); it != params.end(); ++it) {
if (!it->second.splitPlanesIntoChannels) {
it->second.param->setIsSecretAndDisabled(allPlanesSelected);
}
}
}
void
MultiPlaneEffectPrivate::refreshSelectorsVisibility()
{
for (map<string, ChoiceParamClips>::iterator it = params.begin(); it != params.end(); ++it) {
if ( it->second.isOutput || !it->second.hideIfClipDisconnected) {
continue;
}
bool hasClipVisible = false;
for (std::size_t i = 0; i < it->second.clips.size(); ++i) {
if (it->second.clips[i]->isConnected()) {
hasClipVisible = true;
break;
}
}
it->second.param->setIsSecretAndDisabled(!hasClipVisible);
}
}
void
MultiPlaneEffect::onAllParametersFetched()
{
_imp->refreshSelectorsVisibility();
}
void
MultiPlaneEffect::refreshPlaneChoiceMenus()
{
_imp->buildChannelMenus();
}
void
MultiPlaneEffect::changedParam(const InstanceChangedArgs & /*args*/, const std::string ¶mName)
{
if (_imp->allPlanesCheckbox && paramName == _imp->allPlanesCheckbox->getName()) {
_imp->handleAllPlanesCheckboxParamChanged();
}
}
void
MultiPlaneEffect::changedClip(const InstanceChangedArgs & /*args*/, const std::string &clipName)
{
_imp->refreshSelectorsVisibility();
if (gHostIsNatron3OrGreater && clipName == kOfxImageEffectOutputClipName) {
_imp->buildChannelMenus();
}
}
void
MultiPlaneEffect::getClipPreferences(ClipPreferencesSetter &clipPreferences)
{
// Refresh the channel menus on Natron < 3 or if it has never been refreshed, otherwise this is done in clipChanged in Natron >= 3
if (!gHostIsNatron3OrGreater) {
_imp->buildChannelMenus();
}
// By default set the clip preferences according to what is selected with the output plane selector.
for (map<string, ChoiceParamClips>::iterator it = _imp->params.begin(); it != _imp->params.end(); ++it) {
if (!it->second.isOutput) {
continue;
}
MultiPlane::ImagePlaneDesc dstPlane;
{
OFX::Clip* clip = 0;
int channelIndex = -1;
MultiPlane::MultiPlaneEffect::GetPlaneNeededRetCodeEnum stat = getPlaneNeeded(it->second.param->getName(), &clip, &dstPlane, &channelIndex);
if (stat != MultiPlane::MultiPlaneEffect::eGetPlaneNeededRetCodeReturnedPlane) {
dstPlane = MultiPlane::ImagePlaneDesc::getNoneComponents();
}
}
// Get clip preferences expects a hard coded components string but here we may have any kind of plane.
// To respect OpenFX, we map our plane number of components to the components of the corresponding color plane
// e.g: if our plane is Toto.XYZ and has 3 channels, it becomes RGB
MultiPlane::ImagePlaneDesc colorPlaneMapped = MultiPlane::ImagePlaneDesc::mapNCompsToColorPlane(dstPlane.getNumComponents());
PixelComponentEnum dstPixelComps = mapStrToPixelComponentEnum(MultiPlane::ImagePlaneDesc::mapPlaneToOFXComponentsTypeString(colorPlaneMapped));
clipPreferences.setClipComponents(*it->second.clips[0], dstPixelComps);
}
} // getClipPreferences
OfxStatus
MultiPlaneEffect::getClipComponents(const ClipComponentsArguments& args, ClipComponentsSetter& clipComponents)
{
assert(gHostSupportsMultiPlaneV2 || gHostSupportsMultiPlaneV1);
// Record clips that have already had their planes set because multipla parameters can reference the same clip
std::map<Clip*, std::set<std::string> > clipMap;
bool passThroughClipSet = false;
for (map<string, ChoiceParamClips>::iterator it = _imp->params.begin(); it != _imp->params.end(); ++it) {
MultiPlane::ImagePlaneDesc plane;
OFX::Clip* clip = 0;
int channelIndex = -1;
MultiPlane::MultiPlaneEffect::GetPlaneNeededRetCodeEnum stat = getPlaneNeeded(it->second.param->getName(), &clip, &plane, &channelIndex);