-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathofxsFilter.h
1390 lines (1266 loc) · 49.4 KB
/
ofxsFilter.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 ***** */
/*
* OFX Filter/Interpolation help functions
*/
#ifndef openfx_supportext_ofxsFilter_h
#define openfx_supportext_ofxsFilter_h
#include <cmath>
#include <cassert>
#include <algorithm>
#include "ofxsImageEffect.h"
namespace OFX {
// GENERIC
#define kParamFilterType "filter"
#define kParamFilterTypeLabel "Filter"
#define kParamFilterTypeHint "Filtering algorithm - some filters may produce values outside of the initial range (*) or modify the values even if there is no movement (+)."
#define kParamFilterClamp "clamp"
#define kParamFilterClampLabel "Clamp"
#define kParamFilterClampHint "Clamp filter output within the original range - useful to avoid negative values in mattes"
#define kParamFilterBlackOutside "black_outside"
#define kParamFilterBlackOutsideLabel "Black outside"
#define kParamFilterBlackOutsideHint "Fill the area outside the source image with black"
enum FilterEnum
{
eFilterImpulse,
eFilterBox,
eFilterBilinear,
eFilterCubic,
eFilterKeys,
eFilterSimon,
eFilterRifman,
eFilterMitchell,
eFilterParzen,
eFilterNotch,
};
#define kFilterImpulse "Impulse", "(nearest neighbor / box) Use original values.", "impulse"
#define kFilterBox "Box", "Integrate the source image over the bounding box of the back-transformed pixel.", "box"
#define kFilterBilinear "Bilinear", "(tent / triangle) Bilinear interpolation between original values.", "bilinear"
#define kFilterCubic "Cubic", "(cubic spline) Some smoothing.", "cubic"
#define kFilterKeys "Keys", "(Catmull-Rom / Hermite spline) Some smoothing, plus minor sharpening (*).", "keys"
#define kFilterSimon "Simon", "Some smoothing, plus medium sharpening (*).", "simon"
#define kFilterRifman "Rifman", "Some smoothing, plus significant sharpening (*).", "rifman"
#define kFilterMitchell "Mitchell", "Some smoothing, plus blurring to hide pixelation (*)(+).", "mitchell"
#define kFilterParzen "Parzen", "(cubic B-spline) Greatest smoothing of all filters (+).", "parzen"
#define kFilterNotch "Notch", "Flat smoothing (which tends to hide moire' patterns) (+).", "notch"
inline
void
ofxsFilterDescribeParamsInterpolate2D(OFX::ImageEffectDescriptor &desc,
OFX::PageParamDescriptor *page,
bool blackOutsideDefault = true)
{
// GENERIC PARAMETERS
//
{
OFX::ChoiceParamDescriptor* param = desc.defineChoiceParam(kParamFilterType);
param->setLabel(kParamFilterTypeLabel);
param->setHint(kParamFilterTypeHint);
assert(param->getNOptions() == eFilterImpulse);
param->appendOption(kFilterImpulse);
assert(param->getNOptions() == eFilterBox);
param->appendOption(kFilterBox);
assert(param->getNOptions() == eFilterBilinear);
param->appendOption(kFilterBilinear);
assert(param->getNOptions() == eFilterCubic);
param->appendOption(kFilterCubic);
assert(param->getNOptions() == eFilterKeys);
param->appendOption(kFilterKeys);
assert(param->getNOptions() == eFilterSimon);
param->appendOption(kFilterSimon);
assert(param->getNOptions() == eFilterRifman);
param->appendOption(kFilterRifman);
assert(param->getNOptions() == eFilterMitchell);
param->appendOption(kFilterMitchell);
assert(param->getNOptions() == eFilterParzen);
param->appendOption(kFilterParzen);
assert(param->getNOptions() == eFilterNotch);
param->appendOption(kFilterNotch);
param->setDefault(eFilterCubic);
param->setAnimates(true);
#ifdef OFX_EXTENSIONS_NUKE
param->setLayoutHint(OFX::eLayoutHintNoNewLine, 1);
#endif
if (page) {
page->addChild(*param);
}
}
// clamp
{
OFX::BooleanParamDescriptor* param = desc.defineBooleanParam(kParamFilterClamp);
param->setLabel(kParamFilterClampLabel);
param->setHint(kParamFilterClampHint);
param->setDefault(false);
param->setAnimates(true);
#ifdef OFX_EXTENSIONS_NUKE
param->setLayoutHint(OFX::eLayoutHintNoNewLine, 1);
#endif
if (page) {
page->addChild(*param);
}
}
// blackOutside
{
OFX::BooleanParamDescriptor* param = desc.defineBooleanParam(kParamFilterBlackOutside);
param->setLabel(kParamFilterBlackOutsideLabel);
param->setHint(kParamFilterBlackOutsideHint);
param->setDefault(blackOutsideDefault);
param->setAnimates(true);
if (page) {
page->addChild(*param);
}
}
} // ofxsFilterDescribeParamsInterpolate2D
/*
Maple code to compute the filters.
# Mitchell, D. and A. Netravali, "Reconstruction Filters in Computer Graphics."
# http://www.cs.utexas.edu/users/fussell/courses/cs384g/lectures/mitchell/Mitchell.pdf
# Computer Graphics, Vol. 22, No. 4, pp. 221-228.
# (B, C)
# (1/3, 1/3) - Defaults recommended by Mitchell and Netravali
# (1, 0) - Equivalent to the Cubic B-Spline
# (0, 0.5) - Equivalent to the Catmull-Rom Spline
# (0, C) - The family of Cardinal Cubic Splines
# (B, 0) - Duff's tensioned B-Splines.
unassign('Ip'):unassign('Ic'):unassign('In'):unassign('Ia'):
unassign('Jp'):unassign('Jc'):unassign('Jn'):unassign('Ja'):
P:= x -> ((12-9*B-6*C)*x**3 + (-18+12*B+6*C)*x**2+(6-2*B))/6;
Q:= x -> ((-B-6*C)*x**3 + (6*B+30*C)*x**2 + (-12*B-48*C)*x + (8*B+24*C))/6;
R := d -> Q(d+1)*Ip + P(d)*Ic + P(1-d) * In + Q(2-d)*Ia;
# how does it perform on a linear function?
R0 := d -> Q(d+1)*(Ic-1) + P(d)*Ic + P(1-d) * (Ic+1) + Q(2-d)*(Ic+2);
# Cubic (cubic splines - depends only on In and Ic, derivatives are 0 at the center of each sample)
collect(subs({B=0,C=0},R(d)),d);
collect(subs({B=0,C=0},R0(d)),d);
# Catmull-Rom / Keys / Hermite spline - gives linear func if input is linear
collect(subs({B=0,C=0.5},R(d)),d);
collect(subs({B=0,C=0.5},R0(d)),d);
# Simon
collect(subs({B=0,C=0.75},R(d)),d);
collect(subs({B=0,C=0.75},R0(d)),d);
# Rifman
collect(subs({B=0,C=1.},R(d)),d);
collect(subs({B=0,C=1.},R0(d)),d);
# Mitchell - gives linear func if input is linear
collect(subs({B=1/3, C=1/3},R(d)),d);
collect(subs({B=1/3, C=1/3},R0(d)),d);
# Parzen (Cubic B-spline) - gives linear func if input is linear
collect(subs({B=1,C=0},R(d)),d);
collect(subs({B=1,C=0},R0(d)),d);
# Notch - gives linear func if input is linear
collect(subs({B=3/2,C=-1/4},R(d)),d);
collect(subs({B=3/2,C=-1/4},R0(d)),d);
*/
inline double
ofxsFilterClampVal(double I,
double Ic,
double In)
{
double Imin = (std::min)(Ic, In);
if (I < Imin) {
return Imin;
}
double Imax = (std::max)(Ic, In);
if (I > Imax) {
return Imax;
}
return I;
}
inline
double
ofxsFilterLinear(double Ic,
double In,
double d)
{
return Ic + d * (In - Ic);
}
static inline
double
ofxsFilterCubic(double Ic,
double In,
double d,
bool clamp)
{
double I = Ic + d * d * ( (-3 * Ic + 3 * In ) + d * (2 * Ic - 2 * In ) );
if (clamp) {
I = ofxsFilterClampVal(I, Ic, In);
}
return I;
}
inline
double
ofxsFilterKeys(double Ip,
double Ic,
double In,
double Ia,
double d,
bool clamp)
{
double I = Ic + d * ( (-Ip + In ) + d * ( (2 * Ip - 5 * Ic + 4 * In - Ia ) + d * (-Ip + 3 * Ic - 3 * In + Ia ) ) ) / 2;
if (clamp) {
I = ofxsFilterClampVal(I, Ic, In);
}
return I;
}
inline
double
ofxsFilterSimon(double Ip,
double Ic,
double In,
double Ia,
double d,
bool clamp)
{
double I = Ic + d * ( (-3 * Ip + 3 * In ) + d * ( (6 * Ip - 9 * Ic + 6 * In - 3 * Ia ) + d * (-3 * Ip + 5 * Ic - 5 * In + 3 * Ia ) ) ) / 4;
if (clamp) {
I = ofxsFilterClampVal(I, Ic, In);
}
return I;
}
inline
double
ofxsFilterRifman(double Ip,
double Ic,
double In,
double Ia,
double d,
bool clamp)
{
double I = Ic + d * ( (-Ip + In ) + d * ( (2 * Ip - 2 * Ic + In - Ia ) + d * (-Ip + Ic - In + Ia ) ) );
if (clamp) {
I = ofxsFilterClampVal(I, Ic, In);
}
return I;
}
inline
double
ofxsFilterMitchell(double Ip,
double Ic,
double In,
double Ia,
double d,
bool clamp)
{
double I = ( Ip + 16 * Ic + In + d * ( (-9 * Ip + 9 * In ) + d * ( (15 * Ip - 36 * Ic + 27 * In - 6 * Ia ) + d * (-7 * Ip + 21 * Ic - 21 * In + 7 * Ia ) ) ) ) / 18;
if (clamp) {
I = ofxsFilterClampVal(I, Ic, In);
}
return I;
}
inline
double
ofxsFilterParzen(double Ip,
double Ic,
double In,
double Ia,
double d,
bool /*clamp*/)
{
double I = ( Ip + 4 * Ic + In + d * ( (-3 * Ip + 3 * In ) + d * ( (3 * Ip - 6 * Ic + 3 * In ) + d * (-Ip + 3 * Ic - 3 * In + Ia ) ) ) ) / 6;
// clamp is not necessary for Parzen
return I;
}
inline
double
ofxsFilterNotch(double Ip,
double Ic,
double In,
double Ia,
double d,
bool /*clamp*/)
{
double I = ( Ip + 2 * Ic + In + d * ( (-2 * Ip + 2 * In ) + d * ( (Ip - Ic - In + Ia ) ) ) ) / 4;
// clamp is not necessary for Notch
return I;
}
/////////////////////////////////////////////////
// BOX FILTER START
/////////////////////////////////////////////////
/// @brief Add to vector v the integral of the signal contained in l, seen as piecewise constant, from x1 to x2.
/// x = 0 corresponds to the left of the first pixel, x = 1 corresponds to the right of the first pixel / left of the second pixel
///
template <class PIX>
void
ofxsFilterIntegrate1d(const PIX* l, // pointer to data start
const size_t nsamples, // number of samples in the line
const size_t stride, // increment from one data point to the next
const size_t depth, // dimension of each sample, also the dimension of result vector v
const double x1,
const double x2,
const bool zeroOutside, // if true, outside of the data is zero. If false, use Neumann boundary conditions (outside is the closest data point)
float *v) // vector of dimension depth containing the result
{
assert(x2 >= x1);
assert(stride >= depth);
size_t ifirst, ilast; // index of the first/last pixel
double fracfirst, fraclast; // fraction to remove from the first/last pixel
if (x1 < 0.) {
ifirst = 0;
fracfirst = 0.;
} else if (nsamples <= x1) {
ifirst = nsamples - 1;
fracfirst = 0.;
} else {
ifirst = (size_t)floor(x1);
fracfirst = x1 - ifirst;
}
if (x2 < 0.) {
ilast = 0;
fraclast = 0.;
} else if (nsamples <= x2) {
ilast = nsamples - 1;
fraclast = 0.;
} else {
ilast = (size_t)floor(x2);
fraclast = ilast + 1 - x2;
}
// start border condition
if (x1 < ifirst && !zeroOutside) {
for (size_t j = 0; j < depth; ++j) {
v[j] += l[ifirst * stride + j] * (float)(ifirst-x1);
}
}
// pre-subtract partial first pixel
if (fracfirst > 0.) {
for (size_t j = 0; j < depth; ++j) {
v[j] -= l[ifirst * stride + j] * (float)fracfirst;
}
}
// sum all covered pixels
for (size_t i = ifirst; i <= ilast; ++i) {
for (size_t j = 0; j < depth; ++j) {
v[j] += l[i * stride + j];
}
}
// subtract partial last pixel
if (fraclast > 0.) {
for (size_t j = 0; j < depth; ++j) {
v[j] -= l[ilast * stride + j] * (float)fraclast;
}
}
// end border condition
if (x2 > nsamples && !zeroOutside) {
for (size_t j = 0; j < depth; ++j) {
v[j] += l[ilast * stride + j] * (float)(x2 - nsamples);
}
}
}
/// @brief Compute the mean of the signal contained in l, seen as piecewise constant, in the rectangular area delimited by x1, x2, y1, y2.
/// x = 0 corresponds to the left of the first pixel, x = 1 corresponds to the right of the first pixel / left of the second pixel
///
template <class PIX>
void
ofxsFilterIntegrate2d(const PIX* a, // pointer to data start
const size_t awidth, // width of the array
const size_t aheight, // height of the array
const size_t axstride, // increment from one data point to the next (must be >= depth)
const size_t aystride, // increment from one data line to the next (usually awidth * axstride)
const size_t depth, // dimension of each sample, also the dimension of result vector v
const OfxRectD& area,
const bool zeroOutside, // if true, outside of the data is zero. If false, use Neumann boundary conditions (outside is the closest data point)
float *p, // temporary storage of size depth
float *v) // vector of dimension depth containing the result
{
double x1 = area.x1;
double y1 = area.y1;
double x2 = area.x2;
double y2 = area.y2;
assert(y2 >= y1);
size_t ifirst, ilast; // index of the first/last line
double fracfirst, fraclast; // fraction to remove from the first/last line
if (y1 < 0.) {
ifirst = 0;
fracfirst = 0.;
} else if (aheight <= y1) {
ifirst = aheight - 1;
fracfirst = 0.;
} else {
ifirst = (size_t)floor(y1);
fracfirst = y1 - ifirst;
}
if (y2 < 0.) {
ilast = 0;
fraclast = 0.;
} else if (aheight <= y2) {
ilast = aheight - 1;
fraclast = 0.;
} else {
ilast = (size_t)floor(y2);
fraclast = ilast + 1 - y2;
}
// compute result for first line
for (size_t j = 0; j < depth; ++j) {
p[j] = 0.;
}
ofxsFilterIntegrate1d(&a[ifirst * aystride], awidth, axstride, depth, x1, x2, zeroOutside, p);
// start border condition
if (y1 < ifirst && !zeroOutside) {
for (size_t j = 0; j < depth; ++j) {
v[j] += p[j] * (float)(ifirst - y1);
}
}
// subtract partial first line
if (fracfirst > 0.) {
for (size_t j = 0; j < depth; ++j) {
v[j] -= p[j] * (float)fracfirst;
}
}
// sum all covered lines
// first line
for (size_t j = 0; j < depth; ++j) {
v[j] += p[j];
}
// all lines except first and last
for (size_t i = ifirst + 1; i < ilast; ++i) {
// (results accumulates in v)
ofxsFilterIntegrate1d(&a[i * aystride], awidth, axstride, depth, x1, x2, zeroOutside, v);
}
// last line
if (ilast > ifirst) { // (if equal, the result is already in p)
for (size_t j = 0; j < depth; ++j) {
p[j] = 0.;
}
ofxsFilterIntegrate1d(&a[ilast * aystride], awidth, axstride, depth, x1, x2, zeroOutside, p);
for (size_t j = 0; j < depth; ++j) {
v[j] += p[j];
}
}
// subtract partial last pixel
if (fraclast > 0.) {
for (size_t j = 0; j < depth; ++j) {
v[j] -= p[j] * (float)fraclast;
}
}
// end border condition
if (y2 > aheight && !zeroOutside) {
for (size_t j = 0; j < depth; ++j) {
v[j] += p[j] * (float)(y2 - aheight);
}
}
}
/// @brief resize the area from image a indicated by from and put it in image b at to.
/// If @param from is partially outside of a, pixels are considered to be black and transparent if zeroOutside is true,
/// else they take the value of the closest pixel in a.
/// The @param to may be partially outside of b.
template <class PIX>
void
ofxsFilterResize2d(const PIX* a, // pointer to data start
const size_t awidth, // number of samples in the line
const size_t aheight, // number of samples in the line
const size_t axstride, // increment from one data point to the next (must be >= depth)
const size_t aystride, // increment from one data line to the next (usually awidth * axstride)
const size_t depth, // dimension of each sample, also the dimension of result vector v
const OfxRectD& from,
const bool zeroOutside, // if true, outside of the data is zero (Dirichlet boundary conditions). If false, outside is the closest data point (Neumann boundary conditions).
float* b, // pointer to output start
const size_t bwidth, // number of samples in the line
const size_t bheight, // number of samples in the line
const size_t bxstride, // inscrement from one data point to the next (must be >= depth)
const size_t bystride,
const OfxRectI& to)
{
assert(awidth > 0 && aheight > 0 && axstride > 0 && aystride > 0 && depth > 0);
assert(bwidth > 0 && bheight > 0 && bxstride > 0 && bystride > 0);
double x1 = from.x1;
double y1 = from.y1;
double x2 = from.x2;
double y2 = from.y2;
assert(x2 >= x1);
assert(y2 >= y1);
int ox1 = to.x1;
int oy1 = to.y1;
int ox2 = to.x2;
int oy2 = to.y2;
assert(ox2 > ox1);
assert(oy2 > oy1);
// pixel factor
double vwidth = (x2 - x1) / (ox2 - ox1);
double vheight = (y2 - y1) / (oy2 - oy1);
// adjust output to valid areas of b
if (ox1 < 0) {
x1 -= vwidth * ox1;
ox1 = 0;
}
if (ox2 > (int)bwidth) {
x2 -= vwidth * ((int)bwidth - ox2);
ox2 = (int)bwidth;
}
assert(x2 >= x1);
assert(ox2 >= ox1);
if (ox2 <= ox1) {
// nothing to draw
return;
}
if (oy1 < 0) {
y1 -= vheight * oy1;
oy1 = 0;
}
if (oy2 > (int)bheight) {
y2 -= vheight * ((int)bheight - oy2);
oy2 = (int)bheight;
}
assert(y2 >= y1);
assert(oy2 >= oy1);
if (oy2 <= oy1) {
// nothing to draw
return;
}
float *p = new float[depth];
// #pragma parallel for
for (int j = oy1; j < oy2; ++j) {
OfxRectD area;
area.y1 = y1 + (j - oy1) * vheight;
area.y2 = area.y1 + vheight;
for (int i = ox1; i < ox2; ++i) {
area.x1 = x1 + (i - ox1) * vwidth;
area.x2 = area.x1 + vwidth;
// compute one pixel of the resized image
float *v = &b[j * bystride + i * bxstride];
// zero the result, since integrate_2d accumulates
for (size_t k = 0; k < depth; ++k) {
v[k] = 0.;
}
ofxsFilterIntegrate2d(a, awidth, aheight, axstride, aystride, depth,
area,
zeroOutside,
p,
v);
// normalize by the surface of the pixel
for (size_t k = 0; k < depth; ++k) {
v[k] /= vwidth * vheight;
}
}
}
delete [] p;
}
/////////////////////////////////////////////////
// BOX FILTER END
/////////////////////////////////////////////////
#define OFXS_APPLY4(f, j) double I ## j = f(Ip ## j, Ic ## j, In ## j, Ia ## j, dx, clamp)
#define OFXS_CUBIC2D(f) \
inline \
double \
f ## 2D (double Ipp, double Icp, double Inp, double Iap, \
double Ipc, double Icc, double Inc, double Iac, \
double Ipn, double Icn, double Inn, double Ian, \
double Ipa, double Ica, double Ina, double Iaa, \
double dx, double dy, bool clamp) \
{ \
OFXS_APPLY4(f, p); OFXS_APPLY4(f, c); OFXS_APPLY4(f, n); OFXS_APPLY4(f, a); \
return f(Ip, Ic, In, Ia, dy, clamp); \
}
OFXS_CUBIC2D(ofxsFilterKeys);
OFXS_CUBIC2D(ofxsFilterSimon);
OFXS_CUBIC2D(ofxsFilterRifman);
OFXS_CUBIC2D(ofxsFilterMitchell);
OFXS_CUBIC2D(ofxsFilterParzen);
OFXS_CUBIC2D(ofxsFilterNotch);
#undef OFXS_CUBIC2D
#undef OFXS_APPLY4
template <class PIX>
PIX
ofxsGetPixComp(const PIX* p,
int c)
{
return p ? p[c] : PIX();
}
// Macros used in ofxsFilterInterpolate2D
#define OFXS_CLAMPXY(m) \
m ## x = (std::max)( srcImg->getBounds().x1, (std::min)(m ## x, srcImg->getBounds().x2 - 1) ); \
m ## y = (std::max)( srcImg->getBounds().y1, (std::min)(m ## y, srcImg->getBounds().y2 - 1) )
#define OFXS_GETPIX(i, j) PIX * P ## i ## j = (PIX *)srcImg->getPixelAddress(i ## x, j ## y)
#define OFXS_GETI(i, j) const double I ## i ## j = ofxsGetPixComp(P ## i ## j, c)
#define OFXS_GETPIX4(i) OFXS_GETPIX(i, p); OFXS_GETPIX(i, c); OFXS_GETPIX(i, n); OFXS_GETPIX(i, a);
#define OFXS_GETI4(i) OFXS_GETI(i, p); OFXS_GETI(i, c); OFXS_GETI(i, n); OFXS_GETI(i, a);
#define OFXS_I44 Ipp, Icp, Inp, Iap, \
Ipc, Icc, Inc, Iac, \
Ipn, Icn, Inn, Ian, \
Ipa, Ica, Ina, Iaa
// note that the center of pixel (0,0) has pixel coordinates (0.5,0.5)
template <class PIX, int nComponents, FilterEnum filter, bool clamp>
bool
ofxsFilterInterpolate2D(double fx,
double fy, //!< coordinates of the pixel to be interpolated in srcImg in pixel coordinates
const OFX::Image *srcImg, //!< image to be transformed
bool blackOutside,
float *tmpPix) //!< destination pixel in float format
{
if (!srcImg) {
for (int c = 0; c < nComponents; ++c) {
tmpPix[c] = 0;
}
return false;
}
bool inside = true; // return true, except if outside and black
// GENERIC TRANSFORM
// from here on, everything is generic, and should be moved to a generic transform class
// Important: (0,0) is the *corner*, not the *center* of the first pixel (see OpenFX specs)
switch (filter) {
case eFilterImpulse:
case eFilterBox: {
///nearest neighboor
// the center of pixel (0,0) has coordinates (0.5,0.5)
int mx = (int)std::floor(fx); // don't add 0.5
int my = (int)std::floor(fy); // don't add 0.5
if (!blackOutside) {
OFXS_CLAMPXY(m);
}
OFXS_GETPIX(m, m);
if (Pmm) {
for (int c = 0; c < nComponents; ++c) {
tmpPix[c] = Pmm[c];
}
} else {
for (int c = 0; c < nComponents; ++c) {
tmpPix[c] = 0;
}
inside = false;
}
break;
}
case eFilterBilinear:
case eFilterCubic: {
// bilinear or cubic
// the center of pixel (0,0) has coordinates (0.5,0.5)
int cx = (int)std::floor(fx - 0.5);
int cy = (int)std::floor(fy - 0.5);
int nx = cx + 1;
int ny = cy + 1;
if (!blackOutside) {
OFXS_CLAMPXY(c);
OFXS_CLAMPXY(n);
}
const double dx = (std::max)( 0., (std::min)(fx - 0.5 - cx, 1.) );
const double dy = (std::max)( 0., (std::min)(fy - 0.5 - cy, 1.) );
OFXS_GETPIX(c, c); OFXS_GETPIX(n, c); OFXS_GETPIX(c, n); OFXS_GETPIX(n, n);
if (Pcc || Pnc || Pcn || Pnn) {
for (int c = 0; c < nComponents; ++c) {
OFXS_GETI(c, c); OFXS_GETI(n, c); OFXS_GETI(c, n); OFXS_GETI(n, n);
if (filter == eFilterBilinear) {
double Ic = ofxsFilterLinear(Icc, Inc, dx);
double In = ofxsFilterLinear(Icn, Inn, dx);
tmpPix[c] = (float)ofxsFilterLinear(Ic, In, dy);
} else if (filter == eFilterCubic) {
double Ic = ofxsFilterCubic(Icc, Inc, dx, clamp);
double In = ofxsFilterCubic(Icn, Inn, dx, clamp);
tmpPix[c] = (float)ofxsFilterCubic(Ic, In, dy, clamp);
} else {
assert(0);
}
}
} else {
for (int c = 0; c < nComponents; ++c) {
tmpPix[c] = 0;
}
inside = false;
}
break;
}
// (B,C) cubic filters
case eFilterKeys:
case eFilterSimon:
case eFilterRifman:
case eFilterMitchell:
case eFilterParzen:
case eFilterNotch: {
// the center of pixel (0,0) has coordinates (0.5,0.5)
int cx = (int)std::floor(fx - 0.5);
int cy = (int)std::floor(fy - 0.5);
int px = cx - 1;
int py = cy - 1;
int nx = cx + 1;
int ny = cy + 1;
int ax = cx + 2;
int ay = cy + 2;
if (!blackOutside) {
OFXS_CLAMPXY(c);
OFXS_CLAMPXY(p);
OFXS_CLAMPXY(n);
OFXS_CLAMPXY(a);
}
const double dx = (std::max)( 0., (std::min)(fx - 0.5 - cx, 1.) );
const double dy = (std::max)( 0., (std::min)(fy - 0.5 - cy, 1.) );
OFXS_GETPIX4(p); OFXS_GETPIX4(c); OFXS_GETPIX4(n); OFXS_GETPIX4(a);
if (Ppp || Pcp || Pnp || Pap || Ppc || Pcc || Pnc || Pac || Ppn || Pcn || Pnn || Pan || Ppa || Pca || Pna || Paa) {
for (int c = 0; c < nComponents; ++c) {
//double Ipp = get(Ppp,c);, etc.
OFXS_GETI4(p); OFXS_GETI4(c); OFXS_GETI4(n); OFXS_GETI4(a);
double I = 0.;
switch (filter) {
case eFilterKeys:
I = ofxsFilterKeys2D(OFXS_I44, dx, dy, clamp);
break;
case eFilterSimon:
I = ofxsFilterSimon2D(OFXS_I44, dx, dy, clamp);
break;
case eFilterRifman:
I = ofxsFilterRifman2D(OFXS_I44, dx, dy, clamp);
break;
case eFilterMitchell:
I = ofxsFilterMitchell2D(OFXS_I44, dx, dy, clamp);
break;
case eFilterParzen:
I = ofxsFilterParzen2D(OFXS_I44, dx, dy, false);
break;
case eFilterNotch:
I = ofxsFilterNotch2D(OFXS_I44, dx, dy, false);
break;
default:
assert(0);
}
tmpPix[c] = (float)I;
}
} else {
for (int c = 0; c < nComponents; ++c) {
tmpPix[c] = 0;
}
inside = false;
}
break;
}
default:
assert(0);
break;
} // switch
return inside;
} // ofxsFilterInterpolate2D
/*
* Interpolation with SuperSampling, to avoid moire artifacts when minimizing.
*
ofxsFilterInterpolate2D() does not take into account scaling or distortion effects.
A consequence is that the transform nodes may produce aliasing artifacts when downscaling by a factor of 2 or more.
There are several solutions to this problem is the case where the same texture has to be mapped *several times*:
* Trilinear mipmapping (as implemented by OpenGL) still produces artifacts when scaling is anisotropic (i.e. the scaling factor is different along two directions)
* [Feline (McCormack, 1999)](http://www.hpl.hp.com/techreports/Compaq-DEC/WRL-99-1.pdf), which is close to what is proposed in [OpenGL's anisotropic texture filter](http://www.opengl.org/registry/specs/EXT/texture_filter_anisotropic.txt) is probably 4-5 times slower than mipmapping, but produces less artifacts
* [EWA (Heckbert 1989)](https://www.cs.cmu.edu/~ph/texfund/texfund.pdf) would give the highest quality, but is probably 20 times slower than mipmapping.
A sample implementation of the three methods is given in [Mesa 3D](http://mesa3d.org/)'s [software rasterizer, src/mesa/swrast/s_texfilter.c](http://cgit.freedesktop.org/mesa/mesa/tree/src/mesa/swrast/s_texfilter.c).
* However*, in our case, the texture usually has to be mapped only once. Thus it is more appropriate to use one of the techniques described in this document: <http://people.cs.clemson.edu/~dhouse/courses/405/notes/antialiasing.pdf>.
# Our implementation:
We chose to use a standard supersampling method without jitter (to avoid randomness in rendered images).
The first implementation was interpolating accross scales between supersampled pixels (see OFX_FILTER_SUPERSAMPLING_TRILINEAR below), but since we noticed that using the highest scale produces less moire, and it even costs a bit less (less tests in the processing).
We also noticed that supersampled pixels don't need to use anything better than bilinear filter. The impulse filter still produces moire, and other filters are overkill or may even produce more moire.
*/
#ifdef DEBUG
#define _DBG_COUNT(x) (x)
#else
#define _DBG_COUNT(x) ( (void)0 )
#endif
// Internal function for supersampling (should never be called by the user)
// note that the center of pixel (0,0) has pixel coordinates (0.5,0.5)
template <class PIX, int nComponents, FilterEnum filter, int subx, int suby>
void
ofxsFilterInterpolate2DSuperInternal(double fx,
double fy, //!< coordinates of the pixel to be interpolated in srcImg in pixel coordinates
double Jxx, //!< derivative of fx over x
double Jxy, //!< derivative of fx over y
double Jyx, //!< derivative of fy over x
double Jyy, //!< derivative of fy over y
double sx, //!< scale over x as a power of 3
double sy, //!< scale over y as a power of 3
int isx, //!< floor(sx)
int isy, //!< floor(sy)
const OFX::Image *srcImg, //!< image to be transformed
bool blackOutside,
float *tmpPix) //!< input: interpolated center filter. output: destination pixel in float format
{
// do supersampling.
// All values are computed using nearest neighbor interpolation, except for the center value
// compute number of samples over each dimension, i.e. pow(nis*,3)
// see http://stackoverflow.com/a/101613/2607517
int nisx;
{
int base = 3;
int exp = isx;
int result = 1;
while (exp) {
if (exp & 1) {
result *= base;
}
exp >>= 1;
base *= base;
}
nisx = result;
}
/// linear version:
//nisx = 1;
//for (int p = 0; p < isx; ++p) {
// nisx *= 3;
//}
int nisy;
{
int base = 3;
int exp = isy;
int result = 1;
while (exp) {
if (exp & 1) {
result *= base;
}
exp >>= 1;
base *= base;
}
nisy = result;
}
/// linear version:
//nisy = 1;
//for (int p = 0; p < isy; ++p) {
// nisy *= 3;
//}
assert( nisx == std::pow( (double)3, (double)isx ) && nisy == std::pow( (double)3, (double)isy ) );
// compute the pixel value at scales (isx,isy) (nsx,isy) (isx,nsy) (nsx,nsy), and interpolate bilinearly using sx,sy
float *pii = tmpPix;
float pni[nComponents];
float pin[nComponents];
float pnn[nComponents];
#ifdef DEBUG
int piicount = 1;
int pnicount = 0;
int pincount = 0;
int pnncount = 0;
#endif
// initialize to value of center pixel
if (subx) {
std::copy(tmpPix, tmpPix + nComponents, pni);
_DBG_COUNT(pnicount = 1);
if (suby) {
std::copy(tmpPix, tmpPix + nComponents, pnn);
_DBG_COUNT(pnncount = 1);
}
}
if (suby) {
std::copy(tmpPix, tmpPix + nComponents, pin);
_DBG_COUNT(pincount = 1);
}
// accumulate
for (int y = -nisy / 2; y <= nisy / 2; ++y) {
for (int x = -nisx / 2; x <= nisx / 2; ++x) {
// subsample position
double sfx = fx + (Jxx * x) / nisx + (Jxy * y) / nisy;
double sfy = fy + (Jyx * x) / nisx + (Jyy * y) / nisy;
if ( (x != 0) || (y != 0) ) { // center pixel was already accumulated
float tmp[nComponents];
ofxsFilterInterpolate2D<PIX, nComponents, filter, false>(sfx, sfy, srcImg, blackOutside, tmp);
for (int c = 0; c < nComponents; ++c) {
pii[c] += tmp[c];
_DBG_COUNT( piicount += (c == 0) );
// other scales
if (subx) {
pni[c] += tmp[c];
_DBG_COUNT( pnicount += (c == 0) );
if (suby) {
pnn[c] += tmp[c];
_DBG_COUNT( pnncount += (c == 0) );
}
}
if (suby) {
pin[c] += tmp[c];
_DBG_COUNT( pincount += (c == 0) );
}
}
}
// subsamples from next scales
for (int j = -suby; j <= suby; ++j) {
for (int i = -subx; i <= subx; ++i) {
if ( (i != 0) || (j != 0) ) { // center subsample was already accumulated
double subfx = sfx + (Jxx * i) / (nisx * 3) + (Jxy * j) / (nisy * 3);
double subfy = sfy + (Jyx * i) / (nisx * 3) + (Jyy * j) / (nisy * 3);
{
float tmp[nComponents];
ofxsFilterInterpolate2D<PIX, nComponents, filter, false>(subfx, subfy, srcImg, blackOutside, tmp);
for (int c = 0; c < nComponents; ++c) {
// other scales
if (subx) {
if (j == 0) {
pni[c] += tmp[c];
_DBG_COUNT( pnicount += (c == 0) );
}
if (suby) {
pnn[c] += tmp[c];
_DBG_COUNT( pnncount += (c == 0) );
}
}
if (suby) {
if (i == 0) {
pin[c] += tmp[c];
_DBG_COUNT( pincount += (c == 0) );
}
}
}
}
}
}