Blender V2.61 - r43446

node_composite_tonemap.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): Alfredo de Greef  (eeshlo)
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  */
00027 
00033 #include "node_composite_util.h"
00034 
00035 static bNodeSocketTemplate cmp_node_tonemap_in[]= {
00036     {   SOCK_RGBA, 1, "Image",          1.0f, 1.0f, 1.0f, 1.0f},
00037     {   -1, 0, ""   }
00038 };
00039 static bNodeSocketTemplate cmp_node_tonemap_out[]= {
00040     {   SOCK_RGBA, 0, "Image"},
00041     {   -1, 0, ""   }
00042 };
00043 
00044 
00045 static float avgLogLum(CompBuf *src, float* auto_key, float* Lav, float* Cav)
00046 {
00047     float lsum = 0;
00048     int p = src->x*src->y;
00049     fRGB* bc = (fRGB*)src->rect;
00050     float avl, maxl = -1e10f, minl = 1e10f;
00051     const float sc = 1.f/(src->x*src->y);
00052     *Lav = 0.f;
00053     while (p--) {
00054         float L = 0.212671f*bc[0][0] + 0.71516f*bc[0][1] + 0.072169f*bc[0][2];
00055         *Lav += L;
00056         fRGB_add(Cav, bc[0]);
00057         lsum += (float)log((double)MAX2(L, 0.0) + 1e-5);
00058         maxl = (L > maxl) ? L : maxl;
00059         minl = (L < minl) ? L : minl;
00060         bc++;
00061     }
00062     *Lav *= sc;
00063     fRGB_mult(Cav, sc);
00064     maxl = log((double)maxl + 1e-5); minl = log((double)minl + 1e-5f); avl = lsum*sc;
00065     *auto_key = (maxl > minl) ? ((maxl - avl) / (maxl - minl)) : 1.f;
00066     return exp((double)avl);
00067 }
00068 
00069 
00070 static void tonemap(NodeTonemap* ntm, CompBuf* dst, CompBuf* src)
00071 {
00072     int x, y;
00073     float dr, dg, db, al, igm = (ntm->gamma==0.f) ? 1 : (1.f / ntm->gamma);
00074     float auto_key, Lav, Cav[3] = {0, 0, 0};
00075 
00076     al = avgLogLum(src, &auto_key, &Lav, Cav);
00077     al = (al == 0.f) ? 0.f : (ntm->key / al);
00078 
00079     if (ntm->type == 1) {
00080         // Reinhard/Devlin photoreceptor
00081         const float f = exp((double)-ntm->f);
00082         const float m = (ntm->m > 0.f) ? ntm->m : (0.3f + 0.7f*pow((double)auto_key, 1.4));
00083         const float ic = 1.f - ntm->c, ia = 1.f - ntm->a;
00084         if (ntm->m == 0.f) printf("tonemap node, M: %g\n", m); 
00085         for (y=0; y<src->y; ++y) {
00086             fRGB* sp = (fRGB*)&src->rect[y*src->x*src->type];
00087             fRGB* dp = (fRGB*)&dst->rect[y*src->x*src->type];
00088             for (x=0; x<src->x; ++x) {
00089                 const float L = 0.212671f*sp[x][0] + 0.71516f*sp[x][1] + 0.072169f*sp[x][2];
00090                 float I_l = sp[x][0] + ic*(L - sp[x][0]);
00091                 float I_g = Cav[0] + ic*(Lav - Cav[0]);
00092                 float I_a = I_l + ia*(I_g - I_l);
00093                 dp[x][0] /= (dp[x][0] + pow((double)f*I_a, (double)m));
00094                 I_l = sp[x][1] + ic*(L - sp[x][1]);
00095                 I_g = Cav[1] + ic*(Lav - Cav[1]);
00096                 I_a = I_l + ia*(I_g - I_l);
00097                 dp[x][1] /= (dp[x][1] + pow((double)f*I_a,(double)m));
00098                 I_l = sp[x][2] + ic*(L - sp[x][2]);
00099                 I_g = Cav[2] + ic*(Lav - Cav[2]);
00100                 I_a = I_l + ia*(I_g - I_l);
00101                 dp[x][2] /= (dp[x][2] + pow((double)f*I_a, (double)m));
00102             }
00103         }
00104         return;
00105     }
00106 
00107     // Reinhard simple photographic tm (simplest, not using whitepoint var)
00108     for (y=0; y<src->y; y++) {
00109         fRGB* sp = (fRGB*)&src->rect[y*src->x*src->type];
00110         fRGB* dp = (fRGB*)&dst->rect[y*src->x*src->type];
00111         for (x=0; x<src->x; x++) {
00112             fRGB_copy(dp[x], sp[x]);
00113             fRGB_mult(dp[x], al);
00114             dr = dp[x][0] + ntm->offset;
00115             dg = dp[x][1] + ntm->offset;
00116             db = dp[x][2] + ntm->offset;
00117             dp[x][0] /= ((dr == 0.f) ? 1.f : dr);
00118             dp[x][1] /= ((dg == 0.f) ? 1.f : dg);
00119             dp[x][2] /= ((db == 0.f) ? 1.f : db);
00120             if (igm != 0.f) {
00121                 dp[x][0] = pow((double)MAX2(dp[x][0], 0.), igm);
00122                 dp[x][1] = pow((double)MAX2(dp[x][1], 0.), igm);
00123                 dp[x][2] = pow((double)MAX2(dp[x][2], 0.), igm);
00124             }
00125         }
00126     }
00127 }
00128 
00129 
00130 static void node_composit_exec_tonemap(void *UNUSED(data), bNode *node, bNodeStack **in, bNodeStack **out)
00131 {
00132     CompBuf *new, *img = in[0]->data;
00133 
00134     if ((img==NULL) || (out[0]->hasoutput==0)) return;
00135 
00136     if (img->type != CB_RGBA)
00137         img = typecheck_compbuf(img, CB_RGBA);
00138     
00139     new = dupalloc_compbuf(img);
00140 
00141     tonemap(node->storage, new, img);
00142 
00143     out[0]->data = new;
00144     
00145     if(img!=in[0]->data)
00146         free_compbuf(img);
00147 }
00148 
00149 static void node_composit_init_tonemap(bNodeTree *UNUSED(ntree), bNode* node, bNodeTemplate *UNUSED(ntemp))
00150 {
00151     NodeTonemap *ntm = MEM_callocN(sizeof(NodeTonemap), "node tonemap data");
00152     ntm->type = 1;
00153     ntm->key = 0.18;
00154     ntm->offset = 1;
00155     ntm->gamma = 1;
00156     ntm->f = 0;
00157     ntm->m = 0; // actual value is set according to input
00158     // default a of 1 works well with natural HDR images, but not always so for cgi.
00159     // Maybe should use 0 or at least lower initial value instead
00160     ntm->a = 1;
00161     ntm->c = 0;
00162     node->storage = ntm;
00163 }
00164 
00165 void register_node_type_cmp_tonemap(bNodeTreeType *ttype)
00166 {
00167     static bNodeType ntype;
00168 
00169     node_type_base(ttype, &ntype, CMP_NODE_TONEMAP, "Tonemap", NODE_CLASS_OP_COLOR, NODE_OPTIONS);
00170     node_type_socket_templates(&ntype, cmp_node_tonemap_in, cmp_node_tonemap_out);
00171     node_type_size(&ntype, 150, 120, 200);
00172     node_type_init(&ntype, node_composit_init_tonemap);
00173     node_type_storage(&ntype, "NodeTonemap", node_free_standard_storage, node_copy_standard_storage);
00174     node_type_exec(&ntype, node_composit_exec_tonemap);
00175 
00176     nodeRegisterType(ttype, &ntype);
00177 }