| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com)
|
|---|
| 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 qt-sales@nokia.com.
|
|---|
| 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 |
|
|---|
| 144 | #elif defined(Q_WS_X11) && !defined(QT_NO_MITSHM)
|
|---|
| 145 |
|
|---|
| 146 | QNativeImage::QNativeImage(int width, int height, QImage::Format format,bool /* isTextBuffer */, QWidget *widget)
|
|---|
| 147 | {
|
|---|
| 148 | if (!X11->use_mitshm) {
|
|---|
| 149 | xshmimg = 0;
|
|---|
| 150 | xshmpm = 0;
|
|---|
| 151 | image = QImage(width, height, format);
|
|---|
| 152 | return;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | QX11Info info = widget->x11Info();
|
|---|
| 156 |
|
|---|
| 157 | int dd = info.depth();
|
|---|
| 158 | Visual *vis = (Visual*) info.visual();
|
|---|
| 159 |
|
|---|
| 160 | xshmimg = XShmCreateImage(X11->display, vis, dd, ZPixmap, 0, &xshminfo, width, height);
|
|---|
| 161 | if (!xshmimg) {
|
|---|
| 162 | qWarning("QNativeImage: Unable to create shared XImage.");
|
|---|
| 163 | return;
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | bool ok;
|
|---|
| 167 | xshminfo.shmid = shmget(IPC_PRIVATE, xshmimg->bytes_per_line * xshmimg->height,
|
|---|
| 168 | IPC_CREAT | 0777);
|
|---|
| 169 | ok = xshminfo.shmid != -1;
|
|---|
| 170 | if (ok) {
|
|---|
| 171 | xshmimg->data = (char*)shmat(xshminfo.shmid, 0, 0);
|
|---|
| 172 | xshminfo.shmaddr = xshmimg->data;
|
|---|
| 173 | ok = (xshminfo.shmaddr != (char*)-1);
|
|---|
| 174 | if (ok)
|
|---|
| 175 | image = QImage((uchar *)xshmimg->data, width, height, systemFormat());
|
|---|
| 176 | }
|
|---|
| 177 | xshminfo.readOnly = false;
|
|---|
| 178 | if (ok)
|
|---|
| 179 | ok = XShmAttach(X11->display, &xshminfo);
|
|---|
| 180 | if (!ok) {
|
|---|
| 181 | qWarning() << "QNativeImage: Unable to attach to shared memory segment.";
|
|---|
| 182 | if (xshmimg->data) {
|
|---|
| 183 | free(xshmimg->data);
|
|---|
| 184 | xshmimg->data = 0;
|
|---|
| 185 | }
|
|---|
| 186 | XDestroyImage(xshmimg);
|
|---|
| 187 | xshmimg = 0;
|
|---|
| 188 | if (xshminfo.shmaddr)
|
|---|
| 189 | shmdt(xshminfo.shmaddr);
|
|---|
| 190 | if (xshminfo.shmid != -1)
|
|---|
| 191 | shmctl(xshminfo.shmid, IPC_RMID, 0);
|
|---|
| 192 | return;
|
|---|
| 193 | }
|
|---|
| 194 | xshmpm = XShmCreatePixmap(X11->display, DefaultRootWindow(X11->display), xshmimg->data,
|
|---|
| 195 | &xshminfo, width, height, dd);
|
|---|
| 196 | if (!xshmpm) {
|
|---|
| 197 | qWarning() << "QNativeImage: Unable to create shared Pixmap.";
|
|---|
| 198 | }
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 |
|
|---|
| 202 | QNativeImage::~QNativeImage()
|
|---|
| 203 | {
|
|---|
| 204 | if (!xshmimg)
|
|---|
| 205 | return;
|
|---|
| 206 |
|
|---|
| 207 | if (xshmpm) {
|
|---|
| 208 | XFreePixmap(X11->display, xshmpm);
|
|---|
| 209 | xshmpm = 0;
|
|---|
| 210 | }
|
|---|
| 211 | XShmDetach(X11->display, &xshminfo);
|
|---|
| 212 | xshmimg->data = 0;
|
|---|
| 213 | XDestroyImage(xshmimg);
|
|---|
| 214 | xshmimg = 0;
|
|---|
| 215 | shmdt(xshminfo.shmaddr);
|
|---|
| 216 | shmctl(xshminfo.shmid, IPC_RMID, 0);
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | QImage::Format QNativeImage::systemFormat()
|
|---|
| 220 | {
|
|---|
| 221 | if (QX11Info::appDepth() == 16)
|
|---|
| 222 | return QImage::Format_RGB16;
|
|---|
| 223 | return QImage::Format_RGB32;
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | #elif defined(Q_WS_MAC)
|
|---|
| 227 |
|
|---|
| 228 | QNativeImage::QNativeImage(int width, int height, QImage::Format format, bool /* isTextBuffer */, QWidget *)
|
|---|
| 229 | : image(width, height, format)
|
|---|
| 230 | {
|
|---|
| 231 | cgColorSpace = CGColorSpaceCreateDeviceRGB();
|
|---|
| 232 | uint cgflags = kCGImageAlphaNoneSkipFirst;
|
|---|
| 233 |
|
|---|
| 234 | #ifdef kCGBitmapByteOrder32Host //only needed because CGImage.h added symbols in the minor version
|
|---|
| 235 | if(QSysInfo::MacintoshVersion >= QSysInfo::MV_10_4)
|
|---|
| 236 | cgflags |= kCGBitmapByteOrder32Host;
|
|---|
| 237 | #endif
|
|---|
| 238 |
|
|---|
| 239 | cg = CGBitmapContextCreate(image.bits(), width, height, 8, image.bytesPerLine(), cgColorSpace, cgflags);
|
|---|
| 240 | CGContextTranslateCTM(cg, 0, height);
|
|---|
| 241 | CGContextScaleCTM(cg, 1, -1);
|
|---|
| 242 |
|
|---|
| 243 | Q_ASSERT(image.paintEngine()->type() == QPaintEngine::Raster);
|
|---|
| 244 | static_cast<QRasterPaintEngine *>(image.paintEngine())->setCGContext(cg);
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 |
|
|---|
| 248 | QNativeImage::~QNativeImage()
|
|---|
| 249 | {
|
|---|
| 250 | CGContextRelease(cg);
|
|---|
| 251 | CGColorSpaceRelease(cgColorSpace);
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | QImage::Format QNativeImage::systemFormat()
|
|---|
| 255 | {
|
|---|
| 256 | return QImage::Format_RGB32;
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 |
|
|---|
| 260 | #else // other platforms...
|
|---|
| 261 |
|
|---|
| 262 | QNativeImage::QNativeImage(int width, int height, QImage::Format format, bool /* isTextBuffer */, QWidget *)
|
|---|
| 263 | : image(width, height, format)
|
|---|
| 264 | {
|
|---|
| 265 |
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 |
|
|---|
| 269 | QNativeImage::~QNativeImage()
|
|---|
| 270 | {
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 | QImage::Format QNativeImage::systemFormat()
|
|---|
| 274 | {
|
|---|
| 275 | return QImage::Format_RGB32;
|
|---|
| 276 | }
|
|---|
| 277 |
|
|---|
| 278 | #endif // platforms
|
|---|
| 279 |
|
|---|
| 280 | QT_END_NAMESPACE
|
|---|
| 281 |
|
|---|