image/qpixmap_raster.cpp

Source codeSwitch to Preprocessed file
LineSource CodeCoverage
1/****************************************************************************-
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/****************************************************************************
2** -
3** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -
4** Contact: http://www.qt-project.org/legal -
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 Digia. For licensing terms and -
14** conditions see http://qt.digia.com/licensing. For further information -
15** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software -
20** Foundation and appearing in the file LICENSE.LGPL included in the -
21** packaging of this file. Please review the following information to -
22** ensure the GNU Lesser General Public License version 2.1 requirements -
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -
24** -
25** In addition, as a special exception, Digia gives you certain additional -
26** rights. These rights are described in the Digia Qt LGPL Exception -
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -
28** -
29** GNU General Public License Usage -
30** Alternatively, this file may be used under the terms of the GNU -
31** General Public License version 3.0 as published by the Free Software -
32** Foundation and appearing in the file LICENSE.GPL included in the -
33** packaging of this file. Please review the following information to -
34** ensure the GNU General Public License version 3.0 requirements will be -
35** met: http://www.gnu.org/copyleft/gpl.html. -
36** -
37** -
38** $QT_END_LICENSE$ -
39** -
40****************************************************************************/ -
41 -
42#include "qpixmap.h" -
43 -
44#include <private/qfont_p.h> -
45 -
46#include "qpixmap_raster_p.h" -
47#include "qnativeimage_p.h" -
48#include "qimage_p.h" -
49#include "qpaintengine.h" -
50 -
51#include "qbitmap.h" -
52#include "qimage.h" -
53#include <QBuffer> -
54#include <QImageReader> -
55#include <private/qimage_p.h> -
56#include <private/qsimd_p.h> -
57#include <private/qdrawhelper_p.h> -
58 -
59QT_BEGIN_NAMESPACE -
60 -
61QPixmap qt_toRasterPixmap(const QImage &image) -
62{ -
63 QPlatformPixmap *data = -
64 new QRasterPlatformPixmap(image.depth() == 1 -
65 ? QPlatformPixmap::BitmapType -
66 : QPlatformPixmap::PixmapType); -
67 -
68 data->fromImage(image, Qt::AutoColor); -
69 -
70 return QPixmap(data); -
71} -
72 -
73QPixmap qt_toRasterPixmap(const QPixmap &pixmap) -
74{ -
75 if (pixmap.isNull()) -
76 return QPixmap(); -
77 -
78 if (QPixmap(pixmap).data_ptr()->classId() == QPlatformPixmap::RasterClass) -
79 return pixmap; -
80 -
81 return qt_toRasterPixmap(pixmap.toImage()); -
82} -
83 -
84QRasterPlatformPixmap::QRasterPlatformPixmap(PixelType type) -
85 : QPlatformPixmap(type, RasterClass) -
86{ -
87} -
88 -
89QRasterPlatformPixmap::~QRasterPlatformPixmap() -
90{ -
91} -
92 -
93QPlatformPixmap *QRasterPlatformPixmap::createCompatiblePlatformPixmap() const -
94{ -
95 return new QRasterPlatformPixmap(pixelType()); -
96} -
97 -
98void QRasterPlatformPixmap::resize(int width, int height) -
99{ -
100 QImage::Format format; -
101 if (pixelType() == BitmapType) -
102 format = QImage::Format_MonoLSB; -
103 else -
104 format = QNativeImage::systemFormat(); -
105 -
106 image = QImage(width, height, format); -
107 w = width; -
108 h = height; -
109 d = image.depth(); -
110 is_null = (w <= 0 || h <= 0); -
111 -
112 if (pixelType() == BitmapType && !image.isNull()) { -
113 image.setColorCount(2); -
114 image.setColor(0, QColor(Qt::color0).rgba()); -
115 image.setColor(1, QColor(Qt::color1).rgba()); -
116 } -
117 -
118 setSerialNumber(image.cacheKey() >> 32); -
119} -
120 -
121bool QRasterPlatformPixmap::fromData(const uchar *buffer, uint len, const char *format, -
122 Qt::ImageConversionFlags flags) -
123{ -
124 QByteArray a = QByteArray::fromRawData(reinterpret_cast<const char *>(buffer), len); -
125 QBuffer b(&a); -
126 b.open(QIODevice::ReadOnly); -
127 QImage image = QImageReader(&b, format).read(); -
128 if (image.isNull()) -
129 return false; -
130 -
131 createPixmapForImage(image, flags, /* inplace = */true); -
132 return !isNull(); -
133} -
134 -
135void QRasterPlatformPixmap::fromImage(const QImage &sourceImage, -
136 Qt::ImageConversionFlags flags) -
137{ -
138 Q_UNUSED(flags); -
139 QImage image = sourceImage; -
140 createPixmapForImage(image, flags, /* inplace = */false); -
141} -
142 -
143void QRasterPlatformPixmap::fromImageReader(QImageReader *imageReader, -
144 Qt::ImageConversionFlags flags) -
145{ -
146 Q_UNUSED(flags); -
147 QImage image = imageReader->read(); -
148 if (image.isNull()) -
149 return; -
150 -
151 createPixmapForImage(image, flags, /* inplace = */true); -
152} -
153 -
154// from qbackingstore.cpp -
155extern void qt_scrollRectInImage(QImage &img, const QRect &rect, const QPoint &offset); -
156 -
157void QRasterPlatformPixmap::copy(const QPlatformPixmap *data, const QRect &rect) -
158{ -
159 fromImage(data->toImage(rect).copy(), Qt::NoOpaqueDetection); -
160} -
161 -
162bool QRasterPlatformPixmap::scroll(int dx, int dy, const QRect &rect) -
163{ -
164 if (!image.isNull()) -
165 qt_scrollRectInImage(image, rect, QPoint(dx, dy)); -
166 return true; -
167} -
168 -
169void QRasterPlatformPixmap::fill(const QColor &color) -
170{ -
171 uint pixel; -
172 -
173 if (image.depth() == 1) { -
174 int gray = qGray(color.rgba()); -
175 // Pick the best approximate color in the image's colortable. -
176 if (qAbs(qGray(image.color(0)) - gray) < qAbs(qGray(image.color(1)) - gray)) -
177 pixel = 0; -
178 else -
179 pixel = 1; -
180 } else if (image.depth() >= 15) { -
181 int alpha = color.alpha(); -
182 if (alpha != 255) { -
183 if (!image.hasAlphaChannel()) { -
184 QImage::Format toFormat; -
185#if !(defined(QT_COMPILER_SUPPORTS_NEON) || defined(__SSE2__)) -
186 if (image.format() == QImage::Format_RGB16) -
187 toFormat = QImage::Format_ARGB8565_Premultiplied; -
188 else if (image.format() == QImage::Format_RGB666) -
189 toFormat = QImage::Format_ARGB6666_Premultiplied; -
190 else if (image.format() == QImage::Format_RGB555) -
191 toFormat = QImage::Format_ARGB8555_Premultiplied; -
192 else if (image.format() == QImage::Format_RGB444) -
193 toFormat = QImage::Format_ARGB4444_Premultiplied; -
194 else -
195#endif -
196 toFormat = QImage::Format_ARGB32_Premultiplied; -
197 -
198 if (!image.isNull() && qt_depthForFormat(image.format()) == qt_depthForFormat(toFormat)) { -
199 image.detach(); -
200 image.d->format = toFormat; -
201 } else { -
202 image = QImage(image.width(), image.height(), toFormat); -
203 } -
204 } -
205 } -
206 pixel = PREMUL(color.rgba()); -
207 const QPixelLayout *layout = &qPixelLayouts[image.format()]; -
208 layout->convertFromARGB32PM(&pixel, &pixel, 1, layout, 0); -
209 } else { -
210 pixel = 0; -
211 // ### what about 8 bits -
212 } -
213 -
214 image.fill(pixel); -
215} -
216 -
217bool QRasterPlatformPixmap::hasAlphaChannel() const -
218{ -
219 return image.hasAlphaChannel(); -
220} -
221 -
222QImage QRasterPlatformPixmap::toImage() const -
223{ -
224 if (!image.isNull()) { -
225 QImageData *data = const_cast<QImage &>(image).data_ptr(); -
226 if (data->paintEngine && data->paintEngine->isActive() -
227 && data->paintEngine->paintDevice() == &image) -
228 { -
229 return image.copy(); -
230 } -
231 } -
232 -
233 return image; -
234} -
235 -
236QImage QRasterPlatformPixmap::toImage(const QRect &rect) const -
237{ -
238 if (rect.isNull())
partially evaluated: rect.isNull()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:55
0-55
239 return image;
never executed: return image;
0
240 -
241 QRect clipped = rect.intersected(QRect(0, 0, w, h));
executed (the execution status of this line is deduced): QRect clipped = rect.intersected(QRect(0, 0, w, h));
-
242 ifconst uint du = uint(d);
executed (the execution status of this line is deduced): const uint du = uint(d);
-
243 if ((du % 8 == 0) && (((uint(clipped.x()) * du)) % 32 == 0))
evaluated: (du % 8 == 0)
TRUEFALSE
yes
Evaluation Count:54
yes
Evaluation Count:1
evaluated: (((uint(clipped.x()) * du)) % 32 == 0)
TRUEFALSE
yes
Evaluation Count:53
yes
Evaluation Count:1
1-54
244 return QImage(image.scanLine(clipped.y()) + clipped.x() * (ddu / 8),
executed: return QImage(image.scanLine(clipped.y()) + clipped.x() * (du / 8), clipped.width(), clipped.height(), image.bytesPerLine(), image.format());
Execution Count:53
53
245 clipped.width(), clipped.height(),
executed: return QImage(image.scanLine(clipped.y()) + clipped.x() * (du / 8), clipped.width(), clipped.height(), image.bytesPerLine(), image.format());
Execution Count:53
53
246 image.bytesPerLine(), image.format());
executed: return QImage(image.scanLine(clipped.y()) + clipped.x() * (du / 8), clipped.width(), clipped.height(), image.bytesPerLine(), image.format());
Execution Count:53
53
247 else -
248 return image.copy(clipped);
executed: return image.copy(clipped);
Execution Count:2
2
249} -
250 -
251QPaintEngine* QRasterPlatformPixmap::paintEngine() const -
252{ -
253 return image.paintEngine(); -
254} -
255 -
256int QRasterPlatformPixmap::metric(QPaintDevice::PaintDeviceMetric metric) const -
257{ -
258 QImageData *d = image.d; -
259 if (!d) -
260 return 0; -
261 -
262 // override the image dpi with the screen dpi when rendering to a pixmap -
263 switch (metric) { -
264 case QPaintDevice::PdmWidth: -
265 return w; -
266 case QPaintDevice::PdmHeight: -
267 return h; -
268 case QPaintDevice::PdmWidthMM: -
269 return qRound(d->width * 25.4 / qt_defaultDpiX()); -
270 case QPaintDevice::PdmHeightMM: -
271 return qRound(d->height * 25.4 / qt_defaultDpiY()); -
272 case QPaintDevice::PdmNumColors: -
273 return d->colortable.size(); -
274 case QPaintDevice::PdmDepth: -
275 return this->d; -
276 case QPaintDevice::PdmDpiX: -
277 return qt_defaultDpiX(); -
278 case QPaintDevice::PdmPhysicalDpiX: -
279 return qt_defaultDpiX() * image.devicePixelRatio(); -
280 case QPaintDevice::PdmDpiY: -
281 return qt_defaultDpiX(); -
282 case QPaintDevice::PdmPhysicalDpiY: -
283 return qt_defaultDpiY() * image.devicePixelRatio(); -
284 default: -
285 qWarning("QRasterPlatformPixmap::metric(): Unhandled metric type %d", metric); -
286 break; -
287 } -
288 -
289 return 0; -
290} -
291 -
292void QRasterPlatformPixmap::createPixmapForImage(QImage &sourceImage, Qt::ImageConversionFlags flags, bool inPlace) -
293{ -
294 QImage::Format format; -
295 if (flags & Qt::NoFormatConversion) -
296 format = sourceImage.format(); -
297 else -
298 if (pixelType() == BitmapType) { -
299 format = QImage::Format_MonoLSB; -
300 } else { -
301 if (sourceImage.depth() == 1) { -
302 format = sourceImage.hasAlphaChannel() -
303 ? QImage::Format_ARGB32_Premultiplied -
304 : QImage::Format_RGB32; -
305 } else { -
306 QImage::Format opaqueFormat = QNativeImage::systemFormat(); -
307 QImage::Format alphaFormat = QImage::Format_ARGB32_Premultiplied; -
308 -
309#if !defined(QT_COMPILER_SUPPORTS_NEON) && !defined(__SSE2__) -
310 switch (opaqueFormat) { -
311 case QImage::Format_RGB16: -
312 alphaFormat = QImage::Format_ARGB8565_Premultiplied; -
313 break; -
314 default: // We don't care about the others... -
315 break; -
316 } -
317#endif -
318 -
319 if (!sourceImage.hasAlphaChannel()) { -
320 format = opaqueFormat; -
321 } else if ((flags & Qt::NoOpaqueDetection) == 0 -
322 && !const_cast<QImage &>(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 = sourceImage; -
337 if (!inPlace) -
338 image.detach(); -
339 if (image.d) -
340 image.d->format = QImage::Format_RGB32; -
341 } else if (inPlace && sourceImage.d->convertInPlace(format, flags)) { -
342 image = sourceImage; -
343 } else { -
344 image = sourceImage.convertToFormat(format); -
345 } -
346 -
347 if (image.d) { -
348 w = image.d->width; -
349 h = image.d->height; -
350 d = image.d->depth; -
351 } else { -
352 w = h = d = 0; -
353 } -
354 is_null = (w <= 0 || h <= 0); -
355 -
356 image.d->devicePixelRatio = sourceImage.devicePixelRatio(); -
357 setSerialNumber(image.cacheKey() >> 32); -
358} -
359 -
360QImage* QRasterPlatformPixmap::buffer() -
361{ -
362 return &image; -
363} -
364 -
365qreal QRasterPlatformPixmap::devicePixelRatio() const -
366{ -
367 return image.devicePixelRatio(); -
368} -
369 -
370void QRasterPlatformPixmap::setDevicePixelRatio(qreal scaleFactor) -
371{ -
372 image.setDevicePixelRatio(scaleFactor); -
373} -
374 -
375QT_END_NAMESPACE -
376 -
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial