/src/qtbase/src/gui/image/qplatformpixmap.cpp
Line | Count | Source |
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 | | |
5 | | #include "qplatformpixmap.h" |
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 | | |
13 | | QT_BEGIN_NAMESPACE |
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 | | */ |
24 | | QPlatformPixmap *QPlatformPixmap::create(int w, int h, PixelType type) |
25 | 0 | { |
26 | 0 | if (Q_UNLIKELY(!QGuiApplicationPrivate::platformIntegration())) |
27 | 0 | qFatal("QPlatformPixmap: QGuiApplication required"); |
28 | |
|
29 | 0 | QPlatformPixmap *data = QGuiApplicationPrivate::platformIntegration()->createPlatformPixmap(static_cast<QPlatformPixmap::PixelType>(type)); |
30 | 0 | data->resize(w, h); |
31 | 0 | return data; |
32 | 0 | } |
33 | | |
34 | | |
35 | | QPlatformPixmap::QPlatformPixmap(PixelType pixelType, int objectId) |
36 | 0 | : w(0), |
37 | 0 | h(0), |
38 | 0 | d(0), |
39 | 0 | is_null(true), |
40 | 0 | detach_no(0), |
41 | 0 | type(pixelType), |
42 | 0 | id(objectId), |
43 | 0 | ser_no(0), |
44 | 0 | is_cached(false) |
45 | 0 | { |
46 | 0 | } |
47 | | |
48 | | QPlatformPixmap::~QPlatformPixmap() |
49 | 0 | { |
50 | 0 | callDestructionHooks(); |
51 | 0 | } |
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. |
57 | | void QPlatformPixmap::callDestructionHooks() |
58 | 0 | { |
59 | 0 | if (is_cached) { |
60 | 0 | QImagePixmapCleanupHooks::executePlatformPixmapDestructionHooks(this); |
61 | 0 | is_cached = false; |
62 | 0 | } |
63 | 0 | } |
64 | | |
65 | | QPlatformPixmap *QPlatformPixmap::createCompatiblePlatformPixmap() const |
66 | 0 | { |
67 | 0 | QPlatformPixmap *d = QGuiApplicationPrivate::platformIntegration()->createPlatformPixmap(pixelType()); |
68 | 0 | return d; |
69 | 0 | } |
70 | | |
71 | | static QImage makeBitmapCompliantIfNeeded(QPlatformPixmap *d, QImage image, Qt::ImageConversionFlags flags) |
72 | 0 | { |
73 | 0 | if (d->pixelType() == QPlatformPixmap::BitmapType) { |
74 | 0 | 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 | 0 | const QRgb c0 = QColor(Qt::black).rgb(); |
79 | 0 | const QRgb c1 = QColor(Qt::white).rgb(); |
80 | 0 | if (img.color(0) == c0 && img.color(1) == c1) { |
81 | 0 | img.invertPixels(); |
82 | 0 | img.setColor(0, c1); |
83 | 0 | img.setColor(1, c0); |
84 | 0 | } |
85 | 0 | return img; |
86 | 0 | } |
87 | | |
88 | 0 | return image; |
89 | 0 | } |
90 | | |
91 | | void QPlatformPixmap::fromImageReader(QImageReader *imageReader, |
92 | | Qt::ImageConversionFlags flags) |
93 | 0 | { |
94 | 0 | const QImage image = imageReader->read(); |
95 | 0 | fromImage(image, flags); |
96 | 0 | } |
97 | | |
98 | | bool QPlatformPixmap::fromFile(const QString &fileName, const char *format, |
99 | | Qt::ImageConversionFlags flags) |
100 | 0 | { |
101 | 0 | QImage image = QImageReader(fileName, format).read(); |
102 | 0 | if (image.isNull()) |
103 | 0 | return false; |
104 | 0 | fromImage(makeBitmapCompliantIfNeeded(this, std::move(image), flags), flags); |
105 | 0 | return !isNull(); |
106 | 0 | } |
107 | | |
108 | | bool QPlatformPixmap::fromData(const uchar *buf, uint len, const char *format, Qt::ImageConversionFlags flags) |
109 | 0 | { |
110 | 0 | QByteArray a = QByteArray::fromRawData(reinterpret_cast<const char *>(buf), len); |
111 | 0 | QBuffer b(&a); |
112 | 0 | b.open(QIODevice::ReadOnly); |
113 | 0 | QImage image = QImageReader(&b, format).read(); |
114 | 0 | if (image.isNull()) |
115 | 0 | return false; |
116 | 0 | fromImage(makeBitmapCompliantIfNeeded(this, std::move(image), flags), flags); |
117 | 0 | return !isNull(); |
118 | 0 | } |
119 | | |
120 | | void QPlatformPixmap::copy(const QPlatformPixmap *data, const QRect &rect) |
121 | 0 | { |
122 | 0 | fromImage(data->toImage(rect), Qt::NoOpaqueDetection); |
123 | 0 | } |
124 | | |
125 | | bool QPlatformPixmap::scroll(int dx, int dy, const QRect &rect) |
126 | 0 | { |
127 | 0 | Q_UNUSED(dx); |
128 | 0 | Q_UNUSED(dy); |
129 | 0 | Q_UNUSED(rect); |
130 | 0 | return false; |
131 | 0 | } |
132 | | |
133 | | QBitmap QPlatformPixmap::mask() const |
134 | 0 | { |
135 | 0 | if (!hasAlphaChannel()) |
136 | 0 | return QBitmap(); |
137 | | |
138 | 0 | QImage img = toImage(); |
139 | 0 | bool shouldConvert = (img.format() != QImage::Format_ARGB32 && img.format() != QImage::Format_ARGB32_Premultiplied); |
140 | 0 | const QImage image = (shouldConvert ? std::move(img).convertToFormat(QImage::Format_ARGB32_Premultiplied) : img); |
141 | 0 | const int w = image.width(); |
142 | 0 | const int h = image.height(); |
143 | |
|
144 | 0 | QImage mask(w, h, QImage::Format_MonoLSB); |
145 | 0 | if (mask.isNull()) // allocation failed |
146 | 0 | return QBitmap(); |
147 | | |
148 | 0 | mask.setDevicePixelRatio(devicePixelRatio()); |
149 | 0 | mask.setColorCount(2); |
150 | 0 | mask.setColor(0, QColor(Qt::color0).rgba()); |
151 | 0 | mask.setColor(1, QColor(Qt::color1).rgba()); |
152 | |
|
153 | 0 | const qsizetype bpl = mask.bytesPerLine(); |
154 | |
|
155 | 0 | for (int y = 0; y < h; ++y) { |
156 | 0 | const QRgb *src = reinterpret_cast<const QRgb*>(image.scanLine(y)); |
157 | 0 | uchar *dest = mask.scanLine(y); |
158 | 0 | memset(dest, 0, bpl); |
159 | 0 | for (int x = 0; x < w; ++x) { |
160 | 0 | if (qAlpha(*src) > 0) |
161 | 0 | dest[x >> 3] |= (1 << (x & 7)); |
162 | 0 | ++src; |
163 | 0 | } |
164 | 0 | } |
165 | |
|
166 | 0 | return QBitmap::fromImage(std::move(mask)); |
167 | 0 | } |
168 | | |
169 | | void QPlatformPixmap::setMask(const QBitmap &mask) |
170 | 0 | { |
171 | 0 | QImage image = toImage(); |
172 | 0 | if (mask.size().isEmpty()) { |
173 | 0 | if (image.depth() != 1) { // hw: ???? |
174 | 0 | image = std::move(image).convertToFormat(QImage::Format_RGB32); |
175 | 0 | } |
176 | 0 | } else { |
177 | 0 | const int w = image.width(); |
178 | 0 | const int h = image.height(); |
179 | |
|
180 | 0 | switch (image.depth()) { |
181 | 0 | case 1: { |
182 | 0 | const QImage imageMask = mask.toImage().convertToFormat(image.format()); |
183 | 0 | for (int y = 0; y < h; ++y) { |
184 | 0 | const uchar *mscan = imageMask.scanLine(y); |
185 | 0 | uchar *tscan = image.scanLine(y); |
186 | 0 | qsizetype bytesPerLine = image.bytesPerLine(); |
187 | 0 | for (int i = 0; i < bytesPerLine; ++i) |
188 | 0 | tscan[i] &= mscan[i]; |
189 | 0 | } |
190 | 0 | break; |
191 | 0 | } |
192 | 0 | default: { |
193 | 0 | const QImage imageMask = mask.toImage().convertToFormat(QImage::Format_MonoLSB); |
194 | 0 | image = std::move(image).convertToFormat(QImage::Format_ARGB32_Premultiplied); |
195 | 0 | for (int y = 0; y < h; ++y) { |
196 | 0 | const uchar *mscan = imageMask.scanLine(y); |
197 | 0 | QRgb *tscan = (QRgb *)image.scanLine(y); |
198 | 0 | for (int x = 0; x < w; ++x) { |
199 | 0 | if (!(mscan[x>>3] & (1 << (x&7)))) |
200 | 0 | tscan[x] = 0; |
201 | 0 | } |
202 | 0 | } |
203 | 0 | break; |
204 | 0 | } |
205 | 0 | } |
206 | 0 | } |
207 | 0 | fromImage(image, Qt::AutoColor); |
208 | 0 | } |
209 | | |
210 | | QPixmap QPlatformPixmap::transformed(const QTransform &matrix, |
211 | | Qt::TransformationMode mode) const |
212 | 0 | { |
213 | 0 | return QPixmap::fromImage(toImage().transformed(matrix, mode)); |
214 | 0 | } |
215 | | |
216 | | void QPlatformPixmap::setSerialNumber(int serNo) |
217 | 0 | { |
218 | 0 | ser_no = serNo; |
219 | 0 | } |
220 | | |
221 | | void QPlatformPixmap::setDetachNumber(int detNo) |
222 | 0 | { |
223 | 0 | detach_no = detNo; |
224 | 0 | } |
225 | | |
226 | | QImage QPlatformPixmap::toImage(const QRect &rect) const |
227 | 0 | { |
228 | 0 | if (rect.contains(QRect(0, 0, w, h))) |
229 | 0 | return toImage(); |
230 | 0 | else |
231 | 0 | return toImage().copy(rect); |
232 | 0 | } |
233 | | |
234 | | QImage* QPlatformPixmap::buffer() |
235 | 0 | { |
236 | 0 | return nullptr; |
237 | 0 | } |
238 | | |
239 | | |
240 | | QT_END_NAMESPACE |