Qt 4.8
qpixmapdata.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 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 "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 
51 
52 const uchar qt_pixmap_bit_mask[] = { 0x01, 0x02, 0x04, 0x08,
53  0x10, 0x20, 0x40, 0x80 };
54 
56 {
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 
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 
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) {
92  is_cached = false;
93  }
94 }
95 
97 {
98  QPixmapData *d;
100  if (gs)
101  d = gs->createPixmapData(pixelType());
102  else
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 
128  Qt::ImageConversionFlags flags)
129 {
130  const QImage image = imageReader->read();
131  fromImage(image, flags);
132 }
133 
134 bool QPixmapData::fromFile(const QString &fileName, const char *format,
135  Qt::ImageConversionFlags flags)
136 {
137  QImage image = QImageReader(fileName, format).read();
138  if (image.isNull())
139  return false;
140  fromImage(makeBitmapCompliantIfNeeded(this, image, flags), flags);
141  return !isNull();
142 }
143 
144 bool QPixmapData::fromData(const uchar *buf, uint len, const char *format, Qt::ImageConversionFlags flags)
145 {
146  QByteArray a = QByteArray::fromRawData(reinterpret_cast<const char *>(buf), len);
147  QBuffer b(&a);
149  QImage image = QImageReader(&b, format).read();
150  fromImage(makeBitmapCompliantIfNeeded(this, image, flags), flags);
151  return !isNull();
152 }
153 
154 void QPixmapData::copy(const QPixmapData *data, const QRect &rect)
155 {
157 }
158 
159 bool QPixmapData::scroll(int dx, int dy, const QRect &rect)
160 {
161  Q_UNUSED(dx);
162  Q_UNUSED(dy);
163  Q_UNUSED(rect);
164  return false;
165 }
166 
168 {
169  if (mask.size().isEmpty()) {
170  if (depth() != 1)
172  Qt::AutoColor);
173  } else {
174  QImage image = toImage();
175  const int w = image.width();
176  const int h = image.height();
177 
178  switch (image.depth()) {
179  case 1: {
180  const QImage imageMask = mask.toImage().convertToFormat(image.format());
181  for (int y = 0; y < h; ++y) {
182  const uchar *mscan = imageMask.scanLine(y);
183  uchar *tscan = image.scanLine(y);
184  int bytesPerLine = image.bytesPerLine();
185  for (int i = 0; i < bytesPerLine; ++i)
186  tscan[i] &= mscan[i];
187  }
188  break;
189  }
190  default: {
191  const QImage imageMask = mask.toImage().convertToFormat(QImage::Format_MonoLSB);
193  for (int y = 0; y < h; ++y) {
194  const uchar *mscan = imageMask.scanLine(y);
195  QRgb *tscan = (QRgb *)image.scanLine(y);
196  for (int x = 0; x < w; ++x) {
197  if (!(mscan[x>>3] & qt_pixmap_bit_mask[x&7]))
198  tscan[x] = 0;
199  }
200  }
201  break;
202  }
203  }
204  fromImage(image, Qt::AutoColor);
205  }
206 }
207 
209 {
210  if (!hasAlphaChannel())
211  return QBitmap();
212 
213  const QImage img = toImage();
214  const QImage image = (img.depth() < 32 ? img.convertToFormat(QImage::Format_ARGB32_Premultiplied) : img);
215  const int w = image.width();
216  const int h = image.height();
217 
219  if (mask.isNull()) // allocation failed
220  return QBitmap();
221 
222  mask.setColorCount(2);
223  mask.setColor(0, QColor(Qt::color0).rgba());
224  mask.setColor(1, QColor(Qt::color1).rgba());
225 
226  const int bpl = mask.bytesPerLine();
227 
228  for (int y = 0; y < h; ++y) {
229  const QRgb *src = reinterpret_cast<const QRgb*>(image.scanLine(y));
230  uchar *dest = mask.scanLine(y);
231  memset(dest, 0, bpl);
232  for (int x = 0; x < w; ++x) {
233  if (qAlpha(*src) > 0)
234  dest[x >> 3] |= qt_pixmap_bit_mask[x & 7];
235  ++src;
236  }
237  }
238 
239  return QBitmap::fromImage(mask);
240 }
241 
243  Qt::TransformationMode mode) const
244 {
245  return QPixmap::fromImage(toImage().transformed(matrix, mode));
246 }
247 
249 {
250  QImage image = toImage();
251  image.setAlphaChannel(alphaChannel.toImage());
252  fromImage(image, Qt::AutoColor);
253 }
254 
256 {
258 }
259 
261 {
262  ser_no = serNo;
263 }
264 
266 {
267  if (rect.contains(QRect(0, 0, w, h)))
268  return toImage();
269  else
270  return toImage().copy(rect);
271 }
272 
274 {
275  return 0;
276 }
277 
278 #if defined(Q_OS_SYMBIAN)
279 QVolatileImage QPixmapData::toVolatileImage() const
280 {
281  return QVolatileImage();
282 }
283 
284 void* QPixmapData::toNativeType(NativeType /* type */)
285 {
286  return 0;
287 }
288 
289 void QPixmapData::fromNativeType(void* /* pixmap */, NativeType /* typre */)
290 {
291  return;
292 }
293 #endif
294 
virtual QPixmapData * createCompatiblePixmapData() const
Definition: qpixmapdata.cpp:96
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
virtual bool fromFile(const QString &filename, const char *format, Qt::ImageConversionFlags flags)
QImage toImage() const
Converts the pixmap to a QImage.
Definition: qpixmap.cpp:542
static QPixmap fromImage(const QImage &image, Qt::ImageConversionFlags flags=Qt::AutoColor)
Converts the given image to a pixmap using the specified flags to control the conversion.
Definition: qpixmap.cpp:2197
virtual QPixmap transformed(const QTransform &matrix, Qt::TransformationMode mode) const
unsigned int QRgb
Definition: qrgb.h:53
QImage copy(const QRect &rect=QRect()) const
Returns a sub-area of the image as a new image.
Definition: qimage.cpp:1410
int type
Definition: qmetatype.cpp:239
void setColor(int i, QRgb c)
Sets the color at the given index in the color table, to the given to colorValue. ...
Definition: qimage.cpp:1850
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QSize size() const
Returns the size of the pixmap.
Definition: qpixmap.cpp:661
Q_GUI_EXPORT_INLINE int qAlpha(QRgb rgb)
Definition: qrgb.h:66
bool open(OpenMode openMode)
Reimplemented Function
Definition: qbuffer.cpp:338
bool isNull() const
Returns true if it is a null image, otherwise returns false.
Definition: qimage.cpp:1542
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
virtual void resize(int width, int height)=0
int bytesPerLine() const
Returns the number of bytes per image scanline.
Definition: qimage.cpp:1812
long ASN1_INTEGER_get ASN1_INTEGER * a
The QBuffer class provides a QIODevice interface for a QByteArray.
Definition: qbuffer.h:57
The QString class provides a Unicode character string.
Definition: qstring.h:83
virtual QBitmap mask() const
virtual bool fromData(const uchar *buffer, uint len, const char *format, Qt::ImageConversionFlags flags)
static void executePixmapDataDestructionHooks(QPixmapData *)
Format format() const
Returns the format of the image.
Definition: qimage.cpp:2305
PixelType pixelType() const
virtual QPixmap alphaChannel() const
unsigned char uchar
Definition: qglobal.h:994
The QBitmap class provides monochrome (1-bit depth) pixmaps.
Definition: qbitmap.h:55
virtual QImage toImage() const =0
void setColorCount(int)
Resizes the color table to contain colorCount entries.
Definition: qimage.cpp:2275
virtual ~QPixmapData()
Definition: qpixmapdata.cpp:82
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
virtual void fromImage(const QImage &image, Qt::ImageConversionFlags flags)=0
static QByteArray fromRawData(const char *, int size)
Constructs a QByteArray that uses the first size bytes of the data array.
TransformationMode
Definition: qnamespace.h:1510
The QImage class provides a hardware-independent image representation that allows direct access to th...
Definition: qimage.h:87
static const char * data(const QByteArray &arr)
unsigned int uint
Definition: qglobal.h:996
int depth() const
Returns the depth of the image.
Definition: qimage.cpp:1620
PixelType type
QPixmapData(PixelType pixelType, int classId)
Definition: qpixmapdata.cpp:68
bool contains(const QPoint &p, bool proper=false) const
Returns true if the given point is inside or on the edge of the rectangle, otherwise returns false...
Definition: qrect.cpp:1101
The QImageReader class provides a format independent interface for reading images from files or other...
Definition: qimagereader.h:62
virtual bool hasAlphaChannel() const =0
int width() const
Returns the width of the image.
Definition: qimage.cpp:1557
QImage convertToFormat(Format f, Qt::ImageConversionFlags flags=Qt::AutoColor) const Q_REQUIRED_RESULT
Returns a copy of the image in the given format.
Definition: qimage.cpp:3966
int depth() const
const uchar qt_pixmap_bit_mask[]
Definition: qpixmapdata.cpp:52
QAtomicInt ref
virtual void copy(const QPixmapData *data, const QRect &rect)
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
static QPixmapData * create(int w, int h, PixelType type)
Definition: qpixmapdata.cpp:55
virtual void setMask(const QBitmap &mask)
int height() const
Returns the height of the image.
Definition: qimage.cpp:1572
The QPixmap class is an off-screen image representation that can be used as a paint device...
Definition: qpixmap.h:71
static QImage makeBitmapCompliantIfNeeded(QPixmapData *d, const QImage &image, Qt::ImageConversionFlags flags)
virtual QImage * buffer()
static QBitmap fromImage(const QImage &image, Qt::ImageConversionFlags flags=Qt::AutoColor)
Returns a copy of the given image converted to a bitmap using the specified image conversion flags...
Definition: qbitmap.cpp:281
bool isNull() const
virtual void setAlphaChannel(const QPixmap &alphaChannel)
void invertPixels(InvertMode=InvertRgb)
Inverts all pixel values in the image.
Definition: qimage.cpp:2179
virtual QPixmapData * createPixmapData(QPixmapData::PixelType type) const =0
void setAlphaChannel(const QImage &alphaChannel)
Sets the alpha channel of this image to the given alphaChannel.
Definition: qimage.cpp:6329
bool isEmpty() const
Returns true if either of the width and height is less than or equal to 0; otherwise returns false...
Definition: qsize.h:120
static QGraphicsSystem * graphicsSystem()
void setSerialNumber(int serNo)
static QPixmapData * createDefaultPixmapData(QPixmapData::PixelType type)
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
static QString fileName(const QString &fileUrl)
QImage read()
Reads an image from the device.
QRgb color(int i) const
Returns the color in the color table at index i.
Definition: qimage.cpp:1829
QRgb rgb() const
Returns the RGB value of the color.
Definition: qcolor.cpp:1051
virtual bool scroll(int dx, int dy, const QRect &rect)
uchar * scanLine(int)
Returns a pointer to the pixel data at the scanline with index i.
Definition: qimage.cpp:1886
virtual void fromImageReader(QImageReader *imageReader, Qt::ImageConversionFlags flags)
The QTransform class specifies 2D transformations of a coordinate system.
Definition: qtransform.h:65