Blender  V2.59
CMP_tonemap.c
Go to the documentation of this file.
00001 /*
00002  *
00003  * ***** BEGIN GPL LICENSE BLOCK *****
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * as published by the Free Software Foundation; either version 2
00008  * of the License, or (at your option) any later version. 
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software Foundation,
00017  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018  *
00019  * The Original Code is Copyright (C) 2006 Blender Foundation.
00020  * All rights reserved.
00021  *
00022  * The Original Code is: all of this file.
00023  *
00024  * Contributor(s): Alfredo de Greef  (eeshlo)
00025  *
00026  * ***** END GPL LICENSE BLOCK *****
00027  */
00028 
00034 #include "../CMP_util.h"
00035 
00036 static bNodeSocketType cmp_node_tonemap_in[]= {
00037         {       SOCK_RGBA, 1, "Image",                  0.8f, 0.8f, 0.8f, 1.0f, 0.0f, 1.0f},
00038         {       -1, 0, ""       }
00039 };
00040 static bNodeSocketType cmp_node_tonemap_out[]= {
00041         {       SOCK_RGBA, 0, "Image",                  0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
00042         {       -1, 0, ""       }
00043 };
00044 
00045 
00046 static float avgLogLum(CompBuf *src, float* auto_key, float* Lav, float* Cav)
00047 {
00048         float lsum = 0;
00049         int p = src->x*src->y;
00050         fRGB* bc = (fRGB*)src->rect;
00051         float avl, maxl = -1e10f, minl = 1e10f;
00052         const float sc = 1.f/(src->x*src->y);
00053         *Lav = 0.f;
00054         while (p--) {
00055                 float L = 0.212671f*bc[0][0] + 0.71516f*bc[0][1] + 0.072169f*bc[0][2];
00056                 *Lav += L;
00057                 fRGB_add(Cav, bc[0]);
00058                 lsum += (float)log((double)MAX2(L, 0.0) + 1e-5);
00059                 maxl = (L > maxl) ? L : maxl;
00060                 minl = (L < minl) ? L : minl;
00061                 bc++;
00062         }
00063         *Lav *= sc;
00064         fRGB_mult(Cav, sc);
00065         maxl = log((double)maxl + 1e-5); minl = log((double)minl + 1e-5f); avl = lsum*sc;
00066         *auto_key = (maxl > minl) ? ((maxl - avl) / (maxl - minl)) : 1.f;
00067         return exp((double)avl);
00068 }
00069 
00070 
00071 static void tonemap(NodeTonemap* ntm, CompBuf* dst, CompBuf* src)
00072 {
00073         int x, y;
00074         float dr, dg, db, al, igm = (ntm->gamma==0.f) ? 1 : (1.f / ntm->gamma);
00075         float auto_key, Lav, Cav[3] = {0, 0, 0};
00076 
00077         al = avgLogLum(src, &auto_key, &Lav, Cav);
00078         al = (al == 0.f) ? 0.f : (ntm->key / al);
00079 
00080         if (ntm->type == 1) {
00081                 // Reinhard/Devlin photoreceptor
00082                 const float f = exp((double)-ntm->f);
00083                 const float m = (ntm->m > 0.f) ? ntm->m : (0.3f + 0.7f*pow((double)auto_key, 1.4));
00084                 const float ic = 1.f - ntm->c, ia = 1.f - ntm->a;
00085                 if (ntm->m == 0.f) printf("tonemap node, M: %g\n", m); 
00086                 for (y=0; y<src->y; ++y) {
00087                         fRGB* sp = (fRGB*)&src->rect[y*src->x*src->type];
00088                         fRGB* dp = (fRGB*)&dst->rect[y*src->x*src->type];
00089                         for (x=0; x<src->x; ++x) {
00090                                 const float L = 0.212671f*sp[x][0] + 0.71516f*sp[x][1] + 0.072169f*sp[x][2];
00091                                 float I_l = sp[x][0] + ic*(L - sp[x][0]);
00092                                 float I_g = Cav[0] + ic*(Lav - Cav[0]);
00093                                 float I_a = I_l + ia*(I_g - I_l);
00094                                 dp[x][0] /= (dp[x][0] + pow((double)f*I_a, (double)m));
00095                                 I_l = sp[x][1] + ic*(L - sp[x][1]);
00096                                 I_g = Cav[1] + ic*(Lav - Cav[1]);
00097                                 I_a = I_l + ia*(I_g - I_l);
00098                                 dp[x][1] /= (dp[x][1] + pow((double)f*I_a,(double)m));
00099                                 I_l = sp[x][2] + ic*(L - sp[x][2]);
00100                                 I_g = Cav[2] + ic*(Lav - Cav[2]);
00101                                 I_a = I_l + ia*(I_g - I_l);
00102                                 dp[x][2] /= (dp[x][2] + pow((double)f*I_a, (double)m));
00103                         }
00104                 }
00105                 return;
00106         }
00107 
00108         // Reinhard simple photographic tm (simplest, not using whitepoint var)
00109         for (y=0; y<src->y; y++) {
00110                 fRGB* sp = (fRGB*)&src->rect[y*src->x*src->type];
00111                 fRGB* dp = (fRGB*)&dst->rect[y*src->x*src->type];
00112                 for (x=0; x<src->x; x++) {
00113                         fRGB_copy(dp[x], sp[x]);
00114                         fRGB_mult(dp[x], al);
00115                         dr = dp[x][0] + ntm->offset;
00116                         dg = dp[x][1] + ntm->offset;
00117                         db = dp[x][2] + ntm->offset;
00118                         dp[x][0] /= ((dr == 0.f) ? 1.f : dr);
00119                         dp[x][1] /= ((dg == 0.f) ? 1.f : dg);
00120                         dp[x][2] /= ((db == 0.f) ? 1.f : db);
00121                         if (igm != 0.f) {
00122                                 dp[x][0] = pow((double)MAX2(dp[x][0], 0.), igm);
00123                                 dp[x][1] = pow((double)MAX2(dp[x][1], 0.), igm);
00124                                 dp[x][2] = pow((double)MAX2(dp[x][2], 0.), igm);
00125                         }
00126                 }
00127         }
00128 }
00129 
00130 
00131 static void node_composit_exec_tonemap(void *UNUSED(data), bNode *node, bNodeStack **in, bNodeStack **out)
00132 {
00133         CompBuf *new, *img = in[0]->data;
00134 
00135         if ((img==NULL) || (out[0]->hasoutput==0)) return;
00136 
00137         if (img->type != CB_RGBA)
00138                 img = typecheck_compbuf(img, CB_RGBA);
00139         
00140         new = dupalloc_compbuf(img);
00141 
00142         tonemap(node->storage, new, img);
00143 
00144         out[0]->data = new;
00145         
00146         if(img!=in[0]->data)
00147                 free_compbuf(img);
00148 }
00149 
00150 static void node_composit_init_tonemap(bNode* node)
00151 {
00152         NodeTonemap *ntm = MEM_callocN(sizeof(NodeTonemap), "node tonemap data");
00153         ntm->type = 1;
00154         ntm->key = 0.18;
00155         ntm->offset = 1;
00156         ntm->gamma = 1;
00157         ntm->f = 0;
00158         ntm->m = 0;     // actual value is set according to input
00159         // default a of 1 works well with natural HDR images, but not always so for cgi.
00160         // Maybe should use 0 or at least lower initial value instead
00161         ntm->a = 1;
00162         ntm->c = 0;
00163         node->storage = ntm;
00164 }
00165 
00166 void register_node_type_cmp_tonemap(ListBase *lb)
00167 {
00168         static bNodeType ntype;
00169 
00170         node_type_base(&ntype, CMP_NODE_TONEMAP, "Tonemap", NODE_CLASS_OP_COLOR, NODE_OPTIONS,
00171                 cmp_node_tonemap_in, cmp_node_tonemap_out);
00172         node_type_size(&ntype, 150, 120, 200);
00173         node_type_init(&ntype, node_composit_init_tonemap);
00174         node_type_storage(&ntype, "NodeTonemap", node_free_standard_storage, node_copy_standard_storage);
00175         node_type_exec(&ntype, node_composit_exec_tonemap);
00176 
00177         nodeRegisterType(lb, &ntype);
00178 }
00179