Qt 4.8
qbitmap.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
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 
51 
107  : QPixmap(QSize(0, 0), QPixmapData::BitmapType)
108 {
109 }
110 
123 QBitmap::QBitmap(int w, int h)
125 {
126 }
127 
136  : QPixmap(size, QPixmapData::BitmapType)
137 {
138 }
139 
158 QBitmap::QBitmap(const QPixmap &pixmap)
159 {
160  QBitmap::operator=(pixmap);
161 }
162 
188 {
189  load(fileName, format, Qt::MonoOnly);
190 }
191 
208 {
209  if (pixmap.isNull()) { // a null pixmap
210  QBitmap bm(0, 0);
211  QBitmap::operator=(bm);
212  } else if (pixmap.depth() == 1) { // 1-bit pixmap
213  QPixmap::operator=(pixmap); // shallow assignment
214  } else { // n-bit depth pixmap
215  QImage image;
216  image = pixmap.toImage(); // convert pixmap to image
217  *this = fromImage(image); // will dither image
218  }
219  return *this;
220 }
221 
222 
223 #ifdef QT3_SUPPORT
224 QBitmap::QBitmap(int w, int h, const uchar *bits, bool isXbitmap)
225 {
226  *this = fromData(QSize(w, h), bits, isXbitmap ? QImage::Format_MonoLSB : QImage::Format_Mono);
227 }
228 
229 
230 QBitmap::QBitmap(const QSize &size, const uchar *bits, bool isXbitmap)
231 {
232  *this = fromData(size, bits, isXbitmap ? QImage::Format_MonoLSB : QImage::Format_Mono);
233 }
234 #endif
235 
240 {
241 }
242 
257 QBitmap::operator QVariant() const
258 {
259  return QVariant(QVariant::Bitmap, this);
260 }
261 
281 QBitmap QBitmap::fromImage(const QImage &image, Qt::ImageConversionFlags flags)
282 {
283  if (image.isNull())
284  return QBitmap();
285 
286  QImage img = image.convertToFormat(QImage::Format_MonoLSB, flags);
287 
288  // make sure image.color(0) == Qt::color0 (white)
289  // and image.color(1) == Qt::color1 (black)
290  const QRgb c0 = QColor(Qt::black).rgb();
291  const QRgb c1 = QColor(Qt::white).rgb();
292  if (img.color(0) == c0 && img.color(1) == c1) {
293  img.invertPixels();
294  img.setColor(0, c1);
295  img.setColor(1, c0);
296  }
297 
301 
302  data->fromImage(img, flags | Qt::MonoOnly);
303  return QPixmap(data.take());
304 }
305 
318 QBitmap QBitmap::fromData(const QSize &size, const uchar *bits, QImage::Format monoFormat)
319 {
320  Q_ASSERT(monoFormat == QImage::Format_Mono || monoFormat == QImage::Format_MonoLSB);
321 
322  QImage image(size, monoFormat);
323  image.setColor(0, QColor(Qt::color0).rgb());
324  image.setColor(1, QColor(Qt::color1).rgb());
325 
326  // Need to memcpy each line separatly since QImage is 32bit aligned and
327  // this data is only byte aligned...
328  int bytesPerLine = (size.width() + 7) / 8;
329  for (int y = 0; y < size.height(); ++y)
330  memcpy(image.scanLine(y), bits + bytesPerLine * y, bytesPerLine);
331  return QBitmap::fromImage(image);
332 }
333 
341 {
342  QBitmap bm = QPixmap::transformed(matrix);
343  return bm;
344 }
345 
357 {
358  return transformed(QTransform(matrix));
359 }
360 
361 #ifdef QT3_SUPPORT
362 
441 #endif
442 
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
QImage toImage() const
Converts the pixmap to a QImage.
Definition: qpixmap.cpp:542
Format
The following image formats are available in Qt.
Definition: qimage.h:91
unsigned int QRgb
Definition: qrgb.h:53
QPixmap & operator=(const QPixmap &)
Assigns the given pixmap to this pixmap and returns a reference to this pixmap.
Definition: qpixmap.cpp:471
void setColor(int i, QRgb c)
Sets the color at the given index in the color table, to the given to colorValue. ...
Definition: qimage.cpp:1850
QPixmap()
Constructs a null pixmap.
Definition: qpixmap.cpp:174
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
~QBitmap()
Destroys the bitmap.
Definition: qbitmap.cpp:239
QSize size() const
Returns the size of the pixmap.
Definition: qpixmap.cpp:661
QBitmap & operator=(const QPixmap &)
Assigns the given pixmap to this bitmap and returns a reference to this bitmap.
Definition: qbitmap.cpp:207
The QMatrix class specifies 2D transformations of a coordinate system.
Definition: qmatrix.h:61
QExplicitlySharedDataPointer< QPixmapData > data
Definition: qpixmap.h:252
bool isNull() const
Returns true if it is a null image, otherwise returns false.
Definition: qimage.cpp:1542
static QBitmap fromData(const QSize &size, const uchar *bits, QImage::Format monoFormat=QImage::Format_MonoLSB)
Constructs a bitmap with the given size, and sets the contents to the bits supplied.
Definition: qbitmap.cpp:318
QPixmap transformed(const QMatrix &, Qt::TransformationMode mode=Qt::FastTransformation) const
This convenience function loads the matrix into a QTransform and calls the overloaded function...
Definition: qpixmap.cpp:1723
int depth() const
Returns the depth of the pixmap.
Definition: qpixmap.cpp:695
bool load(const QString &fileName, const char *format=0, Qt::ImageConversionFlags flags=Qt::AutoColor)
Loads a pixmap from the file with the given fileName.
Definition: qpixmap.cpp:930
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QBitmap transformed(const QMatrix &) const
This convenience function converts the matrix to a QTransform and calls the overloaded function...
Definition: qbitmap.cpp:356
The QScopedPointer class stores a pointer to a dynamically allocated object, and deletes it upon dest...
unsigned char uchar
Definition: qglobal.h:994
The QBitmap class provides monochrome (1-bit depth) pixmaps.
Definition: qbitmap.h:55
int width() const
Returns the width.
Definition: qsize.h:126
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
virtual void fromImage(const QImage &image, Qt::ImageConversionFlags flags)=0
QBitmap()
Constructs a null bitmap.
Definition: qbitmap.cpp:106
The QImage class provides a hardware-independent image representation that allows direct access to th...
Definition: qimage.h:87
#define rgb(r, g, b)
Definition: qcolor_p.cpp:130
QImage convertToFormat(Format f, Qt::ImageConversionFlags flags=Qt::AutoColor) const Q_REQUIRED_RESULT
Returns a copy of the image in the given format.
Definition: qimage.cpp:3966
int height() const
Returns the height.
Definition: qsize.h:129
The QPixmap class is an off-screen image representation that can be used as a paint device...
Definition: qpixmap.h:71
static QBitmap fromImage(const QImage &image, Qt::ImageConversionFlags flags=Qt::AutoColor)
Returns a copy of the given image converted to a bitmap using the specified image conversion flags...
Definition: qbitmap.cpp:281
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
void invertPixels(InvertMode=InvertRgb)
Inverts all pixel values in the image.
Definition: qimage.cpp:2179
virtual QPixmapData * createPixmapData(QPixmapData::PixelType type) const =0
bool isNull() const
Returns true if this is a null pixmap; otherwise returns false.
Definition: qpixmap.cpp:615
static QGraphicsSystem * graphicsSystem()
static QPixmapData * createDefaultPixmapData(QPixmapData::PixelType type)
static QString fileName(const QString &fileUrl)
QRgb color(int i) const
Returns the color in the color table at index i.
Definition: qimage.cpp:1829
QRgb rgb() const
Returns the RGB value of the color.
Definition: qcolor.cpp:1051
uchar * scanLine(int)
Returns a pointer to the pixel data at the scanline with index i.
Definition: qimage.cpp:1886
The QTransform class specifies 2D transformations of a coordinate system.
Definition: qtransform.h:65