1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the painting module of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
30
#include "qpixmap_p.h"
35
#include <private/qt_x11_p.h>
39
\class QBitmap qbitmap.h
40
\brief The QBitmap class provides monochrome (1-bit depth) pixmaps.
44
The QBitmap class is a monochrome off-screen paint device used mainly for
45
creating custom QCursor and QBrush objects, settings masks for \link
46
QPixmap::setMask() pixmaps \endlink and \link QWidget::setMask() widgets
47
\endlink, and for QRegion.
49
A QBitmap is a QPixmap with a \link QPixmap::depth() depth\endlink
50
of 1. If a pixmap with a depth greater than 1 is assigned to a
51
bitmap, the bitmap will be dithered automatically. A QBitmap is
52
guaranteed to always have the depth 1, unless it is
53
\link QPixmap::isNull() null \endlink since null pixmaps have depth 0.
55
When drawing in a QBitmap (or QPixmap with depth 1), we recommend
56
using the QColor objects Qt::color0 and Qt::color1.
57
Painting with Qt::color0 sets the bitmap bits to 0, and painting
58
with Qt::color1 sets the bits to 1. For a bitmap, 0-bits indicate
59
background (or transparent pixels) and 1-bits indicate foreground (or
60
opaque pixels). Using the Qt::black and Qt::white colors make no
61
sense because the QColor::pixel() value is not necessarily 0 for
62
black and 1 for white.
64
The QBitmap can be transformed (translated, scaled, sheared, and
65
rotated) using transform().
67
Just like the QPixmap class, QBitmap is optimized by the use of
68
\link shclass.html implicit sharing\endlink, so it is very
69
efficient to pass QBitmap objects as arguments.
71
\sa QPixmap, QPainter::drawPixmap(), bitBlt(), \link shclass.html Shared Classes\endlink
76
Constructs a null bitmap.
82
: QPixmap(QSize(0, 0), BitmapType)
87
\fn QBitmap::QBitmap(int width, int height)
89
Constructs a bitmap with the given \a width and \a height. The pixels
90
inside are uninitialized.
94
QBitmap::QBitmap(int w, int h)
95
: QPixmap(QSize(w, h), BitmapType)
102
Clear the bitmap to Qt::color0.
108
Constructs a bitmap with the given \a size.
110
The pixels in the bitmap are uninitialized.
113
QBitmap::QBitmap(const QSize &size)
114
: QPixmap(size, BitmapType)
119
Constructs a bitmap that is a copy of the \a pixmap given.
121
Dithering will be performed if the pixmap has a QPixmap::depth()
125
QBitmap::QBitmap(const QPixmap &pixmap)
127
QBitmap::operator=(pixmap);
131
\fn QBitmap::QBitmap(const QImage &image)
133
Constructs a bitmap that is a copy of the \a image given.
135
Dithering will be performed if the image has a QImage::depth()
139
#ifndef QT_NO_IMAGEIO
141
Constructs a bitmap from the file referred to by \a fileName. If the
142
file does not exist, or is of an unknown format, the bitmap becomes a
145
The parameters \a fileName and \a format are passed on to
146
QPixmap::load(). Dithering will be performed if the file format
147
uses more than 1 bit per pixel.
149
\sa QPixmap::isNull(), QPixmap::load(), QPixmap::loadFromData(),
150
QPixmap::save(), QImageReader::imageFormat()
153
QBitmap::QBitmap(const QString& fileName, const char *format)
154
: QPixmap(QSize(0, 0), BitmapType)
156
load(fileName, format, Qt::MonoOnly);
163
Assigns the \a pixmap to this bitmap and returns a
166
Dithering will be performed if the pixmap has a QPixmap::depth()
170
QBitmap &QBitmap::operator=(const QPixmap &pixmap)
172
if (pixmap.isNull()) { // a null pixmap
174
QBitmap::operator=(bm);
175
} else if (pixmap.depth() == 1) { // 1-bit pixmap
176
QPixmap::operator=(pixmap); // shallow assignment
177
} else { // n-bit depth pixmap
179
image = pixmap.toImage(); // convert pixmap to image
180
*this = fromImage(image); // will dither image
187
QBitmap::QBitmap(int w, int h, const uchar *bits, bool isXbitmap)
189
*this = fromData(QSize(w, h), bits, isXbitmap ? QImage::Format_MonoLSB : QImage::Format_Mono);
193
QBitmap::QBitmap(const QSize &size, const uchar *bits, bool isXbitmap)
195
*this = fromData(size, bits, isXbitmap ? QImage::Format_MonoLSB : QImage::Format_Mono);
207
Returns the bitmap as a QVariant.
209
QBitmap::operator QVariant() const
211
return QVariant(QVariant::Bitmap, this);
215
\fn QBitmap &QBitmap::operator=(const QImage &image)
219
Converts the image \a image to a bitmap, and assigns the result to
220
this bitmap. Returns a reference to the bitmap.
222
Dithering will be performed if the image has a QImage::depth()
227
Returns a copy of the given \a image converted to a bitmap using the
228
image conversion flags specified by \a flags.
230
QBitmap QBitmap::fromImage(const QImage &image, Qt::ImageConversionFlags flags)
234
QImage img = image.convertToFormat(QImage::Format_MonoLSB, flags);
235
#if defined (Q_WS_WIN) || defined (Q_WS_QWS)
237
bm.data->image = img;
239
// Swap colors to match so that default config draws more correctly.
240
// black bits -> black pen in QPainter
241
if (image.numColors() == 2 && qGray(image.color(0)) < qGray(image.color(1))) {
242
QRgb color0 = image.color(0);
243
QRgb color1 = image.color(1);
244
bm.data->image.setColor(0, color1);
245
bm.data->image.setColor(1, color0);
246
bm.data->image.invertPixels();
249
#elif defined(Q_WS_X11)
251
// make sure image.color(0) == Qt::color0 (white) and image.color(1) == Qt::color1 (black)
252
const QRgb c0 = QColor(Qt::black).rgb();
253
const QRgb c1 = QColor(Qt::white).rgb();
254
if (img.color(0) == c0 && img.color(1) == c1) {
263
int h = img.height();
265
int ibpl = img.bytesPerLine();
267
tmp_bits = new uchar[bpl*h];
268
bits = (char *)tmp_bits;
273
for (y = 0; y < h; y++) {
279
bits = (char *)img.bits();
282
bm.data->hd = (Qt::HANDLE)XCreateBitmapFromData(bm.data->xinfo.display(),
283
RootWindow(bm.data->xinfo.display(), bm.data->xinfo.screen()),
286
#ifndef QT_NO_XRENDER
287
if (X11->use_xrender)
288
bm.data->picture = XRenderCreatePicture(X11->display, bm.data->hd,
289
XRenderFindStandardFormat(X11->display, PictStandardA1), 0, 0);
290
#endif // QT_NO_XRENDER
292
if (tmp_bits) // Avoid purify complaint
294
bm.data->w = w; bm.data->h = h; bm.data->d = 1;
298
return QBitmap(QPixmap::fromImage(img, flags|Qt::MonoOnly));
303
Constructs a bitmap with the given \a size, and sets the contents to
304
the \a bits supplied.
306
The bitmap data has to be byte aligned and provided in in the bit
307
order specified by \a monoFormat. The mono format must be either
308
QImage::Mono or QImage::MonoLSB.
310
Use QImage::Mono to specify data on the XBM format.
313
QBitmap QBitmap::fromData(const QSize &size, const uchar *bits, QImage::Format monoFormat)
315
Q_ASSERT(monoFormat == QImage::Format_Mono || monoFormat == QImage::Format_MonoLSB);
317
QImage image(size, monoFormat);
318
image.setColor(0, Qt::color0);
319
image.setColor(1, Qt::color1);
321
// Need to memcpy each line separatly since QImage is 32bit aligned and
322
// this data is only byte aligned...
323
int bytesPerLine = (size.width() + 7) / 8;
324
for (int y = 0; y < size.height(); ++y)
325
memcpy(image.scanLine(y), bits + bytesPerLine * y, bytesPerLine);
326
return QBitmap::fromImage(image);
330
#ifndef QT_NO_PIXMAP_TRANSFORMATION
332
Returns a transformed copy of this bitmap using the \a matrix given.
334
This function does exactly the same as QPixmap::transform(), except
335
that it returns a QBitmap instead of a QPixmap.
337
\sa QPixmap::transformed()
340
QBitmap QBitmap::transformed(const QMatrix &matrix) const
342
QBitmap bm = QPixmap::transformed(matrix);
345
#endif // QT_NO_TRANSFORMATIONS
349
\fn QBitmap QBitmap::xForm(const QMatrix &matrix) const
351
Use transform(\a matrix) instead.
355
\fn QBitmap::QBitmap(const QSize &size, bool clear)
357
Use the QBitmap(\a size) constructor. If \a clear is true, call
364
\fn QBitmap::QBitmap(int w, int h, bool clear)
366
Use the QBitmap(\a w, \a h) constructor. If \a clear is true, call
373
\fn QBitmap::QBitmap(int width, int height, const uchar *bits, bool isXbitmap)
375
Constructs a bitmap with the given \a width and \a height,
376
and sets the contents to the \a bits supplied.
378
The \a isXbitmap flag should be true if \a bits was generated by
379
the X11 bitmap program. The X bitmap bit order is little endian.
380
The QImage documentation discusses bit order of monochrome
381
images. Opposed to QImage, the data has to be byte aligned.
383
Example (creates an arrow bitmap):
385
uchar arrow_bits[] = { 0x3f, 0x1f, 0x0f, 0x1f, 0x3b, 0x71, 0xe0, 0xc0 };
386
QBitmap bm(8, 8, arrow_bits, true);
392
\fn QBitmap::QBitmap(const QSize &size, const uchar *bits, bool isXbitmap)
396
Constructs a bitmap with the given \a size, and sets the contents to
397
the \a bits supplied.
399
The \a isXbitmap flag should be true if \a bits was generated by
400
the X11 bitmap program. The X bitmap bit order is little endian.
401
The QImage documentation discusses bit order of monochrome images.