-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathofxsImageBlenderMasked.h
136 lines (119 loc) · 4.42 KB
/
ofxsImageBlenderMasked.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
/* -*- 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 ***** */
#ifndef openfx_supportext_ofxsImageBlenderMasked_h
#define openfx_supportext_ofxsImageBlenderMasked_h
#include "ofxsProcessing.H"
#include "ofxsMaskMix.h"
#include "ofxsImageBlender.H"
#include "ofxsMacros.h"
namespace OFX {
/** @brief Base class used to blend two images together */
class ImageBlenderMaskedBase
: public ImageBlenderBase
{
protected:
bool _doMasking;
const OFX::Image *_maskImg;
bool _maskInvert;
public:
/** @brief no arg ctor */
ImageBlenderMaskedBase(OFX::ImageEffect &instance)
: ImageBlenderBase(instance)
, _doMasking(false)
, _maskImg(NULL)
, _maskInvert(false)
{
}
void setMaskImg(const OFX::Image *v,
bool maskInvert)
{
_maskImg = v; _maskInvert = maskInvert;
}
void doMasking(bool v)
{
_doMasking = v;
}
};
/** @brief templated class to blend between two images */
template <class PIX, int nComponents, int maxValue, bool masked>
class ImageBlenderMasked
: public ImageBlenderMaskedBase
{
public:
// ctor
ImageBlenderMasked(OFX::ImageEffect &instance)
: ImageBlenderMaskedBase(instance)
{
}
static PIX Lerp(const PIX &v1,
const PIX &v2,
float blend)
{
return PIX( (v2 - v1) * blend + v1 );
}
// and do some processing
void multiThreadProcessImages(const OfxRectI& procWindow, const OfxPointD& rs) OVERRIDE FINAL
{
unused(rs);
float tmpPix[nComponents];
float blend = _blend;
float blendComp = 1.0f - blend;
for (int y = procWindow.y1; y < procWindow.y2; y++) {
if ( _effect.abort() ) {
break;
}
PIX *dstPix = (PIX *) _dstImg->getPixelAddress(procWindow.x1, y);
for (int x = procWindow.x1; x < procWindow.x2; x++) {
PIX *fromPix = (PIX *) (_fromImg ? _fromImg->getPixelAddress(x, y) : 0);
PIX *toPix = (PIX *) (_toImg ? _toImg->getPixelAddress(x, y) : 0);
if ( masked && (fromPix || toPix) ) {
for (int c = 0; c < nComponents; ++c) {
// all images are supposed to be black and transparent outside o
tmpPix[c] = toPix ? (float)toPix[c] : 0.f;
}
ofxsMaskMixPix<PIX, nComponents, maxValue, masked>(tmpPix, x, y, fromPix, _doMasking, _maskImg, blend, _maskInvert, dstPix);
} else if (fromPix && toPix) {
assert(!masked);
for (int c = 0; c < nComponents; ++c) {
dstPix[c] = Lerp(fromPix[c], toPix[c], blend);
}
} else if (fromPix) {
assert(!masked);
for (int c = 0; c < nComponents; ++c) {
dstPix[c] = PIX(fromPix[c] * blendComp);
}
} else if (toPix) {
assert(!masked);
for (int c = 0; c < nComponents; ++c) {
dstPix[c] = PIX(toPix[c] * blend);
}
} else {
// everything is black and transparent
for (int c = 0; c < nComponents; ++c) {
dstPix[c] = PIX(0);
}
}
dstPix += nComponents;
}
}
}
};
};
#endif // ifndef openfx_supportext_ofxsImageBlenderMasked_h