25QPixmap qt_toRasterPixmap(
const QImage &image)
27 QPlatformPixmap *data =
28 new QRasterPlatformPixmap(image.depth() == 1
29 ? QPlatformPixmap::BitmapType
30 : QPlatformPixmap::PixmapType);
32 data->fromImage(image, Qt::AutoColor);
69void QRasterPlatformPixmap::resize(
int width,
int height)
71 QImage::Format format;
72 if (pixelType() == BitmapType)
73 format = QImage::Format_MonoLSB;
75 format = systemNativeFormat();
77 image = QImage(width, height, format);
81 is_null = (w <= 0 || h <= 0);
83 if (pixelType() == BitmapType && !image.isNull()) {
84 image.setColorCount(2);
85 image.setColor(0, QColor(Qt::color0).rgba());
86 image.setColor(1, QColor(Qt::color1).rgba());
89 setSerialNumber(image.cacheKey() >> 32);
92bool QRasterPlatformPixmap::fromData(
const uchar *buffer, uint len,
const char *format,
93 Qt::ImageConversionFlags flags)
95 QByteArray a = QByteArray::fromRawData(
reinterpret_cast<
const char *>(buffer), len);
97 b.open(QIODevice::ReadOnly);
98 QImage image = QImageReader(&b, format).read();
102 createPixmapForImage(std::move(image), flags);
106void QRasterPlatformPixmap::fromImage(
const QImage &sourceImage,
107 Qt::ImageConversionFlags flags)
109 QImage image = sourceImage;
110 createPixmapForImage(std::move(image), flags);
119void QRasterPlatformPixmap::fromImageReader(QImageReader *imageReader,
120 Qt::ImageConversionFlags flags)
123 QImage image = imageReader->read();
127 createPixmapForImage(std::move(image), flags);
145void QRasterPlatformPixmap::fill(
const QColor &color)
149 if (image.depth() == 1) {
150 int gray = qGray(color.rgba());
152 if (qAbs(qGray(image.color(0)) - gray) < qAbs(qGray(image.color(1)) - gray))
156 }
else if (image.depth() >= 15) {
157 int alpha = color.alpha();
159 if (!image.hasAlphaChannel()) {
160 QImage::Format toFormat = qt_alphaVersionForPainting(image.format());
161 if (!image.reinterpretAsFormat(toFormat))
162 image = QImage(image.width(), image.height(), toFormat);
167 }
else if (image.format() == QImage::Format_Alpha8) {
168 pixel = qAlpha(color.rgba());
169 }
else if (image.format() == QImage::Format_Grayscale8) {
170 pixel = qGray(color.rgba());
171 }
else if (image.format() == QImage::Format_Grayscale16) {
172 QRgba64 c = color.rgba64();
173 pixel = qGray(c.red(), c.green(), c.blue());
202QImage QRasterPlatformPixmap::toImage(
const QRect &rect)
const
207 QRect clipped = rect.intersected(QRect(0, 0, w, h));
208 const uint du = uint(d);
209 if ((du % 8 == 0) && (((uint(clipped.x()) * du)) % 32 == 0)) {
210 QImage newImage(image.scanLine(clipped.y()) + clipped.x() * (du / 8),
211 clipped.width(), clipped.height(),
212 image.bytesPerLine(), image.format());
213 newImage.setDevicePixelRatio(image.devicePixelRatio());
216 return image.copy(clipped);
225int QRasterPlatformPixmap::metric(QPaintDevice::PaintDeviceMetric metric)
const
227 QImageData *d = image.d;
233 case QPaintDevice::PdmWidth:
235 case QPaintDevice::PdmHeight:
237 case QPaintDevice::PdmWidthMM:
238 return qRound(d->width * 25.4 / qt_defaultDpiX());
239 case QPaintDevice::PdmHeightMM:
240 return qRound(d->height * 25.4 / qt_defaultDpiY());
241 case QPaintDevice::PdmNumColors:
242 return d->colortable.size();
243 case QPaintDevice::PdmDepth:
245 case QPaintDevice::PdmDpiX:
246 return qt_defaultDpiX();
247 case QPaintDevice::PdmPhysicalDpiX:
248 return qt_defaultDpiX();
249 case QPaintDevice::PdmDpiY:
250 return qt_defaultDpiY();
251 case QPaintDevice::PdmPhysicalDpiY:
252 return qt_defaultDpiY();
253 case QPaintDevice::PdmDevicePixelRatio:
254 return image.devicePixelRatio();
255 case QPaintDevice::PdmDevicePixelRatioScaled:
256 return image.devicePixelRatio() * QPaintDevice::devicePixelRatioFScale();
257 case QPaintDevice::PdmDevicePixelRatioF_EncodedA:
259 case QPaintDevice::PdmDevicePixelRatioF_EncodedB:
260 return QPaintDevice::encodeMetricF(metric, image.devicePixelRatio());
263 qWarning(
"QRasterPlatformPixmap::metric(): Unhandled metric type %d", metric);
270void QRasterPlatformPixmap::createPixmapForImage(QImage sourceImage, Qt::ImageConversionFlags flags)
272 QImage::Format format;
273 if (flags & Qt::NoFormatConversion)
274 format = sourceImage.format();
276 if (pixelType() == BitmapType) {
277 format = QImage::Format_MonoLSB;
279 if (sourceImage.depth() == 1) {
280 format = sourceImage.hasAlphaChannel()
281 ? QImage::Format_ARGB32_Premultiplied
282 : QImage::Format_RGB32;
284 QImage::Format nativeFormat = systemNativeFormat();
285 QImage::Format opaqueFormat = qt_opaqueVersionForPainting(nativeFormat);
286 QImage::Format alphaFormat = qt_alphaVersionForPainting(nativeFormat);
288 if (!sourceImage.hasAlphaChannel()) {
289 format = opaqueFormat;
290 }
else if ((flags & Qt::NoOpaqueDetection) == 0
291 && !sourceImage.data_ptr()->checkForAlphaPixels())
293 format = opaqueFormat;
295 format = alphaFormat;
302 if (format == QImage::Format_RGB32 && (sourceImage.format() == QImage::Format_ARGB32
303 || sourceImage.format() == QImage::Format_ARGB32_Premultiplied))
305 image = std::move(sourceImage);
306 image.reinterpretAsFormat(QImage::Format_RGB32);
308 image = std::move(sourceImage).convertToFormat(format, flags);
318 is_null = (w <= 0 || h <= 0);
321 setSerialNumber(image.cacheKey() >> 32);
323 setDetachNumber(image.d->detach_no);