The Distance Node in 2.49/2.5/2.6 pre-tiles has a different calculation for RGB and...
authorDalai Felinto <dfelinto@gmail.com>
Sun, 19 Aug 2012 03:05:38 +0000 (03:05 +0000)
committerDalai Felinto <dfelinto@gmail.com>
Sun, 19 Aug 2012 03:05:38 +0000 (03:05 +0000)
calculate the distance in 3d between R,G and B, the YCC only takes Cb and Cr
into consideration.

This commit makes COM_DistanceMatteOperation inheritable and expose the calculate distance function
to be re-implemented for the YCC node operation.

Thanks Troy Sobotka for the report over email.
Patch incorporates review suggestions by Jeroen Bakker.

source/blender/compositor/CMakeLists.txt
source/blender/compositor/nodes/COM_DistanceMatteNode.cpp
source/blender/compositor/operations/COM_DistanceRGBMatteOperation.cpp [moved from source/blender/compositor/operations/COM_DistanceMatteOperation.cpp with 73% similarity]
source/blender/compositor/operations/COM_DistanceRGBMatteOperation.h [moved from source/blender/compositor/operations/COM_DistanceMatteOperation.h with 84% similarity]
source/blender/compositor/operations/COM_DistanceYCCMatteOperation.cpp [new file with mode: 0644]
source/blender/compositor/operations/COM_DistanceYCCMatteOperation.h [new file with mode: 0644]

index 65d46bf515ace85b60230557c822b67c441b04ad..c110d4f077e96f1fb168df38bcb291fc1ca417ef 100644 (file)
@@ -473,8 +473,10 @@ set(SRC
        operations/COM_DifferenceMatteOperation.h
        operations/COM_LuminanceMatteOperation.cpp
        operations/COM_LuminanceMatteOperation.h
-       operations/COM_DistanceMatteOperation.cpp
-       operations/COM_DistanceMatteOperation.h
+       operations/COM_DistanceRGBMatteOperation.cpp
+       operations/COM_DistanceRGBMatteOperation.h
+       operations/COM_DistanceYCCMatteOperation.cpp
+       operations/COM_DistanceYCCMatteOperation.h
        operations/COM_ChromaMatteOperation.cpp
        operations/COM_ChromaMatteOperation.h
        operations/COM_ColorMatteOperation.cpp
index d6730ef6a007fed49b9509a73515b7d573346023..87e7b9d078868732a3af7244fcd20c9d79617d7b 100644 (file)
 
 #include "COM_DistanceMatteNode.h"
 #include "BKE_node.h"
-#include "COM_DistanceMatteOperation.h"
+#include "COM_DistanceRGBMatteOperation.h"
+#include "COM_DistanceYCCMatteOperation.h"
 #include "COM_SetAlphaOperation.h"
+#include "COM_ConvertRGBToYCCOperation.h"
 
 DistanceMatteNode::DistanceMatteNode(bNode *editorNode) : Node(editorNode)
 {
@@ -36,12 +38,33 @@ void DistanceMatteNode::convertToOperations(ExecutionSystem *graph, CompositorCo
        OutputSocket *outputSocketImage = this->getOutputSocket(0);
        OutputSocket *outputSocketMatte = this->getOutputSocket(1);
 
-       DistanceMatteOperation *operation = new DistanceMatteOperation();
+       NodeOperation *operation;
        bNode *editorsnode = getbNode();
-       operation->setSettings((NodeChroma *)editorsnode->storage);
+       NodeChroma *storage = (NodeChroma *)editorsnode->storage;
 
-       inputSocketImage->relinkConnections(operation->getInputSocket(0), 0, graph);
-       inputSocketKey->relinkConnections(operation->getInputSocket(1), 1, graph);
+       /* work in RGB color space */
+       if (storage->channel == 1) {
+               operation = new DistanceRGBMatteOperation();
+               ((DistanceRGBMatteOperation *) operation)->setSettings(storage);
+
+               inputSocketImage->relinkConnections(operation->getInputSocket(0), 0, graph);
+               inputSocketKey->relinkConnections(operation->getInputSocket(1), 1, graph);
+       }
+       /* work in YCbCr color space */
+       else {
+               operation = new DistanceYCCMatteOperation();
+               ((DistanceYCCMatteOperation *) operation)->setSettings(storage);
+
+               ConvertRGBToYCCOperation *operationYCCImage = new ConvertRGBToYCCOperation();
+               inputSocketImage->relinkConnections(operationYCCImage->getInputSocket(0), 0, graph);
+               addLink(graph, operationYCCImage->getOutputSocket(), operation->getInputSocket(0));
+               graph->addOperation(operationYCCImage);
+
+               ConvertRGBToYCCOperation *operationYCCMatte = new ConvertRGBToYCCOperation();
+               inputSocketKey->relinkConnections(operationYCCMatte->getInputSocket(0), 1, graph);
+               addLink(graph, operationYCCMatte->getOutputSocket(), operation->getInputSocket(1));
+               graph->addOperation(operationYCCMatte);
+       }
 
        if (outputSocketMatte->isConnected()) {
                outputSocketMatte->relinkConnections(operation->getOutputSocket());
similarity index 73%
rename from source/blender/compositor/operations/COM_DistanceMatteOperation.cpp
rename to source/blender/compositor/operations/COM_DistanceRGBMatteOperation.cpp
index b65b5e0f224d8810c1ec444968dc56ad21f43e81..df3809ba129dcf5b5483bf9284be73375ff95158 100644 (file)
  *             Dalai Felinto
  */
 
-#include "COM_DistanceMatteOperation.h"
+#include "COM_DistanceRGBMatteOperation.h"
 #include "BLI_math.h"
 
-DistanceMatteOperation::DistanceMatteOperation() : NodeOperation()
+DistanceRGBMatteOperation::DistanceRGBMatteOperation() : NodeOperation()
 {
-       addInputSocket(COM_DT_COLOR);
-       addInputSocket(COM_DT_COLOR);
-       addOutputSocket(COM_DT_VALUE);
+       this->addInputSocket(COM_DT_COLOR);
+       this->addInputSocket(COM_DT_COLOR);
+       this->addOutputSocket(COM_DT_VALUE);
 
        this->m_inputImageProgram = NULL;
        this->m_inputKeyProgram = NULL;
 }
 
-void DistanceMatteOperation::initExecution()
+void DistanceRGBMatteOperation::initExecution()
 {
        this->m_inputImageProgram = this->getInputSocketReader(0);
        this->m_inputKeyProgram = this->getInputSocketReader(1);
 }
 
-void DistanceMatteOperation::deinitExecution()
+void DistanceRGBMatteOperation::deinitExecution()
 {
        this->m_inputImageProgram = NULL;
        this->m_inputKeyProgram = NULL;
 }
 
-void DistanceMatteOperation::executePixel(float output[4], float x, float y, PixelSampler sampler)
+float DistanceRGBMatteOperation::calculateDistance(float key[4], float image[4])
+{
+       return sqrt(pow((key[0] - image[0]), 2) +
+                   pow((key[1] - image[1]), 2) +
+                   pow((key[2] - image[2]), 2));
+}
+
+void DistanceRGBMatteOperation::executePixel(float output[4], float x, float y, PixelSampler sampler)
 {
        float inKey[4];
        float inImage[4];
@@ -58,9 +65,7 @@ void DistanceMatteOperation::executePixel(float output[4], float x, float y, Pix
        this->m_inputKeyProgram->read(inKey, x, y, sampler);
        this->m_inputImageProgram->read(inImage, x, y, sampler);
        
-       distance = sqrt(pow((inKey[0] - inImage[0]), 2) +
-                       pow((inKey[1] - inImage[1]), 2) +
-                       pow((inKey[2] - inImage[2]), 2));
+       distance = this->calculateDistance(inKey, inImage);
 
        /* store matte(alpha) value in [0] to go with
         * COM_SetAlphaOperation and the Value output
@@ -87,4 +92,3 @@ void DistanceMatteOperation::executePixel(float output[4], float x, float y, Pix
                output[0] = inImage[3];
        }
 }
-
similarity index 84%
rename from source/blender/compositor/operations/COM_DistanceMatteOperation.h
rename to source/blender/compositor/operations/COM_DistanceRGBMatteOperation.h
index a176e5da888cfb23e49e94bd20e9ebeaf80fa4d1..5a34135b1a4a639dcf17063792b364d602954e6a 100644 (file)
@@ -19,8 +19,8 @@
  *             Dalai Felinto
  */
 
-#ifndef _COM_DistanceMatteOperation_h
-#define _COM_DistanceMatteOperation_h
+#ifndef _COM_DistanceRGBMatteOperation_h
+#define _COM_DistanceRGBMatteOperation_h
 #include "COM_MixBaseOperation.h"
 
 
  * this program converts an input color to an output value.
  * it assumes we are in sRGB color space.
  */
-class DistanceMatteOperation : public NodeOperation {
-private:
+class DistanceRGBMatteOperation : public NodeOperation {
+protected:
        NodeChroma *m_settings;
        SocketReader *m_inputImageProgram;
        SocketReader *m_inputKeyProgram;
+
+       virtual float calculateDistance(float key[4], float image[4]);
+
 public:
        /**
         * Default constructor
         */
-       DistanceMatteOperation();
+       DistanceRGBMatteOperation();
        
        /**
         * the inner loop of this program
diff --git a/source/blender/compositor/operations/COM_DistanceYCCMatteOperation.cpp b/source/blender/compositor/operations/COM_DistanceYCCMatteOperation.cpp
new file mode 100644 (file)
index 0000000..32ed748
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2011, Blender Foundation.
+ *
+ * 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.
+ *
+ * Contributor:
+ *             Dalai Felinto
+ */
+
+#include "COM_DistanceYCCMatteOperation.h"
+#include "BLI_math.h"
+
+DistanceYCCMatteOperation::DistanceYCCMatteOperation() : DistanceRGBMatteOperation()
+{
+       /* pass */
+}
+
+float DistanceYCCMatteOperation::calculateDistance(float key[4], float image[4])
+{
+       return sqrt(pow((key[1] - image[1]), 2) +
+                   pow((key[2] - image[2]), 2));
+}
+
diff --git a/source/blender/compositor/operations/COM_DistanceYCCMatteOperation.h b/source/blender/compositor/operations/COM_DistanceYCCMatteOperation.h
new file mode 100644 (file)
index 0000000..f4866a3
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2011, Blender Foundation.
+ *
+ * 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.
+ *
+ * Contributor:
+ *             Dalai Felinto
+ */
+
+#ifndef _COM_DistanceYCCMatteOperation_h
+#define _COM_DistanceYCCMatteOperation_h
+#include "COM_MixBaseOperation.h"
+#include "COM_DistanceRGBMatteOperation.h"
+
+
+/**
+ * this program converts an input color to an output value.
+ * it assumes we are in sRGB color space.
+ */
+class DistanceYCCMatteOperation : public DistanceRGBMatteOperation {
+protected:
+       virtual float calculateDistance(float key[4], float image[4]);
+
+public:
+       /**
+        * Default constructor
+        */
+       DistanceYCCMatteOperation();
+
+};
+#endif