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 | * This program is free software; you can redistribute it and/or modify | ||||
5 | * it under the terms of the GNU General Public License as published by | ||||
6 | * the Free Software Foundation; either version 2 of the License, or | ||||
7 | * (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 | ||||
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||||
17 | */ | ||||
18 | | ||||
19 | // own | ||||
20 | #include "breezeboxshadowrenderer.h" | ||||
21 | | ||||
22 | // auto-generated | ||||
23 | #include "config-breezecommon.h" | ||||
24 | | ||||
25 | // Qt | ||||
26 | #include <QPainter> | ||||
27 | #include <QtMath> | ||||
28 | | ||||
29 | namespace Breeze | ||||
30 | { | ||||
31 | | ||||
32 | static inline int calculateBlurRadius(qreal stdDev) | ||||
33 | { | ||||
34 | // See https://www.w3.org/TR/SVG11/filters.html#feGaussianBlurElement | ||||
35 | const qreal gaussianScaleFactor = (3.0 * qSqrt(2.0 * M_PI) / 4.0) * 1.5; | ||||
36 | return qMax(2, qFloor(stdDev * gaussianScaleFactor + 0.5)); | ||||
37 | } | ||||
38 | | ||||
39 | static inline qreal calculateBlurStdDev(int radius) | ||||
40 | { | ||||
41 | // The resulting shadow must approximate the image that would be generated by | ||||
42 | // applying to the shadow a Gaussian blur with a standard deviation equal to | ||||
43 | // half the blur radius. See https://www.w3.org/TR/css-backgrounds-3/#shadow-blur | ||||
44 | return radius / 2.0; | ||||
45 | } | ||||
46 | | ||||
47 | static inline QSize calculateBlurExtent(int radius) | ||||
48 | { | ||||
49 | const int blurRadius = calculateBlurRadius(calculateBlurStdDev(radius)); | ||||
50 | return QSize(blurRadius, blurRadius); | ||||
51 | } | ||||
52 | | ||||
53 | struct BoxLobes | ||||
54 | { | ||||
55 | int left; ///< how many pixels sample to the left | ||||
56 | int right; ///< how many pixels sample to the right | ||||
57 | }; | ||||
58 | | ||||
59 | /** | ||||
60 | * Compute box filter parameters. | ||||
61 | * | ||||
62 | * @param radius The blur radius. | ||||
63 | * @returns Parameters for three box filters. | ||||
64 | **/ | ||||
65 | static QVector<BoxLobes> computeLobes(int radius) | ||||
66 | { | ||||
67 | const int blurRadius = calculateBlurRadius(calculateBlurStdDev(radius)); | ||||
68 | const int z = blurRadius / 3; | ||||
69 | | ||||
70 | int major; | ||||
71 | int minor; | ||||
72 | int final; | ||||
73 | | ||||
74 | switch (blurRadius % 3) { | ||||
75 | case 0: | ||||
76 | major = z; | ||||
77 | minor = z; | ||||
78 | final = z; | ||||
79 | break; | ||||
80 | | ||||
81 | case 1: | ||||
82 | major = z + 1; | ||||
83 | minor = z; | ||||
84 | final = z; | ||||
85 | break; | ||||
86 | | ||||
87 | case 2: | ||||
88 | major = z + 1; | ||||
89 | minor = z; | ||||
90 | final = z + 1; | ||||
91 | break; | ||||
92 | | ||||
93 | default: | ||||
94 | Q_UNREACHABLE(); | ||||
95 | break; | ||||
96 | } | ||||
97 | | ||||
98 | Q_ASSERT(major + minor + final == blurRadius); | ||||
99 | | ||||
100 | return { | ||||
101 | {major, minor}, | ||||
102 | {minor, major}, | ||||
103 | {final, final} | ||||
104 | }; | ||||
105 | } | ||||
106 | | ||||
107 | /** | ||||
108 | * Process a row with a box filter. | ||||
109 | * | ||||
110 | * @param src The start of the row. | ||||
111 | * @param dst The destination. | ||||
112 | * @param width The width of the row, in pixels. | ||||
113 | * @param horizontalStride The number of bytes from one alpha value to the | ||||
114 | * next alpha value. | ||||
115 | * @param verticalStride The number of bytes from one row to the next row. | ||||
116 | * @param lobes Params of the box filter. | ||||
117 | * @param transposeInput Whether the input is transposed. | ||||
118 | * @param transposeOutput Whether the output should be transposed. | ||||
119 | **/ | ||||
120 | static inline void boxBlurRowAlpha(const uint8_t *src, uint8_t *dst, int width, int horizontalStride, | ||||
121 | int verticalStride, const BoxLobes &lobes, bool transposeInput, | ||||
122 | bool transposeOutput) | ||||
123 | { | ||||
124 | const int inputStep = transposeInput ? verticalStride : horizontalStride; | ||||
125 | const int outputStep = transposeOutput ? verticalStride : horizontalStride; | ||||
126 | | ||||
127 | const int boxSize = lobes.left + 1 + lobes.right; | ||||
128 | const int reciprocal = (1 << 24) / boxSize; | ||||
129 | | ||||
130 | uint32_t alphaSum = (boxSize + 1) / 2; | ||||
131 | | ||||
132 | const uint8_t *left = src; | ||||
133 | const uint8_t *right = src; | ||||
134 | uint8_t *out = dst; | ||||
135 | | ||||
136 | const uint8_t firstValue = src[0]; | ||||
137 | const uint8_t lastValue = src[(width - 1) * inputStep]; | ||||
138 | | ||||
139 | alphaSum += firstValue * lobes.left; | ||||
140 | | ||||
141 | const uint8_t *initEnd = src + (boxSize - lobes.left) * inputStep; | ||||
142 | while (right < initEnd) { | ||||
143 | alphaSum += *right; | ||||
144 | right += inputStep; | ||||
145 | } | ||||
146 | | ||||
147 | const uint8_t *leftEnd = src + boxSize * inputStep; | ||||
148 | while (right < leftEnd) { | ||||
149 | *out = (alphaSum * reciprocal) >> 24; | ||||
150 | alphaSum += *right - firstValue; | ||||
151 | right += inputStep; | ||||
152 | out += outputStep; | ||||
153 | } | ||||
154 | | ||||
155 | const uint8_t *centerEnd = src + width * inputStep; | ||||
156 | while (right < centerEnd) { | ||||
157 | *out = (alphaSum * reciprocal) >> 24; | ||||
158 | alphaSum += *right - *left; | ||||
159 | left += inputStep; | ||||
160 | right += inputStep; | ||||
161 | out += outputStep; | ||||
162 | } | ||||
163 | | ||||
164 | const uint8_t *rightEnd = dst + width * outputStep; | ||||
165 | while (out < rightEnd) { | ||||
166 | *out = (alphaSum * reciprocal) >> 24; | ||||
167 | alphaSum += lastValue - *left; | ||||
168 | left += inputStep; | ||||
169 | out += outputStep; | ||||
170 | } | ||||
171 | } | ||||
172 | | ||||
173 | /** | ||||
174 | * Blur the alpha channel of a given image. | ||||
175 | * | ||||
176 | * @param image The input image. | ||||
177 | * @param radius The blur radius. | ||||
178 | * @param rect Specifies what part of the image to blur. If nothing is provided, then | ||||
179 | * the whole alpha channel of the input image will be blurred. | ||||
180 | **/ | ||||
181 | static inline void boxBlurAlpha(QImage &image, int radius, const QRect &rect = {}) | ||||
182 | { | ||||
183 | if (radius < 2) { | ||||
184 | return; | ||||
185 | } | ||||
186 | | ||||
187 | const QVector<BoxLobes> lobes = computeLobes(radius); | ||||
188 | | ||||
189 | const QRect blurRect = rect.isNull() ? image.rect() : rect; | ||||
190 | | ||||
191 | const int alphaOffset = QSysInfo::ByteOrder == QSysInfo::BigEndian ? 0 : 3; | ||||
192 | const int width = blurRect.width(); | ||||
193 | const int height = blurRect.height(); | ||||
194 | const int rowStride = image.bytesPerLine(); | ||||
195 | const int pixelStride = image.depth() >> 3; | ||||
196 | | ||||
197 | const int bufferStride = qMax(width, height) * pixelStride; | ||||
198 | QScopedPointer<uint8_t, QScopedPointerArrayDeleter<uint8_t> > buf(new uint8_t[2 * bufferStride]); | ||||
199 | uint8_t *buf1 = buf.data(); | ||||
200 | uint8_t *buf2 = buf1 + bufferStride; | ||||
201 | | ||||
202 | // Blur the image in horizontal direction. | ||||
203 | for (int i = 0; i < height; ++i) { | ||||
204 | uint8_t *row = image.scanLine(blurRect.y() + i) + blurRect.x() * pixelStride + alphaOffset; | ||||
205 | boxBlurRowAlpha(row, buf1, width, pixelStride, rowStride, lobes[0], false, false); | ||||
206 | boxBlurRowAlpha(buf1, buf2, width, pixelStride, rowStride, lobes[1], false, false); | ||||
207 | boxBlurRowAlpha(buf2, row, width, pixelStride, rowStride, lobes[2], false, false); | ||||
208 | } | ||||
209 | | ||||
210 | // Blur the image in vertical direction. | ||||
211 | for (int i = 0; i < width; ++i) { | ||||
212 | uint8_t *column = image.scanLine(blurRect.y()) + (blurRect.x() + i) * pixelStride + alphaOffset; | ||||
213 | boxBlurRowAlpha(column, buf1, height, pixelStride, rowStride, lobes[0], true, false); | ||||
214 | boxBlurRowAlpha(buf1, buf2, height, pixelStride, rowStride, lobes[1], false, false); | ||||
215 | boxBlurRowAlpha(buf2, column, height, pixelStride, rowStride, lobes[2], false, true); | ||||
216 | } | ||||
217 | } | ||||
218 | | ||||
219 | static inline void mirrorTopLeftQuadrant(QImage &image) | ||||
220 | { | ||||
221 | const int width = image.width(); | ||||
222 | const int height = image.height(); | ||||
223 | | ||||
224 | const int centerX = qCeil(width * 0.5); | ||||
225 | const int centerY = qCeil(height * 0.5); | ||||
226 | | ||||
227 | const int alphaOffset = QSysInfo::ByteOrder == QSysInfo::BigEndian ? 0 : 3; | ||||
228 | const int stride = image.depth() >> 3; | ||||
229 | | ||||
230 | for (int y = 0; y < centerY; ++y) { | ||||
231 | uint8_t *in = image.scanLine(y) + alphaOffset; | ||||
232 | uint8_t *out = in + (width - 1) * stride; | ||||
233 | | ||||
234 | for (int x = 0; x < centerX; ++x, in += stride, out -= stride) { | ||||
235 | *out = *in; | ||||
236 | } | ||||
237 | } | ||||
238 | | ||||
239 | for (int y = 0; y < centerY; ++y) { | ||||
240 | const uint8_t *in = image.scanLine(y) + alphaOffset; | ||||
241 | uint8_t *out = image.scanLine(width - y - 1) + alphaOffset; | ||||
242 | | ||||
243 | for (int x = 0; x < width; ++x, in += stride, out += stride) { | ||||
244 | *out = *in; | ||||
245 | } | ||||
246 | } | ||||
247 | } | ||||
248 | | ||||
249 | static void renderShadow(QPainter *painter, const QRect &rect, qreal borderRadius, const QPoint &offset, int radius, const QColor &color) | ||||
250 | { | ||||
251 | const QSize inflation = calculateBlurExtent(radius); | ||||
252 | const QSize size = rect.size() + 2 * inflation; | ||||
253 | | ||||
254 | #if BREEZE_COMMON_USE_KDE4 | ||||
255 | const qreal dpr = 1.0; | ||||
256 | #else | ||||
257 | const qreal dpr = painter->device()->devicePixelRatioF(); | ||||
258 | #endif | ||||
259 | | ||||
260 | QImage shadow(size * dpr, QImage::Format_ARGB32_Premultiplied); | ||||
261 | #if !BREEZE_COMMON_USE_KDE4 | ||||
262 | shadow.setDevicePixelRatio(dpr); | ||||
263 | #endif | ||||
264 | shadow.fill(Qt::transparent); | ||||
265 | | ||||
266 | QRect boxRect(QPoint(0, 0), rect.size()); | ||||
267 | boxRect.moveCenter(shadow.rect().center()); | ||||
268 | | ||||
269 | const qreal xRadius = 2.0 * borderRadius / boxRect.width(); | ||||
270 | const qreal yRadius = 2.0 * borderRadius / boxRect.height(); | ||||
271 | | ||||
272 | QPainter shadowPainter; | ||||
273 | shadowPainter.begin(&shadow); | ||||
274 | shadowPainter.setRenderHint(QPainter::Antialiasing); | ||||
275 | shadowPainter.setPen(Qt::NoPen); | ||||
276 | shadowPainter.setBrush(Qt::black); | ||||
277 | shadowPainter.drawRoundedRect(boxRect, xRadius, yRadius); | ||||
278 | shadowPainter.end(); | ||||
279 | | ||||
280 | // Because the shadow texture is symmetrical, that's enough to blur | ||||
281 | // only the top-left quadrant and then mirror it. | ||||
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 | calculateMinimumShadowTextureSize(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::calculateMinimumBoxSize(int radius) | ||||
355 | { | ||||
356 | const QSize blurExtent = calculateBlurExtent(radius); | ||||
357 | return 2 * blurExtent + QSize(1, 1); | ||||
358 | } | ||||
359 | | ||||
360 | QSize BoxShadowRenderer::calculateMinimumShadowTextureSize(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 |