Merged the particles-2010 branch with node improvements into trunk.
[blender-staging.git] / source / blender / nodes / composite / nodes / node_composite_lensdist.c
1 /*
2  *
3  * ***** BEGIN GPL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version. 
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  * The Original Code is Copyright (C) 2006 Blender Foundation.
20  * All rights reserved.
21  *
22  * The Original Code is: all of this file.
23  *
24  * Contributor(s): Alfredo de Greef  (eeshlo)
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /** \file blender/nodes/composite/nodes/node_composite_lensdist.c
30  *  \ingroup cmpnodes
31  */
32
33
34 #include "node_composite_util.h"
35
36 static bNodeSocketTemplate cmp_node_lensdist_in[]= {
37         {       SOCK_RGBA, 1, "Image",                  0.8f, 0.8f, 0.8f, 1.0f},
38         {       SOCK_FLOAT, 1, "Distort",       0.f, 0.f, 0.f, 0.f, -0.999f, 1.f, PROP_NONE},
39         {       SOCK_FLOAT, 1, "Dispersion", 0.f, 0.f, 0.f, 0.f, 0.f, 1.f, PROP_NONE},
40         {       -1, 0, ""       }
41 };
42 static bNodeSocketTemplate cmp_node_lensdist_out[]= {
43         {       SOCK_RGBA, 0, "Image"},
44         {       -1, 0, ""       }
45 };
46
47 /* assumes *dst is type RGBA */
48 static void lensDistort(CompBuf *dst, CompBuf *src, float kr, float kg, float kb, int jit, int proj, int fit)
49 {
50         int x, y, z;
51         const float cx = 0.5f*(float)dst->x, cy = 0.5f*(float)dst->y;
52
53         if (proj) {
54                 // shift
55                 CompBuf *tsrc = dupalloc_compbuf(src);
56                 
57                 for (z=0; z<tsrc->type; ++z)
58                         IIR_gauss(tsrc, (kr+0.5f)*(kr+0.5f), z, 1);
59                 kr *= 20.f;
60                 
61                 for (y=0; y<dst->y; y++) {
62                         fRGB *colp = (fRGB*)&dst->rect[y*dst->x*dst->type];
63                         const float v = (y + 0.5f)/(float)dst->y;
64                         
65                         for (x=0; x<dst->x; x++) {
66                                 const float u = (x + 0.5f)/(float)dst->x;
67                                 
68                                 qd_getPixelLerpChan(tsrc, (u*dst->x + kr) - 0.5f, v*dst->y - 0.5f, 0, colp[x]);
69                                 if (tsrc->type == CB_VAL)
70                                         colp[x][1] = tsrc->rect[x + y*tsrc->x];
71                                 else
72                                         colp[x][1] = tsrc->rect[(x + y*tsrc->x)*tsrc->type + 1];
73                                 qd_getPixelLerpChan(tsrc, (u*dst->x - kr) - 0.5f, v*dst->y - 0.5f, 2, colp[x]+2);
74                                 
75                                 /* set alpha */
76                                 colp[x][3]= 1.0f;
77                         }
78                 }
79                 free_compbuf(tsrc);
80         }
81         else {
82                 // Spherical
83                 // Scale factor to make bottom/top & right/left sides fit in window after deform
84                 // so in the case of pincushion (kn < 0), corners will be outside window.
85                 // Now also optionally scales image such that black areas are not visible when distort factor is positive
86                 // (makes distorted corners match window corners, but really only valid if mk<=0.5)
87                 const float mk = MAX3(kr, kg, kb);
88                 const float sc = (fit && (mk > 0.f)) ? (1.f/(1.f + 2.f*mk)) : (1.f/(1.f + mk));
89                 const float drg = 4.f*(kg - kr), dgb = 4.f*(kb - kg);
90                 
91                 kr *= 4.f, kg *= 4.f, kb *= 4.f;
92
93                 for (y=0; y<dst->y; y++) {
94                         fRGB *colp = (fRGB*)&dst->rect[y*dst->x*dst->type];
95                         const float v = sc*((y + 0.5f) - cy)/cy;
96                         
97                         for (x=0; x<dst->x; x++) {
98                                 int dr = 0, dg = 0, db = 0;
99                                 float d, t, ln[6] = {0, 0, 0, 0, 0, 0};
100                                 fRGB c1, tc = {0, 0, 0, 0};
101                                 const float u = sc*((x + 0.5f) - cx)/cx;
102                                 int sta = 0, mid = 0, end = 0;
103                                 
104                                 if ((t = 1.f - kr*(u*u + v*v)) >= 0.f) {
105                                         d = 1.f/(1.f + sqrtf(t));
106                                         ln[0] = (u*d + 0.5f)*dst->x - 0.5f, ln[1] = (v*d + 0.5f)*dst->y - 0.5f;
107                                         sta = 1;
108                                 }
109                                 if ((t = 1.f - kg*(u*u + v*v)) >= 0.f) {
110                                         d = 1.f/(1.f + sqrtf(t));
111                                         ln[2] = (u*d + 0.5f)*dst->x - 0.5f, ln[3] = (v*d + 0.5f)*dst->y - 0.5f;
112                                         mid = 1;
113                                 }
114                                 if ((t = 1.f - kb*(u*u + v*v)) >= 0.f) {
115                                         d = 1.f/(1.f + sqrtf(t));
116                                         ln[4] = (u*d + 0.5f)*dst->x - 0.5f, ln[5] = (v*d + 0.5f)*dst->y - 0.5f;
117                                         end = 1;
118                                 }
119         
120                                 if (sta && mid && end) {
121                                         // RG
122                                         const int dx = ln[2] - ln[0], dy = ln[3] - ln[1];
123                                         const float dsf = sqrtf(dx*dx + dy*dy) + 1.f;
124                                         const int ds = (int)(jit ? ((dsf < 4.f) ? 2.f : sqrtf(dsf)) : dsf);
125                                         const float sd = 1.f/(float)ds;
126                                         
127                                         for (z=0; z<ds; ++z) {
128                                                 const float tz = ((float)z + (jit ? BLI_frand() : 0.5f))*sd;
129                                                 t = 1.f - (kr + tz*drg)*(u*u + v*v);
130                                                 d = 1.f / (1.f + sqrtf(t));
131                                                 qd_getPixelLerp(src, (u*d + 0.5f)*dst->x - 0.5f, (v*d + 0.5f)*dst->y - 0.5f, c1);
132                                                 if (src->type == CB_VAL) c1[1] = c1[2] = c1[0];
133                                                 tc[0] += (1.f-tz)*c1[0], tc[1] += tz*c1[1];
134                                                 dr++, dg++;
135                                         }
136                                         // GB
137                                         {
138                                                 const int dx = ln[4] - ln[2], dy = ln[5] - ln[3];
139                                                 const float dsf = sqrtf(dx*dx + dy*dy) + 1.f;
140                                                 const int ds = (int)(jit ? ((dsf < 4.f) ? 2.f : sqrtf(dsf)) : dsf);
141                                                 const float sd = 1.f/(float)ds;
142                                                 
143                                                 for (z=0; z<ds; ++z) {
144                                                         const float tz = ((float)z + (jit ? BLI_frand() : 0.5f))*sd;
145                                                         t = 1.f - (kg + tz*dgb)*(u*u + v*v);
146                                                         d = 1.f / (1.f + sqrtf(t));
147                                                         qd_getPixelLerp(src, (u*d + 0.5f)*dst->x - 0.5f, (v*d + 0.5f)*dst->y - 0.5f, c1);
148                                                         if (src->type == CB_VAL) c1[1] = c1[2] = c1[0];
149                                                         tc[1] += (1.f-tz)*c1[1], tc[2] += tz*c1[2];
150                                                         dg++, db++;
151                                                 }
152                                         }
153                                 }
154         
155                                 if (dr) colp[x][0] = 2.f*tc[0] / (float)dr;
156                                 if (dg) colp[x][1] = 2.f*tc[1] / (float)dg;
157                                 if (db) colp[x][2] = 2.f*tc[2] / (float)db;
158         
159                                 /* set alpha */
160                                 colp[x][3]= 1.0f;
161                         }
162                 }
163         }
164 }
165
166
167 static void node_composit_exec_lensdist(void *UNUSED(data), bNode *node, bNodeStack **in, bNodeStack **out)
168 {
169         CompBuf *new, *img = in[0]->data;
170         NodeLensDist *nld = node->storage;
171         const float k = MAX2(MIN2(in[1]->vec[0], 1.f), -0.999f);
172         // smaller dispersion range for somewhat more control
173         const float d = 0.25f*MAX2(MIN2(in[2]->vec[0], 1.f), 0.f);
174         const float kr = MAX2(MIN2((k+d), 1.f), -0.999f), kb = MAX2(MIN2((k-d), 1.f), -0.999f);
175
176         if ((img==NULL) || (out[0]->hasoutput==0)) return;
177
178         new = alloc_compbuf(img->x, img->y, CB_RGBA, 1);
179
180         lensDistort(new, img, (nld->proj ? d : kr), k, kb, nld->jit, nld->proj, nld->fit);
181
182         out[0]->data = new;
183 }
184
185
186 static void node_composit_init_lensdist(bNodeTree *UNUSED(ntree), bNode* node, bNodeTemplate *UNUSED(ntemp))
187 {
188         NodeLensDist *nld = MEM_callocN(sizeof(NodeLensDist), "node lensdist data");
189         nld->jit = nld->proj = nld->fit = 0;
190         node->storage = nld;
191 }
192
193
194 void register_node_type_cmp_lensdist(ListBase *lb)
195 {
196         static bNodeType ntype;
197
198         node_type_base(&ntype, CMP_NODE_LENSDIST, "Lens Distortion", NODE_CLASS_DISTORT, NODE_OPTIONS);
199         node_type_socket_templates(&ntype, cmp_node_lensdist_in, cmp_node_lensdist_out);
200         node_type_size(&ntype, 150, 120, 200);
201         node_type_init(&ntype, node_composit_init_lensdist);
202         node_type_storage(&ntype, "NodeLensDist", node_free_standard_storage, node_copy_standard_storage);
203         node_type_exec(&ntype, node_composit_exec_lensdist);
204
205         nodeRegisterType(lb, &ntype);
206 }
207