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