1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qplatformpixmap.h"
41#include <qpa/qplatformintegration.h>
42#include <QtCore/qbuffer.h>
43#include <QtGui/qbitmap.h>
44#include <QtGui/qimagereader.h>
45#include <private/qguiapplication_p.h>
46#include <private/qimagepixmapcleanuphooks_p.h>
47
48QT_BEGIN_NAMESPACE
49
50/*!
51 \class QPlatformPixmap
52 \since 5.0
53 \internal
54 \preliminary
55 \ingroup qpa
56
57 \brief The QPlatformPixmap class provides an abstraction for native pixmaps.
58 */
59QPlatformPixmap *QPlatformPixmap::create(int w, int h, PixelType type)
60{
61 if (Q_UNLIKELY(!QGuiApplicationPrivate::platformIntegration()))
62 qFatal(msg: "QPlatformPixmap: QGuiApplication required");
63
64 QPlatformPixmap *data = QGuiApplicationPrivate::platformIntegration()->createPlatformPixmap(type: static_cast<QPlatformPixmap::PixelType>(type));
65 data->resize(width: w, height: h);
66 return data;
67}
68
69
70QPlatformPixmap::QPlatformPixmap(PixelType pixelType, int objectId)
71 : w(0),
72 h(0),
73 d(0),
74 is_null(true),
75 ref(0),
76 detach_no(0),
77 type(pixelType),
78 id(objectId),
79 ser_no(0),
80 is_cached(false)
81{
82}
83
84QPlatformPixmap::~QPlatformPixmap()
85{
86 // Sometimes the pixmap cleanup hooks will be called from derrived classes, which will
87 // then set is_cached to false. For example, on X11 Qt GUI needs to delete the GLXPixmap
88 // or EGL Pixmap Surface for a given pixmap _before_ the native X11 pixmap is deleted,
89 // otherwise some drivers will leak the GL surface. In this case, QX11PlatformPixmap will
90 // call the cleanup hooks itself before deleting the native pixmap and set is_cached to
91 // false.
92 if (is_cached) {
93 QImagePixmapCleanupHooks::executePlatformPixmapDestructionHooks(this);
94 is_cached = false;
95 }
96}
97
98QPlatformPixmap *QPlatformPixmap::createCompatiblePlatformPixmap() const
99{
100 QPlatformPixmap *d = QGuiApplicationPrivate::platformIntegration()->createPlatformPixmap(type: pixelType());
101 return d;
102}
103
104static QImage makeBitmapCompliantIfNeeded(QPlatformPixmap *d, const QImage &image, Qt::ImageConversionFlags flags)
105{
106 if (d->pixelType() == QPlatformPixmap::BitmapType) {
107 QImage img = image.convertToFormat(f: QImage::Format_MonoLSB, flags);
108
109 // make sure image.color(0) == Qt::color0 (white)
110 // and image.color(1) == Qt::color1 (black)
111 const QRgb c0 = QColor(Qt::black).rgb();
112 const QRgb c1 = QColor(Qt::white).rgb();
113 if (img.color(i: 0) == c0 && img.color(i: 1) == c1) {
114 img.invertPixels();
115 img.setColor(i: 0, c: c1);
116 img.setColor(i: 1, c: c0);
117 }
118 return img;
119 }
120
121 return image;
122}
123
124void QPlatformPixmap::fromImageReader(QImageReader *imageReader,
125 Qt::ImageConversionFlags flags)
126{
127 const QImage image = imageReader->read();
128 fromImage(image, flags);
129}
130
131bool QPlatformPixmap::fromFile(const QString &fileName, const char *format,
132 Qt::ImageConversionFlags flags)
133{
134 QImage image = QImageReader(fileName, format).read();
135 if (image.isNull())
136 return false;
137 fromImage(image: makeBitmapCompliantIfNeeded(d: this, image, flags), flags);
138 return !isNull();
139}
140
141bool QPlatformPixmap::fromData(const uchar *buf, uint len, const char *format, Qt::ImageConversionFlags flags)
142{
143 QByteArray a = QByteArray::fromRawData(reinterpret_cast<const char *>(buf), size: len);
144 QBuffer b(&a);
145 b.open(openMode: QIODevice::ReadOnly);
146 QImage image = QImageReader(&b, format).read();
147 if (image.isNull())
148 return false;
149 fromImage(image: makeBitmapCompliantIfNeeded(d: this, image, flags), flags);
150 return !isNull();
151}
152
153void QPlatformPixmap::copy(const QPlatformPixmap *data, const QRect &rect)
154{
155 fromImage(image: data->toImage(rect), flags: Qt::NoOpaqueDetection);
156}
157
158bool QPlatformPixmap::scroll(int dx, int dy, const QRect &rect)
159{
160 Q_UNUSED(dx);
161 Q_UNUSED(dy);
162 Q_UNUSED(rect);
163 return false;
164}
165
166QBitmap QPlatformPixmap::mask() const
167{
168 if (!hasAlphaChannel())
169 return QBitmap();
170
171 const QImage img = toImage();
172 bool shouldConvert = (img.format() != QImage::Format_ARGB32 && img.format() != QImage::Format_ARGB32_Premultiplied);
173 const QImage image = (shouldConvert ? img.convertToFormat(f: QImage::Format_ARGB32_Premultiplied) : img);
174 const int w = image.width();
175 const int h = image.height();
176
177 QImage mask(w, h, QImage::Format_MonoLSB);
178 if (mask.isNull()) // allocation failed
179 return QBitmap();
180
181 mask.setDevicePixelRatio(devicePixelRatio());
182 mask.setColorCount(2);
183 mask.setColor(i: 0, c: QColor(Qt::color0).rgba());
184 mask.setColor(i: 1, c: QColor(Qt::color1).rgba());
185
186 const int bpl = mask.bytesPerLine();
187
188 for (int y = 0; y < h; ++y) {
189 const QRgb *src = reinterpret_cast<const QRgb*>(image.scanLine(y));
190 uchar *dest = mask.scanLine(y);
191 memset(s: dest, c: 0, n: bpl);
192 for (int x = 0; x < w; ++x) {
193 if (qAlpha(rgb: *src) > 0)
194 dest[x >> 3] |= (1 << (x & 7));
195 ++src;
196 }
197 }
198
199 return QBitmap::fromImage(image: mask);
200}
201
202void QPlatformPixmap::setMask(const QBitmap &mask)
203{
204 QImage image = toImage();
205 if (mask.size().isEmpty()) {
206 if (image.depth() != 1) { // hw: ????
207 image = image.convertToFormat(f: QImage::Format_RGB32);
208 }
209 } else {
210 const int w = image.width();
211 const int h = image.height();
212
213 switch (image.depth()) {
214 case 1: {
215 const QImage imageMask = mask.toImage().convertToFormat(f: image.format());
216 for (int y = 0; y < h; ++y) {
217 const uchar *mscan = imageMask.scanLine(y);
218 uchar *tscan = image.scanLine(y);
219 int bytesPerLine = image.bytesPerLine();
220 for (int i = 0; i < bytesPerLine; ++i)
221 tscan[i] &= mscan[i];
222 }
223 break;
224 }
225 default: {
226 const QImage imageMask = mask.toImage().convertToFormat(f: QImage::Format_MonoLSB);
227 image = image.convertToFormat(f: QImage::Format_ARGB32_Premultiplied);
228 for (int y = 0; y < h; ++y) {
229 const uchar *mscan = imageMask.scanLine(y);
230 QRgb *tscan = (QRgb *)image.scanLine(y);
231 for (int x = 0; x < w; ++x) {
232 if (!(mscan[x>>3] & (1 << (x&7))))
233 tscan[x] = 0;
234 }
235 }
236 break;
237 }
238 }
239 }
240 fromImage(image, flags: Qt::AutoColor);
241}
242
243QPixmap QPlatformPixmap::transformed(const QTransform &matrix,
244 Qt::TransformationMode mode) const
245{
246 return QPixmap::fromImage(image: toImage().transformed(matrix, mode));
247}
248
249void QPlatformPixmap::setSerialNumber(int serNo)
250{
251 ser_no = serNo;
252}
253
254void QPlatformPixmap::setDetachNumber(int detNo)
255{
256 detach_no = detNo;
257}
258
259QImage QPlatformPixmap::toImage(const QRect &rect) const
260{
261 if (rect.contains(r: QRect(0, 0, w, h)))
262 return toImage();
263 else
264 return toImage().copy(rect);
265}
266
267QImage* QPlatformPixmap::buffer()
268{
269 return nullptr;
270}
271
272
273QT_END_NAMESPACE
274

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