Port mask node to new compositor system
authorSergey Sharybin <sergey.vfx@gmail.com>
Thu, 24 May 2012 14:01:00 +0000 (14:01 +0000)
committerSergey Sharybin <sergey.vfx@gmail.com>
Thu, 24 May 2012 14:01:00 +0000 (14:01 +0000)
Works in the same way as double edge node -- not actually multithreaded
but currently it's fast enough to be used in such way. In the future it
might be changed in some way.

Move actual mask rasterization code to BKE so it's resued by old compositor
system and new compositor. Also in the future it might be used to display
mask preview in mask editor.

source/blender/blenkernel/BKE_mask.h
source/blender/blenkernel/intern/mask.c
source/blender/compositor/CMakeLists.txt
source/blender/compositor/intern/COM_Converter.cpp
source/blender/compositor/nodes/COM_MaskNode.cpp [new file with mode: 0644]
source/blender/compositor/nodes/COM_MaskNode.h [new file with mode: 0644]
source/blender/compositor/operations/COM_MaskOperation.cpp [new file with mode: 0644]
source/blender/compositor/operations/COM_MaskOperation.h [new file with mode: 0644]
source/blender/nodes/CMakeLists.txt
source/blender/nodes/composite/nodes/node_composite_mask.c

index e09aa9d8355fa034dfcb80d2a355136a2db31a4a..21be180b05fdd0ac7894e26d8a6a960bad69f585 100644 (file)
@@ -110,6 +110,9 @@ struct MaskObjectShape *BKE_mask_object_shape_varify_frame(struct MaskObject *ma
 void BKE_mask_object_shape_unlink(struct MaskObject *maskobj, struct MaskObjectShape *maskobj_shape);
 void BKE_mask_object_shape_sort(struct MaskObject *maskobj);
 
+/* rasterization */
+void BKE_mask_rasterize(struct Mask *mask, int width, int height, float *buffer);
+
 #define MASKPOINT_ISSEL(p)  ( ((p)->bezt.f1 | (p)->bezt.f2 | (p)->bezt.f2) & SELECT)
 #define MASKPOINT_SEL(p)    { (p)->bezt.f1 |=  SELECT; (p)->bezt.f2 |=  SELECT; (p)->bezt.f3 |=  SELECT; } (void)0
 #define MASKPOINT_DESEL(p)  { (p)->bezt.f1 &= ~SELECT; (p)->bezt.f2 &= ~SELECT; (p)->bezt.f3 &= ~SELECT; } (void)0
index 56bf90499e587b035c848ee6c48d1fc51392a461..ea2acc4dbd342f598a03e000eb95c763ed668657 100644 (file)
@@ -55,6 +55,8 @@
 #include "BKE_movieclip.h"
 #include "BKE_utildefines.h"
 
+#include "raskter.h"
+
 /* mask objects */
 
 MaskObject *BKE_mask_object_new(Mask *mask, const char *name)
@@ -1271,3 +1273,46 @@ void BKE_mask_object_shape_sort(MaskObject *maskobj)
 {
        BLI_sortlist(&maskobj->splines_shapes, mask_object_shape_sort_cb);
 }
+
+/* rasterization */
+void BKE_mask_rasterize(Mask *mask, int width, int height, float *buffer)
+{
+       MaskObject *maskobj;
+
+       for (maskobj = mask->maskobjs.first; maskobj; maskobj = maskobj->next) {
+               MaskSpline *spline;
+
+               for (spline = maskobj->splines.first; spline; spline = spline->next) {
+                       float *diff_points;
+                       int tot_diff_point;
+
+                       diff_points = BKE_mask_spline_differentiate(spline, &tot_diff_point);
+
+                       /* TODO, make this optional! */
+                       if (width != height) {
+                               float *fp;
+                               int i;
+                               float asp;
+
+                               if (width < height) {
+                                       fp = &diff_points[0];
+                                       asp = (float)width / (float)height;
+                               }
+                               else {
+                                       fp = &diff_points[1];
+                                       asp = (float)height / (float)width;
+                               }
+
+                               for (i = 0; i < tot_diff_point; i++, fp += 2) {
+                                       (*fp) = (((*fp) - 0.5f) / asp) + 0.5f;
+                               }
+                       }
+
+                       if (tot_diff_point) {
+                               PLX_raskterize(diff_points, tot_diff_point, buffer, width, height);
+
+                               MEM_freeN(diff_points);
+                       }
+               }
+       }
+}
index 3230c0ec33db920c216323282de6a0a46a88e5fa..5e778d4d03b70e8c5f182adb547984c95d6874c8 100644 (file)
@@ -134,6 +134,8 @@ set(SRC
        nodes/COM_MovieClipNode.h
        nodes/COM_OutputFileNode.cpp
        nodes/COM_OutputFileNode.h
+       nodes/COM_MaskNode.cpp
+       nodes/COM_MaskNode.h
 
 # output nodes
        nodes/COM_CompositorNode.cpp
@@ -603,6 +605,9 @@ operations/COM_ConvertDepthToRadiusOperation.cpp
 
        operations/COM_AntiAliasOperation.cpp
        operations/COM_AntiAliasOperation.h
+
+       operations/COM_MaskOperation.cpp
+       operations/COM_MaskOperation.h
 )
 
 blender_add_lib(bf_compositor "${SRC}" "${INC}" "${INC_SYS}")
index 3cb297801caa65ed4cd8c58724f80c97af2f6b1c..dc6409e7b8657b7f89a118f285a9c4ab0d97b1d9 100644 (file)
 #include "COM_DefocusNode.h"
 #include "COM_DoubleEdgeMaskNode.h"
 #include "COM_CropNode.h"
+#include "COM_MaskNode.h"
 
 Node *Converter::convert(bNode *bNode)
 {
@@ -347,6 +348,9 @@ case CMP_NODE_OUTPUT_FILE:
        case CMP_NODE_CROP:
                node = new CropNode(bNode);
                break;
+       case CMP_NODE_MASK:
+               node = new MaskNode(bNode);
+               break;
        /* not inplemented yet */
        default:
                node = new MuteNode(bNode);
diff --git a/source/blender/compositor/nodes/COM_MaskNode.cpp b/source/blender/compositor/nodes/COM_MaskNode.cpp
new file mode 100644 (file)
index 0000000..991c3f7
--- /dev/null
@@ -0,0 +1,65 @@
+/*
+ * Copyright 2012, 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:
+ *             Jeroen Bakker
+ *             Monique Dewanchand
+ *             Sergey Sharybin
+ */
+
+#include "COM_MaskNode.h"
+#include "COM_ExecutionSystem.h"
+#include "COM_MaskOperation.h"
+
+extern "C" {
+       #include "DNA_mask_types.h"
+}
+
+MaskNode::MaskNode(bNode *editorNode): Node(editorNode)
+{
+}
+
+void MaskNode::convertToOperations(ExecutionSystem *graph, CompositorContext * context)
+{
+       const RenderData *data = &context->getScene()->r;
+
+       InputSocket *inputImage = this->getInputSocket(0);
+       OutputSocket *outputMask = this->getOutputSocket(0);
+
+       bNode *editorNode = this->getbNode();
+       Mask *mask = (Mask *)editorNode->id;
+
+       // always connect the output image
+       MaskOperation *operation = new MaskOperation();
+
+       if (inputImage->isConnected()) {
+               inputImage->relinkConnections(operation->getInputSocket(0), 0, graph);
+       }
+       else {
+               operation->setMaskWidth(data->xsch * data->size / 100.0f);
+               operation->setMaskHeight(data->ysch * data->size / 100.0f);
+       }
+
+       if (outputMask->isConnected()) {
+               outputMask->relinkConnections(operation->getOutputSocket());
+       }
+
+       operation->setMask(mask);
+       operation->setFramenumber(context->getFramenumber());
+
+       graph->addOperation(operation);
+}
diff --git a/source/blender/compositor/nodes/COM_MaskNode.h b/source/blender/compositor/nodes/COM_MaskNode.h
new file mode 100644 (file)
index 0000000..9d2ea18
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2012, 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:
+ *             Jeroen Bakker
+ *             Monique Dewanchand
+ *             Sergey Sharybin
+ */
+
+#include "COM_Node.h"
+#include "DNA_node_types.h"
+
+/**
+  * @brief MaskNode
+  * @ingroup Node
+  */
+class MaskNode : public Node {
+
+
+public:
+       MaskNode(bNode *editorNode);
+       void convertToOperations(ExecutionSystem *graph, CompositorContext *context);
+
+};
diff --git a/source/blender/compositor/operations/COM_MaskOperation.cpp b/source/blender/compositor/operations/COM_MaskOperation.cpp
new file mode 100644 (file)
index 0000000..7c71e88
--- /dev/null
@@ -0,0 +1,110 @@
+/*
+ * Copyright 2012, 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:
+ *             Jeroen Bakker
+ *             Monique Dewanchand
+ *             Sergey Sharybin
+ */
+
+#include "COM_MaskOperation.h"
+
+#include "MEM_guardedalloc.h"
+
+#include "BLI_listbase.h"
+#include "BLI_math.h"
+
+#include "DNA_scene_types.h"
+
+extern "C" {
+       #include "BKE_mask.h"
+}
+
+MaskOperation::MaskOperation(): NodeOperation()
+{
+       this->addInputSocket(COM_DT_COLOR);
+       this->addOutputSocket(COM_DT_COLOR);
+       this->mask = NULL;
+       this->maskWidth = 0;
+       this->maskHeight = 0;
+       this->framenumber = 0;
+       this->rasterizedMask = NULL;
+       setComplex(true);
+}
+
+void MaskOperation::initExecution()
+{
+       initMutex();
+       this->rasterizedMask = NULL;
+}
+
+void MaskOperation::deinitExecution()
+{
+       if (this->rasterizedMask) {
+               MEM_freeN(rasterizedMask);
+               this->rasterizedMask = NULL;
+       }
+}
+
+void *MaskOperation::initializeTileData(rcti *rect, MemoryBuffer **memoryBuffers)
+{
+       if (this->rasterizedMask)
+               return this->rasterizedMask;
+
+       BLI_mutex_lock(getMutex());
+       if (this->rasterizedMask == NULL) {
+               int width = this->getWidth();
+               int height = this->getHeight();
+
+               this->rasterizedMask = (float *)MEM_callocN(sizeof(float) * width * height, "rasterized mask");
+               BKE_mask_rasterize(mask, width, height, this->rasterizedMask);
+       }
+       BLI_mutex_unlock(getMutex());
+
+       return this->rasterizedMask;
+}
+
+void MaskOperation::determineResolution(unsigned int resolution[], unsigned int preferredResolution[])
+{
+       if (maskWidth == 0 || maskHeight == 0) {
+               NodeOperation::determineResolution(resolution, preferredResolution);
+       }
+       else {
+               unsigned int nr[2];
+
+               nr[0] = maskWidth;
+               nr[1] = maskHeight;
+
+               NodeOperation::determineResolution(resolution, nr);
+
+               resolution[0] = maskWidth;
+               resolution[1] = maskHeight;
+       }
+}
+
+void MaskOperation::executePixel(float *color, int x, int y, MemoryBuffer *inputBuffers[], void *data)
+{
+       float *buffer = (float*) data;
+       int index = (y * this->getWidth() + x);
+
+       color[0] = buffer[index];
+       color[1] = buffer[index];
+       color[2] = buffer[index];
+       color[3] = 1.0f;
+}
+
+
diff --git a/source/blender/compositor/operations/COM_MaskOperation.h b/source/blender/compositor/operations/COM_MaskOperation.h
new file mode 100644 (file)
index 0000000..9f2c7f5
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2012, 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:
+ *             Jeroen Bakker
+ *             Monique Dewanchand
+ *             Sergey Sharybin
+ */
+
+
+#ifndef _COM_MaskOperation_h
+#define _COM_MaskOperation_h
+
+#include "COM_NodeOperation.h"
+#include "DNA_scene_types.h"
+#include "DNA_mask_types.h"
+#include "BLI_listbase.h"
+#include "IMB_imbuf_types.h"
+
+/**
+  * Class with implementation of mask rasterization
+  */
+class MaskOperation : public NodeOperation {
+protected:
+       Mask *mask;
+       int maskWidth;
+       int maskHeight;
+       int framenumber;
+       float *rasterizedMask;
+
+       /**
+         * Determine the output resolution. The resolution is retrieved from the Renderer
+         */
+       void determineResolution(unsigned int resolution[], unsigned int preferredResolution[]);
+
+public:
+       MaskOperation();
+
+       void initExecution();
+       void deinitExecution();
+
+       void *initializeTileData(rcti *rect, MemoryBuffer **memoryBuffers);
+
+       void setMask(Mask *mask) {this->mask = mask;}
+       void setMaskWidth(int width) {this->maskWidth = width;}
+       void setMaskHeight(int height) {this->maskHeight = height;}
+       void setFramenumber(int framenumber) {this->framenumber = framenumber;}
+
+       void executePixel(float *color, int x, int y, MemoryBuffer *inputBuffers[], void *data);
+};
+
+#endif
index 358abb08ba4fcc269e5064f6b90968f0601933f4..5e36f90f2175d185ae1b1b7c82f12d81d8981c88 100644 (file)
@@ -35,7 +35,6 @@ set(INC
        ../makesrna
        ../render/extern/include
        ../../../intern/guardedalloc
-       ../../../intern/raskter
        ../compositor
 )
 
index 93d1edd76e8bdea2291243e5a28525b7d16383b5..c90c7918660102d51f15377c89e9466ac99d868d 100644 (file)
@@ -36,8 +36,6 @@
 
 #include "BKE_mask.h"
 
-// XXX: ...
-#include "../../../../intern/raskter/raskter.h"
 #include "node_composite_util.h"
 
 /* **************** Translate  ******************** */
@@ -58,7 +56,6 @@ static void exec(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
                Mask *mask = (Mask *)node->id;
                CompBuf *stackbuf;
                RenderData *rd = data;
-               MaskObject *maskobj = mask->maskobjs.first;
                float *res;
                int sx, sy;
 
@@ -84,42 +81,7 @@ static void exec(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
                stackbuf = alloc_compbuf(sx, sy, CB_VAL, TRUE);
                res = stackbuf->rect;
 
-               for (maskobj = mask->maskobjs.first; maskobj; maskobj = maskobj->next) {
-                       MaskSpline *spline;
-
-                       for (spline = maskobj->splines.first; spline; spline = spline->next) {
-                               float *diff_points;
-                               int tot_diff_point;
-
-                               diff_points = BKE_mask_spline_differentiate(spline, &tot_diff_point);
-
-                               /* TODO, make this optional! */
-                               if (sx != sy) {
-                                       float *fp;
-                                       int i;
-                                       float asp;
-
-                                       if (sx < sy) {
-                                               fp = &diff_points[0];
-                                               asp = (float)sx / (float)sy;
-                                       }
-                                       else {
-                                               fp = &diff_points[1];
-                                               asp = (float)sy / (float)sx;
-                                       }
-
-                                       for (i = 0; i < tot_diff_point; i++, fp += 2) {
-                                               (*fp) = (((*fp) - 0.5f) / asp) + 0.5f;
-                                       }
-                               }
-
-                               if (tot_diff_point) {
-                                       PLX_raskterize(diff_points, tot_diff_point, res, sx, sy);
-
-                                       MEM_freeN(diff_points);
-                               }
-                       }
-               }
+               BKE_mask_rasterize(mask, sx, sy, res);
 
                /* pass on output and free */
                out[0]->data = stackbuf;