Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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// Qt-Security score:significant reason:default
4
6#include <qpa/qplatformintegration.h>
7#include <QtCore/qbuffer.h>
8#include <QtGui/qbitmap.h>
9#include <QtGui/qimagereader.h>
10#include <private/qguiapplication_p.h>
11#include <private/qimagepixmapcleanuphooks_p.h>
12
14
15/*!
16 \class QPlatformPixmap
17 \since 5.0
18 \internal
19 \preliminary
20 \ingroup qpa
21
22 \brief The QPlatformPixmap class provides an abstraction for native pixmaps.
23 */
24QPlatformPixmap *QPlatformPixmap::create(int w, int h, PixelType type)
25{
26 if (Q_UNLIKELY(!QGuiApplicationPrivate::platformIntegration()))
27 qFatal("QPlatformPixmap: QGuiApplication required");
28
29 QPlatformPixmap *data = QGuiApplicationPrivate::platformIntegration()->createPlatformPixmap(static_cast<QPlatformPixmap::PixelType>(type));
30 data->resize(w, h);
31 return data;
32}
33
34
35QPlatformPixmap::QPlatformPixmap(PixelType pixelType, int objectId)
36 : w(0),
37 h(0),
38 d(0),
39 is_null(true),
40 detach_no(0),
41 type(pixelType),
42 id(objectId),
43 ser_no(0),
44 is_cached(false)
45{
46}
47
48QPlatformPixmap::~QPlatformPixmap()
49{
50 callDestructionHooks();
51}
52
53// Sometimes derived classes need the cache cleanup / destruction hooks to be executed _before_
54// deleting their native pixmap handles. Such classes can call this function from their own
55// destructor. The is_cached guard ensures the hooks are only executed once even though both the
56// derived and base destructors call this function.
57void QPlatformPixmap::callDestructionHooks()
58{
59 if (is_cached) {
60 QImagePixmapCleanupHooks::executePlatformPixmapDestructionHooks(this);
61 is_cached = false;
62 }
63}
64
65QPlatformPixmap *QPlatformPixmap::createCompatiblePlatformPixmap() const
66{
67 QPlatformPixmap *d = QGuiApplicationPrivate::platformIntegration()->createPlatformPixmap(pixelType());
68 return d;
69}
70
71static QImage makeBitmapCompliantIfNeeded(QPlatformPixmap *d, QImage image, Qt::ImageConversionFlags flags)
72{
73 if (d->pixelType() == QPlatformPixmap::BitmapType) {
74 QImage img = std::move(image).convertToFormat(QImage::Format_MonoLSB, flags);
75
76 // make sure image.color(0) == Qt::color0 (white)
77 // and image.color(1) == Qt::color1 (black)
78 const QRgb c0 = QColor(Qt::black).rgb();
79 const QRgb c1 = QColor(Qt::white).rgb();
80 if (img.color(0) == c0 && img.color(1) == c1) {
81 img.invertPixels();
82 img.setColor(0, c1);
83 img.setColor(1, c0);
84 }
85 return img;
86 }
87
88 return image;
89}
90
91void QPlatformPixmap::fromImageReader(QImageReader *imageReader,
92 Qt::ImageConversionFlags flags)
93{
94 const QImage image = imageReader->read();
95 fromImage(image, flags);
96}
97
98bool QPlatformPixmap::fromFile(const QString &fileName, const char *format,
99 Qt::ImageConversionFlags flags)
100{
101 QImage image = QImageReader(fileName, format).read();
102 if (image.isNull())
103 return false;
104 fromImage(makeBitmapCompliantIfNeeded(this, std::move(image), flags), flags);
105 return !isNull();
106}
107
108bool QPlatformPixmap::fromData(const uchar *buf, uint len, const char *format, Qt::ImageConversionFlags flags)
109{
110 QByteArray a = QByteArray::fromRawData(reinterpret_cast<const char *>(buf), len);
111 QBuffer b(&a);
112 b.open(QIODevice::ReadOnly);
113 QImage image = QImageReader(&b, format).read();
114 if (image.isNull())
115 return false;
116 fromImage(makeBitmapCompliantIfNeeded(this, std::move(image), flags), flags);
117 return !isNull();
118}
119
120void QPlatformPixmap::copy(const QPlatformPixmap *data, const QRect &rect)
121{
122 fromImage(data->toImage(rect), Qt::NoOpaqueDetection);
123}
124
125bool QPlatformPixmap::scroll(int dx, int dy, const QRect &rect)
126{
127 Q_UNUSED(dx);
128 Q_UNUSED(dy);
129 Q_UNUSED(rect);
130 return false;
131}
132
133QBitmap QPlatformPixmap::mask() const
134{
135 if (!hasAlphaChannel())
136 return QBitmap();
137
138 QImage img = toImage();
139 bool shouldConvert = (img.format() != QImage::Format_ARGB32 && img.format() != QImage::Format_ARGB32_Premultiplied);
140 const QImage image = (shouldConvert ? std::move(img).convertToFormat(QImage::Format_ARGB32_Premultiplied) : img);
141 const int w = image.width();
142 const int h = image.height();
143
144 QImage mask(w, h, QImage::Format_MonoLSB);
145 if (mask.isNull()) // allocation failed
146 return QBitmap();
147
148 mask.setDevicePixelRatio(devicePixelRatio());
149 mask.setColorCount(2);
150 mask.setColor(0, QColor(Qt::color0).rgba());
151 mask.setColor(1, QColor(Qt::color1).rgba());
152
153 const qsizetype bpl = mask.bytesPerLine();
154
155 for (int y = 0; y < h; ++y) {
156 const QRgb *src = reinterpret_cast<const QRgb*>(image.scanLine(y));
157 uchar *dest = mask.scanLine(y);
158 memset(dest, 0, bpl);
159 for (int x = 0; x < w; ++x) {
160 if (qAlpha(*src) > 0)
161 dest[x >> 3] |= (1 << (x & 7));
162 ++src;
163 }
164 }
165
166 return QBitmap::fromImage(std::move(mask));
167}
168
169void QPlatformPixmap::setMask(const QBitmap &mask)
170{
171 QImage image = toImage();
172 if (mask.size().isEmpty()) {
173 if (image.depth() != 1) { // hw: ????
174 image = std::move(image).convertToFormat(QImage::Format_RGB32);
175 }
176 } else {
177 const int w = image.width();
178 const int h = image.height();
179
180 switch (image.depth()) {
181 case 1: {
182 const QImage imageMask = mask.toImage().convertToFormat(image.format());
183 for (int y = 0; y < h; ++y) {
184 const uchar *mscan = imageMask.scanLine(y);
185 uchar *tscan = image.scanLine(y);
186 qsizetype bytesPerLine = image.bytesPerLine();
187 for (int i = 0; i < bytesPerLine; ++i)
188 tscan[i] &= mscan[i];
189 }
190 break;
191 }
192 default: {
193 const QImage imageMask = mask.toImage().convertToFormat(QImage::Format_MonoLSB);
194 image = std::move(image).convertToFormat(QImage::Format_ARGB32_Premultiplied);
195 for (int y = 0; y < h; ++y) {
196 const uchar *mscan = imageMask.scanLine(y);
197 QRgb *tscan = (QRgb *)image.scanLine(y);
198 for (int x = 0; x < w; ++x) {
199 if (!(mscan[x>>3] & (1 << (x&7))))
200 tscan[x] = 0;
201 }
202 }
203 break;
204 }
205 }
206 }
207 fromImage(image, Qt::AutoColor);
208}
209
210QPixmap QPlatformPixmap::transformed(const QTransform &matrix,
211 Qt::TransformationMode mode) const
212{
213 return QPixmap::fromImage(toImage().transformed(matrix, mode));
214}
215
216void QPlatformPixmap::setSerialNumber(int serNo)
217{
218 ser_no = serNo;
219}
220
221void QPlatformPixmap::setDetachNumber(int detNo)
222{
223 detach_no = detNo;
224}
225
226QImage QPlatformPixmap::toImage(const QRect &rect) const
227{
228 if (rect.contains(QRect(0, 0, w, h)))
229 return toImage();
230 else
231 return toImage().copy(rect);
232}
233
234QImage* QPlatformPixmap::buffer()
235{
236 return nullptr;
237}
238
239
240QT_END_NAMESPACE
Combined button and popup list for selecting options.
static QImage makeBitmapCompliantIfNeeded(QPlatformPixmap *d, QImage image, Qt::ImageConversionFlags flags)