1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qimage.h"
41
42#include <private/qcore_mac_p.h>
43#include <private/qcoregraphics_p.h>
44
45#import <Foundation/Foundation.h>
46#import <CoreGraphics/CoreGraphics.h>
47
48QT_BEGIN_NAMESPACE
49
50/*!
51 \brief Creates a \c CGImage equivalent to this QImage.
52
53 Returns a \c CGImageRef handle.
54
55 The returned CGImageRef partakes in the QImage implicit sharing,
56 and holds a reference to the QImage data. CGImage is immutable
57 and will never detach the QImage. Writing to the QImage will detach
58 as usual.
59
60 This function is fast, and does not copy or convert image data.
61
62 The following image formats are supported, and will be mapped to
63 a corresponding native image type:
64
65 \table
66 \header
67 \li Qt
68 \li CoreGraphics
69 \row
70 \li Format_ARGB32
71 \li kCGImageAlphaFirst | kCGBitmapByteOrder32Host
72 \row
73 \li Format_RGB32
74 \li kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Host
75 \row
76 \li Format_RGBA8888_Premultiplied
77 \li kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big
78 \row
79 \li Format_RGBA8888
80 \li kCGImageAlphaLast | kCGBitmapByteOrder32Big
81 \row
82 \li Format_RGBX8888
83 \li kCGImageAlphaNoneSkipLast | kCGBitmapByteOrder32Big
84 \row
85 \li Format_ARGB32_Premultiplied
86 \li kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host
87 \endtable
88
89 Other formats are not supported; this function returns a null
90 CGImageRef for those cases. Users of this function may then
91 convert the QImage to a supported formate first, for example
92 Format_ARGB32_Premultiplied.
93
94 The CGImageRef color space is set to the sRGB color space.
95
96 \ingroup platform-type-conversions
97*/
98CGImageRef QImage::toCGImage() const
99{
100 if (isNull())
101 return nil;
102
103 CGBitmapInfo bitmapInfo = qt_mac_bitmapInfoForImage(*this);
104
105 // Format not supported: return nil CGImageRef
106 if (bitmapInfo == kCGImageAlphaNone)
107 return nil;
108
109 // Create a data provider that owns a copy of the QImage and references the image data.
110 auto deleter = [](void *image, const void *, size_t)
111 { delete static_cast<QImage *>(image); };
112 QCFType<CGDataProviderRef> dataProvider =
113 CGDataProviderCreateWithData(new QImage(*this), bits(), sizeInBytes(), deleter);
114
115 QCFType<CGColorSpaceRef> colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
116
117 const size_t bitsPerComponent = 8;
118 const size_t bitsPerPixel = 32;
119 const CGFloat *decode = nullptr;
120 const bool shouldInterpolate = false;
121
122 return CGImageCreate(width(), height(), bitsPerComponent, bitsPerPixel,
123 this->bytesPerLine(), colorSpace, bitmapInfo, dataProvider,
124 decode, shouldInterpolate, kCGRenderingIntentDefault);
125}
126
127QT_END_NAMESPACE
128