Debian Codegraph @

src/plugins/imageformats/shared/qiiofhelpers.cpp

raw symbols SCIP
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the MacJp2 plugin in the Qt ImageFormats module.
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 <QGuiApplication>
41 #include <QBuffer>
42 #include <QImageIOHandler>
43 #include <QImage>
44
45 #include "qiiofhelpers_p.h"
46
47
48 QT_BEGIN_NAMESPACE
49
50 // Callbacks for sequential data provider & consumer:
51
52 static size_t cbGetBytes(void *info, void *buffer, size_t count)
53 {
54 QIODevice *dev = static_cast<QIODevice *>(info);
55 if (!dev || !buffer)
56 return 0;
57 qint64 res = dev->read(static_cast<char *>(buffer), qint64(count));
58 return size_t(qMax(qint64(0), res));
59 }
60
61 static off_t cbSkipForward(void *info, off_t count)
62 {
63 QIODevice *dev = static_cast<QIODevice *>(info);
64 if (!dev || count <= 0)
65 return 0;
66 qint64 res = 0;
67 if (!dev->isSequential()) {
68 qint64 prevPos = dev->pos();
69 dev->seek(prevPos + count);
70 res = dev->pos() - prevPos;
71 } else {
72 char *buf = new char[quint64(count)];
73 res = dev->read(buf, count);
74 delete[] buf;
75 }
76 return qMax(qint64(0), res);
77 }
78
79 static void cbRewind(void *)
80 {
81 // Ignore this; we do not want the Qt device to be rewound after reading the image
82 }
83
84 static size_t cbPutBytes(void *info, const void *buffer, size_t count)
85 {
86 QIODevice *dev = static_cast<QIODevice *>(info);
87 if (!dev || !buffer)
88 return 0;
89 qint64 res = dev->write(static_cast<const char *>(buffer), qint64(count));
90 return size_t(qMax(qint64(0), res));
91 }
92
93
94 // QImage <-> CGImage conversion functions from QtGui on darwin
95 CGImageRef qt_mac_toCGImage(const QImage &qImage);
96 QImage qt_mac_toQImage(CGImageRef image);
97
98 QImageIOPlugin::Capabilities QIIOFHelpers::systemCapabilities(const QString &uti)
99 {
100 QImageIOPlugin::Capabilities res;
101 QCFString cfUti(uti);
102
103 QCFType<CFArrayRef> cfSourceTypes = CGImageSourceCopyTypeIdentifiers();
104 CFIndex len = CFArrayGetCount(cfSourceTypes);
105 if (CFArrayContainsValue(cfSourceTypes, CFRangeMake(0, len), cfUti))
106 res |= QImageIOPlugin::CanRead;
107
108 QCFType<CFArrayRef> cfDestTypes = CGImageDestinationCopyTypeIdentifiers();
109 len = CFArrayGetCount(cfDestTypes);
110 if (CFArrayContainsValue(cfDestTypes, CFRangeMake(0, len), cfUti))
111 res |= QImageIOPlugin::CanWrite;
112
113 return res;
114 }
115
116 bool QIIOFHelpers::readImage(QImageIOHandler *q_ptr, QImage *out)
117 {
118 QIIOFHelper h(q_ptr);
119 return h.readImage(out);
120 }
121
122 bool QIIOFHelpers::writeImage(QImageIOHandler *q_ptr, const QImage &in, const QString &uti)
123 {
124 QIIOFHelper h(q_ptr);
125 return h.writeImage(in, uti);
126 }
127
128 QIIOFHelper::QIIOFHelper(QImageIOHandler *q)
129 : q_ptr(q)
130 {
131 }
132
133 bool QIIOFHelper::initRead()
134 {
135 static const CGDataProviderSequentialCallbacks cgCallbacks = { 0, &cbGetBytes, &cbSkipForward, &cbRewind, nullptr };
136
137 if (cgImageSource)
138 return true;
139 if (!q_ptr || !q_ptr->device())
140 return false;
141
142 if (QBuffer *b = qobject_cast<QBuffer *>(q_ptr->device())) {
143 // do direct access to avoid data copy
144 const void *rawData = b->data().constData() + b->pos();
145 cgDataProvider = CGDataProviderCreateWithData(nullptr, rawData, size_t(b->data().size() - b->pos()), nullptr);
146 } else {
147 cgDataProvider = CGDataProviderCreateSequential(q_ptr->device(), &cgCallbacks);
148 }
149
150 cgImageSource = CGImageSourceCreateWithDataProvider(cgDataProvider, nullptr);
151
152 if (cgImageSource)
153 cfImageDict = CGImageSourceCopyPropertiesAtIndex(cgImageSource, 0, nullptr);
154
155 return (cgImageSource);
156 }
157
158 bool QIIOFHelper::readImage(QImage *out)
159 {
160 if (!out || !initRead())
161 return false;
162
163 QCFType<CGImageRef> cgImage = CGImageSourceCreateImageAtIndex(cgImageSource, 0, nullptr);
164 if (!cgImage)
165 return false;
166
167 *out = qt_mac_toQImage(cgImage);
168 if (out->isNull())
169 return false;
170
171 int dpi = 0;
172 if (getIntProperty(kCGImagePropertyDPIWidth, &dpi))
173 out->setDotsPerMeterX(qRound(dpi / 0.0254f));
174 if (getIntProperty(kCGImagePropertyDPIHeight, &dpi))
175 out->setDotsPerMeterY(qRound(dpi / 0.0254f));
176
177 return true;
178 }
179
180 bool QIIOFHelper::getIntProperty(CFStringRef property, int *value)
181 {
182 if (!cfImageDict)
183 return false;
184
185 CFNumberRef cfNumber = static_cast<CFNumberRef>(CFDictionaryGetValue(cfImageDict, property));
186 if (cfNumber) {
187 int intVal;
188 if (CFNumberGetValue(cfNumber, kCFNumberIntType, &intVal)) {
189 if (value)
190 *value = intVal;
191 return true;
192 }
193 }
194 return false;
195 }
196
197 static QImageIOHandler::Transformations exif2Qt(int exifOrientation)
198 {
199 switch (exifOrientation) {
200 case 1: // normal
201 return QImageIOHandler::TransformationNone;
202 case 2: // mirror horizontal
203 return QImageIOHandler::TransformationMirror;
204 case 3: // rotate 180
205 return QImageIOHandler::TransformationRotate180;
206 case 4: // mirror vertical
207 return QImageIOHandler::TransformationFlip;
208 case 5: // mirror horizontal and rotate 270 CW
209 return QImageIOHandler::TransformationFlipAndRotate90;
210 case 6: // rotate 90 CW
211 return QImageIOHandler::TransformationRotate90;
212 case 7: // mirror horizontal and rotate 90 CW
213 return QImageIOHandler::TransformationMirrorAndRotate90;
214 case 8: // rotate 270 CW
215 return QImageIOHandler::TransformationRotate270;
216 }
217 return QImageIOHandler::TransformationNone;
218 }
219
220 static int qt2Exif(QImageIOHandler::Transformations transformation)
221 {
222 switch (transformation) {
223 case QImageIOHandler::TransformationNone:
224 return 1;
225 case QImageIOHandler::TransformationMirror:
226 return 2;
227 case QImageIOHandler::TransformationRotate180:
228 return 3;
229 case QImageIOHandler::TransformationFlip:
230 return 4;
231 case QImageIOHandler::TransformationFlipAndRotate90:
232 return 5;
233 case QImageIOHandler::TransformationRotate90:
234 return 6;
235 case QImageIOHandler::TransformationMirrorAndRotate90:
236 return 7;
237 case QImageIOHandler::TransformationRotate270:
238 return 8;
239 }
240 qWarning("Invalid Qt image transformation");
241 return 1;
242 }
243
244 QVariant QIIOFHelper::imageProperty(QImageIOHandler::ImageOption option)
245 {
246 if (!initRead())
247 return QVariant();
248
249 switch (option) {
250 case QImageIOHandler::Size: {
251 QSize sz;
252 if (getIntProperty(kCGImagePropertyPixelWidth, &sz.rwidth())
253 && getIntProperty(kCGImagePropertyPixelHeight, &sz.rheight())) {
254 return sz;
255 }
256 break;
257 }
258 case QImageIOHandler::ImageTransformation: {
259 int orient;
260 if (getIntProperty(kCGImagePropertyOrientation, &orient))
261 return int(exif2Qt(orient));
262 break;
263 }
264 default:
265 break;
266 }
267
268 return QVariant();
269 }
270
271 void QIIOFHelper::setOption(QImageIOHandler::ImageOption option, const QVariant &value)
272 {
273 if (writeOptions.size() < option + 1)
274 writeOptions.resize(option + 1);
275 writeOptions[option] = value;
276 }
277
278 bool QIIOFHelper::writeImage(const QImage &in, const QString &uti)
279 {
280 static const CGDataConsumerCallbacks cgCallbacks = { &cbPutBytes, nullptr };
281
282 if (!q_ptr || !q_ptr->device() || in.isNull())
283 return false;
284
285 QCFType<CGImageRef> cgImage = qt_mac_toCGImage(in);
286 QCFType<CGDataConsumerRef> cgDataConsumer = CGDataConsumerCreate(q_ptr->device(), &cgCallbacks);
287 QCFType<CFStringRef> cfUti = uti.toCFString();
288 QCFType<CGImageDestinationRef> cgImageDest = CGImageDestinationCreateWithDataConsumer(cgDataConsumer, cfUti, 1, nullptr);
289 if (!cgImageDest || !cgImage)
290 return false;
291
292 QCFType<CFNumberRef> cfQuality = nullptr;
293 QCFType<CFNumberRef> cfOrientation = nullptr;
294 const void *dictKeys[2];
295 const void *dictVals[2];
296 int dictSize = 0;
297
298 if (q_ptr->supportsOption(QImageIOHandler::Quality)) {
299 bool ok = false;
300 int writeQuality = writeOptions.value(QImageIOHandler::Quality).toInt(&ok);
301 // If quality is unset, default to 75%
302 float quality = (ok && writeQuality >= 0 ? (qMin(writeQuality, 100)) : 75) / 100.0f;
303 cfQuality = CFNumberCreate(nullptr, kCFNumberFloatType, &quality);
304 dictKeys[dictSize] = static_cast<const void *>(kCGImageDestinationLossyCompressionQuality);
305 dictVals[dictSize] = static_cast<const void *>(cfQuality);
306 dictSize++;
307 }
308 if (q_ptr->supportsOption(QImageIOHandler::ImageTransformation)) {
309 int orient = qt2Exif(static_cast<QImageIOHandler::Transformation>(writeOptions.value(QImageIOHandler::ImageTransformation).toInt()));
310 cfOrientation = CFNumberCreate(nullptr, kCFNumberIntType, &orient);
311 dictKeys[dictSize] = static_cast<const void *>(kCGImagePropertyOrientation);
312 dictVals[dictSize] = static_cast<const void *>(cfOrientation);
313 dictSize++;
314 }
315
316 QCFType<CFDictionaryRef> cfProps = nullptr;
317 if (dictSize)
318 cfProps = CFDictionaryCreate(nullptr, dictKeys, dictVals, dictSize,
319 &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
320
321 CGImageDestinationAddImage(cgImageDest, cgImage, cfProps);
322 return CGImageDestinationFinalize(cgImageDest);
323 }
324
325 QT_END_NAMESPACE