-
Notifications
You must be signed in to change notification settings - Fork 14
/
ofxsCopier.h
1566 lines (1432 loc) · 68.1 KB
/
ofxsCopier.h
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 ***** */
//
// ofxsCopier.h
//
#ifndef IO_ofxsCopier_h
#define IO_ofxsCopier_h
#include <cstring>
#include <algorithm>
#include "ofxsPixelProcessor.h"
#include "ofxsMaskMix.h"
namespace OFX {
// Base class for the RGBA and the Alpha processor
template <class PIX, int nComponents>
class PixelCopier
: public OFX::PixelProcessorFilterBase
{
public:
// ctor
PixelCopier(OFX::ImageEffect &instance)
: OFX::PixelProcessorFilterBase(instance)
{
}
// and do some processing
void multiThreadProcessImages(const OfxRectI& procWindow_, const OfxPointD& rs)
{
unused(rs);
OfxRectI procWindow = procWindow_;
assert(_dstBounds.x1 <= procWindow.x1 && procWindow.x2 <= _dstBounds.x2 && _dstBounds.y1 <= procWindow.y1 && procWindow.y2 <= _dstBounds.y2);
// for more safety, make sure procWindow is within dstBounds (as covered by the above assert)
if (_dstBounds.x1 > procWindow.x1) {
procWindow.x1 = _dstBounds.x1;
}
if (_dstBounds.x2 < procWindow.x2) {
procWindow.x2 = _dstBounds.x2;
}
if (_dstBounds.y1 > procWindow.y1) {
procWindow.y1 = _dstBounds.y1;
}
if (_dstBounds.y2 < procWindow.y2) {
procWindow.y2 = _dstBounds.y2;
}
int rowBytes = sizeof(PIX) * nComponents * (procWindow.x2 - procWindow.x1);
for (int dsty = procWindow.y1; dsty < procWindow.y2; ++dsty) {
if ( _effect.abort() ) {
break;
}
PIX *dstPix = (PIX *) getDstPixelAddress(procWindow.x1, dsty);
assert(dstPix);
if (!dstPix) {
// coverity[dead_error_line]
continue;
}
int srcy = dsty;
if (_srcBoundary == 1) {
if (_srcBounds.y2 <= srcy) {
srcy = _srcBounds.y2 - 1;
}
if (srcy < _srcBounds.y1) {
srcy = _srcBounds.y1;
}
} else if (_srcBoundary == 2) {
if ( (srcy < _srcBounds.y1) || (_srcBounds.y2 <= srcy) ) {
srcy = _srcBounds.y1 + positive_modulo(srcy - _srcBounds.y1, _srcBounds.y2 - _srcBounds.y1);
}
}
if ( (srcy < _srcBounds.y1) || (_srcBounds.y2 <= srcy) || (_srcBounds.y2 <= _srcBounds.y1) ) {
assert(_srcBoundary == 0);
std::memset(dstPix, 0, rowBytes);
} else {
int x1 = (std::max)(_srcBounds.x1, procWindow.x1);
int x2 = (std::min)(_srcBounds.x2, procWindow.x2);
// start of line may be black
if (procWindow.x1 < x1) {
if ( (_srcBoundary != 1) && (_srcBoundary != 2) ) {
std::memset( dstPix, 0, sizeof(PIX) * nComponents * (x1 - procWindow.x1) );
dstPix += nComponents * (x1 - procWindow.x1);
} else if (_srcBoundary == 1) {
const PIX *srcPix = (const PIX *) getSrcPixelAddress(x1, srcy);
assert(srcPix);
if (!srcPix) {
std::memset( dstPix, 0, sizeof(PIX) * nComponents * (x1 - procWindow.x1) );
dstPix += nComponents * (x1 - procWindow.x1);
} else {
# ifdef DEBUG
for (int c = 0; c < nComponents; ++c) {
assert( !OFX::IsNaN(srcPix[c]) ); // check for NaN
}
# endif
for (int x = procWindow.x1; x < x1; ++x) {
std::copy(srcPix, srcPix + nComponents, dstPix);
dstPix += nComponents;
}
}
} else if (_srcBoundary == 2) {
int srcx = procWindow.x1;
if ( (srcx < _srcBounds.x1) || (_srcBounds.x2 <= srcx) ) {
srcx = _srcBounds.x1 + positive_modulo(srcx - _srcBounds.x1, _srcBounds.x2 - _srcBounds.x1);
}
const PIX *srcPix = (const PIX *) getSrcPixelAddress(srcx, srcy);
assert(srcPix);
if (!srcPix) {
std::memset( dstPix, 0, sizeof(PIX) * nComponents * (x1 - procWindow.x1) );
dstPix += nComponents * (x1 - procWindow.x1);
} else {
for (int x = procWindow.x1; x < x1; ++x) {
# ifdef DEBUG
for (int c = 0; c < nComponents; ++c) {
assert( !OFX::IsNaN(srcPix[c]) ); // check for NaN
}
# endif
std::copy(srcPix, srcPix + nComponents, dstPix);
dstPix += nComponents;
++srcx;
if (_srcBounds.x2 <= srcx) {
srcx -= (_srcBounds.x2 - _srcBounds.x1);
srcPix -= (_srcBounds.x2 - _srcBounds.x1) * nComponents;
}
}
}
}
}
// then, copy the relevant fraction of src
if ( (x1 < x2) && (procWindow.x1 <= x1) && (x2 <= procWindow.x2) ) {
const PIX *srcPix = (const PIX *) getSrcPixelAddress(x1, srcy);
assert(srcPix);
if (!srcPix) {
std::memset( dstPix, 0, sizeof(PIX) * nComponents * (x2 - x1) );
} else {
# ifdef DEBUG
for (int c = 0; c < nComponents * (x2 - x1); ++c) {
assert( !OFX::IsNaN(srcPix[c]) ); // check for NaN
}
# endif
std::memcpy( dstPix, srcPix, sizeof(PIX) * nComponents * (x2 - x1) );
}
dstPix += nComponents * (x2 - x1);
}
// end of line may be black
if (x2 < procWindow.x2) {
if ( (_srcBoundary != 1) && (_srcBoundary != 2) ) {
std::memset( dstPix, 0, sizeof(PIX) * nComponents * (procWindow.x2 - x2) );
dstPix += nComponents * (procWindow.x2 - x2);
} else if (_srcBoundary == 1) {
const PIX *srcPix = (const PIX *) getSrcPixelAddress(x2 - 1, srcy);
assert(srcPix);
if (!srcPix) {
std::memset( dstPix, 0, sizeof(PIX) * nComponents * (procWindow.x2 - x2) );
} else {
for (int x = x2; x < procWindow.x2; ++x) {
std::memcpy( dstPix, srcPix, sizeof(PIX) * nComponents );
dstPix += nComponents;
}
}
} else if (_srcBoundary == 2) {
int srcx = x2;
while (_srcBounds.x2 <= srcx) {
srcx -= (_srcBounds.x2 - _srcBounds.x1);
}
const PIX *srcPix = (const PIX *) getSrcPixelAddress(srcx, srcy);
assert(srcPix);
if (!srcPix) {
std::memset( dstPix, 0, sizeof(PIX) * nComponents * (procWindow.x2 - x2) );
dstPix += nComponents * (procWindow.x2 - x2);
} else {
for (int x = x2; x < procWindow.x2; ++x) {
# ifdef DEBUG
for (int c = 0; c < nComponents; ++c) {
assert( !OFX::IsNaN(srcPix[c]) ); // check for NaN
}
# endif
std::copy(srcPix, srcPix + nComponents, dstPix);
dstPix += nComponents;
++srcx;
if (_srcBounds.x2 <= srcx) {
srcx -= (_srcBounds.x2 - _srcBounds.x1);
srcPix -= (_srcBounds.x2 - _srcBounds.x1) * nComponents;
}
}
}
}
}
}
}
} // multiThreadProcessImages
};
/*
* @brief Same as PixelCopier except that the alpha channel is set to maxValue instead of being copied
*/
template <class PIX, int nComponents, int maxValue>
class PixelCopierOpaque
: public OFX::PixelProcessorFilterBase
{
public:
// ctor
PixelCopierOpaque(OFX::ImageEffect &instance)
: OFX::PixelProcessorFilterBase(instance)
{
}
// and do some processing
void multiThreadProcessImages(const OfxRectI& procWindow_, const OfxPointD& rs)
{
unused(rs);
OfxRectI procWindow = procWindow_;
assert(_dstBounds.x1 <= procWindow.x1 && procWindow.x2 <= _dstBounds.x2 && _dstBounds.y1 <= procWindow.y1 && procWindow.y2 <= _dstBounds.y2);
// for more safety, make sure procWindow is within dstBounds (as covered by the above assert)
if (_dstBounds.x1 > procWindow.x1) {
procWindow.x1 = _dstBounds.x1;
}
if (_dstBounds.x2 < procWindow.x2) {
procWindow.x2 = _dstBounds.x2;
}
if (_dstBounds.y1 > procWindow.y1) {
procWindow.y1 = _dstBounds.y1;
}
if (_dstBounds.y2 < procWindow.y2) {
procWindow.y2 = _dstBounds.y2;
}
assert(nComponents == 4 || nComponents == 1);
for (int dsty = procWindow.y1; dsty < procWindow.y2; ++dsty) {
if ( _effect.abort() ) {
break;
}
int srcy = dsty;
if (_srcBoundary == 1) {
if (_srcBounds.y2 <= srcy) {
srcy = _srcBounds.y2 - 1;
}
if (srcy < _srcBounds.y1) {
srcy = _srcBounds.y1;
}
} else if (_srcBoundary == 2) {
if ( (srcy < _srcBounds.y1) || (_srcBounds.y2 <= srcy) ) {
srcy = _srcBounds.y1 + positive_modulo(srcy - _srcBounds.y1, _srcBounds.y2 - _srcBounds.y1);
}
}
PIX *dstPix = (PIX *) getDstPixelAddress(procWindow.x1, dsty);
assert(dstPix);
if (!dstPix) {
// coverity[dead_error_line]
continue;
}
for (int dstx = procWindow.x1; dstx < procWindow.x2; ++dstx) {
int srcx = dstx;
if (_srcBoundary == 1) {
if (_srcBounds.x2 <= srcx) {
srcx = _srcBounds.x2 - 1;
}
if (srcx < _srcBounds.x1) {
srcx = _srcBounds.x1;
}
} else if (_srcBoundary == 2) {
if ( (srcx < _srcBounds.x1) || (_srcBounds.x2 <= srcx) ) {
srcx = _srcBounds.x1 + positive_modulo(srcx - _srcBounds.x1, _srcBounds.x2 - _srcBounds.x1);
}
}
// origPix is at dstx,dsty
const PIX *srcPix = (const PIX *) getSrcPixelAddress(srcx, srcy);
if (srcPix) {
std::copy(srcPix, srcPix + nComponents - 1, dstPix);
dstPix[nComponents - 1] = maxValue;
} else {
std::fill( dstPix, dstPix + nComponents, PIX() ); // no src pixel here, be black and transparent
}
// increment the dst pixel
dstPix += nComponents;
}
}
} // multiThreadProcessImages
};
template <class PIX, int nComponents, int maxValue, bool masked>
class PixelCopierMaskMix
: public OFX::PixelProcessorFilterBase
{
public:
// ctor
PixelCopierMaskMix(OFX::ImageEffect &instance)
: OFX::PixelProcessorFilterBase(instance)
{
}
// and do some processing
void multiThreadProcessImages(const OfxRectI& procWindow_, const OfxPointD& rs)
{
unused(rs);
OfxRectI procWindow = procWindow_;
assert(_dstBounds.x1 <= procWindow.x1 && procWindow.x2 <= _dstBounds.x2 && _dstBounds.y1 <= procWindow.y1 && procWindow.y2 <= _dstBounds.y2);
// for more safety, make sure procWindow is within dstBounds (as covered by the above assert)
if (_dstBounds.x1 > procWindow.x1) {
procWindow.x1 = _dstBounds.x1;
}
if (_dstBounds.x2 < procWindow.x2) {
procWindow.x2 = _dstBounds.x2;
}
if (_dstBounds.y1 > procWindow.y1) {
procWindow.y1 = _dstBounds.y1;
}
if (_dstBounds.y2 < procWindow.y2) {
procWindow.y2 = _dstBounds.y2;
}
float tmpPix[nComponents];
for (int dsty = procWindow.y1; dsty < procWindow.y2; ++dsty) {
if ( _effect.abort() ) {
break;
}
int srcy = dsty;
if (_srcBoundary == 1) {
if (_srcBounds.y2 <= srcy) {
srcy = _srcBounds.y2 - 1;
}
if (srcy < _srcBounds.y1) {
srcy = _srcBounds.y1;
}
} else if (_srcBoundary == 2) {
if ( (srcy < _srcBounds.y1) || (_srcBounds.y2 <= srcy) ) {
srcy = _srcBounds.y1 + positive_modulo(srcy - _srcBounds.y1, _srcBounds.y2 - _srcBounds.y1);
}
}
PIX *dstPix = (PIX *) getDstPixelAddress(procWindow.x1, dsty);
assert(dstPix);
if (!dstPix) {
// coverity[dead_error_line]
continue;
}
for (int dstx = procWindow.x1; dstx < procWindow.x2; ++dstx) {
int srcx = dstx;
if (_srcBoundary == 1) {
if (_srcBounds.x2 <= srcx) {
srcx = _srcBounds.x2 - 1;
}
if (srcx < _srcBounds.x1) {
srcx = _srcBounds.x1;
}
} else if (_srcBoundary == 2) {
if ( (srcx < _srcBounds.x1) || (_srcBounds.x2 <= srcx) ) {
srcx = _srcBounds.x1 + positive_modulo(srcx - _srcBounds.x1, _srcBounds.x2 - _srcBounds.x1);
}
}
// origPix is at dstx,dsty
const PIX *origPix = (const PIX *) (_origImg ? _origImg->getPixelAddress(dstx, dsty) : 0);
const PIX *srcPix = (const PIX *) getSrcPixelAddress(srcx, srcy);
if (srcPix) {
std::copy(srcPix, srcPix + nComponents, tmpPix);
} else {
std::fill(tmpPix, tmpPix + nComponents, 0.f); // no src pixel here, be black and transparent
}
// dstx,dsty are the mask image coordinates (no boundary conditions)
ofxsMaskMixPix<PIX, nComponents, maxValue, masked>(tmpPix, dstx, dsty, origPix, _doMasking, _maskImg, (float)_mix, _maskInvert, dstPix);
// increment the dst pixel
dstPix += nComponents;
}
}
} // multiThreadProcessImages
};
template <class SRCPIX, int srcNComponents, int srcMaxValue, class DSTPIX, int dstNComponents, int dstMaxValue>
class PixelCopierUnPremult
: public OFX::PixelProcessorFilterBase
{
public:
// ctor
PixelCopierUnPremult(OFX::ImageEffect &instance)
: OFX::PixelProcessorFilterBase(instance)
{
assert( (srcNComponents == 3 || srcNComponents == 4) && (dstNComponents == 3 || dstNComponents == 4) );
}
// and do some processing
void multiThreadProcessImages(const OfxRectI& procWindow_, const OfxPointD& rs)
{
unused(rs);
OfxRectI procWindow = procWindow_;
assert(_dstBounds.x1 <= procWindow.x1 && procWindow.x2 <= _dstBounds.x2 && _dstBounds.y1 <= procWindow.y1 && procWindow.y2 <= _dstBounds.y2);
// for more safety, make sure procWindow is within dstBounds (as covered by the above assert)
if (_dstBounds.x1 > procWindow.x1) {
procWindow.x1 = _dstBounds.x1;
}
if (_dstBounds.x2 < procWindow.x2) {
procWindow.x2 = _dstBounds.x2;
}
if (_dstBounds.y1 > procWindow.y1) {
procWindow.y1 = _dstBounds.y1;
}
if (_dstBounds.y2 < procWindow.y2) {
procWindow.y2 = _dstBounds.y2;
}
float unpPix[4] = {0.f, 0.f, 0.f, 0.f};
for (int dsty = procWindow.y1; dsty < procWindow.y2; ++dsty) {
if ( _effect.abort() ) {
break;
}
int srcy = dsty;
if (_srcBoundary == 1) {
if (_srcBounds.y2 <= srcy) {
srcy = _srcBounds.y2 - 1;
}
if (srcy < _srcBounds.y1) {
srcy = _srcBounds.y1;
}
} else if (_srcBoundary == 2) {
if ( (srcy < _srcBounds.y1) || (_srcBounds.y2 <= srcy) ) {
srcy = _srcBounds.y1 + positive_modulo(srcy - _srcBounds.y1, _srcBounds.y2 - _srcBounds.y1);
}
}
DSTPIX *dstPix = (DSTPIX *) getDstPixelAddress(procWindow.x1, dsty);
assert(dstPix);
if (!dstPix) {
// coverity[dead_error_line]
continue;
}
for (int dstx = procWindow.x1; dstx < procWindow.x2; ++dstx) {
int srcx = dstx;
if (_srcBoundary == 1) {
if (_srcBounds.x2 <= srcx) {
srcx = _srcBounds.x2 - 1;
}
if (srcx < _srcBounds.x1) {
srcx = _srcBounds.x1;
}
} else if (_srcBoundary == 2) {
if ( (srcx < _srcBounds.x1) || (_srcBounds.x2 <= srcx) ) {
srcx = _srcBounds.x1 + positive_modulo(srcx - _srcBounds.x1, _srcBounds.x2 - _srcBounds.x1);
}
}
const SRCPIX *srcPix = (const SRCPIX *) getSrcPixelAddress(srcx, srcy);
if (!srcPix) {
// no source, be black and transparent
for (int c = 0; c < dstNComponents; ++c) {
dstPix[c] = DSTPIX();
}
} else {
ofxsUnPremult<SRCPIX, srcNComponents, srcMaxValue>(srcPix, unpPix, _premult, _premultChannel);
for (int c = 0; c < dstNComponents; ++c) {
float v = unpPix[c] * dstMaxValue;
dstPix[c] = ofxsClampIfInt<DSTPIX, dstMaxValue>(v, 0, dstMaxValue);
}
}
// increment the dst pixel
dstPix += dstNComponents;
}
}
} // multiThreadProcessImages
};
template <class SRCPIX, int srcNComponents, int srcMaxValue, class DSTPIX, int dstNComponents, int dstMaxValue>
class PixelCopierPremult
: public OFX::PixelProcessorFilterBase
{
public:
// ctor
PixelCopierPremult(OFX::ImageEffect &instance)
: OFX::PixelProcessorFilterBase(instance)
{
assert(srcMaxValue);
assert( (srcNComponents == 3 || srcNComponents == 4) && (dstNComponents == 3 || dstNComponents == 4) );
}
// and do some processing
void multiThreadProcessImages(const OfxRectI& procWindow_, const OfxPointD& rs)
{
unused(rs);
OfxRectI procWindow = procWindow_;
assert(_dstBounds.x1 <= procWindow.x1 && procWindow.x2 <= _dstBounds.x2 && _dstBounds.y1 <= procWindow.y1 && procWindow.y2 <= _dstBounds.y2);
// for more safety, make sure procWindow is within dstBounds (as covered by the above assert)
if (_dstBounds.x1 > procWindow.x1) {
procWindow.x1 = _dstBounds.x1;
}
if (_dstBounds.x2 < procWindow.x2) {
procWindow.x2 = _dstBounds.x2;
}
if (_dstBounds.y1 > procWindow.y1) {
procWindow.y1 = _dstBounds.y1;
}
if (_dstBounds.y2 < procWindow.y2) {
procWindow.y2 = _dstBounds.y2;
}
for (int dsty = procWindow.y1; dsty < procWindow.y2; ++dsty) {
if ( _effect.abort() ) {
break;
}
int srcy = dsty;
if (_srcBoundary == 1) {
if (_srcBounds.y2 <= srcy) {
srcy = _srcBounds.y2 - 1;
}
if (srcy < _srcBounds.y1) {
srcy = _srcBounds.y1;
}
} else if (_srcBoundary == 2) {
if ( (srcy < _srcBounds.y1) || (_srcBounds.y2 <= srcy) ) {
srcy = _srcBounds.y1 + positive_modulo(srcy - _srcBounds.y1, _srcBounds.y2 - _srcBounds.y1);
}
}
DSTPIX *dstPix = (DSTPIX *) getDstPixelAddress(procWindow.x1, dsty);
assert(dstPix);
if (!dstPix) {
// coverity[dead_error_line]
continue;
}
for (int dstx = procWindow.x1; dstx < procWindow.x2; ++dstx) {
int srcx = dstx;
if (_srcBoundary == 1) {
if (_srcBounds.x2 <= srcx) {
srcx = _srcBounds.x2 - 1;
}
if (srcx < _srcBounds.x1) {
srcx = _srcBounds.x1;
}
} else if (_srcBoundary == 2) {
if ( (srcx < _srcBounds.x1) || (_srcBounds.x2 <= srcx) ) {
srcx = _srcBounds.x1 + positive_modulo(srcx - _srcBounds.x1, _srcBounds.x2 - _srcBounds.x1);
}
}
const SRCPIX *srcPix = (const SRCPIX *) getSrcPixelAddress(srcx, srcy);
if (!srcPix) {
// no source, be black and transparent
for (int c = 0; c < dstNComponents; ++c) {
dstPix[c] = DSTPIX();
}
} else {
float unpPix[4] = {0.f, 0.f, 0.f, 0.f};
if (srcNComponents == 1) {
unpPix[3] = srcPix[0] * (1.f / srcMaxValue);
} else {
unpPix[0] = srcPix[0] * (1.f / srcMaxValue);
unpPix[1] = srcPix[1] * (1.f / srcMaxValue);
unpPix[2] = (srcNComponents > 2) ? (srcPix[2] * (1.f / srcMaxValue)) : 0.f;
unpPix[3] = (srcNComponents == 4) ? (srcPix[3] * (1.f / srcMaxValue)) : 1.0f;
}
float pPix[dstNComponents];
// unpPix is in [0, 1]
// premultiply and denormalize in [0, maxValue]
// if premult is false, just denormalize
ofxsPremult<DSTPIX, dstNComponents, dstMaxValue>(unpPix, pPix, _premult, _premultChannel);
for (int c = 0; c < dstNComponents; ++c) {
dstPix[c] = ofxsClampIfInt<DSTPIX, dstMaxValue>(pPix[c], 0, dstMaxValue);
}
}
// increment the dst pixel
dstPix += dstNComponents;
}
}
} // multiThreadProcessImages
};
// _srcBoundarys The border condition type { 0=zero | 1=dirichlet | 2=periodic }.
// template to do the RGBA processing
template <class SRCPIX, int srcNComponents, int srcMaxValue, class DSTPIX, int dstNComponents, int dstMaxValue>
class PixelCopierPremultMaskMix
: public OFX::PixelProcessorFilterBase
{
public:
// ctor
PixelCopierPremultMaskMix(OFX::ImageEffect &instance)
: OFX::PixelProcessorFilterBase(instance)
{
assert( (srcNComponents == 3 || srcNComponents == 4) && (dstNComponents == 3 || dstNComponents == 4) );
}
// and do some processing
void multiThreadProcessImages(const OfxRectI& procWindow_, const OfxPointD& rs)
{
unused(rs);
OfxRectI procWindow = procWindow_;
assert(_dstBounds.x1 <= procWindow.x1 && procWindow.x2 <= _dstBounds.x2 && _dstBounds.y1 <= procWindow.y1 && procWindow.y2 <= _dstBounds.y2);
// for more safety, make sure procWindow is within dstBounds (as covered by the above assert)
if (_dstBounds.x1 > procWindow.x1) {
procWindow.x1 = _dstBounds.x1;
}
if (_dstBounds.x2 < procWindow.x2) {
procWindow.x2 = _dstBounds.x2;
}
if (_dstBounds.y1 > procWindow.y1) {
procWindow.y1 = _dstBounds.y1;
}
if (_dstBounds.y2 < procWindow.y2) {
procWindow.y2 = _dstBounds.y2;
}
float unpPix[4] = {0.f, 0.f, 0.f, 0.f};
for (int dsty = procWindow.y1; dsty < procWindow.y2; ++dsty) {
if ( _effect.abort() ) {
break;
}
int srcy = dsty;
if (_srcBoundary == 1) {
if (_srcBounds.y2 <= srcy) {
srcy = _srcBounds.y2 - 1;
}
if (srcy < _srcBounds.y1) {
srcy = _srcBounds.y1;
}
} else if (_srcBoundary == 2) {
if ( (srcy < _srcBounds.y1) || (_srcBounds.y2 <= srcy) ) {
srcy = _srcBounds.y1 + positive_modulo(srcy - _srcBounds.y1, _srcBounds.y2 - _srcBounds.y1);
}
}
DSTPIX *dstPix = (DSTPIX *) getDstPixelAddress(procWindow.x1, dsty);
assert(dstPix);
if (!dstPix) {
// coverity[dead_error_line]
continue;
}
for (int dstx = procWindow.x1; dstx < procWindow.x2; ++dstx) {
int srcx = dstx;
if (_srcBoundary == 1) {
if (_srcBounds.x2 <= srcx) {
srcx = _srcBounds.x2 - 1;
}
if (srcx < _srcBounds.x1) {
srcx = _srcBounds.x1;
}
} else if (_srcBoundary == 2) {
if ( (srcx < _srcBounds.x1) || (_srcBounds.x2 <= srcx) ) {
srcx = _srcBounds.x1 + positive_modulo(srcx - _srcBounds.x1, _srcBounds.x2 - _srcBounds.x1);
}
}
// origPix is at dstx,dsty
const DSTPIX *origPix = (const DSTPIX *) (_origImg ? _origImg->getPixelAddress(dstx, dsty) : 0);
const SRCPIX *srcPix = (const SRCPIX *) getSrcPixelAddress(srcx, srcy);
if (!srcPix) {
// no source, be black and transparent
for (int c = 0; c < dstNComponents; ++c) {
dstPix[c] = DSTPIX();
}
} else {
if (srcNComponents == 1) {
unpPix[3] = srcPix[0] * (1.f / srcMaxValue);
} else {
unpPix[0] = srcPix[0] * (1.f / srcMaxValue);
unpPix[1] = srcPix[1] * (1.f / srcMaxValue);
unpPix[2] = (srcNComponents > 2) ? (srcPix[2] * (1.f / srcMaxValue)) : 0.f;
unpPix[3] = (srcNComponents == 4) ? (srcPix[3] * (1.f / srcMaxValue)) : 1.0f;
}
// dstx,dsty are the mask image coordinates (no boundary conditions)
ofxsPremultMaskMixPix<DSTPIX, dstNComponents, dstMaxValue, true>(unpPix, _premult, _premultChannel, dstx, dsty, origPix, _doMasking, _maskImg, (float)_mix, _maskInvert, dstPix);
}
// increment the dst pixel
dstPix += dstNComponents;
}
}
} // multiThreadProcessImages
};
template <class PIX>
class BlackFiller
: public OFX::PixelProcessorFilterBase
{
public:
// ctor
BlackFiller(OFX::ImageEffect &instance,
int comps)
: OFX::PixelProcessorFilterBase(instance)
, _nComponents(comps)
{
}
// and do some processing
void multiThreadProcessImages(const OfxRectI& procWindow_, const OfxPointD& rs)
{
unused(rs);
OfxRectI procWindow = procWindow_;
assert(_dstBounds.x1 <= procWindow.x1 && procWindow.x2 <= _dstBounds.x2 && _dstBounds.y1 <= procWindow.y1 && procWindow.y2 <= _dstBounds.y2);
// for more safety, make sure procWindow is within dstBounds (as covered by the above assert)
if (_dstBounds.x1 > procWindow.x1) {
procWindow.x1 = _dstBounds.x1;
}
if (_dstBounds.x2 < procWindow.x2) {
procWindow.x2 = _dstBounds.x2;
}
if (_dstBounds.y1 > procWindow.y1) {
procWindow.y1 = _dstBounds.y1;
}
if (_dstBounds.y2 < procWindow.y2) {
procWindow.y2 = _dstBounds.y2;
}
int rowSize = _nComponents * (procWindow.x2 - procWindow.x1);
for (int y = procWindow.y1; y < procWindow.y2; ++y) {
if ( _effect.abort() ) {
break;
}
PIX *dstPix = (PIX *) getDstPixelAddress(procWindow.x1, y);
assert(dstPix);
if (!dstPix) {
// coverity[dead_error_line]
continue;
}
std::fill( dstPix, dstPix + rowSize, PIX() );
}
}
private:
int _nComponents;
};
// black fillers, non-threaded versions
template<class PIX>
void
fillBlackNTForDepth(const OfxRectI & renderWindow,
const OfxPointD& renderScale,
void *dstPixelData,
const OfxRectI & dstBounds,
int dstPixelComponentCount,
int dstRowBytes)
{
unused(renderScale);
assert(dstPixelData);
// do the rendering
int dstRowElements = dstRowBytes / sizeof(PIX);
int x1 = (std::max)(renderWindow.x1, dstBounds.x1);
int x2 = (std::min)(renderWindow.x2, dstBounds.x2);
int y1 = (std::max)(renderWindow.y1, dstBounds.y1);
int y2 = (std::min)(renderWindow.y2, dstBounds.y2);
PIX* dstPixels = (PIX*)dstPixelData + (size_t)(y1 - dstBounds.y1) * dstRowElements + (x1 - dstBounds.x1) * dstPixelComponentCount;
int rowElements = dstPixelComponentCount * (x2 - renderWindow.x1);
for (int y = y1; y < y2; ++y, dstPixels += dstRowElements) {
std::fill( dstPixels, dstPixels + rowElements, PIX() ); // no src pixel here, be black and transparent
}
}
inline void
fillBlackNT(const OfxRectI & renderWindow,
const OfxPointD& renderScale,
void *dstPixelData,
const OfxRectI & dstBounds,
int dstPixelComponentCount,
OFX::BitDepthEnum dstBitDepth,
int dstRowBytes)
{
assert(dstPixelData);
if (!dstPixelData) {
// coverity[dead_error_line]
return;
}
# ifndef NDEBUG
// do the rendering
if ( (dstBitDepth != OFX::eBitDepthUByte) && (dstBitDepth != OFX::eBitDepthUShort) && (dstBitDepth != OFX::eBitDepthHalf) && (dstBitDepth != OFX::eBitDepthFloat) ) {
OFX::throwSuiteStatusException(kOfxStatErrFormat);
return;
}
# endif
if (dstBitDepth == OFX::eBitDepthUByte) {
fillBlackNTForDepth<unsigned char>(renderWindow, renderScale,
dstPixelData, dstBounds, dstPixelComponentCount, dstRowBytes);
} else if ( (dstBitDepth == OFX::eBitDepthUShort) || (dstBitDepth == OFX::eBitDepthHalf) ) {
fillBlackNTForDepth<unsigned short>(renderWindow, renderScale,
dstPixelData, dstBounds, dstPixelComponentCount, dstRowBytes);
} else if (dstBitDepth == OFX::eBitDepthFloat) {
fillBlackNTForDepth<float>(renderWindow, renderScale,
dstPixelData, dstBounds, dstPixelComponentCount, dstRowBytes);
} // switch
}
inline void
fillBlackNT(const OfxRectI & renderWindow,
const OfxPointD& renderScale,
OFX::Image* dstImg)
{
void* dstPixelData;
OfxRectI dstBounds;
OFX::PixelComponentEnum dstPixelComponents;
OFX::BitDepthEnum dstBitDepth;
int dstRowBytes;
assert(dstImg);
if (!dstImg) {
// coverity[dead_error_line]
return;
}
getImageData(dstImg, &dstPixelData, &dstBounds, &dstPixelComponents, &dstBitDepth, &dstRowBytes);
int dstPixelComponentCount = dstImg->getPixelComponentCount();
return fillBlackNT(renderWindow, renderScale, dstPixelData, dstBounds, dstPixelComponentCount, dstBitDepth, dstRowBytes);
}
#if 0 // Don't use threaded version: very probably less efficient
// black fillers, threaded versions
template<class PIX, int nComponents>
void
fillBlackForDepthAndComponents(OFX::ImageEffect &instance,
const OfxRectI & renderWindow,
const OfxPointD& renderScale,
PIX *dstPixelData,
const OfxRectI & dstBounds,
OFX::PixelComponentEnum dstPixelComponents,
int dstPixelComponentCount,
OFX::BitDepthEnum dstBitDepth,
int dstRowBytes)
{
(void)dstPixelComponents;
(void)dstBitDepth;
OFX::BlackFiller<PIX> processor(instance, nComponents);
// set the images
processor.setDstImg(dstPixelData, dstBounds, dstPixelComponents, dstPixelComponentCount, dstBitDepth, dstRowBytes);
// set the render window
processor.setRenderWindow(renderWindow, renderScale);
// Call the base class process member, this will call the derived templated process code
processor.process();
}
template<class PIX>
void
fillBlackForDepth(OFX::ImageEffect &instance,
const OfxRectI & renderWindow,
const OfxPointD& renderScale,
void *dstPixelData,
const OfxRectI & dstBounds,
OFX::PixelComponentEnum dstPixelComponents,
int dstPixelComponentCount,
OFX::BitDepthEnum dstBitDepth,
int dstRowBytes)
{
assert(dstPixelData);
if (!dstPixelData) {
// coverity[dead_error_line]
return;
}
// do the rendering
if ( (dstPixelComponentCount < 0) || (4 < dstPixelComponentCount) ) {
OFX::throwSuiteStatusException(kOfxStatErrFormat);
return;
}
if (dstPixelComponentCount == 4) {
fillBlackForDepthAndComponents<PIX, 4>(instance, renderWindow, renderScale,
(PIX *)dstPixelData, dstBounds, dstPixelComponents, dstPixelComponentCount, dstBitDepth, dstRowBytes);
} else if (dstPixelComponentCount == 3) {
fillBlackForDepthAndComponents<PIX, 3>(instance, renderWindow, renderScale,
(PIX *)dstPixelData, dstBounds, dstPixelComponents, dstPixelComponentCount, dstBitDepth, dstRowBytes);
} else if (dstPixelComponentCount == 2) {
fillBlackForDepthAndComponents<PIX, 2>(instance, renderWindow, renderScale,
(PIX *)dstPixelData, dstBounds, dstPixelComponents, dstPixelComponentCount, dstBitDepth, dstRowBytes);
} else if (dstPixelComponentCount == 1) {
fillBlackForDepthAndComponents<PIX, 1>(instance, renderWindow, renderScale,
(PIX *)dstPixelData, dstBounds, dstPixelComponents, dstPixelComponentCount, dstBitDepth, dstRowBytes);
} // switch
}
inline void
fillBlack(OFX::ImageEffect &instance,
const OfxRectI & renderWindow,
const OfxPointD& renderScale,
void *dstPixelData,
const OfxRectI & dstBounds,
OFX::PixelComponentEnum dstPixelComponents,
int dstPixelComponentCount,
OFX::BitDepthEnum dstBitDepth,
int dstRowBytes)
{
assert(dstPixelData);
if (!dstPixelData) {
// coverity[dead_error_line]
return;
}
// do the rendering
if ( (dstBitDepth != OFX::eBitDepthUByte) && (dstBitDepth != OFX::eBitDepthUShort) && (dstBitDepth != OFX::eBitDepthHalf) && (dstBitDepth != OFX::eBitDepthFloat) ) {
OFX::throwSuiteStatusException(kOfxStatErrFormat);
return;
}
if (dstBitDepth == OFX::eBitDepthUByte) {
fillBlackForDepth<unsigned char>(instance, renderWindow, renderScale,
dstPixelData, dstBounds, dstPixelComponents, dstPixelComponentCount, dstBitDepth, dstRowBytes);
} else if ( (dstBitDepth == OFX::eBitDepthUShort) || (dstBitDepth == OFX::eBitDepthHalf) ) {
fillBlackForDepth<unsigned short>(instance, renderWindow, renderScale,
dstPixelData, dstBounds, dstPixelComponents, dstPixelComponentCount, dstBitDepth, dstRowBytes);
} else if (dstBitDepth == OFX::eBitDepthFloat) {
fillBlackForDepth<float>(instance, renderWindow, renderScale,
dstPixelData, dstBounds, dstPixelComponents, dstPixelComponentCount, dstBitDepth, dstRowBytes);
} // switch
}
inline void
fillBlack(OFX::ImageEffect &instance,
const OfxRectI & renderWindow,
const OfxPointD& renderScale,
OFX::Image* dstImg)
{
void* dstPixelData;
OfxRectI dstBounds;
OFX::PixelComponentEnum dstPixelComponents;
OFX::BitDepthEnum dstBitDepth;
int dstRowBytes;
assert(dstImg);
if (!dstImg) {
// coverity[dead_error_line]
return;
}
getImageData(dstImg, &dstPixelData, &dstBounds, &dstPixelComponents, &dstBitDepth, &dstRowBytes);
int dstPixelComponentCount = dstImg->getPixelComponentCount();
return fillBlack(instance, renderWindow, renderScale, dstPixelData, dstBounds, dstPixelComponents, dstPixelComponentCount, dstBitDepth, dstRowBytes);
}
#else // if 0
// Use non-threaded version: probably more efficient
inline void
fillBlack(OFX::ImageEffect &instance,
const OfxRectI & renderWindow,
const OfxPointD& renderScale,
void *dstPixelData,
const OfxRectI & dstBounds,
OFX::PixelComponentEnum dstPixelComponents,
int dstPixelComponentCount,
OFX::BitDepthEnum dstBitDepth,
int dstRowBytes)
{
(void)instance;
(void)dstPixelComponents;
return fillBlackNT(renderWindow, renderScale, dstPixelData, dstBounds, dstPixelComponentCount, dstBitDepth, dstRowBytes);
}
inline void
fillBlack(OFX::ImageEffect &instance,
const OfxRectI & renderWindow,
const OfxPointD& renderScale,
OFX::Image* dstImg)
{
(void)instance;