src/gui/image/qpixmap.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 <qglobal.h>
       
    43 
       
    44 #include "qpixmap.h"
       
    45 #include "qpixmapdata_p.h"
       
    46 #include "qimagepixmapcleanuphooks_p.h"
       
    47 
       
    48 #include "qbitmap.h"
       
    49 #include "qcolormap.h"
       
    50 #include "qimage.h"
       
    51 #include "qwidget.h"
       
    52 #include "qpainter.h"
       
    53 #include "qdatastream.h"
       
    54 #include "qbuffer.h"
       
    55 #include "qapplication.h"
       
    56 #include <private/qapplication_p.h>
       
    57 #include <private/qgraphicssystem_p.h>
       
    58 #include <private/qwidget_p.h>
       
    59 #include "qevent.h"
       
    60 #include "qfile.h"
       
    61 #include "qfileinfo.h"
       
    62 #include "qpixmapcache.h"
       
    63 #include "qdatetime.h"
       
    64 #include "qimagereader.h"
       
    65 #include "qimagewriter.h"
       
    66 #include "qpaintengine.h"
       
    67 #include "qthread.h"
       
    68 
       
    69 #ifdef Q_WS_MAC
       
    70 # include "private/qt_mac_p.h"
       
    71 # include "private/qpixmap_mac_p.h"
       
    72 #endif
       
    73 
       
    74 #if defined(Q_WS_X11)
       
    75 # include "qx11info_x11.h"
       
    76 # include <private/qt_x11_p.h>
       
    77 # include <private/qpixmap_x11_p.h>
       
    78 #endif
       
    79 
       
    80 #if defined(Q_OS_SYMBIAN)
       
    81 # include <private/qt_s60_p.h>
       
    82 #endif
       
    83 
       
    84 #include "qpixmap_raster_p.h"
       
    85 
       
    86 QT_BEGIN_NAMESPACE
       
    87 
       
    88 // ### Qt 5: remove
       
    89 Q_GUI_EXPORT qint64 qt_pixmap_id(const QPixmap &pixmap)
       
    90 {
       
    91     return pixmap.cacheKey();
       
    92 }
       
    93 
       
    94 static bool qt_pixmap_thread_test()
       
    95 {
       
    96     if (!qApp) {
       
    97         qFatal("QPixmap: Must construct a QApplication before a QPaintDevice");
       
    98         return false;
       
    99     }
       
   100 #ifndef Q_WS_WIN
       
   101     if (qApp->thread() != QThread::currentThread()) {
       
   102         qWarning("QPixmap: It is not safe to use pixmaps outside the GUI thread");
       
   103         return false;
       
   104     }
       
   105 #endif
       
   106     return true;
       
   107 }
       
   108 
       
   109 void QPixmap::init(int w, int h, Type type)
       
   110 {
       
   111     init(w, h, int(type));
       
   112 }
       
   113 
       
   114 void QPixmap::init(int w, int h, int type)
       
   115 {
       
   116     QGraphicsSystem* gs = QApplicationPrivate::graphicsSystem();
       
   117     if (gs)
       
   118         data = gs->createPixmapData(static_cast<QPixmapData::PixelType>(type));
       
   119     else
       
   120         data = QGraphicsSystem::createDefaultPixmapData(static_cast<QPixmapData::PixelType>(type));
       
   121 
       
   122     data->resize(w, h);
       
   123 }
       
   124 
       
   125 /*!
       
   126     \enum QPixmap::ColorMode
       
   127 
       
   128     \compat
       
   129 
       
   130     This enum type defines the color modes that exist for converting
       
   131     QImage objects to QPixmap.  It is provided here for compatibility
       
   132     with earlier versions of Qt.
       
   133 
       
   134     Use Qt::ImageConversionFlags instead.
       
   135 
       
   136     \value Auto  Select \c Color or \c Mono on a case-by-case basis.
       
   137     \value Color Always create colored pixmaps.
       
   138     \value Mono  Always create bitmaps.
       
   139 */
       
   140 
       
   141 /*!
       
   142     Constructs a null pixmap.
       
   143 
       
   144     \sa isNull()
       
   145 */
       
   146 
       
   147 QPixmap::QPixmap()
       
   148     : QPaintDevice()
       
   149 {
       
   150     (void) qt_pixmap_thread_test();
       
   151     init(0, 0, QPixmapData::PixmapType);
       
   152 }
       
   153 
       
   154 /*!
       
   155     \fn QPixmap::QPixmap(int width, int height)
       
   156 
       
   157     Constructs a pixmap with the given \a width and \a height. If
       
   158     either \a width or \a height is zero, a null pixmap is
       
   159     constructed.
       
   160 
       
   161     \warning This will create a QPixmap with uninitialized data. Call
       
   162     fill() to fill the pixmap with an appropriate color before drawing
       
   163     onto it with QPainter.
       
   164 
       
   165     \sa isNull()
       
   166 */
       
   167 
       
   168 QPixmap::QPixmap(int w, int h)
       
   169     : QPaintDevice()
       
   170 {
       
   171     if (!qt_pixmap_thread_test())
       
   172         init(0, 0, QPixmapData::PixmapType);
       
   173     else
       
   174         init(w, h, QPixmapData::PixmapType);
       
   175 }
       
   176 
       
   177 /*!
       
   178     \overload
       
   179 
       
   180     Constructs a pixmap of the given \a size.
       
   181 
       
   182     \warning This will create a QPixmap with uninitialized data. Call
       
   183     fill() to fill the pixmap with an appropriate color before drawing
       
   184     onto it with QPainter.
       
   185 */
       
   186 
       
   187 QPixmap::QPixmap(const QSize &size)
       
   188     : QPaintDevice()
       
   189 {
       
   190     if (!qt_pixmap_thread_test())
       
   191         init(0, 0, QPixmapData::PixmapType);
       
   192     else
       
   193         init(size.width(), size.height(), QPixmapData::PixmapType);
       
   194 }
       
   195 
       
   196 /*!
       
   197   \internal
       
   198 */
       
   199 QPixmap::QPixmap(const QSize &s, Type type)
       
   200 {
       
   201     if (!qt_pixmap_thread_test())
       
   202         init(0, 0, type);
       
   203     else
       
   204         init(s.width(), s.height(), type);
       
   205 }
       
   206 
       
   207 /*!
       
   208   \internal
       
   209 */
       
   210 QPixmap::QPixmap(const QSize &s, int type)
       
   211 {
       
   212     if (!qt_pixmap_thread_test())
       
   213         init(0, 0, static_cast<QPixmapData::PixelType>(type));
       
   214     else
       
   215         init(s.width(), s.height(), static_cast<QPixmapData::PixelType>(type));
       
   216 }
       
   217 
       
   218 /*!
       
   219     \internal
       
   220 */
       
   221 QPixmap::QPixmap(QPixmapData *d)
       
   222     : QPaintDevice(), data(d)
       
   223 {
       
   224 }
       
   225 
       
   226 /*!
       
   227     Constructs a pixmap from the file with the given \a fileName. If the
       
   228     file does not exist or is of an unknown format, the pixmap becomes a
       
   229     null pixmap.
       
   230 
       
   231     The loader attempts to read the pixmap using the specified \a
       
   232     format. If the \a format is not specified (which is the default),
       
   233     the loader probes the file for a header to guess the file format.
       
   234 
       
   235     The file name can either refer to an actual file on disk or to
       
   236     one of the application's embedded resources. See the
       
   237     \l{resources.html}{Resource System} overview for details on how
       
   238     to embed images and other resource files in the application's
       
   239     executable.
       
   240 
       
   241     If the image needs to be modified to fit in a lower-resolution
       
   242     result (e.g. converting from 32-bit to 8-bit), use the \a
       
   243     flags to control the conversion.
       
   244 
       
   245     The \a fileName, \a format and \a flags parameters are
       
   246     passed on to load(). This means that the data in \a fileName is
       
   247     not compiled into the binary. If \a fileName contains a relative
       
   248     path (e.g. the filename only) the relevant file must be found
       
   249     relative to the runtime working directory.
       
   250 
       
   251     \sa {QPixmap#Reading and Writing Image Files}{Reading and Writing
       
   252     Image Files}
       
   253 */
       
   254 
       
   255 QPixmap::QPixmap(const QString& fileName, const char *format, Qt::ImageConversionFlags flags)
       
   256     : QPaintDevice()
       
   257 {
       
   258     init(0, 0, QPixmapData::PixmapType);
       
   259     if (!qt_pixmap_thread_test())
       
   260         return;
       
   261 
       
   262     load(fileName, format, flags);
       
   263 }
       
   264 
       
   265 /*!
       
   266     Constructs a pixmap that is a copy of the given \a pixmap.
       
   267 
       
   268     \sa copy()
       
   269 */
       
   270 
       
   271 QPixmap::QPixmap(const QPixmap &pixmap)
       
   272     : QPaintDevice()
       
   273 {
       
   274     if (!qt_pixmap_thread_test()) {
       
   275         init(0, 0, QPixmapData::PixmapType);
       
   276         return;
       
   277     }
       
   278     if (pixmap.paintingActive()) {                // make a deep copy
       
   279         operator=(pixmap.copy());
       
   280     } else {
       
   281         data = pixmap.data;
       
   282     }
       
   283 }
       
   284 
       
   285 /*!
       
   286     Constructs a pixmap from the given \a xpm data, which must be a
       
   287     valid XPM image.
       
   288 
       
   289     Errors are silently ignored.
       
   290 
       
   291     Note that it's possible to squeeze the XPM variable a little bit
       
   292     by using an unusual declaration:
       
   293 
       
   294     \snippet doc/src/snippets/code/src_gui_image_qpixmap.cpp 0
       
   295 
       
   296     The extra \c const makes the entire definition read-only, which is
       
   297     slightly more efficient (for example, when the code is in a shared
       
   298     library) and ROMable when the application is to be stored in ROM.
       
   299 */
       
   300 #ifndef QT_NO_IMAGEFORMAT_XPM
       
   301 QPixmap::QPixmap(const char * const xpm[])
       
   302     : QPaintDevice()
       
   303 {
       
   304     init(0, 0, QPixmapData::PixmapType);
       
   305     if (!xpm)
       
   306         return;
       
   307 
       
   308     QImage image(xpm);
       
   309     if (!image.isNull()) {
       
   310         if (data->pixelType() == QPixmapData::BitmapType)
       
   311             *this = QBitmap::fromImage(image);
       
   312         else
       
   313             *this = fromImage(image);
       
   314     }
       
   315 }
       
   316 #endif
       
   317 
       
   318 
       
   319 /*!
       
   320     Destroys the pixmap.
       
   321 */
       
   322 
       
   323 QPixmap::~QPixmap()
       
   324 {
       
   325     Q_ASSERT(data->ref >= 1); // Catch if ref-counting changes again
       
   326     if (data->is_cached && data->ref == 1) // ref will be decrememnted after destructor returns
       
   327         QImagePixmapCleanupHooks::executePixmapDestructionHooks(this);
       
   328 }
       
   329 
       
   330 /*!
       
   331   \internal
       
   332 */
       
   333 int QPixmap::devType() const
       
   334 {
       
   335     return QInternal::Pixmap;
       
   336 }
       
   337 
       
   338 /*!
       
   339     \fn QPixmap QPixmap::copy(int x, int y, int width, int height) const
       
   340     \overload
       
   341 
       
   342     Returns a deep copy of the subset of the pixmap that is specified
       
   343     by the rectangle QRect( \a x, \a y, \a width, \a height).
       
   344 */
       
   345 
       
   346 /*!
       
   347     \fn QPixmap QPixmap::copy(const QRect &rectangle) const
       
   348 
       
   349     Returns a deep copy of the subset of the pixmap that is specified
       
   350     by the given \a rectangle. For more information on deep copies,
       
   351     see the \l {Implicit Data Sharing} documentation.
       
   352 
       
   353     If the given \a rectangle is empty, the whole image is copied.
       
   354 
       
   355     \sa operator=(), QPixmap(), {QPixmap#Pixmap
       
   356     Transformations}{Pixmap Transformations}
       
   357 */
       
   358 QPixmap QPixmap::copy(const QRect &rect) const
       
   359 {
       
   360     if (isNull())
       
   361         return QPixmap();
       
   362 
       
   363     const QRect r = rect.isEmpty() ? QRect(0, 0, width(), height()) : rect;
       
   364 
       
   365     QPixmapData *d = data->createCompatiblePixmapData();
       
   366     d->copy(data.data(), r);
       
   367     return QPixmap(d);
       
   368 }
       
   369 
       
   370 /*!
       
   371     \fn QPixmap::scroll(int dx, int dy, int x, int y, int width, int height, QRegion *exposed)
       
   372     \since 4.6
       
   373 
       
   374     This convenience function is equivalent to calling QPixmap::scroll(\a dx,
       
   375     \a dy, QRect(\a x, \a y, \a width, \a height), \a exposed).
       
   376 
       
   377     \sa QWidget::scroll(), QGraphicsItem::scroll()
       
   378 */
       
   379 
       
   380 /*!
       
   381     \since 4.6
       
   382 
       
   383     Scrolls the area \a rect of this pixmap by (\a dx, \a dy). The exposed
       
   384     region is left unchanged. You can optionally pass a pointer to an empty
       
   385     QRegion to get the region that is \a exposed by the scroll operation.
       
   386 
       
   387     \snippet doc/src/snippets/code/src_gui_image_qpixmap.cpp 2
       
   388 
       
   389     You cannot scroll while there is an active painter on the pixmap.
       
   390 
       
   391     \sa QWidget::scroll(), QGraphicsItem::scroll()
       
   392 */
       
   393 void QPixmap::scroll(int dx, int dy, const QRect &rect, QRegion *exposed)
       
   394 {
       
   395     if (isNull() || (dx == 0 && dy == 0))
       
   396         return;
       
   397     QRect dest = rect & this->rect();
       
   398     QRect src = dest.translated(-dx, -dy) & dest;
       
   399     if (src.isEmpty()) {
       
   400         if (exposed)
       
   401             *exposed += dest;
       
   402         return;
       
   403     }
       
   404 
       
   405     detach();
       
   406 
       
   407     if (!data->scroll(dx, dy, src)) {
       
   408         // Fallback
       
   409         QPixmap pix = *this;
       
   410         QPainter painter(&pix);
       
   411         painter.setCompositionMode(QPainter::CompositionMode_Source);
       
   412         painter.drawPixmap(src.translated(dx, dy), *this, src);
       
   413         painter.end();
       
   414         *this = pix;
       
   415     }
       
   416 
       
   417     if (exposed) {
       
   418         *exposed += dest;
       
   419         *exposed -= src.translated(dx, dy);
       
   420     }
       
   421 }
       
   422 
       
   423 /*!
       
   424     Assigns the given \a pixmap to this pixmap and returns a reference
       
   425     to this pixmap.
       
   426 
       
   427     \sa copy(), QPixmap()
       
   428 */
       
   429 
       
   430 QPixmap &QPixmap::operator=(const QPixmap &pixmap)
       
   431 {
       
   432     if (paintingActive()) {
       
   433         qWarning("QPixmap::operator=: Cannot assign to pixmap during painting");
       
   434         return *this;
       
   435     }
       
   436     if (pixmap.paintingActive()) {                // make a deep copy
       
   437         *this = pixmap.copy();
       
   438     } else {
       
   439         data = pixmap.data;
       
   440     }
       
   441     return *this;
       
   442 }
       
   443 
       
   444 /*!
       
   445    Returns the pixmap as a QVariant.
       
   446 */
       
   447 QPixmap::operator QVariant() const
       
   448 {
       
   449     return QVariant(QVariant::Pixmap, this);
       
   450 }
       
   451 
       
   452 /*!
       
   453     \fn bool QPixmap::operator!() const
       
   454 
       
   455     Returns true if this is a null pixmap; otherwise returns false.
       
   456 
       
   457     \sa isNull()
       
   458 */
       
   459 
       
   460 /*!
       
   461     \fn QPixmap::operator QImage() const
       
   462 
       
   463     Returns the pixmap as a QImage.
       
   464 
       
   465     Use the toImage() function instead.
       
   466 */
       
   467 
       
   468 /*!
       
   469     Converts the pixmap to a QImage. Returns a null image if the
       
   470     conversion fails.
       
   471 
       
   472     If the pixmap has 1-bit depth, the returned image will also be 1
       
   473     bit deep. Images with more bits will be returned in a format
       
   474     closely represents the underlying system. Usually this will be
       
   475     QImage::Format_ARGB32_Premultiplied for pixmaps with an alpha and
       
   476     QImage::Format_RGB32 or QImage::Format_RGB16 for pixmaps without
       
   477     alpha.
       
   478 
       
   479     Note that for the moment, alpha masks on monochrome images are
       
   480     ignored.
       
   481 
       
   482     \sa fromImage(), {QImage#Image Formats}{Image Formats}
       
   483 */
       
   484 QImage QPixmap::toImage() const
       
   485 {
       
   486     if (isNull())
       
   487         return QImage();
       
   488 
       
   489     return data->toImage();
       
   490 }
       
   491 
       
   492 /*!
       
   493     \fn QMatrix QPixmap::trueMatrix(const QTransform &matrix, int width, int height)
       
   494 
       
   495     Returns the actual matrix used for transforming a pixmap with the
       
   496     given \a width, \a height and \a matrix.
       
   497 
       
   498     When transforming a pixmap using the transformed() function, the
       
   499     transformation matrix is internally adjusted to compensate for
       
   500     unwanted translation, i.e. transformed() returns the smallest
       
   501     pixmap containing all transformed points of the original
       
   502     pixmap. This function returns the modified matrix, which maps
       
   503     points correctly from the original pixmap into the new pixmap.
       
   504 
       
   505     \sa transformed(), {QPixmap#Pixmap Transformations}{Pixmap
       
   506     Transformations}
       
   507 */
       
   508 QTransform QPixmap::trueMatrix(const QTransform &m, int w, int h)
       
   509 {
       
   510     return QImage::trueMatrix(m, w, h);
       
   511 }
       
   512 
       
   513 /*!
       
   514   \overload
       
   515 
       
   516   This convenience function loads the matrix \a m into a
       
   517   QTransform and calls the overloaded function with the
       
   518   QTransform and the width \a w and the height \a h.
       
   519  */
       
   520 QMatrix QPixmap::trueMatrix(const QMatrix &m, int w, int h)
       
   521 {
       
   522     return trueMatrix(QTransform(m), w, h).toAffine();
       
   523 }
       
   524 
       
   525 
       
   526 /*!
       
   527     \fn bool QPixmap::isQBitmap() const
       
   528 
       
   529     Returns true if this is a QBitmap; otherwise returns false.
       
   530 */
       
   531 
       
   532 bool QPixmap::isQBitmap() const
       
   533 {
       
   534     return data->type == QPixmapData::BitmapType;
       
   535 }
       
   536 
       
   537 /*!
       
   538     \fn bool QPixmap::isNull() const
       
   539 
       
   540     Returns true if this is a null pixmap; otherwise returns false.
       
   541 
       
   542     A null pixmap has zero width, zero height and no contents. You
       
   543     cannot draw in a null pixmap.
       
   544 */
       
   545 bool QPixmap::isNull() const
       
   546 {
       
   547     return data->isNull();
       
   548 }
       
   549 
       
   550 /*!
       
   551     \fn int QPixmap::width() const
       
   552 
       
   553     Returns the width of the pixmap.
       
   554 
       
   555     \sa size(), {QPixmap#Pixmap Information}{Pixmap Information}
       
   556 */
       
   557 int QPixmap::width() const
       
   558 {
       
   559     return data->width();
       
   560 }
       
   561 
       
   562 /*!
       
   563     \fn int QPixmap::height() const
       
   564 
       
   565     Returns the height of the pixmap.
       
   566 
       
   567     \sa size(), {QPixmap#Pixmap Information}{Pixmap Information}
       
   568 */
       
   569 int QPixmap::height() const
       
   570 {
       
   571     return data->height();
       
   572 }
       
   573 
       
   574 /*!
       
   575     \fn QSize QPixmap::size() const
       
   576 
       
   577     Returns the size of the pixmap.
       
   578 
       
   579     \sa width(), height(), {QPixmap#Pixmap Information}{Pixmap
       
   580     Information}
       
   581 */
       
   582 QSize QPixmap::size() const
       
   583 {
       
   584     return QSize(data->width(), data->height());
       
   585 }
       
   586 
       
   587 /*!
       
   588     \fn QRect QPixmap::rect() const
       
   589 
       
   590     Returns the pixmap's enclosing rectangle.
       
   591 
       
   592     \sa {QPixmap#Pixmap Information}{Pixmap Information}
       
   593 */
       
   594 QRect QPixmap::rect() const
       
   595 {
       
   596     return QRect(0, 0, data->width(), data->height());
       
   597 }
       
   598 
       
   599 /*!
       
   600     \fn int QPixmap::depth() const
       
   601 
       
   602     Returns the depth of the pixmap.
       
   603 
       
   604     The pixmap depth is also called bits per pixel (bpp) or bit planes
       
   605     of a pixmap. A null pixmap has depth 0.
       
   606 
       
   607     \sa defaultDepth(), {QPixmap#Pixmap Information}{Pixmap
       
   608     Information}
       
   609 */
       
   610 int QPixmap::depth() const
       
   611 {
       
   612     return data->depth();
       
   613 }
       
   614 
       
   615 /*!
       
   616     \fn void QPixmap::resize(const QSize &size)
       
   617     \overload
       
   618     \compat
       
   619 
       
   620     Use QPixmap::copy() instead to get the pixmap with the new size.
       
   621 
       
   622     \oldcode
       
   623         pixmap.resize(size);
       
   624     \newcode
       
   625         pixmap = pixmap.copy(QRect(QPoint(0, 0), size));
       
   626     \endcode
       
   627 */
       
   628 #ifdef QT3_SUPPORT
       
   629 void QPixmap::resize_helper(const QSize &s)
       
   630 {
       
   631     int w = s.width();
       
   632     int h = s.height();
       
   633     if (w < 1 || h < 1) {
       
   634         *this = QPixmap();
       
   635         return;
       
   636     }
       
   637 
       
   638     if (size() == s)
       
   639         return;
       
   640 
       
   641     // Create new pixmap
       
   642     QPixmap pm(QSize(w, h), data->type);
       
   643     bool uninit = false;
       
   644 #if defined(Q_WS_X11)
       
   645     QX11PixmapData *x11Data = data->classId() == QPixmapData::X11Class ? static_cast<QX11PixmapData*>(data.data()) : 0;
       
   646     if (x11Data) {
       
   647         pm.x11SetScreen(x11Data->xinfo.screen());
       
   648         uninit = x11Data->flags & QX11PixmapData::Uninitialized;
       
   649     }
       
   650 #elif defined(Q_WS_MAC)
       
   651     QMacPixmapData *macData = data->classId() == QPixmapData::MacClass ? static_cast<QMacPixmapData*>(data.data()) : 0;
       
   652     if (macData)
       
   653         uninit = macData->uninit;
       
   654 #endif
       
   655     if (!uninit && !isNull()) {
       
   656         // Copy old pixmap
       
   657         if (hasAlphaChannel())
       
   658             pm.fill(Qt::transparent);
       
   659         QPainter p(&pm);
       
   660         p.drawPixmap(0, 0, *this, 0, 0, qMin(width(), w), qMin(height(), h));
       
   661     }
       
   662 
       
   663 #if defined(Q_WS_X11)
       
   664     if (x11Data && x11Data->x11_mask) {
       
   665         QX11PixmapData *pmData = static_cast<QX11PixmapData*>(pm.data.data());
       
   666         pmData->x11_mask = (Qt::HANDLE)XCreatePixmap(X11->display,
       
   667                                                      RootWindow(x11Data->xinfo.display(),
       
   668                                                                 x11Data->xinfo.screen()),
       
   669                                                       w, h, 1);
       
   670         GC gc = XCreateGC(X11->display, pmData->x11_mask, 0, 0);
       
   671         XCopyArea(X11->display, x11Data->x11_mask, pmData->x11_mask, gc, 0, 0, qMin(width(), w), qMin(height(), h), 0, 0);
       
   672         XFreeGC(X11->display, gc);
       
   673     }
       
   674 #endif
       
   675     *this = pm;
       
   676 }
       
   677 #endif
       
   678 
       
   679 /*!
       
   680     \fn void QPixmap::resize(int width, int height)
       
   681     \compat
       
   682 
       
   683     Use QPixmap::copy() instead to get the pixmap with the new size.
       
   684 
       
   685     \oldcode
       
   686         pixmap.resize(10, 20);
       
   687     \newcode
       
   688         pixmap = pixmap.copy(0, 0, 10, 20);
       
   689     \endcode
       
   690 */
       
   691 
       
   692 /*!
       
   693     \fn bool QPixmap::selfMask() const
       
   694     \compat
       
   695 
       
   696     Returns whether the pixmap is its own mask or not.
       
   697 
       
   698     This function is no longer relevant since the concept of self
       
   699     masking doesn't exists anymore.
       
   700 */
       
   701 
       
   702 /*!
       
   703     Sets a mask bitmap.
       
   704 
       
   705     This function merges the \a mask with the pixmap's alpha channel. A pixel
       
   706     value of 1 on the mask means the pixmap's pixel is unchanged; a value of 0
       
   707     means the pixel is transparent. The mask must have the same size as this
       
   708     pixmap.
       
   709 
       
   710     Setting a null mask resets the mask, leaving the previously transparent
       
   711     pixels black. The effect of this function is undefined when the pixmap is
       
   712     being painted on.
       
   713 
       
   714     \warning This is potentially an expensive operation.
       
   715 
       
   716     \sa mask(), {QPixmap#Pixmap Transformations}{Pixmap Transformations},
       
   717     QBitmap
       
   718 */
       
   719 void QPixmap::setMask(const QBitmap &mask)
       
   720 {
       
   721     if (paintingActive()) {
       
   722         qWarning("QPixmap::setMask: Cannot set mask while pixmap is being painted on");
       
   723         return;
       
   724     }
       
   725 
       
   726     if (!mask.isNull() && mask.size() != size()) {
       
   727         qWarning("QPixmap::setMask() mask size differs from pixmap size");
       
   728         return;
       
   729     }
       
   730 
       
   731     if (static_cast<const QPixmap &>(mask).data == data) // trying to selfmask
       
   732        return;
       
   733 
       
   734     detach();
       
   735     data->setMask(mask);
       
   736 }
       
   737 
       
   738 #ifndef QT_NO_IMAGE_HEURISTIC_MASK
       
   739 /*!
       
   740     Creates and returns a heuristic mask for this pixmap.
       
   741 
       
   742     The function works by selecting a color from one of the corners
       
   743     and then chipping away pixels of that color, starting at all the
       
   744     edges.  If \a clipTight is true (the default) the mask is just
       
   745     large enough to cover the pixels; otherwise, the mask is larger
       
   746     than the data pixels.
       
   747 
       
   748     The mask may not be perfect but it should be reasonable, so you
       
   749     can do things such as the following:
       
   750 
       
   751     \snippet doc/src/snippets/code/src_gui_image_qpixmap.cpp 1
       
   752 
       
   753     This function is slow because it involves converting to/from a
       
   754     QImage, and non-trivial computations.
       
   755 
       
   756     \sa QImage::createHeuristicMask(), createMaskFromColor()
       
   757 */
       
   758 QBitmap QPixmap::createHeuristicMask(bool clipTight) const
       
   759 {
       
   760     QBitmap m = QBitmap::fromImage(toImage().createHeuristicMask(clipTight));
       
   761     return m;
       
   762 }
       
   763 #endif
       
   764 
       
   765 /*!
       
   766     Creates and returns a mask for this pixmap based on the given \a
       
   767     maskColor. If the \a mode is Qt::MaskInColor, all pixels matching the
       
   768     maskColor will be opaque. If \a mode is Qt::MaskOutColor, all pixels
       
   769     matching the maskColor will be transparent.
       
   770 
       
   771     This function is slow because it involves converting to/from a
       
   772     QImage.
       
   773 
       
   774     \sa createHeuristicMask(), QImage::createMaskFromColor()
       
   775 */
       
   776 QBitmap QPixmap::createMaskFromColor(const QColor &maskColor, Qt::MaskMode mode) const
       
   777 {
       
   778     QImage image = toImage().convertToFormat(QImage::Format_ARGB32);
       
   779     return QBitmap::fromImage(image.createMaskFromColor(maskColor.rgba(), mode));
       
   780 }
       
   781 
       
   782 /*! \overload
       
   783 
       
   784     Creates and returns a mask for this pixmap based on the given \a
       
   785     maskColor. Same as calling createMaskFromColor(maskColor,
       
   786     Qt::MaskInColor)
       
   787 
       
   788     \sa createHeuristicMask(), QImage::createMaskFromColor()
       
   789 */
       
   790 QBitmap QPixmap::createMaskFromColor(const QColor &maskColor) const
       
   791 {
       
   792     return createMaskFromColor(maskColor, Qt::MaskInColor);
       
   793 }
       
   794 
       
   795 /*!
       
   796     Loads a pixmap from the file with the given \a fileName. Returns
       
   797     true if the pixmap was successfully loaded; otherwise returns
       
   798     false.
       
   799 
       
   800     The loader attempts to read the pixmap using the specified \a
       
   801     format. If the \a format is not specified (which is the default),
       
   802     the loader probes the file for a header to guess the file format.
       
   803 
       
   804     The file name can either refer to an actual file on disk or to one
       
   805     of the application's embedded resources. See the
       
   806     \l{resources.html}{Resource System} overview for details on how to
       
   807     embed pixmaps and other resource files in the application's
       
   808     executable.
       
   809 
       
   810     If the data needs to be modified to fit in a lower-resolution
       
   811     result (e.g. converting from 32-bit to 8-bit), use the \a flags to
       
   812     control the conversion.
       
   813 
       
   814     Note that QPixmaps are automatically added to the QPixmapCache
       
   815     when loaded from a file; the key used is internal and can not
       
   816     be acquired.
       
   817 
       
   818     \sa loadFromData(), {QPixmap#Reading and Writing Image
       
   819     Files}{Reading and Writing Image Files}
       
   820 */
       
   821 
       
   822 bool QPixmap::load(const QString &fileName, const char *format, Qt::ImageConversionFlags flags)
       
   823 {
       
   824     if (fileName.isEmpty())
       
   825         return false;
       
   826 
       
   827     QFileInfo info(fileName);
       
   828     QString key = QLatin1String("qt_pixmap_") + info.absoluteFilePath() + QLatin1Char('_') + QString::number(info.lastModified().toTime_t()) + QLatin1Char('_') +
       
   829                   QString::number(info.size()) + QLatin1Char('_') + QString::number(data->pixelType());
       
   830 
       
   831     if (QPixmapCache::find(key, *this))
       
   832         return true;
       
   833 
       
   834     if (data->fromFile(fileName, format, flags)) {
       
   835         QPixmapCache::insert(key, *this);
       
   836         return true;
       
   837     }
       
   838 
       
   839     return false;
       
   840 }
       
   841 
       
   842 /*!
       
   843     \fn bool QPixmap::loadFromData(const uchar *data, uint len, const char *format, Qt::ImageConversionFlags flags)
       
   844 
       
   845     Loads a pixmap from the \a len first bytes of the given binary \a
       
   846     data.  Returns true if the pixmap was loaded successfully;
       
   847     otherwise returns false.
       
   848 
       
   849     The loader attempts to read the pixmap using the specified \a
       
   850     format. If the \a format is not specified (which is the default),
       
   851     the loader probes the file for a header to guess the file format.
       
   852 
       
   853     If the data needs to be modified to fit in a lower-resolution
       
   854     result (e.g. converting from 32-bit to 8-bit), use the \a flags to
       
   855     control the conversion.
       
   856 
       
   857     \sa load(), {QPixmap#Reading and Writing Image Files}{Reading and
       
   858     Writing Image Files}
       
   859 */
       
   860 
       
   861 bool QPixmap::loadFromData(const uchar *buf, uint len, const char *format, Qt::ImageConversionFlags flags)
       
   862 {
       
   863     return data->fromData(buf, len, format, flags);
       
   864 }
       
   865 
       
   866 /*!
       
   867     \fn bool QPixmap::loadFromData(const QByteArray &data, const char *format, Qt::ImageConversionFlags flags)
       
   868 
       
   869     \overload
       
   870 
       
   871     Loads a pixmap from the binary \a data using the specified \a
       
   872     format and conversion \a flags.
       
   873 */
       
   874 
       
   875 
       
   876 /*!
       
   877     Saves the pixmap to the file with the given \a fileName using the
       
   878     specified image file \a format and \a quality factor. Returns true
       
   879     if successful; otherwise returns false.
       
   880 
       
   881     The \a quality factor must be in the range [0,100] or -1. Specify
       
   882     0 to obtain small compressed files, 100 for large uncompressed
       
   883     files, and -1 to use the default settings.
       
   884 
       
   885     If \a format is 0, an image format will be chosen from \a fileName's
       
   886     suffix.
       
   887 
       
   888     \sa {QPixmap#Reading and Writing Image Files}{Reading and Writing
       
   889     Image Files}
       
   890 */
       
   891 
       
   892 bool QPixmap::save(const QString &fileName, const char *format, int quality) const
       
   893 {
       
   894     if (isNull())
       
   895         return false;                                // nothing to save
       
   896     QImageWriter writer(fileName, format);
       
   897     return doImageIO(&writer, quality);
       
   898 }
       
   899 
       
   900 /*!
       
   901     \overload
       
   902 
       
   903     This function writes a QPixmap to the given \a device using the
       
   904     specified image file \a format and \a quality factor. This can be
       
   905     used, for example, to save a pixmap directly into a QByteArray:
       
   906 
       
   907     \snippet doc/src/snippets/image/image.cpp 1
       
   908 */
       
   909 
       
   910 bool QPixmap::save(QIODevice* device, const char* format, int quality) const
       
   911 {
       
   912     if (isNull())
       
   913         return false;                                // nothing to save
       
   914     QImageWriter writer(device, format);
       
   915     return doImageIO(&writer, quality);
       
   916 }
       
   917 
       
   918 /*! \internal
       
   919 */
       
   920 bool QPixmap::doImageIO(QImageWriter *writer, int quality) const
       
   921 {
       
   922     if (quality > 100  || quality < -1)
       
   923         qWarning("QPixmap::save: quality out of range [-1,100]");
       
   924     if (quality >= 0)
       
   925         writer->setQuality(qMin(quality,100));
       
   926     return writer->write(toImage());
       
   927 }
       
   928 
       
   929 
       
   930 // The implementation (and documentation) of
       
   931 // QPixmap::fill(const QWidget *, const QPoint &)
       
   932 // is in qwidget.cpp
       
   933 
       
   934 /*!
       
   935     \fn void QPixmap::fill(const QWidget *widget, int x, int y)
       
   936     \overload
       
   937 
       
   938     Fills the pixmap with the \a widget's background color or pixmap.
       
   939     The given point, (\a x, \a y), defines an offset in widget
       
   940     coordinates to which the pixmap's top-left pixel will be mapped
       
   941     to.
       
   942 */
       
   943 
       
   944 /*!
       
   945     Fills the pixmap with the given \a color.
       
   946 
       
   947     The effect of this function is undefined when the pixmap is
       
   948     being painted on.
       
   949 
       
   950     \sa {QPixmap#Pixmap Transformations}{Pixmap Transformations}
       
   951 */
       
   952 
       
   953 void QPixmap::fill(const QColor &color)
       
   954 {
       
   955     if (isNull())
       
   956         return;
       
   957 
       
   958     // Some people are probably already calling fill while a painter is active, so to not break
       
   959     // their programs, only print a warning and return when the fill operation could cause a crash.
       
   960     if (paintingActive() && (color.alpha() != 255) && !hasAlphaChannel()) {
       
   961         qWarning("QPixmap::fill: Cannot fill while pixmap is being painted on");
       
   962         return;
       
   963     }
       
   964 
       
   965     if (data->ref == 1) {
       
   966         // detach() will also remove this pixmap from caches, so
       
   967         // it has to be called even when ref == 1.
       
   968         detach();
       
   969     } else {
       
   970         // Don't bother to make a copy of the data object, since
       
   971         // it will be filled with new pixel data anyway.
       
   972         QPixmapData *d = data->createCompatiblePixmapData();
       
   973         d->resize(data->width(), data->height());
       
   974         data = d;
       
   975     }
       
   976     data->fill(color);
       
   977 }
       
   978 
       
   979 /*! \obsolete
       
   980     Returns a number that identifies the contents of this QPixmap
       
   981     object. Distinct QPixmap objects can only have the same serial
       
   982     number if they refer to the same contents (but they don't have
       
   983     to).
       
   984 
       
   985     Use cacheKey() instead.
       
   986 
       
   987     \warning The serial number doesn't necessarily change when
       
   988     the pixmap is altered. This means that it may be dangerous to use
       
   989     it as a cache key. For caching pixmaps, we recommend using the
       
   990     QPixmapCache class whenever possible.
       
   991 */
       
   992 int QPixmap::serialNumber() const
       
   993 {
       
   994     if (isNull())
       
   995         return 0;
       
   996     return data->serialNumber();
       
   997 }
       
   998 
       
   999 /*!
       
  1000     Returns a number that identifies this QPixmap. Distinct QPixmap
       
  1001     objects can only have the same cache key if they refer to the same
       
  1002     contents.
       
  1003 
       
  1004     The cacheKey() will change when the pixmap is altered.
       
  1005 */
       
  1006 qint64 QPixmap::cacheKey() const
       
  1007 {
       
  1008     int classKey = data->classId();
       
  1009     if (classKey >= 1024)
       
  1010         classKey = -(classKey >> 10);
       
  1011     return ((((qint64) classKey) << 56)
       
  1012             | (((qint64) data->serialNumber()) << 32)
       
  1013             | ((qint64) (data->detach_no)));
       
  1014 }
       
  1015 
       
  1016 static void sendResizeEvents(QWidget *target)
       
  1017 {
       
  1018     QResizeEvent e(target->size(), QSize());
       
  1019     QApplication::sendEvent(target, &e);
       
  1020 
       
  1021     const QObjectList children = target->children();
       
  1022     for (int i = 0; i < children.size(); ++i) {
       
  1023         QWidget *child = static_cast<QWidget*>(children.at(i));
       
  1024         if (child->isWidgetType() && !child->isWindow() && child->testAttribute(Qt::WA_PendingResizeEvent))
       
  1025             sendResizeEvents(child);
       
  1026     }
       
  1027 }
       
  1028 
       
  1029 /*!
       
  1030     \fn QPixmap QPixmap::grabWidget(QWidget * widget, const QRect &rectangle)
       
  1031 
       
  1032     Creates a pixmap and paints the given \a widget, restricted by the
       
  1033     given \a rectangle, in it. If the \a widget has any children, then
       
  1034     they are also painted in the appropriate positions.
       
  1035 
       
  1036     If no rectangle is specified (the default) the entire widget is
       
  1037     painted.
       
  1038 
       
  1039     If \a widget is 0, the specified rectangle doesn't overlap the
       
  1040     widget's rectangle, or an error occurs, the function will return a
       
  1041     null QPixmap.  If the rectangle is a superset of the given \a
       
  1042     widget, the areas outside the \a widget are covered with the
       
  1043     widget's background.
       
  1044 
       
  1045     This function actually asks \a widget to paint itself (and its
       
  1046     children to paint themselves) by calling paintEvent() with painter
       
  1047     redirection turned on. But QPixmap also provides the grabWindow()
       
  1048     function which is a bit faster by grabbing pixels directly off the
       
  1049     screen. In addition, if there are overlaying windows,
       
  1050     grabWindow(), unlike grabWidget(), will see them.
       
  1051 
       
  1052     \warning Do not grab a widget from its QWidget::paintEvent().
       
  1053     However, it is safe to grab a widget from another widget's
       
  1054     \l {QWidget::}{paintEvent()}.
       
  1055 
       
  1056     \sa grabWindow()
       
  1057 */
       
  1058 
       
  1059 QPixmap QPixmap::grabWidget(QWidget * widget, const QRect &rect)
       
  1060 {
       
  1061     if (!widget)
       
  1062         return QPixmap();
       
  1063 
       
  1064     if (widget->testAttribute(Qt::WA_PendingResizeEvent) || !widget->testAttribute(Qt::WA_WState_Created))
       
  1065         sendResizeEvents(widget);
       
  1066 
       
  1067     QRect r(rect);
       
  1068     if (r.width() < 0)
       
  1069         r.setWidth(widget->width() - rect.x());
       
  1070     if (r.height() < 0)
       
  1071         r.setHeight(widget->height() - rect.y());
       
  1072 
       
  1073     if (!r.intersects(widget->rect()))
       
  1074         return QPixmap();
       
  1075 
       
  1076     QPixmap res(r.size());
       
  1077     widget->render(&res, QPoint(), r,
       
  1078                    QWidget::DrawWindowBackground | QWidget::DrawChildren | QWidget::IgnoreMask);
       
  1079     return res;
       
  1080 }
       
  1081 
       
  1082 /*!
       
  1083     \fn QPixmap QPixmap::grabWidget(QWidget *widget, int x, int y, int
       
  1084     width, int height)
       
  1085 
       
  1086     \overload
       
  1087 
       
  1088     Creates a pixmap and paints the given \a widget, restricted by
       
  1089     QRect(\a x, \a y, \a width, \a height), in it.
       
  1090 
       
  1091     \warning Do not grab a widget from its QWidget::paintEvent().
       
  1092     However, it is safe to grab a widget from another widget's
       
  1093     \l {QWidget::}{paintEvent()}.
       
  1094 */
       
  1095 
       
  1096 
       
  1097 /*!
       
  1098     \since 4.5
       
  1099 
       
  1100     \enum QPixmap::ShareMode
       
  1101 
       
  1102     This enum type defines the share modes that are available when
       
  1103     creating a QPixmap object from a raw X11 Pixmap handle.
       
  1104 
       
  1105     \value ImplicitlyShared  This mode will cause the QPixmap object to
       
  1106     create a copy of the internal data before it is modified, thus
       
  1107     keeping the original X11 pixmap intact.
       
  1108 
       
  1109     \value ExplicitlyShared  In this mode, the pixmap data will \e not be
       
  1110     copied before it is modified, which in effect will change the
       
  1111     original X11 pixmap.
       
  1112 
       
  1113     \warning This enum is only used for X11 specific functions; using
       
  1114     it is non-portable.
       
  1115 
       
  1116     \sa QPixmap::fromX11Pixmap()
       
  1117 */
       
  1118 
       
  1119 /*!
       
  1120     \since 4.5
       
  1121 
       
  1122     \fn QPixmap QPixmap::fromX11Pixmap(Qt::HANDLE pixmap, QPixmap::ShareMode mode)
       
  1123 
       
  1124     Creates a QPixmap from the native X11 Pixmap handle \a pixmap,
       
  1125     using \a mode as the share mode. The default share mode is
       
  1126     QPixmap::ImplicitlyShared, which means that a copy of the pixmap is
       
  1127     made if someone tries to modify it by e.g. drawing onto it.
       
  1128 
       
  1129     QPixmap does \e not take ownership of the \a pixmap handle, and
       
  1130     have to be deleted by the user.
       
  1131 
       
  1132     \warning This function is X11 specific; using it is non-portable.
       
  1133 
       
  1134     \sa QPixmap::ShareMode
       
  1135 */
       
  1136 
       
  1137 
       
  1138 #if defined(Q_WS_X11) || defined(Q_WS_QWS)
       
  1139 
       
  1140 /*!
       
  1141     Returns the pixmap's handle to the device context.
       
  1142 
       
  1143     Note that, since QPixmap make use of \l {Implicit Data
       
  1144     Sharing}{implicit data sharing}, the detach() function must be
       
  1145     called explicitly to ensure that only \e this pixmap's data is
       
  1146     modified if the pixmap data is shared.
       
  1147 
       
  1148     \warning This function is X11 specific; using it is non-portable.
       
  1149 
       
  1150     \sa detach()
       
  1151 */
       
  1152 
       
  1153 Qt::HANDLE QPixmap::handle() const
       
  1154 {
       
  1155 #if defined(Q_WS_X11)
       
  1156     if (data->classId() == QPixmapData::X11Class)
       
  1157         return static_cast<const QX11PixmapData*>(data.constData())->handle();
       
  1158 #endif
       
  1159     return 0;
       
  1160 }
       
  1161 #endif
       
  1162 
       
  1163 
       
  1164 #ifdef QT3_SUPPORT
       
  1165 static Qt::ImageConversionFlags colorModeToFlags(QPixmap::ColorMode mode)
       
  1166 {
       
  1167     Qt::ImageConversionFlags flags = Qt::AutoColor;
       
  1168     switch (mode) {
       
  1169       case QPixmap::Color:
       
  1170         flags |= Qt::ColorOnly;
       
  1171         break;
       
  1172       case QPixmap::Mono:
       
  1173         flags |= Qt::MonoOnly;
       
  1174         break;
       
  1175       default:
       
  1176         break;// Nothing.
       
  1177     }
       
  1178     return flags;
       
  1179 }
       
  1180 
       
  1181 /*!
       
  1182     Use the constructor that takes a Qt::ImageConversionFlag instead.
       
  1183 */
       
  1184 
       
  1185 QPixmap::QPixmap(const QString& fileName, const char *format, ColorMode mode)
       
  1186     : QPaintDevice()
       
  1187 {
       
  1188     init(0, 0, QPixmapData::PixmapType);
       
  1189     if (!qt_pixmap_thread_test())
       
  1190         return;
       
  1191 
       
  1192     load(fileName, format, colorModeToFlags(mode));
       
  1193 }
       
  1194 
       
  1195 /*!
       
  1196     Constructs a pixmap from the QImage \a image.
       
  1197 
       
  1198     Use the static fromImage() function instead.
       
  1199 */
       
  1200 QPixmap::QPixmap(const QImage& image)
       
  1201     : QPaintDevice()
       
  1202 {
       
  1203     init(0, 0, QPixmapData::PixmapType);
       
  1204     if (!qt_pixmap_thread_test())
       
  1205         return;
       
  1206 
       
  1207     if (data->pixelType() == QPixmapData::BitmapType)
       
  1208         *this = QBitmap::fromImage(image);
       
  1209     else
       
  1210         *this = fromImage(image);
       
  1211 }
       
  1212 
       
  1213 /*!
       
  1214     \overload
       
  1215 
       
  1216     Converts the given \a image to a pixmap that is assigned to this
       
  1217     pixmap.
       
  1218 
       
  1219     Use the static fromImage() function instead.
       
  1220 */
       
  1221 
       
  1222 QPixmap &QPixmap::operator=(const QImage &image)
       
  1223 {
       
  1224     if (data->pixelType() == QPixmapData::BitmapType)
       
  1225         *this = QBitmap::fromImage(image);
       
  1226     else
       
  1227         *this = fromImage(image);
       
  1228     return *this;
       
  1229 }
       
  1230 
       
  1231 /*!
       
  1232     Use the load() function that takes a Qt::ImageConversionFlag instead.
       
  1233 */
       
  1234 
       
  1235 bool QPixmap::load(const QString &fileName, const char *format, ColorMode mode)
       
  1236 {
       
  1237     return load(fileName, format, colorModeToFlags(mode));
       
  1238 }
       
  1239 
       
  1240 /*!
       
  1241     Use the loadFromData() function that takes a Qt::ImageConversionFlag instead.
       
  1242 */
       
  1243 
       
  1244 bool QPixmap::loadFromData(const uchar *buf, uint len, const char *format, ColorMode mode)
       
  1245 {
       
  1246     return loadFromData(buf, len, format, colorModeToFlags(mode));
       
  1247 }
       
  1248 
       
  1249 /*!
       
  1250     Use the static fromImage() function instead.
       
  1251 */
       
  1252 bool QPixmap::convertFromImage(const QImage &image, ColorMode mode)
       
  1253 {
       
  1254     if (data->pixelType() == QPixmapData::BitmapType)
       
  1255         *this = QBitmap::fromImage(image, colorModeToFlags(mode));
       
  1256     else
       
  1257         *this = fromImage(image, colorModeToFlags(mode));
       
  1258     return !isNull();
       
  1259 }
       
  1260 
       
  1261 #endif
       
  1262 
       
  1263 /*****************************************************************************
       
  1264   QPixmap stream functions
       
  1265  *****************************************************************************/
       
  1266 #if !defined(QT_NO_DATASTREAM)
       
  1267 /*!
       
  1268     \relates QPixmap
       
  1269 
       
  1270     Writes the given \a pixmap to the given \a stream as a PNG
       
  1271     image. Note that writing the stream to a file will not produce a
       
  1272     valid image file.
       
  1273 
       
  1274     \sa QPixmap::save(), {Format of the QDataStream Operators}
       
  1275 */
       
  1276 
       
  1277 QDataStream &operator<<(QDataStream &stream, const QPixmap &pixmap)
       
  1278 {
       
  1279     return stream << pixmap.toImage();
       
  1280 }
       
  1281 
       
  1282 /*!
       
  1283     \relates QPixmap
       
  1284 
       
  1285     Reads an image from the given \a stream into the given \a pixmap.
       
  1286 
       
  1287     \sa QPixmap::load(), {Format of the QDataStream Operators}
       
  1288 */
       
  1289 
       
  1290 QDataStream &operator>>(QDataStream &stream, QPixmap &pixmap)
       
  1291 {
       
  1292     QImage image;
       
  1293     stream >> image;
       
  1294 
       
  1295     if (image.isNull()) {
       
  1296         pixmap = QPixmap();
       
  1297     } else if (image.depth() == 1) {
       
  1298         pixmap = QBitmap::fromImage(image);
       
  1299     } else {
       
  1300         pixmap = QPixmap::fromImage(image);
       
  1301     }
       
  1302     return stream;
       
  1303 }
       
  1304 
       
  1305 #endif // QT_NO_DATASTREAM
       
  1306 
       
  1307 #ifdef QT3_SUPPORT
       
  1308 Q_GUI_EXPORT void copyBlt(QPixmap *dst, int dx, int dy,
       
  1309                           const QPixmap *src, int sx, int sy, int sw, int sh)
       
  1310 {
       
  1311     Q_ASSERT_X(dst, "::copyBlt", "Destination pixmap must be non-null");
       
  1312     Q_ASSERT_X(src, "::copyBlt", "Source pixmap must be non-null");
       
  1313 
       
  1314     if (src->hasAlphaChannel()) {
       
  1315         if (dst->paintEngine()->hasFeature(QPaintEngine::PorterDuff)) {
       
  1316             QPainter p(dst);
       
  1317             p.setCompositionMode(QPainter::CompositionMode_Source);
       
  1318             p.drawPixmap(dx, dy, *src, sx, sy, sw, sh);
       
  1319         } else {
       
  1320             QImage image = dst->toImage().convertToFormat(QImage::Format_ARGB32_Premultiplied);
       
  1321             QPainter p(&image);
       
  1322             p.setCompositionMode(QPainter::CompositionMode_Source);
       
  1323             p.drawPixmap(dx, dy, *src, sx, sy, sw, sh);
       
  1324             p.end();
       
  1325             *dst = QPixmap::fromImage(image);
       
  1326         }
       
  1327     } else {
       
  1328         QPainter p(dst);
       
  1329         p.drawPixmap(dx, dy, *src, sx, sy, sw, sh);
       
  1330     }
       
  1331 
       
  1332 }
       
  1333 #endif
       
  1334 
       
  1335 /*!
       
  1336     \internal
       
  1337 */
       
  1338 
       
  1339 bool QPixmap::isDetached() const
       
  1340 {
       
  1341     return data->ref == 1;
       
  1342 }
       
  1343 
       
  1344 /*! \internal
       
  1345   ### Qt5 - remove me.
       
  1346 */
       
  1347 void QPixmap::deref()
       
  1348 {
       
  1349     Q_ASSERT_X(false, "QPixmap::deref()", "Do not call this function anymore!");
       
  1350 }
       
  1351 
       
  1352 /*!
       
  1353     \fn QImage QPixmap::convertToImage() const
       
  1354 
       
  1355     Use the toImage() function instead.
       
  1356 */
       
  1357 
       
  1358 /*!
       
  1359     \fn bool QPixmap::convertFromImage(const QImage &image, Qt::ImageConversionFlags flags)
       
  1360 
       
  1361     Use the static fromImage() function instead.
       
  1362 */
       
  1363 
       
  1364 /*!
       
  1365     \fn QPixmap QPixmap::xForm(const QMatrix &matrix) const
       
  1366 
       
  1367     Use transformed() instead.
       
  1368 */
       
  1369 
       
  1370 /*!
       
  1371     \fn QPixmap QPixmap::scaled(int width, int height,
       
  1372     Qt::AspectRatioMode aspectRatioMode, Qt::TransformationMode
       
  1373     transformMode) const
       
  1374 
       
  1375     \overload
       
  1376 
       
  1377     Returns a copy of the pixmap scaled to a rectangle with the given
       
  1378     \a width and \a height according to the given \a aspectRatioMode and
       
  1379     \a transformMode.
       
  1380 
       
  1381     If either the \a width or the \a height is zero or negative, this
       
  1382     function returns a null pixmap.
       
  1383 */
       
  1384 
       
  1385 /*!
       
  1386     \fn QPixmap QPixmap::scaled(const QSize &size, Qt::AspectRatioMode
       
  1387     aspectRatioMode, Qt::TransformationMode transformMode) const
       
  1388 
       
  1389     Scales the pixmap to the given \a size, using the aspect ratio and
       
  1390     transformation modes specified by \a aspectRatioMode and \a
       
  1391     transformMode.
       
  1392 
       
  1393     \image qimage-scaling.png
       
  1394 
       
  1395     \list
       
  1396     \i If \a aspectRatioMode is Qt::IgnoreAspectRatio, the pixmap
       
  1397        is scaled to \a size.
       
  1398     \i If \a aspectRatioMode is Qt::KeepAspectRatio, the pixmap is
       
  1399        scaled to a rectangle as large as possible inside \a size, preserving the aspect ratio.
       
  1400     \i If \a aspectRatioMode is Qt::KeepAspectRatioByExpanding,
       
  1401        the pixmap is scaled to a rectangle as small as possible
       
  1402        outside \a size, preserving the aspect ratio.
       
  1403     \endlist
       
  1404 
       
  1405     If the given \a size is empty, this function returns a null
       
  1406     pixmap.
       
  1407 
       
  1408 
       
  1409     In some cases it can be more beneficial to draw the pixmap to a
       
  1410     painter with a scale set rather than scaling the pixmap. This is
       
  1411     the case when the painter is for instance based on OpenGL or when
       
  1412     the scale factor changes rapidly.
       
  1413 
       
  1414     \sa isNull(), {QPixmap#Pixmap Transformations}{Pixmap
       
  1415     Transformations}
       
  1416 
       
  1417 */
       
  1418 QPixmap QPixmap::scaled(const QSize& s, Qt::AspectRatioMode aspectMode, Qt::TransformationMode mode) const
       
  1419 {
       
  1420     if (isNull()) {
       
  1421         qWarning("QPixmap::scaled: Pixmap is a null pixmap");
       
  1422         return QPixmap();
       
  1423     }
       
  1424     if (s.isEmpty())
       
  1425         return QPixmap();
       
  1426 
       
  1427     QSize newSize = size();
       
  1428     newSize.scale(s, aspectMode);
       
  1429     if (newSize == size())
       
  1430         return *this;
       
  1431 
       
  1432     QTransform wm = QTransform::fromScale((qreal)newSize.width() / width(),
       
  1433                                           (qreal)newSize.height() / height());
       
  1434     QPixmap pix = transformed(wm, mode);
       
  1435     return pix;
       
  1436 }
       
  1437 
       
  1438 /*!
       
  1439     \fn QPixmap QPixmap::scaledToWidth(int width, Qt::TransformationMode
       
  1440     mode) const
       
  1441 
       
  1442     Returns a scaled copy of the image. The returned image is scaled
       
  1443     to the given \a width using the specified transformation \a mode.
       
  1444     The height of the pixmap is automatically calculated so that the
       
  1445     aspect ratio of the pixmap is preserved.
       
  1446 
       
  1447     If \a width is 0 or negative, a null pixmap is returned.
       
  1448 
       
  1449     \sa isNull(), {QPixmap#Pixmap Transformations}{Pixmap
       
  1450     Transformations}
       
  1451 */
       
  1452 QPixmap QPixmap::scaledToWidth(int w, Qt::TransformationMode mode) const
       
  1453 {
       
  1454     if (isNull()) {
       
  1455         qWarning("QPixmap::scaleWidth: Pixmap is a null pixmap");
       
  1456         return copy();
       
  1457     }
       
  1458     if (w <= 0)
       
  1459         return QPixmap();
       
  1460 
       
  1461     qreal factor = (qreal) w / width();
       
  1462     QTransform wm = QTransform::fromScale(factor, factor);
       
  1463     return transformed(wm, mode);
       
  1464 }
       
  1465 
       
  1466 /*!
       
  1467     \fn QPixmap QPixmap::scaledToHeight(int height,
       
  1468     Qt::TransformationMode mode) const
       
  1469 
       
  1470     Returns a scaled copy of the image. The returned image is scaled
       
  1471     to the given \a height using the specified transformation \a mode.
       
  1472     The width of the pixmap is automatically calculated so that the
       
  1473     aspect ratio of the pixmap is preserved.
       
  1474 
       
  1475     If \a height is 0 or negative, a null pixmap is returned.
       
  1476 
       
  1477     \sa isNull(), {QPixmap#Pixmap Transformations}{Pixmap
       
  1478     Transformations}
       
  1479 */
       
  1480 QPixmap QPixmap::scaledToHeight(int h, Qt::TransformationMode mode) const
       
  1481 {
       
  1482     if (isNull()) {
       
  1483         qWarning("QPixmap::scaleHeight: Pixmap is a null pixmap");
       
  1484         return copy();
       
  1485     }
       
  1486     if (h <= 0)
       
  1487         return QPixmap();
       
  1488 
       
  1489     qreal factor = (qreal) h / height();
       
  1490     QTransform wm = QTransform::fromScale(factor, factor);
       
  1491     return transformed(wm, mode);
       
  1492 }
       
  1493 
       
  1494 /*!
       
  1495     Returns a copy of the pixmap that is transformed using the given
       
  1496     transformation \a transform and transformation \a mode. The original
       
  1497     pixmap is not changed.
       
  1498 
       
  1499     The transformation \a transform is internally adjusted to compensate
       
  1500     for unwanted translation; i.e. the pixmap produced is the smallest
       
  1501     pixmap that contains all the transformed points of the original
       
  1502     pixmap. Use the trueMatrix() function to retrieve the actual
       
  1503     matrix used for transforming the pixmap.
       
  1504 
       
  1505     This function is slow because it involves transformation to a
       
  1506     QImage, non-trivial computations and a transformation back to a
       
  1507     QPixmap.
       
  1508 
       
  1509     \sa trueMatrix(), {QPixmap#Pixmap Transformations}{Pixmap
       
  1510     Transformations}
       
  1511 */
       
  1512 QPixmap QPixmap::transformed(const QTransform &transform,
       
  1513                              Qt::TransformationMode mode) const
       
  1514 {
       
  1515     if (isNull() || transform.type() <= QTransform::TxTranslate)
       
  1516         return *this;
       
  1517 
       
  1518     return data->transformed(transform, mode);
       
  1519 }
       
  1520 
       
  1521 /*!
       
  1522   \overload
       
  1523 
       
  1524   This convenience function loads the \a matrix into a
       
  1525   QTransform and calls the overloaded function.
       
  1526  */
       
  1527 QPixmap QPixmap::transformed(const QMatrix &matrix, Qt::TransformationMode mode) const
       
  1528 {
       
  1529     return transformed(QTransform(matrix), mode);
       
  1530 }
       
  1531 
       
  1532 
       
  1533 
       
  1534 
       
  1535 
       
  1536 
       
  1537 
       
  1538 
       
  1539 /*!
       
  1540     \class QPixmap
       
  1541 
       
  1542     \brief The QPixmap class is an off-screen image representation
       
  1543     that can be used as a paint device.
       
  1544 
       
  1545     \ingroup painting
       
  1546     \ingroup shared
       
  1547 
       
  1548 
       
  1549     Qt provides four classes for handling image data: QImage, QPixmap,
       
  1550     QBitmap and QPicture. QImage is designed and optimized for I/O,
       
  1551     and for direct pixel access and manipulation, while QPixmap is
       
  1552     designed and optimized for showing images on screen. QBitmap is
       
  1553     only a convenience class that inherits QPixmap, ensuring a depth
       
  1554     of 1. The isQBitmap() function returns true if a QPixmap object is
       
  1555     really a bitmap, otherwise returns false. Finally, the QPicture class
       
  1556     is a paint device that records and replays QPainter commands.
       
  1557 
       
  1558     A QPixmap can easily be displayed on the screen using QLabel or
       
  1559     one of QAbstractButton's subclasses (such as QPushButton and
       
  1560     QToolButton). QLabel has a pixmap property, whereas
       
  1561     QAbstractButton has an icon property.
       
  1562 
       
  1563     In addition to the ordinary constructors, a QPixmap can be
       
  1564     constructed using the static grabWidget() and grabWindow()
       
  1565     functions which creates a QPixmap and paints the given widget, or
       
  1566     window, into it.
       
  1567 
       
  1568     QPixmap objects can be passed around by value since the QPixmap
       
  1569     class uses implicit data sharing. For more information, see the \l
       
  1570     {Implicit Data Sharing} documentation. QPixmap objects can also be
       
  1571     streamed.
       
  1572 
       
  1573     Depending on the system, QPixmap is stored using a RGB32 or a
       
  1574     premultiplied alpha format. If the image has an alpha channel, and
       
  1575     if the system allows, the preferred format is premultiplied alpha.
       
  1576     Note also that QPixmap, unlike QImage, may be hardware dependent.
       
  1577     On X11, Mac and Symbian, a QPixmap is stored on the server side while
       
  1578     a QImage is stored on the client side (on Windows, these two classes
       
  1579     have an equivalent internal representation, i.e. both QImage and
       
  1580     QPixmap are stored on the client side and don't use any GDI
       
  1581     resources).
       
  1582 
       
  1583     Note that the pixel data in a pixmap is internal and is managed by
       
  1584     the underlying window system. Because QPixmap is a QPaintDevice
       
  1585     subclass, QPainter can be used to draw directly onto pixmaps.
       
  1586     Pixels can only be accessed through QPainter functions or by
       
  1587     converting the QPixmap to a QImage. However, the fill() function
       
  1588     is available for initializing the entire pixmap with a given color.
       
  1589 
       
  1590     There are functions to convert between QImage and
       
  1591     QPixmap. Typically, the QImage class is used to load an image
       
  1592     file, optionally manipulating the image data, before the QImage
       
  1593     object is converted into a QPixmap to be shown on
       
  1594     screen. Alternatively, if no manipulation is desired, the image
       
  1595     file can be loaded directly into a QPixmap. On Windows, the
       
  1596     QPixmap class also supports conversion between \c HBITMAP and
       
  1597     QPixmap. On Symbian, the QPixmap class also supports conversion
       
  1598     between CFbsBitmap and QPixmap.
       
  1599 
       
  1600     QPixmap provides a collection of functions that can be used to
       
  1601     obtain a variety of information about the pixmap. In addition,
       
  1602     there are several functions that enables transformation of the
       
  1603     pixmap.
       
  1604 
       
  1605     \tableofcontents
       
  1606 
       
  1607     \section1 Reading and Writing Image Files
       
  1608 
       
  1609     QPixmap provides several ways of reading an image file: The file
       
  1610     can be loaded when constructing the QPixmap object, or by using
       
  1611     the load() or loadFromData() functions later on. When loading an
       
  1612     image, the file name can either refer to an actual file on disk or
       
  1613     to one of the application's embedded resources. See \l{The Qt
       
  1614     Resource System} overview for details on how to embed images and
       
  1615     other resource files in the application's executable.
       
  1616 
       
  1617     Simply call the save() function to save a QPixmap object.
       
  1618 
       
  1619     The complete list of supported file formats are available through
       
  1620     the QImageReader::supportedImageFormats() and
       
  1621     QImageWriter::supportedImageFormats() functions. New file formats
       
  1622     can be added as plugins. By default, Qt supports the following
       
  1623     formats:
       
  1624 
       
  1625     \table
       
  1626     \header \o Format \o Description                      \o Qt's support
       
  1627     \row    \o BMP    \o Windows Bitmap                   \o Read/write
       
  1628     \row    \o GIF    \o Graphic Interchange Format (optional) \o Read
       
  1629     \row    \o JPG    \o Joint Photographic Experts Group \o Read/write
       
  1630     \row    \o JPEG   \o Joint Photographic Experts Group \o Read/write
       
  1631     \row    \o PNG    \o Portable Network Graphics        \o Read/write
       
  1632     \row    \o PBM    \o Portable Bitmap                  \o Read
       
  1633     \row    \o PGM    \o Portable Graymap                 \o Read
       
  1634     \row    \o PPM    \o Portable Pixmap                  \o Read/write
       
  1635     \row    \o XBM    \o X11 Bitmap                       \o Read/write
       
  1636     \row    \o XPM    \o X11 Pixmap                       \o Read/write
       
  1637     \endtable
       
  1638 
       
  1639     \section1 Pixmap Information
       
  1640 
       
  1641     QPixmap provides a collection of functions that can be used to
       
  1642     obtain a variety of information about the pixmap:
       
  1643 
       
  1644     \table
       
  1645     \header
       
  1646     \o \o Available Functions
       
  1647     \row
       
  1648     \o Geometry
       
  1649     \o
       
  1650     The size(), width() and height() functions provide information
       
  1651     about the pixmap's size. The rect() function returns the image's
       
  1652     enclosing rectangle.
       
  1653 
       
  1654     \row
       
  1655     \o Alpha component
       
  1656     \o
       
  1657 
       
  1658     The hasAlphaChannel() returns true if the pixmap has a format that
       
  1659     respects the alpha channel, otherwise returns false, while the
       
  1660     hasAlpha() function returns true if the pixmap has an alpha
       
  1661     channel \e or a mask (otherwise false). The mask() function returns
       
  1662     the mask as a QBitmap object, which can be set using setMask().
       
  1663 
       
  1664     The createHeuristicMask() function creates and returns a 1-bpp
       
  1665     heuristic mask (i.e. a QBitmap) for this pixmap. It works by
       
  1666     selecting a color from one of the corners and then chipping away
       
  1667     pixels of that color, starting at all the edges. The
       
  1668     createMaskFromColor() function creates and returns a mask (i.e. a
       
  1669     QBitmap) for the pixmap based on a given color.
       
  1670 
       
  1671     \row
       
  1672     \o Low-level information
       
  1673     \o
       
  1674 
       
  1675     The depth() function returns the depth of the pixmap. The
       
  1676     defaultDepth() function returns the default depth, i.e. the depth
       
  1677     used by the application on the given screen.
       
  1678 
       
  1679     The cacheKey() function returns a number that uniquely
       
  1680     identifies the contents of the QPixmap object.
       
  1681 
       
  1682     The x11Info() function returns information about the configuration
       
  1683     of the X display used by the screen to which the pixmap currently
       
  1684     belongs. The x11PictureHandle() function returns the X11 Picture
       
  1685     handle of the pixmap for XRender support. Note that the two latter
       
  1686     functions are only available on x11.
       
  1687 
       
  1688     \endtable
       
  1689 
       
  1690     \section1 Pixmap Conversion
       
  1691 
       
  1692     A QPixmap object can be converted into a QImage using the
       
  1693     toImage() function. Likewise, a QImage can be converted into a
       
  1694     QPixmap using the fromImage(). If this is too expensive an
       
  1695     operation, you can use QBitmap::fromImage() instead.
       
  1696 
       
  1697     In addition, on Windows, the QPixmap class supports conversion to
       
  1698     and from HBITMAP: the toWinHBITMAP() function creates a HBITMAP
       
  1699     equivalent to the QPixmap, based on the given HBitmapFormat, and
       
  1700     returns the HBITMAP handle. The fromWinHBITMAP() function returns
       
  1701     a QPixmap that is equivalent to the given bitmap which has the
       
  1702     specified format. The QPixmap class also supports conversion to
       
  1703     and from HICON: the toWinHICON() function creates a HICON equivalent
       
  1704     to the QPixmap, and returns the HICON handle. The fromWinHICON()
       
  1705     function returns a QPixmap that is equivalent to the given icon.
       
  1706 
       
  1707     In addition, on Symbian, the QPixmap class supports conversion to
       
  1708     and from CFbsBitmap: the toSymbianCFbsBitmap() function creates
       
  1709     CFbsBitmap equivalent to the QPixmap, based on given mode and returns
       
  1710     a CFbsBitmap object. The fromSymbianCFbsBitmap() function returns a
       
  1711     QPixmap that is equivalent to the given bitmap and given mode.
       
  1712 
       
  1713     \section1 Pixmap Transformations
       
  1714 
       
  1715     QPixmap supports a number of functions for creating a new pixmap
       
  1716     that is a transformed version of the original:
       
  1717 
       
  1718     The scaled(), scaledToWidth() and scaledToHeight() functions
       
  1719     return scaled copies of the pixmap, while the copy() function
       
  1720     creates a QPixmap that is a plain copy of the original one.
       
  1721 
       
  1722     The transformed() function returns a copy of the pixmap that is
       
  1723     transformed with the given transformation matrix and
       
  1724     transformation mode: Internally, the transformation matrix is
       
  1725     adjusted to compensate for unwanted translation,
       
  1726     i.e. transformed() returns the smallest pixmap containing all
       
  1727     transformed points of the original pixmap. The static trueMatrix()
       
  1728     function returns the actual matrix used for transforming the
       
  1729     pixmap.
       
  1730 
       
  1731     \sa QBitmap, QImage, QImageReader, QImageWriter
       
  1732 */
       
  1733 
       
  1734 
       
  1735 /*!
       
  1736     \typedef QPixmap::DataPtr
       
  1737     \internal
       
  1738 */
       
  1739 
       
  1740 /*!
       
  1741     \fn DataPtr &QPixmap::data_ptr()
       
  1742     \internal
       
  1743 */
       
  1744 
       
  1745 /*!
       
  1746     Returns true if this pixmap has an alpha channel, \e or has a
       
  1747     mask, otherwise returns false.
       
  1748 
       
  1749     \sa hasAlphaChannel(), mask()
       
  1750 */
       
  1751 bool QPixmap::hasAlpha() const
       
  1752 {
       
  1753     return (data->hasAlphaChannel() || !data->mask().isNull());
       
  1754 }
       
  1755 
       
  1756 /*!
       
  1757     Returns true if the pixmap has a format that respects the alpha
       
  1758     channel, otherwise returns false.
       
  1759 
       
  1760     \sa hasAlpha()
       
  1761 */
       
  1762 bool QPixmap::hasAlphaChannel() const
       
  1763 {
       
  1764     return data->hasAlphaChannel();
       
  1765 }
       
  1766 
       
  1767 /*!
       
  1768     \internal
       
  1769 */
       
  1770 int QPixmap::metric(PaintDeviceMetric metric) const
       
  1771 {
       
  1772     return data->metric(metric);
       
  1773 }
       
  1774 
       
  1775 /*!
       
  1776     \fn void QPixmap::setAlphaChannel(const QPixmap &alphaChannel)
       
  1777     \obsolete
       
  1778 
       
  1779     Sets the alpha channel of this pixmap to the given \a alphaChannel
       
  1780     by converting the \a alphaChannel into 32 bit and using the
       
  1781     intensity of the RGB pixel values.
       
  1782 
       
  1783     The effect of this function is undefined when the pixmap is being
       
  1784     painted on.
       
  1785 
       
  1786     \warning This is potentially an expensive operation. Most usecases
       
  1787     for this function are covered by QPainter and compositionModes
       
  1788     which will normally execute faster.
       
  1789 
       
  1790     \sa alphaChannel(), {QPixmap#Pixmap Transformations}{Pixmap
       
  1791     Transformations}
       
  1792  */
       
  1793 void QPixmap::setAlphaChannel(const QPixmap &alphaChannel)
       
  1794 {
       
  1795     if (alphaChannel.isNull())
       
  1796         return;
       
  1797 
       
  1798     if (paintingActive()) {
       
  1799         qWarning("QPixmap::setAlphaChannel: "
       
  1800                  "Cannot set alpha channel while pixmap is being painted on");
       
  1801         return;
       
  1802     }
       
  1803 
       
  1804     if (width() != alphaChannel.width() && height() != alphaChannel.height()) {
       
  1805         qWarning("QPixmap::setAlphaChannel: "
       
  1806                  "The pixmap and the alpha channel pixmap must have the same size");
       
  1807         return;
       
  1808     }
       
  1809 
       
  1810     detach();
       
  1811     data->setAlphaChannel(alphaChannel);
       
  1812 }
       
  1813 
       
  1814 /*!
       
  1815     \obsolete
       
  1816 
       
  1817     Returns the alpha channel of the pixmap as a new grayscale QPixmap in which
       
  1818     each pixel's red, green, and blue values are given the alpha value of the
       
  1819     original pixmap. The color depth of the returned pixmap is the system depth
       
  1820     on X11 and 8-bit on Windows and Mac OS X.
       
  1821 
       
  1822     You can use this function while debugging
       
  1823     to get a visible image of the alpha channel. If the pixmap doesn't have an
       
  1824     alpha channel, i.e., the alpha channel's value for all pixels equals
       
  1825     0xff), a null pixmap is returned. You can check this with the \c isNull()
       
  1826     function.
       
  1827 
       
  1828     We show an example:
       
  1829 
       
  1830     \snippet doc/src/snippets/alphachannel.cpp 0
       
  1831 
       
  1832     \image alphachannelimage.png The pixmap and channelImage QPixmaps
       
  1833 
       
  1834     \warning This is an expensive operation. The alpha channel of the
       
  1835     pixmap is extracted dynamically from the pixeldata. Most usecases of this
       
  1836     function are covered by QPainter and compositionModes which will normally
       
  1837     execute faster.
       
  1838 
       
  1839     \sa setAlphaChannel(), {QPixmap#Pixmap Information}{Pixmap
       
  1840     Information}
       
  1841 */
       
  1842 QPixmap QPixmap::alphaChannel() const
       
  1843 {
       
  1844     return data->alphaChannel();
       
  1845 }
       
  1846 
       
  1847 /*!
       
  1848     \internal
       
  1849 */
       
  1850 QPaintEngine *QPixmap::paintEngine() const
       
  1851 {
       
  1852     return data->paintEngine();
       
  1853 }
       
  1854 
       
  1855 /*!
       
  1856     \fn QBitmap QPixmap::mask() const
       
  1857 
       
  1858     Extracts a bitmap mask from the pixmap's alpha channel.
       
  1859 
       
  1860     \warning This is potentially an expensive operation. The mask of
       
  1861     the pixmap is extracted dynamically from the pixeldata.
       
  1862 
       
  1863     \sa setMask(), {QPixmap#Pixmap Information}{Pixmap Information}
       
  1864 */
       
  1865 QBitmap QPixmap::mask() const
       
  1866 {
       
  1867     return data->mask();
       
  1868 }
       
  1869 
       
  1870 /*!
       
  1871     Returns the default pixmap depth used by the application.
       
  1872 
       
  1873     On Windows and Mac, the default depth is always 32. On X11 and
       
  1874     embedded, the depth of the screen will be returned by this
       
  1875     function.
       
  1876 
       
  1877     \sa depth(), QColormap::depth(), {QPixmap#Pixmap Information}{Pixmap Information}
       
  1878 
       
  1879 */
       
  1880 int QPixmap::defaultDepth()
       
  1881 {
       
  1882 #if defined(Q_WS_QWS)
       
  1883     return QScreen::instance()->depth();
       
  1884 #elif defined(Q_WS_X11)
       
  1885     return QX11Info::appDepth();
       
  1886 #elif defined(Q_WS_WINCE)
       
  1887     return QColormap::instance().depth();
       
  1888 #elif defined(Q_WS_WIN)
       
  1889     return 32; // XXX
       
  1890 #elif defined(Q_WS_MAC)
       
  1891     return 32;
       
  1892 #elif defined(Q_OS_SYMBIAN)
       
  1893     return S60->screenDepth;
       
  1894 #endif
       
  1895 }
       
  1896 
       
  1897 /*!
       
  1898     Detaches the pixmap from shared pixmap data.
       
  1899 
       
  1900     A pixmap is automatically detached by Qt whenever its contents are
       
  1901     about to change. This is done in almost all QPixmap member
       
  1902     functions that modify the pixmap (fill(), fromImage(),
       
  1903     load(), etc.), and in QPainter::begin() on a pixmap.
       
  1904 
       
  1905     There are two exceptions in which detach() must be called
       
  1906     explicitly, that is when calling the handle() or the
       
  1907     x11PictureHandle() function (only available on X11). Otherwise,
       
  1908     any modifications done using system calls, will be performed on
       
  1909     the shared data.
       
  1910 
       
  1911     The detach() function returns immediately if there is just a
       
  1912     single reference or if the pixmap has not been initialized yet.
       
  1913 */
       
  1914 void QPixmap::detach()
       
  1915 {
       
  1916     QPixmapData::ClassId id = data->classId();
       
  1917     if (id == QPixmapData::RasterClass) {
       
  1918         QRasterPixmapData *rasterData = static_cast<QRasterPixmapData*>(data.data());
       
  1919         rasterData->image.detach();
       
  1920     }
       
  1921 
       
  1922     if (data->is_cached && data->ref == 1)
       
  1923         QImagePixmapCleanupHooks::executePixmapModificationHooks(this);
       
  1924 
       
  1925 #if defined(Q_WS_MAC)
       
  1926     QMacPixmapData *macData = id == QPixmapData::MacClass ? static_cast<QMacPixmapData*>(data.data()) : 0;
       
  1927     if (macData) {
       
  1928         if (macData->cg_mask) {
       
  1929             CGImageRelease(macData->cg_mask);
       
  1930             macData->cg_mask = 0;
       
  1931         }
       
  1932     }
       
  1933 #endif
       
  1934 
       
  1935     if (data->ref != 1) {
       
  1936         *this = copy();
       
  1937     }
       
  1938     ++data->detach_no;
       
  1939 
       
  1940 #if defined(Q_WS_X11)
       
  1941     if (data->classId() == QPixmapData::X11Class) {
       
  1942         QX11PixmapData *d = static_cast<QX11PixmapData*>(data.data());
       
  1943         d->flags &= ~QX11PixmapData::Uninitialized;
       
  1944 
       
  1945         // reset the cache data
       
  1946         if (d->hd2) {
       
  1947             XFreePixmap(X11->display, d->hd2);
       
  1948             d->hd2 = 0;
       
  1949         }
       
  1950     }
       
  1951 #elif defined(Q_WS_MAC)
       
  1952     if (macData) {
       
  1953         macData->macReleaseCGImageRef();
       
  1954         macData->uninit = false;
       
  1955     }
       
  1956 #endif
       
  1957 }
       
  1958 
       
  1959 /*!
       
  1960     \fn QPixmap QPixmap::fromImage(const QImage &image, Qt::ImageConversionFlags flags)
       
  1961 
       
  1962     Converts the given \a image to a pixmap using the specified \a
       
  1963     flags to control the conversion.  The \a flags argument is a
       
  1964     bitwise-OR of the \l{Qt::ImageConversionFlags}. Passing 0 for \a
       
  1965     flags sets all the default options.
       
  1966 
       
  1967     In case of monochrome and 8-bit images, the image is first
       
  1968     converted to a 32-bit pixmap and then filled with the colors in
       
  1969     the color table. If this is too expensive an operation, you can
       
  1970     use QBitmap::fromImage() instead.
       
  1971 
       
  1972     \sa toImage(), {QPixmap#Pixmap Conversion}{Pixmap Conversion}
       
  1973 */
       
  1974 QPixmap QPixmap::fromImage(const QImage &image, Qt::ImageConversionFlags flags)
       
  1975 {
       
  1976     if (image.isNull())
       
  1977         return QPixmap();
       
  1978 
       
  1979     QGraphicsSystem* gs = QApplicationPrivate::graphicsSystem();
       
  1980     QScopedPointer<QPixmapData> data(gs ? gs->createPixmapData(QPixmapData::PixmapType)
       
  1981             : QGraphicsSystem::createDefaultPixmapData(QPixmapData::PixmapType));
       
  1982     data->fromImage(image, flags);
       
  1983     return QPixmap(data.take());
       
  1984 }
       
  1985 
       
  1986 /*!
       
  1987     \fn QPixmap QPixmap::grabWindow(WId window, int x, int y, int
       
  1988     width, int height)
       
  1989 
       
  1990     Creates and returns a pixmap constructed by grabbing the contents
       
  1991     of the given \a window restricted by QRect(\a x, \a y, \a width,
       
  1992     \a height).
       
  1993 
       
  1994     The arguments (\a{x}, \a{y}) specify the offset in the window,
       
  1995     whereas (\a{width}, \a{height}) specify the area to be copied.  If
       
  1996     \a width is negative, the function copies everything to the right
       
  1997     border of the window. If \a height is negative, the function
       
  1998     copies everything to the bottom of the window.
       
  1999 
       
  2000     The window system identifier (\c WId) can be retrieved using the
       
  2001     QWidget::winId() function. The rationale for using a window
       
  2002     identifier and not a QWidget, is to enable grabbing of windows
       
  2003     that are not part of the application, window system frames, and so
       
  2004     on.
       
  2005 
       
  2006     The grabWindow() function grabs pixels from the screen, not from
       
  2007     the window, i.e. if there is another window partially or entirely
       
  2008     over the one you grab, you get pixels from the overlying window,
       
  2009     too. The mouse cursor is generally not grabbed.
       
  2010 
       
  2011     Note on X11that if the given \a window doesn't have the same depth
       
  2012     as the root window, and another window partially or entirely
       
  2013     obscures the one you grab, you will \e not get pixels from the
       
  2014     overlying window.  The contents of the obscured areas in the
       
  2015     pixmap will be undefined and uninitialized.
       
  2016 
       
  2017     \warning In general, grabbing an area outside the screen is not
       
  2018     safe. This depends on the underlying window system.
       
  2019 
       
  2020     \sa grabWidget(), {Screenshot Example}
       
  2021 */
       
  2022 
       
  2023 /*!
       
  2024   \internal
       
  2025 */
       
  2026 QPixmapData* QPixmap::pixmapData() const
       
  2027 {
       
  2028     return data.data();
       
  2029 }
       
  2030 
       
  2031 /*!
       
  2032     \enum QPixmap::HBitmapFormat
       
  2033 
       
  2034     \bold{Win32 only:} This enum defines how the conversion between \c
       
  2035     HBITMAP and QPixmap is performed.
       
  2036 
       
  2037     \warning This enum is only available on Windows.
       
  2038 
       
  2039     \value NoAlpha The alpha channel is ignored and always treated as
       
  2040     being set to fully opaque. This is preferred if the \c HBITMAP is
       
  2041     used with standard GDI calls, such as \c BitBlt().
       
  2042 
       
  2043     \value PremultipliedAlpha The \c HBITMAP is treated as having an
       
  2044     alpha channel and premultiplied colors. This is preferred if the
       
  2045     \c HBITMAP is accessed through the \c AlphaBlend() GDI function.
       
  2046 
       
  2047     \value Alpha The \c HBITMAP is treated as having a plain alpha
       
  2048     channel. This is the preferred format if the \c HBITMAP is going
       
  2049     to be used as an application icon or systray icon.
       
  2050 
       
  2051     \sa fromWinHBITMAP(), toWinHBITMAP()
       
  2052 */
       
  2053 
       
  2054 /*! \fn HBITMAP QPixmap::toWinHBITMAP(HBitmapFormat format) const
       
  2055     \bold{Win32 only:} Creates a \c HBITMAP equivalent to the QPixmap,
       
  2056     based on the given \a format. Returns the \c HBITMAP handle.
       
  2057 
       
  2058     It is the caller's responsibility to free the \c HBITMAP data
       
  2059     after use.
       
  2060 
       
  2061     \warning This function is only available on Windows.
       
  2062 
       
  2063     \sa fromWinHBITMAP(), {QPixmap#Pixmap Conversion}{Pixmap Conversion}
       
  2064 */
       
  2065 
       
  2066 /*! \fn QPixmap QPixmap::fromWinHBITMAP(HBITMAP bitmap, HBitmapFormat format)
       
  2067     \bold{Win32 only:} Returns a QPixmap that is equivalent to the
       
  2068     given \a bitmap. The conversion is based on the specified \a
       
  2069     format.
       
  2070 
       
  2071     \warning This function is only available on Windows.
       
  2072 
       
  2073     \sa toWinHBITMAP(), {QPixmap#Pixmap Conversion}{Pixmap Conversion}
       
  2074 
       
  2075 */
       
  2076 
       
  2077 /*! \fn HICON QPixmap::toWinHICON() const
       
  2078     \since 4.6
       
  2079 
       
  2080     \bold{Win32 only:} Creates a \c HICON equivalent to the QPixmap.
       
  2081     Returns the \c HICON handle.
       
  2082 
       
  2083     It is the caller's responsibility to free the \c HICON data after use.
       
  2084 
       
  2085     \warning This function is only available on Windows.
       
  2086 
       
  2087     \sa fromWinHICON(), {QPixmap#Pixmap Conversion}{Pixmap Conversion}
       
  2088 */
       
  2089 
       
  2090 /*! \fn QPixmap QPixmap::fromWinHICON(HICON icon)
       
  2091     \since 4.6
       
  2092 
       
  2093     \bold{Win32 only:} Returns a QPixmap that is equivalent to the given
       
  2094     \a icon.
       
  2095 
       
  2096     \warning This function is only available on Windows.
       
  2097 
       
  2098     \sa toWinHICON(), {QPixmap#Pixmap Conversion}{Pixmap Conversion}
       
  2099 
       
  2100 */
       
  2101 
       
  2102 /*! \fn const QX11Info &QPixmap::x11Info() const
       
  2103     \bold{X11 only:} Returns information about the configuration of
       
  2104     the X display used by the screen to which the pixmap currently belongs.
       
  2105 
       
  2106     \warning This function is only available on X11.
       
  2107 
       
  2108     \sa {QPixmap#Pixmap Information}{Pixmap Information}
       
  2109 */
       
  2110 
       
  2111 /*! \fn Qt::HANDLE QPixmap::x11PictureHandle() const
       
  2112     \bold{X11 only:} Returns the X11 Picture handle of the pixmap for
       
  2113     XRender support.
       
  2114 
       
  2115     This function will return 0 if XRender support is not compiled
       
  2116     into Qt, if the XRender extension is not supported on the X11
       
  2117     display, or if the handle could not be created. Use of this
       
  2118     function is not portable.
       
  2119 
       
  2120     \warning This function is only available on X11.
       
  2121 
       
  2122     \sa {QPixmap#Pixmap Information}{Pixmap Information}
       
  2123 */
       
  2124 
       
  2125 /*! \fn int QPixmap::x11SetDefaultScreen(int screen)
       
  2126   \internal
       
  2127 */
       
  2128 
       
  2129 /*! \fn void QPixmap::x11SetScreen(int screen)
       
  2130   \internal
       
  2131 */
       
  2132 
       
  2133 /*! \fn QRgb* QPixmap::clut() const
       
  2134     \internal
       
  2135 */
       
  2136 
       
  2137 /*! \fn int QPixmap::numCols() const
       
  2138     \internal
       
  2139 */
       
  2140 
       
  2141 /*! \fn const uchar* QPixmap::qwsBits() const
       
  2142     \internal
       
  2143     \since 4.1
       
  2144 */
       
  2145 
       
  2146 /*! \fn int QPixmap::qwsBytesPerLine() const
       
  2147     \internal
       
  2148     \since 4.1
       
  2149 */
       
  2150 
       
  2151 QT_END_NAMESPACE