1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * Copyright 2011, Blender Foundation.
17  */
18 
19 #include "COM_GlareGhostOperation.h"
20 #include "BLI_math.h"
21 #include "COM_FastGaussianBlurOperation.h"
22 
smoothMask(float x,float y)23 static float smoothMask(float x, float y)
24 {
25   float t;
26   x = 2.0f * x - 1.0f;
27   y = 2.0f * y - 1.0f;
28   if ((t = 1.0f - sqrtf(x * x + y * y)) > 0.0f) {
29     return t;
30   }
31 
32   return 0.0f;
33 }
34 
generateGlare(float * data,MemoryBuffer * inputTile,NodeGlare * settings)35 void GlareGhostOperation::generateGlare(float *data, MemoryBuffer *inputTile, NodeGlare *settings)
36 {
37   const int qt = 1 << settings->quality;
38   const float s1 = 4.0f / (float)qt, s2 = 2.0f * s1;
39   int x, y, n, p, np;
40   fRGB c, tc, cm[64];
41   float sc, isc, u, v, sm, s, t, ofs, scalef[64];
42   const float cmo = 1.0f - settings->colmod;
43 
44   MemoryBuffer *gbuf = inputTile->duplicate();
45   MemoryBuffer *tbuf1 = inputTile->duplicate();
46 
47   bool breaked = false;
48 
49   FastGaussianBlurOperation::IIR_gauss(tbuf1, s1, 0, 3);
50   if (!breaked) {
51     FastGaussianBlurOperation::IIR_gauss(tbuf1, s1, 1, 3);
52   }
53   if (isBraked()) {
54     breaked = true;
55   }
56   if (!breaked) {
57     FastGaussianBlurOperation::IIR_gauss(tbuf1, s1, 2, 3);
58   }
59 
60   MemoryBuffer *tbuf2 = tbuf1->duplicate();
61 
62   if (isBraked()) {
63     breaked = true;
64   }
65   if (!breaked) {
66     FastGaussianBlurOperation::IIR_gauss(tbuf2, s2, 0, 3);
67   }
68   if (isBraked()) {
69     breaked = true;
70   }
71   if (!breaked) {
72     FastGaussianBlurOperation::IIR_gauss(tbuf2, s2, 1, 3);
73   }
74   if (isBraked()) {
75     breaked = true;
76   }
77   if (!breaked) {
78     FastGaussianBlurOperation::IIR_gauss(tbuf2, s2, 2, 3);
79   }
80 
81   ofs = (settings->iter & 1) ? 0.5f : 0.0f;
82   for (x = 0; x < (settings->iter * 4); x++) {
83     y = x & 3;
84     cm[x][0] = cm[x][1] = cm[x][2] = 1;
85     if (y == 1) {
86       fRGB_rgbmult(cm[x], 1.0f, cmo, cmo);
87     }
88     if (y == 2) {
89       fRGB_rgbmult(cm[x], cmo, cmo, 1.0f);
90     }
91     if (y == 3) {
92       fRGB_rgbmult(cm[x], cmo, 1.0f, cmo);
93     }
94     scalef[x] = 2.1f * (1.0f - (x + ofs) / (float)(settings->iter * 4));
95     if (x & 1) {
96       scalef[x] = -0.99f / scalef[x];
97     }
98   }
99 
100   sc = 2.13;
101   isc = -0.97;
102   for (y = 0; y < gbuf->getHeight() && (!breaked); y++) {
103     v = ((float)y + 0.5f) / (float)gbuf->getHeight();
104     for (x = 0; x < gbuf->getWidth(); x++) {
105       u = ((float)x + 0.5f) / (float)gbuf->getWidth();
106       s = (u - 0.5f) * sc + 0.5f;
107       t = (v - 0.5f) * sc + 0.5f;
108       tbuf1->readBilinear(c, s * gbuf->getWidth(), t * gbuf->getHeight());
109       sm = smoothMask(s, t);
110       mul_v3_fl(c, sm);
111       s = (u - 0.5f) * isc + 0.5f;
112       t = (v - 0.5f) * isc + 0.5f;
113       tbuf2->readBilinear(tc, s * gbuf->getWidth() - 0.5f, t * gbuf->getHeight() - 0.5f);
114       sm = smoothMask(s, t);
115       madd_v3_v3fl(c, tc, sm);
116 
117       gbuf->writePixel(x, y, c);
118     }
119     if (isBraked()) {
120       breaked = true;
121     }
122   }
123 
124   memset(tbuf1->getBuffer(),
125          0,
126          tbuf1->getWidth() * tbuf1->getHeight() * COM_NUM_CHANNELS_COLOR * sizeof(float));
127   for (n = 1; n < settings->iter && (!breaked); n++) {
128     for (y = 0; y < gbuf->getHeight() && (!breaked); y++) {
129       v = ((float)y + 0.5f) / (float)gbuf->getHeight();
130       for (x = 0; x < gbuf->getWidth(); x++) {
131         u = ((float)x + 0.5f) / (float)gbuf->getWidth();
132         tc[0] = tc[1] = tc[2] = 0.0f;
133         for (p = 0; p < 4; p++) {
134           np = (n << 2) + p;
135           s = (u - 0.5f) * scalef[np] + 0.5f;
136           t = (v - 0.5f) * scalef[np] + 0.5f;
137           gbuf->readBilinear(c, s * gbuf->getWidth() - 0.5f, t * gbuf->getHeight() - 0.5f);
138           mul_v3_v3(c, cm[np]);
139           sm = smoothMask(s, t) * 0.25f;
140           madd_v3_v3fl(tc, c, sm);
141         }
142         tbuf1->addPixel(x, y, tc);
143       }
144       if (isBraked()) {
145         breaked = true;
146       }
147     }
148     memcpy(gbuf->getBuffer(),
149            tbuf1->getBuffer(),
150            tbuf1->getWidth() * tbuf1->getHeight() * COM_NUM_CHANNELS_COLOR * sizeof(float));
151   }
152   memcpy(data,
153          gbuf->getBuffer(),
154          gbuf->getWidth() * gbuf->getHeight() * COM_NUM_CHANNELS_COLOR * sizeof(float));
155 
156   delete gbuf;
157   delete tbuf1;
158   delete tbuf2;
159 }
160