1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the QtGui module of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** 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 are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include <qdebug.h>
|
---|
43 | #include "qnativeimage_p.h"
|
---|
44 | #include "qcolormap.h"
|
---|
45 |
|
---|
46 | #include "private/qpaintengine_raster_p.h"
|
---|
47 |
|
---|
48 | #if defined(Q_WS_X11) && !defined(QT_NO_MITSHM)
|
---|
49 | #include <qx11info_x11.h>
|
---|
50 | #include <sys/ipc.h>
|
---|
51 | #include <sys/shm.h>
|
---|
52 | #include <qwidget.h>
|
---|
53 | #endif
|
---|
54 |
|
---|
55 | QT_BEGIN_NAMESPACE
|
---|
56 |
|
---|
57 | #ifdef Q_WS_WIN
|
---|
58 | typedef struct {
|
---|
59 | BITMAPINFOHEADER bmiHeader;
|
---|
60 | DWORD redMask;
|
---|
61 | DWORD greenMask;
|
---|
62 | DWORD blueMask;
|
---|
63 | } BITMAPINFO_MASK;
|
---|
64 |
|
---|
65 |
|
---|
66 | QNativeImage::QNativeImage(int width, int height, QImage::Format format, bool isTextBuffer, QWidget *)
|
---|
67 | {
|
---|
68 | #ifndef Q_OS_WINCE
|
---|
69 | Q_UNUSED(isTextBuffer);
|
---|
70 | #endif
|
---|
71 | BITMAPINFO_MASK bmi;
|
---|
72 | memset(&bmi, 0, sizeof(bmi));
|
---|
73 | bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
|
---|
74 | bmi.bmiHeader.biWidth = width;
|
---|
75 | bmi.bmiHeader.biHeight = -height;
|
---|
76 | bmi.bmiHeader.biPlanes = 1;
|
---|
77 | bmi.bmiHeader.biSizeImage = 0;
|
---|
78 |
|
---|
79 | if (format == QImage::Format_RGB16) {
|
---|
80 | bmi.bmiHeader.biBitCount = 16;
|
---|
81 | #ifdef Q_OS_WINCE
|
---|
82 | if (isTextBuffer) {
|
---|
83 | bmi.bmiHeader.biCompression = BI_RGB;
|
---|
84 | bmi.redMask = 0;
|
---|
85 | bmi.greenMask = 0;
|
---|
86 | bmi.blueMask = 0;
|
---|
87 | } else
|
---|
88 | #endif
|
---|
89 | {
|
---|
90 | bmi.bmiHeader.biCompression = BI_BITFIELDS;
|
---|
91 | bmi.redMask = 0xF800;
|
---|
92 | bmi.greenMask = 0x07E0;
|
---|
93 | bmi.blueMask = 0x001F;
|
---|
94 | }
|
---|
95 | } else {
|
---|
96 | bmi.bmiHeader.biBitCount = 32;
|
---|
97 | bmi.bmiHeader.biCompression = BI_RGB;
|
---|
98 | bmi.redMask = 0;
|
---|
99 | bmi.greenMask = 0;
|
---|
100 | bmi.blueMask = 0;
|
---|
101 | }
|
---|
102 |
|
---|
103 | HDC display_dc = GetDC(0);
|
---|
104 | hdc = CreateCompatibleDC(display_dc);
|
---|
105 | ReleaseDC(0, display_dc);
|
---|
106 | Q_ASSERT(hdc);
|
---|
107 |
|
---|
108 | uchar *bits = 0;
|
---|
109 | bitmap = CreateDIBSection(hdc, reinterpret_cast<BITMAPINFO *>(&bmi), DIB_RGB_COLORS, (void**) &bits, 0, 0);
|
---|
110 | Q_ASSERT(bitmap);
|
---|
111 | Q_ASSERT(bits);
|
---|
112 |
|
---|
113 | null_bitmap = (HBITMAP)SelectObject(hdc, bitmap);
|
---|
114 | image = QImage(bits, width, height, format);
|
---|
115 |
|
---|
116 | Q_ASSERT(image.paintEngine()->type() == QPaintEngine::Raster);
|
---|
117 | static_cast<QRasterPaintEngine *>(image.paintEngine())->setDC(hdc);
|
---|
118 |
|
---|
119 | #ifndef Q_OS_WINCE
|
---|
120 | GdiFlush();
|
---|
121 | #endif
|
---|
122 | }
|
---|
123 |
|
---|
124 | QNativeImage::~QNativeImage()
|
---|
125 | {
|
---|
126 | if (bitmap || hdc) {
|
---|
127 | Q_ASSERT(hdc);
|
---|
128 | Q_ASSERT(bitmap);
|
---|
129 | if (null_bitmap)
|
---|
130 | SelectObject(hdc, null_bitmap);
|
---|
131 | DeleteDC(hdc);
|
---|
132 | DeleteObject(bitmap);
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | QImage::Format QNativeImage::systemFormat()
|
---|
137 | {
|
---|
138 | if (QColormap::instance().depth() == 16)
|
---|
139 | return QImage::Format_RGB16;
|
---|
140 | return QImage::Format_RGB32;
|
---|
141 | }
|
---|
142 |
|
---|
143 | #elif defined(Q_WS_PM)
|
---|
144 |
|
---|
145 | QNativeImage::QNativeImage(int width, int height, QImage::Format format,
|
---|
146 | bool isTextBuffer, QWidget *widget)
|
---|
147 | {
|
---|
148 | image = QImage(width, height, format);
|
---|
149 | }
|
---|
150 |
|
---|
151 | QNativeImage::~QNativeImage()
|
---|
152 | {
|
---|
153 | }
|
---|
154 |
|
---|
155 | QImage::Format QNativeImage::systemFormat()
|
---|
156 | {
|
---|
157 | // @todo support 8-bit indexed colors?
|
---|
158 | // if (QColormap::instance().depth() == 16)
|
---|
159 | // return QImage::Format_RGB16;
|
---|
160 | return QImage::Format_RGB32;
|
---|
161 | }
|
---|
162 |
|
---|
163 | #elif defined(Q_WS_X11) && !defined(QT_NO_MITSHM)
|
---|
164 |
|
---|
165 | QNativeImage::QNativeImage(int width, int height, QImage::Format format,bool /* isTextBuffer */, QWidget *widget)
|
---|
166 | {
|
---|
167 | if (!X11->use_mitshm) {
|
---|
168 | xshmimg = 0;
|
---|
169 | xshmpm = 0;
|
---|
170 | image = QImage(width, height, format);
|
---|
171 | return;
|
---|
172 | }
|
---|
173 |
|
---|
174 | QX11Info info = widget->x11Info();
|
---|
175 |
|
---|
176 | int dd = info.depth();
|
---|
177 | Visual *vis = (Visual*) info.visual();
|
---|
178 |
|
---|
179 | xshmimg = XShmCreateImage(X11->display, vis, dd, ZPixmap, 0, &xshminfo, width, height);
|
---|
180 | if (!xshmimg) {
|
---|
181 | qWarning("QNativeImage: Unable to create shared XImage.");
|
---|
182 | return;
|
---|
183 | }
|
---|
184 |
|
---|
185 | bool ok;
|
---|
186 | xshminfo.shmid = shmget(IPC_PRIVATE, xshmimg->bytes_per_line * xshmimg->height,
|
---|
187 | IPC_CREAT | 0777);
|
---|
188 | ok = xshminfo.shmid != -1;
|
---|
189 | if (ok) {
|
---|
190 | xshmimg->data = (char*)shmat(xshminfo.shmid, 0, 0);
|
---|
191 | xshminfo.shmaddr = xshmimg->data;
|
---|
192 | ok = (xshminfo.shmaddr != (char*)-1);
|
---|
193 | if (ok)
|
---|
194 | image = QImage((uchar *)xshmimg->data, width, height, systemFormat());
|
---|
195 | }
|
---|
196 | xshminfo.readOnly = false;
|
---|
197 | if (ok)
|
---|
198 | ok = XShmAttach(X11->display, &xshminfo);
|
---|
199 | if (!ok) {
|
---|
200 | qWarning() << "QNativeImage: Unable to attach to shared memory segment.";
|
---|
201 | if (xshmimg->data) {
|
---|
202 | free(xshmimg->data);
|
---|
203 | xshmimg->data = 0;
|
---|
204 | }
|
---|
205 | XDestroyImage(xshmimg);
|
---|
206 | xshmimg = 0;
|
---|
207 | if (xshminfo.shmaddr)
|
---|
208 | shmdt(xshminfo.shmaddr);
|
---|
209 | if (xshminfo.shmid != -1)
|
---|
210 | shmctl(xshminfo.shmid, IPC_RMID, 0);
|
---|
211 | return;
|
---|
212 | }
|
---|
213 | xshmpm = XShmCreatePixmap(X11->display, DefaultRootWindow(X11->display), xshmimg->data,
|
---|
214 | &xshminfo, width, height, dd);
|
---|
215 | if (!xshmpm) {
|
---|
216 | qWarning() << "QNativeImage: Unable to create shared Pixmap.";
|
---|
217 | }
|
---|
218 | }
|
---|
219 |
|
---|
220 |
|
---|
221 | QNativeImage::~QNativeImage()
|
---|
222 | {
|
---|
223 | if (!xshmimg)
|
---|
224 | return;
|
---|
225 |
|
---|
226 | if (xshmpm) {
|
---|
227 | XFreePixmap(X11->display, xshmpm);
|
---|
228 | xshmpm = 0;
|
---|
229 | }
|
---|
230 | XShmDetach(X11->display, &xshminfo);
|
---|
231 | xshmimg->data = 0;
|
---|
232 | XDestroyImage(xshmimg);
|
---|
233 | xshmimg = 0;
|
---|
234 | shmdt(xshminfo.shmaddr);
|
---|
235 | shmctl(xshminfo.shmid, IPC_RMID, 0);
|
---|
236 | }
|
---|
237 |
|
---|
238 | QImage::Format QNativeImage::systemFormat()
|
---|
239 | {
|
---|
240 | if (QX11Info::appDepth() == 16)
|
---|
241 | return QImage::Format_RGB16;
|
---|
242 | return QImage::Format_RGB32;
|
---|
243 | }
|
---|
244 |
|
---|
245 | #elif defined(Q_WS_MAC)
|
---|
246 |
|
---|
247 | QNativeImage::QNativeImage(int width, int height, QImage::Format format, bool /* isTextBuffer */, QWidget *)
|
---|
248 | : image(width, height, format)
|
---|
249 | {
|
---|
250 | cgColorSpace = CGColorSpaceCreateDeviceRGB();
|
---|
251 | uint cgflags = kCGImageAlphaNoneSkipFirst;
|
---|
252 |
|
---|
253 | #ifdef kCGBitmapByteOrder32Host //only needed because CGImage.h added symbols in the minor version
|
---|
254 | if(QSysInfo::MacintoshVersion >= QSysInfo::MV_10_4)
|
---|
255 | cgflags |= kCGBitmapByteOrder32Host;
|
---|
256 | #endif
|
---|
257 |
|
---|
258 | cg = CGBitmapContextCreate(image.bits(), width, height, 8, image.bytesPerLine(), cgColorSpace, cgflags);
|
---|
259 | CGContextTranslateCTM(cg, 0, height);
|
---|
260 | CGContextScaleCTM(cg, 1, -1);
|
---|
261 |
|
---|
262 | Q_ASSERT(image.paintEngine()->type() == QPaintEngine::Raster);
|
---|
263 | static_cast<QRasterPaintEngine *>(image.paintEngine())->setCGContext(cg);
|
---|
264 | }
|
---|
265 |
|
---|
266 |
|
---|
267 | QNativeImage::~QNativeImage()
|
---|
268 | {
|
---|
269 | CGContextRelease(cg);
|
---|
270 | CGColorSpaceRelease(cgColorSpace);
|
---|
271 | }
|
---|
272 |
|
---|
273 | QImage::Format QNativeImage::systemFormat()
|
---|
274 | {
|
---|
275 | return QImage::Format_RGB32;
|
---|
276 | }
|
---|
277 |
|
---|
278 |
|
---|
279 | #else // other platforms...
|
---|
280 |
|
---|
281 | QNativeImage::QNativeImage(int width, int height, QImage::Format format, bool /* isTextBuffer */, QWidget *)
|
---|
282 | : image(width, height, format)
|
---|
283 | {
|
---|
284 |
|
---|
285 | }
|
---|
286 |
|
---|
287 |
|
---|
288 | QNativeImage::~QNativeImage()
|
---|
289 | {
|
---|
290 | }
|
---|
291 |
|
---|
292 | QImage::Format QNativeImage::systemFormat()
|
---|
293 | {
|
---|
294 | return QImage::Format_RGB32;
|
---|
295 | }
|
---|
296 |
|
---|
297 | #endif // platforms
|
---|
298 |
|
---|
299 | QT_END_NAMESPACE
|
---|
300 |
|
---|