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
qplatformpixmap.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 "qplatformpixmap.h"
5#include <qpa/qplatformintegration.h>
6#include <QtCore/qbuffer.h>
7#include <QtGui/qbitmap.h>
8#include <QtGui/qimagereader.h>
9#include <private/qguiapplication_p.h>
10#include <private/qimagepixmapcleanuphooks_p.h>
11
13
24{
26 qFatal("QPlatformPixmap: QGuiApplication required");
27
29 data->resize(w, h);
30 return data;
31}
32
33
35 : w(0),
36 h(0),
37 d(0),
38 is_null(true),
39 ref(0),
40 detach_no(0),
41 type(pixelType),
42 id(objectId),
43 ser_no(0),
44 is_cached(false)
45{
46}
47
49{
50 // Sometimes the pixmap cleanup hooks will be called from derived classes, which will
51 // then set is_cached to false. For example, on X11 Qt GUI needs to delete the GLXPixmap
52 // or EGL Pixmap Surface for a given pixmap _before_ the native X11 pixmap is deleted,
53 // otherwise some drivers will leak the GL surface. In this case, QX11PlatformPixmap will
54 // call the cleanup hooks itself before deleting the native pixmap and set is_cached to
55 // false.
56 if (is_cached) {
58 is_cached = false;
59 }
60}
61
67
69{
70 if (d->pixelType() == QPlatformPixmap::BitmapType) {
72
73 // make sure image.color(0) == Qt::color0 (white)
74 // and image.color(1) == Qt::color1 (black)
75 const QRgb c0 = QColor(Qt::black).rgb();
76 const QRgb c1 = QColor(Qt::white).rgb();
77 if (img.color(0) == c0 && img.color(1) == c1) {
78 img.invertPixels();
79 img.setColor(0, c1);
80 img.setColor(1, c0);
81 }
82 return img;
83 }
84
85 return image;
86}
87
89 Qt::ImageConversionFlags flags)
90{
91 const QImage image = imageReader->read();
93}
94
96 Qt::ImageConversionFlags flags)
97{
99 if (image.isNull())
100 return false;
102 return !isNull();
103}
104
105bool QPlatformPixmap::fromData(const uchar *buf, uint len, const char *format, Qt::ImageConversionFlags flags)
106{
107 QByteArray a = QByteArray::fromRawData(reinterpret_cast<const char *>(buf), len);
108 QBuffer b(&a);
111 if (image.isNull())
112 return false;
114 return !isNull();
115}
116
121
122bool QPlatformPixmap::scroll(int dx, int dy, const QRect &rect)
123{
124 Q_UNUSED(dx);
125 Q_UNUSED(dy);
126 Q_UNUSED(rect);
127 return false;
128}
129
131{
132 if (!hasAlphaChannel())
133 return QBitmap();
134
135 QImage img = toImage();
136 bool shouldConvert = (img.format() != QImage::Format_ARGB32 && img.format() != QImage::Format_ARGB32_Premultiplied);
137 const QImage image = (shouldConvert ? std::move(img).convertToFormat(QImage::Format_ARGB32_Premultiplied) : img);
138 const int w = image.width();
139 const int h = image.height();
140
142 if (mask.isNull()) // allocation failed
143 return QBitmap();
144
145 mask.setDevicePixelRatio(devicePixelRatio());
146 mask.setColorCount(2);
147 mask.setColor(0, QColor(Qt::color0).rgba());
148 mask.setColor(1, QColor(Qt::color1).rgba());
149
150 const qsizetype bpl = mask.bytesPerLine();
151
152 for (int y = 0; y < h; ++y) {
153 const QRgb *src = reinterpret_cast<const QRgb*>(image.scanLine(y));
154 uchar *dest = mask.scanLine(y);
155 memset(dest, 0, bpl);
156 for (int x = 0; x < w; ++x) {
157 if (qAlpha(*src) > 0)
158 dest[x >> 3] |= (1 << (x & 7));
159 ++src;
160 }
161 }
162
163 return QBitmap::fromImage(std::move(mask));
164}
165
167{
168 QImage image = toImage();
169 if (mask.size().isEmpty()) {
170 if (image.depth() != 1) { // hw: ????
171 image = std::move(image).convertToFormat(QImage::Format_RGB32);
172 }
173 } else {
174 const int w = image.width();
175 const int h = image.height();
176
177 switch (image.depth()) {
178 case 1: {
179 const QImage imageMask = mask.toImage().convertToFormat(image.format());
180 for (int y = 0; y < h; ++y) {
181 const uchar *mscan = imageMask.scanLine(y);
182 uchar *tscan = image.scanLine(y);
183 qsizetype bytesPerLine = image.bytesPerLine();
184 for (int i = 0; i < bytesPerLine; ++i)
185 tscan[i] &= mscan[i];
186 }
187 break;
188 }
189 default: {
190 const QImage imageMask = mask.toImage().convertToFormat(QImage::Format_MonoLSB);
191 image = std::move(image).convertToFormat(QImage::Format_ARGB32_Premultiplied);
192 for (int y = 0; y < h; ++y) {
193 const uchar *mscan = imageMask.scanLine(y);
194 QRgb *tscan = (QRgb *)image.scanLine(y);
195 for (int x = 0; x < w; ++x) {
196 if (!(mscan[x>>3] & (1 << (x&7))))
197 tscan[x] = 0;
198 }
199 }
200 break;
201 }
202 }
203 }
205}
206
212
214{
215 ser_no = serNo;
216}
217
219{
220 detach_no = detNo;
221}
222
224{
225 if (rect.contains(QRect(0, 0, w, h)))
226 return toImage();
227 else
228 return toImage().copy(rect);
229}
230
232{
233 return nullptr;
234}
235
236
\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
\inmodule QtCore \reentrant
Definition qbuffer.h:16
\inmodule QtCore
Definition qbytearray.h:57
static QByteArray fromRawData(const char *data, qsizetype size)
Constructs a QByteArray that uses the first size bytes of the data array.
Definition qbytearray.h:409
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()
static void executePlatformPixmapDestructionHooks(QPlatformPixmap *)
The QImageReader class provides a format independent interface for reading images from files or other...
QImage read()
Reads an image from the device.
\inmodule QtGui
Definition qimage.h:37
uchar * scanLine(int)
Returns a pointer to the pixel data at the scanline with index i.
Definition qimage.cpp:1637
@ Format_RGB32
Definition qimage.h:46
@ Format_MonoLSB
Definition qimage.h:44
@ Format_ARGB32_Premultiplied
Definition qimage.h:48
@ Format_ARGB32
Definition qimage.h:47
QImage convertToFormat(Format f, Qt::ImageConversionFlags flags=Qt::AutoColor) const &
Definition qimage.h:125
Returns a copy of the pixmap that is transformed using the given transformation transform and transfo...
Definition qpixmap.h:27
static QPixmap fromImage(const QImage &image, Qt::ImageConversionFlags flags=Qt::AutoColor)
Converts the given image to a pixmap using the specified flags to control the conversion.
Definition qpixmap.cpp:1437
The QPlatformPixmap class provides an abstraction for native pixmaps.
virtual void setMask(const QBitmap &mask)
void setDetachNumber(int detNo)
virtual bool scroll(int dx, int dy, const QRect &rect)
virtual ~QPlatformPixmap()
virtual QBitmap mask() const
virtual void fromImageReader(QImageReader *imageReader, Qt::ImageConversionFlags flags)
virtual QImage toImage() const =0
void setSerialNumber(int serNo)
virtual bool fromFile(const QString &filename, const char *format, Qt::ImageConversionFlags flags)
virtual QPlatformPixmap * createCompatiblePlatformPixmap() const
virtual qreal devicePixelRatio() const =0
virtual bool hasAlphaChannel() const =0
QPlatformPixmap(PixelType pixelType, int classId)
static QPlatformPixmap * create(int w, int h, PixelType type)
bool isNull() const
virtual QImage * buffer()
virtual void copy(const QPlatformPixmap *data, const QRect &rect)
virtual QPixmap transformed(const QTransform &matrix, Qt::TransformationMode mode) const
virtual void fromImage(const QImage &image, Qt::ImageConversionFlags flags)=0
virtual bool fromData(const uchar *buffer, uint len, const char *format, Qt::ImageConversionFlags flags)
PixelType pixelType() const
\inmodule QtCore\reentrant
Definition qrect.h:30
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
The QTransform class specifies 2D transformations of a coordinate system.
Definition qtransform.h:20
rect
[4]
Combined button and popup list for selecting options.
@ NoOpaqueDetection
Definition qnamespace.h:500
@ AutoColor
Definition qnamespace.h:478
TransformationMode
@ color1
Definition qnamespace.h:29
@ white
Definition qnamespace.h:31
@ black
Definition qnamespace.h:30
@ color0
Definition qnamespace.h:28
Definition image.cpp:4
#define Q_UNLIKELY(x)
#define qFatal
Definition qlogging.h:168
GLboolean GLboolean GLboolean b
GLint GLint GLint GLint GLint x
[0]
GLenum mode
GLfloat GLfloat GLfloat w
[0]
GLboolean GLboolean GLboolean GLboolean a
[7]
GLenum GLuint id
[7]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum src
GLenum type
GLenum GLuint GLenum GLsizei const GLchar * buf
GLbitfield flags
GLint ref
GLint GLint GLint GLint GLint GLint GLint GLbitfield mask
GLint GLsizei GLsizei GLenum format
GLint y
GLfloat GLfloat GLfloat GLfloat h
GLint void * img
Definition qopenglext.h:233
GLuint GLenum matrix
GLenum GLsizei len
static QImage makeBitmapCompliantIfNeeded(QPlatformPixmap *d, QImage image, Qt::ImageConversionFlags flags)
QT_BEGIN_NAMESPACE typedef unsigned int QRgb
Definition qrgb.h:13
constexpr int qAlpha(QRgb rgb)
Definition qrgb.h:27
#define Q_UNUSED(x)
unsigned char uchar
Definition qtypes.h:32
ptrdiff_t qsizetype
Definition qtypes.h:165
unsigned int uint
Definition qtypes.h:34