24QPixmap qt_toRasterPixmap(
const QImage &image)
26 QPlatformPixmap *data =
27 new QRasterPlatformPixmap(image.depth() == 1
28 ? QPlatformPixmap::BitmapType
29 : QPlatformPixmap::PixmapType);
31 data->fromImage(image, Qt::AutoColor);
68void QRasterPlatformPixmap::resize(
int width,
int height)
70 QImage::Format format;
71 if (pixelType() == BitmapType)
72 format = QImage::Format_MonoLSB;
74 format = systemNativeFormat();
76 image = QImage(width, height, format);
80 is_null = (w <= 0 || h <= 0);
82 if (pixelType() == BitmapType && !image.isNull()) {
83 image.setColorCount(2);
84 image.setColor(0, QColor(Qt::color0).rgba());
85 image.setColor(1, QColor(Qt::color1).rgba());
88 setSerialNumber(image.cacheKey() >> 32);
91bool QRasterPlatformPixmap::fromData(
const uchar *buffer, uint len,
const char *format,
92 Qt::ImageConversionFlags flags)
94 QByteArray a = QByteArray::fromRawData(
reinterpret_cast<
const char *>(buffer), len);
96 b.open(QIODevice::ReadOnly);
97 QImage image = QImageReader(&b, format).read();
101 createPixmapForImage(std::move(image), flags);
105void QRasterPlatformPixmap::fromImage(
const QImage &sourceImage,
106 Qt::ImageConversionFlags flags)
108 QImage image = sourceImage;
109 createPixmapForImage(std::move(image), flags);
118void QRasterPlatformPixmap::fromImageReader(QImageReader *imageReader,
119 Qt::ImageConversionFlags flags)
122 QImage image = imageReader->read();
126 createPixmapForImage(std::move(image), flags);
144void QRasterPlatformPixmap::fill(
const QColor &color)
148 if (image.depth() == 1) {
149 int gray = qGray(color.rgba());
151 if (qAbs(qGray(image.color(0)) - gray) < qAbs(qGray(image.color(1)) - gray))
155 }
else if (image.depth() >= 15) {
156 int alpha = color.alpha();
158 if (!image.hasAlphaChannel()) {
159 QImage::Format toFormat = qt_alphaVersionForPainting(image.format());
160 if (!image.reinterpretAsFormat(toFormat))
161 image = QImage(image.width(), image.height(), toFormat);
166 }
else if (image.format() == QImage::Format_Alpha8) {
167 pixel = qAlpha(color.rgba());
168 }
else if (image.format() == QImage::Format_Grayscale8) {
169 pixel = qGray(color.rgba());
170 }
else if (image.format() == QImage::Format_Grayscale16) {
171 QRgba64 c = color.rgba64();
172 pixel = qGray(c.red(), c.green(), c.blue());
201QImage QRasterPlatformPixmap::toImage(
const QRect &rect)
const
206 QRect clipped = rect.intersected(QRect(0, 0, w, h));
207 const uint du = uint(d);
208 if ((du % 8 == 0) && (((uint(clipped.x()) * du)) % 32 == 0)) {
209 QImage newImage(image.scanLine(clipped.y()) + clipped.x() * (du / 8),
210 clipped.width(), clipped.height(),
211 image.bytesPerLine(), image.format());
212 newImage.setDevicePixelRatio(image.devicePixelRatio());
215 return image.copy(clipped);
224int QRasterPlatformPixmap::metric(QPaintDevice::PaintDeviceMetric metric)
const
226 QImageData *d = image.d;
232 case QPaintDevice::PdmWidth:
234 case QPaintDevice::PdmHeight:
236 case QPaintDevice::PdmWidthMM:
237 return qRound(d->width * 25.4 / qt_defaultDpiX());
238 case QPaintDevice::PdmHeightMM:
239 return qRound(d->height * 25.4 / qt_defaultDpiY());
240 case QPaintDevice::PdmNumColors:
241 return d->colortable.size();
242 case QPaintDevice::PdmDepth:
244 case QPaintDevice::PdmDpiX:
245 return qt_defaultDpiX();
246 case QPaintDevice::PdmPhysicalDpiX:
247 return qt_defaultDpiX();
248 case QPaintDevice::PdmDpiY:
249 return qt_defaultDpiY();
250 case QPaintDevice::PdmPhysicalDpiY:
251 return qt_defaultDpiY();
252 case QPaintDevice::PdmDevicePixelRatio:
253 return image.devicePixelRatio();
254 case QPaintDevice::PdmDevicePixelRatioScaled:
255 return image.devicePixelRatio() * QPaintDevice::devicePixelRatioFScale();
256 case QPaintDevice::PdmDevicePixelRatioF_EncodedA:
258 case QPaintDevice::PdmDevicePixelRatioF_EncodedB:
259 return QPaintDevice::encodeMetricF(metric, image.devicePixelRatio());
262 qWarning(
"QRasterPlatformPixmap::metric(): Unhandled metric type %d", metric);
269void QRasterPlatformPixmap::createPixmapForImage(QImage sourceImage, Qt::ImageConversionFlags flags)
271 QImage::Format format;
272 if (flags & Qt::NoFormatConversion)
273 format = sourceImage.format();
275 if (pixelType() == BitmapType) {
276 format = QImage::Format_MonoLSB;
278 if (sourceImage.depth() == 1) {
279 format = sourceImage.hasAlphaChannel()
280 ? QImage::Format_ARGB32_Premultiplied
281 : QImage::Format_RGB32;
283 QImage::Format nativeFormat = systemNativeFormat();
284 QImage::Format opaqueFormat = qt_opaqueVersionForPainting(nativeFormat);
285 QImage::Format alphaFormat = qt_alphaVersionForPainting(nativeFormat);
287 if (!sourceImage.hasAlphaChannel()) {
288 format = opaqueFormat;
289 }
else if ((flags & Qt::NoOpaqueDetection) == 0
290 && !sourceImage.data_ptr()->checkForAlphaPixels())
292 format = opaqueFormat;
294 format = alphaFormat;
301 if (format == QImage::Format_RGB32 && (sourceImage.format() == QImage::Format_ARGB32
302 || sourceImage.format() == QImage::Format_ARGB32_Premultiplied))
304 image = std::move(sourceImage);
305 image.reinterpretAsFormat(QImage::Format_RGB32);
307 image = std::move(sourceImage).convertToFormat(format, flags);
317 is_null = (w <= 0 || h <= 0);
320 setSerialNumber(image.cacheKey() >> 32);
322 setDetachNumber(image.d->detach_no);