VirtualBox

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

Last change on this file was 101396, checked in by vboxsync, 7 months ago

FE/Qt: VBox Manager: Moving foreground color selection to UIImageTools so that it could be reused from various places; It's previously used in Chooser and Tools panes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.8 KB
Line 
1/* $Id: UIImageTools.cpp 101396 2023-10-10 05:46:03Z 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#include <QPalette>
33
34/* GUI include */
35#include "UIDesktopWidgetWatchdog.h"
36#include "UIImageTools.h"
37
38/* External includes: */
39#include <math.h>
40
41
42QImage UIImageTools::toGray(const QImage &image)
43{
44 QImage result = image.convertToFormat(QImage::Format_ARGB32);
45 for (int y = 0; y < result.height(); ++y)
46 {
47 QRgb *pScanLine = (QRgb*)result.scanLine(y);
48 for (int x = 0; x < result.width(); ++x)
49 {
50 const int g = qGray(pScanLine[x]);
51 pScanLine[x] = qRgba(g, g, g, qAlpha(pScanLine[x]));
52 }
53 }
54 return result;
55}
56
57void UIImageTools::dimImage(QImage &image)
58{
59 for (int y = 0; y < image.height(); ++y)
60 {
61 QRgb *pScanLine = (QRgb*)image.scanLine(y);
62 if (y % 2)
63 {
64 if (image.depth() == 32)
65 {
66 for (int x = 0; x < image.width(); ++x)
67 {
68 const int iGray = qGray(pScanLine[x]) / 2;
69 pScanLine[x] = qRgba(iGray, iGray, iGray, qAlpha(pScanLine[x]));
70 }
71 }
72 else
73 ::memset(pScanLine, 0, image.bytesPerLine());
74 }
75 else
76 {
77 if (image.depth() == 32)
78 {
79 for (int x = 0; x < image.width(); ++x)
80 {
81 const int iGray = (2 * qGray(pScanLine[x])) / 3;
82 pScanLine[x] = qRgba(iGray, iGray, iGray, qAlpha(pScanLine[x]));
83 }
84 }
85 }
86 }
87}
88
89void UIImageTools::blurImage(const QImage &source, QImage &destination, int iRadius)
90{
91 /* Blur in two steps, first horizontal and then vertical: */
92 QImage tmpImage(source.size(), QImage::Format_ARGB32);
93 blurImageHorizontal(source, tmpImage, iRadius);
94 blurImageVertical(tmpImage, destination, iRadius);
95}
96
97void UIImageTools::blurImageHorizontal(const QImage &source, QImage &destination, int iRadius)
98{
99 QSize s = source.size();
100 for (int y = 0; y < s.height(); ++y)
101 {
102 int rt = 0;
103 int gt = 0;
104 int bt = 0;
105 int at = 0;
106
107 /* In the horizontal case we can just use the scanline, which is
108 * much faster than accessing every pixel with the QImage::pixel
109 * method. Unfortunately this doesn't work in the vertical case. */
110 QRgb *ssl = (QRgb*)source.scanLine(y);
111 QRgb *dsl = (QRgb*)destination.scanLine(y);
112 /* First process the horizontal zero line at once: */
113 int b = iRadius + 1;
114 for (int x1 = 0; x1 <= iRadius; ++x1)
115 {
116 QRgb rgba = ssl[x1];
117 rt += qRed(rgba);
118 gt += qGreen(rgba);
119 bt += qBlue(rgba);
120 at += qAlpha(rgba);
121 }
122 /* Set the new weighted pixel: */
123 dsl[0] = qRgba(rt / b, gt / b, bt / b, at / b);
124
125 /* Now process the rest */
126 for (int x = 1; x < s.width(); ++x)
127 {
128 /* Subtract the pixel which fall out of our blur matrix: */
129 int x1 = x - iRadius - 1;
130 if (x1 >= 0)
131 {
132 /* Adjust the weight (necessary for the border case): */
133 --b;
134 QRgb rgba = ssl[x1];
135 rt -= qRed(rgba);
136 gt -= qGreen(rgba);
137 bt -= qBlue(rgba);
138 at -= qAlpha(rgba);
139 }
140
141 /* Add the pixel which get into our blur matrix: */
142 int x2 = x + iRadius;
143 if (x2 < s.width())
144 {
145 /* Adjust the weight (necessary for the border case): */
146 ++b;
147 QRgb rgba = ssl[x2];
148 rt += qRed(rgba);
149 gt += qGreen(rgba);
150 bt += qBlue(rgba);
151 at += qAlpha(rgba);
152 }
153 /* Set the new weighted pixel: */
154 dsl[x] = qRgba(rt / b, gt / b, bt / b, at / b);
155 }
156 }
157}
158
159void UIImageTools::blurImageVertical(const QImage &source, QImage &destination, int iRadius)
160{
161 QSize s = source.size();
162 destination = QImage(s, source.format());
163 for (int x = 0; x < s.width(); ++x)
164 {
165 int rt = 0;
166 int gt = 0;
167 int bt = 0;
168 int at = 0;
169
170 /* First process the vertical zero line at once: */
171 int b = iRadius + 1;
172 for (int y1 = 0; y1 <= iRadius; ++y1)
173 {
174 QRgb rgba = source.pixel(x, y1);
175 rt += qRed(rgba);
176 gt += qGreen(rgba);
177 bt += qBlue(rgba);
178 at += qAlpha(rgba);
179 }
180 /* Set the new weighted pixel: */
181 destination.setPixel(x, 0, qRgba(rt / b, gt / b, bt / b, at / b));
182
183 /* Now process the rest: */
184 for (int y = 1; y < s.height(); ++y)
185 {
186 /* Subtract the pixel which fall out of our blur matrix: */
187 int y1 = y - iRadius - 1;
188 if (y1 >= 0)
189 {
190 --b; /* Adjust the weight (necessary for the border case): */
191 QRgb rgba = source.pixel(x, y1);
192 rt -= qRed(rgba);
193 gt -= qGreen(rgba);
194 bt -= qBlue(rgba);
195 at -= qAlpha(rgba);
196 }
197
198 /* Add the pixel which get into our blur matrix: */
199 int y2 = y + iRadius;
200 if (y2 < s.height())
201 {
202 ++b; /* Adjust the weight (necessary for the border case): */
203 QRgb rgba = source.pixel(x, y2);
204 rt += qRed(rgba);
205 gt += qGreen(rgba);
206 bt += qBlue(rgba);
207 at += qAlpha(rgba);
208 }
209 /* Set the new weighted pixel: */
210 destination.setPixel(x, y, qRgba(rt / b, gt / b, bt / b, at / b));
211 }
212 }
213}
214
215static QImage betaLabelImage(QSize size, QWidget *pHint)
216{
217 /* Calculate device pixel ratio: */
218 const double dDpr = pHint ? UIDesktopWidgetWatchdog::devicePixelRatio(pHint) : UIDesktopWidgetWatchdog::devicePixelRatio(-1);
219 if (dDpr > 1.0)
220 size *= dDpr;
221
222 /* Beta label: */
223 QColor bgc(246, 179, 0);
224 QImage i(size, QImage::Format_ARGB32);
225 i.fill(Qt::transparent);
226 QPainter p(&i);
227 p.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
228 p.setPen(Qt::NoPen);
229
230 /* Background: */
231 p.setBrush(bgc);
232 p.drawRect(0, 0, size.width(), size.height());
233
234 /* The black stripes: */
235 p.setPen(QPen(QColor(70, 70, 70), 5));
236 float c = ((float)size.width() / size.height()) + 1;
237 float g = (size.width() / (c - 1));
238 for (int i = 0; i < c; ++i)
239 p.drawLine((int)(-g / 2 + g * i), size.height(), (int)(-g / 2 + g * (i + 1)), 0);
240
241 /* The text: */
242 QFont f = p.font();
243 if (dDpr > 1.0)
244 f.setPointSize(f.pointSize() * dDpr);
245 f.setBold(true);
246 QPainterPath tp;
247 tp.addText(0, 0, f, "BETA");
248 QRectF r = tp.boundingRect();
249
250 /* Center the text path: */
251 p.translate((size.width() - r.width()) / 2, size.height() - (size.height() - r.height()) / 2);
252 QPainterPathStroker pps;
253 QPainterPath pp = pps.createStroke(tp);
254 p.setPen(QPen(bgc.darker(80), 2, Qt::SolidLine, Qt::RoundCap));
255 p.drawPath(pp);
256 p.setBrush(Qt::black);
257 p.setPen(Qt::NoPen);
258 p.drawPath(tp);
259 p.end();
260
261 /* Smoothing: */
262 QImage i1(size, QImage::Format_ARGB32);
263 i1.fill(Qt::transparent);
264 QPainter p1(&i1);
265 p1.setCompositionMode(QPainter::CompositionMode_Source);
266 p1.drawImage(0, 0, i);
267 p1.setCompositionMode(QPainter::CompositionMode_DestinationIn);
268 QLinearGradient lg(0, 0, size.width(), 0);
269 lg.setColorAt(0, QColor(Qt::transparent));
270 lg.setColorAt(0.20, QColor(Qt::white));
271 lg.setColorAt(0.80, QColor(Qt::white));
272 lg.setColorAt(1, QColor(Qt::transparent));
273 p1.fillRect(0, 0, size.width(), size.height(), lg);
274 p1.end();
275 if (dDpr > 1.0)
276 i1.setDevicePixelRatio(dDpr);
277
278 return i1;
279}
280
281QPixmap UIImageTools::betaLabel(const QSize &size /* = QSize(80, 16) */, QWidget *pHint /* = 0 */)
282{
283 return QPixmap::fromImage(betaLabelImage(size, pHint));
284}
285
286QColor UIImageTools::suitableForegroundColor(const QPalette &pal, const QColor &background)
287{
288 /* Get possible foreground colors: */
289 const QColor simpleText = pal.color(QPalette::Active, QPalette::Text);
290 const QColor highlightText = pal.color(QPalette::Active, QPalette::HighlightedText);
291 QColor lightText = simpleText.black() < highlightText.black() ? simpleText : highlightText;
292 QColor darkText = simpleText.black() > highlightText.black() ? simpleText : highlightText;
293 if (lightText.black() > 128)
294 lightText = QColor(Qt::white);
295 if (darkText.black() < 128)
296 darkText = QColor(Qt::black);
297
298 /* Measure background luminance: */
299 double dLuminance = (0.299 * background.red() + 0.587 * background.green() + 0.114 * background.blue()) / 255;
300 //printf("luminance = %f\n", dLuminance);
301
302 /* Gather foreground color for background one: */
303 return dLuminance > 0.5 ? darkText : lightText;
304}
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use