Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qbitmap.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qbitmap.h"
5#include <qpa/qplatformpixmap.h>
6#include <qpa/qplatformintegration.h>
7#include "qimage.h"
8#include "qscreen.h"
9#include "qvariant.h"
10#include <qpainter.h>
11#include <private/qguiapplication_p.h>
12
13#include <memory>
14
16
71 : QPixmap(QSize(0, 0), QPlatformPixmap::BitmapType)
72{
73}
74
84 : QPixmap(QSize(w, h), QPlatformPixmap::BitmapType)
85{
86}
87
97 : QPixmap(size, QPlatformPixmap::BitmapType)
98{
99}
100
105QBitmap::~QBitmap() = default;
106
125 : QPixmap(QSize(0, 0), QPlatformPixmap::BitmapType)
126{
128}
129
141QBitmap::operator QVariant() const
142{
143 return QVariant::fromValue(*this);
144}
145
146static QBitmap makeBitmap(QImage &&image, Qt::ImageConversionFlags flags)
147{
148 // make sure image.color(0) == Qt::color0 (white)
149 // and image.color(1) == Qt::color1 (black)
150 const QRgb c0 = QColor(Qt::black).rgb();
151 const QRgb c1 = QColor(Qt::white).rgb();
152 if (image.color(0) == c0 && image.color(1) == c1) {
153 image.invertPixels();
154 image.setColor(0, c1);
155 image.setColor(1, c0);
156 }
157
158 std::unique_ptr<QPlatformPixmap> data(QGuiApplicationPrivate::platformIntegration()->createPlatformPixmap(QPlatformPixmap::BitmapType));
159
160 data->fromImageInPlace(image, flags | Qt::MonoOnly);
161 return QBitmap::fromPixmap(QPixmap(data.release()));
162}
163
170QBitmap QBitmap::fromImage(const QImage &image, Qt::ImageConversionFlags flags)
171{
172 if (image.isNull())
173 return QBitmap();
174
175 return makeBitmap(image.convertToFormat(QImage::Format_MonoLSB, flags), flags);
176}
177
187QBitmap QBitmap::fromImage(QImage &&image, Qt::ImageConversionFlags flags)
188{
189 if (image.isNull())
190 return QBitmap();
191
192 return makeBitmap(std::move(image).convertToFormat(QImage::Format_MonoLSB, flags), flags);
193}
194
208{
209 Q_ASSERT(monoFormat == QImage::Format_Mono || monoFormat == QImage::Format_MonoLSB);
210
211 QImage image(size, monoFormat);
212 image.setColor(0, QColor(Qt::color0).rgb());
213 image.setColor(1, QColor(Qt::color1).rgb());
214
215 // Need to memcpy each line separately since QImage is 32bit aligned and
216 // this data is only byte aligned...
217 int bytesPerLine = (size.width() + 7) / 8;
218 for (int y = 0; y < size.height(); ++y)
219 memcpy(image.scanLine(y), bits + bytesPerLine * y, bytesPerLine);
220 return QBitmap::fromImage(std::move(image));
221}
222
235{
236 if (pixmap.isNull()) { // a null pixmap
237 return QBitmap(0, 0);
238 } else if (pixmap.depth() == 1) { // 1-bit pixmap
239 QBitmap bm;
240 if (pixmap.paintingActive()) { // make a deep copy
241 pixmap.copy().swap(bm);
242 } else {
243 bm.data = pixmap.data; // shallow assignment
244 }
245 return bm;
246 }
247 // n-bit depth pixmap, will dither image
248 return fromImage(pixmap.toImage());
249}
250
251#if QT_DEPRECATED_SINCE(6, 0)
262{
264}
265
279{
281 return *this;
282}
283#endif
284
295
\inmodule QtGui
Definition qbitmap.h:16
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:170
QBitmap()
Constructs a null bitmap.
Definition qbitmap.cpp:70
QBitmap transformed(const QTransform &matrix) const
Returns a copy of this bitmap, transformed according to the given matrix.
Definition qbitmap.cpp:291
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:207
static QBitmap fromPixmap(const QPixmap &pixmap)
Returns a copy of the given pixmap converted to a bitmap.
Definition qbitmap.cpp:234
~QBitmap() override
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition qcolor.h:31
QRgb rgb() const noexcept
Returns the RGB value of the color.
Definition qcolor.cpp:1439
static QPlatformIntegration * platformIntegration()
\inmodule QtGui
Definition qimage.h:37
Format
The following image formats are available in Qt.
Definition qimage.h:41
@ Format_MonoLSB
Definition qimage.h:44
@ Format_Mono
Definition qimage.h:43
Returns a copy of the pixmap that is transformed using the given transformation transform and transfo...
Definition qpixmap.h:27
void swap(QPixmap &other) noexcept
Swaps pixmap other with this pixmap.
Definition qpixmap.h:43
bool load(const QString &fileName, const char *format=nullptr, Qt::ImageConversionFlags flags=Qt::AutoColor)
Loads a pixmap from the file with the given fileName.
Definition qpixmap.cpp:704
QPixmap copy(int x, int y, int width, int height) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qpixmap.h:153
QPixmap transformed(const QTransform &, Qt::TransformationMode mode=Qt::FastTransformation) const
QPixmap & operator=(const QPixmap &)
Assigns the given pixmap to this pixmap and returns a reference to this pixmap.
Definition qpixmap.cpp:347
The QPlatformPixmap class provides an abstraction for native pixmaps.
\inmodule QtCore
Definition qsize.h:25
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
The QTransform class specifies 2D transformations of a coordinate system.
Definition qtransform.h:20
\inmodule QtCore
Definition qvariant.h:65
static auto fromValue(T &&value) noexcept(std::is_nothrow_copy_constructible_v< T > &&Private::CanUseInternalSpace< T >) -> std::enable_if_t< std::conjunction_v< std::is_copy_constructible< T >, std::is_destructible< T > >, QVariant >
Definition qvariant.h:536
Combined button and popup list for selecting options.
@ MonoOnly
Definition qnamespace.h:480
@ color1
Definition qnamespace.h:29
@ white
Definition qnamespace.h:31
@ black
Definition qnamespace.h:30
@ color0
Definition qnamespace.h:28
Definition image.cpp:4
static QBitmap makeBitmap(QImage &&image, Qt::ImageConversionFlags flags)
Definition qbitmap.cpp:146
#define rgb(r, g, b)
Definition qcolor.cpp:124
GLfloat GLfloat GLfloat w
[0]
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLbitfield flags
GLint GLsizei GLsizei GLenum format
GLint y
GLfloat GLfloat GLfloat GLfloat h
GLuint GLenum matrix
GLenum GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const void * bits
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
QT_BEGIN_NAMESPACE typedef unsigned int QRgb
Definition qrgb.h:13
unsigned char uchar
Definition qtypes.h:32
widget render & pixmap