From b0abe20d4b05e9e1e0800b8be64df15fa1660367 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morten=20Johan=20S=C3=B8rvig?= Date: Mon, 2 Nov 2015 10:49:17 +0100 Subject: Darwin: Add QImage::toCGImage() conversion function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (Move QT_FORWARD_DECLARE_CG to qglobal.h) This function converts to CGImage for supported formats. This is done by creating a CGImageRef that reuses the QImage data. The CGImage and QImage ref counting systems are bridged, implemented by using CGDataProvider that holds a copy of the QImage. Unlike the previous internal implementation this public version does not implicitly convert unsupported formats to ARGB32_Premultiplied. See included documentation for the complete description. Change-Id: Ie3984a7a8331e02a6f1c42943caaf76854e93538 Reviewed-by: Morten Johan Sørvig Reviewed-by: Gabriel de Dietrich --- src/gui/image/image.pri | 2 + src/gui/image/qimage.h | 9 +++ src/gui/image/qimage_darwin.mm | 141 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 152 insertions(+) create mode 100644 src/gui/image/qimage_darwin.mm (limited to 'src/gui/image') diff --git a/src/gui/image/image.pri b/src/gui/image/image.pri index 378256516c..fe21ff4cc0 100644 --- a/src/gui/image/image.pri +++ b/src/gui/image/image.pri @@ -53,6 +53,8 @@ SOURCES += \ win32:!winrt: SOURCES += image/qpixmap_win.cpp +darwin: OBJECTIVE_SOURCES += image/qimage_darwin.mm + NO_PCH_SOURCES += image/qimage_compat.cpp false: SOURCES += $$NO_PCH_SOURCES # Hack for QtCreator diff --git a/src/gui/image/qimage.h b/src/gui/image/qimage.h index a99134d3bb..91aaf673d0 100644 --- a/src/gui/image/qimage.h +++ b/src/gui/image/qimage.h @@ -54,6 +54,10 @@ #include #endif +#if defined(Q_OS_DARWIN) || defined(Q_QDOC) +Q_FORWARD_DECLARE_MUTABLE_CG_TYPE(CGImage); +#endif + QT_BEGIN_NAMESPACE @@ -321,6 +325,11 @@ public: static QPixelFormat toPixelFormat(QImage::Format format) Q_DECL_NOTHROW; static QImage::Format toImageFormat(QPixelFormat format) Q_DECL_NOTHROW; + // Platform spesific conversion functions +#if defined(Q_OS_DARWIN) || defined(Q_QDOC) + CGImageRef toCGImage() const Q_DECL_CF_RETURNS_RETAINED; +#endif + #if QT_DEPRECATED_SINCE(5, 0) QT_DEPRECATED inline QString text(const char *key, const char *lang = Q_NULLPTR) const; QT_DEPRECATED inline QList textList() const; diff --git a/src/gui/image/qimage_darwin.mm b/src/gui/image/qimage_darwin.mm new file mode 100644 index 0000000000..d72733abd3 --- /dev/null +++ b/src/gui/image/qimage_darwin.mm @@ -0,0 +1,141 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the QtGui module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL21$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** As a special exception, The Qt Company gives you certain additional +** rights. These rights are described in The Qt Company LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qimage.h" + +#include + +#import +#import + +QT_BEGIN_NAMESPACE + +/*! + Creates a \c CGImage equivalent to the QImage \a image. Returns a + \c CGImageRef handle. + + The returned CGImageRef partakes in the QImage implicit sharing, + and holds a reference to the QImage data. CGImage is immutable + and will never detach the QImage. Writing to the QImage will detach + as usual. + + This function is fast, and does not copy or convert image data. + + The following image formats are supported, and will be mapped to + a corresponding native image type: + + \table + \header + \li Qt + \li CoreGraphics + \row + \li Format_ARGB32 + \li kCGImageAlphaFirst | kCGBitmapByteOrder32Host + \row + \li Format_RGB32 + \li kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Host + \row + \li Format_RGBA8888_Premultiplied + \li kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big + \row + \li Format_RGBA8888 + \li kCGImageAlphaLast | kCGBitmapByteOrder32Big + \row + \li Format_RGBX8888 + \li kCGImageAlphaNoneSkipLast | kCGBitmapByteOrder32Big + \row + \li Format_ARGB32_Premultiplied + \li kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host + \endtable + + Other formats are not supported; this function returns a null + CGImageRef for those cases. Users of this function may then + convert the QImage to a supported formate first, for example + Format_ARGB32_Premultiplied. + + The CGImageRef color space is set to the sRGB color space. + + \sa toNSImage() +*/ +CGImageRef QImage::toCGImage() const +{ + if (isNull()) + return nil; + + // Determine the target native format + uint cgflags = kCGImageAlphaNone; + switch (format()) { + case QImage::Format_ARGB32: + cgflags = kCGImageAlphaFirst | kCGBitmapByteOrder32Host; + break; + case QImage::Format_RGB32: + cgflags = kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Host; + break; + case QImage::Format_RGBA8888_Premultiplied: + cgflags = kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big; + break; + case QImage::Format_RGBA8888: + cgflags = kCGImageAlphaLast | kCGBitmapByteOrder32Big; + break; + case QImage::Format_RGBX8888: + cgflags = kCGImageAlphaNoneSkipLast | kCGBitmapByteOrder32Big; + break; + case QImage::Format_ARGB32_Premultiplied: + cgflags = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host; + break; + default: break; + } + + // Format not supported: return nil CGImageRef + if (cgflags == kCGImageAlphaNone) + return nil; + + // Create a data provider that owns a copy of the QImage and references the image data. + auto deleter = [](void *image, const void *, size_t) + { delete static_cast(image); }; + QCFType dataProvider = + CGDataProviderCreateWithData(new QImage(*this), bits(), byteCount(), deleter); + + QCFType colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceSRGB); + + const size_t bitsPerComponent = 8; + const size_t bitsPerPixel = 32; + const CGFloat *decode = nullptr; + const bool shouldInterpolate = false; + + return CGImageCreate(width(), height(), bitsPerComponent, bitsPerPixel, + this->bytesPerLine(), colorSpace, cgflags, dataProvider, + decode, shouldInterpolate, kCGRenderingIntentDefault); +} + +QT_END_NAMESPACE -- cgit v1.2.3