| 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 "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 | void QPixmapData::fromImageReader(QImageReader *imageReader, | 
|---|
| 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); | 
|---|
| 148 | b.open(QIODevice::ReadOnly); | 
|---|
| 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 | { | 
|---|
| 156 | fromImage(data->toImage(rect), Qt::NoOpaqueDetection); | 
|---|
| 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 |  | 
|---|
| 167 | void QPixmapData::setMask(const QBitmap &mask) | 
|---|
| 168 | { | 
|---|
| 169 | if (mask.size().isEmpty()) { | 
|---|
| 170 | if (depth() != 1) | 
|---|
| 171 | fromImage(toImage().convertToFormat(QImage::Format_RGB32), | 
|---|
| 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); | 
|---|
| 192 | image = image.convertToFormat(QImage::Format_ARGB32_Premultiplied); | 
|---|
| 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 |  | 
|---|
| 208 | QBitmap QPixmapData::mask() const | 
|---|
| 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 |  | 
|---|
| 218 | QImage mask(w, h, QImage::Format_MonoLSB); | 
|---|
| 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 |  | 
|---|
| 242 | QPixmap QPixmapData::transformed(const QTransform &matrix, | 
|---|
| 243 | Qt::TransformationMode mode) const | 
|---|
| 244 | { | 
|---|
| 245 | return QPixmap::fromImage(toImage().transformed(matrix, mode)); | 
|---|
| 246 | } | 
|---|
| 247 |  | 
|---|
| 248 | void QPixmapData::setAlphaChannel(const QPixmap &alphaChannel) | 
|---|
| 249 | { | 
|---|
| 250 | QImage image = toImage(); | 
|---|
| 251 | image.setAlphaChannel(alphaChannel.toImage()); | 
|---|
| 252 | fromImage(image, Qt::AutoColor); | 
|---|
| 253 | } | 
|---|
| 254 |  | 
|---|
| 255 | QPixmap QPixmapData::alphaChannel() const | 
|---|
| 256 | { | 
|---|
| 257 | return QPixmap::fromImage(toImage().alphaChannel()); | 
|---|
| 258 | } | 
|---|
| 259 |  | 
|---|
| 260 | void QPixmapData::setSerialNumber(int serNo) | 
|---|
| 261 | { | 
|---|
| 262 | ser_no = serNo; | 
|---|
| 263 | } | 
|---|
| 264 |  | 
|---|
| 265 | QImage QPixmapData::toImage(const QRect &rect) const | 
|---|
| 266 | { | 
|---|
| 267 | if (rect.contains(QRect(0, 0, w, h))) | 
|---|
| 268 | return toImage(); | 
|---|
| 269 | else | 
|---|
| 270 | return toImage().copy(rect); | 
|---|
| 271 | } | 
|---|
| 272 |  | 
|---|
| 273 | QImage* QPixmapData::buffer() | 
|---|
| 274 | { | 
|---|
| 275 | return 0; | 
|---|
| 276 | } | 
|---|
| 277 |  | 
|---|
| 278 | #if defined(Q_OS_SYMBIAN) | 
|---|
| 279 | void* QPixmapData::toNativeType(NativeType /* type */) | 
|---|
| 280 | { | 
|---|
| 281 | return 0; | 
|---|
| 282 | } | 
|---|
| 283 |  | 
|---|
| 284 | void QPixmapData::fromNativeType(void* /* pixmap */, NativeType /* typre */) | 
|---|
| 285 | { | 
|---|
| 286 | return; | 
|---|
| 287 | } | 
|---|
| 288 | #endif | 
|---|
| 289 |  | 
|---|
| 290 | QT_END_NAMESPACE | 
|---|