openfx-sys 0.1.0

Rust bindings for OpenFX
Documentation
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
/*
Software License :

Copyright (c) 2003, The Open Effects Association Ltd. All rights reserved.

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.
*/

/*
  Ofx Example plugin that shows how an overlay is drawn and interacted with.

  It is meant to illustrate certain features of the API, as opposed to being a perfectly
  crafted piece of image processing software.

  Note, that the default bitdepths and components are specified for the source (RGBA + A, 8 + 16 + 32) and that
  this plugin does absolutely no image processing, it assumes the isIdentity action will be called before a render
  and so do nothing.
 */

#ifdef WIN32
#include <windows.h>
#endif
 
#include <cstring>
#ifdef __APPLE__
#include <OpenGL/gl.h>
#else
#include <GL/gl.h>
#endif
#include <cmath>
#include <stdexcept>
#include <new>
#include "ofxImageEffect.h"
#include "ofxMemory.h"
#include "ofxMultiThread.h"

#include "../include/ofxUtilities.H" // example support utils

#if defined __APPLE__ || defined linux || defined __FreeBSD__
#  define EXPORT __attribute__((visibility("default")))
#elif defined _WIN32
#  define EXPORT OfxExport
#else
#  error Not building on your operating system quite yet
#endif

#define kPointParam "point"

// pointers to various bits of the host
OfxHost               *gHost;
OfxImageEffectSuiteV1 *gEffectHost = 0;
OfxPropertySuiteV1    *gPropHost = 0;
OfxParameterSuiteV1   *gParamHost = 0;
OfxMemorySuiteV1      *gMemoryHost = 0;
OfxMultiThreadSuiteV1 *gThreadHost = 0;
OfxMessageSuiteV1     *gMessageSuite = 0;
OfxInteractSuiteV1    *gInteractHost = 0;


// we are always identity as we are just a hack example plugin
static OfxStatus
isIdentity(OfxImageEffectHandle  /*pluginInstance*/,
	   OfxPropertySetHandle /*inArgs*/,
	   OfxPropertySetHandle outArgs)
{
  // set the property in the out args indicating which is the identity clip
  gPropHost->propSetString(outArgs, kOfxPropName, 0, kOfxImageEffectSimpleSourceClipName);
  return kOfxStatOK;
}

// the process code  that the host sees
static OfxStatus render(OfxImageEffectHandle  /*instance*/,
			OfxPropertySetHandle /*inArgs*/,
			OfxPropertySetHandle /*outArgs*/)
{
  // do nothing as this should never be called as isIdentity should always be trapped
  return kOfxStatOK;
}

////////////////////////////////////////////////////////////////////////////////
// the interaction routines
struct MyInteractData {
  bool selected;
  OfxParamHandle pointParam;

  explicit MyInteractData(OfxParamHandle pParam)
    : selected(false)
    , pointParam(pParam)
  {
  }
};

// get the interact data from an interact instance
static MyInteractData *
getInteractData(OfxInteractHandle interactInstance)
{    
  void *dataV = ofxuGetInteractInstanceData(interactInstance);
  return (MyInteractData *) dataV;
}

// creation of an interact instance
static OfxStatus 
interactDescribe(OfxInteractHandle /*interactDescriptor*/)
{

  // and we are good
  return kOfxStatOK;
}

// creation of an interact instance
static OfxStatus 
interactCreateInstance(OfxImageEffectHandle pluginInstance,
		       OfxInteractHandle interactInstance)
{
  // get the parameter set for this effect
  OfxParamSetHandle paramSet;
  gEffectHost->getParamSet(pluginInstance, &paramSet);
  
  // fetch a handle to the point param from the parameter set
  OfxParamHandle pointParam;
  gParamHost->paramGetHandle(paramSet, kPointParam, &pointParam, 0);

  // make my interact's instance data
  MyInteractData *data = new MyInteractData(pointParam);

  // and set the interact's data pointer
  ofxuSetInteractInstanceData(interactInstance, (void *) data);

  OfxPropertySetHandle interactProps;
  gInteractHost->interactGetPropertySet(interactInstance, &interactProps);

  // slave this interact to the point param so redraws are triggered cleanly
  gPropHost->propSetString(interactProps, kOfxInteractPropSlaveToParam, 0, kPointParam);

  return kOfxStatOK;
}

// destruction of an interact instance
static OfxStatus 
interactDestroyInstance(OfxImageEffectHandle  /*pluginInstance*/,
			OfxInteractHandle interactInstance)
{
  MyInteractData *data = getInteractData(interactInstance);
  delete data;
  return kOfxStatOK;
}

// size of the cross hair in screen pixels
#define kXHairSize 10

// draw an interact instance
static OfxStatus 
interactDraw(OfxImageEffectHandle  pluginInstance,
	     OfxInteractHandle interactInstance,
	     OfxPropertySetHandle drawArgs)
{
  // get my private interact data
  MyInteractData *data = getInteractData(interactInstance);

  // get the size of a pixel in the current projection
  double pixelScale[2];
  ofxuGetInteractPixelScale(drawArgs, pixelScale);

  // get my param's value
  double x, y;
  gParamHost->paramGetValue(data->pointParam, &x, &y);

  // make the xhair a constant size on screen by scaling by the pixel scale
  float dx = kXHairSize * pixelScale[0];
  float dy = kXHairSize * pixelScale[1];

  // if the we have selected the Xhair, draw it highlit
  if(data->selected)
    glColor3f(1, 1, 1);
  else
    glColor3f(1, 0, 0);

  // Draw a cross hair, the current coordinate system aligns with the image plane.
  glPushMatrix();
  
  glTranslated(x, y, 0);

  glBegin(GL_LINES);

  glVertex2f(-dx, 0);
  glVertex2f(dx, 0);

  glVertex2f(0, -dy);
  glVertex2f(0, dy);

  glEnd();
  

  glPopMatrix();

  return kOfxStatOK;
}

// function reacting to pen motion
static OfxStatus
interactPenMotion(OfxImageEffectHandle  pluginInstance,
		  OfxInteractHandle interactInstance,	
		  OfxPropertySetHandle inArgs)
{
  // get my data handle
  MyInteractData *data = getInteractData(interactInstance);

  // Have we grabbed on a pen down already?
  if(data->selected) {
    // get the pen position
    OfxPointD penPos;
    gPropHost->propGetDoubleN(inArgs, kOfxInteractPropPenPosition, 2, &penPos.x);

    // set the value of the 'point' param
    gParamHost->paramSetValue(data->pointParam, penPos.x, penPos.y);
    return kOfxStatOK;
  }
  return kOfxStatReplyDefault;
}

static OfxStatus
interactPenDown(OfxImageEffectHandle  pluginInstance, 
		OfxInteractHandle interactInstance,	
		OfxPropertySetHandle inArgs)
{
  // get my data handle
  MyInteractData *data = getInteractData(interactInstance);

  // get the point param's value
  double x, y;
  gParamHost->paramGetValue(data->pointParam, &x, &y);

  // get the size of a pixel on screen
  double pixelScale[2];
  ofxuGetInteractPixelScale(inArgs, pixelScale);

  // see if the pen is within 5 screen pixels of the point, in which case, select it
  double penPos[2];
  gPropHost->propGetDoubleN(inArgs, kOfxInteractPropPenPosition, 2, penPos);
  if(fabs(x - penPos[0]) < 5 * pixelScale[0] && fabs(y - penPos[1]) < 5 * pixelScale[1]) {
    data->selected = true;   
    return kOfxStatOK;
  }
  return kOfxStatReplyDefault;
}

static OfxStatus
interactPenUp(OfxImageEffectHandle  /*pluginInstance*/,
		OfxInteractHandle interactInstance,
		OfxPropertySetHandle /*inArgs*/)
{
  // get my data handle
  MyInteractData *data = getInteractData(interactInstance);
  
  if(data->selected) {
    // pen's gone up, deselect
    data->selected = false;
    return kOfxStatOK;
  }
  return kOfxStatReplyDefault;
}

////////////////////////////////////////////////////////////////////////////////
// the entry point for the overlay
static OfxStatus
overlayMain(const char *action,  const void *handle, OfxPropertySetHandle inArgs, OfxPropertySetHandle /*outArgs*/)
{
  OfxInteractHandle interact = (OfxInteractHandle ) handle;
  
  OfxPropertySetHandle props;
  gInteractHost->interactGetPropertySet(interact, &props);

  if(strcmp(action, kOfxActionDescribe) == 0) {
    return interactDescribe(interact);
  }
  else {
    // fetch the effect instance from the interact
    OfxImageEffectHandle pluginInstance;
    gPropHost->propGetPointer(props, kOfxPropEffectInstance, 0, (void **) &pluginInstance); 
    
    if(strcmp(action, kOfxActionCreateInstance) == 0) {
      return interactCreateInstance(pluginInstance, interact);
    }
    else if(strcmp(action, kOfxActionDestroyInstance) == 0) {
      return interactDestroyInstance(pluginInstance, interact);
    }
    else if(strcmp(action, kOfxInteractActionDraw) == 0) {
      return interactDraw(pluginInstance, interact, inArgs);
    }
    else if(strcmp(action, kOfxInteractActionPenMotion) == 0) {
      return interactPenMotion(pluginInstance, interact, inArgs);
    }
    else if(strcmp(action, kOfxInteractActionPenDown) == 0) {
      return interactPenDown(pluginInstance, interact, inArgs);
    }
    else if(strcmp(action, kOfxInteractActionPenUp) == 0) {
      return interactPenUp(pluginInstance, interact, inArgs);
    }
    return kOfxStatReplyDefault;
  }
  return kOfxStatReplyDefault;
}

////////////////////////////////////////////////////////////////////////////////
// the plugin's description routine
static OfxStatus
describe(OfxImageEffectHandle effect)
{
  // fetch the host APIs
  OfxStatus stat;
  if((stat = ofxuFetchHostSuites()) != kOfxStatOK)
    return stat;

  // see if the host supports overlays
  int supportsOverlays;
  gPropHost->propGetInt(gHost->host, kOfxImageEffectPropSupportsOverlays, 0, &supportsOverlays);
  if(supportsOverlays == 0)
    return kOfxStatErrMissingHostFeature;

  // get the property handle for the plugin
  OfxPropertySetHandle effectProps;
  gEffectHost->getPropertySet(effect, &effectProps);

  // define the plugin to the host
  gPropHost->propSetString(effectProps, kOfxImageEffectPropSupportedPixelDepths, 0, kOfxBitDepthByte);
  gPropHost->propSetString(effectProps, kOfxImageEffectPropSupportedPixelDepths, 1, kOfxBitDepthShort);
  gPropHost->propSetString(effectProps, kOfxImageEffectPropSupportedPixelDepths, 2, kOfxBitDepthFloat);

  // set the bit depths the plugin can handle
  gPropHost->propSetString(effectProps, kOfxPropLabel, 0, "OFX Overlay Example");
  gPropHost->propSetString(effectProps, kOfxImageEffectPluginPropGrouping, 0, "OFX Example");

  // define the contexts we can be used in
  gPropHost->propSetString(effectProps, kOfxImageEffectPropSupportedContexts, 0, kOfxImageEffectContextFilter);
    
  // set the property that is the overlay's main entry point for the plugin
  gPropHost->propSetPointer(effectProps, kOfxImageEffectPluginPropOverlayInteractV1, 0,  (void *) overlayMain);
  
  return kOfxStatOK;
}

static OfxStatus
describeInContext(OfxImageEffectHandle  effect, OfxPropertySetHandle /*inArgs*/)
{
  // define the single source clip
  OfxPropertySetHandle props;
  gEffectHost->clipDefine(effect, kOfxImageEffectSimpleSourceClipName, &props);
  gPropHost->propSetString(props, kOfxImageEffectPropSupportedComponents, 0, kOfxImageComponentRGBA);
  gPropHost->propSetString(props, kOfxImageEffectPropSupportedComponents, 1, kOfxImageComponentAlpha);

  // define the output clip
  gEffectHost->clipDefine(effect, kOfxImageEffectOutputClipName, &props);
  gPropHost->propSetString(props, kOfxImageEffectPropSupportedComponents, 0, kOfxImageComponentRGBA);
  gPropHost->propSetString(props, kOfxImageEffectPropSupportedComponents, 1, kOfxImageComponentAlpha);
  
  // fetch the parameter set from the effect
  OfxParamSetHandle paramSet;
  gEffectHost->getParamSet(effect, &paramSet);

  // define the 2D point we are going to draw an overlay for
  gParamHost->paramDefine(paramSet, kOfxParamTypeDouble2D, kPointParam, &props);
  gPropHost->propSetString(props, kOfxParamPropDoubleType, 0, kOfxParamDoubleTypeXYAbsolute);
  gPropHost->propSetString(props, kOfxParamPropDefaultCoordinateSystem, 0, kOfxParamCoordinatesNormalised);
  gPropHost->propSetDouble(props, kOfxParamPropDefault, 0, 0.5);
  gPropHost->propSetDouble(props, kOfxParamPropDefault, 1, 0.5);
  gPropHost->propSetString(props, kOfxParamPropHint, 0, "Point attached to overlay crosshair");
  gPropHost->propSetString(props, kOfxParamPropScriptName, 0, "point");
  gPropHost->propSetString(props, kOfxPropLabel, 0, "Point");

  return kOfxStatOK;
}

////////////////////////////////////////////////////////////////////////////////
// The main function
static OfxStatus
pluginMain(const char *action, const void *handle, OfxPropertySetHandle inArgs, OfxPropertySetHandle outArgs)
{
  try {
  // cast to appropriate type
  OfxImageEffectHandle effect = (OfxImageEffectHandle ) handle;

  if(strcmp(action, kOfxActionDescribe) == 0) {
    return describe(effect);
  }
  else if(strcmp(action, kOfxImageEffectActionDescribeInContext) == 0) {
    return describeInContext(effect, inArgs);
  }
  else if(strcmp(action, kOfxImageEffectActionIsIdentity) == 0) {
    return isIdentity(effect, inArgs, outArgs);
  }    
  else if(strcmp(action, kOfxImageEffectActionRender) == 0) {
    return render(effect, inArgs, outArgs);
  }    
  } catch (std::bad_alloc) {
    // catch memory
    //std::cout << "OFX Plugin Memory error." << std::endl;
    return kOfxStatErrMemory;
  } catch ( const std::exception& e ) {
    // standard exceptions
    //std::cout << "OFX Plugin error: " << e.what() << std::endl;
    return kOfxStatErrUnknown;
  } catch (int err) {
    // ho hum, gone wrong somehow
    return err;
  } catch ( ... ) {
    // everything else
    //std::cout << "OFX Plugin error" << std::endl;
    return kOfxStatErrUnknown;
  }
    
  // other actions to take the default value
  return kOfxStatReplyDefault;
}

// function to set the host structure
static void
setHostFunc(OfxHost *hostStruct)
{
  gHost         = hostStruct;
}

////////////////////////////////////////////////////////////////////////////////
// the plugin struct 
static OfxPlugin basicPlugin = 
{       
  kOfxImageEffectPluginApi,
  1,
  "uk.co.thefoundry.BasicOverlayPlugin",
  1,
  0,
  setHostFunc,
  pluginMain
};
   
// the two mandated functions
EXPORT OfxPlugin *
OfxGetPlugin(int nth)
{
  if(nth == 0)
    return &basicPlugin;
  return 0;
}
 
EXPORT int
OfxGetNumberOfPlugins(void)
{       
  return 1;
}