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
qwindowsdirect2dbitmap.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
8
9#include <QtGui/qimage.h>
10#include <QtGui/qcolor.h>
11
12#include <wrl.h>
13
15
17
19{
20public:
21 QWindowsDirect2DBitmapPrivate(ID2D1DeviceContext *dc = nullptr,
22 ID2D1Bitmap1 *bm = nullptr)
24 , bitmap(bm)
25
26 {
27 deviceContext->get()->SetTarget(bm);
28 }
29
30 D2D1_BITMAP_PROPERTIES1 bitmapProperties() const
31 {
33QT_WARNING_DISABLE_DEPRECATED // see QTBUG-94043
34 FLOAT dpiX, dpiY;
35 QWindowsDirect2DContext::instance()->d2dFactory()->GetDesktopDpi(&dpiX, &dpiY);
37
38 return D2D1::BitmapProperties1(
39 D2D1_BITMAP_OPTIONS_TARGET,
40 D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM,
41 D2D1_ALPHA_MODE_PREMULTIPLIED),
42 dpiX, dpiY);
43
44 }
45
46 bool resize(int width, int height, const void *data = nullptr, int pitch = 0)
47 {
48 deviceContext->get()->SetTarget(nullptr);
49 bitmap.Reset();
50
51 D2D1_SIZE_U size = {
52 UINT32(width), UINT32(height)
53 };
54
55 HRESULT hr = deviceContext->get()->CreateBitmap(size, data, UINT32(pitch),
57 bitmap.ReleaseAndGetAddressOf());
58 if (SUCCEEDED(hr))
59 deviceContext->get()->SetTarget(bitmap.Get());
60 else
61 qWarning("%s: Could not create bitmap: %#lx", __FUNCTION__, hr);
62
63 return SUCCEEDED(hr);
64 }
65
67 {
68 if (!bitmap)
69 return QImage();
70
71 ComPtr<ID2D1Bitmap1> mappingCopy;
72
73 HRESULT hr = S_OK;
74 D2D1_SIZE_U size = bitmap->GetPixelSize();
75
76 D2D1_BITMAP_PROPERTIES1 properties = bitmapProperties();
77 properties.bitmapOptions = D2D1_BITMAP_OPTIONS_CANNOT_DRAW | D2D1_BITMAP_OPTIONS_CPU_READ;
78
79 hr = deviceContext->get()->CreateBitmap(size, nullptr, 0,
80 properties, &mappingCopy);
81 if (FAILED(hr)) {
82 qWarning("%s: Could not create bitmap: %#lx", __FUNCTION__, hr);
83 return QImage();
84 }
85
86 hr = mappingCopy->CopyFromBitmap(nullptr, bitmap.Get(), nullptr);
87 if (FAILED(hr)) {
88 qWarning("%s: Could not copy from bitmap: %#lx", __FUNCTION__, hr);
89 return QImage();
90 }
91
92 D2D1_MAPPED_RECT mappedRect;
93 hr = mappingCopy->Map(D2D1_MAP_OPTIONS_READ, &mappedRect);
94 if (FAILED(hr)) {
95 qWarning("%s: Could not map: %#lx", __FUNCTION__, hr);
96 return QImage();
97 }
98
99 return QImage(static_cast<const uchar *>(mappedRect.bits),
100 int(size.width), int(size.height), int(mappedRect.pitch),
102 }
103
104 QScopedPointer<QWindowsDirect2DDeviceContext> deviceContext;
105 ComPtr<ID2D1Bitmap1> bitmap;
106};
107
112
113QWindowsDirect2DBitmap::QWindowsDirect2DBitmap(ID2D1Bitmap1 *bitmap, ID2D1DeviceContext *dc)
114 : d_ptr(new QWindowsDirect2DBitmapPrivate(dc, bitmap))
115{
116}
117
121
123{
125 return d->resize(width, height);
126}
127
128bool QWindowsDirect2DBitmap::fromImage(const QImage &image, Qt::ImageConversionFlags flags)
129{
131
132 QImage converted = image.convertToFormat(QImage::Format_ARGB32_Premultiplied, flags);
133 return d->resize(converted.width(), converted.height(),
134 converted.constBits(), converted.bytesPerLine());
135}
136
138{
139 Q_D(const QWindowsDirect2DBitmap);
140 return d->bitmap.Get();
141}
142
144{
145 Q_D(const QWindowsDirect2DBitmap);
146 return d->deviceContext.data();
147}
148
150{
152
153 d->deviceContext->begin();
154 d->deviceContext->get()->Clear(to_d2d_color_f(color));
155 d->deviceContext->end();
156}
157
159{
161 return d->toImage(rect);
162}
163
165{
166 Q_D(const QWindowsDirect2DBitmap);
167
168 D2D1_SIZE_U size = d->bitmap->GetPixelSize();
169 return QSize(int(size.width), int(size.height));
170}
171
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition qcolor.h:31
\inmodule QtGui
Definition qimage.h:37
qsizetype bytesPerLine() const
Returns the number of bytes per image scanline.
Definition qimage.cpp:1560
QImage copy(const QRect &rect=QRect()) const
Returns a sub-area of the image as a new image.
int width() const
Returns the width of the image.
int height() const
Returns the height of the image.
@ Format_ARGB32_Premultiplied
Definition qimage.h:48
const uchar * constBits() const
Returns a pointer to the first pixel data.
Definition qimage.cpp:1733
\inmodule QtCore\reentrant
Definition qrect.h:30
\inmodule QtCore
Definition qsize.h:25
QScopedPointer< QWindowsDirect2DDeviceContext > deviceContext
D2D1_BITMAP_PROPERTIES1 bitmapProperties() const
bool resize(int width, int height, const void *data=nullptr, int pitch=0)
QWindowsDirect2DBitmapPrivate(ID2D1DeviceContext *dc=nullptr, ID2D1Bitmap1 *bm=nullptr)
QWindowsDirect2DDeviceContext * deviceContext() const
void fill(const QColor &color)
ID2D1Bitmap1 * bitmap() const
bool resize(int width, int height)
bool fromImage(const QImage &image, Qt::ImageConversionFlags flags)
QImage toImage(const QRect &rect=QRect())
static QWindowsDirect2DContext * instance()
rect
[4]
Combined button and popup list for selecting options.
Definition image.cpp:4
#define QT_WARNING_POP
#define QT_WARNING_DISABLE_DEPRECATED
#define QT_WARNING_PUSH
static const QCssKnownValue properties[NumProperties - 1]
#define qWarning
Definition qlogging.h:166
GLint GLsizei GLsizei height
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLint GLsizei width
GLuint color
[2]
GLbitfield flags
GLsizei GLfixed GLfixed GLfixed GLfixed const GLubyte * bitmap
unsigned char uchar
Definition qtypes.h:32
long HRESULT
D2D1::ColorF to_d2d_color_f(const QColor &c)