Brightness/Contrast Node for Cycles
authorDalai Felinto <dfelinto@gmail.com>
Tue, 24 Jan 2012 16:32:31 +0000 (16:32 +0000)
committerDalai Felinto <dfelinto@gmail.com>
Tue, 24 Jan 2012 16:32:31 +0000 (16:32 +0000)
Contrast helps to adjust IBL (HDR images used for background lighting).
Note: In the UI we are caling it Bright instead of Brightness. This copy what Blender composite is doing.
Note2: the algorithm we are using produces pure black when contrast is 100. I'm not a fan of that, but it's a division by zero. I would like to look at other algorithms (what gimp does for example). But that would be only after 2.62.

17 files changed:
intern/cycles/app/cycles_xml.cpp
intern/cycles/blender/blender_shader.cpp
intern/cycles/kernel/CMakeLists.txt
intern/cycles/kernel/osl/nodes/CMakeLists.txt
intern/cycles/kernel/osl/nodes/node_brightness.osl [new file with mode: 0644]
intern/cycles/kernel/svm/svm.h
intern/cycles/kernel/svm/svm_brightness.h [new file with mode: 0644]
intern/cycles/kernel/svm/svm_types.h
intern/cycles/render/nodes.cpp
intern/cycles/render/nodes.h
source/blender/blenkernel/BKE_node.h
source/blender/blenkernel/intern/node.c
source/blender/makesrna/intern/rna_nodetree_types.h
source/blender/nodes/CMakeLists.txt
source/blender/nodes/NOD_shader.h
source/blender/nodes/shader/nodes/node_shader_brightness.c [new file with mode: 0644]
source/blender/nodes/shader/nodes/node_shader_gamma.c

index af52520d87c37980ebd786eec76cb736815b2106..5e4b3da071dcf67b9ef21bd754eebbc86474886a 100644 (file)
@@ -462,6 +462,9 @@ static void xml_read_shader_graph(const XMLReadState& state, Shader *shader, pug
                else if(string_iequals(node.name(), "gamma")) {
                        snode = new GammaNode();
                }
+               else if(string_iequals(node.name(), "brightness")) {
+                       snode = new BrightContrastNode();
+               }
                else if(string_iequals(node.name(), "combine_rgb")) {
                        snode = new CombineRGBNode();
                }
index 1ce134f309470b7782dee3fb9ae98fb818377d46..f7b750e6dab8b5cfdb1d86f7710c483c40d1a310 100644 (file)
@@ -142,6 +142,10 @@ static ShaderNode *add_node(BL::BlendData b_data, ShaderGraph *graph, BL::Shader
                        node = new GammaNode();
                        break;
                }
+               case BL::ShaderNode::type_BRIGHTCONTRAST: {
+                       node = new BrightContrastNode();
+                       break;
+               }
                case BL::ShaderNode::type_MIX_RGB: {
                        BL::ShaderNodeMixRGB b_mix_node(b_node);
                        MixNode *mix = new MixNode();
index a4e0c0ddb56141b52c1871a2542b1d06cf51b13e..b6e749f3fcbbf9d2f790d790fb135f8d6b115d36 100644 (file)
@@ -61,6 +61,7 @@ set(SRC_SVM_HEADERS
        svm/svm_displace.h
        svm/svm_fresnel.h
        svm/svm_gamma.h
+       svm/svm_brightness.h
        svm/svm_geometry.h
        svm/svm_gradient.h
        svm/svm_hsv.h
index 1a207d6c139ed2446ebff9b6de5595f04e735127..d3a1cf59a372918418d6341f98a40ff6d3763f0f 100644 (file)
@@ -20,6 +20,7 @@ set(SRC_OSL
        node_environment_texture.osl
        node_fresnel.osl
        node_gamma.osl
+       node_brightness.osl
        node_geometry.osl
        node_glass_bsdf.osl
        node_glossy_bsdf.osl
diff --git a/intern/cycles/kernel/osl/nodes/node_brightness.osl b/intern/cycles/kernel/osl/nodes/node_brightness.osl
new file mode 100644 (file)
index 0000000..a93ff06
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * 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.
+ */
+
+#include "stdosl.h"
+
+shader node_brightness(
+       color ColorIn = color(0.8, 0.8, 0.8),
+       float Bright = 0.0,
+       float Contrast = 0.0,
+       output ColorOut = color(0.8, 0.8, 0.8)
+{
+       float delta = Contrast * (1.0/200.0);
+       float a = 1.0 - delta * 2.0;
+       float b;
+
+       Bright *= 1.0/100.0;
+
+       /*
+       * The algorithm is by Werner D. Streidt
+       * (http://visca.com/ffactory/archives/5-99/msg00021.html)
+       * Extracted of OpenCV demhist.c
+       */
+
+       if (Contrast > 0.0) {
+               a = (a < 0.0 ? 1.0/a : 0.0);
+               b = a * (Brightness - delta);
+       }
+       else {
+               delta *= -1.0;
+               b = a * (Brightness + delta);
+       }
+
+       ColorOut = a * ColorIn + b;
+}
+
index d22c9181a84e091886fa8929048ab12eba5991d5..f1bae9c8d9f9f88dcc24f29fa6a2640825ddf520 100644 (file)
@@ -131,6 +131,7 @@ CCL_NAMESPACE_END
 #include "svm_hsv.h"
 #include "svm_image.h"
 #include "svm_gamma.h"
+#include "svm_brightness.h"
 #include "svm_invert.h"
 #include "svm_light_path.h"
 #include "svm_magic.h"
@@ -270,6 +271,9 @@ __device_noinline void svm_eval_nodes(KernelGlobals *kg, ShaderData *sd, ShaderT
                        case NODE_GAMMA:
                                svm_node_gamma(sd, stack, node.y, node.z, node.w);
                                break;
+                       case NODE_BRIGHTCONTRAST:
+                               svm_node_brightness(sd, stack, node.y, node.z, node.w);
+                               break;
                        case NODE_MIX:
                                svm_node_mix(kg, sd, stack, node.y, node.z, node.w, &offset);
                                break;
diff --git a/intern/cycles/kernel/svm/svm_brightness.h b/intern/cycles/kernel/svm/svm_brightness.h
new file mode 100644 (file)
index 0000000..698899f
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * 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.
+ */
+
+CCL_NAMESPACE_BEGIN
+
+__device void svm_node_brightness(ShaderData *sd, float *stack, uint in_color, uint out_color, uint node)
+{
+       uint bright_offset, contrast_offset;
+       float3 color = stack_load_float3(stack, in_color);
+
+       decode_node_uchar4(node, &bright_offset, &contrast_offset, NULL, NULL);
+       float brightness = stack_load_float(stack, bright_offset);
+       float contrast  = stack_load_float(stack, contrast_offset);
+
+       brightness *= 1.0f/100.0f;
+       float delta = contrast * (1.0f/200.0f);
+       float a = 1.0f - delta * 2.0f;
+       float b;
+
+       /*
+       * The algorithm is by Werner D. Streidt
+       * (http://visca.com/ffactory/archives/5-99/msg00021.html)
+       * Extracted of OpenCV demhist.c
+       */
+       if (contrast > 0.0f) {
+               a = (a > 0.0f? (1.0f / a): 0.0f);
+               b = a * (brightness - delta);
+       }
+       else {
+               delta *= -1.0f;
+               b = a * (brightness + delta);
+       }
+
+       color.x = a*color.x + b;
+       color.y = a*color.y + b;
+       color.z = a*color.z + b;
+
+       if (stack_valid(out_color))
+               stack_store_float3(stack, out_color, color);
+}
+
+CCL_NAMESPACE_END
index 8203ed4c15ea12e4369af9550a5ffdc1638be093..ff6e9b94a0f106ed9385ec1586155b1f60a9b869 100644 (file)
@@ -87,7 +87,8 @@ typedef enum NodeType {
        NODE_INVERT = 5400,
        NODE_NORMAL = 5500,
        NODE_GAMMA = 5600,
-       NODE_TEX_CHECKER = 5700
+       NODE_TEX_CHECKER = 5700,
+       NODE_BRIGHTCONTRAST = 5800
 } NodeType;
 
 typedef enum NodeAttributeType {
index 7397c9c09af90a0e5c53b4ff97a462df1cc79516..982521b31f2dcd062110e54a8afa7b20e6b8fdf7 100644 (file)
@@ -1820,6 +1820,38 @@ void GammaNode::compile(OSLCompiler& compiler)
        compiler.add(this, "node_gamma");
 }
 
+/* Bright Contrast */
+BrightContrastNode::BrightContrastNode()
+: ShaderNode("brightness")
+{
+       add_input("Color", SHADER_SOCKET_COLOR);
+       add_input("Bright", SHADER_SOCKET_FLOAT);
+       add_input("Contrast", SHADER_SOCKET_FLOAT);
+       add_output("Color", SHADER_SOCKET_COLOR);
+}
+
+void BrightContrastNode::compile(SVMCompiler& compiler)
+{
+       ShaderInput *color_in = input("Color");
+       ShaderInput *bright_in = input("Bright");
+       ShaderInput *contrast_in = input("Contrast");
+       ShaderOutput *color_out = output("Color");
+
+       compiler.stack_assign(color_in);
+       compiler.stack_assign(bright_in);
+       compiler.stack_assign(contrast_in);
+       compiler.stack_assign(color_out);
+
+       compiler.add_node(NODE_BRIGHTCONTRAST,
+               color_in->stack_offset, color_out->stack_offset,
+               compiler.encode_uchar4(bright_in->stack_offset, contrast_in->stack_offset));
+}
+
+void BrightContrastNode::compile(OSLCompiler& compiler)
+{
+       compiler.add(this, "node_brightness");
+}
+
 /* Separate RGB */
 SeparateRGBNode::SeparateRGBNode()
 : ShaderNode("separate_rgb")
index c2f9ead5924132fc25f47e4a200ab777852177de..338af4ef48ac9c1158d7cae83a5f4986e99fb887 100644 (file)
@@ -320,6 +320,11 @@ public:
        SHADER_NODE_CLASS(GammaNode)
 };
 
+class BrightContrastNode : public ShaderNode {
+public:
+       SHADER_NODE_CLASS(BrightContrastNode)
+};
+
 class SeparateRGBNode : public ShaderNode {
 public:
        SHADER_NODE_CLASS(SeparateRGBNode)
index 67de699c7c42a697149eb120da6f7740795268c3..2fa587e168a58959e6c2d73529675972c4134b2c 100644 (file)
@@ -523,6 +523,7 @@ struct ShadeResult;
 #define SH_NODE_VOLUME_ISOTROPIC               162
 #define SH_NODE_GAMMA                          163
 #define SH_NODE_TEX_CHECKER                    164
+#define SH_NODE_BRIGHTCONTRAST                 165
 
 /* custom defines options for Material node */
 #define SH_NODE_MAT_DIFF   1
index 96ee2bd03490a25e699aef83c0a49ee9f72ca2ca..b72abcd79f42d62e78c739a9440350f086c5d776 100644 (file)
@@ -1881,6 +1881,7 @@ static void registerShaderNodes(bNodeTreeType *ttype)
        register_node_type_sh_material(ttype);
        register_node_type_sh_camera(ttype);
        register_node_type_sh_gamma(ttype);
+       register_node_type_sh_brightcontrast(ttype);
        register_node_type_sh_value(ttype);
        register_node_type_sh_rgb(ttype);
        register_node_type_sh_mix_rgb(ttype);
index 5ab907c87dc0afd32d693037cb7e6fbc271ac68b..2284b09e941763c8413347ef4cfebc8dd6319314 100644 (file)
@@ -41,6 +41,7 @@ DefNode( ShaderNode,     SH_NODE_RGBTOBW,         0,                      "RGBTO
 DefNode( ShaderNode,     SH_NODE_TEXTURE,         def_texture,            "TEXTURE",        Texture,          "Texture",           ""              )
 DefNode( ShaderNode,     SH_NODE_NORMAL,          0,                      "NORMAL",         Normal,           "Normal",            ""              )
 DefNode( ShaderNode,     SH_NODE_GAMMA,           0,                      "GAMMA",          Gamma,            "Gamma",             ""              )
+DefNode( ShaderNode,     SH_NODE_BRIGHTCONTRAST,  0,                      "BRIGHTCONTRAST", BrightContrast,   "Bright Contrast",   ""              )
 DefNode( ShaderNode,     SH_NODE_GEOMETRY,        def_sh_geometry,        "GEOMETRY",       Geometry,         "Geometry",          ""              )
 DefNode( ShaderNode,     SH_NODE_MAPPING,         def_sh_mapping,         "MAPPING",        Mapping,          "Mapping",           ""              )
 DefNode( ShaderNode,     SH_NODE_CURVE_VEC,       def_vector_curve,       "CURVE_VEC",      VectorCurve,      "Vector Curve",      ""              )
index 6d5c282d308d61ad64cd4131a0bb45c2338bb160..b458ae3a77dbe4132898f38757da53e120337c6e 100644 (file)
@@ -115,6 +115,7 @@ set(SRC
        shader/nodes/node_shader_curves.c
        shader/nodes/node_shader_dynamic.c
        shader/nodes/node_shader_gamma.c
+       shader/nodes/node_shader_brightness.c
        shader/nodes/node_shader_geom.c
        shader/nodes/node_shader_hueSatVal.c
        shader/nodes/node_shader_invert.c
index 907efd7678647a172023261e51cf8f0b1e7771c9..63fc0f4232c71381b9d52b49765f8ffec1ef60a5 100644 (file)
@@ -55,6 +55,7 @@ void register_node_type_sh_rgbtobw(struct bNodeTreeType *ttype);
 void register_node_type_sh_texture(struct bNodeTreeType *ttype);
 void register_node_type_sh_normal(struct bNodeTreeType *ttype);
 void register_node_type_sh_gamma(struct bNodeTreeType *ttype);
+void register_node_type_sh_brightcontrast(struct bNodeTreeType *ttype);
 void register_node_type_sh_geom(struct bNodeTreeType *ttype);
 void register_node_type_sh_mapping(struct bNodeTreeType *ttype);
 void register_node_type_sh_curve_vec(struct bNodeTreeType *ttype);
diff --git a/source/blender/nodes/shader/nodes/node_shader_brightness.c b/source/blender/nodes/shader/nodes/node_shader_brightness.c
new file mode 100644 (file)
index 0000000..7448a0b
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * ***** 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) 2006 Blender Foundation.
+ * All rights reserved.
+ * 
+ * The Original Code is: all of this file.
+ * 
+ * Contributor(s): none yet.
+ * 
+ * ***** END GPL LICENSE BLOCK *****
+
+*/
+
+#include "node_shader_util.h"
+
+
+/* **************** Brigh and contrsast  ******************** */
+
+static bNodeSocketTemplate sh_node_brightcontrast_in[]= {
+       {       SOCK_RGBA, 1, "Color",                  1.0f, 1.0f, 1.0f, 1.0f},
+       {       SOCK_FLOAT, 1, "Bright",                0.0f, 0.0f, 0.0f, 0.0f, -100.0f, 100.0f, PROP_NONE},
+       {       SOCK_FLOAT, 1, "Contrast",              0.0f, 0.0f, 0.0f, 0.0f, -100.0f, 100.0f, PROP_NONE},
+       {       -1, 0, ""       }
+};
+
+static bNodeSocketTemplate sh_node_brightcontrast_out[]= {
+       {       SOCK_RGBA, 0, "Color"},
+       {       -1, 0, ""       }
+};
+
+void register_node_type_sh_brightcontrast(bNodeTreeType *ttype)
+{
+       static bNodeType ntype;
+       
+       node_type_base(ttype, &ntype, SH_NODE_BRIGHTCONTRAST, "Bright/Contrast", NODE_CLASS_OP_COLOR, NODE_OPTIONS);
+       node_type_compatibility(&ntype, NODE_NEW_SHADING);
+       node_type_socket_templates(&ntype, sh_node_brightcontrast_in, sh_node_brightcontrast_out);
+       node_type_size(&ntype, 140, 100, 320);
+       node_type_init(&ntype, NULL);
+       node_type_storage(&ntype, "", NULL, NULL);
+       node_type_exec(&ntype, NULL);
+       node_type_gpu(&ntype, NULL);
+       
+       nodeRegisterType(ttype, &ntype);
+}
index 1d525d7169898a0602a9b8cbde3329b22c478944..e2907f5ba912e9ac440dacb2951e7ed713f76d15 100644 (file)
 
 */
 
-/** \file blender/nodes/composite/nodes/node_composite_gamma.c
- *  \ingroup cmpnodes
- */
-
-
 #include "node_shader_util.h"
 
 /* **************** Gamma Tools  ******************** */