Brightness/Contrast Node for Cycles

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.
This commit is contained in:
Dalai Felinto 2012-01-24 16:32:31 +00:00
parent 1f9e25ac1a
commit 335ffb0ff3
17 changed files with 224 additions and 6 deletions

@ -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();
}

@ -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();

@ -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

@ -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

@ -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;
}

@ -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;

@ -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

@ -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 {

@ -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")

@ -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)

@ -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

@ -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);

@ -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", "" )

@ -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

@ -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);

@ -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);
}

@ -26,11 +26,6 @@
*/
/** \file blender/nodes/composite/nodes/node_composite_gamma.c
* \ingroup cmpnodes
*/
#include "node_shader_util.h"
/* **************** Gamma Tools ******************** */