source: trunk/src/gui/image/qpixmapdata.cpp@ 605

Last change on this file since 605 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

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