Blender V2.61 - r43446

node_composite_huecorrect.c

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version. 
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * The Original Code is Copyright (C) 2006 Blender Foundation.
00019  * All rights reserved.
00020  *
00021  * The Original Code is: all of this file.
00022  *
00023  * Contributor(s): Matt Ebb
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  */
00027 
00033 #include "node_composite_util.h"
00034 
00035 static bNodeSocketTemplate cmp_node_huecorrect_in[]= {
00036     {   SOCK_FLOAT, 1, "Fac",   1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, PROP_FACTOR},
00037     {   SOCK_RGBA, 1, "Image",  1.0f, 1.0f, 1.0f, 1.0f},
00038     {   -1, 0, ""   }
00039 };
00040 
00041 static bNodeSocketTemplate cmp_node_huecorrect_out[]= {
00042     {   SOCK_RGBA, 0, "Image"},
00043     {   -1, 0, ""   }
00044 };
00045 
00046 static void do_huecorrect(bNode *node, float *out, float *in)
00047 {
00048     float hsv[3], f;
00049     
00050     rgb_to_hsv(in[0], in[1], in[2], hsv, hsv+1, hsv+2);
00051     
00052     /* adjust hue, scaling returned default 0.5 up to 1 */
00053     f = curvemapping_evaluateF(node->storage, 0, hsv[0]);
00054     hsv[0] += f-0.5f;
00055     
00056     /* adjust saturation, scaling returned default 0.5 up to 1 */
00057     f = curvemapping_evaluateF(node->storage, 1, hsv[0]);
00058     hsv[1] *= (f * 2.f);
00059     
00060     /* adjust value, scaling returned default 0.5 up to 1 */
00061     f = curvemapping_evaluateF(node->storage, 2, hsv[0]);
00062     hsv[2] *= (f * 2.f);
00063     
00064     hsv[0] = hsv[0] - floorf(hsv[0]); /* mod 1.0 */
00065     CLAMP(hsv[1], 0.f, 1.f);
00066     
00067     /* convert back to rgb */
00068     hsv_to_rgb(hsv[0], hsv[1], hsv[2], out, out+1, out+2);
00069     
00070     out[3]= in[3];
00071 }
00072 
00073 static void do_huecorrect_fac(bNode *node, float *out, float *in, float *fac)
00074 {
00075     float hsv[3], rgb[3], f;
00076     const float mfac = 1.f-*fac;
00077     
00078     rgb_to_hsv(in[0], in[1], in[2], hsv, hsv+1, hsv+2);
00079     
00080     /* adjust hue, scaling returned default 0.5 up to 1 */
00081     f = curvemapping_evaluateF(node->storage, 0, hsv[0]);
00082     hsv[0] += f-0.5f;
00083     
00084     /* adjust saturation, scaling returned default 0.5 up to 1 */
00085     f = curvemapping_evaluateF(node->storage, 1, hsv[0]);
00086     hsv[1] *= (f * 2.f);
00087     
00088     /* adjust value, scaling returned default 0.5 up to 1 */
00089     f = curvemapping_evaluateF(node->storage, 2, hsv[0]);
00090     hsv[2] *= (f * 2.f);
00091     
00092     hsv[0] = hsv[0] - floorf(hsv[0]);  /* mod 1.0 */
00093     CLAMP(hsv[1], 0.f, 1.f);
00094     
00095     /* convert back to rgb */
00096     hsv_to_rgb(hsv[0], hsv[1], hsv[2], rgb, rgb+1, rgb+2);
00097     
00098     out[0]= mfac*in[0] + *fac*rgb[0];
00099     out[1]= mfac*in[1] + *fac*rgb[1];
00100     out[2]= mfac*in[2] + *fac*rgb[2];
00101     out[3]= in[3];
00102 }
00103 
00104 static void node_composit_exec_huecorrect(void *UNUSED(data), bNode *node, bNodeStack **in, bNodeStack **out)
00105 {
00106     CompBuf *cbuf= in[1]->data;
00107     CompBuf *stackbuf;
00108     
00109     /* stack order input:  fac, image, black level, white level */
00110     /* stack order output: image */
00111     
00112     if(out[0]->hasoutput==0)
00113         return;
00114 
00115     if(in[0]->vec[0] == 0.f && in[0]->data == NULL) {
00116         out[0]->data = pass_on_compbuf(cbuf);
00117         return;
00118     }
00119     
00120     /* input no image? then only color operation */
00121     if(in[1]->data==NULL) {
00122         do_huecorrect_fac(node, out[0]->vec, in[1]->vec, in[0]->vec);
00123     }
00124     
00125     if (cbuf) {
00126         stackbuf= alloc_compbuf(cbuf->x, cbuf->y, CB_RGBA, 1); /* make output size of input image */
00127         
00128         if ((in[0]->data==NULL) && (in[0]->vec[0] >= 1.f))
00129             composit1_pixel_processor(node, stackbuf, in[1]->data, in[1]->vec, do_huecorrect, CB_RGBA);
00130         else
00131             composit2_pixel_processor(node, stackbuf, in[1]->data, in[1]->vec, in[0]->data, in[0]->vec, do_huecorrect_fac, CB_RGBA, CB_VAL);
00132         
00133         out[0]->data= stackbuf;
00134     }
00135     
00136 }
00137 
00138 static void node_composit_init_huecorrect(bNodeTree *UNUSED(ntree), bNode* node, bNodeTemplate *UNUSED(ntemp))
00139 {
00140     CurveMapping *cumapping = node->storage= curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
00141     int c;
00142     
00143     cumapping->preset = CURVE_PRESET_MID9;
00144     
00145     for (c=0; c<3; c++) {
00146         CurveMap *cuma = &cumapping->cm[c];
00147         curvemap_reset(cuma, &cumapping->clipr, cumapping->preset, CURVEMAP_SLOPE_POSITIVE);
00148     }
00149     
00150     /* default to showing Saturation */
00151     cumapping->cur = 1;
00152 }
00153 
00154 void register_node_type_cmp_huecorrect(bNodeTreeType *ttype)
00155 {
00156     static bNodeType ntype;
00157 
00158     node_type_base(ttype, &ntype, CMP_NODE_HUECORRECT, "Hue Correct", NODE_CLASS_OP_COLOR, NODE_OPTIONS);
00159     node_type_socket_templates(&ntype, cmp_node_huecorrect_in, cmp_node_huecorrect_out);
00160     node_type_size(&ntype, 320, 140, 400);
00161     node_type_init(&ntype, node_composit_init_huecorrect);
00162     node_type_storage(&ntype, "CurveMapping", node_free_curves, node_copy_curves);
00163     node_type_exec(&ntype, node_composit_exec_huecorrect);
00164 
00165     nodeRegisterType(ttype, &ntype);
00166 }