QtBase  v6.3.1
qbitmap.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
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 The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #include "qbitmap.h"
41 #include <qpa/qplatformpixmap.h>
42 #include <qpa/qplatformintegration.h>
43 #include "qimage.h"
44 #include "qscreen.h"
45 #include "qvariant.h"
46 #include <qpainter.h>
47 #include <private/qguiapplication_p.h>
48 
49 #include <memory>
50 
52 
108 {
109 }
110 
121 {
122 }
123 
134 {
135 }
136 
141 QBitmap::~QBitmap() = default;
142 
162 {
164 }
165 
177 QBitmap::operator QVariant() const
178 {
179  return QVariant::fromValue(*this);
180 }
181 
182 static QBitmap makeBitmap(QImage &&image, Qt::ImageConversionFlags flags)
183 {
184  // make sure image.color(0) == Qt::color0 (white)
185  // and image.color(1) == Qt::color1 (black)
186  const QRgb c0 = QColor(Qt::black).rgb();
187  const QRgb c1 = QColor(Qt::white).rgb();
188  if (image.color(0) == c0 && image.color(1) == c1) {
189  image.invertPixels();
190  image.setColor(0, c1);
191  image.setColor(1, c0);
192  }
193 
194  std::unique_ptr<QPlatformPixmap> data(QGuiApplicationPrivate::platformIntegration()->createPlatformPixmap(QPlatformPixmap::BitmapType));
195 
196  data->fromImageInPlace(image, flags | Qt::MonoOnly);
197  return QBitmap::fromPixmap(QPixmap(data.release()));
198 }
199 
206 QBitmap QBitmap::fromImage(const QImage &image, Qt::ImageConversionFlags flags)
207 {
208  if (image.isNull())
209  return QBitmap();
210 
211  return makeBitmap(image.convertToFormat(QImage::Format_MonoLSB, flags), flags);
212 }
213 
223 QBitmap QBitmap::fromImage(QImage &&image, Qt::ImageConversionFlags flags)
224 {
225  if (image.isNull())
226  return QBitmap();
227 
228  return makeBitmap(std::move(image).convertToFormat(QImage::Format_MonoLSB, flags), flags);
229 }
230 
244 {
245  Q_ASSERT(monoFormat == QImage::Format_Mono || monoFormat == QImage::Format_MonoLSB);
246 
247  QImage image(size, monoFormat);
248  image.setColor(0, QColor(Qt::color0).rgb());
249  image.setColor(1, QColor(Qt::color1).rgb());
250 
251  // Need to memcpy each line separately since QImage is 32bit aligned and
252  // this data is only byte aligned...
253  int bytesPerLine = (size.width() + 7) / 8;
254  for (int y = 0; y < size.height(); ++y)
255  memcpy(image.scanLine(y), bits + bytesPerLine * y, bytesPerLine);
256  return QBitmap::fromImage(std::move(image));
257 }
258 
271 {
272  if (pixmap.isNull()) { // a null pixmap
273  return QBitmap(0, 0);
274  } else if (pixmap.depth() == 1) { // 1-bit pixmap
275  QBitmap bm;
276  if (pixmap.paintingActive()) { // make a deep copy
277  pixmap.copy().swap(bm);
278  } else {
279  bm.data = pixmap.data; // shallow assignment
280  }
281  return bm;
282  }
283  // n-bit depth pixmap, will dither image
284  return fromImage(pixmap.toImage());
285 }
286 
287 #if QT_DEPRECATED_SINCE(6, 0)
298 {
299  *this = QBitmap::fromPixmap(pixmap);
300 }
301 
315 {
316  *this = QBitmap::fromPixmap(pixmap);
317  return *this;
318 }
319 #endif
320 
328 {
330 }
331 
The QBitmap class provides monochrome (1-bit depth) pixmaps.
Definition: qbitmap.h:52
static QBitmap fromImage(const QImage &image, Qt::ImageConversionFlags flags=Qt::AutoColor)
Definition: qbitmap.cpp:206
QBitmap()
Definition: qbitmap.cpp:106
QBitmap transformed(const QTransform &matrix) const
Definition: qbitmap.cpp:327
static QBitmap fromData(const QSize &size, const uchar *bits, QImage::Format monoFormat=QImage::Format_MonoLSB)
Definition: qbitmap.cpp:243
static QBitmap fromPixmap(const QPixmap &pixmap)
Definition: qbitmap.cpp:270
~QBitmap() override
char * data()
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
QRgb rgb() const noexcept
Definition: qcolor.cpp:1436
QRgb
Definition: qrgb.h:49
static QPlatformIntegration * platformIntegration()
The QImage class provides a hardware-independent image representation that allows direct access to th...
Definition: qimage.h:73
Format
Definition: qimage.h:77
@ Format_MonoLSB
Definition: qimage.h:80
@ Format_Mono
Definition: qimage.h:79
The QPixmap class is an off-screen image representation that can be used as a paint device.
Definition: qpixmap.h:63
bool load(const QString &fileName, const char *format=nullptr, Qt::ImageConversionFlags flags=Qt::AutoColor)
Definition: qpixmap.cpp:737
QPixmap transformed(const QTransform &, Qt::TransformationMode mode=Qt::FastTransformation) const
Definition: qpixmap.cpp:1169
QPixmap & operator=(const QPixmap &)
Definition: qpixmap.cpp:381
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:55
The QString class provides a Unicode character string.
Definition: qstring.h:388
The QTransform class specifies 2D transformations of a coordinate system.
Definition: qtransform.h:56
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:95
static auto fromValue(const T &value) -> std::enable_if_t< std::is_copy_constructible_v< T >, QVariant >
Definition: qvariant.h:391
@ MonoOnly
Definition: qnamespace.h:505
@ color1
Definition: qnamespace.h:60
@ white
Definition: qnamespace.h:62
@ black
Definition: qnamespace.h:61
@ color0
Definition: qnamespace.h:59
Definition: image.cpp:51
@ BitmapType
Definition: paintcommands.h:50
#define rgb(r, g, b)
Definition: qcolor.cpp:157
QT_BEGIN_INCLUDE_NAMESPACE typedef unsigned char uchar
Definition: qglobal.h:332
GLfloat GLfloat GLfloat w
[0]
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLbitfield flags
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLint GLsizei GLsizei GLenum format
GLint y
GLfloat GLfloat GLfloat GLfloat h
GLeglImageOES image
GLenum GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const void * bits
Definition: qopenglext.h:6904
GLuint GLenum matrix
Definition: qopenglext.h:11564
#define Q_ASSERT(cond)
Definition: qrandom.cpp:84
QBitmap bm(8, 8, arrow_bits, true)
widget render & pixmap