1
1
/****************************************************************************
3
3
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4
** All rights reserved.
4
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
** Licensees holding valid Qt Commercial licenses may use this file in
11
** accordance with the Qt Commercial License Agreement provided with the
12
** Software or, alternatively, in accordance with the terms contained in
13
** a written agreement between you and Nokia.
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
15
16
** GNU Lesser General Public License Usage
16
17
** Alternatively, this file may be used under the terms of the GNU Lesser
20
21
** ensure the GNU Lesser General Public License version 2.1 requirements
21
22
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23
** In addition, as a special exception, Nokia gives you certain
24
** additional rights. These rights are described in the Nokia Qt LGPL
25
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
28
** GNU General Public License Usage
29
** Alternatively, this file may be used under the terms of the GNU
30
** General Public License version 3.0 as published by the Free Software
31
** Foundation and appearing in the file LICENSE.GPL included in the
32
** packaging of this file. Please review the following information to
33
** ensure the GNU General Public License version 3.0 requirements will be
34
** met: http://www.gnu.org/copyleft/gpl.html.
36
** If you are unsure which license is appropriate for your use, please
37
** contact the sales department at http://www.qtsoftware.com/contact.
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.
28
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
38
38
** $QT_END_LICENSE$
40
40
****************************************************************************/
76
77
# include <private/qpixmap_x11_p.h>
80
#if defined(Q_OS_SYMBIAN)
81
# include <private/qt_s60_p.h>
79
84
#include "qpixmap_raster_p.h"
83
88
// ### Qt 5: remove
84
typedef void (*_qt_pixmap_cleanup_hook)(int);
85
Q_GUI_EXPORT _qt_pixmap_cleanup_hook qt_pixmap_cleanup_hook = 0;
88
typedef void (*_qt_pixmap_cleanup_hook_64)(qint64);
89
Q_GUI_EXPORT _qt_pixmap_cleanup_hook_64 qt_pixmap_cleanup_hook_64 = 0;
92
89
Q_GUI_EXPORT qint64 qt_pixmap_id(const QPixmap &pixmap)
94
91
return pixmap.cacheKey();
375
369
d = QGraphicsSystem::createDefaultPixmapData(data->pixelType());
371
d->copy(data.data(), r);
378
372
return QPixmap(d);
376
\fn QPixmap::scroll(int dx, int dy, int x, int y, int width, int height, QRegion *exposed)
379
This convenience function is equivalent to calling QPixmap::scroll(\a dx,
380
\a dy, QRect(\a x, \a y, \a width, \a height), \a exposed).
382
\sa QWidget::scroll(), QGraphicsItem::scroll()
388
Scrolls the area \a rect of this pixmap by (\a dx, \a dy). The exposed
389
region is left unchanged. You can optionally pass a pointer to an empty
390
QRegion to get the region that is \a exposed by the scroll operation.
392
\snippet doc/src/snippets/code/src_gui_image_qpixmap.cpp 2
394
You cannot scroll while there is an active painter on the pixmap.
396
\sa QWidget::scroll(), QGraphicsItem::scroll()
398
void QPixmap::scroll(int dx, int dy, const QRect &rect, QRegion *exposed)
400
if (isNull() || (dx == 0 && dy == 0))
402
QRect dest = rect & this->rect();
403
QRect src = dest.translated(-dx, -dy) & dest;
412
if (!data->scroll(dx, dy, src)) {
415
QPainter painter(&pix);
416
painter.setCompositionMode(QPainter::CompositionMode_Source);
417
painter.drawPixmap(src.translated(dx, dy), *this, src);
424
*exposed -= src.translated(dx, dy);
382
429
Assigns the given \a pixmap to this pixmap and returns a reference
600
645
QPixmap pm(QSize(w, h), data->type);
601
646
bool uninit = false;
602
647
#if defined(Q_WS_X11)
603
QX11PixmapData *x11Data = data->classId() == QPixmapData::X11Class ? static_cast<QX11PixmapData*>(data) : 0;
648
QX11PixmapData *x11Data = data->classId() == QPixmapData::X11Class ? static_cast<QX11PixmapData*>(data.data()) : 0;
605
650
pm.x11SetScreen(x11Data->xinfo.screen());
606
uninit = x11Data->uninit;
651
uninit = x11Data->flags & QX11PixmapData::Uninitialized;
608
653
#elif defined(Q_WS_MAC)
609
QMacPixmapData *macData = data->classId() == QPixmapData::MacClass ? static_cast<QMacPixmapData*>(data) : 0;
654
QMacPixmapData *macData = data->classId() == QPixmapData::MacClass ? static_cast<QMacPixmapData*>(data.data()) : 0;
611
656
uninit = macData->uninit;
618
663
p.drawPixmap(0, 0, *this, 0, 0, qMin(width(), w), qMin(height(), h));
621
#if defined(Q_WS_MAC)
622
#ifndef QT_MAC_NO_QUICKDRAW
623
if(macData && macData->qd_alpha)
624
macData->macQDUpdateAlpha();
626
#elif defined(Q_WS_X11)
666
#if defined(Q_WS_X11)
627
667
if (x11Data && x11Data->x11_mask) {
628
QX11PixmapData *pmData = static_cast<QX11PixmapData*>(pm.data);
668
QX11PixmapData *pmData = static_cast<QX11PixmapData*>(pm.data.data());
629
669
pmData->x11_mask = (Qt::HANDLE)XCreatePixmap(X11->display,
630
670
RootWindow(x11Data->xinfo.display(),
631
671
x11Data->xinfo.screen()),
794
834
if (QPixmapCache::find(key, *this))
797
QImage image = QImageReader(fileName, format).read();
801
if (data->pixelType() == QPixmapData::BitmapType)
802
pm = QBitmap::fromImage(image, flags);
804
pm = fromImage(image, flags);
837
if (data->fromFile(fileName, format, flags)) {
807
838
QPixmapCache::insert(key, *this);
832
864
bool QPixmap::loadFromData(const uchar *buf, uint len, const char *format, Qt::ImageConversionFlags flags)
834
QByteArray a = QByteArray::fromRawData(reinterpret_cast<const char *>(buf), len);
836
b.open(QIODevice::ReadOnly);
838
QImage image = QImageReader(&b, format).read();
840
if (data->pixelType() == QPixmapData::BitmapType)
841
pm = QBitmap::fromImage(image, flags);
843
pm = fromImage(image, flags);
866
return data->fromData(buf, len, format, flags);
1306
1324
return data->ref == 1;
1328
### Qt5 - remove me.
1309
1330
void QPixmap::deref()
1311
if (data && !data->ref.deref()) { // Destroy image if last ref
1312
#if !defined(QT_NO_DIRECT3D) && defined(Q_WS_WIN)
1313
if (data->classId() == QPixmapData::RasterClass) {
1314
QRasterPixmapData *rData = static_cast<QRasterPixmapData*>(data);
1316
rData->texture->Release();
1320
if (data->is_cached && qt_pixmap_cleanup_hook_64)
1321
qt_pixmap_cleanup_hook_64(cacheKey());
1332
Q_ASSERT_X(false, "QPixmap::deref()", "Do not call this function anymore!");
1530
1535
designed and optimized for showing images on screen. QBitmap is
1531
1536
only a convenience class that inherits QPixmap, ensuring a depth
1532
1537
of 1. The isQBitmap() function returns true if a QPixmap object is
1533
really a bitmap, otherwise returns false. Finally, the QPicture class is a
1534
paint device that records and replays QPainter commands.
1538
really a bitmap, otherwise returns false. Finally, the QPicture class
1539
is a paint device that records and replays QPainter commands.
1536
1541
A QPixmap can easily be displayed on the screen using QLabel or
1537
1542
one of QAbstractButton's subclasses (such as QPushButton and
1538
1543
QToolButton). QLabel has a pixmap property, whereas
1539
QAbstractButton has an icon property. And because QPixmap is a
1540
QPaintDevice subclass, QPainter can be used to draw directly onto
1544
QAbstractButton has an icon property.
1543
1546
In addition to the ordinary constructors, a QPixmap can be
1544
1547
constructed using the static grabWidget() and grabWindow()
1545
1548
functions which creates a QPixmap and paints the given widget, or
1548
Note that the pixel data in a pixmap is internal and is managed by
1549
the underlying window system. Pixels can only be accessed through
1550
QPainter functions or by converting the QPixmap to a QImage.
1551
QPixmap objects can be passed around by value since the QPixmap
1552
class uses implicit data sharing. For more information, see the \l
1553
{Implicit Data Sharing} documentation. QPixmap objects can also be
1551
1556
Depending on the system, QPixmap is stored using a RGB32 or a
1552
1557
premultiplied alpha format. If the image has an alpha channel, and
1553
1558
if the system allows, the preferred format is premultiplied alpha.
1554
1559
Note also that QPixmap, unlike QImage, may be hardware dependent.
1555
On X11 and Mac, a QPixmap is stored on the server side while a
1556
QImage is stored on the client side (on Windows, these two classes
1560
On X11, Mac and Symbian, a QPixmap is stored on the server side while
1561
a QImage is stored on the client side (on Windows, these two classes
1557
1562
have an equivalent internal representation, i.e. both QImage and
1558
1563
QPixmap are stored on the client side and don't use any GDI
1566
Note that the pixel data in a pixmap is internal and is managed by
1567
the underlying window system. Because QPixmap is a QPaintDevice
1568
subclass, QPainter can be used to draw directly onto pixmaps.
1569
Pixels can only be accessed through QPainter functions or by
1570
converting the QPixmap to a QImage. However, the fill() function
1571
is available for initializing the entire pixmap with a given color.
1561
1573
There are functions to convert between QImage and
1562
1574
QPixmap. Typically, the QImage class is used to load an image
1563
1575
file, optionally manipulating the image data, before the QImage
1565
1577
screen. Alternatively, if no manipulation is desired, the image
1566
1578
file can be loaded directly into a QPixmap. On Windows, the
1567
1579
QPixmap class also supports conversion between \c HBITMAP and
1580
QPixmap. On Symbian, the QPixmap class also supports conversion
1581
between CFbsBitmap and QPixmap.
1570
1583
QPixmap provides a collection of functions that can be used to
1571
1584
obtain a variety of information about the pixmap. In addition,
1572
1585
there are several functions that enables transformation of the
1575
QPixmap objects can be passed around by value since the QPixmap
1576
class uses implicit data sharing. For more information, see the \l
1577
{Implicit Data Sharing} documentation. QPixmap objects can also be
1580
1588
\tableofcontents
1582
1590
\section1 Reading and Writing Image Files
1633
1641
The hasAlphaChannel() returns true if the pixmap has a format that
1634
1642
respects the alpha channel, otherwise returns false, while the
1635
1643
hasAlpha() function returns true if the pixmap has an alpha
1636
channel \e or a mask (otherwise false).
1644
channel \e or a mask (otherwise false). The mask() function returns
1645
the mask as a QBitmap object, which can be set using setMask().
1638
The alphaChannel() function returns the alpha channel as a new
1639
QPixmap object, while the mask() function returns the mask as a
1640
QBitmap object. The alpha channel and mask can be set using the
1641
setAlphaChannel() and setMask() functions, respectively.
1647
The createHeuristicMask() function creates and returns a 1-bpp
1648
heuristic mask (i.e. a QBitmap) for this pixmap. It works by
1649
selecting a color from one of the corners and then chipping away
1650
pixels of that color, starting at all the edges. The
1651
createMaskFromColor() function creates and returns a mask (i.e. a
1652
QBitmap) for the pixmap based on a given color.
1644
1655
\o Low-level information
1667
1678
operation, you can use QBitmap::fromImage() instead.
1669
1680
In addition, on Windows, the QPixmap class supports conversion to
1670
and from HBitmap: the toWinHBITMAP() function creates a HBITMAP
1681
and from HBITMAP: the toWinHBITMAP() function creates a HBITMAP
1671
1682
equivalent to the QPixmap, based on the given HBitmapFormat, and
1672
1683
returns the HBITMAP handle. The fromWinHBITMAP() function returns
1673
1684
a QPixmap that is equivalent to the given bitmap which has the
1685
specified format. The QPixmap class also supports conversion to
1686
and from HICON: the toWinHICON() function creates a HICON equivalent
1687
to the QPixmap, and returns the HICON handle. The fromWinHICON()
1688
function returns a QPixmap that is equivalent to the given icon.
1690
In addition, on Symbian, the QPixmap class supports conversion to
1691
and from CFbsBitmap: the toSymbianCFbsBitmap() function creates
1692
CFbsBitmap equivalent to the QPixmap, based on given mode and returns
1693
a CFbsBitmap object. The fromSymbianCFbsBitmap() function returns a
1694
QPixmap that is equivalent to the given bitmap and given mode.
1676
1696
\section1 Pixmap Transformations
1678
1698
QPixmap supports a number of functions for creating a new pixmap
1679
that is a transformed version of the original: The
1680
createHeuristicMask() function creates and returns a 1-bpp
1681
heuristic mask (i.e. a QBitmap) for this pixmap. It works by
1682
selecting a color from one of the corners and then chipping away
1683
pixels of that color, starting at all the edges. The
1684
createMaskFromColor() function creates and returns a mask (i.e. a
1685
QBitmap) for the pixmap based on a given color.
1699
that is a transformed version of the original:
1688
1701
The scaled(), scaledToWidth() and scaledToHeight() functions
1689
1702
return scaled copies of the pixmap, while the copy() function
1698
1711
function returns the actual matrix used for transforming the
1701
There are also functions for changing attributes of a pixmap.
1702
in-place: The fill() function fills the entire image with the
1703
given color, the setMask() function sets a mask bitmap, and the
1704
setAlphaChannel() function sets the pixmap's alpha channel.
1706
1714
\sa QBitmap, QImage, QImageReader, QImageWriter
1853
1866
return QScreen::instance()->depth();
1854
1867
#elif defined(Q_WS_X11)
1855
1868
return QX11Info::appDepth();
1856
#elif defined(Q_OS_WINCE)
1869
#elif defined(Q_WS_WINCE)
1857
1870
return QColormap::instance().depth();
1858
1871
#elif defined(Q_WS_WIN)
1859
1872
return 32; // XXX
1860
1873
#elif defined(Q_WS_MAC)
1875
#elif defined(Q_OS_SYMBIAN)
1876
return S60->screenDepth;
1865
typedef void (*_qt_pixmap_cleanup_hook_64)(qint64);
1866
extern _qt_pixmap_cleanup_hook_64 qt_pixmap_cleanup_hook_64;
1869
1881
Detaches the pixmap from shared pixmap data.
1887
1899
QPixmapData::ClassId id = data->classId();
1888
1900
if (id == QPixmapData::RasterClass) {
1889
QRasterPixmapData *rasterData = static_cast<QRasterPixmapData*>(data);
1901
QRasterPixmapData *rasterData = static_cast<QRasterPixmapData*>(data.data());
1890
1902
rasterData->image.detach();
1891
#if defined(Q_WS_WIN) && !defined(QT_NO_DIRECT3D)
1892
if (rasterData->texture) {
1893
rasterData->texture->Release();
1894
rasterData->texture = 0;
1899
if (data->is_cached && qt_pixmap_cleanup_hook_64 && data->ref == 1)
1900
qt_pixmap_cleanup_hook_64(cacheKey());
1905
if (data->is_cached && data->ref == 1)
1906
QImagePixmapCleanupHooks::executePixmapHooks(this);
1902
1908
#if defined(Q_WS_MAC)
1903
QMacPixmapData *macData = id == QPixmapData::MacClass ? static_cast<QMacPixmapData*>(data) : 0;
1909
QMacPixmapData *macData = id == QPixmapData::MacClass ? static_cast<QMacPixmapData*>(data.data()) : 0;
1905
1911
if (macData->cg_mask) {
1906
1912
CGImageRelease(macData->cg_mask);
1958
1959
if (image.isNull())
1959
1960
return QPixmap();
1962
1962
QGraphicsSystem* gs = QApplicationPrivate::graphicsSystem();
1964
data = gs->createPixmapData(QPixmapData::PixmapType);
1966
data = QGraphicsSystem::createDefaultPixmapData(QPixmapData::PixmapType);
1963
QScopedPointer<QPixmapData> data(gs ? gs->createPixmapData(QPixmapData::PixmapType)
1964
: QGraphicsSystem::createDefaultPixmapData(QPixmapData::PixmapType));
1968
1965
data->fromImage(image, flags);
1969
return QPixmap(data);
1966
return QPixmap(data.take());
2060
/*! \fn HICON QPixmap::toWinHICON() const
2063
\bold{Win32 only:} Creates a \c HICON equivalent to the QPixmap.
2064
Returns the \c HICON handle.
2066
It is the caller's responsibility to free the \c HICON data after use.
2068
\warning This function is only available on Windows.
2070
\sa fromWinHICON(), {QPixmap#Pixmap Conversion}{Pixmap Conversion}
2073
/*! \fn QPixmap QPixmap::fromWinHICON(HICON icon)
2076
\bold{Win32 only:} Returns a QPixmap that is equivalent to the given
2079
\warning This function is only available on Windows.
2081
\sa toWinHICON(), {QPixmap#Pixmap Conversion}{Pixmap Conversion}
2063
2085
/*! \fn const QX11Info &QPixmap::x11Info() const
2064
2086
\bold{X11 only:} Returns information about the configuration of
2065
2087
the X display used to display the widget.