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
qsharedimageprovider.cpp
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// Qt-Security score:significant reason:default
4
5#include <qsharedimageprovider_p.h>
6#include <private/qquickpixmap_p.h>
7#include <private/qimage_p.h>
8#include <QImageReader>
9#include <QFileInfo>
10#include <QDir>
11
12Q_DECLARE_METATYPE(QQuickImageProviderOptions)
13
14QT_BEGIN_NAMESPACE
15
16QuickSharedImageLoader::QuickSharedImageLoader(QObject *parent) : QSharedImageLoader(parent) {}
17
18QImage QuickSharedImageLoader::loadFile(const QString &path, ImageParameters *params)
19{
20 QImageReader imgio(path);
21 QSize realSize = imgio.size();
22 QSize requestSize;
23 QQuickImageProviderOptions options;
24 if (params) {
25 requestSize = params->value(RequestedSize).toSize();
26 options = params->value(ProviderOptions).value<QQuickImageProviderOptions>();
27 }
28
29 QSize scSize = QQuickImageProviderWithOptions::loadSize(imgio.size(), requestSize, imgio.format(), options);
30
31 if (scSize.isValid())
32 imgio.setScaledSize(scSize);
33
34 QImage image;
35 if (imgio.read(&image)) {
36 if (realSize.isEmpty())
37 realSize = image.size();
38 // Make sure we have acceptable format for texture uploader, or it will convert & lose sharing
39 // This mimics the testing & conversion normally done by the quick pixmapcache & texturefactory
40 if (image.format() != QImage::Format_RGB32 && image.format() != QImage::Format_ARGB32_Premultiplied) {
41 QImage::Format newFmt = QImage::Format_RGB32;
42 if (image.hasAlphaChannel() && image.data_ptr()->checkForAlphaPixels())
43 newFmt = QImage::Format_ARGB32_Premultiplied;
44 qCDebug(lcSharedImage) << "Convert on load from format" << image.format() << "to" << newFmt;
45 image = image.convertToFormat(newFmt);
46 }
47 }
48
49 if (params && params->size() > OriginalSize)
50 params->replace(OriginalSize, realSize);
51
52 return image;
53}
54
55QString QuickSharedImageLoader::key(const QString &path, ImageParameters *params)
56{
57 QSize reqSz;
58 QQuickImageProviderOptions opts;
59 if (params) {
60 reqSz = params->value(RequestedSize).toSize();
61 opts = params->value(ProviderOptions).value<QQuickImageProviderOptions>();
62 }
63 if (!reqSz.isValid())
64 return path;
65 int aspect = opts.preserveAspectRatioCrop() || opts.preserveAspectRatioFit() ? 1 : 0;
66
67 QString key = path + QStringLiteral("_%1x%2_%3").arg(reqSz.width()).arg(reqSz.height()).arg(aspect);
68 qCDebug(lcSharedImage) << "KEY:" << key;
69 return key;
70}
71
72SharedImageProvider::SharedImageProvider()
73 : QQuickImageProviderWithOptions(QQuickImageProvider::Image), loader(new QuickSharedImageLoader)
74{
75}
76
77QImage SharedImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize, const QQuickImageProviderOptions &options)
78{
79 QFileInfo fi(QDir::root(), id);
80 QString path = fi.canonicalFilePath();
81 if (path.isEmpty())
82 return QImage();
83
84 QSharedImageLoader::ImageParameters params(QuickSharedImageLoader::NumImageParameters);
85 params[QuickSharedImageLoader::RequestedSize].setValue(requestedSize);
86 params[QuickSharedImageLoader::ProviderOptions].setValue(options);
87
88 QImage img = loader->load(path, &params);
89 if (img.isNull()) {
90 // May be sharing problem, fall back to normal local load
91 img = loader->loadFile(path, &params);
92 if (!img.isNull())
93 qCWarning(lcSharedImage) << "Sharing problem; loading" << id << "unshared";
94 }
95
96 //... QSize realSize = params.value(QSharedImageLoader::OriginalSize).toSize();
97 // quickpixmapcache's readImage() reports back the original size, prior to requestedSize scaling, in the *size
98 // parameter. That value is currently ignored by quick however, which only cares about the present size of the
99 // returned image. So handling and sharing of info on pre-scaled size is currently not implemented.
100 if (size) {
101 *size = img.size();
102 }
103
104 return img;
105}
106
107QT_END_NAMESPACE
108
109#include "moc_qsharedimageprovider_p.cpp"