24QPlatformPixmap *QPlatformPixmap::create(
int w,
int h, PixelType type)
26 if (Q_UNLIKELY(!QGuiApplicationPrivate::platformIntegration()))
27 qFatal(
"QPlatformPixmap: QGuiApplication required");
29 QPlatformPixmap *data = QGuiApplicationPrivate::platformIntegration()->createPlatformPixmap(
static_cast<QPlatformPixmap::PixelType>(type));
73 if (d->pixelType() == QPlatformPixmap::BitmapType) {
74 QImage img = std::move(image).convertToFormat(QImage::Format_MonoLSB, flags);
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) {
98bool QPlatformPixmap::fromFile(
const QString &fileName,
const char *format,
99 Qt::ImageConversionFlags flags)
101 QImage image = QImageReader(fileName, format).read();
104 fromImage(makeBitmapCompliantIfNeeded(
this, std::move(image), flags), flags);
108bool QPlatformPixmap::fromData(
const uchar *buf, uint len,
const char *format, Qt::ImageConversionFlags flags)
110 QByteArray a = QByteArray::fromRawData(
reinterpret_cast<
const char *>(buf), len);
112 b.open(QIODevice::ReadOnly);
113 QImage image = QImageReader(&b, format).read();
116 fromImage(makeBitmapCompliantIfNeeded(
this, std::move(image), flags), flags);
133QBitmap QPlatformPixmap::mask()
const
135 if (!hasAlphaChannel())
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();
144 QImage mask(w, h, QImage::Format_MonoLSB);
148 mask.setDevicePixelRatio(devicePixelRatio());
149 mask.setColorCount(2);
150 mask.setColor(0, QColor(Qt::color0).rgba());
151 mask.setColor(1, QColor(Qt::color1).rgba());
153 const qsizetype bpl = mask.bytesPerLine();
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));
166 return QBitmap::fromImage(std::move(mask));
169void QPlatformPixmap::setMask(
const QBitmap &mask)
171 QImage image = toImage();
172 if (mask.size().isEmpty()) {
173 if (image.depth() != 1) {
174 image = std::move(image).convertToFormat(QImage::Format_RGB32);
177 const int w = image.width();
178 const int h = image.height();
180 switch (image.depth()) {
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];
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))))
207 fromImage(image, Qt::AutoColor);