Fix T50736: Zero streaks in Glare node.
[blender.git] / source / blender / compositor / operations / COM_GlareStreaksOperation.cpp
1 /*
2  * Copyright 2011, Blender Foundation.
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  * Contributor:
19  *              Jeroen Bakker
20  *              Monique Dewanchand
21  */
22
23 #include "COM_GlareStreaksOperation.h"
24 #include "BLI_math.h"
25
26 void GlareStreaksOperation::generateGlare(float *data, MemoryBuffer *inputTile, NodeGlare *settings)
27 {
28         int x, y, n;
29         unsigned int nump = 0;
30         float c1[4], c2[4], c3[4], c4[4];
31         float a, ang = DEG2RADF(360.0f) / (float)settings->streaks;
32
33         int size = inputTile->getWidth() * inputTile->getHeight();
34         int size4 = size * 4;
35
36         bool breaked = false;
37
38         MemoryBuffer *tsrc = inputTile->duplicate();
39         MemoryBuffer *tdst = new MemoryBuffer(COM_DT_COLOR, inputTile->getRect());
40         tdst->clear();
41         memset(data, 0, size4 * sizeof(float));
42
43         for (a = 0.0f; a < DEG2RADF(360.0f) && (!breaked); a += ang) {
44                 const float an = a + settings->angle_ofs;
45                 const float vx = cos((double)an), vy = sin((double)an);
46                 for (n = 0; n < settings->iter && (!breaked); ++n) {
47                         const float p4 = pow(4.0, (double)n);
48                         const float vxp = vx * p4, vyp = vy * p4;
49                         const float wt = pow((double)settings->fade, (double)p4);
50                         const float cmo = 1.0f - (float)pow((double)settings->colmod, (double)n + 1);  // colormodulation amount relative to current pass
51                         float *tdstcol = tdst->getBuffer();
52                         for (y = 0; y < tsrc->getHeight() && (!breaked); ++y) {
53                                 for (x = 0; x < tsrc->getWidth(); ++x, tdstcol += 4) {
54                                         // first pass no offset, always same for every pass, exact copy,
55                                         // otherwise results in uneven brightness, only need once
56                                         if (n == 0) tsrc->read(c1, x, y); else c1[0] = c1[1] = c1[2] = 0;
57                                         tsrc->readBilinear(c2, x + vxp, y + vyp);
58                                         tsrc->readBilinear(c3, x + vxp * 2.0f, y + vyp * 2.0f);
59                                         tsrc->readBilinear(c4, x + vxp * 3.0f, y + vyp * 3.0f);
60                                         // modulate color to look vaguely similar to a color spectrum
61                                         c2[1] *= cmo;
62                                         c2[2] *= cmo;
63
64                                         c3[0] *= cmo;
65                                         c3[1] *= cmo;
66
67                                         c4[0] *= cmo;
68                                         c4[2] *= cmo;
69
70                                         tdstcol[0] = 0.5f * (tdstcol[0] + c1[0] + wt * (c2[0] + wt * (c3[0] + wt * c4[0])));
71                                         tdstcol[1] = 0.5f * (tdstcol[1] + c1[1] + wt * (c2[1] + wt * (c3[1] + wt * c4[1])));
72                                         tdstcol[2] = 0.5f * (tdstcol[2] + c1[2] + wt * (c2[2] + wt * (c3[2] + wt * c4[2])));
73                                         tdstcol[3] = 1.0f;
74                                 }
75                                 if (isBreaked()) {
76                                         breaked = true;
77                                 }
78                         }
79                         memcpy(tsrc->getBuffer(), tdst->getBuffer(), sizeof(float) * size4);
80                 }
81
82                 float *sourcebuffer = tsrc->getBuffer();
83                 float factor = 1.0f / (float)(6 - settings->iter);
84                 for (int i = 0; i < size4; i += 4) {
85                         madd_v3_v3fl(&data[i], &sourcebuffer[i], factor);
86                         data[i + 3] =  1.0f;
87                 }
88
89                 tdst->clear();
90                 memcpy(tsrc->getBuffer(), inputTile->getBuffer(), sizeof(float) * size4);
91                 nump++;
92         }
93
94         delete tsrc;
95         delete tdst;
96 }