| 1 | /****************************************************************************
|
|---|
| 2 | ** $Id: qbitmap.cpp 2 2005-11-16 15:49:26Z dmik $
|
|---|
| 3 | **
|
|---|
| 4 | ** Implementation of QBitmap class
|
|---|
| 5 | **
|
|---|
| 6 | ** Created : 941020
|
|---|
| 7 | **
|
|---|
| 8 | ** Copyright (C) 1992-2002 Trolltech AS. All rights reserved.
|
|---|
| 9 | **
|
|---|
| 10 | ** This file is part of the kernel module of the Qt GUI Toolkit.
|
|---|
| 11 | **
|
|---|
| 12 | ** This file may be distributed under the terms of the Q Public License
|
|---|
| 13 | ** as defined by Trolltech AS of Norway and appearing in the file
|
|---|
| 14 | ** LICENSE.QPL included in the packaging of this file.
|
|---|
| 15 | **
|
|---|
| 16 | ** This file may be distributed and/or modified under the terms of the
|
|---|
| 17 | ** GNU General Public License version 2 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 19 | ** packaging of this file.
|
|---|
| 20 | **
|
|---|
| 21 | ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
|
|---|
| 22 | ** licenses may use this file in accordance with the Qt Commercial License
|
|---|
| 23 | ** Agreement provided with the Software.
|
|---|
| 24 | **
|
|---|
| 25 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
|---|
| 26 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
|---|
| 27 | **
|
|---|
| 28 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
|
|---|
| 29 | ** information about Qt Commercial License Agreements.
|
|---|
| 30 | ** See http://www.trolltech.com/qpl/ for QPL licensing information.
|
|---|
| 31 | ** See http://www.trolltech.com/gpl/ for GPL licensing information.
|
|---|
| 32 | **
|
|---|
| 33 | ** Contact info@trolltech.com if any conditions of this licensing are
|
|---|
| 34 | ** not clear to you.
|
|---|
| 35 | **
|
|---|
| 36 | **********************************************************************/
|
|---|
| 37 |
|
|---|
| 38 | #include "qbitmap.h"
|
|---|
| 39 | #include "qimage.h"
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 | /*!
|
|---|
| 43 | \class QBitmap qbitmap.h
|
|---|
| 44 | \brief The QBitmap class provides monochrome (1-bit depth) pixmaps.
|
|---|
| 45 |
|
|---|
| 46 | \ingroup graphics
|
|---|
| 47 | \ingroup images
|
|---|
| 48 | \ingroup shared
|
|---|
| 49 |
|
|---|
| 50 | The QBitmap class is a monochrome off-screen paint device used
|
|---|
| 51 | mainly for creating custom QCursor and QBrush objects, in
|
|---|
| 52 | QPixmap::setMask() and for QRegion.
|
|---|
| 53 |
|
|---|
| 54 | A QBitmap is a QPixmap with a \link QPixmap::depth() depth\endlink
|
|---|
| 55 | of 1. If a pixmap with a depth greater than 1 is assigned to a
|
|---|
| 56 | bitmap, the bitmap will be dithered automatically. A QBitmap is
|
|---|
| 57 | guaranteed to always have the depth 1, unless it is
|
|---|
| 58 | QPixmap::isNull() which has depth 0.
|
|---|
| 59 |
|
|---|
| 60 | When drawing in a QBitmap (or QPixmap with depth 1), we recommend
|
|---|
| 61 | using the QColor objects \c Qt::color0 and \c Qt::color1.
|
|---|
| 62 | Painting with \c color0 sets the bitmap bits to 0, and painting
|
|---|
| 63 | with \c color1 sets the bits to 1. For a bitmap, 0-bits indicate
|
|---|
| 64 | background (or transparent) and 1-bits indicate foreground (or
|
|---|
| 65 | opaque). Using the \c black and \c white QColor objects make no
|
|---|
| 66 | sense because the QColor::pixel() value is not necessarily 0 for
|
|---|
| 67 | black and 1 for white.
|
|---|
| 68 |
|
|---|
| 69 | The QBitmap can be transformed (translated, scaled, sheared or
|
|---|
| 70 | rotated) using xForm().
|
|---|
| 71 |
|
|---|
| 72 | Just like the QPixmap class, QBitmap is optimized by the use of
|
|---|
| 73 | \link shclass.html implicit sharing\endlink, so it is very
|
|---|
| 74 | efficient to pass QBitmap objects as arguments.
|
|---|
| 75 |
|
|---|
| 76 | \sa QPixmap, QPainter::drawPixmap(), bitBlt(), \link shclass.html Shared Classes\endlink
|
|---|
| 77 | */
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 | /*!
|
|---|
| 81 | Constructs a null bitmap.
|
|---|
| 82 |
|
|---|
| 83 | \sa QPixmap::isNull()
|
|---|
| 84 | */
|
|---|
| 85 |
|
|---|
| 86 | QBitmap::QBitmap()
|
|---|
| 87 | {
|
|---|
| 88 | data->bitmap = TRUE;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 | /*!
|
|---|
| 93 | Constructs a bitmap with width \a w and height \a h.
|
|---|
| 94 |
|
|---|
| 95 | The contents of the bitmap is uninitialized if \a clear is FALSE;
|
|---|
| 96 | otherwise it is filled with pixel value 0 (the QColor \c
|
|---|
| 97 | Qt::color0).
|
|---|
| 98 |
|
|---|
| 99 | The optional \a optimization argument specifies the optimization
|
|---|
| 100 | setting for the bitmap. The default optimization should be used in
|
|---|
| 101 | most cases. Games and other pixmap-intensive applications may
|
|---|
| 102 | benefit from setting this argument; see \l{QPixmap::Optimization}.
|
|---|
| 103 |
|
|---|
| 104 | \sa QPixmap::setOptimization(), QPixmap::setDefaultOptimization()
|
|---|
| 105 | */
|
|---|
| 106 |
|
|---|
| 107 | QBitmap::QBitmap( int w, int h, bool clear,
|
|---|
| 108 | QPixmap::Optimization optimization )
|
|---|
| 109 | : QPixmap( w, h, 1, optimization )
|
|---|
| 110 | {
|
|---|
| 111 | data->bitmap = TRUE;
|
|---|
| 112 | if ( clear )
|
|---|
| 113 | fill( Qt::color0 );
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 | /*!
|
|---|
| 118 | \overload
|
|---|
| 119 |
|
|---|
| 120 | Constructs a bitmap with the size \a size.
|
|---|
| 121 |
|
|---|
| 122 | The contents of the bitmap is uninitialized if \a clear is FALSE;
|
|---|
| 123 | otherwise it is filled with pixel value 0 (the QColor \c
|
|---|
| 124 | Qt::color0).
|
|---|
| 125 |
|
|---|
| 126 | The optional \a optimization argument specifies the optimization
|
|---|
| 127 | setting for the bitmap. The default optimization should be used in
|
|---|
| 128 | most cases. Games and other pixmap-intensive applications may
|
|---|
| 129 | benefit from setting this argument; see \l{QPixmap::Optimization}.
|
|---|
| 130 | */
|
|---|
| 131 |
|
|---|
| 132 | QBitmap::QBitmap( const QSize &size, bool clear,
|
|---|
| 133 | QPixmap::Optimization optimization )
|
|---|
| 134 | : QPixmap( size, 1, optimization )
|
|---|
| 135 | {
|
|---|
| 136 | data->bitmap = TRUE;
|
|---|
| 137 | if ( clear )
|
|---|
| 138 | fill( Qt::color0 );
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 | /*!
|
|---|
| 143 | Constructs a bitmap with width \a w and height \a h and sets the
|
|---|
| 144 | contents to \a bits.
|
|---|
| 145 |
|
|---|
| 146 | The \a isXbitmap flag should be TRUE if \a bits was generated by
|
|---|
| 147 | the X11 bitmap program. The X bitmap bit order is little endian.
|
|---|
| 148 | The QImage documentation discusses bit order of monochrome images.
|
|---|
| 149 |
|
|---|
| 150 | Example (creates an arrow bitmap):
|
|---|
| 151 | \code
|
|---|
| 152 | uchar arrow_bits[] = { 0x3f, 0x1f, 0x0f, 0x1f, 0x3b, 0x71, 0xe0, 0xc0 };
|
|---|
| 153 | QBitmap bm( 8, 8, arrow_bits, TRUE );
|
|---|
| 154 | \endcode
|
|---|
| 155 | */
|
|---|
| 156 |
|
|---|
| 157 | QBitmap::QBitmap( int w, int h, const uchar *bits, bool isXbitmap )
|
|---|
| 158 | : QPixmap( w, h, bits, isXbitmap )
|
|---|
| 159 | {
|
|---|
| 160 | data->bitmap = TRUE;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 | /*!
|
|---|
| 165 | \overload
|
|---|
| 166 |
|
|---|
| 167 | Constructs a bitmap with the size \a size and sets the contents to
|
|---|
| 168 | \a bits.
|
|---|
| 169 |
|
|---|
| 170 | The \a isXbitmap flag should be TRUE if \a bits was generated by
|
|---|
| 171 | the X11 bitmap program. The X bitmap bit order is little endian.
|
|---|
| 172 | The QImage documentation discusses bit order of monochrome images.
|
|---|
| 173 | */
|
|---|
| 174 |
|
|---|
| 175 | QBitmap::QBitmap( const QSize &size, const uchar *bits, bool isXbitmap )
|
|---|
| 176 | : QPixmap( size.width(), size.height(), bits, isXbitmap )
|
|---|
| 177 | {
|
|---|
| 178 | data->bitmap = TRUE;
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 | /*!
|
|---|
| 183 | Constructs a bitmap that is a copy of \a bitmap.
|
|---|
| 184 | */
|
|---|
| 185 |
|
|---|
| 186 | QBitmap::QBitmap( const QBitmap &bitmap )
|
|---|
| 187 | : QPixmap( bitmap )
|
|---|
| 188 | {
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | #ifndef QT_NO_IMAGEIO
|
|---|
| 192 | /*!
|
|---|
| 193 | Constructs a bitmap from the file \a fileName. If the file does
|
|---|
| 194 | not exist or is of an unknown format, the bitmap becomes a null
|
|---|
| 195 | bitmap.
|
|---|
| 196 |
|
|---|
| 197 | The parameters \a fileName and \a format are passed on to
|
|---|
| 198 | QPixmap::load(). Dithering will be performed if the file format
|
|---|
| 199 | uses more than 1 bit per pixel.
|
|---|
| 200 |
|
|---|
| 201 | \sa QPixmap::isNull(), QPixmap::load(), QPixmap::loadFromData(),
|
|---|
| 202 | QPixmap::save(), QPixmap::imageFormat()
|
|---|
| 203 | */
|
|---|
| 204 |
|
|---|
| 205 | QBitmap::QBitmap( const QString& fileName, const char *format )
|
|---|
| 206 | : QPixmap() // Will set bitmap to null bitmap, explicit call for clarity
|
|---|
| 207 | {
|
|---|
| 208 | data->bitmap = TRUE;
|
|---|
| 209 | load( fileName, format, Mono );
|
|---|
| 210 | }
|
|---|
| 211 | #endif
|
|---|
| 212 |
|
|---|
| 213 | /*!
|
|---|
| 214 | Assigns the bitmap \a bitmap to this bitmap and returns a
|
|---|
| 215 | reference to this bitmap.
|
|---|
| 216 | */
|
|---|
| 217 |
|
|---|
| 218 | QBitmap &QBitmap::operator=( const QBitmap &bitmap )
|
|---|
| 219 | {
|
|---|
| 220 | QPixmap::operator=(bitmap);
|
|---|
| 221 | #if defined(QT_CHECK_STATE)
|
|---|
| 222 | Q_ASSERT( data->bitmap );
|
|---|
| 223 | #endif
|
|---|
| 224 | return *this;
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 | /*!
|
|---|
| 229 | \overload
|
|---|
| 230 |
|
|---|
| 231 | Assigns the pixmap \a pixmap to this bitmap and returns a
|
|---|
| 232 | reference to this bitmap.
|
|---|
| 233 |
|
|---|
| 234 | Dithering will be performed if the pixmap has a QPixmap::depth()
|
|---|
| 235 | greater than 1.
|
|---|
| 236 | */
|
|---|
| 237 |
|
|---|
| 238 | QBitmap &QBitmap::operator=( const QPixmap &pixmap )
|
|---|
| 239 | {
|
|---|
| 240 | if ( pixmap.isNull() ) { // a null pixmap
|
|---|
| 241 | QBitmap bm( 0, 0, FALSE, pixmap.optimization() );
|
|---|
| 242 | QBitmap::operator=(bm);
|
|---|
| 243 | } else if ( pixmap.depth() == 1 ) { // 1-bit pixmap
|
|---|
| 244 | if ( pixmap.isQBitmap() ) { // another QBitmap
|
|---|
| 245 | QPixmap::operator=(pixmap); // shallow assignment
|
|---|
| 246 | } else { // not a QBitmap, but 1-bit
|
|---|
| 247 | QBitmap bm( pixmap.size(), FALSE, pixmap.optimization() );
|
|---|
| 248 | bitBlt( &bm, 0,0, &pixmap, 0,0,pixmap.width(),pixmap.height() );
|
|---|
| 249 | QBitmap::operator=(bm);
|
|---|
| 250 | }
|
|---|
| 251 | } else { // n-bit depth pixmap
|
|---|
| 252 | QImage image;
|
|---|
| 253 | image = pixmap; // convert pixmap to image
|
|---|
| 254 | *this = image; // will dither image
|
|---|
| 255 | }
|
|---|
| 256 | return *this;
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 |
|
|---|
| 260 | /*!
|
|---|
| 261 | \overload
|
|---|
| 262 |
|
|---|
| 263 | Converts the image \a image to a bitmap and assigns the result to
|
|---|
| 264 | this bitmap. Returns a reference to the bitmap.
|
|---|
| 265 |
|
|---|
| 266 | Dithering will be performed if the image has a QImage::depth()
|
|---|
| 267 | greater than 1.
|
|---|
| 268 | */
|
|---|
| 269 |
|
|---|
| 270 | QBitmap &QBitmap::operator=( const QImage &image )
|
|---|
| 271 | {
|
|---|
| 272 | convertFromImage( image );
|
|---|
| 273 | return *this;
|
|---|
| 274 | }
|
|---|
| 275 |
|
|---|
| 276 |
|
|---|
| 277 | #ifndef QT_NO_PIXMAP_TRANSFORMATION
|
|---|
| 278 | /*!
|
|---|
| 279 | Returns a transformed copy of this bitmap by using \a matrix.
|
|---|
| 280 |
|
|---|
| 281 | This function does exactly the same as QPixmap::xForm(), except
|
|---|
| 282 | that it returns a QBitmap instead of a QPixmap.
|
|---|
| 283 |
|
|---|
| 284 | \sa QPixmap::xForm()
|
|---|
| 285 | */
|
|---|
| 286 |
|
|---|
| 287 | QBitmap QBitmap::xForm( const QWMatrix &matrix ) const
|
|---|
| 288 | {
|
|---|
| 289 | QPixmap pm = QPixmap::xForm( matrix );
|
|---|
| 290 | QBitmap bm;
|
|---|
| 291 | // Here we fake the pixmap to think it's a QBitmap. With this trick,
|
|---|
| 292 | // the QBitmap::operator=(const QPixmap&) will just refer the
|
|---|
| 293 | // pm.data and we do not need to perform a bitBlt.
|
|---|
| 294 | pm.data->bitmap = TRUE;
|
|---|
| 295 | bm = pm;
|
|---|
| 296 | return bm;
|
|---|
| 297 | }
|
|---|
| 298 | #endif // QT_NO_TRANSFORMATIONS
|
|---|
| 299 |
|
|---|
| 300 |
|
|---|
| 301 |
|
|---|