src/gui/image/qpixmapdata.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    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 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    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 
       
    49 QT_BEGIN_NAMESPACE
       
    50 
       
    51 const uchar qt_pixmap_bit_mask[] = { 0x01, 0x02, 0x04, 0x08,
       
    52                                      0x10, 0x20, 0x40, 0x80 };
       
    53 
       
    54 QPixmapData::QPixmapData(PixelType pixelType, int objectId)
       
    55     : w(0),
       
    56       h(0),
       
    57       d(0),
       
    58       is_null(true),
       
    59       ref(0),
       
    60       detach_no(0),
       
    61       type(pixelType),
       
    62       id(objectId),
       
    63       ser_no(0),
       
    64       is_cached(false)
       
    65 {
       
    66 }
       
    67 
       
    68 QPixmapData::~QPixmapData()
       
    69 {
       
    70 }
       
    71 
       
    72 QPixmapData *QPixmapData::createCompatiblePixmapData() const
       
    73 {
       
    74     QPixmapData *d;
       
    75     QGraphicsSystem *gs = QApplicationPrivate::graphicsSystem();
       
    76     if (gs)
       
    77         d = gs->createPixmapData(pixelType());
       
    78     else
       
    79         d = QGraphicsSystem::createDefaultPixmapData(pixelType());
       
    80     return d;
       
    81 }
       
    82 
       
    83 static QImage makeBitmapCompliantIfNeeded(QPixmapData *d, const QImage &image, Qt::ImageConversionFlags flags)
       
    84 {
       
    85     if (d->pixelType() == QPixmapData::BitmapType) {
       
    86         QImage img = image.convertToFormat(QImage::Format_MonoLSB, flags);
       
    87 
       
    88         // make sure image.color(0) == Qt::color0 (white)
       
    89         // and image.color(1) == Qt::color1 (black)
       
    90         const QRgb c0 = QColor(Qt::black).rgb();
       
    91         const QRgb c1 = QColor(Qt::white).rgb();
       
    92         if (img.color(0) == c0 && img.color(1) == c1) {
       
    93             img.invertPixels();
       
    94             img.setColor(0, c1);
       
    95             img.setColor(1, c0);
       
    96         }
       
    97         return img;
       
    98     }
       
    99 
       
   100     return image;
       
   101 }
       
   102 
       
   103 bool QPixmapData::fromFile(const QString &fileName, const char *format,
       
   104                            Qt::ImageConversionFlags flags)
       
   105 {
       
   106     QImage image = QImageReader(fileName, format).read();
       
   107     if (image.isNull())
       
   108         return false;
       
   109     fromImage(makeBitmapCompliantIfNeeded(this, image, flags), flags);
       
   110     return !isNull();
       
   111 }
       
   112 
       
   113 bool QPixmapData::fromData(const uchar *buf, uint len, const char *format, Qt::ImageConversionFlags flags)
       
   114 {
       
   115     QByteArray a = QByteArray::fromRawData(reinterpret_cast<const char *>(buf), len);
       
   116     QBuffer b(&a);
       
   117     b.open(QIODevice::ReadOnly);
       
   118     QImage image = QImageReader(&b, format).read();
       
   119     fromImage(makeBitmapCompliantIfNeeded(this, image, flags), flags);
       
   120     return !isNull();
       
   121 }
       
   122 
       
   123 void QPixmapData::copy(const QPixmapData *data, const QRect &rect)
       
   124 {
       
   125     fromImage(data->toImage().copy(rect), Qt::AutoColor);
       
   126 }
       
   127 
       
   128 bool QPixmapData::scroll(int dx, int dy, const QRect &rect)
       
   129 {
       
   130     Q_UNUSED(dx);
       
   131     Q_UNUSED(dy);
       
   132     Q_UNUSED(rect);
       
   133     return false;
       
   134 }
       
   135 
       
   136 void QPixmapData::setMask(const QBitmap &mask)
       
   137 {
       
   138     if (mask.size().isEmpty()) {
       
   139         if (depth() != 1)
       
   140             fromImage(toImage().convertToFormat(QImage::Format_RGB32),
       
   141                       Qt::AutoColor);
       
   142     } else {
       
   143         QImage image = toImage();
       
   144         const int w = image.width();
       
   145         const int h = image.height();
       
   146 
       
   147         switch (image.depth()) {
       
   148         case 1: {
       
   149             const QImage imageMask = mask.toImage().convertToFormat(image.format());
       
   150             for (int y = 0; y < h; ++y) {
       
   151                 const uchar *mscan = imageMask.scanLine(y);
       
   152                 uchar *tscan = image.scanLine(y);
       
   153                 int bytesPerLine = image.bytesPerLine();
       
   154                 for (int i = 0; i < bytesPerLine; ++i)
       
   155                     tscan[i] &= mscan[i];
       
   156             }
       
   157             break;
       
   158         }
       
   159         default: {
       
   160             const QImage imageMask = mask.toImage().convertToFormat(QImage::Format_MonoLSB);
       
   161             image = image.convertToFormat(QImage::Format_ARGB32_Premultiplied);
       
   162             for (int y = 0; y < h; ++y) {
       
   163                 const uchar *mscan = imageMask.scanLine(y);
       
   164                 QRgb *tscan = (QRgb *)image.scanLine(y);
       
   165                 for (int x = 0; x < w; ++x) {
       
   166                     if (!(mscan[x>>3] & qt_pixmap_bit_mask[x&7]))
       
   167                         tscan[x] = 0;
       
   168                 }
       
   169             }
       
   170             break;
       
   171         }
       
   172         }
       
   173         fromImage(image, Qt::AutoColor);
       
   174     }
       
   175 }
       
   176 
       
   177 QBitmap QPixmapData::mask() const
       
   178 {
       
   179     if (!hasAlphaChannel())
       
   180         return QBitmap();
       
   181 
       
   182     const QImage img = toImage();
       
   183     const QImage image = (img.depth() < 32 ? img.convertToFormat(QImage::Format_ARGB32_Premultiplied) : img);
       
   184     const int w = image.width();
       
   185     const int h = image.height();
       
   186 
       
   187     QImage mask(w, h, QImage::Format_MonoLSB);
       
   188     if (mask.isNull()) // allocation failed
       
   189         return QBitmap();
       
   190 
       
   191     mask.setNumColors(2);
       
   192     mask.setColor(0, QColor(Qt::color0).rgba());
       
   193     mask.setColor(1, QColor(Qt::color1).rgba());
       
   194 
       
   195     const int bpl = mask.bytesPerLine();
       
   196 
       
   197     for (int y = 0; y < h; ++y) {
       
   198         const QRgb *src = reinterpret_cast<const QRgb*>(image.scanLine(y));
       
   199         uchar *dest = mask.scanLine(y);
       
   200         memset(dest, 0, bpl);
       
   201         for (int x = 0; x < w; ++x) {
       
   202             if (qAlpha(*src) > 0)
       
   203                 dest[x >> 3] |= qt_pixmap_bit_mask[x & 7];
       
   204             ++src;
       
   205         }
       
   206     }
       
   207 
       
   208     return QBitmap::fromImage(mask);
       
   209 }
       
   210 
       
   211 QPixmap QPixmapData::transformed(const QTransform &matrix,
       
   212                                  Qt::TransformationMode mode) const
       
   213 {
       
   214     return QPixmap::fromImage(toImage().transformed(matrix, mode));
       
   215 }
       
   216 
       
   217 void QPixmapData::setAlphaChannel(const QPixmap &alphaChannel)
       
   218 {
       
   219     QImage image = toImage();
       
   220     image.setAlphaChannel(alphaChannel.toImage());
       
   221     fromImage(image, Qt::AutoColor);
       
   222 }
       
   223 
       
   224 QPixmap QPixmapData::alphaChannel() const
       
   225 {
       
   226     return QPixmap::fromImage(toImage().alphaChannel());
       
   227 }
       
   228 
       
   229 void QPixmapData::setSerialNumber(int serNo)
       
   230 {
       
   231     ser_no = serNo;
       
   232 }
       
   233 
       
   234 QImage* QPixmapData::buffer()
       
   235 {
       
   236     return 0;
       
   237 }
       
   238 
       
   239 #if defined(Q_OS_SYMBIAN)
       
   240 void* QPixmapData::toNativeType(NativeType /* type */)
       
   241 {
       
   242     return 0;
       
   243 }
       
   244 
       
   245 void QPixmapData::fromNativeType(void* /* pixmap */, NativeType /* typre */)
       
   246 {
       
   247     return;
       
   248 }
       
   249 #endif
       
   250 
       
   251 QT_END_NAMESPACE