forked from NatronGitHub/openfx-misc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReConverge.cpp
534 lines (443 loc) · 19.5 KB
/
ReConverge.cpp
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
/*
OFX ReConverge plugin.
Shift convergence so that tracked point appears at screen-depth.
The ReConverge node only shifts views horizontally, not vertically.
Copyright (C) 2013 INRIA
Author: Frederic Devernay <[email protected]>
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
Neither the name of the {organization} nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
INRIA
Domaine de Voluceau
Rocquencourt - B.P. 105
78153 Le Chesnay Cedex - France
The skeleton for this source file is from:
OFX Basic Example plugin, a plugin that illustrates the use of the OFX Support library.
Copyright (C) 2004-2005 The Open Effects Association Ltd
Author Bruno Nicoletti [email protected]
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name The Open Effects Association Ltd, nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The Open Effects Association Ltd
1 Wardour St
London W1D 6PA
England
*/
#ifdef DEBUG
#include "ReConverge.h"
#include <cstdio>
#include <cstdlib>
#ifdef _WINDOWS
#include <windows.h>
#endif
#include "ofxsProcessing.H"
#include "ofxsPositionInteract.h"
#include "ofxsMacros.h"
#define kPluginName "ReConvergeOFX"
#define kPluginGrouping "Views/Stereo"
#define kPluginDescription "Shift convergence so that a tracked point appears at screen-depth. " \
"Horizontal disparity may be provided in the red channel of the " \
"disparity input if it has RGBA components, or the Alpha channel " \
"if it only has Alpha. " \
"If no disparity is given, only the offset is taken into account. " \
"The amount of shift in pixels is rounded to the closest integer. " \
"The ReConverge node only shifts views horizontally, not vertically."
#define kPluginIdentifier "net.sf.openfx.reConvergePlugin"
#define kPluginVersionMajor 1 // Incrementing this number means that you have broken backwards compatibility of the plug-in.
#define kPluginVersionMinor 0 // Increment this when you have fixed a bug or made it faster.
#define kSupportsTiles 1
#define kSupportsMultiResolution 1
#define kSupportsRenderScale 1
#define kRenderThreadSafety eRenderFullySafe
#define kParamConvergePoint "convergePoint"
#define kParamConvergePointLabel "Converge Upon"
#define kParamConvergePointHint "Position of the tracked point when the convergence is set"
#define kParamOffset "offset"
#define kParamOffsetLabel "Convergence Offset"
#define kParamOffsetHint "The disparity of the tracked point will be set to this"
#define kParamConvergeMode "convergeMode"
#define kParamConvergeModeLabel "Mode"
#define kParamConvergeModeHint "Select to view to be shifted in order to set convergence"
#define kParamConvergeModeOptionShiftRight "Shift Right"
#define kParamConvergeModeOptionShiftLeft "Shift Left"
#define kParamConvergeModeOptionShiftBoth "Shift Both"
#define kClipDisparity "Disparity"
// Base class for the RGBA and the Alpha processor
// This class performs a translation by an integer number of pixels (x,y)
class TranslateBase : public OFX::ImageProcessor
{
protected:
const OFX::Image *_srcImg;
int _translateX;
int _translateY;
public:
/** @brief no arg ctor */
TranslateBase(OFX::ImageEffect &instance)
: OFX::ImageProcessor(instance)
, _srcImg(0)
, _translateX(0)
, _translateY(0)
{
}
/** @brief set the src image */
void setSrcImg(const OFX::Image *v) {_srcImg = v;}
/** @brief set the translation vector */
void setTranslate(int x, int y) {_translateX = x; _translateY = y;}
};
// template to do the RGBA processing
template <class PIX, int nComponents, int max>
class ImageTranslator : public TranslateBase
{
public:
// ctor
ImageTranslator(OFX::ImageEffect &instance)
: TranslateBase(instance)
{}
private:
// and do some processing
void multiThreadProcessImages(OfxRectI procWindow)
{
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++) {
const PIX *srcPix = (const PIX *) (_srcImg ? _srcImg->getPixelAddress(x, y) : 0);
// do we have a source image to scale up
if (srcPix) {
for (int c = 0; c < nComponents; c++) {
#pragma message ("TODO")
dstPix[c] = max - srcPix[c];
}
}
else {
// no src pixel here, be black and transparent
for (int c = 0; c < nComponents; c++) {
dstPix[c] = 0;
}
}
// increment the dst pixel
dstPix += nComponents;
}
}
}
};
using namespace OFX;
////////////////////////////////////////////////////////////////////////////////
/** @brief The plugin that does our work */
class ReConvergePlugin : public OFX::ImageEffect
{
public:
/** @brief ctor */
ReConvergePlugin(OfxImageEffectHandle handle)
: ImageEffect(handle)
, dstClip_(0)
, srcClip_(0)
{
dstClip_ = fetchClip(kOfxImageEffectOutputClipName);
assert(dstClip_ && (dstClip_->getPixelComponents() == ePixelComponentAlpha || dstClip_->getPixelComponents() == ePixelComponentRGB || dstClip_->getPixelComponents() == ePixelComponentRGBA));
srcClip_ = fetchClip(kOfxImageEffectSimpleSourceClipName);
assert(srcClip_ && (srcClip_->getPixelComponents() == ePixelComponentAlpha || srcClip_->getPixelComponents() == ePixelComponentRGB || srcClip_->getPixelComponents() == ePixelComponentRGBA));
dispClip_ = getContext() == OFX::eContextFilter ? NULL : fetchClip(kClipDisparity);
assert(!dispClip_ || (dispClip_->getPixelComponents() == ePixelComponentAlpha || dispClip_->getPixelComponents() == ePixelComponentRGB || dispClip_->getPixelComponents() == ePixelComponentRGBA));
convergepoint_ = fetchDouble2DParam(kParamConvergePoint);
offset_ = fetchIntParam(kParamOffset);
convergemode_ = fetchChoiceParam(kParamConvergeMode);
assert(convergepoint_ && offset_ && convergepoint_);
}
private:
/* Override the render */
virtual void render(const OFX::RenderArguments &args) OVERRIDE FINAL;
// override the roi call
virtual void getRegionsOfInterest(const OFX::RegionsOfInterestArguments &args, OFX::RegionOfInterestSetter &rois) OVERRIDE FINAL;
/* set up and run a processor */
void setupAndProcess(TranslateBase &, const OFX::RenderArguments &args);
private:
// do not need to delete these, the ImageEffect is managing them for us
OFX::Clip *dstClip_;
OFX::Clip *srcClip_;
OFX::Clip *dispClip_;
OFX::Double2DParam *convergepoint_;
OFX::IntParam *offset_;
OFX::ChoiceParam *convergemode_;
};
////////////////////////////////////////////////////////////////////////////////
/** @brief render for the filter */
////////////////////////////////////////////////////////////////////////////////
// basic plugin render function, just a skelington to instantiate templates from
/* set up and run a processor */
void
ReConvergePlugin::setupAndProcess(TranslateBase &processor, const OFX::RenderArguments &args)
{
// get a dst image
std::auto_ptr<OFX::Image> dst(dstClip_->fetchImage(args.time));
if (!dst.get()) {
OFX::throwSuiteStatusException(kOfxStatFailed);
}
if (dst->getRenderScale().x != args.renderScale.x ||
dst->getRenderScale().y != args.renderScale.y ||
dst->getField() != args.fieldToRender) {
setPersistentMessage(OFX::Message::eMessageError, "", "OFX Host gave image with wrong scale or field properties");
OFX::throwSuiteStatusException(kOfxStatFailed);
}
OFX::BitDepthEnum dstBitDepth = dst->getPixelDepth();
OFX::PixelComponentEnum dstComponents = dst->getPixelComponents();
// fetch main input image
std::auto_ptr<const OFX::Image> src(srcClip_->fetchImage(args.time));
// make sure bit depths are sane
if (src.get()) {
OFX::BitDepthEnum srcBitDepth = src->getPixelDepth();
OFX::PixelComponentEnum srcComponents = src->getPixelComponents();
// see if they have the same depths and bytes and all
if (srcBitDepth != dstBitDepth || srcComponents != dstComponents)
OFX::throwSuiteStatusException(kOfxStatErrImageFormat);
}
int offset = offset_->getValueAtTime(args.time);
int convergemode;
convergemode_->getValueAtTime(args.time, convergemode);
// set the images
processor.setDstImg(dst.get());
processor.setSrcImg(src.get());
// set the render window
processor.setRenderWindow(args.renderWindow);
#pragma message ("TODO")
(void)offset;
// set the parameters
if (getContext() == OFX::eContextGeneral && convergepoint_ && dispClip_) {
// fetch the disparity of the tracked point
}
//
switch (convergemode) {
case 0: // shift left
break;
case 1: // shift right
break;
case 2: // shift both
break;
}
// Call the base class process member, this will call the derived templated process code
processor.process();
}
// override the roi call
void
ReConvergePlugin::getRegionsOfInterest(const OFX::RegionsOfInterestArguments &args, OFX::RegionOfInterestSetter &rois)
{
// set the ROI of the disp clip to the tracked point position (rounded to the nearest pixel)
if (getContext() == OFX::eContextGeneral && convergepoint_ && dispClip_) {
OfxRectD roi;
// since getRegionsOfInterest is not view-specific, return a full horizontal band
roi = srcClip_->getRegionOfDefinition(args.time);
roi.y1 = args.regionOfInterest.y1;
roi.y2 = args.regionOfInterest.y2;
// TODO: we could compute a smaller area, depending on the convergence
rois.setRegionOfInterest(*dispClip_, roi);
}
}
// the overridden render function
void
ReConvergePlugin::render(const OFX::RenderArguments &args)
{
// instantiate the render code based on the pixel depth of the dst clip
OFX::BitDepthEnum dstBitDepth = dstClip_->getPixelDepth();
OFX::PixelComponentEnum dstComponents = dstClip_->getPixelComponents();
// do the rendering
if (dstComponents == OFX::ePixelComponentRGBA) {
switch (dstBitDepth) {
case OFX::eBitDepthUByte : {
ImageTranslator<unsigned char, 4, 255> fred(*this);
setupAndProcess(fred, args);
}
break;
case OFX::eBitDepthUShort : {
ImageTranslator<unsigned short, 4, 65535> fred(*this);
setupAndProcess(fred, args);
}
break;
case OFX::eBitDepthFloat : {
ImageTranslator<float, 4, 1> fred(*this);
setupAndProcess(fred, args);
}
break;
default :
OFX::throwSuiteStatusException(kOfxStatErrUnsupported);
}
} else if (dstComponents == OFX::ePixelComponentRGB) {
switch (dstBitDepth) {
case OFX::eBitDepthUByte : {
ImageTranslator<unsigned char, 3, 255> fred(*this);
setupAndProcess(fred, args);
}
break;
case OFX::eBitDepthUShort : {
ImageTranslator<unsigned short, 3, 65535> fred(*this);
setupAndProcess(fred, args);
}
break;
case OFX::eBitDepthFloat : {
ImageTranslator<float, 3, 1> fred(*this);
setupAndProcess(fred, args);
}
break;
default :
OFX::throwSuiteStatusException(kOfxStatErrUnsupported);
}
} else {
assert(dstComponents == OFX::ePixelComponentAlpha);
switch (dstBitDepth) {
case OFX::eBitDepthUByte : {
ImageTranslator<unsigned char, 1, 255> fred(*this);
setupAndProcess(fred, args);
}
break;
case OFX::eBitDepthUShort : {
ImageTranslator<unsigned short, 1, 65535> fred(*this);
setupAndProcess(fred, args);
}
break;
case OFX::eBitDepthFloat : {
ImageTranslator<float, 1, 1> fred(*this);
setupAndProcess(fred, args);
}
break;
default :
OFX::throwSuiteStatusException(kOfxStatErrUnsupported);
}
}
}
mDeclarePluginFactory(ReConvergePluginFactory, {}, {});
struct ConvergePointParam {
static const char* name() { return kParamConvergePoint; }
};
using namespace OFX;
void ReConvergePluginFactory::describe(OFX::ImageEffectDescriptor &desc)
{
// basic labels
desc.setLabels(kPluginName, kPluginName, kPluginName);
desc.setPluginGrouping(kPluginGrouping);
desc.setPluginDescription(kPluginDescription);
// add the supported contexts
desc.addSupportedContext(eContextFilter); // parameters are offset and convergemode
desc.addSupportedContext(eContextGeneral); // adds second input for disparity (in the red channel), and convergepoint (with interact)
// add supported pixel depths
desc.addSupportedBitDepth(eBitDepthUByte);
desc.addSupportedBitDepth(eBitDepthUShort);
desc.addSupportedBitDepth(eBitDepthFloat);
// set a few flags
desc.setSingleInstance(false);
desc.setHostFrameThreading(false);
desc.setSupportsMultiResolution(kSupportsMultiResolution);
desc.setSupportsTiles(kSupportsTiles);
desc.setTemporalClipAccess(false);
desc.setRenderTwiceAlways(false);
desc.setSupportsMultipleClipPARs(false);
desc.setOverlayInteractDescriptor(new PositionOverlayDescriptor<ConvergePointParam>);
}
void ReConvergePluginFactory::describeInContext(OFX::ImageEffectDescriptor &desc, OFX::ContextEnum context)
{
// Source clip only in the filter context
// create the mandated source clip
ClipDescriptor *srcClip = desc.defineClip(kOfxImageEffectSimpleSourceClipName);
srcClip->addSupportedComponent(ePixelComponentRGB);
srcClip->addSupportedComponent(ePixelComponentRGBA);
srcClip->addSupportedComponent(ePixelComponentAlpha);
srcClip->setTemporalClipAccess(false);
srcClip->setSupportsTiles(kSupportsTiles);
srcClip->setIsMask(false);
if (context == eContextGeneral) {
// Optional disparity clip
ClipDescriptor *dispClip = desc.defineClip(kClipDisparity);
dispClip->addSupportedComponent(ePixelComponentRGB);
dispClip->addSupportedComponent(ePixelComponentRGBA);
dispClip->addSupportedComponent(ePixelComponentAlpha);
dispClip->setTemporalClipAccess(false);
dispClip->setOptional(true);
dispClip->setSupportsTiles(kSupportsTiles);
}
// create the mandated output clip
ClipDescriptor *dstClip = desc.defineClip(kOfxImageEffectOutputClipName);
dstClip->addSupportedComponent(ePixelComponentRGBA);
dstClip->addSupportedComponent(ePixelComponentRGB);
dstClip->addSupportedComponent(ePixelComponentAlpha);
dstClip->setSupportsTiles(kSupportsTiles);
// make some pages and to things in
PageParamDescriptor *page = desc.definePageParam("Controls");
// convergepoint
if (context == eContextGeneral) {
Double2DParamDescriptor *param = desc.defineDouble2DParam(kParamConvergePoint);
param->setLabels(kParamConvergePointLabel, kParamConvergePointLabel, kParamConvergePointLabel);
param->setHint(kParamConvergePointHint);
param->setDoubleType(eDoubleTypeXYAbsolute);
param->setDefaultCoordinateSystem(eCoordinatesNormalised);
param->setDefault(0.5, 0.5);
param->setIncrement(1.);
param->setAnimates(true);
page->addChild(*param);
}
// offset
{
IntParamDescriptor *param = desc.defineIntParam(kParamOffset);
param->setLabels(kParamOffsetLabel, kParamOffsetLabel, kParamOffsetLabel);
param->setHint(kParamOffsetHint);
param->setDefault(0);
param->setRange(-1000, 1000);
param->setDisplayRange(-100, 100);
param->setAnimates(true);
page->addChild(*param);
}
// convergemode
{
ChoiceParamDescriptor *param = desc.defineChoiceParam(kParamConvergeMode);
param->setLabels(kParamConvergeModeLabel, kParamConvergeModeLabel, kParamConvergeModeLabel);
param->setHint(kParamConvergeModeHint);
param->appendOption(kParamConvergeModeOptionShiftRight);
param->appendOption(kParamConvergeModeOptionShiftLeft);
param->appendOption(kParamConvergeModeOptionShiftBoth);
param->setAnimates(true);
page->addChild(*param);
}
}
OFX::ImageEffect* ReConvergePluginFactory::createInstance(OfxImageEffectHandle handle, OFX::ContextEnum /*context*/)
{
return new ReConvergePlugin(handle);
}
void getReConvergePluginID(OFX::PluginFactoryArray &ids)
{
static ReConvergePluginFactory p(kPluginIdentifier, kPluginVersionMajor, kPluginVersionMinor);
ids.push_back(&p);
}
#endif