Changeset View
Changeset View
Standalone View
Standalone View
libbreezecommon/breezeboxshadowrenderer.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2018 Vlad Zagorodniy <vladzzag@gmail.com> | ||||
3 | * | ||||
4 | * The box blur implementation is based on AlphaBoxBlur from Firefox. | ||||
5 | * | ||||
6 | * This program is free software; you can redistribute it and/or | ||||
7 | * modify it under the terms of the GNU General Public License as | ||||
8 | * published by the Free Software Foundation; either version 2 of | ||||
9 | * the License or (at your option) version 3 or any later version | ||||
10 | * accepted by the membership of KDE e.V. (or its successor approved | ||||
11 | * by the membership of KDE e.V.), which shall act as a proxy | ||||
12 | * defined in Section 14 of version 3 of the license. | ||||
13 | * | ||||
14 | * This program is distributed in the hope that it will be useful, | ||||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
17 | * GNU General Public License for more details. | ||||
18 | * | ||||
19 | * You should have received a copy of the GNU General Public License | ||||
20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
21 | */ | ||||
22 | | ||||
23 | // own | ||||
24 | #include "breezeboxshadowrenderer.h" | ||||
25 | | ||||
26 | // Qt | ||||
27 | #include <QPainter> | ||||
28 | #include <QtMath> | ||||
29 | | ||||
30 | namespace Breeze | ||||
31 | { | ||||
32 | | ||||
33 | static inline int calculateBlurRadius(qreal stdDev) | ||||
34 | { | ||||
35 | // See https://www.w3.org/TR/SVG11/filters.html#feGaussianBlurElement | ||||
36 | const qreal gaussianScaleFactor = (3.0 * qSqrt(2.0 * M_PI) / 4.0) * 1.5; // * 0.9; | ||||
37 | return qMax(2, qFloor(stdDev * gaussianScaleFactor + 0.5)); | ||||
38 | } | ||||
39 | | ||||
40 | static inline qreal calculateBlurStdDev(int radius) | ||||
41 | { | ||||
42 | // The resulting shadow must approximate the image that would be generated by | ||||
43 | // applying to the shadow a Gaussian blur with a standard deviation equal to | ||||
44 | // half the blur radius. See https://www.w3.org/TR/css-backgrounds-3/#shadow-blur | ||||
45 | return radius / 2.0; | ||||
46 | } | ||||
47 | | ||||
48 | static inline QSize calculateBlurExtent(int radius) | ||||
49 | { | ||||
50 | const int blurRadius = calculateBlurRadius(calculateBlurStdDev(radius)); | ||||
51 | return QSize(blurRadius, blurRadius); | ||||
52 | } | ||||
53 | | ||||
54 | struct BoxLobes | ||||
55 | { | ||||
56 | int left; | ||||
57 | int right; | ||||
58 | }; | ||||
59 | | ||||
60 | static QVector<BoxLobes> computeLobes(int radius) | ||||
61 | { | ||||
62 | const int blurRadius = calculateBlurRadius(calculateBlurStdDev(radius)); | ||||
63 | const int z = blurRadius / 3; | ||||
64 | | ||||
65 | int major; | ||||
66 | int minor; | ||||
67 | int final; | ||||
68 | | ||||
69 | switch (blurRadius % 3) { | ||||
70 | case 0: | ||||
71 | major = z; | ||||
72 | minor = z; | ||||
73 | final = z; | ||||
74 | break; | ||||
75 | | ||||
76 | case 1: | ||||
77 | major = z + 1; | ||||
78 | minor = z; | ||||
79 | final = z; | ||||
80 | break; | ||||
81 | | ||||
82 | case 2: | ||||
83 | major = z + 1; | ||||
84 | minor = z; | ||||
85 | final = z + 1; | ||||
86 | break; | ||||
87 | | ||||
88 | default: | ||||
89 | Q_UNREACHABLE(); | ||||
90 | break; | ||||
91 | } | ||||
92 | | ||||
93 | Q_ASSERT(major + minor + final == blurRadius); | ||||
94 | | ||||
95 | return { | ||||
96 | {major, minor}, | ||||
97 | {minor, major}, | ||||
98 | {final, final} | ||||
99 | }; | ||||
100 | | ||||
101 | // QVector<BoxLobes> lobes; | ||||
102 | // lobes.reserve(3); | ||||
103 | | ||||
104 | // if (blurRadius % 2) { | ||||
105 | // const int lobeSize = (blurRadius - 1) / 2; | ||||
106 | // lobes.append({lobeSize, lobeSize}); | ||||
107 | // lobes.append({lobeSize, lobeSize}); | ||||
108 | // lobes.append({lobeSize, lobeSize}); | ||||
109 | // } else { | ||||
110 | // const int lobeSize = blurRadius / 2; | ||||
111 | // lobes.append({lobeSize, lobeSize - 1}); | ||||
112 | // lobes.append({lobeSize - 1, lobeSize}); | ||||
113 | // lobes.append({lobeSize, lobeSize}); | ||||
114 | // } | ||||
115 | | ||||
116 | // return lobes; | ||||
117 | } | ||||
118 | | ||||
119 | static inline void boxBlurRowAlpha(const uint8_t *src, uint8_t *dst, int width, int horizontalStride, | ||||
120 | int verticalStride, const BoxLobes &lobes, bool transposeInput, | ||||
121 | bool transposeOutput) | ||||
122 | { | ||||
123 | const int inputStep = transposeInput ? verticalStride : horizontalStride; | ||||
124 | const int outputStep = transposeOutput ? verticalStride : horizontalStride; | ||||
125 | | ||||
126 | const int boxSize = lobes.left + 1 + lobes.right; | ||||
127 | const int reciprocal = (1 << 24) / boxSize; | ||||
128 | | ||||
129 | uint32_t alphaSum = (boxSize + 1) / 2; | ||||
130 | | ||||
131 | const uint8_t *left = src; | ||||
132 | const uint8_t *right = src; | ||||
133 | uint8_t *out = dst; | ||||
134 | | ||||
135 | const uint8_t firstValue = src[0]; | ||||
136 | const uint8_t lastValue = src[(width - 1) * inputStep]; | ||||
137 | | ||||
138 | alphaSum += firstValue * lobes.left; | ||||
139 | | ||||
140 | const uint8_t *initEnd = src + (boxSize - lobes.left) * inputStep; | ||||
141 | while (right < initEnd) { | ||||
142 | alphaSum += *right; | ||||
143 | right += inputStep; | ||||
144 | } | ||||
145 | | ||||
146 | const uint8_t *leftEnd = src + boxSize * inputStep; | ||||
147 | while (right < leftEnd) { | ||||
148 | *out = (alphaSum * reciprocal) >> 24; | ||||
149 | alphaSum += *right - firstValue; | ||||
150 | right += inputStep; | ||||
151 | out += outputStep; | ||||
152 | } | ||||
153 | | ||||
154 | const uint8_t *centerEnd = src + width * inputStep; | ||||
155 | while (right < centerEnd) { | ||||
156 | *out = (alphaSum * reciprocal) >> 24; | ||||
157 | alphaSum += *right - *left; | ||||
158 | left += inputStep; | ||||
159 | right += inputStep; | ||||
160 | out += outputStep; | ||||
161 | } | ||||
162 | | ||||
163 | const uint8_t *rightEnd = dst + width * outputStep; | ||||
164 | while (out < rightEnd) { | ||||
165 | *out = (alphaSum * reciprocal) >> 24; | ||||
166 | alphaSum += lastValue - *left; | ||||
167 | left += inputStep; | ||||
168 | out += outputStep; | ||||
169 | } | ||||
170 | } | ||||
171 | | ||||
172 | /** | ||||
173 | * Blur the alpha channel of a given image | ||||
174 | * | ||||
175 | * @param image The input image | ||||
176 | * @param radius The blur radius | ||||
177 | * @param rect Specifies what part of the image to blur. If nothing is provided, then | ||||
178 | * the whole alpha channel of the input image will be blurred | ||||
179 | **/ | ||||
180 | static inline void boxBlurAlpha(QImage &image, int radius, const QRect &rect = {}) | ||||
181 | { | ||||
182 | if (radius < 2) { | ||||
183 | return; | ||||
184 | } | ||||
185 | | ||||
186 | const QVector<BoxLobes> lobes = computeLobes(radius); | ||||
187 | | ||||
188 | const QRect blurRect = rect.isNull() ? image.rect() : rect; | ||||
189 | | ||||
190 | const int alphaOffset = QSysInfo::ByteOrder == QSysInfo::BigEndian ? 0 : 3; | ||||
191 | const int width = blurRect.width(); | ||||
192 | const int height = blurRect.height(); | ||||
193 | const int rowStride = image.bytesPerLine(); | ||||
194 | const int pixelStride = image.depth() >> 3; | ||||
195 | | ||||
196 | const int bufferStride = qMax(width, height) * pixelStride; | ||||
197 | QScopedPointer<uint8_t, QScopedPointerArrayDeleter<uint8_t> > buf(new uint8_t[2 * bufferStride]); | ||||
198 | uint8_t *buf1 = buf.data(); | ||||
199 | uint8_t *buf2 = buf1 + bufferStride; | ||||
200 | | ||||
201 | // Blur the image in horizontal direction. | ||||
202 | for (int i = 0; i < height; ++i) { | ||||
203 | uint8_t *row = image.scanLine(blurRect.y() + i) + blurRect.x() * pixelStride + alphaOffset; | ||||
204 | boxBlurRowAlpha(row, buf1, width, pixelStride, rowStride, lobes[0], false, false); | ||||
205 | boxBlurRowAlpha(buf1, buf2, width, pixelStride, rowStride, lobes[1], false, false); | ||||
206 | boxBlurRowAlpha(buf2, row, width, pixelStride, rowStride, lobes[2], false, false); | ||||
207 | } | ||||
208 | | ||||
209 | // Blur the image in vertical direction. | ||||
210 | for (int i = 0; i < width; ++i) { | ||||
211 | uint8_t *column = image.scanLine(blurRect.y()) + (blurRect.x() + i) * pixelStride + alphaOffset; | ||||
212 | boxBlurRowAlpha(column, buf1, height, pixelStride, rowStride, lobes[0], true, false); | ||||
213 | boxBlurRowAlpha(buf1, buf2, height, pixelStride, rowStride, lobes[1], false, false); | ||||
214 | boxBlurRowAlpha(buf2, column, height, pixelStride, rowStride, lobes[2], false, true); | ||||
215 | } | ||||
216 | } | ||||
217 | | ||||
218 | static inline void mirrorTopLeftQuadrant(QImage &image) | ||||
219 | { | ||||
220 | const int width = image.width(); | ||||
221 | const int height = image.height(); | ||||
222 | | ||||
223 | const int centerX = qCeil(width * 0.5); | ||||
224 | const int centerY = qCeil(height * 0.5); | ||||
225 | | ||||
226 | const int alphaOffset = QSysInfo::ByteOrder == QSysInfo::BigEndian ? 0 : 3; | ||||
227 | const int stride = image.depth() >> 3; | ||||
228 | | ||||
229 | for (int y = 0; y < centerY; ++y) { | ||||
230 | uint8_t *in = image.scanLine(y) + alphaOffset; | ||||
231 | uint8_t *out = in + (width - 1) * stride; | ||||
232 | | ||||
233 | for (int x = 0; x < centerX; ++x, in += stride, out -= stride) { | ||||
234 | *out = *in; | ||||
235 | } | ||||
236 | } | ||||
237 | | ||||
238 | for (int y = 0; y < centerY; ++y) { | ||||
239 | const uint8_t *in = image.scanLine(y) + alphaOffset; | ||||
240 | uint8_t *out = image.scanLine(width - y - 1) + alphaOffset; | ||||
241 | | ||||
242 | for (int x = 0; x < width; ++x, in += stride, out += stride) { | ||||
243 | *out = *in; | ||||
244 | } | ||||
245 | } | ||||
246 | } | ||||
247 | | ||||
248 | static void renderShadow(QPainter *painter, const QRect &rect, qreal borderRadius, const QPoint &offset, int radius, const QColor &color) | ||||
249 | { | ||||
250 | const QSize inflation = calculateBlurExtent(radius); | ||||
251 | const QSize size = rect.size() + 2 * inflation; | ||||
252 | | ||||
253 | #if BREEZE_COMMON_USE_KDE4 | ||||
254 | const qreal dpr = 1.0; | ||||
255 | #else | ||||
256 | const qreal dpr = painter->device()->devicePixelRatioF(); | ||||
257 | #endif | ||||
258 | | ||||
259 | QImage shadow(size * dpr, QImage::Format_ARGB32_Premultiplied); | ||||
260 | #if !BREEZE_COMMON_USE_KDE4 | ||||
261 | shadow.setDevicePixelRatio(dpr); | ||||
262 | #endif | ||||
263 | shadow.fill(Qt::transparent); | ||||
264 | | ||||
265 | QPainter shadowPainter; | ||||
266 | | ||||
267 | // Draw the box. | ||||
268 | QRect boxRect(QPoint(0, 0), rect.size()); | ||||
269 | boxRect.moveCenter(shadow.rect().center()); | ||||
270 | | ||||
271 | shadowPainter.begin(&shadow); | ||||
272 | shadowPainter.setRenderHint(QPainter::Antialiasing); | ||||
273 | shadowPainter.setPen(Qt::NoPen); | ||||
274 | shadowPainter.setBrush(Qt::black); | ||||
275 | if (borderRadius > 0.0) { | ||||
276 | shadowPainter.drawRoundedRect(boxRect, borderRadius, borderRadius); | ||||
277 | } else { | ||||
278 | shadowPainter.drawRect(boxRect); | ||||
279 | } | ||||
280 | shadowPainter.end(); | ||||
281 | | ||||
282 | const QRect blurRect(0, 0, qCeil(shadow.width() * 0.5), qCeil(shadow.height() * 0.5)); | ||||
283 | const int scaledRadius = qRound(radius * dpr); | ||||
284 | boxBlurAlpha(shadow, scaledRadius, blurRect); | ||||
285 | mirrorTopLeftQuadrant(shadow); | ||||
286 | | ||||
287 | // Give the shadow a tint of the desired color. | ||||
288 | shadowPainter.begin(&shadow); | ||||
289 | shadowPainter.setCompositionMode(QPainter::CompositionMode_SourceIn); | ||||
290 | shadowPainter.fillRect(shadow.rect(), color); | ||||
291 | shadowPainter.end(); | ||||
292 | | ||||
293 | // Actually, present the shadow. | ||||
294 | QRect shadowRect = shadow.rect(); | ||||
295 | shadowRect.setSize(shadowRect.size() / dpr); | ||||
296 | shadowRect.moveCenter(rect.center() + offset); | ||||
297 | painter->drawImage(shadowRect, shadow); | ||||
298 | } | ||||
299 | | ||||
300 | void BoxShadowRenderer::setBoxSize(const QSize &size) | ||||
301 | { | ||||
302 | m_boxSize = size; | ||||
303 | } | ||||
304 | | ||||
305 | void BoxShadowRenderer::setBorderRadius(qreal radius) | ||||
306 | { | ||||
307 | m_borderRadius = radius; | ||||
308 | } | ||||
309 | | ||||
310 | void BoxShadowRenderer::setDevicePixelRatio(qreal dpr) | ||||
311 | { | ||||
312 | m_dpr = dpr; | ||||
313 | } | ||||
314 | | ||||
315 | void BoxShadowRenderer::addShadow(const QPoint &offset, int radius, const QColor &color) | ||||
316 | { | ||||
317 | Shadow shadow = {}; | ||||
318 | shadow.offset = offset; | ||||
319 | shadow.radius = radius; | ||||
320 | shadow.color = color; | ||||
321 | m_shadows.append(shadow); | ||||
322 | } | ||||
323 | | ||||
324 | QImage BoxShadowRenderer::render() const | ||||
325 | { | ||||
326 | if (m_shadows.isEmpty()) { | ||||
327 | return {}; | ||||
328 | } | ||||
329 | | ||||
330 | QSize canvasSize; | ||||
331 | for (const Shadow &shadow : qAsConst(m_shadows)) { | ||||
332 | canvasSize = canvasSize.expandedTo( | ||||
333 | minimumRequiredCanvasSize(m_boxSize, shadow.radius, shadow.offset)); | ||||
334 | } | ||||
335 | | ||||
336 | QImage canvas(canvasSize * m_dpr, QImage::Format_ARGB32_Premultiplied); | ||||
337 | #if !BREEZE_COMMON_USE_KDE4 | ||||
338 | canvas.setDevicePixelRatio(m_dpr); | ||||
339 | #endif | ||||
340 | canvas.fill(Qt::transparent); | ||||
341 | | ||||
342 | QRect boxRect(QPoint(0, 0), m_boxSize); | ||||
343 | boxRect.moveCenter(QRect(QPoint(0, 0), canvasSize).center()); | ||||
344 | | ||||
345 | QPainter painter(&canvas); | ||||
346 | for (const Shadow &shadow : qAsConst(m_shadows)) { | ||||
347 | renderShadow(&painter, boxRect, m_borderRadius, shadow.offset, shadow.radius, shadow.color); | ||||
348 | } | ||||
349 | painter.end(); | ||||
350 | | ||||
351 | return canvas; | ||||
352 | } | ||||
353 | | ||||
354 | QSize BoxShadowRenderer::minimumRequiredBoxSize(int radius) | ||||
355 | { | ||||
356 | const QSize blurExtent = calculateBlurExtent(radius); | ||||
357 | return 2 * blurExtent + QSize(1, 1); | ||||
358 | } | ||||
359 | | ||||
360 | QSize BoxShadowRenderer::minimumRequiredCanvasSize(const QSize &boxSize, int radius, const QPoint &offset) | ||||
361 | { | ||||
362 | return boxSize + 2 * calculateBlurExtent(radius) + QSize(qAbs(offset.x()), qAbs(offset.y())); | ||||
363 | } | ||||
364 | | ||||
365 | } // namespace Breeze |