-
Notifications
You must be signed in to change notification settings - Fork 14
/
ofxsRamp.h
352 lines (300 loc) · 11.6 KB
/
ofxsRamp.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
/* -*- 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 generic rectangle interact with 4 corner points + center point and 4 mid-points.
* You can use it to define any rectangle in an image resizable by the user.
*/
#ifndef openfx_supportext_ofxsRamp_h
#define openfx_supportext_ofxsRamp_h
#include <cmath>
#include <ofxsInteract.h>
#include <ofxsImageEffect.h>
#include "ofxsMacros.h"
#include "ofxsOGLTextRenderer.h"
#include "ofxsOGLHiDPI.h"
#define kParamRampPoint0 "rampPoint0"
#define kParamRampPoint0Label "Point 0"
#define kParamRampColor0 "rampColor0"
#define kParamRampColor0Label "Color 0"
#define kParamRampPoint1 "rampPoint1"
#define kParamRampPoint1Label "Point 1"
#define kParamRampColor1 "rampColor1"
#define kParamRampColor1Label "Color 1"
#define kParamRampType "rampType"
#define kParamRampTypeLabel "Ramp Type", "The type of interpolation used to generate the ramp"
#define kParamRampTypeOptionLinear "Linear", "Linear ramp.", "linear"
#define kParamRampTypeOptionPLinear "PLinear", "Perceptually linear ramp in Rec.709.", "plinear"
#define kParamRampTypeOptionEaseIn "Ease-in", "Catmull-Rom spline, smooth start, linear end (a.k.a. smooth0).", "easein"
#define kParamRampTypeOptionEaseOut "Ease-out", "Catmull-Rom spline, linear start, smooth end (a.k.a. smooth1).", "easeout"
#define kParamRampTypeOptionSmooth "Smooth", "Traditional smoothstep ramp.", "smooth"
#define kParamRampTypeOptionNone "None", "No color gradient.", "none"
#define kParamRampInteractOpen "rampInteractOpen"
#define kParamRampInteractOpenLabel "Show Interact", "If checked, the ramp interact is displayed over the image."
#define kParamRampInteractive "rampInteractive"
#define kParamRampInteractiveLabel "Interactive Update", "If checked, update the parameter values during interaction with the image viewer, else update the values when pen is released."
// old names, for the Ramp plugin only
#define kParamRampPoint0Old "point0"
#define kParamRampColor0Old "color0"
#define kParamRampPoint1Old "point1"
#define kParamRampColor1Old "color1"
#define kParamRampTypeOld "type"
#define kParamRampInteractiveOld "interactive"
namespace OFX {
enum RampTypeEnum
{
eRampTypeLinear = 0,
eRampTypePLinear,
eRampTypeEaseIn,
eRampTypeEaseOut,
eRampTypeSmooth,
eRampTypeNone
};
class RampInteractHelper
: private OFX::InteractAbstract
{
enum InteractState
{
eInteractStateIdle = 0,
eInteractStateDraggingPoint0,
eInteractStateDraggingPoint1
};
Double2DParam* _point0;
Double2DParam* _point1;
ChoiceParam* _type;
BooleanParam* _interactOpen;
BooleanParam* _interactive;
BooleanParam* _hiDPI;
OfxPointD _point0DragPos, _point1DragPos;
bool _interactiveDrag;
OfxPointD _lastMousePos;
InteractState _state;
OFX::ImageEffect* _effect;
OFX::Interact* _interact;
Clip *_dstClip;
public:
RampInteractHelper(OFX::ImageEffect* effect,
OFX::Interact* interact,
bool oldParams = false)
: _point0(NULL)
, _point1(NULL)
, _type(NULL)
, _interactOpen(NULL)
, _interactive(NULL)
, _hiDPI(NULL)
, _point0DragPos()
, _point1DragPos()
, _interactiveDrag(false)
, _lastMousePos()
, _state(eInteractStateIdle)
, _effect(effect)
, _interact(interact)
, _dstClip(NULL)
{
if (oldParams) {
_point0 = effect->fetchDouble2DParam(kParamRampPoint0Old);
_point1 = effect->fetchDouble2DParam(kParamRampPoint1Old);
_type = effect->fetchChoiceParam(kParamRampTypeOld);
_interactive = effect->fetchBooleanParam(kParamRampInteractiveOld);
} else {
_point0 = effect->fetchDouble2DParam(kParamRampPoint0);
_point1 = effect->fetchDouble2DParam(kParamRampPoint1);
_type = effect->fetchChoiceParam(kParamRampType);
_interactive = effect->fetchBooleanParam(kParamRampInteractive);
}
if ( effect->paramExists(kParamHiDPI) ) {
_hiDPI = effect->fetchBooleanParam(kParamHiDPI);
assert(_hiDPI);
// TODO: cannot addParamToSlaveTo(_hiDPI);
}
_interactOpen = _effect->fetchBooleanParam(kParamRampInteractOpen);
assert(_point0 && _point1 && _type && _interactOpen && _interactive);
assert(_effect && _interact);
_dstClip = _effect->fetchClip(kOfxImageEffectOutputClipName);
assert(_dstClip);
}
/** @brief virtual destructor */
virtual ~RampInteractHelper()
{
// fetched clips and params are owned and deleted by the ImageEffect and its ParamSet
}
/** @brief the function called to draw in the interact */
virtual bool draw(const DrawArgs &args) OVERRIDE;
/** @brief the function called to handle pen motion in the interact
returns true if the interact trapped the action in some sense. This will block the action being passed to
any other interact that may share the viewer.
*/
virtual bool penMotion(const PenArgs &args) OVERRIDE;
/** @brief the function called to handle pen down events in the interact
returns true if the interact trapped the action in some sense. This will block the action being passed to
any other interact that may share the viewer.
*/
virtual bool penDown(const PenArgs &args) OVERRIDE;
/** @brief the function called to handle pen up events in the interact
returns true if the interact trapped the action in some sense. This will block the action being passed to
any other interact that may share the viewer.
*/
virtual bool penUp(const PenArgs &args) OVERRIDE;
/** @brief the function called to handle key down events in the interact
returns true if the interact trapped the action in some sense. This will block the action being passed to
any other interact that may share the viewer.
*/
virtual bool keyDown(const KeyArgs & /*args*/) OVERRIDE { return false; };
/** @brief the function called to handle key up events in the interact
returns true if the interact trapped the action in some sense. This will block the action being passed to
any other interact that may share the viewer.
*/
virtual bool keyUp(const KeyArgs & /*args*/) OVERRIDE { return false; };
/** @brief the function called to handle key down repeat events in the interact
returns true if the interact trapped the action in some sense. This will block the action being passed to
any other interact that may share the viewer.
*/
virtual bool keyRepeat(const KeyArgs & /*args*/) OVERRIDE { return false; };
/** @brief Called when the interact is given input focus */
virtual void gainFocus(const FocusArgs & /*args*/) OVERRIDE {};
/** @brief Called when the interact is loses input focus */
virtual void loseFocus(const FocusArgs &args) OVERRIDE;
};
typedef OverlayInteractFromHelper<RampInteractHelper> RampInteract;
class RampOverlayDescriptor
: public DefaultEffectOverlayDescriptor<RampOverlayDescriptor, RampInteract>
{
};
class RampInteractHelperOldParams
: public RampInteractHelper
{
public:
RampInteractHelperOldParams(OFX::ImageEffect* effect,
OFX::Interact* interact)
: RampInteractHelper(effect, interact, true) {}
};
typedef OverlayInteractFromHelper<RampInteractHelperOldParams> RampInteractOldParams;
class RampOverlayDescriptorOldParams
: public DefaultEffectOverlayDescriptor<RampOverlayDescriptorOldParams, RampInteractOldParams>
{
};
template<RampTypeEnum type>
double
ofxsRampFunc(double t)
{
if ( (t >= 1.) || (type == eRampTypeNone) ) {
t = 1.;
} else if (t <= 0) {
t = 0.;
} else {
// from http://www.comp-fu.com/2012/01/nukes-smooth-ramp-functions/
// linear
//y = x
// plinear: perceptually linear in rec709
//y = pow(x, 3)
// smooth: traditional smoothstep
//y = x*x*(3 - 2*x)
// smooth0: Catmull-Rom spline, smooth start, linear end
//y = x*x*(2 - x)
// smooth1: Catmull-Rom spline, linear start, smooth end
//y = x*(1 + x*(1 - x))
switch (type) {
case eRampTypeLinear:
break;
case eRampTypePLinear:
// plinear: perceptually linear in rec709
t = t * t * t;
break;
case eRampTypeEaseIn:
//t *= t; // old version, end of curve is too sharp
// smooth0: Catmull-Rom spline, smooth start, linear end
t = t * t * (2 - t);
break;
case eRampTypeEaseOut:
//t = - t * (t - 2); // old version, start of curve is too sharp
// smooth1: Catmull-Rom spline, linear start, smooth end
t = t * ( 1 + t * (1 - t) );
break;
case eRampTypeSmooth:
/*
t *= 2.;
if (t < 1) {
t = t * t / (2.);
} else {
--t;
t = -0.5 * (t * (t - 2) - 1);
}
*/
// smooth: traditional smoothstep
t = t * t * (3 - 2 * t);
break;
case eRampTypeNone:
t = 1.;
break;
default:
break;
}
}
return t;
} // ofxsRampFunc
template<RampTypeEnum type>
double
ofxsRampFunc(const OfxPointD& p0,
double nx,
double ny,
const OfxPointD& p)
{
double t = (p.x - p0.x) * nx + (p.y - p0.y) * ny;
return ofxsRampFunc<type>(t);
}
inline double
ofxsRampFunc(const OfxPointD& p0,
double nx,
double ny,
RampTypeEnum type,
const OfxPointD& p)
{
double t = (p.x - p0.x) * nx + (p.y - p0.y) * ny;
switch (type) {
case eRampTypeLinear:
return ofxsRampFunc<eRampTypeLinear>(t);
break;
case eRampTypePLinear:
return ofxsRampFunc<eRampTypePLinear>(t);
break;
case eRampTypeEaseIn:
return ofxsRampFunc<eRampTypeEaseIn>(t);
break;
case eRampTypeEaseOut:
return ofxsRampFunc<eRampTypeEaseOut>(t);
break;
case eRampTypeSmooth:
return ofxsRampFunc<eRampTypeSmooth>(t);
break;
case eRampTypeNone:
t = 1.;
break;
default:
break;
}
return t;
}
void ofxsRampDescribeParams(OFX::ImageEffectDescriptor &desc,
OFX::PageParamDescriptor *page,
OFX::GroupParamDescriptor *group,
RampTypeEnum defaultType,
bool isOpen,
bool oldParams);
} // namespace OFX
#endif /* defined(openfx_supportext_ofxsRamp_h) */