source: trunk/src/gui/image/qbitmap.cpp@ 461

Last change on this file since 461 was 2, checked in by Dmitry A. Kuminov, 17 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 11.9 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
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.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
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
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qbitmap.h"
43#include "qpixmapdata_p.h"
44#include "qimage.h"
45#include "qvariant.h"
46#include <qpainter.h>
47#include <private/qgraphicssystem_p.h>
48#include <private/qapplication_p.h>
49
50QT_BEGIN_NAMESPACE
51
52/*!
53 \class QBitmap
54 \brief The QBitmap class provides monochrome (1-bit depth) pixmaps.
55
56 \ingroup multimedia
57 \ingroup shared
58
59 The QBitmap class is a monochrome off-screen paint device used
60 mainly for creating custom QCursor and QBrush objects,
61 constructing QRegion objects, and for setting masks for pixmaps
62 and widgets.
63
64 QBitmap is a QPixmap subclass ensuring a depth of 1, except for
65 null objects which have a depth of 0. If a pixmap with a depth
66 greater than 1 is assigned to a bitmap, the bitmap will be
67 dithered automatically.
68
69 Use the QColor objects Qt::color0 and Qt::color1 when drawing on a
70 QBitmap object (or a QPixmap object with depth 1).
71
72 Painting with Qt::color0 sets the bitmap bits to 0, and painting
73 with Qt::color1 sets the bits to 1. For a bitmap, 0-bits indicate
74 background (or transparent pixels) and 1-bits indicate foreground
75 (or opaque pixels). Use the clear() function to set all the bits
76 to Qt::color0. Note that using the Qt::black and Qt::white colors
77 make no sense because the QColor::pixel() value is not necessarily
78 0 for black and 1 for white.
79
80 The QBitmap class provides the transformed() function returning a
81 transformed copy of the bitmap; use the QMatrix argument to
82 translate, scale, shear, and rotate the bitmap. In addition,
83 QBitmap provides the static fromData() function which returns a
84 bitmap constructed from the given \c uchar data, and the static
85 fromImage() function returning a converted copy of a QImage
86 object.
87
88 Just like the QPixmap class, QBitmap is optimized by the use of
89 implicit data sharing. For more information, see the {Implicit
90 Data Sharing} documentation.
91
92 \sa QPixmap, QImage, QImageReader, QImageWriter
93*/
94
95
96/*!
97 Constructs a null bitmap.
98
99 \sa QPixmap::isNull()
100*/
101
102QBitmap::QBitmap()
103 : QPixmap(QSize(0, 0), QPixmapData::BitmapType)
104{
105}
106
107/*!
108 \fn QBitmap::QBitmap(int width, int height)
109
110 Constructs a bitmap with the given \a width and \a height. The pixels
111 inside are uninitialized.
112
113 \sa clear()
114*/
115
116QBitmap::QBitmap(int w, int h)
117 : QPixmap(QSize(w, h), QPixmapData::BitmapType)
118{
119}
120
121/*!
122 Constructs a bitmap with the given \a size. The pixels in the
123 bitmap are uninitialized.
124
125 \sa clear()
126*/
127
128QBitmap::QBitmap(const QSize &size)
129 : QPixmap(size, QPixmapData::BitmapType)
130{
131}
132
133/*!
134 \fn QBitmap::clear()
135
136 Clears the bitmap, setting all its bits to Qt::color0.
137*/
138
139/*!
140 Constructs a bitmap that is a copy of the given \a pixmap.
141
142 If the pixmap has a depth greater than 1, the resulting bitmap
143 will be dithered automatically.
144
145 \sa QPixmap::depth(), fromImage(), fromData()
146*/
147
148QBitmap::QBitmap(const QPixmap &pixmap)
149{
150 QBitmap::operator=(pixmap);
151}
152
153/*!
154 \fn QBitmap::QBitmap(const QImage &image)
155
156 Constructs a bitmap that is a copy of the given \a image.
157
158 Use the static fromImage() function instead.
159*/
160
161/*!
162 Constructs a bitmap from the file specified by the given \a
163 fileName. If the file does not exist, or has an unknown format,
164 the bitmap becomes a null bitmap.
165
166 The \a fileName and \a format parameters are passed on to the
167 QPixmap::load() function. If the file format uses more than 1 bit
168 per pixel, the resulting bitmap will be dithered automatically.
169
170 \sa QPixmap::isNull(), QImageReader::imageFormat()
171*/
172
173QBitmap::QBitmap(const QString& fileName, const char *format)
174 : QPixmap(QSize(0, 0), QPixmapData::BitmapType)
175{
176 load(fileName, format, Qt::MonoOnly);
177}
178
179/*!
180 \overload
181
182 Assigns the given \a pixmap to this bitmap and returns a reference
183 to this bitmap.
184
185 If the pixmap has a depth greater than 1, the resulting bitmap
186 will be dithered automatically.
187
188 \sa QPixmap::depth()
189 */
190
191QBitmap &QBitmap::operator=(const QPixmap &pixmap)
192{
193 if (pixmap.isNull()) { // a null pixmap
194 QBitmap bm(0, 0);
195 QBitmap::operator=(bm);
196 } else if (pixmap.depth() == 1) { // 1-bit pixmap
197 QPixmap::operator=(pixmap); // shallow assignment
198 } else { // n-bit depth pixmap
199 QImage image;
200 image = pixmap.toImage(); // convert pixmap to image
201 *this = fromImage(image); // will dither image
202 }
203 return *this;
204}
205
206
207#ifdef QT3_SUPPORT
208QBitmap::QBitmap(int w, int h, const uchar *bits, bool isXbitmap)
209{
210 *this = fromData(QSize(w, h), bits, isXbitmap ? QImage::Format_MonoLSB : QImage::Format_Mono);
211}
212
213
214QBitmap::QBitmap(const QSize &size, const uchar *bits, bool isXbitmap)
215{
216 *this = fromData(size, bits, isXbitmap ? QImage::Format_MonoLSB : QImage::Format_Mono);
217}
218#endif
219
220/*!
221 Destroys the bitmap.
222*/
223QBitmap::~QBitmap()
224{
225}
226
227/*!
228 Returns the bitmap as a QVariant.
229*/
230QBitmap::operator QVariant() const
231{
232 return QVariant(QVariant::Bitmap, this);
233}
234
235/*!
236 \fn QBitmap &QBitmap::operator=(const QImage &image)
237 \overload
238
239 Converts the given \a image to a bitmap, and assigns the result to
240 this bitmap. Returns a reference to the bitmap.
241
242 Use the static fromImage() function instead.
243*/
244
245/*!
246 Returns a copy of the given \a image converted to a bitmap using
247 the specified image conversion \a flags.
248
249 \sa fromData()
250*/
251QBitmap QBitmap::fromImage(const QImage &image, Qt::ImageConversionFlags flags)
252{
253 if (image.isNull())
254 return QBitmap();
255
256 QImage img = image.convertToFormat(QImage::Format_MonoLSB, flags);
257
258 // make sure image.color(0) == Qt::color0 (white)
259 // and image.color(1) == Qt::color1 (black)
260 const QRgb c0 = QColor(Qt::black).rgb();
261 const QRgb c1 = QColor(Qt::white).rgb();
262 if (img.color(0) == c0 && img.color(1) == c1) {
263 img.invertPixels();
264 img.setColor(0, c1);
265 img.setColor(1, c0);