Solved issue with distorted compositor results in some cases

Originally issue was discovered when using stabilization and movie distortion
nodes, but in fact issue was caused by render layer node always doing nearest
interpolation. Now made it so this node will respect sampler passed to it's
executePixel function and do an interpolation.

Added two new functions to do bilinear/bicubic interpolation in float buffer
with variable number of components per element, so it could interpolate 1, 3
and 4 component vectors. This functions currently mostly duplicates the same
functions from imageprocess.c and it should actually be de-duplicated. Think
it's ok to leave a bit of time with such duplication, since functions should
be generalized one more time to support byte buffers, which could backfire on
readability.

Also removed mark as complex from stabilization node, which isn't needed sine
int fact this node is not complex.
This commit is contained in:
Sergey Sharybin 2012-11-10 19:11:25 +00:00
parent 45cd54bcd1
commit f81e30a41f
11 changed files with 372 additions and 50 deletions

@ -58,6 +58,7 @@
#include "BLI_math_rotation.h"
#include "BLI_math_vector.h"
#include "BLI_math_geom.h"
#include "BLI_math_interp.h"
#endif /* __BLI_MATH_H__ */

@ -0,0 +1,35 @@
/*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2012 by Blender Foundation.
* All rights reserved.
*
* The Original Code is: all of this file.
*
* Contributor(s): Sergey Sharybin
*
* ***** END GPL LICENSE BLOCK *****
*
*/
#ifndef BLI_MATH_INTERP
#define BLI_MATH_INTERP
void BLI_bicubic_interpolation(const float *buffer, float *output, int width, int height, int components, float u, float v);
void BLI_bilinear_interpolation(const float *buffer, float *output, int width, int height, int components, float u, float v);
#endif

@ -70,6 +70,7 @@ set(SRC
intern/math_color_inline.c
intern/math_geom.c
intern/math_geom_inline.c
intern/math_interp.c
intern/math_matrix.c
intern/math_rotation.c
intern/math_vector.c

@ -0,0 +1,235 @@
/*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2012 by Blender Foundation.
* All rights reserved.
*
* The Original Code is: all of this file.
*
* Contributor(s): Sergey Sharybin
*
* ***** END GPL LICENSE BLOCK *****
*
*/
#include <math.h>
#include "BLI_math.h"
/**************************************************************************
* INTERPOLATIONS
*
* Reference and docs:
* http://wiki.blender.org/index.php/User:Damiles#Interpolations_Algorithms
***************************************************************************/
/* BICUBIC Interpolation functions
* More info: http://wiki.blender.org/index.php/User:Damiles#Bicubic_pixel_interpolation
* function assumes out to be zero'ed, only does RGBA */
static float P(float k)
{
float p1, p2, p3, p4;
p1 = MAX2(k + 2.0f, 0);
p2 = MAX2(k + 1.0f, 0);
p3 = MAX2(k, 0);
p4 = MAX2(k - 1.0f, 0);
return (float)(1.0f / 6.0f) * (p1 * p1 * p1 - 4.0f * p2 * p2 * p2 + 6.0f * p3 * p3 * p3 - 4.0f * p4 * p4 * p4);
}
#if 0
/* older, slower function, works the same as above */
static float P(float k)
{
return (float)(1.0f / 6.0f) * (pow(MAX2(k + 2.0f, 0), 3.0f) - 4.0f * pow(MAX2(k + 1.0f, 0), 3.0f) + 6.0f * pow(MAX2(k, 0), 3.0f) - 4.0f * pow(MAX2(k - 1.0f, 0), 3.0f));
}
#endif
/* BICUBIC INTERPOLATION */
void BLI_bicubic_interpolation(const float *buffer, float *output, int width, int height, int components, float u, float v)
{
int i, j, n, m, x1, y1;
float a, b, w, wx, wy[4], out[4];
const float *data;
/* sample area entirely outside image? */
if (ceil(u) < 0 || floor(u) > width - 1 || ceil(v) < 0 || floor(v) > height - 1) {
return;
}
i = (int)floor(u);
j = (int)floor(v);
a = u - i;
b = v - j;
zero_v4(out);
/* Optimized and not so easy to read */
/* avoid calling multiple times */
wy[0] = P(b - (-1));
wy[1] = P(b - 0);
wy[2] = P(b - 1);
wy[3] = P(b - 2);
for (n = -1; n <= 2; n++) {
x1 = i + n;
CLAMP(x1, 0, width - 1);
wx = P(n - a);
for (m = -1; m <= 2; m++) {
y1 = j + m;
CLAMP(y1, 0, height - 1);
/* normally we could do this */
/* w = P(n-a) * P(b-m); */
/* except that would call P() 16 times per pixel therefor pow() 64 times, better precalc these */
w = wx * wy[m + 1];
data = buffer + width * y1 * 4 + 4 * x1;
if (components == 1) {
out[0] += data[0] * w;
}
else if (components == 2) {
out[0] += data[0] * w;
out[1] += data[1] * w;
}
else if (components == 3) {
out[0] += data[0] * w;
out[1] += data[1] * w;
out[2] += data[2] * w;
}
else {
out[0] += data[0] * w;
out[1] += data[1] * w;
out[2] += data[2] * w;
out[3] += data[3] * w;
}
}
}
/* Done with optimized part */
#if 0
/* older, slower function, works the same as above */
for (n = -1; n <= 2; n++) {
for (m = -1; m <= 2; m++) {
x1 = i + n;
y1 = j + m;
if (x1 > 0 && x1 < width && y1 > 0 && y1 < height) {
data = in->rect_float + width * y1 * 4 + 4 * x1;
if (components == 1) {
out[0] += data[0] * P(n - a) * P(b - m);
}
else if (components == 2) {
out[0] += data[0] * P(n - a) * P(b - m);
out[1] += data[1] * P(n - a) * P(b - m);
}
else if (components == 3) {
out[0] += data[0] * P(n - a) * P(b - m);
out[1] += data[1] * P(n - a) * P(b - m);
out[2] += data[2] * P(n - a) * P(b - m);
}
else {
out[0] += data[0] * P(n - a) * P(b - m);
out[1] += data[1] * P(n - a) * P(b - m);
out[2] += data[2] * P(n - a) * P(b - m);
out[3] += data[3] * P(n - a) * P(b - m);
}
}
}
}
#endif
if (components == 1) {
output[0] = out[0];
}
else if (components == 2) {
output[0] = out[0];
output[1] = out[1];
}
else if (components == 3) {
output[0] = out[0];
output[1] = out[1];
output[2] = out[2];
}
else {
output[0] = out[0];
output[1] = out[1];
output[2] = out[2];
output[3] = out[3];
}
}
/* BILINEAR INTERPOLATION */
void BLI_bilinear_interpolation(const float *buffer, float *output, int width, int height, int components, float u, float v)
{
const float *row1, *row2, *row3, *row4;
float a, b;
float a_b, ma_b, a_mb, ma_mb;
float empty[4] = {0.0f, 0.0f, 0.0f, 0.0f};
int y1, y2, x1, x2;
/* ImBuf in must have a valid rect or rect_float, assume this is already checked */
x1 = (int)floor(u);
x2 = (int)ceil(u);
y1 = (int)floor(v);
y2 = (int)ceil(v);
/* sample area entirely outside image? */
if (x2 < 0 || x1 > width - 1 || y2 < 0 || y1 > height - 1) {
return;
}
/* sample including outside of edges of image */
if (x1 < 0 || y1 < 0) row1 = empty;
else row1 = buffer + width * y1 * 4 + 4 * x1;
if (x1 < 0 || y2 > height - 1) row2 = empty;
else row2 = buffer + width * y2 * 4 + 4 * x1;
if (x2 > width - 1 || y1 < 0) row3 = empty;
else row3 = buffer + width * y1 * 4 + 4 * x2;
if (x2 > width - 1 || y2 > height - 1) row4 = empty;
else row4 = buffer + width * y2 * 4 + 4 * x2;
a = u - floorf(u);
b = v - floorf(v);
a_b = a * b; ma_b = (1.0f - a) * b; a_mb = a * (1.0f - b); ma_mb = (1.0f - a) * (1.0f - b);
if (components == 1) {
output[0] = ma_mb * row1[0] + a_mb * row3[0] + ma_b * row2[0] + a_b * row4[0];
}
else if (components == 2) {
output[0] = ma_mb * row1[0] + a_mb * row3[0] + ma_b * row2[0] + a_b * row4[0];
output[1] = ma_mb * row1[1] + a_mb * row3[1] + ma_b * row2[1] + a_b * row4[1];
}
else if (components == 3) {
output[0] = ma_mb * row1[0] + a_mb * row3[0] + ma_b * row2[0] + a_b * row4[0];
output[1] = ma_mb * row1[1] + a_mb * row3[1] + ma_b * row2[1] + a_b * row4[1];
output[2] = ma_mb * row1[2] + a_mb * row3[2] + ma_b * row2[2] + a_b * row4[2];
}
else {
output[0] = ma_mb * row1[0] + a_mb * row3[0] + ma_b * row2[0] + a_b * row4[0];
output[1] = ma_mb * row1[1] + a_mb * row3[1] + ma_b * row2[1] + a_b * row4[1];
output[2] = ma_mb * row1[2] + a_mb * row3[2] + ma_b * row2[2] + a_b * row4[2];
output[3] = ma_mb * row1[3] + a_mb * row3[3] + ma_b * row2[3] + a_b * row4[3];
}
}

@ -26,6 +26,7 @@
#include "COM_ExecutionSystem.h"
#include "BKE_node.h"
#include "COM_SetValueOperation.h"
#include "COM_SetSamplerOperation.h"
ScaleNode::ScaleNode(bNode *editorNode) : Node(editorNode)
{
@ -38,7 +39,9 @@ void ScaleNode::convertToOperations(ExecutionSystem *graph, CompositorContext *c
InputSocket *inputXSocket = this->getInputSocket(1);
InputSocket *inputYSocket = this->getInputSocket(2);
OutputSocket *outputSocket = this->getOutputSocket(0);
BaseScaleOperation *scaleoperation;
bNode *bnode = this->getbNode();
switch (bnode->custom1) {
case CMP_SCALE_RELATIVE: {
ScaleOperation *operation = new ScaleOperation();
@ -46,8 +49,8 @@ void ScaleNode::convertToOperations(ExecutionSystem *graph, CompositorContext *c
inputSocket->relinkConnections(operation->getInputSocket(0), 0, graph);
inputXSocket->relinkConnections(operation->getInputSocket(1), 1, graph);
inputYSocket->relinkConnections(operation->getInputSocket(2), 2, graph);
outputSocket->relinkConnections(operation->getOutputSocket(0));
graph->addOperation(operation);
scaleoperation = operation;
}
break;
case CMP_SCALE_SCENEPERCENT: {
@ -57,9 +60,9 @@ void ScaleNode::convertToOperations(ExecutionSystem *graph, CompositorContext *c
inputSocket->relinkConnections(operation->getInputSocket(0), 0, graph);
addLink(graph, scaleFactorOperation->getOutputSocket(), operation->getInputSocket(1));
addLink(graph, scaleFactorOperation->getOutputSocket(), operation->getInputSocket(2));
outputSocket->relinkConnections(operation->getOutputSocket(0));
graph->addOperation(scaleFactorOperation);
graph->addOperation(operation);
scaleoperation = operation;
}
break;
@ -75,9 +78,9 @@ void ScaleNode::convertToOperations(ExecutionSystem *graph, CompositorContext *c
operation->setNewWidth(rd->xsch * rd->size / 100.0f);
operation->setNewHeight(rd->ysch * rd->size / 100.0f);
inputSocket->relinkConnections(operation->getInputSocket(0), 0, graph);
outputSocket->relinkConnections(operation->getOutputSocket(0));
operation->getInputSocket(0)->getConnection()->setIgnoreResizeCheck(true);
graph->addOperation(operation);
scaleoperation = operation;
}
break;
@ -87,9 +90,12 @@ void ScaleNode::convertToOperations(ExecutionSystem *graph, CompositorContext *c
inputSocket->relinkConnections(operation->getInputSocket(0), 0, graph);
inputXSocket->relinkConnections(operation->getInputSocket(1), 1, graph);
inputYSocket->relinkConnections(operation->getInputSocket(2), 2, graph);
outputSocket->relinkConnections(operation->getOutputSocket(0));
graph->addOperation(operation);
scaleoperation = operation;
}
break;
}
outputSocket->relinkConnections(scaleoperation->getOutputSocket(0));
graph->addOperation(scaleoperation);
}

@ -72,6 +72,8 @@ void Stabilize2dNode::convertToOperations(ExecutionSystem *graph, CompositorCont
addLink(graph, scaleAttribute->getOutputSocket(), scaleOperation->getInputSocket(1));
addLink(graph, scaleAttribute->getOutputSocket(), scaleOperation->getInputSocket(2));
scaleOperation->setSampler((PixelSampler)this->getbNode()->custom1);
addLink(graph, scaleOperation->getOutputSocket(), rotateOperation->getInputSocket(0));
addLink(graph, angleAttribute->getOutputSocket(), rotateOperation->getInputSocket(1));
rotateOperation->setDoDegree2RadConversion(false);

@ -49,7 +49,6 @@ MovieDistortionOperation::MovieDistortionOperation(bool distortion) : NodeOperat
this->m_movieClip = NULL;
this->m_cache = NULL;
this->m_distortion = distortion;
setComplex(true);
}
void MovieDistortionOperation::initExecution()

@ -69,6 +69,46 @@ void RenderLayersBaseProg::initExecution()
}
}
void RenderLayersBaseProg::doInterpolation(float output[4], float x, float y, PixelSampler sampler)
{
unsigned int offset;
int ix, iy;
int width = this->getWidth(), height = this->getHeight();
switch (sampler) {
case COM_PS_NEAREST:
ix = x;
iy = y;
offset = (iy * width + ix) * this->m_elementsize;
if (this->m_elementsize == 1)
output[0] = this->m_inputBuffer[offset];
else if (this->m_elementsize == 3)
copy_v3_v3(output, &this->m_inputBuffer[offset]);
else
copy_v4_v4(output, &this->m_inputBuffer[offset]);
break;
case COM_PS_BILINEAR:
BLI_bilinear_interpolation(this->m_inputBuffer, output, width, height, this->m_elementsize, x, y);
break;
case COM_PS_BICUBIC:
BLI_bicubic_interpolation(this->m_inputBuffer, output, width, height, this->m_elementsize, x, y);
break;
}
if (this->m_elementsize == 1) {
output[1] = 0.0f;
output[2] = 0.0f;
output[3] = 0.0f;
}
else if (this->m_elementsize == 3) {
output[3] = 1.0f;
}
}
void RenderLayersBaseProg::executePixel(float output[4], float x, float y, PixelSampler sampler)
{
int ix = x;
@ -78,20 +118,7 @@ void RenderLayersBaseProg::executePixel(float output[4], float x, float y, Pixel
zero_v4(output);
}
else {
unsigned int offset = (iy * this->getWidth() + ix) * this->m_elementsize;
if (this->m_elementsize == 1) {
output[0] = this->m_inputBuffer[offset];
output[1] = 0.0f;
output[2] = 0.0f;
output[3] = 0.0f;
}
else if (this->m_elementsize == 3) {
copy_v3_v3(output, &this->m_inputBuffer[offset]);
output[3] = 1.0f;
}
else {
copy_v4_v4(output, &this->m_inputBuffer[offset]);
}
doInterpolation(output, x, y, sampler);
}
}

@ -80,6 +80,7 @@ protected:
*/
inline float *getInputBuffer() { return this->m_inputBuffer; }
void doInterpolation(float output[4], float x, float y, PixelSampler sampler);
public:
/**
* setter for the scene field. Will be called from

@ -29,7 +29,16 @@
* note: use bilinear because bicubic makes fuzzy even when not scaling at all (1:1)
*/
ScaleOperation::ScaleOperation() : NodeOperation()
BaseScaleOperation::BaseScaleOperation()
{
#ifdef USE_FORCE_BILINEAR
m_sampler = (int) COM_PS_BILINEAR;
#else
m_sampler = -1;
#endif
}
ScaleOperation::ScaleOperation() : BaseScaleOperation()
{
this->addInputSocket(COM_DT_COLOR);
this->addInputSocket(COM_DT_VALUE);
@ -59,22 +68,20 @@ void ScaleOperation::deinitExecution()
void ScaleOperation::executePixel(float output[4], float x, float y, PixelSampler sampler)
{
#ifdef USE_FORCE_BILINEAR
sampler = COM_PS_BILINEAR;
#endif
PixelSampler effective_sampler = getEffectiveSampler(sampler);
float scaleX[4];
float scaleY[4];
this->m_inputXOperation->read(scaleX, x, y, sampler);
this->m_inputYOperation->read(scaleY, x, y, sampler);
this->m_inputXOperation->read(scaleX, x, y, effective_sampler);
this->m_inputYOperation->read(scaleY, x, y, effective_sampler);
const float scx = scaleX[0];
const float scy = scaleY[0];
float nx = this->m_centerX + (x - this->m_centerX) / scx;
float ny = this->m_centerY + (y - this->m_centerY) / scy;
this->m_inputOperation->read(output, nx, ny, sampler);
this->m_inputOperation->read(output, nx, ny, effective_sampler);
}
bool ScaleOperation::determineDependingAreaOfInterest(rcti *input, ReadBufferOperation *readOperation, rcti *output)
@ -94,12 +101,12 @@ bool ScaleOperation::determineDependingAreaOfInterest(rcti *input, ReadBufferOpe
newInput.ymax = this->m_centerY + (input->ymax - this->m_centerY) / scy;
newInput.ymin = this->m_centerY + (input->ymin - this->m_centerY) / scy;
return NodeOperation::determineDependingAreaOfInterest(&newInput, readOperation, output);
return BaseScaleOperation::determineDependingAreaOfInterest(&newInput, readOperation, output);
}
// SCALE ABSOLUTE
ScaleAbsoluteOperation::ScaleAbsoluteOperation() : NodeOperation()
ScaleAbsoluteOperation::ScaleAbsoluteOperation() : BaseScaleOperation()
{
this->addInputSocket(COM_DT_COLOR);
this->addInputSocket(COM_DT_VALUE);
@ -129,15 +136,13 @@ void ScaleAbsoluteOperation::deinitExecution()
void ScaleAbsoluteOperation::executePixel(float output[4], float x, float y, PixelSampler sampler)
{
#ifdef USE_FORCE_BILINEAR
sampler = COM_PS_BILINEAR;
#endif
PixelSampler effective_sampler = getEffectiveSampler(sampler);
float scaleX[4];
float scaleY[4];
this->m_inputXOperation->read(scaleX, x, y, sampler);
this->m_inputYOperation->read(scaleY, x, y, sampler);
this->m_inputXOperation->read(scaleX, x, y, effective_sampler);
this->m_inputYOperation->read(scaleY, x, y, effective_sampler);
const float scx = scaleX[0]; // target absolute scale
const float scy = scaleY[0]; // target absolute scale
@ -151,7 +156,7 @@ void ScaleAbsoluteOperation::executePixel(float output[4], float x, float y, Pix
float nx = this->m_centerX + (x - this->m_centerX) / relativeXScale;
float ny = this->m_centerY + (y - this->m_centerY) / relativeYScale;
this->m_inputOperation->read(output, nx, ny, sampler);
this->m_inputOperation->read(output, nx, ny, effective_sampler);
}
bool ScaleAbsoluteOperation::determineDependingAreaOfInterest(rcti *input, ReadBufferOperation *readOperation, rcti *output)
@ -176,12 +181,12 @@ bool ScaleAbsoluteOperation::determineDependingAreaOfInterest(rcti *input, ReadB
newInput.ymax = this->m_centerY + (input->ymax - this->m_centerY) / relateveYScale;
newInput.ymin = this->m_centerY + (input->ymin - this->m_centerY) / relateveYScale;
return NodeOperation::determineDependingAreaOfInterest(&newInput, readOperation, output);
return BaseScaleOperation::determineDependingAreaOfInterest(&newInput, readOperation, output);
}
// Absolute fixed siez
ScaleFixedSizeOperation::ScaleFixedSizeOperation() : NodeOperation()
ScaleFixedSizeOperation::ScaleFixedSizeOperation() : BaseScaleOperation()
{
this->addInputSocket(COM_DT_COLOR, COM_SC_NO_RESIZE);
this->addOutputSocket(COM_DT_COLOR);
@ -250,17 +255,15 @@ void ScaleFixedSizeOperation::deinitExecution()
void ScaleFixedSizeOperation::executePixel(float output[4], float x, float y, PixelSampler sampler)
{
#ifdef USE_FORCE_BILINEAR
sampler = COM_PS_BILINEAR;
#endif
PixelSampler effective_sampler = getEffectiveSampler(sampler);
if (this->m_is_offset) {
float nx = ((x - this->m_offsetX) * this->m_relX);
float ny = ((y - this->m_offsetY) * this->m_relY);
this->m_inputOperation->read(output, nx, ny, sampler);
this->m_inputOperation->read(output, nx, ny, effective_sampler);
}
else {
this->m_inputOperation->read(output, x * this->m_relX, y * this->m_relY, sampler);
this->m_inputOperation->read(output, x * this->m_relX, y * this->m_relY, effective_sampler);
}
}
@ -273,7 +276,7 @@ bool ScaleFixedSizeOperation::determineDependingAreaOfInterest(rcti *input, Read
newInput.ymax = input->ymax * this->m_relY;
newInput.ymin = input->ymin * this->m_relY;
return NodeOperation::determineDependingAreaOfInterest(&newInput, readOperation, output);
return BaseScaleOperation::determineDependingAreaOfInterest(&newInput, readOperation, output);
}
void ScaleFixedSizeOperation::determineResolution(unsigned int resolution[2], unsigned int preferredResolution[2])
@ -281,7 +284,7 @@ void ScaleFixedSizeOperation::determineResolution(unsigned int resolution[2], un
unsigned int nr[2];
nr[0] = this->m_newWidth;
nr[1] = this->m_newHeight;
NodeOperation::determineResolution(resolution, nr);
BaseScaleOperation::determineResolution(resolution, nr);
resolution[0] = this->m_newWidth;
resolution[1] = this->m_newHeight;
}

@ -25,7 +25,19 @@
#include "COM_NodeOperation.h"
class ScaleOperation : public NodeOperation {
class BaseScaleOperation : public NodeOperation {
public:
void setSampler(PixelSampler sampler) { this->m_sampler = (int) sampler; }
protected:
BaseScaleOperation();
PixelSampler getEffectiveSampler(PixelSampler sampler) { return (m_sampler == -1) ? sampler : (PixelSampler) m_sampler; }
int m_sampler;
};
class ScaleOperation : public BaseScaleOperation {
private:
SocketReader *m_inputOperation;
SocketReader *m_inputXOperation;
@ -41,7 +53,7 @@ public:
void deinitExecution();
};
class ScaleAbsoluteOperation : public NodeOperation {
class ScaleAbsoluteOperation : public BaseScaleOperation {
SocketReader *m_inputOperation;
SocketReader *m_inputXOperation;
SocketReader *m_inputYOperation;
@ -57,7 +69,7 @@ public:
void deinitExecution();
};
class ScaleFixedSizeOperation : public NodeOperation {
class ScaleFixedSizeOperation : public BaseScaleOperation {
SocketReader *m_inputOperation;
int m_newWidth;
int m_newHeight;