| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com)
|
|---|
| 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 qt-sales@nokia.com.
|
|---|
| 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 |
|
|---|
| 50 | QT_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 |
|
|---|
| 102 | QBitmap::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 |
|
|---|
| 116 | QBitmap::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 |
|
|---|
| 128 | QBitmap::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 |
|
|---|
| 148 | QBitmap::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 |
|
|---|
| 173 | QBitmap::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 |
|
|---|
| 191 | QBitmap &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
|
|---|
| 208 | QBitmap::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 |
|
|---|
| 214 | QBitmap::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 | */
|
|---|
| 223 | QBitmap::~QBitmap()
|
|---|
| 224 | {
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | /*!
|
|---|
| 228 | Returns the bitmap as a QVariant.
|
|---|
| 229 | */
|
|---|
| 230 | QBitmap::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 | */
|
|---|
| 251 | QBitmap 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);
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | QPixmapData *d;
|
|---|
| 269 | QGraphicsSystem* gs = QApplicationPrivate::graphicsSystem();
|
|---|
| 270 | if (gs)
|
|---|
| 271 | d = gs->createPixmapData(QPixmapData::BitmapType);
|
|---|
| 272 | else
|
|---|
| 273 | d = QGraphicsSystem::createDefaultPixmapData(QPixmapData::BitmapType);
|
|---|
| 274 |
|
|---|
| 275 | d->fromImage(img, flags | Qt::MonoOnly);
|
|---|
| 276 | return QPixmap(d);
|
|---|
| 277 | }
|
|---|
| 278 |
|
|---|
| 279 | /*!
|
|---|
| 280 | Constructs a bitmap with the given \a size, and sets the contents to
|
|---|
| 281 | the \a bits supplied.
|
|---|
| 282 |
|
|---|
| 283 | The bitmap data has to be byte aligned and provided in in the bit
|
|---|
| 284 | order specified by \a monoFormat. The mono format must be either
|
|---|
| 285 | QImage::Format_Mono or QImage::Format_MonoLSB. Use
|
|---|
| 286 | QImage::Format_Mono to specify data on the XBM format.
|
|---|
| 287 |
|
|---|
| 288 | \sa fromImage()
|
|---|
| 289 |
|
|---|
| 290 | */
|
|---|
| 291 | QBitmap QBitmap::fromData(const QSize &size, const uchar *bits, QImage::Format monoFormat)
|
|---|
| 292 | {
|
|---|
| 293 | Q_ASSERT(monoFormat == QImage::Format_Mono || monoFormat == QImage::Format_MonoLSB);
|
|---|
| 294 |
|
|---|
| 295 | QImage image(size, monoFormat);
|
|---|
| 296 | image.setColor(0, QColor(Qt::color0).rgb());
|
|---|
| 297 | image.setColor(1, QColor(Qt::color1).rgb());
|
|---|
| 298 |
|
|---|
| 299 | // Need to memcpy each line separatly since QImage is 32bit aligned and
|
|---|
| 300 | // this data is only byte aligned...
|
|---|
| 301 | int bytesPerLine = (size.width() + 7) / 8;
|
|---|
| 302 | for (int y = 0; y < size.height(); ++y)
|
|---|
| 303 | memcpy(image.scanLine(y), bits + bytesPerLine * y, bytesPerLine);
|
|---|
| 304 | return QBitmap::fromImage(image);
|
|---|
| 305 | }
|
|---|
| 306 |
|
|---|
| 307 | /*!
|
|---|
| 308 | Returns a copy of this bitmap, transformed according to the given
|
|---|
| 309 | \a matrix.
|
|---|
| 310 |
|
|---|
| 311 | \sa QPixmap::transformed()
|
|---|
| 312 | */
|
|---|
| 313 | QBitmap QBitmap::transformed(const QTransform &matrix) const
|
|---|
| 314 | {
|
|---|
| 315 | QBitmap bm = QPixmap::transformed(matrix);
|
|---|
| 316 | return bm;
|
|---|
| 317 | }
|
|---|
| 318 |
|
|---|
| 319 | /*!
|
|---|
| 320 | \overload
|
|---|
| 321 |
|
|---|
| 322 | This convenience function converts the \a matrix to a QTransform
|
|---|
| 323 | and calls the overloaded function.
|
|---|
| 324 | */
|
|---|
| 325 | QBitmap QBitmap::transformed(const QMatrix &matrix) const
|
|---|
| 326 | {
|
|---|
| 327 | return transformed(QTransform(matrix));
|
|---|
| 328 | }
|
|---|
| 329 |
|
|---|
| 330 | #ifdef QT3_SUPPORT
|
|---|
| 331 | /*!
|
|---|
| 332 | \fn QBitmap QBitmap::xForm(const QMatrix &matrix) const
|
|---|
| 333 |
|
|---|
| 334 | Returns a copy of this bitmap, transformed according to the given
|
|---|
| 335 | \a matrix.
|
|---|
| 336 |
|
|---|
| 337 | Use transformed() instead.
|
|---|
| 338 | */
|
|---|
| 339 |
|
|---|
| 340 | /*!
|
|---|
| 341 | \fn QBitmap::QBitmap(const QSize &size, bool clear)
|
|---|
| 342 |
|
|---|
| 343 | Constructs a bitmap with the given \a size. If \a clear is true,
|
|---|
| 344 | the bits are initialized to Qt::color0.
|
|---|
| 345 |
|
|---|
| 346 | Use the corresponding QBitmap() constructor instead, and then call
|
|---|
| 347 | the clear() function if the \a clear parameter is true.
|
|---|
| 348 | */
|
|---|
| 349 |
|
|---|
| 350 | /*!
|
|---|
| 351 | \fn QBitmap::QBitmap(int width, int height, bool clear)
|
|---|
| 352 |
|
|---|
| 353 | Constructs a bitmap with the given \a width and \a height. If \a
|
|---|
| 354 | clear is true, the bits are initialized to Qt::color0.
|
|---|
| 355 |
|
|---|
| 356 | Use the corresponding QBitmap() constructor instead, and then call
|
|---|
| 357 | the clear() function if the \a clear parameter is true.
|
|---|
| 358 | */
|
|---|
| 359 |
|
|---|
| 360 | /*!
|
|---|
| 361 | \fn QBitmap::QBitmap(int width, int height, const uchar *bits, bool isXbitmap)
|
|---|
| 362 |
|
|---|
| 363 | Constructs a bitmap with the given \a width and \a height, and
|
|---|
| 364 | sets the contents to the \a bits supplied. The \a isXbitmap flag
|
|---|
| 365 | should be true if \a bits was generated by the X11 bitmap
|
|---|
| 366 | program.
|
|---|
| 367 |
|
|---|
| 368 | Use the static fromData() function instead. If \a isXbitmap is
|
|---|
| 369 | true, use the default bit order(QImage_FormatMonoLSB) otherwise
|
|---|
| 370 | use QImage::Format_Mono.
|
|---|
| 371 |
|
|---|
| 372 | \omit
|
|---|
| 373 | The X bitmap bit order is little endian. The QImage
|
|---|
| 374 | documentation discusses bit order of monochrome images. Opposed to
|
|---|
| 375 | QImage, the data has to be byte aligned.
|
|---|
| 376 |
|
|---|
| 377 | Example (creates an arrow bitmap):
|
|---|
| 378 | \snippet doc/src/snippets/code/src_gui_image_qbitmap.cpp 0
|
|---|
| 379 | \endomit
|
|---|
| 380 | */
|
|---|
| 381 |
|
|---|
| 382 |
|
|---|
| 383 | /*!
|
|---|
| 384 | \fn QBitmap::QBitmap(const QSize &size, const uchar *bits, bool isXbitmap)
|
|---|
| 385 |
|
|---|
| 386 | \overload
|
|---|
| 387 |
|
|---|
| 388 | Constructs a bitmap with the given \a size, and sets the contents
|
|---|
| 389 | to the \a bits supplied. The \a isXbitmap flag should be true if
|
|---|
| 390 | \a bits was generated by the X11 bitmap program.
|
|---|
| 391 |
|
|---|
| 392 | \omit
|
|---|
| 393 | The X bitmap bit order is little endian. The QImage documentation
|
|---|
| 394 | discusses bit order of monochrome images.
|
|---|
| 395 | \endomit
|
|---|
| 396 |
|
|---|
| 397 | Use the static fromData() function instead. If \a isXbitmap is
|
|---|
| 398 | true, use the default bit order(QImage_FormatMonoLSB) otherwise
|
|---|
| 399 | use QImage::Format_Mono.
|
|---|
| 400 | */
|
|---|
| 401 | #endif
|
|---|
| 402 |
|
|---|
| 403 | QT_END_NAMESPACE
|
|---|