1 #include "blur.h"
2 
blur(QImage & result,const QRect & rect,int radius,bool alphaOnly)3 void olive::ui::blur(QImage& result, const QRect& rect, int radius, bool alphaOnly) {
4   int tab[] = { 14, 10, 8, 6, 5, 5, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2 };
5   int alpha = (radius < 1)  ? 16 : (radius > 17) ? 1 : tab[radius-1];
6 
7   int r1 = rect.top();
8   int r2 = rect.bottom();
9   int c1 = rect.left();
10   int c2 = rect.right();
11 
12   int bpl = result.bytesPerLine();
13   int rgba[4];
14   unsigned char* p;
15 
16   int i1 = 0;
17   int i2 = 3;
18 
19   if (alphaOnly)
20     i1 = i2 = (QSysInfo::ByteOrder == QSysInfo::BigEndian ? 0 : 3);
21 
22   for (int col = c1; col <= c2; col++) {
23     p = result.scanLine(r1) + col * 4;
24     for (int i = i1; i <= i2; i++)
25       rgba[i] = p[i] << 4;
26 
27     p += bpl;
28     for (int j = r1; j < r2; j++, p += bpl)
29       for (int i = i1; i <= i2; i++)
30         p[i] = (rgba[i] += ((p[i] << 4) - rgba[i]) * alpha / 16) >> 4;
31   }
32 
33   for (int row = r1; row <= r2; row++) {
34     p = result.scanLine(row) + c1 * 4;
35     for (int i = i1; i <= i2; i++)
36       rgba[i] = p[i] << 4;
37 
38     p += 4;
39     for (int j = c1; j < c2; j++, p += 4)
40       for (int i = i1; i <= i2; i++)
41         p[i] = (rgba[i] += ((p[i] << 4) - rgba[i]) * alpha / 16) >> 4;
42   }
43 
44   for (int col = c1; col <= c2; col++) {
45     p = result.scanLine(r2) + col * 4;
46     for (int i = i1; i <= i2; i++)
47       rgba[i] = p[i] << 4;
48 
49     p -= bpl;
50     for (int j = r1; j < r2; j++, p -= bpl)
51       for (int i = i1; i <= i2; i++)
52         p[i] = (rgba[i] += ((p[i] << 4) - rgba[i]) * alpha / 16) >> 4;
53   }
54 
55   for (int row = r1; row <= r2; row++) {
56     p = result.scanLine(row) + c2 * 4;
57     for (int i = i1; i <= i2; i++)
58       rgba[i] = p[i] << 4;
59 
60     p -= 4;
61     for (int j = c1; j < c2; j++, p -= 4)
62       for (int i = i1; i <= i2; i++)
63         p[i] = (rgba[i] += ((p[i] << 4) - rgba[i]) * alpha / 16) >> 4;
64   }
65 }
66