source: trunk/src/gui/image/qpixmap_win.cpp@ 698

Last change on this file since 698 was 651, checked in by Dmitry A. Kuminov, 16 years ago

trunk: Merged in qt 4.6.2 sources.

File size: 15.4 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtGui module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qpixmap.h"
43#include "qpixmap_raster_p.h"
44
45#include "qbitmap.h"
46#include "qimage.h"
47#include "qwidget.h"
48#include "qpainter.h"
49#include "qdatastream.h"
50#include "qbuffer.h"
51#include "qapplication.h"
52#include "qevent.h"
53#include "qfile.h"
54#include "qfileinfo.h"
55#include "qdatetime.h"
56#include "qpixmapcache.h"
57#include "qimagereader.h"
58#include "qimagewriter.h"
59#include "qdebug.h"
60#include "qt_windows.h"
61
62#if defined(Q_WS_WINCE)
63#include <winbase.h>
64#include "qguifunctions_wince.h"
65extern bool qt_wince_is_high_dpi();
66extern bool qt_wince_is_pocket_pc();
67#endif
68
69#ifndef CAPTUREBLT
70#define CAPTUREBLT ((DWORD)0x40000000)
71#endif
72
73QT_BEGIN_NAMESPACE
74
75QPixmap QPixmap::grabWindow(WId winId, int x, int y, int w, int h )
76{
77 RECT r;
78 GetClientRect(winId, &r);
79
80 if (w < 0) w = r.right - r.left;
81 if (h < 0) h = r.bottom - r.top;
82
83#ifdef Q_WS_WINCE_WM
84 if (qt_wince_is_pocket_pc()) {
85 QWidget *widget = QWidget::find(winId);
86 if (qobject_cast<QDesktopWidget *>(widget)) {
87 RECT rect = {0,0,0,0};
88 AdjustWindowRectEx(&rect, WS_BORDER | WS_CAPTION, FALSE, 0);
89 int magicNumber = qt_wince_is_high_dpi() ? 4 : 2;
90 y += rect.top - magicNumber;
91 }
92 }
93#endif
94
95 // Create and setup bitmap
96 HDC display_dc = GetDC(0);
97 HDC bitmap_dc = CreateCompatibleDC(display_dc);
98 HBITMAP bitmap = CreateCompatibleBitmap(display_dc, w, h);
99 HGDIOBJ null_bitmap = SelectObject(bitmap_dc, bitmap);
100
101 // copy data
102 HDC window_dc = GetDC(winId);
103 BitBlt(bitmap_dc, 0, 0, w, h, window_dc, x, y, SRCCOPY
104#ifndef Q_WS_WINCE
105 | CAPTUREBLT
106#endif
107 );
108
109 // clean up all but bitmap
110 ReleaseDC(winId, window_dc);
111 SelectObject(bitmap_dc, null_bitmap);
112 DeleteDC(bitmap_dc);
113
114 QPixmap pixmap = QPixmap::fromWinHBITMAP(bitmap);
115
116 DeleteObject(bitmap);
117 ReleaseDC(0, display_dc);
118
119 return pixmap;
120}
121
122HBITMAP QPixmap::toWinHBITMAP(HBitmapFormat format) const
123{
124 if (isNull())
125 return 0;
126
127 HBITMAP bitmap = 0;
128 if (data->classId() == QPixmapData::RasterClass) {
129 QRasterPixmapData* d = static_cast<QRasterPixmapData*>(data.data());
130 int w = d->image.width();
131 int h = d->image.height();
132
133 HDC display_dc = GetDC(0);
134
135 // Define the header
136 BITMAPINFO bmi;
137 memset(&bmi, 0, sizeof(bmi));
138 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
139 bmi.bmiHeader.biWidth = w;
140 bmi.bmiHeader.biHeight = -h;
141 bmi.bmiHeader.biPlanes = 1;
142 bmi.bmiHeader.biBitCount = 32;
143 bmi.bmiHeader.biCompression = BI_RGB;
144 bmi.bmiHeader.biSizeImage = w * h * 4;
145
146 // Create the pixmap
147 uchar *pixels = 0;
148 bitmap = CreateDIBSection(display_dc, &bmi, DIB_RGB_COLORS, (void **) &pixels, 0, 0);
149 ReleaseDC(0, display_dc);
150 if (!bitmap) {
151 qErrnoWarning("QPixmap::toWinHBITMAP(), failed to create dibsection");
152 return 0;
153 }
154 if (!pixels) {
155 qErrnoWarning("QPixmap::toWinHBITMAP(), did not allocate pixel data");
156 return 0;
157 }
158
159 // Copy over the data
160 QImage::Format imageFormat = QImage::Format_ARGB32;
161 if (format == NoAlpha)
162 imageFormat = QImage::Format_RGB32;
163 else if (format == PremultipliedAlpha)
164 imageFormat = QImage::Format_ARGB32_Premultiplied;
165 const QImage image = d->image.convertToFormat(imageFormat);
166 int bytes_per_line = w * 4;
167 for (int y=0; y<h; ++y)
168 memcpy(pixels + y * bytes_per_line, image.scanLine(y), bytes_per_line);
169
170 } else {
171 QPixmapData *data = new QRasterPixmapData(depth() == 1 ?
172 QPixmapData::BitmapType : QPixmapData::PixmapType);
173 data->fromImage(toImage(), Qt::AutoColor);
174 return QPixmap(data).toWinHBITMAP(format);
175 }
176 return bitmap;
177}
178
179QPixmap QPixmap::fromWinHBITMAP(HBITMAP bitmap, HBitmapFormat format)
180{
181 // Verify size
182 BITMAP bitmap_info;
183 memset(&bitmap_info, 0, sizeof(BITMAP));
184
185 int res = GetObject(bitmap, sizeof(BITMAP), &bitmap_info);
186 if (!res) {
187 qErrnoWarning("QPixmap::fromWinHBITMAP(), failed to get bitmap info");
188 return QPixmap();
189 }
190 int w = bitmap_info.bmWidth;
191 int h = bitmap_info.bmHeight;
192
193 BITMAPINFO bmi;
194 memset(&bmi, 0, sizeof(bmi));
195 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
196 bmi.bmiHeader.biWidth = w;
197 bmi.bmiHeader.biHeight = -h;
198 bmi.bmiHeader.biPlanes = 1;
199 bmi.bmiHeader.biBitCount = 32;
200 bmi.bmiHeader.biCompression = BI_RGB;
201 bmi.bmiHeader.biSizeImage = w * h * 4;
202
203 QImage result;
204 // Get bitmap bits
205 uchar *data = (uchar *) qMalloc(bmi.bmiHeader.biSizeImage);