Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qimage_darwin.mm
Go to the documentation of this file.
1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qimage.h"
5
6#include <private/qcore_mac_p.h>
7#include <private/qcoregraphics_p.h>
8
9#import <Foundation/Foundation.h>
10#import <CoreGraphics/CoreGraphics.h>
11#include <Accelerate/Accelerate.h>
12
14
15/*!
16 \fn CGImageRef QImage::toCGImage() const
17
18 Creates a \c CGImage equivalent to this QImage, and returns a \c CGImageRef
19 handle.
20
21 The returned CGImageRef partakes in the QImage implicit sharing,
22 and holds a reference to the QImage data. CGImage is immutable
23 and will never detach the QImage. Writing to the QImage will detach
24 as usual.
25
26 This function is fast, and does not copy or convert image data.
27
28 If the image format can not be converted a null CGImageRef will
29 be returned. Users of this function may then convert the QImage
30 to a supported format first, for example Format_ARGB32_Premultiplied.
31
32 If the image does not have a color space set the resulting
33 CGImageRef color space is set to the sRGB color space.
34
35 \ingroup platform-type-conversions
36*/
37CGImageRef QImage::toCGImage() const
38{
39 if (isNull())
40 return nil;
41
42 auto cgImageFormat = qt_mac_cgImageFormatForImage(*this);
43 if (!cgImageFormat)
44 return nil;
45
46 // Create a data provider that owns a copy of the QImage and references the image data.
47 auto deleter = [](void *image, const void *, size_t)
48 { delete static_cast<QImage *>(image); };
49 QCFType<CGDataProviderRef> dataProvider =
50 CGDataProviderCreateWithData(new QImage(*this), bits(), sizeInBytes(), deleter);
51
52 const bool shouldInterpolate = false;
53
54 return CGImageCreate(width(), height(),
55 cgImageFormat->bitsPerComponent, cgImageFormat->bitsPerPixel,
56 this->bytesPerLine(), cgImageFormat->colorSpace,
57 cgImageFormat->bitmapInfo, dataProvider, cgImageFormat->decode,
58 shouldInterpolate, cgImageFormat->renderingIntent
59 );
60}
61
62QT_END_NAMESPACE