qpixmap_raster.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/gui/image/qpixmap_raster.cpp
Source codeSwitch to Preprocessed file
LineSourceCount
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 "qnativeimage_p.h"-
46#include "qimage_p.h"-
47#include "qpaintengine.h"-
48-
49#include "qbitmap.h"-
50#include "qimage.h"-
51#include <QBuffer>-
52#include <QImageReader>-
53#include <private/qimage_p.h>-
54#include <private/qsimd_p.h>-
55#include <private/qdrawhelper_p.h>-
56-
57QT_BEGIN_NAMESPACE-
58-
59QPixmap qt_toRasterPixmap(const QImage &image)-
60{-
61 QPlatformPixmap *data =-
62 new QRasterPlatformPixmap(image.depth() == 1-
63 ? QPlatformPixmap::BitmapType-
64 : QPlatformPixmap::PixmapType);-
65-
66 data->fromImage(image, Qt::AutoColor);-
67-
68 return QPixmap(data);
never executed: return QPixmap(data);
0
69}-
70-
71QPixmap qt_toRasterPixmap(const QPixmap &pixmap)-
72{-
73 if (pixmap.isNull())
pixmap.isNull()Description
TRUEnever evaluated
FALSEnever evaluated
0
74 return QPixmap();
never executed: return QPixmap();
0
75-
76 if (QPixmap(pixmap).data_ptr()->classId() == QPlatformPixmap::RasterClass)
QPixmap(pixmap...p::RasterClassDescription
TRUEnever evaluated
FALSEnever evaluated
0
77 return pixmap;
never executed: return pixmap;
0
78-
79 return qt_toRasterPixmap(pixmap.toImage());
never executed: return qt_toRasterPixmap(pixmap.toImage());
0
80}-
81-
82QRasterPlatformPixmap::QRasterPlatformPixmap(PixelType type)-
83 : QPlatformPixmap(type, RasterClass)-
84{-
85}
never executed: end of block
0
86-
87QRasterPlatformPixmap::~QRasterPlatformPixmap()-
88{-
89}-
90-
91QPlatformPixmap *QRasterPlatformPixmap::createCompatiblePlatformPixmap() const-
92{-
93 return new QRasterPlatformPixmap(pixelType());
never executed: return new QRasterPlatformPixmap(pixelType());
0
94}-
95-
96void QRasterPlatformPixmap::resize(int width, int height)-
97{-
98 QImage::Format format;-
99 if (pixelType() == BitmapType)
pixelType() == BitmapTypeDescription
TRUEnever evaluated
FALSEnever evaluated
0
100 format = QImage::Format_MonoLSB;
never executed: format = QImage::Format_MonoLSB;
0
101 else-
102 format = QNativeImage::systemFormat();
never executed: format = QNativeImage::systemFormat();
0
103-
104 image = QImage(width, height, format);-
105 w = width;-
106 h = height;-
107 d = image.depth();-
108 is_null = (w <= 0 || h <= 0);
w <= 0Description
TRUEnever evaluated
FALSEnever evaluated
h <= 0Description
TRUEnever evaluated
FALSEnever evaluated
0
109-
110 if (pixelType() == BitmapType && !image.isNull()) {
pixelType() == BitmapTypeDescription
TRUEnever evaluated
FALSEnever evaluated
!image.isNull()Description
TRUEnever evaluated
FALSEnever evaluated
0
111 image.setColorCount(2);-
112 image.setColor(0, QColor(Qt::color0).rgba());-
113 image.setColor(1, QColor(Qt::color1).rgba());-
114 }
never executed: end of block
0
115-
116 setSerialNumber(image.cacheKey() >> 32);-
117}
never executed: end of block
0
118-
119bool QRasterPlatformPixmap::fromData(const uchar *buffer, uint len, const char *format,-
120 Qt::ImageConversionFlags flags)-
121{-
122 QByteArray a = QByteArray::fromRawData(reinterpret_cast<const char *>(buffer), len);-
123 QBuffer b(&a);-
124 b.open(QIODevice::ReadOnly);-
125 QImage image = QImageReader(&b, format).read();-
126 if (image.isNull())
image.isNull()Description
TRUEnever evaluated
FALSEnever evaluated
0
127 return false;
never executed: return false;
0
128-
129 createPixmapForImage(image, flags, /* inplace = */true);-
130 return !isNull();
never executed: return !isNull();
0
131}-
132-
133void QRasterPlatformPixmap::fromImage(const QImage &sourceImage,-
134 Qt::ImageConversionFlags flags)-
135{-
136 QImage image = sourceImage;-
137 createPixmapForImage(image, flags, /* inplace = */false);-
138}
never executed: end of block
0
139-
140void QRasterPlatformPixmap::fromImageInPlace(QImage &sourceImage,-
141 Qt::ImageConversionFlags flags)-
142{-
143 createPixmapForImage(sourceImage, flags, /* inplace = */true);-
144}
never executed: end of block
0
145-
146void QRasterPlatformPixmap::fromImageReader(QImageReader *imageReader,-
147 Qt::ImageConversionFlags flags)-
148{-
149 Q_UNUSED(flags);-
150 QImage image = imageReader->read();-
151 if (image.isNull())
image.isNull()Description
TRUEnever evaluated
FALSEnever evaluated
0
152 return;
never executed: return;
0
153-
154 createPixmapForImage(image, flags, /* inplace = */true);-
155}
never executed: end of block
0
156-
157// from qbackingstore.cpp-
158extern void qt_scrollRectInImage(QImage &img, const QRect &rect, const QPoint &offset);-
159-
160void QRasterPlatformPixmap::copy(const QPlatformPixmap *data, const QRect &rect)-
161{-
162 fromImage(data->toImage(rect).copy(), Qt::NoOpaqueDetection);-
163}
never executed: end of block
0
164-
165bool QRasterPlatformPixmap::scroll(int dx, int dy, const QRect &rect)-
166{-
167 if (!image.isNull())
!image.isNull()Description
TRUEnever evaluated
FALSEnever evaluated
0
168 qt_scrollRectInImage(image, rect, QPoint(dx, dy));
never executed: qt_scrollRectInImage(image, rect, QPoint(dx, dy));
0
169 return true;
never executed: return true;
0
170}-
171-
172void QRasterPlatformPixmap::fill(const QColor &color)-
173{-
174 uint pixel;-
175-
176 if (image.depth() == 1) {
image.depth() == 1Description
TRUEnever evaluated
FALSEnever evaluated
0
177 int gray = qGray(color.rgba());-
178 // Pick the best approximate color in the image's colortable.-
179 if (qAbs(qGray(image.color(0)) - gray) < qAbs(qGray(image.color(1)) - gray))
qAbs(qGray(ima...or(1)) - gray)Description
TRUEnever evaluated
FALSEnever evaluated
0
180 pixel = 0;
never executed: pixel = 0;
0
181 else-
182 pixel = 1;
never executed: pixel = 1;
0
183 } else if (image.depth() >= 15) {
image.depth() >= 15Description
TRUEnever evaluated
FALSEnever evaluated
0
184 int alpha = color.alpha();-
185 if (alpha != 255) {
alpha != 255Description
TRUEnever evaluated
FALSEnever evaluated
0
186 if (!image.hasAlphaChannel()) {
!image.hasAlphaChannel()Description
TRUEnever evaluated
FALSEnever evaluated
0
187 QImage::Format toFormat = qt_alphaVersionForPainting(image.format());-
188 if (!image.isNull() && qt_depthForFormat(image.format()) == qt_depthForFormat(toFormat)) {
!image.isNull()Description
TRUEnever evaluated
FALSEnever evaluated
qt_depthForFor...rmat(toFormat)Description
TRUEnever evaluated
FALSEnever evaluated
0
189 image.detach();-
190 image.d->format = toFormat;-
191 } else {
never executed: end of block
0
192 image = QImage(image.width(), image.height(), toFormat);-
193 }
never executed: end of block
0
194 }-
195 }
never executed: end of block
0
196 pixel = qPremultiply(color.rgba());-
197 const QPixelLayout *layout = &qPixelLayouts[image.format()];-
198 layout->convertFromARGB32PM(&pixel, &pixel, 1, layout, 0);-
199 } else if (image.format() == QImage::Format_Alpha8) {
never executed: end of block
image.format()...:Format_Alpha8Description
TRUEnever evaluated
FALSEnever evaluated
0
200 pixel = qAlpha(color.rgba());-
201 } else if (image.format() == QImage::Format_Grayscale8) {
never executed: end of block
image.format()...mat_Grayscale8Description
TRUEnever evaluated
FALSEnever evaluated
0
202 pixel = qGray(color.rgba());-
203 } else
never executed: end of block
0
204 {-
205 pixel = 0;-
206 // ### what about 8 bit indexed?-
207 }
never executed: end of block
0
208-
209 image.fill(pixel);-
210}
never executed: end of block
0
211-
212bool QRasterPlatformPixmap::hasAlphaChannel() const-
213{-
214 return image.hasAlphaChannel();
never executed: return image.hasAlphaChannel();
0
215}-
216-
217QImage QRasterPlatformPixmap::toImage() const-
218{-
219 if (!image.isNull()) {
!image.isNull()Description
TRUEnever evaluated
FALSEnever evaluated
0
220 QImageData *data = const_cast<QImage &>(image).data_ptr();-
221 if (data->paintEngine && data->paintEngine->isActive()
data->paintEngineDescription
TRUEnever evaluated
FALSEnever evaluated
data->paintEngine->isActive()Description
TRUEnever evaluated
FALSEnever evaluated
0
222 && data->paintEngine->paintDevice() == &image)
data->paintEng...ce() == &imageDescription
TRUEnever evaluated
FALSEnever evaluated
0
223 {-
224 return image.copy();
never executed: return image.copy();
0
225 }-
226 }
never executed: end of block
0
227-
228 return image;
never executed: return image;
0
229}-
230-
231QImage QRasterPlatformPixmap::toImage(const QRect &rect) const-
232{-
233 if (rect.isNull())
rect.isNull()Description
TRUEnever evaluated
FALSEnever evaluated
0
234 return image;
never executed: return image;
0
235-
236 QRect clipped = rect.intersected(QRect(0, 0, w, h));-
237 const uint du = uint(d);-
238 if ((du % 8 == 0) && (((uint(clipped.x()) * du)) % 32 == 0)) {
(du % 8 == 0)Description
TRUEnever evaluated
FALSEnever evaluated
(((uint(clippe...u)) % 32 == 0)Description
TRUEnever evaluated
FALSEnever evaluated
0
239 QImage newImage(image.scanLine(clipped.y()) + clipped.x() * (du / 8),-
240 clipped.width(), clipped.height(),-
241 image.bytesPerLine(), image.format());-
242 newImage.setDevicePixelRatio(image.devicePixelRatio());-
243 return newImage;
never executed: return newImage;
0
244 } else {-
245 return image.copy(clipped);
never executed: return image.copy(clipped);
0
246 }-
247}-
248-
249QPaintEngine* QRasterPlatformPixmap::paintEngine() const-
250{-
251 return image.paintEngine();
never executed: return image.paintEngine();
0
252}-
253-
254int QRasterPlatformPixmap::metric(QPaintDevice::PaintDeviceMetric metric) const-
255{-
256 QImageData *d = image.d;-
257 if (!d)
!dDescription
TRUEnever evaluated
FALSEnever evaluated
0
258 return 0;
never executed: return 0;
0
259-
260 // override the image dpi with the screen dpi when rendering to a pixmap-
261 switch (metric) {-
262 case QPaintDevice::PdmWidth:
never executed: case QPaintDevice::PdmWidth:
0
263 return w;
never executed: return w;
0
264 case QPaintDevice::PdmHeight:
never executed: case QPaintDevice::PdmHeight:
0
265 return h;
never executed: return h;
0
266 case QPaintDevice::PdmWidthMM:
never executed: case QPaintDevice::PdmWidthMM:
0
267 return qRound(d->width * 25.4 / qt_defaultDpiX());
never executed: return qRound(d->width * 25.4 / qt_defaultDpiX());
0
268 case QPaintDevice::PdmHeightMM:
never executed: case QPaintDevice::PdmHeightMM:
0
269 return qRound(d->height * 25.4 / qt_defaultDpiY());
never executed: return qRound(d->height * 25.4 / qt_defaultDpiY());
0
270 case QPaintDevice::PdmNumColors:
never executed: case QPaintDevice::PdmNumColors:
0
271 return d->colortable.size();
never executed: return d->colortable.size();
0
272 case QPaintDevice::PdmDepth:
never executed: case QPaintDevice::PdmDepth:
0
273 return this->d;
never executed: return this->d;
0
274 case QPaintDevice::PdmDpiX:
never executed: case QPaintDevice::PdmDpiX:
0
275 return qt_defaultDpiX();
never executed: return qt_defaultDpiX();
0
276 case QPaintDevice::PdmPhysicalDpiX:
never executed: case QPaintDevice::PdmPhysicalDpiX:
0
277 return qt_defaultDpiX();
never executed: return qt_defaultDpiX();
0
278 case QPaintDevice::PdmDpiY:
never executed: case QPaintDevice::PdmDpiY:
0
279 return qt_defaultDpiX();
never executed: return qt_defaultDpiX();
0
280 case QPaintDevice::PdmPhysicalDpiY:
never executed: case QPaintDevice::PdmPhysicalDpiY:
0
281 return qt_defaultDpiY();
never executed: return qt_defaultDpiY();
0
282 case QPaintDevice::PdmDevicePixelRatio:
never executed: case QPaintDevice::PdmDevicePixelRatio:
0
283 return image.devicePixelRatio();
never executed: return image.devicePixelRatio();
0
284 case QPaintDevice::PdmDevicePixelRatioScaled:
never executed: case QPaintDevice::PdmDevicePixelRatioScaled:
0
285 return image.devicePixelRatio() * QPaintDevice::devicePixelRatioFScale();
never executed: return image.devicePixelRatio() * QPaintDevice::devicePixelRatioFScale();
0
286-
287 default:
never executed: default:
0
288 qWarning("QRasterPlatformPixmap::metric(): Unhandled metric type %d", metric);-
289 break;
never executed: break;
0
290 }-
291-
292 return 0;
never executed: return 0;
0
293}-
294-
295void QRasterPlatformPixmap::createPixmapForImage(QImage &sourceImage, Qt::ImageConversionFlags flags, bool inPlace)-
296{-
297 QImage::Format format;-
298 if (flags & Qt::NoFormatConversion)
flags & Qt::NoFormatConversionDescription
TRUEnever evaluated
FALSEnever evaluated
0
299 format = sourceImage.format();
never executed: format = sourceImage.format();
0
300 else-
301 if (pixelType() == BitmapType) {
pixelType() == BitmapTypeDescription
TRUEnever evaluated
FALSEnever evaluated
0
302 format = QImage::Format_MonoLSB;-
303 } else {
never executed: end of block
0
304 if (sourceImage.depth() == 1) {
sourceImage.depth() == 1Description
TRUEnever evaluated
FALSEnever evaluated
0
305 format = sourceImage.hasAlphaChannel()
sourceImage.hasAlphaChannel()Description
TRUEnever evaluated
FALSEnever evaluated
0
306 ? QImage::Format_ARGB32_Premultiplied-
307 : QImage::Format_RGB32;-
308 } else {
never executed: end of block
0
309 QImage::Format opaqueFormat = QNativeImage::systemFormat();-
310 QImage::Format alphaFormat = qt_alphaVersionForPainting(opaqueFormat);-
311-
312 if (!sourceImage.hasAlphaChannel()) {
!sourceImage.hasAlphaChannel()Description
TRUEnever evaluated
FALSEnever evaluated
0
313 format = opaqueFormat;-
314 } else if ((flags & Qt::NoOpaqueDetection) == 0
never executed: end of block
(flags & Qt::N...etection) == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
315 && !const_cast<QImage &>(sourceImage).data_ptr()->checkForAlphaPixels())
!const_cast<QI...rAlphaPixels()Description
TRUEnever evaluated
FALSEnever evaluated
0
316 {-
317 format = opaqueFormat;-
318 } else {
never executed: end of block
0
319 format = alphaFormat;-
320 }
never executed: end of block
0
321 }-
322 }-
323-
324 // image has alpha format but is really opaque, so try to do a-
325 // more efficient conversion-
326 if (format == QImage::Format_RGB32 && (sourceImage.format() == QImage::Format_ARGB32
format == QImage::Format_RGB32Description
TRUEnever evaluated
FALSEnever evaluated
sourceImage.fo...:Format_ARGB32Description
TRUEnever evaluated
FALSEnever evaluated
0
327 || sourceImage.format() == QImage::Format_ARGB32_Premultiplied))
sourceImage.fo..._PremultipliedDescription
TRUEnever evaluated
FALSEnever evaluated
0
328 {-
329 inPlace = inPlace && sourceImage.isDetached();
inPlaceDescription
TRUEnever evaluated
FALSEnever evaluated
sourceImage.isDetached()Description
TRUEnever evaluated
FALSEnever evaluated
0
330 image = sourceImage;-
331 if (!inPlace)
!inPlaceDescription
TRUEnever evaluated
FALSEnever evaluated
0
332 image.detach();
never executed: image.detach();
0
333 if (image.d)
image.dDescription
TRUEnever evaluated
FALSEnever evaluated
0
334 image.d->format = QImage::Format_RGB32;
never executed: image.d->format = QImage::Format_RGB32;
0
335 } else if (inPlace && sourceImage.d->convertInPlace(format, flags)) {
never executed: end of block
inPlaceDescription
TRUEnever evaluated
FALSEnever evaluated
sourceImage.d-...format, flags)Description
TRUEnever evaluated
FALSEnever evaluated
0
336 image = sourceImage;-
337 } else {
never executed: end of block
0
338 image = sourceImage.convertToFormat(format);-
339 }
never executed: end of block
0
340-
341 if (image.d) {
image.dDescription
TRUEnever evaluated
FALSEnever evaluated
0
342 w = image.d->width;-
343 h = image.d->height;-
344 d = image.d->depth;-
345 } else {
never executed: end of block
0
346 w = h = d = 0;-
347 }
never executed: end of block
0
348 is_null = (w <= 0 || h <= 0);
w <= 0Description
TRUEnever evaluated
FALSEnever evaluated
h <= 0Description
TRUEnever evaluated
FALSEnever evaluated
0
349-
350 if (image.d)
image.dDescription
TRUEnever evaluated
FALSEnever evaluated
0
351 image.d->devicePixelRatio = sourceImage.devicePixelRatio();
never executed: image.d->devicePixelRatio = sourceImage.devicePixelRatio();
0
352 //ensure the pixmap and the image resulting from toImage() have the same cacheKey();-
353 setSerialNumber(image.cacheKey() >> 32);-
354 if (image.d)
image.dDescription
TRUEnever evaluated
FALSEnever evaluated
0
355 setDetachNumber(image.d->detach_no);
never executed: setDetachNumber(image.d->detach_no);
0
356}
never executed: end of block
0
357-
358QImage* QRasterPlatformPixmap::buffer()-
359{-
360 return &image;
never executed: return &image;
0
361}-
362-
363qreal QRasterPlatformPixmap::devicePixelRatio() const-
364{-
365 return image.devicePixelRatio();
never executed: return image.devicePixelRatio();
0
366}-
367-
368void QRasterPlatformPixmap::setDevicePixelRatio(qreal scaleFactor)-
369{-
370 image.setDevicePixelRatio(scaleFactor);-
371}
never executed: end of block
0
372-
373QT_END_NAMESPACE-
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9