1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2010 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 "qpixmapdata_p.h"
|
---|
43 | #include <QtCore/qbuffer.h>
|
---|
44 | #include <QtGui/qbitmap.h>
|
---|
45 | #include <QtGui/qimagereader.h>
|
---|
46 | #include <private/qgraphicssystem_p.h>
|
---|
47 | #include <private/qapplication_p.h>
|
---|
48 | #include <private/qimagepixmapcleanuphooks_p.h>
|
---|
49 |
|
---|
50 | QT_BEGIN_NAMESPACE
|
---|
51 |
|
---|
52 | const uchar qt_pixmap_bit_mask[] = { 0x01, 0x02, 0x04, 0x08,
|
---|
53 | 0x10, 0x20, 0x40, 0x80 };
|
---|
54 |
|
---|
55 | QPixmapData *QPixmapData::create(int w, int h, PixelType type)
|
---|
56 | {
|
---|
57 | QPixmapData *data;
|
---|
58 | QGraphicsSystem* gs = QApplicationPrivate::graphicsSystem();
|
---|
59 | if (gs)
|
---|
60 | data = gs->createPixmapData(static_cast<QPixmapData::PixelType>(type));
|
---|
61 | else
|
---|
62 | data = QGraphicsSystem::createDefaultPixmapData(static_cast<QPixmapData::PixelType>(type));
|
---|
63 | data->resize(w, h);
|
---|
64 | return data;
|
---|
65 | }
|
---|
66 |
|
---|
67 |
|
---|
68 | QPixmapData::QPixmapData(PixelType pixelType, int objectId)
|
---|
69 | : w(0),
|
---|
70 | h(0),
|
---|
71 | d(0),
|
---|
72 | is_null(true),
|
---|
73 | ref(0),
|
---|
74 | detach_no(0),
|
---|
75 | type(pixelType),
|
---|
76 | id(objectId),
|
---|
77 | ser_no(0),
|
---|
78 | is_cached(false)
|
---|
79 | {
|
---|
80 | }
|
---|
81 |
|
---|
82 | QPixmapData::~QPixmapData()
|
---|
83 | {
|
---|
84 | // Sometimes the pixmap cleanup hooks will be called from derrived classes, which will
|
---|
85 | // then set is_cached to false. For example, on X11 QtOpenGL needs to delete the GLXPixmap
|
---|
86 | // or EGL Pixmap Surface for a given pixmap _before_ the native X11 pixmap is deleted,
|
---|
87 | // otherwise some drivers will leak the GL surface. In this case, QX11PixmapData will
|
---|
88 | // call the cleanup hooks itself before deleting the native pixmap and set is_cached to
|
---|
89 | // false.
|
---|
90 | if (is_cached) {
|
---|
91 | QImagePixmapCleanupHooks::executePixmapDataDestructionHooks(this);
|
---|
92 | is_cached = false;
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | QPixmapData *QPixmapData::createCompatiblePixmapData() const
|
---|
97 | {
|
---|
98 | QPixmapData *d;
|
---|
99 | QGraphicsSystem *gs = QApplicationPrivate::graphicsSystem();
|
---|
100 | if (gs)
|
---|
101 | d = gs->createPixmapData(pixelType());
|
---|
102 | else
|
---|
103 | d = QGraphicsSystem::createDefaultPixmapData(pixelType());
|
---|
104 | return d;
|
---|
105 | }
|
---|
106 |
|
---|
107 | static QImage makeBitmapCompliantIfNeeded(QPixmapData *d, const QImage &image, Qt::ImageConversionFlags flags)
|
---|
108 | {
|
---|
109 | if (d->pixelType() == QPixmapData::BitmapType) {
|
---|
110 | QImage img = image.convertToFormat(QImage::Format_MonoLSB, flags);
|
---|
111 |
|
---|
112 | // make sure image.color(0) == Qt::color0 (white)
|
---|
113 | // and image.color(1) == Qt::color1 (black)
|
---|
114 | const QRgb c0 = QColor(Qt::black).rgb();
|
---|
115 | const QRgb c1 = QColor(Qt::white).rgb();
|
---|
116 | if (img.color(0) == c0 && img.color(1) == c1) {
|
---|
117 | img.invertPixels();
|
---|
118 | img.setColor(0, c1);
|
---|
119 | img.setColor(1, c0);
|
---|
120 | }
|
---|
121 | return img;
|
---|
122 | }
|
---|
123 |
|
---|
124 | return image;
|
---|
125 | }
|
---|
126 |
|
---|
127 | bool QPixmapData::fromFile(const QString &fileName, const char *format,
|
---|
128 | Qt::ImageConversionFlags flags)
|
---|
129 | {
|
---|
130 | QImage image = QImageReader(fileName, format).read();
|
---|
131 | if (image.isNull())
|
---|
132 | return false;
|
---|
133 | fromImage(makeBitmapCompliantIfNeeded(this, image, flags), flags);
|
---|
134 | return !isNull();
|
---|
135 | }
|
---|
136 |
|
---|
137 | bool QPixmapData::fromData(const uchar *buf, uint len, const char *format, Qt::ImageConversionFlags flags)
|
---|
138 | {
|
---|
139 | QByteArray a = QByteArray::fromRawData(reinterpret_cast<const char *>(buf), len);
|
---|
140 | QBuffer b(&a);
|
---|
141 | b.open(QIODevice::ReadOnly);
|
---|
142 | QImage image = QImageReader(&b, format).read();
|
---|
143 | fromImage(makeBitmapCompliantIfNeeded(this, image, flags), flags);
|
---|
144 | return !isNull();
|
---|
145 | }
|
---|
146 |
|
---|
147 | void QPixmapData::copy(const QPixmapData *data, const QRect &rect)
|
---|
148 | {
|
---|
149 | fromImage(data->toImage().copy(rect), Qt::AutoColor);
|
---|
150 | }
|
---|
151 |
|
---|
152 | bool QPixmapData::scroll(int dx, int dy, const QRect &rect)
|
---|
153 | {
|
---|
154 | Q_UNUSED(dx);
|
---|
155 | Q_UNUSED(dy);
|
---|
156 | Q_UNUSED(rect);
|
---|
157 | return false;
|
---|
158 | }
|
---|
159 |
|
---|
160 | void QPixmapData::setMask(const QBitmap &mask)
|
---|
161 | {
|
---|
162 | if (mask.size().isEmpty()) {
|
---|
163 | if (depth() != 1)
|
---|
164 | fromImage(toImage().convertToFormat(QImage::Format_RGB32),
|
---|
165 | Qt::AutoColor);
|
---|
166 | } else {
|
---|
167 | QImage image = toImage();
|
---|
168 | const int w = image.width();
|
---|
169 | const int h = image.height();
|
---|
170 |
|
---|
171 | switch (image.depth()) {
|
---|
172 | case 1: {
|
---|
173 | const QImage imageMask = mask.toImage().convertToFormat(image.format());
|
---|
174 | for (int y = 0; y < h; ++y) {
|
---|
175 | const uchar *mscan = imageMask.scanLine(y);
|
---|
176 | uchar *tscan = image.scanLine(y);
|
---|
177 | int bytesPerLine = image.bytesPerLine();
|
---|
178 | for (int i = 0; i < bytesPerLine; ++i)
|
---|
179 | tscan[i] &= mscan[i];
|
---|
180 | }
|
---|
181 | break;
|
---|
182 | }
|
---|
183 | default: {
|
---|
184 | const QImage imageMask = mask.toImage().convertToFormat(QImage::Format_MonoLSB);
|
---|
185 | image = image.convertToFormat(QImage::Format_ARGB32_Premultiplied);
|
---|
186 | for (int y = 0; y < h; ++y) {
|
---|
187 | const uchar *mscan = imageMask.scanLine(y);
|
---|
188 | QRgb *tscan = (QRgb *)image.scanLine(y);
|
---|
189 | for (int x = 0; x < w; ++x) {
|
---|
190 | if (!(mscan[x>>3] & qt_pixmap_bit_mask[x&7]))
|
---|
191 | tscan[x] = 0;
|
---|
192 | }
|
---|
193 | }
|
---|
194 | break;
|
---|
195 | }
|
---|
196 | }
|
---|
197 | fromImage(image, Qt::AutoColor);
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | QBitmap QPixmapData::mask() const
|
---|
202 | {
|
---|
203 | if (!hasAlphaChannel())
|
---|
204 | return QBitmap();
|
---|
205 |
|
---|
206 | const QImage img = toImage();
|
---|
207 | const QImage image = (img.depth() < 32 ? img.convertToFormat(QImage::Format_ARGB32_Premultiplied) : img);
|
---|
208 | const int w = image.width();
|
---|
209 | const int h = image.height();
|
---|
210 |
|
---|
211 | QImage mask(w, h, QImage::Format_MonoLSB);
|
---|
212 | if (mask.isNull()) // allocation failed
|
---|
213 | return QBitmap();
|
---|
214 |
|
---|
215 | mask.setColorCount(2);
|
---|
216 | mask.setColor(0, QColor(Qt::color0).rgba());
|
---|
217 | mask.setColor(1, QColor(Qt::color1).rgba());
|
---|
218 |
|
---|
219 | const int bpl = mask.bytesPerLine();
|
---|
220 |
|
---|
221 | for (int y = 0; y < h; ++y) {
|
---|
222 | const QRgb *src = reinterpret_cast<const QRgb*>(image.scanLine(y));
|
---|
223 | uchar *dest = mask.scanLine(y);
|
---|
224 | memset(dest, 0, bpl);
|
---|
225 | for (int x = 0; x < w; ++x) {
|
---|
226 | if (qAlpha(*src) > 0)
|
---|
227 | dest[x >> 3] |= qt_pixmap_bit_mask[x & 7];
|
---|
228 | ++src;
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | return QBitmap::fromImage(mask);
|
---|
233 | }
|
---|
234 |
|
---|
235 | QPixmap QPixmapData::transformed(const QTransform &matrix,
|
---|
236 | Qt::TransformationMode mode) const
|
---|
237 | {
|
---|
238 | return QPixmap::fromImage(toImage().transformed(matrix, mode));
|
---|
239 | }
|
---|
240 |
|
---|
241 | void QPixmapData::setAlphaChannel(const QPixmap &alphaChannel)
|
---|
242 | {
|
---|
243 | QImage image = toImage();
|
---|
244 | image.setAlphaChannel(alphaChannel.toImage());
|
---|
245 | fromImage(image, Qt::AutoColor);
|
---|
246 | }
|
---|
247 |
|
---|
248 | QPixmap QPixmapData::alphaChannel() const
|
---|
249 | {
|
---|
250 | return QPixmap::fromImage(toImage().alphaChannel());
|
---|
251 | }
|
---|
252 |
|
---|
253 | void QPixmapData::setSerialNumber(int serNo)
|
---|
254 | {
|
---|
255 | ser_no = serNo;
|
---|
256 | }
|
---|
257 |
|
---|
258 | QImage* QPixmapData::buffer()
|
---|
259 | {
|
---|
260 | return 0;
|
---|
261 | }
|
---|
262 |
|
---|
263 | #if defined(Q_OS_SYMBIAN)
|
---|
264 | void* QPixmapData::toNativeType(NativeType /* type */)
|
---|
265 | {
|
---|
266 | return 0;
|
---|
267 | }
|
---|
268 |
|
---|
269 | void QPixmapData::fromNativeType(void* /* pixmap */, NativeType /* typre */)
|
---|
270 | {
|
---|
271 | return;
|
---|
272 | }
|
---|
273 | #endif
|
---|
274 |
|
---|
275 | QT_END_NAMESPACE
|
---|