1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
6 | **
|
---|
7 | ** This file is part of the QtGui module of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at qt-info@nokia.com.
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include "qpixmap.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/qwidget_p.h>
|
---|
56 | #include <private/qdrawhelper_p.h>
|
---|
57 |
|
---|
58 | QT_BEGIN_NAMESPACE
|
---|
59 |
|
---|
60 | const uchar qt_pixmap_bit_mask[] = { 0x01, 0x02, 0x04, 0x08,
|
---|
61 | 0x10, 0x20, 0x40, 0x80 };
|
---|
62 |
|
---|
63 | QPixmap qt_toRasterPixmap(const QImage &image)
|
---|
64 | {
|
---|
65 | QPixmapData *data =
|
---|
66 | new QRasterPixmapData(image.depth() == 1
|
---|
67 | ? QPixmapData::BitmapType
|
---|
68 | : QPixmapData::PixmapType);
|
---|
69 |
|
---|
70 | data->fromImage(image, Qt::AutoColor);
|
---|
71 |
|
---|
72 | return QPixmap(data);
|
---|
73 | }
|
---|
74 |
|
---|
75 | QPixmap qt_toRasterPixmap(const QPixmap &pixmap)
|
---|
76 | {
|
---|
77 | if (pixmap.isNull())
|
---|
78 | return QPixmap();
|
---|
79 |
|
---|
80 | if (QPixmap(pixmap).data_ptr()->classId() == QPixmapData::RasterClass)
|
---|
81 | return pixmap;
|
---|
82 |
|
---|
83 | return qt_toRasterPixmap(pixmap.toImage());
|
---|
84 | }
|
---|
85 |
|
---|
86 | QRasterPixmapData::QRasterPixmapData(PixelType type)
|
---|
87 | : QPixmapData(type, RasterClass)
|
---|
88 | {
|
---|
89 | }
|
---|
90 |
|
---|
91 | QRasterPixmapData::~QRasterPixmapData()
|
---|
92 | {
|
---|
93 | }
|
---|
94 |
|
---|
95 | QPixmapData *QRasterPixmapData::createCompatiblePixmapData() const
|
---|
96 | {
|
---|
97 | return new QRasterPixmapData(pixelType());
|
---|
98 | }
|
---|
99 |
|
---|
100 | void QRasterPixmapData::resize(int width, int height)
|
---|
101 | {
|
---|
102 | QImage::Format format;
|
---|
103 | #ifdef Q_WS_QWS
|
---|
104 | if (pixelType() == BitmapType) {
|
---|
105 | format = QImage::Format_Mono;
|
---|
106 | } else {
|
---|
107 | format = QScreen::instance()->pixelFormat();
|
---|
108 | if (format == QImage::Format_Invalid)
|
---|
109 | format = QImage::Format_ARGB32_Premultiplied;
|
---|
110 | else if (format == QImage::Format_Indexed8) // currently not supported
|
---|
111 | format = QImage::Format_RGB444;
|
---|
112 | }
|
---|
113 | #else
|
---|
114 | if (pixelType() == BitmapType)
|
---|
115 | format = QImage::Format_MonoLSB;
|
---|
116 | else
|
---|
117 | format = QNativeImage::systemFormat();
|
---|
118 | #endif
|
---|
119 |
|
---|
120 | image = QImage(width, height, format);
|
---|
121 | w = width;
|
---|
122 | h = height;
|
---|
123 | d = image.depth();
|
---|
124 | is_null = (w <= 0 || h <= 0);
|
---|
125 |
|
---|
126 | if (pixelType() == BitmapType && !image.isNull()) {
|
---|
127 | image.setColorCount(2);
|
---|
128 | image.setColor(0, QColor(Qt::color0).rgba());
|
---|
129 | image.setColor(1, QColor(Qt::color1).rgba());
|
---|
130 | }
|
---|
131 |
|
---|
132 | setSerialNumber(image.serialNumber());
|
---|
133 | }
|
---|
134 |
|
---|
135 | bool QRasterPixmapData::fromData(const uchar *buffer, uint len, const char *format,
|
---|
136 | Qt::ImageConversionFlags flags)
|
---|
137 | {
|
---|
138 | QByteArray a = QByteArray::fromRawData(reinterpret_cast<const char *>(buffer), len);
|
---|
139 | QBuffer b(&a);
|
---|
140 | b.open(QIODevice::ReadOnly);
|
---|
141 | QImage image = QImageReader(&b, format).read();
|
---|
142 | if (image.isNull())
|
---|
143 | return false;
|
---|
144 |
|
---|
145 | createPixmapForImage(image, flags, /* inplace = */true);
|
---|
146 | return !isNull();
|
---|
147 | }
|
---|
148 |
|
---|
149 | void QRasterPixmapData::fromImage(const QImage &sourceImage,
|
---|
150 | Qt::ImageConversionFlags flags)
|
---|
151 | {
|
---|
152 | Q_UNUSED(flags);
|
---|
153 | QImage image = sourceImage;
|
---|
154 | createPixmapForImage(image, flags, /* inplace = */false);
|
---|
155 | }
|
---|
156 |
|
---|
157 | void QRasterPixmapData::fromImageReader(QImageReader *imageReader,
|
---|
158 | Qt::ImageConversionFlags flags)
|
---|
159 | {
|
---|
160 | Q_UNUSED(flags);
|
---|
161 | QImage image = imageReader->read();
|
---|
162 | if (image.isNull())
|
---|
163 | return;
|
---|
164 |
|
---|
165 | createPixmapForImage(image, flags, /* inplace = */true);
|
---|
166 | }
|
---|
167 |
|
---|
168 | // from qwindowsurface.cpp
|
---|
169 | extern void qt_scrollRectInImage(QImage &img, const QRect &rect, const QPoint &offset);
|
---|
170 |
|
---|
171 | void QRasterPixmapData::copy(const QPixmapData *data, const QRect &rect)
|
---|
172 | {
|
---|
173 | fromImage(data->toImage(rect).copy(), Qt::NoOpaqueDetection);
|
---|
174 | }
|
---|
175 |
|
---|
176 | bool QRasterPixmapData::scroll(int dx, int dy, const QRect &rect)
|
---|
177 | {
|
---|
178 | if (!image.isNull())
|
---|
179 | qt_scrollRectInImage(image, rect, QPoint(dx, dy));
|
---|
180 | return true;
|
---|
181 | }
|
---|
182 |
|
---|
183 | void QRasterPixmapData::fill(const QColor &color)
|
---|
184 | {
|
---|
185 | uint pixel;
|
---|
186 |
|
---|
187 | if (image.depth() == 1) {
|
---|
188 | int gray = qGray(color.rgba());
|
---|
189 | // Pick the best approximate color in the image's colortable.
|
---|
190 | if (qAbs(qGray(image.color(0)) - gray) < qAbs(qGray(image.color(1)) - gray))
|
---|
191 | pixel = 0;
|
---|
192 | else
|
---|
193 | pixel = 1;
|
---|
194 | } else if (image.depth() >= 15) {
|
---|
195 | int alpha = color.alpha();
|
---|
196 | if (alpha != 255) {
|
---|
197 | if (!image.hasAlphaChannel()) {
|
---|
198 | QImage::Format toFormat;
|
---|
199 | #if !(defined(QT_HAVE_NEON) || defined(QT_ALWAYS_HAVE_SSE2))
|
---|
200 | if (image.format() == QImage::Format_RGB16)
|
---|
201 | toFormat = QImage::Format_ARGB8565_Premultiplied;
|
---|
202 | else if (image.format() == QImage::Format_RGB666)
|
---|
203 | toFormat = QImage::Format_ARGB6666_Premultiplied;
|
---|
204 | else if (image.format() == QImage::Format_RGB555)
|
---|
205 | toFormat = QImage::Format_ARGB8555_Premultiplied;
|
---|
206 | else if (image.format() == QImage::Format_RGB444)
|
---|
207 | toFormat = QImage::Format_ARGB4444_Premultiplied;
|
---|
208 | else
|
---|
209 | #endif
|
---|
210 | toFormat = QImage::Format_ARGB32_Premultiplied;
|
---|
211 | image = QImage(image.width(), image.height(), toFormat);
|
---|
212 | }
|
---|
213 |
|
---|
214 | switch (image.format()) {
|
---|
215 | case QImage::Format_ARGB8565_Premultiplied:
|
---|
216 | pixel = qargb8565(color.rgba()).rawValue();
|
---|
217 | break;
|
---|
218 | case QImage::Format_ARGB8555_Premultiplied:
|
---|
219 | pixel = qargb8555(color.rgba()).rawValue();
|
---|
220 | break;
|
---|
221 | case QImage::Format_ARGB6666_Premultiplied:
|
---|
222 | pixel = qargb6666(color.rgba()).rawValue();
|
---|
223 | break;
|
---|
224 | case QImage::Format_ARGB4444_Premultiplied:
|
---|
225 | pixel = qargb4444(color.rgba()).rawValue();
|
---|
226 | break;
|
---|
227 | default:
|
---|
228 | pixel = PREMUL(color.rgba());
|
---|
229 | break;
|
---|
230 | }
|
---|
231 | } else {
|
---|
232 | switch (image.format()) {
|
---|
233 | case QImage::Format_RGB16:
|
---|
234 | pixel = qt_colorConvert<quint16, quint32>(color.rgba(), 0);
|
---|
235 | break;
|
---|
236 | case QImage::Format_RGB444:
|
---|
237 | pixel = qrgb444(color.rgba()).rawValue();
|
---|
238 | break;
|
---|
239 | case QImage::Format_RGB555:
|
---|
240 | pixel = qrgb555(color.rgba()).rawValue();
|
---|
241 | break;
|
---|
242 | case QImage::Format_RGB666:
|
---|
243 | pixel = qrgb666(color.rgba()).rawValue();
|
---|
244 | break;
|
---|
245 | case QImage::Format_RGB888:
|
---|
246 | pixel = qrgb888(color.rgba()).rawValue();
|
---|
247 | break;
|
---|
248 | default:
|
---|
249 | pixel = color.rgba();
|
---|
250 | break;
|
---|
251 | }
|
---|
252 | }
|
---|
253 | } else {
|
---|
254 | pixel = 0;
|
---|
255 | // ### what about 8 bits
|
---|
256 | }
|
---|
257 |
|
---|
258 | image.fill(pixel);
|
---|
259 | }
|
---|
260 |
|
---|
261 | void QRasterPixmapData::setMask(const QBitmap &mask)
|
---|
262 | {
|
---|
263 | if (mask.size().isEmpty()) {
|
---|
264 | if (image.depth() != 1) { // hw: ????
|
---|
265 | image = image.convertToFormat(QImage::Format_RGB32);
|
---|
266 | }
|
---|
267 | } else {
|
---|
268 | const int w = image.width();
|
---|
269 | const int h = image.height();
|
---|
270 |
|
---|
271 | switch (image.depth()) {
|
---|
272 | case 1: {
|
---|
273 | const QImage imageMask = mask.toImage().convertToFormat(image.format());
|
---|
274 | for (int y = 0; y < h; ++y) {
|
---|
275 | const uchar *mscan = imageMask.scanLine(y);
|
---|
276 | uchar *tscan = image.scanLine(y);
|
---|
277 | int bytesPerLine = image.bytesPerLine();
|
---|
278 | for (int i = 0; i < bytesPerLine; ++i)
|
---|
279 | tscan[i] &= mscan[i];
|
---|
280 | }
|
---|
281 | break;
|
---|
282 | }
|
---|
283 | default: {
|
---|
284 | const QImage imageMask = mask.toImage().convertToFormat(QImage::Format_MonoLSB);
|
---|
285 | image = image.convertToFormat(QImage::Format_ARGB32_Premultiplied);
|
---|
286 | for (int y = 0; y < h; ++y) {
|
---|
287 | const uchar *mscan = imageMask.scanLine(y);
|
---|
288 | QRgb *tscan = (QRgb *)image.scanLine(y);
|
---|
289 | for (int x = 0; x < w; ++x) {
|
---|
290 | if (!(mscan[x>>3] & qt_pixmap_bit_mask[x&7]))
|
---|
291 | tscan[x] = 0;
|
---|
292 | }
|
---|
293 | }
|
---|
294 | break;
|
---|
295 | }
|
---|
296 | }
|
---|
297 | }
|
---|
298 | }
|
---|
299 |
|
---|
300 | bool QRasterPixmapData::hasAlphaChannel() const
|
---|
301 | {
|
---|
302 | return image.hasAlphaChannel();
|
---|
303 | }
|
---|
304 |
|
---|
305 | QImage QRasterPixmapData::toImage() const
|
---|
306 | {
|
---|
307 | if (!image.isNull()) {
|
---|
308 | QImageData *data = const_cast<QImage &>(image).data_ptr();
|
---|
309 | if (data->paintEngine && data->paintEngine->isActive()
|
---|
310 | && data->paintEngine->paintDevice() == &image)
|
---|
311 | {
|
---|
312 | return image.copy();
|
---|
313 | }
|
---|
314 | }
|
---|
315 |
|
---|
316 | return image;
|
---|
317 | }
|
---|
318 |
|
---|
319 | QImage QRasterPixmapData::toImage(const QRect &rect) const
|
---|
320 | {
|
---|
321 | if (rect.isNull())
|
---|
322 | return image;
|
---|
323 |
|
---|
324 | QRect clipped = rect.intersected(QRect(0, 0, w, h));
|
---|
325 | if (d % 8 == 0)
|
---|
326 | return QImage(image.scanLine(clipped.y()) + clipped.x() * (d / 8),
|
---|
327 | clipped.width(), clipped.height(),
|
---|
328 | image.bytesPerLine(), image.format());
|
---|
329 | else
|
---|
330 | return image.copy(clipped);
|
---|
331 | }
|
---|
332 |
|
---|
333 | void QRasterPixmapData::setAlphaChannel(const QPixmap &alphaChannel)
|
---|
334 | {
|
---|
335 | image.setAlphaChannel(alphaChannel.toImage());
|
---|
336 | }
|
---|
337 |
|
---|
338 | QPaintEngine* QRasterPixmapData::paintEngine() const
|
---|
339 | {
|
---|
340 | return image.paintEngine();
|
---|
341 | }
|
---|
342 |
|
---|
343 | Q_GUI_EXPORT extern int qt_defaultDpiX();
|
---|
344 | Q_GUI_EXPORT extern int qt_defaultDpiY();
|
---|
345 |
|
---|
346 | int QRasterPixmapData::metric(QPaintDevice::PaintDeviceMetric metric) const
|
---|
347 | {
|
---|
348 | QImageData *d = image.d;
|
---|
349 | if (!d)
|
---|
350 | return 0;
|
---|
351 |
|
---|
352 | // override the image dpi with the screen dpi when rendering to a pixmap
|
---|
353 | switch (metric) {
|
---|
354 | case QPaintDevice::PdmWidth:
|
---|
355 | return w;
|
---|
356 | case QPaintDevice::PdmHeight:
|
---|
357 | return h;
|
---|
358 | case QPaintDevice::PdmWidthMM:
|
---|
359 | return qRound(d->width * 25.4 / qt_defaultDpiX());
|
---|
360 | case QPaintDevice::PdmHeightMM:
|
---|
361 | return qRound(d->height * 25.4 / qt_defaultDpiY());
|
---|
362 | case QPaintDevice::PdmNumColors:
|
---|
363 | return d->colortable.size();
|
---|
364 | case QPaintDevice::PdmDepth:
|
---|
365 | return this->d;
|
---|
366 | case QPaintDevice::PdmDpiX: // fall-through
|
---|
367 | case QPaintDevice::PdmPhysicalDpiX:
|
---|
368 | return qt_defaultDpiX();
|
---|
369 | case QPaintDevice::PdmDpiY: // fall-through
|
---|
370 | case QPaintDevice::PdmPhysicalDpiY:
|
---|
371 | return qt_defaultDpiY();
|
---|
372 | default:
|
---|
373 | qWarning("QRasterPixmapData::metric(): Unhandled metric type %d", metric);
|
---|
374 | break;
|
---|
375 | }
|
---|
376 |
|
---|
377 | return 0;
|
---|
378 | }
|
---|
379 |
|
---|
380 | void QRasterPixmapData::createPixmapForImage(QImage &sourceImage, Qt::ImageConversionFlags flags, bool inPlace)
|
---|
381 | {
|
---|
382 | QImage::Format format;
|
---|
383 | #ifdef Q_WS_QWS
|
---|
384 | if (pixelType() == BitmapType) {
|
---|
385 | format = QImage::Format_Mono;
|
---|
386 | } else {
|
---|
387 | format = QScreen::instance()->pixelFormat();
|
---|
388 | if (format == QImage::Format_Invalid)
|
---|
389 | format = QImage::Format_ARGB32_Premultiplied;
|
---|
390 | else if (format == QImage::Format_Indexed8) // currently not supported
|
---|
391 | format = QImage::Format_RGB444;
|
---|
392 | }
|
---|
393 |
|
---|
394 | if (sourceImage.hasAlphaChannel()
|
---|
395 | && ((flags & Qt::NoOpaqueDetection)
|
---|
396 | || const_cast<QImage &>(sourceImage).data_ptr()->checkForAlphaPixels())) {
|
---|
397 | switch (format) {
|
---|
398 | case QImage::Format_RGB16:
|
---|
399 | format = QImage::Format_ARGB8565_Premultiplied;
|
---|
400 | break;
|
---|
401 | case QImage::Format_RGB666:
|
---|
402 | format = QImage::Format_ARGB6666_Premultiplied;
|
---|
403 | break;
|
---|
404 | case QImage::Format_RGB555:
|
---|
405 | format = QImage::Format_ARGB8555_Premultiplied;
|
---|
406 | break;
|
---|
407 | case QImage::Format_RGB444:
|
---|
408 | format = QImage::Format_ARGB4444_Premultiplied;
|
---|
409 | break;
|
---|
410 | default:
|
---|
411 | format = QImage::Format_ARGB32_Premultiplied;
|
---|
412 | break;
|
---|
413 | }
|
---|
414 | } else if (format == QImage::Format_Invalid) {
|
---|
415 | format = QImage::Format_ARGB32_Premultiplied;
|
---|
416 | }
|
---|
417 | #else
|
---|
418 | if (pixelType() == BitmapType) {
|
---|
419 | format = QImage::Format_MonoLSB;
|
---|
420 | } else {
|
---|
421 | if (sourceImage.depth() == 1) {
|
---|
422 | format = sourceImage.hasAlphaChannel()
|
---|
423 | ? QImage::Format_ARGB32_Premultiplied
|
---|
424 | : QImage::Format_RGB32;
|
---|
425 | } else {
|
---|
426 | QImage::Format opaqueFormat = QNativeImage::systemFormat();
|
---|
427 | QImage::Format alphaFormat = QImage::Format_ARGB32_Premultiplied;
|
---|
428 |
|
---|
429 | #if !defined(QT_HAVE_NEON) && !defined(QT_ALWAYS_HAVE_SSE2)
|
---|
430 | switch (opaqueFormat) {
|
---|
431 | case QImage::Format_RGB16:
|
---|
432 | alphaFormat = QImage::Format_ARGB8565_Premultiplied;
|
---|
433 | break;
|
---|
434 | default: // We don't care about the others...
|
---|
435 | break;
|
---|
436 | }
|
---|
437 | #endif
|
---|
438 |
|
---|
439 | if (!sourceImage.hasAlphaChannel()) {
|
---|
440 | format = opaqueFormat;
|
---|
441 | } else if ((flags & Qt::NoOpaqueDetection) == 0
|
---|
442 | && !const_cast<QImage &>(sourceImage).data_ptr()->checkForAlphaPixels())
|
---|
443 | {
|
---|
444 | // image has alpha format but is really opaque, so try to do a
|
---|
445 | // more efficient conversion
|
---|
446 | if (sourceImage.format() == QImage::Format_ARGB32
|
---|
447 | || sourceImage.format() == QImage::Format_ARGB32_Premultiplied)
|
---|
448 | {
|
---|
449 | if (!inPlace)
|
---|
450 | sourceImage.detach();
|
---|
451 | sourceImage.d->format = QImage::Format_RGB32;
|
---|
452 | }
|
---|
453 | format = opaqueFormat;
|
---|
454 | } else {
|
---|
455 | format = alphaFormat;
|
---|
456 | }
|
---|
457 | }
|
---|
458 | }
|
---|
459 | #endif
|
---|
460 |
|
---|
461 | if (inPlace && sourceImage.d->convertInPlace(format, flags)) {
|
---|
462 | image = sourceImage;
|
---|
463 | } else {
|
---|
464 | image = sourceImage.convertToFormat(format);
|
---|
465 | }
|
---|
466 |
|
---|
467 | if (image.d) {
|
---|
468 | w = image.d->width;
|
---|
469 | h = image.d->height;
|
---|
470 | d = image.d->depth;
|
---|
471 | } else {
|
---|
472 | w = h = d = 0;
|
---|
473 | }
|
---|
474 | is_null = (w <= 0 || h <= 0);
|
---|
475 |
|
---|
476 | setSerialNumber(image.serialNumber());
|
---|
477 | }
|
---|
478 |
|
---|
479 | QImage* QRasterPixmapData::buffer()
|
---|
480 | {
|
---|
481 | return ℑ
|
---|
482 | }
|
---|
483 |
|
---|
484 | QT_END_NAMESPACE
|
---|