VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/globals/UIImageTools.cpp@ 100347

Last change on this file since 100347 was 98103, checked in by vboxsync, 2 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.8 KB
Line 
1/* $Id: UIImageTools.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * VBox Qt GUI - Implementation of utility classes and functions for image manipulation.
4 */
5
6/*
7 * Copyright (C) 2010-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28/* Qt includes: */
29#include <QPainter>
30#include <QPainterPath>
31#include <QPainterPathStroker>
32
33/* GUI include */
34#include "UIDesktopWidgetWatchdog.h"
35#include "UIImageTools.h"
36
37/* External includes: */
38#include <math.h>
39
40
41QImage UIImageTools::toGray(const QImage &image)
42{
43 QImage result = image.convertToFormat(QImage::Format_ARGB32);
44 for (int y = 0; y < result.height(); ++y)
45 {
46 QRgb *pScanLine = (QRgb*)result.scanLine(y);
47 for (int x = 0; x < result.width(); ++x)
48 {
49 const int g = qGray(pScanLine[x]);
50 pScanLine[x] = qRgba(g, g, g, qAlpha(pScanLine[x]));
51 }
52 }
53 return result;
54}
55
56void UIImageTools::dimImage(QImage &image)
57{
58 for (int y = 0; y < image.height(); ++y)
59 {
60 QRgb *pScanLine = (QRgb*)image.scanLine(y);
61 if (y % 2)
62 {
63 if (image.depth() == 32)
64 {
65 for (int x = 0; x < image.width(); ++x)
66 {
67 const int iGray = qGray(pScanLine[x]) / 2;
68 pScanLine[x] = qRgba(iGray, iGray, iGray, qAlpha(pScanLine[x]));
69 }
70 }
71 else
72 ::memset(pScanLine, 0, image.bytesPerLine());
73 }
74 else
75 {
76 if (image.depth() == 32)
77 {
78 for (int x = 0; x < image.width(); ++x)
79 {
80 const int iGray = (2 * qGray(pScanLine[x])) / 3;
81 pScanLine[x] = qRgba(iGray, iGray, iGray, qAlpha(pScanLine[x]));
82 }
83 }
84 }
85 }
86}
87
88void UIImageTools::blurImage(const QImage &source, QImage &destination, int iRadius)
89{
90 /* Blur in two steps, first horizontal and then vertical: */
91 QImage tmpImage(source.size(), QImage::Format_ARGB32);
92 blurImageHorizontal(source, tmpImage, iRadius);
93 blurImageVertical(tmpImage, destination, iRadius);
94}
95
96void UIImageTools::blurImageHorizontal(const QImage &source, QImage &destination, int iRadius)
97{
98 QSize s = source.size();
99 for (int y = 0; y < s.height(); ++y)
100 {
101 int rt = 0;
102 int gt = 0;
103 int bt = 0;
104 int at = 0;
105
106 /* In the horizontal case we can just use the scanline, which is
107 * much faster than accessing every pixel with the QImage::pixel
108 * method. Unfortunately this doesn't work in the vertical case. */
109 QRgb *ssl = (QRgb*)source.scanLine(y);
110 QRgb *dsl = (QRgb*)destination.scanLine(y);
111 /* First process the horizontal zero line at once: */
112 int b = iRadius + 1;
113 for (int x1 = 0; x1 <= iRadius; ++x1)
114 {
115 QRgb rgba = ssl[x1];
116 rt += qRed(rgba);
117 gt += qGreen(rgba);
118 bt += qBlue(rgba);
119 at += qAlpha(rgba);
120 }
121 /* Set the new weighted pixel: */
122 dsl[0] = qRgba(rt / b, gt / b, bt / b, at / b);
123
124 /* Now process the rest */
125 for (int x = 1; x < s.width(); ++x)
126 {
127 /* Subtract the pixel which fall out of our blur matrix: */
128 int x1 = x - iRadius - 1;
129 if (x1 >= 0)
130 {
131 /* Adjust the weight (necessary for the border case): */
132 --b;
133 QRgb rgba = ssl[x1];
134 rt -= qRed(rgba);
135 gt -= qGreen(rgba);
136 bt -= qBlue(rgba);
137 at -= qAlpha(rgba);
138 }
139
140 /* Add the pixel which get into our blur matrix: */
141 int x2 = x + iRadius;
142 if (x2 < s.width())
143 {
144 /* Adjust the weight (necessary for the border case): */
145 ++b;
146 QRgb rgba = ssl[x2];
147 rt += qRed(rgba);
148 gt += qGreen(rgba);
149 bt += qBlue(rgba);
150 at += qAlpha(rgba);
151 }
152 /* Set the new weighted pixel: */
153 dsl[x] = qRgba(rt / b, gt / b, bt / b, at / b);
154 }
155 }
156}
157
158void UIImageTools::blurImageVertical(const QImage &source, QImage &destination, int iRadius)
159{
160 QSize s = source.size();
161 destination = QImage(s, source.format());
162 for (int x = 0; x < s.width(); ++x)
163 {
164 int rt = 0;
165 int gt = 0;
166 int bt = 0;
167 int at = 0;
168
169 /* First process the vertical zero line at once: */
170 int b = iRadius + 1;
171 for (int y1 = 0; y1 <= iRadius; ++y1)
172 {
173 QRgb rgba = source.pixel(x, y1);
174 rt += qRed(rgba);
175 gt += qGreen(rgba);
176 bt += qBlue(rgba);
177 at += qAlpha(rgba);
178 }
179 /* Set the new weighted pixel: */
180 destination.setPixel(x, 0, qRgba(rt / b, gt / b, bt / b, at / b));
181
182 /* Now process the rest: */
183 for (int y = 1; y < s.height(); ++y)
184 {
185 /* Subtract the pixel which fall out of our blur matrix: */
186 int y1 = y - iRadius - 1;
187 if (y1 >= 0)
188 {
189 --b; /* Adjust the weight (necessary for the border case): */
190 QRgb rgba = source.pixel(x, y1);
191 rt -= qRed(rgba);
192 gt -= qGreen(rgba);
193 bt -= qBlue(rgba);
194 at -= qAlpha(rgba);
195 }
196
197 /* Add the pixel which get into our blur matrix: */
198 int y2 = y + iRadius;
199 if (y2 < s.height())
200 {
201 ++b; /* Adjust the weight (necessary for the border case): */
202 QRgb rgba = source.pixel(x, y2);
203 rt += qRed(rgba);
204 gt += qGreen(rgba);
205 bt += qBlue(rgba);
206 at += qAlpha(rgba);
207 }
208 /* Set the new weighted pixel: */
209 destination.setPixel(x, y, qRgba(rt / b, gt / b, bt / b, at / b));
210 }
211 }
212}
213
214static QImage betaLabelImage(QSize size, QWidget *pHint)
215{
216 /* Calculate device pixel ratio: */
217 const double dDpr = pHint ? UIDesktopWidgetWatchdog::devicePixelRatio(pHint) : UIDesktopWidgetWatchdog::devicePixelRatio(-1);
218 if (dDpr > 1.0)
219 size *= dDpr;
220
221 /* Beta label: */
222 QColor bgc(246, 179, 0);
223 QImage i(size, QImage::Format_ARGB32);
224 i.fill(Qt::transparent);
225 QPainter p(&i);
226 p.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
227 p.setPen(Qt::NoPen);
228
229 /* Background: */
230 p.setBrush(bgc);
231 p.drawRect(0, 0, size.width(), size.height());
232
233 /* The black stripes: */
234 p.setPen(QPen(QColor(70, 70, 70), 5));
235 float c = ((float)size.width() / size.height()) + 1;
236 float g = (size.width() / (c - 1));
237 for (int i = 0; i < c; ++i)
238 p.drawLine((int)(-g / 2 + g * i), size.height(), (int)(-g / 2 + g * (i + 1)), 0);
239
240 /* The text: */
241 QFont f = p.font();
242 if (dDpr > 1.0)
243 f.setPointSize(f.pointSize() * dDpr);
244 f.setBold(true);
245 QPainterPath tp;
246 tp.addText(0, 0, f, "BETA");
247 QRectF r = tp.boundingRect();
248
249 /* Center the text path: */
250 p.translate((size.width() - r.width()) / 2, size.height() - (size.height() - r.height()) / 2);
251 QPainterPathStroker pps;
252 QPainterPath pp = pps.createStroke(tp);
253 p.setPen(QPen(bgc.darker(80), 2, Qt::SolidLine, Qt::RoundCap));
254 p.drawPath(pp);
255 p.setBrush(Qt::black);
256 p.setPen(Qt::NoPen);
257 p.drawPath(tp);
258 p.end();
259
260 /* Smoothing: */
261 QImage i1(size, QImage::Format_ARGB32);
262 i1.fill(Qt::transparent);
263 QPainter p1(&i1);
264 p1.setCompositionMode(QPainter::CompositionMode_Source);
265 p1.drawImage(0, 0, i);
266 p1.setCompositionMode(QPainter::CompositionMode_DestinationIn);
267 QLinearGradient lg(0, 0, size.width(), 0);
268 lg.setColorAt(0, QColor(Qt::transparent));
269 lg.setColorAt(0.20, QColor(Qt::white));
270 lg.setColorAt(0.80, QColor(Qt::white));
271 lg.setColorAt(1, QColor(Qt::transparent));
272 p1.fillRect(0, 0, size.width(), size.height(), lg);
273 p1.end();
274 if (dDpr > 1.0)
275 i1.setDevicePixelRatio(dDpr);
276
277 return i1;
278}
279
280QPixmap UIImageTools::betaLabel(const QSize &size /* = QSize(80, 16) */, QWidget *pHint /* = 0 */)
281{
282 return QPixmap::fromImage(betaLabelImage(size, pHint));
283}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette