1 // krazy:excludeall=copyright (email of Maxim is missing)
2 /*
3     This file is a part of the KDE project
4 
5     SPDX-FileCopyrightText: 2006 Zack Rusin <zack@kde.org>
6     SPDX-FileCopyrightText: 2006-2007, 2008 Fredrik Höglund <fredrik@kde.org>
7 
8     The stack blur algorithm was invented by Mario Klingemann <mario@quasimondo.com>
9 
10     This implementation is based on the version in Anti-Grain Geometry Version 2.4,
11     SPDX-FileCopyrightText: 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
12 
13     SPDX-License-Identifier: BSD-2-Clause
14 */
15 
16 #include "kpixmapmodifier.h"
17 
18 #include <QGuiApplication>
19 #include <QImage>
20 #include <QPainter>
21 
22 static const quint32 stackBlur8Mul[255] =
23 {
24     512,512,456,512,328,456,335,512,405,328,271,456,388,335,292,512,
25     454,405,364,328,298,271,496,456,420,388,360,335,312,292,273,512,
26     482,454,428,405,383,364,345,328,312,298,284,271,259,496,475,456,
27     437,420,404,388,374,360,347,335,323,312,302,292,282,273,265,512,
28     497,482,468,454,441,428,417,405,394,383,373,364,354,345,337,328,
29     320,312,305,298,291,284,278,271,265,259,507,496,485,475,465,456,
30     446,437,428,420,412,404,396,388,381,374,367,360,354,347,341,335,
31     329,323,318,312,307,302,297,292,287,282,278,273,269,265,261,512,
32     505,497,489,482,475,468,461,454,447,441,435,428,422,417,411,405,
33     399,394,389,383,378,373,368,364,359,354,350,345,341,337,332,328,
34     324,320,316,312,309,305,301,298,294,291,287,284,281,278,274,271,
35     268,265,262,259,257,507,501,496,491,485,480,475,470,465,460,456,
36     451,446,442,437,433,428,424,420,416,412,408,404,400,396,392,388,
37     385,381,377,374,370,367,363,360,357,354,350,347,344,341,338,335,
38     332,329,326,323,320,318,315,312,310,307,304,302,299,297,294,292,
39     289,287,285,282,280,278,275,273,271,269,267,265,263,261,259
40 };
41 
42 static const quint32 stackBlur8Shr[255] =
43 {
44     9, 11, 12, 13, 13, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17,
45     17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 19,
46     19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20,
47     20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 21,
48     21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
49     21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22,
50     22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22,
51     22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23,
52     23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,
53     23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,
54     23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,
55     23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
56     24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
57     24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
58     24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
59     24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24
60 };
61 
blurHorizontal(QImage & image,unsigned int * stack,int div,int radius)62 static void blurHorizontal(QImage& image, unsigned int* stack, int div, int radius)
63 {
64     int stackindex;
65     int stackstart;
66 
67     quint32 * const pixels = reinterpret_cast<quint32 *>(image.bits());
68     quint32 pixel;
69 
70     int w = image.width();
71     int h = image.height();
72     int wm = w - 1;
73 
74     unsigned int mulSum = stackBlur8Mul[radius];
75     unsigned int shrSum = stackBlur8Shr[radius];
76 
77     unsigned int sum, sumIn, sumOut;
78 
79     for (int y = 0; y < h; y++) {
80         sum    = 0;
81         sumIn  = 0;
82         sumOut = 0;
83 
84         const int yw = y * w;
85         pixel = pixels[yw];
86         for (int i = 0; i <= radius; i++) {
87             stack[i] = qAlpha(pixel);
88 
89             sum += stack[i] * (i + 1);
90             sumOut += stack[i];
91         }
92 
93         for (int i = 1; i <= radius; i++) {
94             pixel = pixels[yw + qMin(i, wm)];
95 
96             unsigned int* stackpix = &stack[i + radius];
97             *stackpix = qAlpha(pixel);
98 
99             sum    += *stackpix * (radius + 1 - i);
100             sumIn += *stackpix;
101         }
102 
103         stackindex = radius;
104         for (int x = 0, i = yw; x < w; x++) {
105             pixels[i++] = (((sum * mulSum) >> shrSum) << 24) & 0xff000000;
106 
107             sum -= sumOut;
108 
109             stackstart = stackindex + div - radius;
110             if (stackstart >= div) {
111                 stackstart -= div;
112             }
113 
114             unsigned int* stackpix = &stack[stackstart];
115 
116             sumOut -= *stackpix;
117 
118             pixel = pixels[yw + qMin(x + radius + 1, wm)];
119 
120             *stackpix = qAlpha(pixel);
121 
122             sumIn += *stackpix;
123             sum    += sumIn;
124 
125             if (++stackindex >= div) {
126                 stackindex = 0;
127             }
128 
129             stackpix = &stack[stackindex];
130 
131             sumOut += *stackpix;
132             sumIn  -= *stackpix;
133         }
134     }
135 }
136 
blurVertical(QImage & image,unsigned int * stack,int div,int radius)137 static void blurVertical(QImage& image, unsigned int* stack, int div, int radius)
138 {
139     int stackindex;
140     int stackstart;
141 
142     quint32 * const pixels = reinterpret_cast<quint32 *>(image.bits());
143     quint32 pixel;
144 
145     int w = image.width();
146     int h = image.height();
147     int hm = h - 1;
148 
149     int mul_sum = stackBlur8Mul[radius];
150     int shr_sum = stackBlur8Shr[radius];
151 
152     unsigned int sum, sumIn, sumOut;
153 
154     for (int x = 0; x < w; x++) {
155         sum    = 0;
156         sumIn  = 0;
157         sumOut = 0;
158 
159         pixel = pixels[x];
160         for (int i = 0; i <= radius; i++) {
161             stack[i] = qAlpha(pixel);
162 
163             sum += stack[i] * (i + 1);
164             sumOut += stack[i];
165         }
166 
167         for (int i = 1; i <= radius; i++) {
168             pixel = pixels[qMin(i, hm) * w + x];
169 
170             unsigned int* stackpix = &stack[i + radius];
171             *stackpix = qAlpha(pixel);
172 
173             sum    += *stackpix * (radius + 1 - i);
174             sumIn += *stackpix;
175         }
176 
177         stackindex = radius;
178         for (int y = 0, i = x; y < h; y++, i += w) {
179             pixels[i] = (((sum * mul_sum) >> shr_sum) << 24) & 0xff000000;
180 
181             sum -= sumOut;
182 
183             stackstart = stackindex + div - radius;
184             if (stackstart >= div)
185                 stackstart -= div;
186 
187             unsigned int* stackpix = &stack[stackstart];
188 
189             sumOut -= *stackpix;
190 
191             pixel = pixels[qMin(y + radius + 1, hm) * w + x];
192 
193             *stackpix = qAlpha(pixel);
194 
195             sumIn += *stackpix;
196             sum    += sumIn;
197 
198             if (++stackindex >= div) {
199                 stackindex = 0;
200             }
201 
202             stackpix = &stack[stackindex];
203 
204             sumOut += *stackpix;
205             sumIn  -= *stackpix;
206         }
207     }
208 }
209 
stackBlur(QImage & image,float radius)210 static void stackBlur(QImage& image, float radius)
211 {
212     radius = qRound(radius);
213 
214     int div = int(radius * 2) + 1;
215     unsigned int* stack  = new unsigned int[div];
216 
217     blurHorizontal(image, stack, div, radius);
218     blurVertical(image, stack, div, radius);
219 
220     delete [] stack;
221 }
222 
shadowBlur(QImage & image,float radius,const QColor & color)223 static void shadowBlur(QImage& image, float radius, const QColor& color)
224 {
225     if (radius < 0) {
226         return;
227     }
228 
229     if (radius > 0) {
230         stackBlur(image, radius);
231     }
232 
233     // Correct the color and opacity of the shadow
234     QPainter p(&image);
235     p.setCompositionMode(QPainter::CompositionMode_SourceIn);
236     p.fillRect(image.rect(), color);
237 }
238 
239 namespace {
240     /** Helper class for drawing frames for KPixmapModifier::applyFrame(). */
241     class TileSet
242     {
243     public:
244         enum { LeftMargin = 3, TopMargin = 2, RightMargin = 3, BottomMargin = 4 };
245 
246         enum Tile { TopLeftCorner = 0, TopSide, TopRightCorner, LeftSide,
247                     RightSide, BottomLeftCorner, BottomSide, BottomRightCorner,
248                     NumTiles };
249 
TileSet()250         TileSet()
251         {
252             QImage image(8 * 3, 8 * 3, QImage::Format_ARGB32_Premultiplied);
253 
254             QPainter p(&image);
255             p.setCompositionMode(QPainter::CompositionMode_Source);
256             p.fillRect(image.rect(), Qt::transparent);
257             p.fillRect(image.rect().adjusted(3, 3, -3, -3), Qt::black);
258             p.end();
259 
260             shadowBlur(image, 3, Qt::black);
261 
262             QPixmap pixmap = QPixmap::fromImage(image);
263             m_tiles[TopLeftCorner]     = pixmap.copy(0, 0, 8, 8);
264             m_tiles[TopSide]           = pixmap.copy(8, 0, 8, 8);
265             m_tiles[TopRightCorner]    = pixmap.copy(16, 0, 8, 8);
266             m_tiles[LeftSide]          = pixmap.copy(0, 8, 8, 8);
267             m_tiles[RightSide]         = pixmap.copy(16, 8, 8, 8);
268             m_tiles[BottomLeftCorner]  = pixmap.copy(0, 16, 8, 8);
269             m_tiles[BottomSide]        = pixmap.copy(8, 16, 8, 8);
270             m_tiles[BottomRightCorner] = pixmap.copy(16, 16, 8, 8);
271         }
272 
paint(QPainter * p,const QRect & r)273         void paint(QPainter* p, const QRect& r)
274         {
275             p->drawPixmap(r.topLeft(), m_tiles[TopLeftCorner]);
276             if (r.width() - 16 > 0) {
277                 p->drawTiledPixmap(r.x() + 8, r.y(), r.width() - 16, 8, m_tiles[TopSide]);
278             }
279             p->drawPixmap(r.right() - 8 + 1, r.y(), m_tiles[TopRightCorner]);
280             if (r.height() - 16 > 0) {
281                 p->drawTiledPixmap(r.x(), r.y() + 8, 8, r.height() - 16,  m_tiles[LeftSide]);
282                 p->drawTiledPixmap(r.right() - 8 + 1, r.y() + 8, 8, r.height() - 16, m_tiles[RightSide]);
283             }
284             p->drawPixmap(r.x(), r.bottom() - 8 + 1, m_tiles[BottomLeftCorner]);
285             if (r.width() - 16 > 0) {
286                 p->drawTiledPixmap(r.x() + 8, r.bottom() - 8 + 1, r.width() - 16, 8, m_tiles[BottomSide]);
287             }
288             p->drawPixmap(r.right() - 8 + 1, r.bottom() - 8 + 1, m_tiles[BottomRightCorner]);
289 
290             const QRect contentRect = r.adjusted(LeftMargin + 1, TopMargin + 1,
291                                                  -(RightMargin + 1), -(BottomMargin + 1));
292             p->fillRect(contentRect, Qt::transparent);
293         }
294 
295         QPixmap m_tiles[NumTiles];
296     };
297 }
298 
scale(QPixmap & pixmap,const QSize & scaledSize)299 void KPixmapModifier::scale(QPixmap& pixmap, const QSize& scaledSize)
300 {
301     if (scaledSize.isEmpty() || pixmap.isNull()) {
302         pixmap = QPixmap();
303         return;
304     }
305     qreal dpr = pixmap.devicePixelRatio();
306     pixmap = pixmap.scaled(scaledSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
307     pixmap.setDevicePixelRatio(dpr);
308 }
309 
applyFrame(QPixmap & icon,const QSize & scaledSize)310 void KPixmapModifier::applyFrame(QPixmap& icon, const QSize& scaledSize)
311 {
312     if (icon.isNull()) {
313         icon = QPixmap(scaledSize);
314         icon.fill(Qt::transparent);
315         return;
316     }
317 
318     static TileSet tileSet;
319     qreal dpr = qApp->devicePixelRatio();
320 
321     // Resize the icon to the maximum size minus the space required for the frame
322     const QSize size(scaledSize.width() - TileSet::LeftMargin - TileSet::RightMargin,
323                      scaledSize.height() - TileSet::TopMargin - TileSet::BottomMargin);
324     scale(icon, size * dpr);
325     icon.setDevicePixelRatio(dpr);
326 
327     QPixmap framedIcon(icon.size().width() + (TileSet::LeftMargin + TileSet::RightMargin) * dpr,
328                        icon.size().height() + (TileSet::TopMargin + TileSet::BottomMargin) * dpr);
329     framedIcon.setDevicePixelRatio(dpr);
330     framedIcon.fill(Qt::transparent);
331 
332     QPainter painter;
333     painter.begin(&framedIcon);
334     painter.setCompositionMode(QPainter::CompositionMode_Source);
335     tileSet.paint(&painter, QRect(QPoint(0,0), framedIcon.size() / dpr));
336     painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
337     painter.drawPixmap(TileSet::LeftMargin, TileSet::TopMargin, icon);
338 
339     icon = framedIcon;
340 }
341 
sizeInsideFrame(const QSize & frameSize)342 QSize KPixmapModifier::sizeInsideFrame(const QSize& frameSize)
343 {
344     return QSize(frameSize.width() - TileSet::LeftMargin - TileSet::RightMargin,
345                  frameSize.height() - TileSet::TopMargin - TileSet::BottomMargin);
346 }
347 
348