-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathofxsPixelProcessor.h
463 lines (406 loc) · 14.9 KB
/
ofxsPixelProcessor.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
/* -*- 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_ofxsPixelProcessor_h
#define openfx_supportext_ofxsPixelProcessor_h
/*
* ofxsPixelProcessor: generic multithreaded OFX pixel processor
*/
#include <cassert>
#include <algorithm>
#include "ofxsImageEffect.h"
#include "ofxsMultiThread.h"
#include "ofxsThreadSuite.h"
/** @file This file contains a useful base class that can be used to process images
The code below is not so much a skin on the base OFX classes, but code used in implementing
specific image processing algorithms. As such it does not sit in the support include lib, but in
its own include directory.
*/
namespace OFX {
inline void
getImageData(OFX::Image* img,
void** pixelData,
OfxRectI* bounds,
OFX::PixelComponentEnum* pixelComponents,
OFX::BitDepthEnum* bitDepth,
int* rowBytes)
{
*pixelData = img->getPixelData();
*bounds = img->getBounds();
*pixelComponents = img->getPixelComponents();
*bitDepth = img->getPixelDepth();
*rowBytes = img->getRowBytes();
}
inline void
getImageData(const OFX::Image* img,
const void** pixelData,
OfxRectI* bounds,
OFX::PixelComponentEnum* pixelComponents,
OFX::BitDepthEnum* bitDepth,
int* rowBytes)
{
if (img) {
*pixelData = img->getPixelData();
*bounds = img->getBounds();
*pixelComponents = img->getPixelComponents();
*bitDepth = img->getPixelDepth();
*rowBytes = img->getRowBytes();
} else {
*pixelData = 0;
bounds->x1 = bounds->x2 = bounds->y1 = bounds->y2 = 0;
*pixelComponents = ePixelComponentNone;
*bitDepth = eBitDepthNone;
*rowBytes = 0;
}
}
inline
int
getComponentBytes(OFX::BitDepthEnum bitDepth)
{
// compute bytes per component
switch (bitDepth) {
case OFX::eBitDepthNone:
return 0; break;
case OFX::eBitDepthUByte:
return 1; break;
case OFX::eBitDepthUShort:
return 2; break;
case OFX::eBitDepthHalf:
return 2; break;
case OFX::eBitDepthFloat:
return 4; break;
#ifdef OFX_EXTENSIONS_VEGAS
case OFX::eBitDepthUByteBGRA:
return 1; break;
case OFX::eBitDepthUShortBGRA:
return 2; break;
case OFX::eBitDepthFloatBGRA:
return 4; break;
#endif
case OFX::eBitDepthCustom:
return 0; break;
}
return 0;
}
inline
void*
getPixelAddress(void* pixelData,
const OfxRectI & bounds,
int pixelComponentCount,
OFX::BitDepthEnum bitDepth,
int rowBytes,
int x,
int y)
{
int pixelBytes = pixelComponentCount * getComponentBytes(bitDepth);
// are we in the image bounds
if ( ( x < bounds.x1) || ( x >= bounds.x2) || ( y < bounds.y1) || ( y >= bounds.y2) || ( pixelBytes == 0) ) {
return 0;
}
char *pix = (char *) ( ( (char *) pixelData ) + (size_t)(y - bounds.y1) * rowBytes );
pix += (x - bounds.x1) * pixelBytes;
return (void *) pix;
}
inline
const void*
getPixelAddress(const void* pixelData,
const OfxRectI & bounds,
int pixelComponentCount,
OFX::BitDepthEnum bitDepth,
int rowBytes,
int x,
int y,
bool withinBoundsCheck = true)
{
int pixelBytes = pixelComponentCount * getComponentBytes(bitDepth);
// are we in the image bounds
if ( withinBoundsCheck && (
( x < bounds.x1) || ( x >= bounds.x2) || ( y < bounds.y1) || ( y >= bounds.y2) || ( pixelBytes == 0) ) ) {
return 0;
}
char *pix = (char *) ( ( (char *) pixelData ) + (size_t)(y - bounds.y1) * (size_t)rowBytes );
pix += (x - bounds.x1) * pixelBytes;
return (void *) pix;
}
////////////////////////////////////////////////////////////////////////////////
// base class to process images with
class PixelProcessor
: public OFX::MultiThread::Processor
{
protected:
OFX::ImageEffect &_effect; /**< @brief effect to render with */
//OFX::Image *_dstImg; /**< @brief image to process into */
void* _dstPixelData;
OfxRectI _dstBounds;
OFX::PixelComponentEnum _dstPixelComponents;
int _dstPixelComponentCount;
OFX::BitDepthEnum _dstBitDepth;
int _dstPixelBytes;
int _dstRowBytes;
OfxRectI _renderWindow; /**< @brief render window to use */
OfxPointD _renderScale; /**< @brief render scale to use */
public:
/** @brief ctor */
PixelProcessor(OFX::ImageEffect &effect)
: _effect(effect)
, _dstPixelData(NULL)
, _dstBounds()
, _dstPixelComponents(OFX::ePixelComponentNone)
, _dstPixelComponentCount(0)
, _dstBitDepth(OFX::eBitDepthNone)
, _dstPixelBytes(0)
, _dstRowBytes(0)
{
_renderWindow.x1 = _renderWindow.y1 = _renderWindow.x2 = _renderWindow.y2 = 0;
_renderScale.x = _renderScale.y = 1.;
}
/** @brief set the destination image */
void setDstImg(OFX::Image *v)
{
_dstPixelData = v->getPixelData();
_dstBounds = v->getBounds();
_dstPixelComponents = v->getPixelComponents();
_dstPixelComponentCount = v->getPixelComponentCount();
_dstBitDepth = v->getPixelDepth();
_dstPixelBytes = _dstPixelComponentCount * getComponentBytes(_dstBitDepth);
_dstRowBytes = v->getRowBytes();
}
/** @brief set the destination image */
void setDstImg(void *dstPixelData,
const OfxRectI & dstBounds,
OFX::PixelComponentEnum dstPixelComponents,
int dstPixelComponentCount,
OFX::BitDepthEnum dstPixelDepth,
int dstRowBytes)
{
_dstPixelData = dstPixelData;
_dstBounds = dstBounds;
_dstPixelComponents = dstPixelComponents;
_dstPixelComponentCount = dstPixelComponentCount;
_dstBitDepth = dstPixelDepth;
_dstPixelBytes = _dstPixelComponentCount * getComponentBytes(_dstBitDepth);
_dstRowBytes = dstRowBytes;
}
/** @brief reset the render window */
void setRenderWindow(const OfxRectI& rect, const OfxPointD& rs)
{
_renderWindow = rect;
_renderScale = rs;
}
/** @brief overridden from OFX::MultiThread::Processor. This function is called once on each SMP thread by the base class */
void multiThreadFunction(unsigned int threadId,
unsigned int nThreads)
{
OfxRectI win = _renderWindow;
MultiThread::getThreadRange(threadId, nThreads, _renderWindow.y1, _renderWindow.y2, &win.y1, &win.y2);
if ( (win.y2 - win.y1) > 0 ) {
// and render that thread on each
multiThreadProcessImages(win, _renderScale);
}
}
/** @brief called before any MP is done */
virtual void preProcess(void)
{
}
/** @brief this is called by multiThreadFunction to actually process images, override in derived classes */
virtual void multiThreadProcessImages(const OfxRectI& window, const OfxPointD& rs) = 0;
/** @brief called before any MP is done */
virtual void postProcess(void)
{
}
/** @brief called to process everything */
virtual void process(void)
{
// _dstPixelData may be NULL (e.g. when doing multi-pass, as in FrameBlend)
assert( !_dstPixelData ||
(_dstBounds.x1 <= _renderWindow.x1 && _renderWindow.x2 <= _dstBounds.x2 &&
_dstBounds.y1 <= _renderWindow.y1 && _renderWindow.y2 <= _dstBounds.y2) );
// is it OK ?
if ( ( _dstPixelData && !( ( _dstBounds.x1 <= _renderWindow.x1) && ( _renderWindow.x2 <= _dstBounds.x2) &&
( _dstBounds.y1 <= _renderWindow.y1) && ( _renderWindow.y2 <= _dstBounds.y2) ) ) ||
(_renderWindow.x1 >= _renderWindow.x2) ||
(_renderWindow.y1 >= _renderWindow.y2) ) {
return;
}
// call the pre MP pass
preProcess();
// make sure there are at least 4096 pixels per CPU and at least 1 line par CPU
unsigned int nCPUs = (unsigned int)( (std::min)(_renderWindow.x2 - _renderWindow.x1, 4096) *
(_renderWindow.y2 - _renderWindow.y1) ) / 4096;
// make sure the number of CPUs is valid (and use at least 1 CPU)
nCPUs = (std::max)( 1u, (std::min)( nCPUs, OFX::MultiThread::getNumCPUs() ) );
// call the base multi threading code, should put a pre & post thread calls in too
multiThread(nCPUs);
// call the post MP pass
postProcess();
}
protected:
void* getDstPixelAddress(int x,
int y) const
{
// are we in the image bounds
if ( !_dstPixelData || ( x < _dstBounds.x1) || ( x >= _dstBounds.x2) || ( y < _dstBounds.y1) || ( y >= _dstBounds.y2) || ( _dstPixelBytes == 0) ) {
return 0;
}
char *pix = (char *) ( ( (char *) _dstPixelData ) + (size_t)(y - _dstBounds.y1) * _dstRowBytes );
pix += (x - _dstBounds.x1) * _dstPixelBytes;
return (void *) pix;
}
};
// base class for a processor with a single source image
class PixelProcessorFilterBase
: public OFX::PixelProcessor
{
protected:
const void *_srcPixelData;
OfxRectI _srcBounds;
OFX::PixelComponentEnum _srcPixelComponents;
int _srcPixelComponentCount;
OFX::BitDepthEnum _srcBitDepth;
int _srcPixelBytes;
int _srcRowBytes;
int _srcBoundary; // Boundary conditions 0: Black/Dirichlet 1:Nearest/Neymann 2:Repeat/Periodic
const OFX::Image *_origImg;
const OFX::Image *_maskImg;
bool _premult;
int _premultChannel;
bool _doMasking;
double _mix;
bool _maskInvert;
public:
/** @brief no arg ctor */
PixelProcessorFilterBase(OFX::ImageEffect &instance)
: OFX::PixelProcessor(instance)
, _srcPixelData(NULL)
, _srcBounds()
, _srcPixelComponents(OFX::ePixelComponentNone)
, _srcPixelComponentCount(0)
, _srcBitDepth(OFX::eBitDepthNone)
, _srcPixelBytes(0)
, _srcRowBytes(0)
, _srcBoundary(0)
, _origImg(NULL)
, _maskImg(NULL)
, _premult(false)
, _premultChannel(3)
, _doMasking(false)
, _mix(1.)
, _maskInvert(false)
{
}
/** @brief set the src image */
void setSrcImg(const OFX::Image *v,
int srcBoundary = 0) //!< The border condition type { 0=zero | 1=dirichlet | 2=periodic }.
{
_srcPixelData = v->getPixelData();
_srcBounds = v->getBounds();
_srcPixelComponents = v->getPixelComponents();
_srcPixelComponentCount = v->getPixelComponentCount();
_srcBitDepth = v->getPixelDepth();
_srcPixelBytes = _srcPixelComponentCount * getComponentBytes(_srcBitDepth);
_srcRowBytes = v->getRowBytes();
_srcBoundary = srcBoundary;
}
/** @brief set the src image */
void setSrcImg(const void *srcPixelData,
const OfxRectI & srcBounds,
OFX::PixelComponentEnum srcPixelComponents,
int srcPixelComponentCount,
OFX::BitDepthEnum srcPixelDepth,
int srcRowBytes,
int srcBoundary) //!< The border condition type { 0=zero | 1=dirichlet | 2=periodic }.
{
_srcPixelData = srcPixelData;
_srcBounds = srcBounds;
_srcPixelComponents = srcPixelComponents;
_srcPixelComponentCount = srcPixelComponentCount;
_srcBitDepth = srcPixelDepth;
_srcPixelBytes = _srcPixelComponentCount * getComponentBytes(_srcBitDepth);
_srcRowBytes = srcRowBytes;
_srcBoundary = srcBoundary;
}
void setOrigImg(const OFX::Image *v)
{
_origImg = v;
}
void setMaskImg(const OFX::Image *v,
bool maskInvert)
{
_maskImg = v;
_maskInvert = maskInvert;
}
void doMasking(bool v)
{
_doMasking = v;
}
void setPremultMaskMix(bool premult,
int premultChannel,
double mix)
{
_premult = premult;
_premultChannel = premultChannel;
_mix = mix;
}
protected:
const void* getSrcPixelAddress(int x,
int y) const
{
if ( !_srcPixelData || (_srcPixelBytes == 0) || (_srcBounds.x2 <= _srcBounds.x1) || (_srcBounds.y2 <= _srcBounds.y1) ) {
return 0;
}
// are we in the image bounds
bool outside = (x < _srcBounds.x1) || ( x >= _srcBounds.x2) || ( y < _srcBounds.y1) || ( y >= _srcBounds.y2);
if (outside) {
if (_srcBoundary == 1) {
// Nearest/Neumann
if (x < _srcBounds.x1) {
x = _srcBounds.x1;
} else if (x >= _srcBounds.x2) {
x = _srcBounds.x2 - 1;
}
if (y < _srcBounds.y1) {
y = _srcBounds.y1;
} else if (y >= _srcBounds.y2) {
y = _srcBounds.y2 - 1;
}
} else if (_srcBoundary == 2) {
// Repeat/Periodic
if ( (x < _srcBounds.x1) || (x >= _srcBounds.x2) ) {
x = _srcBounds.x1 + positive_modulo(x - _srcBounds.x1, _srcBounds.x2 - _srcBounds.x1);
}
if ( (y < _srcBounds.y1) || (y >= _srcBounds.y2) ) {
y = _srcBounds.y1 + positive_modulo(y - _srcBounds.y1, _srcBounds.y2 - _srcBounds.y1);
}
} else {
// Black/Dirichlet
return 0;
}
}
char *pix = (char *) ( ( (char *) _srcPixelData ) + (size_t)(y - _srcBounds.y1) * _srcRowBytes );
pix += (x - _srcBounds.x1) * _srcPixelBytes;
return (void *) pix;
}
static int positive_modulo(int i,
int n)
{
return (i % n + n) % n;
}
};
};
#endif // ifndef openfx_supportext_ofxsPixelProcessor_h