1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the plugins of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qwindowsnativeimage.h"
43 #include "qwindowscontext.h"
45 #include <QtGui/private/qpaintengine_p.h>
46 #include <QtGui/private/qpaintengine_raster_p.h>
51 BITMAPINFOHEADER bmiHeader;
58 \class QWindowsNativeImage
59 \brief Windows Native image
61 Note that size can be 0 (widget autotests with zero size), which
62 causes CreateDIBSection() to fail.
64 \sa QWindowsBackingStore
66 \ingroup qt-lighthouse-win
69 static inline HDC createDC()
71 HDC display_dc = GetDC(0);
72 HDC hdc = CreateCompatibleDC(display_dc);
73 ReleaseDC(0, display_dc);
78 static inline HBITMAP createDIB(HDC hdc, int width, int height,
79 QImage::Format format,
83 memset(&bmi, 0, sizeof(bmi));
84 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
85 bmi.bmiHeader.biWidth = width;
86 bmi.bmiHeader.biHeight = -height; // top-down.
87 bmi.bmiHeader.biPlanes = 1;
88 bmi.bmiHeader.biSizeImage = 0;
90 if (format == QImage::Format_RGB16) {
91 bmi.bmiHeader.biBitCount = 16;
92 bmi.bmiHeader.biCompression = BI_BITFIELDS;
94 bmi.greenMask = 0x07E0;
95 bmi.blueMask = 0x001F;
97 bmi.bmiHeader.biBitCount = 32;
98 bmi.bmiHeader.biCompression = BI_RGB;
105 HBITMAP bitmap = CreateDIBSection(hdc, reinterpret_cast<BITMAPINFO *>(&bmi),
106 DIB_RGB_COLORS, &bits, 0, 0);
107 if (!bitmap || !bits)
108 qFatal("%s: CreateDIBSection failed.", __FUNCTION__);
110 *bitsIn = (uchar*)bits;
114 QWindowsNativeImage::QWindowsNativeImage(int width, int height,
115 QImage::Format format) :
120 if (width != 0 && height != 0) {
122 m_bitmap = createDIB(m_hdc, width, height, format, &bits);
123 m_null_bitmap = (HBITMAP)SelectObject(m_hdc, m_bitmap);
124 m_image = QImage(bits, width, height, format);
125 Q_ASSERT(m_image.paintEngine()->type() == QPaintEngine::Raster);
126 static_cast<QRasterPaintEngine *>(m_image.paintEngine())->setDC(m_hdc);
128 m_image = QImage(width, height, format);
136 QWindowsNativeImage::~QWindowsNativeImage()
141 SelectObject(m_hdc, m_null_bitmap);
142 DeleteObject(m_bitmap);
148 QImage::Format QWindowsNativeImage::systemFormat()
150 static const int depth = QWindowsContext::instance()->screenDepth();
151 return depth == 16 ? QImage::Format_RGB16 : QImage::Format_RGB32;