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
12QT_BEGIN_NAMESPACE
13
14/*!
15 \class QPlatformPixmap
16 \since 5.0
17 \internal
18 \preliminary
19 \ingroup qpa
20
21 \brief The QPlatformPixmap class provides an abstraction for native pixmaps.
22 */
23QPlatformPixmap *QPlatformPixmap::create(int w, int h, PixelType type)
24{
25 if (Q_UNLIKELY(!QGuiApplicationPrivate::platformIntegration()))
26 qFatal(msg: "QPlatformPixmap: QGuiApplication required");
27
28 QPlatformPixmap *data = QGuiApplicationPrivate::platformIntegration()->createPlatformPixmap(type: static_cast<QPlatformPixmap::PixelType>(type));
29 data->resize(width: w, height: h);
30 return data;
31}
32
33
34QPlatformPixmap::QPlatformPixmap(PixelType pixelType, int objectId)
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
48QPlatformPixmap::~QPlatformPixmap()
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) {
57 QImagePixmapCleanupHooks::executePlatformPixmapDestructionHooks(this);
58 is_cached = false;
59 }
60}
61
62QPlatformPixmap *QPlatformPixmap::createCompatiblePlatformPixmap() const
63{
64 QPlatformPixmap *d = QGuiApplicationPrivate::platformIntegration()->createPlatformPixmap(type: pixelType());
65 return d;
66}
67
68static QImage makeBitmapCompliantIfNeeded(QPlatformPixmap *d, const QImage &image, Qt::ImageConversionFlags flags)
69{
70 if (d->pixelType() == QPlatformPixmap::BitmapType) {
71 QImage img = image.convertToFormat(f: QImage::Format_MonoLSB, flags);
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(i: 0) == c0 && img.color(i: 1) == c1) {
78 img.invertPixels();
79 img.setColor(i: 0, c: c1);
80 img.setColor(i: 1, c: c0);
81 }
82 return img;
83 }
84
85 return image;
86}
87
88void QPlatformPixmap::fromImageReader(QImageReader *imageReader,
89 Qt::ImageConversionFlags flags)
90{
91 const QImage image = imageReader->read();
92 fromImage(image, flags);
93}
94
95bool QPlatformPixmap::fromFile(const QString &fileName, const char *format,
96 Qt::ImageConversionFlags flags)
97{
98 QImage image = QImageReader(fileName, format).read();
99 if (image.isNull())
100 return false;
101 fromImage(image: makeBitmapCompliantIfNeeded(d: this, image, flags), flags);
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(data: reinterpret_cast<const char *>(buf), size: len);
108 QBuffer b(&a);
109 b.open(openMode: QIODevice::ReadOnly);
110 QImage image = QImageReader(&b, format).read();
111 if (image.isNull())
112 return false;
113 fromImage(image: makeBitmapCompliantIfNeeded(d: this, image, flags), flags);
114 return !isNull();
115}
116
117void QPlatformPixmap::copy(const QPlatformPixmap *data, const QRect &rect)
118{
119 fromImage(image: data->toImage(rect), flags: Qt::NoOpaqueDetection);
120}
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
130QBitmap QPlatformPixmap::mask() const
131{
132 if (!hasAlphaChannel())
133 return QBitmap();
134
135 const QImage img = toImage();
136 bool shouldConvert = (img.format() != QImage::Format_ARGB32 && img.format() != QImage::Format_ARGB32_Premultiplied);
137 const QImage image = (shouldConvert ? img.convertToFormat(f: QImage::Format_ARGB32_Premultiplied) : img);
138 const int w = image.width();
139 const int h = image.height();
140
141 QImage mask(w, h, QImage::Format_MonoLSB);
142 if (mask.isNull()) // allocation failed
143 return QBitmap();
144
145 mask.setDevicePixelRatio(devicePixelRatio());
146 mask.setColorCount(2);
147 mask.setColor(i: 0, c: QColor(Qt::color0).rgba());
148 mask.setColor(i: 1, c: 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(s: dest, c: 0, n: bpl);
156 for (int x = 0; x < w; ++x) {
157 if (qAlpha(rgb: *src) > 0)
158 dest[x >> 3] |= (1 << (x & 7));
159 ++src;
160 }
161 }
162
163 return QBitmap::fromImage(image: mask);
164}
165
166void QPlatformPixmap::setMask(const QBitmap &mask)
167{
168 QImage image = toImage();
169 if (mask.size().isEmpty()) {
170 if (image.depth() != 1) { // hw: ????
171 image = image.convertToFormat(f: 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(f: 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(f: QImage::Format_MonoLSB);
191 image = image.convertToFormat(f: 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 }
204 fromImage(image, flags: Qt::AutoColor);
205}
206
207QPixmap QPlatformPixmap::transformed(const QTransform &matrix,
208 Qt::TransformationMode mode) const
209{
210 return QPixmap::fromImage(image: toImage().transformed(matrix, mode));
211}
212
213void QPlatformPixmap::setSerialNumber(int serNo)
214{
215 ser_no = serNo;
216}
217
218void QPlatformPixmap::setDetachNumber(int detNo)
219{
220 detach_no = detNo;
221}
222
223QImage QPlatformPixmap::toImage(const QRect &rect) const
224{
225 if (rect.contains(r: QRect(0, 0, w, h)))
226 return toImage();
227 else
228 return toImage().copy(rect);
229}
230
231QImage* QPlatformPixmap::buffer()
232{
233 return nullptr;
234}
235
236
237QT_END_NAMESPACE
238

source code of qtbase/src/gui/image/qplatformpixmap.cpp