1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #include "qpixmap.h"
41
42 #include <private/qfont_p.h>
43
44 #include "qpixmap_raster_p.h"
45 #include "qimage_p.h"
46 #include "qpaintengine.h"
47
48 #include "qbitmap.h"
49 #include "qimage.h"
50 #include <QBuffer>
51 #include <QImageReader>
52 #include <QGuiApplication>
53 #include <QScreen>
54 #include <private/qsimd_p.h>
55 #include <private/qdrawhelper_p.h>
56 #include <qpa/qplatformscreen.h>
57
58 QT_BEGIN_NAMESPACE
59
qt_toRasterPixmap(const QImage & image)60 QPixmap qt_toRasterPixmap(const QImage &image)
61 {
62 QPlatformPixmap *data =
63 new QRasterPlatformPixmap(image.depth() == 1
64 ? QPlatformPixmap::BitmapType
65 : QPlatformPixmap::PixmapType);
66
67 data->fromImage(image, Qt::AutoColor);
68
69 return QPixmap(data);
70 }
71
qt_toRasterPixmap(const QPixmap & pixmap)72 QPixmap qt_toRasterPixmap(const QPixmap &pixmap)
73 {
74 if (pixmap.isNull())
75 return QPixmap();
76
77 if (QPixmap(pixmap).data_ptr()->classId() == QPlatformPixmap::RasterClass)
78 return pixmap;
79
80 return qt_toRasterPixmap(pixmap.toImage());
81 }
82
QRasterPlatformPixmap(PixelType type)83 QRasterPlatformPixmap::QRasterPlatformPixmap(PixelType type)
84 : QPlatformPixmap(type, RasterClass)
85 {
86 }
87
~QRasterPlatformPixmap()88 QRasterPlatformPixmap::~QRasterPlatformPixmap()
89 {
90 }
91
systemNativeFormat()92 QImage::Format QRasterPlatformPixmap::systemNativeFormat()
93 {
94 if (!QGuiApplication::primaryScreen())
95 return QImage::Format_RGB32;
96 return QGuiApplication::primaryScreen()->handle()->format();
97 }
98
createCompatiblePlatformPixmap() const99 QPlatformPixmap *QRasterPlatformPixmap::createCompatiblePlatformPixmap() const
100 {
101 return new QRasterPlatformPixmap(pixelType());
102 }
103
resize(int width,int height)104 void QRasterPlatformPixmap::resize(int width, int height)
105 {
106 QImage::Format format;
107 if (pixelType() == BitmapType)
108 format = QImage::Format_MonoLSB;
109 else
110 format = systemNativeFormat();
111
112 image = QImage(width, height, format);
113 w = width;
114 h = height;
115 d = image.depth();
116 is_null = (w <= 0 || h <= 0);
117
118 if (pixelType() == BitmapType && !image.isNull()) {
119 image.setColorCount(2);
120 image.setColor(0, QColor(Qt::color0).rgba());
121 image.setColor(1, QColor(Qt::color1).rgba());
122 }
123
124 setSerialNumber(image.cacheKey() >> 32);
125 }
126
fromData(const uchar * buffer,uint len,const char * format,Qt::ImageConversionFlags flags)127 bool QRasterPlatformPixmap::fromData(const uchar *buffer, uint len, const char *format,
128 Qt::ImageConversionFlags flags)
129 {
130 QByteArray a = QByteArray::fromRawData(reinterpret_cast<const char *>(buffer), len);
131 QBuffer b(&a);
132 b.open(QIODevice::ReadOnly);
133 QImage image = QImageReader(&b, format).read();
134 if (image.isNull())
135 return false;
136
137 createPixmapForImage(std::move(image), flags);
138 return !isNull();
139 }
140
fromImage(const QImage & sourceImage,Qt::ImageConversionFlags flags)141 void QRasterPlatformPixmap::fromImage(const QImage &sourceImage,
142 Qt::ImageConversionFlags flags)
143 {
144 QImage image = sourceImage;
145 createPixmapForImage(std::move(image), flags);
146 }
147
fromImageInPlace(QImage & sourceImage,Qt::ImageConversionFlags flags)148 void QRasterPlatformPixmap::fromImageInPlace(QImage &sourceImage,
149 Qt::ImageConversionFlags flags)
150 {
151 createPixmapForImage(std::move(sourceImage), flags);
152 }
153
fromImageReader(QImageReader * imageReader,Qt::ImageConversionFlags flags)154 void QRasterPlatformPixmap::fromImageReader(QImageReader *imageReader,
155 Qt::ImageConversionFlags flags)
156 {
157 Q_UNUSED(flags);
158 QImage image = imageReader->read();
159 if (image.isNull())
160 return;
161
162 createPixmapForImage(std::move(image), flags);
163 }
164
165 // from qbackingstore.cpp
166 extern void qt_scrollRectInImage(QImage &img, const QRect &rect, const QPoint &offset);
167
copy(const QPlatformPixmap * data,const QRect & rect)168 void QRasterPlatformPixmap::copy(const QPlatformPixmap *data, const QRect &rect)
169 {
170 fromImage(data->toImage(rect).copy(), Qt::NoOpaqueDetection);
171 }
172
scroll(int dx,int dy,const QRect & rect)173 bool QRasterPlatformPixmap::scroll(int dx, int dy, const QRect &rect)
174 {
175 if (!image.isNull())
176 qt_scrollRectInImage(image, rect, QPoint(dx, dy));
177 return true;
178 }
179
fill(const QColor & color)180 void QRasterPlatformPixmap::fill(const QColor &color)
181 {
182 uint pixel;
183
184 if (image.depth() == 1) {
185 int gray = qGray(color.rgba());
186 // Pick the best approximate color in the image's colortable.
187 if (qAbs(qGray(image.color(0)) - gray) < qAbs(qGray(image.color(1)) - gray))
188 pixel = 0;
189 else
190 pixel = 1;
191 } else if (image.depth() >= 15) {
192 int alpha = color.alpha();
193 if (alpha != 255) {
194 if (!image.hasAlphaChannel()) {
195 QImage::Format toFormat = qt_alphaVersionForPainting(image.format());
196 if (!image.reinterpretAsFormat(toFormat))
197 image = QImage(image.width(), image.height(), toFormat);
198 }
199 }
200 image.fill(color);
201 return;
202 } else if (image.format() == QImage::Format_Alpha8) {
203 pixel = qAlpha(color.rgba());
204 } else if (image.format() == QImage::Format_Grayscale8) {
205 pixel = qGray(color.rgba());
206 } else if (image.format() == QImage::Format_Grayscale16) {
207 QRgba64 c = color.rgba64();
208 pixel = qGray(c.red(), c.green(), c.blue());
209 } else
210 {
211 pixel = 0;
212 // ### what about 8 bit indexed?
213 }
214
215 image.fill(pixel);
216 }
217
hasAlphaChannel() const218 bool QRasterPlatformPixmap::hasAlphaChannel() const
219 {
220 return image.hasAlphaChannel();
221 }
222
toImage() const223 QImage QRasterPlatformPixmap::toImage() const
224 {
225 if (!image.isNull()) {
226 QImageData *data = const_cast<QImage &>(image).data_ptr();
227 if (data->paintEngine && data->paintEngine->isActive()
228 && data->paintEngine->paintDevice() == &image)
229 {
230 return image.copy();
231 }
232 }
233
234 return image;
235 }
236
toImage(const QRect & rect) const237 QImage QRasterPlatformPixmap::toImage(const QRect &rect) const
238 {
239 if (rect.isNull())
240 return image;
241
242 QRect clipped = rect.intersected(QRect(0, 0, w, h));
243 const uint du = uint(d);
244 if ((du % 8 == 0) && (((uint(clipped.x()) * du)) % 32 == 0)) {
245 QImage newImage(image.scanLine(clipped.y()) + clipped.x() * (du / 8),
246 clipped.width(), clipped.height(),
247 image.bytesPerLine(), image.format());
248 newImage.setDevicePixelRatio(image.devicePixelRatio());
249 return newImage;
250 } else {
251 return image.copy(clipped);
252 }
253 }
254
paintEngine() const255 QPaintEngine* QRasterPlatformPixmap::paintEngine() const
256 {
257 return image.paintEngine();
258 }
259
metric(QPaintDevice::PaintDeviceMetric metric) const260 int QRasterPlatformPixmap::metric(QPaintDevice::PaintDeviceMetric metric) const
261 {
262 QImageData *d = image.d;
263 if (!d)
264 return 0;
265
266 // override the image dpi with the screen dpi when rendering to a pixmap
267 switch (metric) {
268 case QPaintDevice::PdmWidth:
269 return w;
270 case QPaintDevice::PdmHeight:
271 return h;
272 case QPaintDevice::PdmWidthMM:
273 return qRound(d->width * 25.4 / qt_defaultDpiX());
274 case QPaintDevice::PdmHeightMM:
275 return qRound(d->height * 25.4 / qt_defaultDpiY());
276 case QPaintDevice::PdmNumColors:
277 return d->colortable.size();
278 case QPaintDevice::PdmDepth:
279 return this->d;
280 case QPaintDevice::PdmDpiX:
281 return qt_defaultDpiX();
282 case QPaintDevice::PdmPhysicalDpiX:
283 return qt_defaultDpiX();
284 case QPaintDevice::PdmDpiY:
285 return qt_defaultDpiY();
286 case QPaintDevice::PdmPhysicalDpiY:
287 return qt_defaultDpiY();
288 case QPaintDevice::PdmDevicePixelRatio:
289 return image.devicePixelRatio();
290 case QPaintDevice::PdmDevicePixelRatioScaled:
291 return image.devicePixelRatio() * QPaintDevice::devicePixelRatioFScale();
292
293 default:
294 qWarning("QRasterPlatformPixmap::metric(): Unhandled metric type %d", metric);
295 break;
296 }
297
298 return 0;
299 }
300
createPixmapForImage(QImage sourceImage,Qt::ImageConversionFlags flags)301 void QRasterPlatformPixmap::createPixmapForImage(QImage sourceImage, Qt::ImageConversionFlags flags)
302 {
303 QImage::Format format;
304 if (flags & Qt::NoFormatConversion)
305 format = sourceImage.format();
306 else
307 if (pixelType() == BitmapType) {
308 format = QImage::Format_MonoLSB;
309 } else {
310 if (sourceImage.depth() == 1) {
311 format = sourceImage.hasAlphaChannel()
312 ? QImage::Format_ARGB32_Premultiplied
313 : QImage::Format_RGB32;
314 } else {
315 QImage::Format nativeFormat = systemNativeFormat();
316 QImage::Format opaqueFormat = qt_opaqueVersionForPainting(nativeFormat);
317 QImage::Format alphaFormat = qt_alphaVersionForPainting(nativeFormat);
318
319 if (!sourceImage.hasAlphaChannel()) {
320 format = opaqueFormat;
321 } else if ((flags & Qt::NoOpaqueDetection) == 0
322 && !sourceImage.data_ptr()->checkForAlphaPixels())
323 {
324 format = opaqueFormat;
325 } else {
326 format = alphaFormat;
327 }
328 }
329 }
330
331 // image has alpha format but is really opaque, so try to do a
332 // more efficient conversion
333 if (format == QImage::Format_RGB32 && (sourceImage.format() == QImage::Format_ARGB32
334 || sourceImage.format() == QImage::Format_ARGB32_Premultiplied))
335 {
336 image = std::move(sourceImage);
337 image.reinterpretAsFormat(QImage::Format_RGB32);
338 } else {
339 image = std::move(sourceImage).convertToFormat(format, flags);
340 }
341
342 if (image.d) {
343 w = image.d->width;
344 h = image.d->height;
345 d = image.d->depth;
346 } else {
347 w = h = d = 0;
348 }
349 is_null = (w <= 0 || h <= 0);
350
351 //ensure the pixmap and the image resulting from toImage() have the same cacheKey();
352 setSerialNumber(image.cacheKey() >> 32);
353 if (image.d)
354 setDetachNumber(image.d->detach_no);
355 }
356
buffer()357 QImage* QRasterPlatformPixmap::buffer()
358 {
359 return ℑ
360 }
361
devicePixelRatio() const362 qreal QRasterPlatformPixmap::devicePixelRatio() const
363 {
364 return image.devicePixelRatio();
365 }
366
setDevicePixelRatio(qreal scaleFactor)367 void QRasterPlatformPixmap::setDevicePixelRatio(qreal scaleFactor)
368 {
369 image.setDevicePixelRatio(scaleFactor);
370 }
371
372 QT_END_NAMESPACE
373