Qt 4.8
qvolatileimage.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 "qvolatileimage_p.h"
43 #include "qvolatileimagedata_p.h"
44 #include <QtGui/private/qpaintengine_raster_p.h>
45 #include <QtGui/private/qpixmapdata_p.h>
46 
48 
50 {
51 public:
54 };
55 
57 {
59 
60 public:
62  bool begin(QPaintDevice *device);
63  bool end();
64  void drawPixmap(const QPointF &p, const QPixmap &pm);
65  void drawPixmap(const QRectF &r, const QPixmap &pm, const QRectF &sr);
66 };
67 
69  : d(new QVolatileImageData)
70 {
71 }
72 
74  : d(new QVolatileImageData(w, h, format))
75 {
76 }
77 
79  : d(new QVolatileImageData(sourceImage))
80 {
81 }
82 
83 QVolatileImage::QVolatileImage(void *nativeImage, void *nativeMask)
84  : d(new QVolatileImageData(nativeImage, nativeMask))
85 {
86 }
87 
88 // Need non-inline, non-autogenerated copy ctor, dtor, op= to keep the
89 // fwd declared QSharedData working.
90 
92  : d(other.d)
93 {
94 }
95 
97 {
98 }
99 
101 {
102  d = rhs.d;
103  return *this;
104 }
105 
107 {
108  return d->pengine && d->pengine->isActive();
109 }
110 
112 {
113  return d->image.isNull();
114 }
115 
117 {
118  return d->image.format();
119 }
120 
122 {
123  return d->image.width();
124 }
125 
127 {
128  return d->image.height();
129 }
130 
132 {
133  return d->image.bytesPerLine();
134 }
135 
137 {
138  return d->image.byteCount();
139 }
140 
142 {
143  return d->image.depth();
144 }
145 
147 {
148  return d->image.hasAlphaChannel();
149 }
150 
152 {
153  d->beginDataAccess();
154 }
155 
156 void QVolatileImage::endDataAccess(bool readOnly) const
157 {
158  d->endDataAccess(readOnly);
159 }
160 
166 {
167  return d->image.bits();
168 }
169 
171 {
172  return d->image.constBits();
173 }
174 
176 {
177  return d->ensureFormat(format);
178 }
179 
184 {
185  d->beginDataAccess();
186  QImage newImage = d->image.copy(); // no sharing allowed
187  d->endDataAccess(true);
188  return newImage;
189 }
190 
197 QImage &QVolatileImage::imageRef() // non-const, in order to cause a detach
198 {
199  d->ensureImage();
200  return d->image;
201 }
202 
208 {
209  const_cast<QVolatileImageData *>(d.data())->ensureImage();
210  return d->image;
211 }
212 
214 {
215  return d->duplicateNativeImage();
216 }
217 
218 void QVolatileImage::setAlphaChannel(const QPixmap &alphaChannel)
219 {
221  beginDataAccess();
222  imageRef().setAlphaChannel(alphaChannel.toImage());
223  endDataAccess();
224  d->ensureImage();
225 }
226 
227 void QVolatileImage::fill(uint pixelValue)
228 {
229  beginDataAccess();
230  imageRef().fill(pixelValue);
231  endDataAccess();
232  d->ensureImage();
233 }
234 
236 {
237  if (source->isNull()) {
238  return;
239  }
240  QRect r = rect;
241  if (rect.isNull()) {
242  r = QRect(0, 0, source->width(), source->height());
243  }
244  source->beginDataAccess();
245  QImage &srcImgRef(source->imageRef());
246  int srcbpl = srcImgRef.bytesPerLine();
247  int srcbpp = srcImgRef.depth() / 8;
248  const uchar *sptr = srcImgRef.constBits() + r.y() * srcbpl;
249  beginDataAccess();
250  QImage &dstImgRef(imageRef());
251  if (!dstImgRef.isNull()) {
252  int dstbpl = dstImgRef.bytesPerLine();
253  uchar *dptr = dstImgRef.bits();
254  for (int y = 0; y < r.height(); ++y) {
255  qMemCopy(dptr, sptr + r.x() * srcbpp, r.width() * srcbpp);
256  sptr += srcbpl;
257  dptr += dstbpl;
258  }
259  }
260  endDataAccess();
261  source->endDataAccess(true);
262 }
263 
268 {
269  if (!d->pengine) {
270  d->pengine = new QVolatileImagePaintEngine(&imageRef(), this);
271  }
272  return d->pengine;
273 }
274 
276  QVolatileImage *img)
278 {
280  d->img = img;
281 }
282 
284 {
286  d->img->beginDataAccess();
287  return QRasterPaintEngine::begin(device);
288 }
289 
291 {
293  bool ret = QRasterPaintEngine::end();
294  d->img->endDataAccess();
295  return ret;
296 }
297 
298 // For non-RasterClass pixmaps drawPixmap() would call toImage() which is slow in
299 // our case. Therefore drawPixmap() is rerouted to drawImage().
300 
302 {
303 #ifdef Q_OS_SYMBIAN
304  QVolatileImage img = pm.pixmapData()->toVolatileImage();
305  if (!img.isNull()) {
306  img.beginDataAccess();
307  // imageRef() would detach and since we received the QVolatileImage from
308  // toVolatileImage() by value, it would cause a copy which would ruin
309  // our goal. So use constImageRef() instead.
311  img.endDataAccess(true);
312  } else {
314  }
315 #else
317 #endif
318 }
319 
320 void QVolatileImagePaintEngine::drawPixmap(const QRectF &r, const QPixmap &pm, const QRectF &sr)
321 {
322 #ifdef Q_OS_SYMBIAN
323  QVolatileImage img = pm.pixmapData()->toVolatileImage();
324  if (!img.isNull()) {
325  img.beginDataAccess();
327  img.endDataAccess(true);
328  } else {
330  }
331 #else
333 #endif
334 }
335 
double d
Definition: qnumeric_p.h:62
QImage toImage() const
Converts the pixmap to a QImage.
Definition: qpixmap.cpp:542
Format
The following image formats are available in Qt.
Definition: qimage.h:91
QImage copy(const QRect &rect=QRect()) const
Returns a sub-area of the image as a new image.
Definition: qimage.cpp:1410
bool isNull() const
Returns true if the rectangle is a null rectangle, otherwise returns false.
Definition: qrect.h:231
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QImage toImage() const
This will always perform a copy of the pixel data.
void beginDataAccess() const
void beginDataAccess() const
T * data()
Returns a pointer to the shared data object.
Definition: qshareddata.h:82
bool isNull() const
Returns true if it is a null image, otherwise returns false.
Definition: qimage.cpp:1542
void fill(uint pixel)
Fills the entire image with the given pixelValue.
Definition: qimage.cpp:2032
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:214
void * duplicateNativeImage() const
bool begin(QPaintDevice *device)
Reimplemented Function
bool ensureFormat(QImage::Format format)
int byteCount() const
Returns the number of bytes occupied by the image data.
Definition: qimage.cpp:1800
int width() const
Returns the width of the rectangle.
Definition: qrect.h:303
const QImage & constImageRef() const
Non-detaching version, for read-only access only.
void endDataAccess(bool readOnly=false) const
bool hasAlphaChannel() const
Returns true if the image has a format that respects the alpha channel, otherwise returns false...
Definition: qimage.cpp:6495
bool end()
Reimplement this function to finish painting on the current paint device.
int bytesPerLine() const
Returns the number of bytes per image scanline.
Definition: qimage.cpp:1812
int height() const
Returns the height of the rectangle.
Definition: qrect.h:306
bool ensureFormat(QImage::Format format)
void drawPixmap(const QPointF &p, const QPixmap &pm)
#define Q_D(Class)
Definition: qglobal.h:2482
void endDataAccess(bool readOnly=false) const
bool end()
Reimplemented Function
Format format() const
Returns the format of the image.
Definition: qimage.cpp:2305
bool isNull() const
void fill(uint pixelValue)
void drawImage(const QPointF &p, const QImage &img)
unsigned char uchar
Definition: qglobal.h:994
bool begin(QPaintDevice *device)
Reimplement this function to initialise your paint engine when painting is to start on the paint devi...
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
bool isActive() const
Returns true if the paint engine is actively drawing; otherwise returns false.
Definition: qpaintengine.h:154
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
QVolatileImagePaintEngine(QPaintDevice *device, QVolatileImage *img)
int depth() const
The QImage class provides a hardware-independent image representation that allows direct access to th...
Definition: qimage.h:87
unsigned int uint
Definition: qglobal.h:996
QSharedDataPointer< QVolatileImageData > d
int depth() const
Returns the depth of the image.
Definition: qimage.cpp:1620
uchar * bits()
Access to pixel data via bits() or constBits() should be guarded by begin/endDataAccess().
const uchar * constBits() const
Returns a pointer to the first pixel data.
Definition: qimage.cpp:1985
The QPaintEngine class provides an abstract definition of how QPainter draws to a given device on a g...
Definition: qpaintengine.h:90
QImage::Format format() const
void drawPixmap(const QPointF &p, const QPixmap &pm)
void * qMemCopy(void *dest, const void *src, size_t n)
Definition: qglobal.cpp:2508
int byteCount() const
uchar * bits()
Returns a pointer to the first pixel data.
Definition: qimage.cpp:1946
int width() const
Returns the width of the image.
Definition: qimage.cpp:1557
int width() const
int y() const
Returns the y-coordinate of the rectangle&#39;s top edge.
Definition: qrect.h:255
int bytesPerLine() const
int x() const
Returns the x-coordinate of the rectangle&#39;s left edge.
Definition: qrect.h:252
bool hasAlphaChannel() const
int height() const
QVolatileImage & operator=(const QVolatileImage &rhs)
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
int height() const
Returns the height of the image.
Definition: qimage.cpp:1572
bool paintingActive() const
The QPixmap class is an off-screen image representation that can be used as a paint device...
Definition: qpixmap.h:71
#define Q_DECLARE_PRIVATE(Class)
Definition: qglobal.h:2467
The QRasterPaintEngine class enables hardware acceleration of painting operations in Qt for Embedded ...
void setAlphaChannel(const QImage &alphaChannel)
Sets the alpha channel of this image to the given alphaChannel.
Definition: qimage.cpp:6329
static const KeyPair *const end
QImage & imageRef()
Returns a reference to the image that is potentially using some native buffer internally.
void copyFrom(QVolatileImage *source, const QRect &rect)
const uchar * constBits() const
QPaintEngine * paintEngine()
To be called from the PixmapData&#39;s paintEngine().
QPixmapData * pixmapData() const
Definition: qpixmap.cpp:2277
void * duplicateNativeImage() const
void setAlphaChannel(const QPixmap &alphaChannel)