QBitmap Class Reference

#include <qbitmap.h>

Inheritance diagram for QBitmap:

Inheritance graph
[legend]
Collaboration diagram for QBitmap:

Collaboration graph
[legend]
List of all members.

Detailed Description

The QBitmap class provides monochrome (1-bit depth) pixmaps.

The QBitmap class is a monochrome off-screen paint device used mainly for creating custom QCursor and QBrush objects, constructing QRegion objects, and for setting masks for pixmaps and widgets.

QBitmap is a QPixmap subclass ensuring a depth of 1, except for null objects which have a depth of 0. If a pixmap with a depth greater than 1 is assigned to a bitmap, the bitmap will be dithered automatically.

Use the QColor objects Qt::color0 and Qt::color1 when drawing on a QBitmap object (or a QPixmap object with depth 1).

Painting with Qt::color0 sets the bitmap bits to 0, and painting with Qt::color1 sets the bits to 1. For a bitmap, 0-bits indicate background (or transparent pixels) and 1-bits indicate foreground (or opaque pixels). Use the clear() function to set all the bits to Qt::color0. Note that using the Qt::black and Qt::white colors make no sense because the QColor::pixel() value is not necessarily 0 for black and 1 for white.

The QBitmap class provides the transformed() function returning a transformed copy of the bitmap; use the QMatrix argument to translate, scale, shear, and rotate the bitmap. In addition, QBitmap provides the static fromData() function which returns a bitmap constructed from the given uchar data, and the static fromImage() function returning a converted copy of a QImage object.

Just like the QPixmap class, QBitmap is optimized by the use of implicit data sharing. For more information, see the {Implicit Data Sharing} documentation.

See also:
QPixmap, QImage, QImageReader, QImageWriter

Definition at line 35 of file qbitmap.h.

Public Member Functions

 QBitmap ()
 QBitmap (const QPixmap &)
 QBitmap (int w, int h)
 QBitmap (const QSize &)
 QBitmap (const QString &fileName, const char *format=0)
 ~QBitmap ()
QBitmapoperator= (const QPixmap &)
 operator QVariant () const
void clear ()
QBitmap transformed (const QMatrix &) const

Static Public Member Functions

static QBitmap fromImage (const QImage &image, Qt::ImageConversionFlags flags=Qt::AutoColor)
static QBitmap fromData (const QSize &size, const uchar *bits, QImage::Format monoFormat=QImage::Format_MonoLSB)


Constructor & Destructor Documentation

QBitmap::QBitmap (  ) 

Constructs a null bitmap.

See also:
QPixmap::isNull()

Definition at line 83 of file qbitmap.cpp.

Referenced by fromImage().

00084     : QPixmap(QSize(0, 0), BitmapType)
00085 {
00086 }

QBitmap::QBitmap ( const QPixmap pixmap  ) 

Constructs a bitmap that is a copy of the given pixmap.

If the pixmap has a depth greater than 1, the resulting bitmap will be dithered automatically.

See also:
QPixmap::depth(), fromImage(), fromData()

Definition at line 129 of file qbitmap.cpp.

References operator=().

00130 {
00131     QBitmap::operator=(pixmap);
00132 }

Here is the call graph for this function:

QBitmap::QBitmap ( int  width,
int  height 
)

Constructs a bitmap with the given width and height. The pixels inside are uninitialized.

See also:
clear()

Definition at line 97 of file qbitmap.cpp.

00098     : QPixmap(QSize(w, h), BitmapType)
00099 {
00100 }

QBitmap::QBitmap ( const QSize size  )  [explicit]

Constructs a bitmap with the given size. The pixels in the bitmap are uninitialized.

See also:
clear()

Definition at line 109 of file qbitmap.cpp.

00110     : QPixmap(size, BitmapType)
00111 {
00112 }

QBitmap::QBitmap ( const QString fileName,
const char *  format = 0 
) [explicit]

Constructs a bitmap from the file specified by the given fileName. If the file does not exist, or has an unknown format, the bitmap becomes a null bitmap.

The fileName and format parameters are passed on to the QPixmap::load() function. If the file format uses more than 1 bit per pixel, the resulting bitmap will be dithered automatically.

See also:
QPixmap::isNull(), QImageReader::imageFormat()

Definition at line 154 of file qbitmap.cpp.

References QPixmap::load(), and Qt::MonoOnly.

00155     : QPixmap(QSize(0, 0), BitmapType)
00156 {
00157     load(fileName, format, Qt::MonoOnly);
00158 }

Here is the call graph for this function:

QBitmap::~QBitmap (  ) 

Destroys the bitmap.

Definition at line 204 of file qbitmap.cpp.

00205 {
00206 }


Member Function Documentation

QBitmap & QBitmap::operator= ( const QPixmap pixmap  ) 

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Assigns the given pixmap to this bitmap and returns a reference to this bitmap.

If the pixmap has a depth greater than 1, the resulting bitmap will be dithered automatically.

See also:
QPixmap::depth()

Reimplemented from QPixmap.

Definition at line 172 of file qbitmap.cpp.

References QPixmap::depth(), fromImage(), image, QPixmap::isNull(), and QPixmap::toImage().

Referenced by QBitmap().

00173 {
00174     if (pixmap.isNull()) {                        // a null pixmap
00175         QBitmap bm(0, 0);
00176         QBitmap::operator=(bm);
00177     } else if (pixmap.depth() == 1) {                // 1-bit pixmap
00178         QPixmap::operator=(pixmap);                // shallow assignment
00179     } else {                                        // n-bit depth pixmap
00180         QImage image;
00181         image = pixmap.toImage();                                // convert pixmap to image
00182         *this = fromImage(image);                                // will dither image
00183     }
00184     return *this;
00185 }

Here is the call graph for this function:

QBitmap::operator QVariant (  )  const

Returns the bitmap as a QVariant.

Reimplemented from QPixmap.

Definition at line 211 of file qbitmap.cpp.

References QVariant::Bitmap.

00212 {
00213     return QVariant(QVariant::Bitmap, this);
00214 }

QBitmap::clear (  )  [inline]

Clears the bitmap, setting all its bits to Qt::color0.

Definition at line 48 of file qbitmap.h.

References Qt::color0.

Referenced by qdesigner_internal::CursorProperty::cursorPixmap(), and QWindowsStyle::drawComplexControl().

00048 { fill(Qt::color0); }

QBitmap QBitmap::fromImage ( const QImage image,
Qt::ImageConversionFlags  flags = Qt::AutoColor 
) [static]

Returns a copy of the given image converted to a bitmap using the specified image conversion flags.

See also:
fromData()

Reimplemented from QPixmap.

Definition at line 232 of file qbitmap.cpp.

References b, QImage::bits(), Qt::black, QImage::bytesPerLine(), QImage::color(), Qt::color0, Qt::color1, QImage::Format_MonoLSB, QPixmap::fromImage(), QImage::height(), image, QImage::invertPixels(), Qt::MonoOnly, p, QBitmap(), qGray(), QRgb, QImage::scanLine(), QImage::setColor(), Qt::white, QImage::width(), X11, and y.

Referenced by convert(), QPixmap::copy(), QPixmap::createHeuristicMask(), QPixmap::createMaskFromColor(), fromData(), QPixmap::load(), QPixmap::loadFromData(), QPixmap::mask(), operator=(), operator>>(), QCursor::QCursor(), QPixmap::QPixmap(), and qt_patternForAlpha().

00233 {
00234     if (image.isNull())
00235         return QBitmap();
00236     QImage img = image.convertToFormat(QImage::Format_MonoLSB, flags);
00237 #if defined (Q_WS_WIN) || defined (Q_WS_QWS)
00238     QBitmap bm;
00239     bm.data->image = img;
00240 
00241     // Swap colors to match so that default config draws more correctly.
00242     // black bits -> black pen in QPainter
00243     if (image.numColors() == 2 && qGray(image.color(0)) < qGray(image.color(1))) {
00244         QRgb color0 = image.color(0);
00245         QRgb color1 = image.color(1);
00246         bm.data->image.setColor(0, color1);
00247         bm.data->image.setColor(1, color0);
00248         bm.data->image.invertPixels();
00249     }
00250     return bm;
00251 #elif defined(Q_WS_X11)
00252     QBitmap bm;
00253     // make sure image.color(0) == Qt::color0 (white) and image.color(1) == Qt::color1 (black)
00254     const QRgb c0 = QColor(Qt::black).rgb();
00255     const QRgb c1 = QColor(Qt::white).rgb();
00256     if (img.color(0) == c0 && img.color(1) == c1) {
00257         img.invertPixels();
00258         img.setColor(0, c1);
00259         img.setColor(1, c0);
00260     }
00261 
00262     char  *bits;
00263     uchar *tmp_bits;
00264     int w = img.width();
00265     int h = img.height();
00266     int bpl = (w+7)/8;
00267     int ibpl = img.bytesPerLine();
00268     if (bpl != ibpl) {
00269         tmp_bits = new uchar[bpl*h];
00270         bits = (char *)tmp_bits;
00271         uchar *p, *b;
00272         int y;
00273         b = tmp_bits;
00274         p = img.scanLine(0);
00275         for (y = 0; y < h; y++) {
00276             memcpy(b, p, bpl);
00277             b += bpl;
00278             p += ibpl;
00279         }
00280     } else {
00281         bits = (char *)img.bits();
00282         tmp_bits = 0;
00283     }
00284     bm.data->hd = (Qt::HANDLE)XCreateBitmapFromData(bm.data->xinfo.display(),
00285                                                     RootWindow(bm.data->xinfo.display(), bm.data->xinfo.screen()),
00286                                                     bits, w, h);
00287 
00288 #ifndef QT_NO_XRENDER
00289     if (X11->use_xrender)
00290         bm.data->picture = XRenderCreatePicture(X11->display, bm.data->hd,
00291                                                 XRenderFindStandardFormat(X11->display, PictStandardA1), 0, 0);
00292 #endif // QT_NO_XRENDER
00293 
00294     if (tmp_bits)                                // Avoid purify complaint
00295         delete [] tmp_bits;
00296     bm.data->w = w;  bm.data->h = h;  bm.data->d = 1;
00297 
00298     return bm;
00299 #else
00300     const QRgb c0 = QColor(Qt::black).rgb();
00301     const QRgb c1 = QColor(Qt::white).rgb();
00302     if (img.color(0) == c0 && img.color(1) == c1) {
00303         img.invertPixels();
00304         img.setColor(0, c1);
00305         img.setColor(1, c0);
00306     }
00307     return QBitmap(QPixmap::fromImage(img, flags|Qt::MonoOnly));
00308 #endif
00309 }

Here is the call graph for this function:

QBitmap QBitmap::fromData ( const QSize size,
const uchar *  bits,
QImage::Format  monoFormat = QImage::Format_MonoLSB 
) [static]

Constructs a bitmap with the given size, and sets the contents to the bits supplied.

The bitmap data has to be byte aligned and provided in in the bit order specified by monoFormat. The mono format must be either QImage::Format_Mono or QImage::Format_MonoLSB. Use QImage::Format_Mono to specify data on the XBM format.

See also:
fromImage()

Definition at line 323 of file qbitmap.cpp.

References Qt::color0, Qt::color1, QImage::Format_Mono, QImage::Format_MonoLSB, fromImage(), QSize::height(), image, QPixmap::size(), QSize::width(), and y.

Referenced by TrWindow::pageCurl(), and qt_pixmapForBrush().

00324 {
00325     Q_ASSERT(monoFormat == QImage::Format_Mono || monoFormat == QImage::Format_MonoLSB);
00326 
00327     QImage image(size, monoFormat);
00328     image.setColor(0, Qt::color0);
00329     image.setColor(1, Qt::color1);
00330 
00331     // Need to memcpy each line separatly since QImage is 32bit aligned and
00332     // this data is only byte aligned...
00333     int bytesPerLine = (size.width() + 7) / 8;
00334     for (int y = 0; y < size.height(); ++y)
00335         memcpy(image.scanLine(y), bits + bytesPerLine * y, bytesPerLine);
00336     return QBitmap::fromImage(image);
00337 }

Here is the call graph for this function:

QBitmap QBitmap::transformed ( const QMatrix matrix  )  const

Returns a copy of this bitmap, transformed according to the given matrix.

See also:
QPixmap::transformed()

Definition at line 347 of file qbitmap.cpp.

References QPixmap::transformed().

00348 {
00349     QBitmap bm = QPixmap::transformed(matrix);
00350     return bm;
00351 }

Here is the call graph for this function:


The documentation for this class was generated from the following files:
Generated on Thu Mar 15 16:52:33 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1