23QPlatformPixmap *QPlatformPixmap::create(
int w,
int h, PixelType type)
25 if (Q_UNLIKELY(!QGuiApplicationPrivate::platformIntegration()))
26 qFatal(
"QPlatformPixmap: QGuiApplication required");
28 QPlatformPixmap *data = QGuiApplicationPrivate::platformIntegration()->createPlatformPixmap(
static_cast<QPlatformPixmap::PixelType>(type));
69 if (d->pixelType() == QPlatformPixmap::BitmapType) {
70 QImage img = std::move(image).convertToFormat(QImage::Format_MonoLSB, flags);
74 const QRgb c0 = QColor(Qt::black).rgb();
75 const QRgb c1 = QColor(Qt::white).rgb();
76 if (img.color(0) == c0 && img.color(1) == c1) {
94bool QPlatformPixmap::fromFile(
const QString &fileName,
const char *format,
95 Qt::ImageConversionFlags flags)
97 QImage image = QImageReader(fileName, format).read();
100 fromImage(makeBitmapCompliantIfNeeded(
this, std::move(image), flags), flags);
104bool QPlatformPixmap::fromData(
const uchar *buf, uint len,
const char *format, Qt::ImageConversionFlags flags)
106 QByteArray a = QByteArray::fromRawData(
reinterpret_cast<
const char *>(buf), len);
108 b.open(QIODevice::ReadOnly);
109 QImage image = QImageReader(&b, format).read();
112 fromImage(makeBitmapCompliantIfNeeded(
this, std::move(image), flags), flags);
129QBitmap QPlatformPixmap::mask()
const
131 if (!hasAlphaChannel())
134 QImage img = toImage();
135 bool shouldConvert = (img.format() != QImage::Format_ARGB32 && img.format() != QImage::Format_ARGB32_Premultiplied);
136 const QImage image = (shouldConvert ? std::move(img).convertToFormat(QImage::Format_ARGB32_Premultiplied) : img);
137 const int w = image.width();
138 const int h = image.height();
140 QImage mask(w, h, QImage::Format_MonoLSB);
144 mask.setDevicePixelRatio(devicePixelRatio());
145 mask.setColorCount(2);
146 mask.setColor(0, QColor(Qt::color0).rgba());
147 mask.setColor(1, QColor(Qt::color1).rgba());
149 const qsizetype bpl = mask.bytesPerLine();
151 for (
int y = 0; y < h; ++y) {
152 const QRgb *src =
reinterpret_cast<
const QRgb*>(image.scanLine(y));
153 uchar *dest = mask.scanLine(y);
154 memset(dest, 0, bpl);
155 for (
int x = 0; x < w; ++x) {
156 if (qAlpha(*src) > 0)
157 dest[x >> 3] |= (1 << (x & 7));
162 return QBitmap::fromImage(std::move(mask));
165void QPlatformPixmap::setMask(
const QBitmap &mask)
167 QImage image = toImage();
168 if (mask.size().isEmpty()) {
169 if (image.depth() != 1) {
170 image = std::move(image).convertToFormat(QImage::Format_RGB32);
173 const int w = image.width();
174 const int h = image.height();
176 switch (image.depth()) {
178 const QImage imageMask = mask.toImage().convertToFormat(image.format());
179 for (
int y = 0; y < h; ++y) {
180 const uchar *mscan = imageMask.scanLine(y);
181 uchar *tscan = image.scanLine(y);
182 qsizetype bytesPerLine = image.bytesPerLine();
183 for (
int i = 0; i < bytesPerLine; ++i)
184 tscan[i] &= mscan[i];
189 const QImage imageMask = mask.toImage().convertToFormat(QImage::Format_MonoLSB);
190 image = std::move(image).convertToFormat(QImage::Format_ARGB32_Premultiplied);
191 for (
int y = 0; y < h; ++y) {
192 const uchar *mscan = imageMask.scanLine(y);
193 QRgb *tscan = (QRgb *)image.scanLine(y);
194 for (
int x = 0; x < w; ++x) {
195 if (!(mscan[x>>3] & (1 << (x&7))))
203 fromImage(image, Qt::AutoColor);