Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qpixmap_raster.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qpixmap.h"
5
6#include <private/qfont_p.h>
7
8#include "qpixmap_raster_p.h"
9#include "qimage_p.h"
10#include "qpaintengine.h"
11
12#include "qbitmap.h"
13#include "qimage.h"
14#include <QBuffer>
15#include <QImageReader>
16#include <QGuiApplication>
17#include <QScreen>
18#include <private/qsimd_p.h>
19#include <private/qdrawhelper_p.h>
20#include <qpa/qplatformscreen.h>
21
23
35
37{
38 if (pixmap.isNull())
39 return QPixmap();
40
42 return pixmap;
43
44 return qt_toRasterPixmap(pixmap.toImage());
45}
46
51
55
62
67
69{
71 if (pixelType() == BitmapType)
73 else
75
77 w = width;
78 h = height;
79 d = image.depth();
80 is_null = (w <= 0 || h <= 0);
81
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());
86 }
87
88 setSerialNumber(image.cacheKey() >> 32);
89}
90
92 Qt::ImageConversionFlags flags)
93{
94 QByteArray a = QByteArray::fromRawData(reinterpret_cast<const char *>(buffer), len);
95 QBuffer b(&a);
98 if (image.isNull())
99 return false;
100
101 createPixmapForImage(std::move(image), flags);
102 return !isNull();
103}
104
106 Qt::ImageConversionFlags flags)
107{
108 QImage image = sourceImage;
109 createPixmapForImage(std::move(image), flags);
110}
111
113 Qt::ImageConversionFlags flags)
114{
115 createPixmapForImage(std::move(sourceImage), flags);
116}
117
119 Qt::ImageConversionFlags flags)
120{
122 QImage image = imageReader->read();
123 if (image.isNull())
124 return;
125
126 createPixmapForImage(std::move(image), flags);
127}
128
129// from qbackingstore.cpp
130extern void qt_scrollRectInImage(QImage &img, const QRect &rect, const QPoint &offset);
131
136
137bool QRasterPlatformPixmap::scroll(int dx, int dy, const QRect &rect)
138{
139 if (!image.isNull())
141 return true;
142}
143
145{
146 uint pixel;
147
148 if (image.depth() == 1) {
149 int gray = qGray(color.rgba());
150 // Pick the best approximate color in the image's colortable.
151 if (qAbs(qGray(image.color(0)) - gray) < qAbs(qGray(image.color(1)) - gray))
152 pixel = 0;
153 else
154 pixel = 1;
155 } else if (image.depth() >= 15) {
156 int alpha = color.alpha();
157 if (alpha != 255) {
158 if (!image.hasAlphaChannel()) {
160 if (!image.reinterpretAsFormat(toFormat))
161 image = QImage(image.width(), image.height(), toFormat);
162 }
163 }
164 image.fill(color);
165 return;
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());
173 } else
174 {
175 pixel = 0;
176 // ### what about 8 bit indexed?
177 }
178
179 image.fill(pixel);
180}
181
183{
184 return image.hasAlphaChannel();
185}
186
188{
189 if (!image.isNull()) {
190 QImageData *data = const_cast<QImage &>(image).data_ptr();
191 if (data->paintEngine && data->paintEngine->isActive()
192 && data->paintEngine->paintDevice() == &image)
193 {
194 return image.copy();
195 }
196 }
197
198 return image;
199}
200
202{
203 if (rect.isNull())
204 return image;
205
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());
213 return newImage;
214 } else {
215 return image.copy(clipped);
216 }
217}
218
220{
221 return image.paintEngine();
222}
223
225{
226 QImageData *d = image.d;
227 if (!d)
228 return 0;
229
230 // override the image dpi with the screen dpi when rendering to a pixmap
231 switch (metric) {
233 return w;
235 return h;
237 return qRound(d->width * 25.4 / qt_defaultDpiX());
239 return qRound(d->height * 25.4 / qt_defaultDpiY());
241 return d->colortable.size();
243 return this->d;
245 return qt_defaultDpiX();
247 return qt_defaultDpiX();
249 return qt_defaultDpiY();
251 return qt_defaultDpiY();
253 return image.devicePixelRatio();
255 return image.devicePixelRatio() * QPaintDevice::devicePixelRatioFScale();
256
257 default:
258 qWarning("QRasterPlatformPixmap::metric(): Unhandled metric type %d", metric);
259 break;
260 }
261
262 return 0;
263}
264
265void QRasterPlatformPixmap::createPixmapForImage(QImage sourceImage, Qt::ImageConversionFlags flags)
266{
269 format = sourceImage.format();
270 else
271 if (pixelType() == BitmapType) {
273 } else {
274 if (sourceImage.depth() == 1) {
275 format = sourceImage.hasAlphaChannel()
278 } else {
279 QImage::Format nativeFormat = systemNativeFormat();
280 QImage::Format opaqueFormat = qt_opaqueVersionForPainting(nativeFormat);
281 QImage::Format alphaFormat = qt_alphaVersionForPainting(nativeFormat);
282
283 if (!sourceImage.hasAlphaChannel()) {
284 format = opaqueFormat;
285 } else if ((flags & Qt::NoOpaqueDetection) == 0
286 && !sourceImage.data_ptr()->checkForAlphaPixels())
287 {
288 format = opaqueFormat;
289 } else {
290 format = alphaFormat;
291 }
292 }
293 }
294
295 // image has alpha format but is really opaque, so try to do a
296 // more efficient conversion
297 if (format == QImage::Format_RGB32 && (sourceImage.format() == QImage::Format_ARGB32
298 || sourceImage.format() == QImage::Format_ARGB32_Premultiplied))
299 {
300 image = std::move(sourceImage);
301 image.reinterpretAsFormat(QImage::Format_RGB32);
302 } else {
303 image = std::move(sourceImage).convertToFormat(format, flags);
304 }
305
306 if (image.d) {
307 w = image.d->width;
308 h = image.d->height;
309 d = image.d->depth;
310 } else {
311 w = h = d = 0;
312 }
313 is_null = (w <= 0 || h <= 0);
314
315 //ensure the pixmap and the image resulting from toImage() have the same cacheKey();
316 setSerialNumber(image.cacheKey() >> 32);
317 if (image.d)
318 setDetachNumber(image.d->detach_no);
319}
320
325
327{
328 return image.devicePixelRatio();
329}
330
332{
333 image.setDevicePixelRatio(scaleFactor);
334}
335
\inmodule QtCore \reentrant
Definition qbuffer.h:16
\inmodule QtCore
Definition qbytearray.h:57
static QByteArray fromRawData(const char *data, qsizetype size)
Constructs a QByteArray that uses the first size bytes of the data array.
Definition qbytearray.h:409
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition qcolor.h:31
QRgb rgba() const noexcept
Returns the RGB value of the color, including its alpha.
Definition qcolor.cpp:1376
QScreen * primaryScreen
the primary (or default) screen of the application.
The QImageReader class provides a format independent interface for reading images from files or other...
QImage read()
Reads an image from the device.
\inmodule QtGui
Definition qimage.h:37
Format
The following image formats are available in Qt.
Definition qimage.h:41
@ Format_Grayscale16
Definition qimage.h:70
@ Format_Alpha8
Definition qimage.h:65
@ Format_RGB32
Definition qimage.h:46
@ Format_MonoLSB
Definition qimage.h:44
@ Format_ARGB32_Premultiplied
Definition qimage.h:48
@ Format_ARGB32
Definition qimage.h:47
@ Format_Grayscale8
Definition qimage.h:66
void setDevicePixelRatio(qreal scaleFactor)
Sets the device pixel ratio for the image.
Definition qimage.cpp:1510
@ PdmDevicePixelRatioScaled
static qreal devicePixelRatioFScale()
\inmodule QtGui
Returns a copy of the pixmap that is transformed using the given transformation transform and transfo...
Definition qpixmap.h:27
The QPlatformPixmap class provides an abstraction for native pixmaps.
void setDetachNumber(int detNo)
int height() const
void setSerialNumber(int serNo)
bool isNull() const
int width() const
PixelType pixelType() const
virtual QImage::Format format() const =0
Reimplement in subclass to return the image format which corresponds to the screen format.
\inmodule QtCore\reentrant
Definition qpoint.h:25
void fill(const QColor &color) override
void fromImageReader(QImageReader *imageReader, Qt::ImageConversionFlags flags) override
void copy(const QPlatformPixmap *data, const QRect &rect) override
int metric(QPaintDevice::PaintDeviceMetric metric) const override
void createPixmapForImage(QImage sourceImage, Qt::ImageConversionFlags flags)
bool scroll(int dx, int dy, const QRect &rect) override
QImage * buffer() override
void fromImage(const QImage &image, Qt::ImageConversionFlags flags) override
static QImage::Format systemNativeFormat()
void fromImageInPlace(QImage &image, Qt::ImageConversionFlags flags) override
QImage toImage() const override
QPaintEngine * paintEngine() const override
qreal devicePixelRatio() const override
bool fromData(const uchar *buffer, uint len, const char *format, Qt::ImageConversionFlags flags) override
bool hasAlphaChannel() const override
void setDevicePixelRatio(qreal scaleFactor) override
void resize(int width, int height) override
QPlatformPixmap * createCompatiblePlatformPixmap() const override
QRasterPlatformPixmap(PixelType type)
\inmodule QtCore\reentrant
Definition qrect.h:30
QRect intersected(const QRect &other) const noexcept
Definition qrect.h:415
QPlatformScreen * handle() const
Get the platform screen handle.
Definition qscreen.cpp:83
rect
[4]
Combined button and popup list for selecting options.
@ NoFormatConversion
Definition qnamespace.h:501
@ NoOpaqueDetection
Definition qnamespace.h:500
@ AutoColor
Definition qnamespace.h:478
@ color1
Definition qnamespace.h:29
@ color0
Definition qnamespace.h:28
Definition image.cpp:4
int qRound(qfloat16 d) noexcept
Definition qfloat16.h:327
Q_GUI_EXPORT int qt_defaultDpiX()
Definition qfont.cpp:110
Q_GUI_EXPORT int qt_defaultDpiY()
Definition qfont.cpp:125
QImage::Format qt_opaqueVersionForPainting(QImage::Format format)
Definition qimage_p.h:534
QImage::Format qt_alphaVersionForPainting(QImage::Format format)
Definition qimage_p.h:543
#define qWarning
Definition qlogging.h:166
constexpr T qAbs(const T &t)
Definition qnumeric.h:328
GLboolean GLboolean GLboolean b
GLfloat GLfloat GLfloat w
[0]
GLint GLsizei GLsizei height
GLboolean GLboolean GLboolean GLboolean a
[7]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum GLuint buffer
GLint GLsizei width
GLuint color
[2]
GLenum type
GLbitfield flags
GLenum GLuint GLintptr offset
GLint GLsizei GLsizei GLenum format
GLfloat GLfloat GLfloat GLfloat h
const GLubyte * c
GLint void * img
Definition qopenglext.h:233
GLenum GLsizei len
GLfloat GLfloat GLfloat alpha
Definition qopenglext.h:418
const void * data_ptr(const QTransform &t)
Definition qpainter_p.h:48
QT_BEGIN_NAMESPACE QPixmap qt_toRasterPixmap(const QImage &image)
void qt_scrollRectInImage(QImage &img, const QRect &rect, const QPoint &offset)
constexpr int qGray(int r, int g, int b)
Definition qrgb.h:36
constexpr int qAlpha(QRgb rgb)
Definition qrgb.h:27
#define Q_UNUSED(x)
unsigned char uchar
Definition qtypes.h:32
unsigned int uint
Definition qtypes.h:34
double qreal
Definition qtypes.h:187
widget render & pixmap