-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathofxsMaskMix.h
544 lines (490 loc) · 18 KB
/
ofxsMaskMix.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
/* -*- 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 Masking/Mixing help functions
*/
#ifndef Misc_ofxsMaskMix_h
#define Misc_ofxsMaskMix_h
#include <cfloat> // FLT_EPSILON
#include <ofxsImageEffect.h>
#define kParamPremult "premult"
#define kParamPremultLabel "(Un)premult"
#define kParamPremultHint \
"Divide the image by the alpha channel before processing, and re-multiply it afterwards. " \
"Use if the input images are premultiplied."
#define kParamPremultChannel "premultChannel"
#define kParamPremultChannelLabel "By"
#define kParamPremultChannelHint \
"The channel to use for (un)premult."
#define kParamPremultChannelR "R", "R channel from input", "r"
#define kParamPremultChannelG "G", "G channel from input", "g"
#define kParamPremultChannelB "B", "B channel from input", "b"
#define kParamPremultChannelA "A", "A channel from input", "a"
#define kParamMix "mix"
#define kParamMixLabel "Mix"
#define kParamMixHint "Mix factor between the original and the transformed image."
#define kParamMaskApply "mask"
#define kParamMaskApplyLabel "Mask"
#define kParamMaskApplyHint "When checked, mask is applied."
#define kParamMaskInvert "maskInvert"
#define kParamMaskInvertLabel "Invert Mask"
#define kParamMaskInvertHint "When checked, the effect is fully applied where the mask is 0."
namespace OFX {
inline
void
ofxsPremultDescribeParams(OFX::ImageEffectDescriptor &desc,
OFX::PageParamDescriptor *page)
{
{
OFX::BooleanParamDescriptor* param = desc.defineBooleanParam(kParamPremult);
param->setLabel(kParamPremultLabel);
param->setHint(kParamPremultHint);
#ifdef OFX_EXTENSIONS_NUKE
param->setLayoutHint(eLayoutHintNoNewLine, 1);
#endif
if (page) {
page->addChild(*param);
}
}
{
// not yet implemented, for future use (whenever deep compositing is supported)
OFX::ChoiceParamDescriptor* param = desc.defineChoiceParam(kParamPremultChannel);
param->setLabel(kParamPremultChannelLabel);
param->setHint(kParamPremultChannelHint);
param->appendOption(kParamPremultChannelR);
param->appendOption(kParamPremultChannelG);
param->appendOption(kParamPremultChannelB);
param->appendOption(kParamPremultChannelA);
param->setDefault(3); // alpha
param->setIsSecret(true); // not yet implemented
if (page) {
page->addChild(*param);
}
}
}
inline
bool
ofxsMaskIsAlwaysConnected(OFX::ImageEffectHostDescription *desc)
{
return (desc->hostName.compare(0, 14, "DaVinciResolve") == 0);
}
inline
void
ofxsMaskDescribeParams(OFX::ImageEffectDescriptor &desc,
OFX::PageParamDescriptor *page)
{
// If the host always sees mask clips are connected, this is a problem because
// mask will appear as black and transparent, although it is not connected
if ( ofxsMaskIsAlwaysConnected( OFX::getImageEffectHostDescription() ) ) {
OFX::BooleanParamDescriptor* param = desc.defineBooleanParam(kParamMaskApply);
param->setLabel(kParamMaskApplyLabel);
param->setHint(kParamMaskApplyHint);
if (page) {
page->addChild(*param);
}
}
{
OFX::BooleanParamDescriptor* param = desc.defineBooleanParam(kParamMaskInvert);
param->setLabel(kParamMaskInvertLabel);
param->setHint(kParamMaskInvertHint);
if (page) {
page->addChild(*param);
}
}
}
inline
void
ofxsMixDescribeParams(OFX::ImageEffectDescriptor &desc,
OFX::PageParamDescriptor *page)
{
// GENERIC (MASKED)
//
{
OFX::DoubleParamDescriptor* param = desc.defineDoubleParam(kParamMix);
param->setLabel(kParamMixLabel);
param->setHint(kParamMixHint);
param->setDefault(1.);
param->setIncrement(0.01);
param->setRange(0., 1.);
param->setDisplayRange(0., 1.);
if (page) {
page->addChild(*param);
}
}
}
inline
void
ofxsMaskMixDescribeParams(OFX::ImageEffectDescriptor &desc,
OFX::PageParamDescriptor *page)
{
// GENERIC (MASKED)
//
ofxsMaskDescribeParams(desc, page);
ofxsMixDescribeParams(desc, page);
}
template <class T>
inline
T
ofxsClamp(T v,
int min,
int max)
{
if ( v < T(min) ) {
return T(min);
}
if ( v > T(max) ) {
return T(max);
}
return v;
}
// v is not normalized, it is within [0,maxValue] (but is allowed to be outside of this range)
template <typename PIX, int maxValue>
inline
PIX
ofxsClampIfInt(float v,
int min,
int max)
{
if (maxValue == 1) {
return (PIX)(v);
}
return (PIX)(ofxsClamp(v, min, max) + 0.5);
}
// normalize in [0,1]
template <class PIX, int nComponents, int maxValue>
void
ofxsToRGBA(const PIX *srcPix,
float unpPix[4])
{
if (!srcPix) {
// no src pixel here, be black and transparent
for (int c = 0; c < 4; ++c) {
unpPix[c] = 0.f;
}
return;
}
if (nComponents == 1) {
unpPix[0] = 0.f;
unpPix[1] = 0.f;
unpPix[2] = 0.f;
unpPix[3] = srcPix[0] / (float)maxValue;
return;
}
if (nComponents == 2) {
unpPix[0] = srcPix[0] / (float)maxValue;
unpPix[1] = srcPix[1] / (float)maxValue;
unpPix[2] = 0.f;
unpPix[3] = 1.0f;
return;
}
unpPix[0] = srcPix[0] / (float)maxValue;
unpPix[1] = srcPix[1] / (float)maxValue;
unpPix[2] = srcPix[2] / (float)maxValue;
unpPix[3] = (nComponents == 4) ? (srcPix[3] / (float)maxValue) : 1.0f;
}
// normalize in [0,1] and unpremultiply srcPix
// if premult is false, just normalize
// unpremult by alpha <= 0 gives identity
// premult(unpremult(p)) or premult(unpremult(p)) is thus not identity for alpha <= 0
template <class PIX, int nComponents, int maxValue>
void
ofxsUnPremult(const PIX *srcPix,
float unpPix[4],
bool premult,
int /*premultChannel*/)
{
if (!srcPix) {
// no src pixel here, be black and transparent
for (int c = 0; c < 4; ++c) {
unpPix[c] = 0.f;
}
return;
}
if (nComponents == 1) {
unpPix[0] = 0.f;
unpPix[1] = 0.f;
unpPix[2] = 0.f;
unpPix[3] = srcPix[0] / (float)maxValue;
return;
}
if (nComponents == 2) {
unpPix[0] = srcPix[0] / (float)maxValue;
unpPix[1] = srcPix[1] / (float)maxValue;
unpPix[2] = 0.f;
unpPix[3] = 1.0f;
return;
}
// unpremult by alpha <= 0 gives identity
if ( !premult || (nComponents == 3) || (srcPix[3] <= 0) ) {
unpPix[0] = srcPix[0] / (float)maxValue;
unpPix[1] = srcPix[1] / (float)maxValue;
unpPix[2] = srcPix[2] / (float)maxValue;
unpPix[3] = (nComponents == 4) ? (srcPix[3] / (float)maxValue) : 1.0f;
return;
}
assert(nComponents == 4);
PIX alpha = srcPix[3];
if ( alpha > (PIX)(FLT_EPSILON * maxValue) ) {
unpPix[0] = srcPix[0] / (float)alpha;
unpPix[1] = srcPix[1] / (float)alpha;
unpPix[2] = srcPix[2] / (float)alpha;
} else {
unpPix[0] = srcPix[0] / (float)maxValue;
unpPix[1] = srcPix[1] / (float)maxValue;
unpPix[2] = srcPix[2] / (float)maxValue;
}
unpPix[3] = srcPix[3] / (float)maxValue;
} // ofxsUnPremult
// unpPix is in [0, 1]
// premultiply and denormalize in [0, maxValue]
// if premult is false, just denormalize
// premult by alpha <= 0 gives 0
// premult(unpremult(p)) or premult(unpremult(p)) is thus not identity for alpha <= 0
template <class PIX, int nComponents, int maxValue>
void
ofxsPremult(const float unpPix[4],
float *tmpPix,
bool premult,
int /*premultChannel*/)
{
if (nComponents == 1) {
tmpPix[0] = unpPix[3] * maxValue;
return;
}
if ( !premult ) {
tmpPix[0] = unpPix[0] * maxValue;
if (nComponents >= 2) {
tmpPix[1] = unpPix[1] * maxValue;
}
if (nComponents >= 3) {
tmpPix[2] = unpPix[2] * maxValue;
}
if (nComponents >= 4) {
tmpPix[3] = unpPix[3] * maxValue;
}
return;
}
// premult by alpha <= 0 gives 0
float alpha = (std::max)(0.f, unpPix[3]);
tmpPix[0] = unpPix[0] * alpha * maxValue;
if (nComponents >= 2) {
tmpPix[1] = unpPix[1] * alpha * maxValue;
}
if (nComponents >= 3) {
tmpPix[2] = unpPix[2] * alpha * maxValue;
}
if (nComponents >= 4) {
tmpPix[3] = alpha * maxValue;
}
}
// tmpPix is not normalized, it is within [0,maxValue] (but is allowed to be outside of this range)
template <class PIX, int nComponents, int maxValue>
void
ofxsPix(const float *tmpPix, //!< interpolated pixel
PIX *dstPix) //!< destination pixel
{
// no mask, no mix
for (int c = 0; c < nComponents; ++c) {
dstPix[c] = ofxsClampIfInt<PIX, maxValue>(tmpPix[c], 0, maxValue);
}
} // ofxsMixPix
// unpPix is normalized between [0,1]
template <class PIX, int nComponents, int maxValue>
void
ofxsPremultPix(const float unpPix[4], //!< interpolated unpremultiplied pixel
bool premult,
int premultChannel,
PIX *dstPix) //!< destination pixel
{
float tmpPix[nComponents];
// unpPix is in [0..1]
ofxsPremult<PIX, nComponents, maxValue>(unpPix, tmpPix, premult, premultChannel);
// tmpPix is in [0..maxValue]
ofxsPix<PIX, nComponents, maxValue>(tmpPix, dstPix);
}
// tmpPix is not normalized, it is within [0,maxValue] (but is allowed to be outside of this range)
template <class PIX, int nComponents, int maxValue>
void
ofxsMixPix(const float *tmpPix, //!< interpolated pixel
const PIX *srcPix, //!< the background image (the output is srcImg where maskImg=0, else it is tmpPix)
float mix, //!< mix factor between the output and bkImg
PIX *dstPix) //!< destination pixel
{
if (mix == 1.) {
ofxsPix<PIX, nComponents, maxValue>(tmpPix, dstPix);
} else {
// just mix
float alpha = mix;
if (alpha == 0.) {
if (srcPix) {
for (int c = 0; c < nComponents; ++c) {
dstPix[c] = ofxsClampIfInt<PIX, maxValue>(srcPix[c], 0, maxValue);
}
} else {
for (int c = 0; c < nComponents; ++c) {
dstPix[c] = 0;
}
}
} else if (alpha == 1) {
for (int c = 0; c < nComponents; ++c) {
dstPix[c] = ofxsClampIfInt<PIX, maxValue>(tmpPix[c], 0, maxValue);
}
} else {
if (srcPix) {
for (int c = 0; c < nComponents; ++c) {
float v = tmpPix[c] * alpha + (1.f - alpha) * srcPix[c];
dstPix[c] = ofxsClampIfInt<PIX, maxValue>(v, 0, maxValue);
}
} else {
for (int c = 0; c < nComponents; ++c) {
float v = tmpPix[c] * alpha;
dstPix[c] = ofxsClampIfInt<PIX, maxValue>(v, 0, maxValue);
}
}
}
}
} // ofxsMixPix
// tmpPix is not normalized, it is within [0,maxValue] (but is allowed to be outside of this range)
template <class PIX, int nComponents, int maxValue, bool masked>
void
ofxsMaskMixPix(const float *tmpPix, //!< interpolated pixel
int x, //!< coordinates for the pixel to be computed (PIXEL coordinates)
int y,
const PIX *srcPix, //!< the background image (the output is srcImg where maskImg=0, else it is tmpPix)
bool domask, //!< apply the mask?
const OFX::Image *maskImg, //!< the mask image (ignored if masked=false or domask=false), which must be Alpha
float mix, //!< mix factor between the output and bkImg
bool maskInvert, //<! invert mask behavior
PIX *dstPix) //!< destination pixel
{
// For a multi-planar effect, the mask image may have any components
assert(!domask || !maskImg || maskImg->getPixelComponentCount() > 0);
const PIX *maskPix = NULL;
float maskScale = 1.f;
// are we doing masking
if (!masked) {
ofxsMixPix<PIX, nComponents, maxValue>(tmpPix, srcPix, mix, dstPix);
} else {
if (domask) {
// we do, get the pixel from the mask
maskPix = maskImg ? (const PIX *)maskImg->getPixelAddress(x, y) : 0;
// figure the scale factor from that pixel
if (maskPix == 0) {
maskScale = maskInvert ? 1.f : 0.f;
} else {
maskScale = *maskPix / float(maxValue);
if (maskInvert) {
maskScale = 1.f - maskScale;
}
}
}
float alpha = maskScale * mix;
if (alpha == 0.) {
if (srcPix) {
for (int c = 0; c < nComponents; ++c) {
dstPix[c] = ofxsClampIfInt<PIX, maxValue>(srcPix[c], 0, maxValue);
}
} else {
for (int c = 0; c < nComponents; ++c) {
dstPix[c] = 0;
}
}
} else if (alpha == 1.) {
for (int c = 0; c < nComponents; ++c) {
dstPix[c] = ofxsClampIfInt<PIX, maxValue>(tmpPix[c], 0, maxValue);
}
} else {
if (srcPix) {
for (int c = 0; c < nComponents; ++c) {
float v = tmpPix[c] * alpha + (1.f - alpha) * srcPix[c];
dstPix[c] = ofxsClampIfInt<PIX, maxValue>(v, 0, maxValue);
}
} else {
for (int c = 0; c < nComponents; ++c) {
float v = tmpPix[c] * alpha;
dstPix[c] = ofxsClampIfInt<PIX, maxValue>(v, 0, maxValue);
}
}
}
}
} // ofxsMaskMixPix
// unpPix is normalized between [0,1]
template <class PIX, int nComponents, int maxValue, bool masked>
void
ofxsPremultMaskMixPix(const float unpPix[4], //!< interpolated unpremultiplied pixel
bool premult,
int premultChannel,
int x, //!< coordinates for the pixel to be computed (PIXEL coordinates)
int y,
const PIX *srcPix, //!< the background image (the output is srcImg where maskImg=0, else it is tmpPix)
bool domask, //!< apply the mask?
const OFX::Image *maskImg, //!< the mask image (ignored if masked=false or domask=false)
float mix, //!< mix factor between the output and bkImg
bool maskInvert, //<! invert mask behavior
PIX *dstPix) //!< destination pixel
{
assert(!domask || !maskImg || maskImg->getPixelComponents() == ePixelComponentAlpha);
float tmpPix[nComponents];
// unpPix is in [0..1]
ofxsPremult<PIX, nComponents, maxValue>(unpPix, tmpPix, premult, premultChannel);
// tmpPix is in [0..maxValue]
ofxsMaskMixPix<PIX, nComponents, maxValue, masked>(tmpPix, x, y, srcPix, domask, maskImg, mix, maskInvert, dstPix);
}
// unpPix is normalized between [0,1]
template <class PIX, int nComponents, int maxValue>
void
ofxsPremultMixPix(const float unpPix[4], //!< interpolated unpremultiplied pixel
bool premult,
int premultChannel,
const PIX *srcPix, //!< the background image (the output is srcImg where maskImg=0, else it is tmpPix)
float mix, //!< mix factor between the output and bkImg
PIX *dstPix) //!< destination pixel
{
float tmpPix[nComponents];
// unpPix is in [0..1]
ofxsPremult<PIX, nComponents, maxValue>(unpPix, tmpPix, premult, premultChannel);
// tmpPix is in [0..maxValue]
ofxsMixPix<PIX, nComponents, maxValue>(tmpPix, srcPix, mix, dstPix);
}
// tmpPix is not normalized, it is within [0,maxValue] (but is allowed to be outside of this range)
template <class PIX, int nComponents, int maxValue, bool masked>
void
ofxsMaskMix(const float *tmpPix, //!< interpolated pixel
int x, //!< coordinates for the pixel to be computed (PIXEL coordinates)
int y,
const OFX::Image *srcImg, //!< the background image (the output is srcImg where maskImg=0, else it is tmpPix)
bool domask, //!< apply the mask?
const OFX::Image *maskImg, //!< the mask image (ignored if masked=false or domask=false)
float mix, //!< mix factor between the output and bkImg
bool maskInvert, //<! invert mask behavior
PIX *dstPix) //!< destination pixel
{
assert(!domask || !maskImg || maskImg->getPixelComponents() == ePixelComponentAlpha);
const PIX *srcPix = NULL;
// are we doing masking/mixing? in this case, retrieve srcPix
if (masked && srcImg) {
if ( (domask /*&& maskImg*/) || (mix != 1.) ) {
srcPix = (const PIX *)srcImg->getPixelAddress(x, y);
}
}
return ofxsMaskMixPix<PIX, nComponents, maxValue, masked>(tmpPix, x, y, srcPix, domask, maskImg, mix, maskInvert, dstPix);
}
} // OFX
#endif // ifndef Misc_ofxsMaskMix_h