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 plugins 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 <sharedimageprovider.h>
41#include <qsharedimageloader_p.h>
42#include <private/qquickpixmapcache_p.h>
43#include <private/qimage_p.h>
44#include <QImageReader>
45#include <QFileInfo>
46#include <QDir>
47
48Q_DECLARE_METATYPE(QQuickImageProviderOptions)
49
50class QuickSharedImageLoader : public QSharedImageLoader
51{
52 Q_OBJECT
53 friend class SharedImageProvider;
54
55public:
56 enum ImageParameter {
57 OriginalSize = 0,
58 RequestedSize,
59 ProviderOptions,
60 NumImageParameters
61 };
62
63 QuickSharedImageLoader(QObject *parent = nullptr)
64 : QSharedImageLoader(parent)
65 {
66 }
67
68protected:
69 QImage loadFile(const QString &path, ImageParameters *params) override
70 {
71 QImageReader imgio(path);
72 QSize realSize = imgio.size();
73 QSize requestSize;
74 QQuickImageProviderOptions options;
75 if (params) {
76 requestSize = params->value(i: RequestedSize).toSize();
77 options = params->value(i: ProviderOptions).value<QQuickImageProviderOptions>();
78 }
79
80 QSize scSize = QQuickImageProviderWithOptions::loadSize(originalSize: imgio.size(), requestedSize: requestSize, format: imgio.format(), options);
81
82 if (scSize.isValid())
83 imgio.setScaledSize(scSize);
84
85 QImage image;
86 if (imgio.read(image: &image)) {
87 if (realSize.isEmpty())
88 realSize = image.size();
89 // Make sure we have acceptable format for texture uploader, or it will convert & lose sharing
90 // This mimics the testing & conversion normally done by the quick pixmapcache & texturefactory
91 if (image.format() != QImage::Format_RGB32 && image.format() != QImage::Format_ARGB32_Premultiplied) {
92 QImage::Format newFmt = QImage::Format_RGB32;
93 if (image.hasAlphaChannel() && image.data_ptr()->checkForAlphaPixels())
94 newFmt = QImage::Format_ARGB32_Premultiplied;
95 qCDebug(lcSharedImage) << "Convert on load from format" << image.format() << "to" << newFmt;
96 image = image.convertToFormat(f: newFmt);
97 }
98 }
99
100 if (params && params->count() > OriginalSize)
101 params->replace(i: OriginalSize, t: realSize);
102
103 return image;
104 }
105
106 QString key(const QString &path, ImageParameters *params) override
107 {
108 QSize reqSz;
109 QQuickImageProviderOptions opts;
110 if (params) {
111 reqSz = params->value(i: RequestedSize).toSize();
112 opts = params->value(i: ProviderOptions).value<QQuickImageProviderOptions>();
113 }
114 if (!reqSz.isValid())
115 return path;
116 int aspect = opts.preserveAspectRatioCrop() || opts.preserveAspectRatioFit() ? 1 : 0;
117
118 QString key = path + QStringLiteral("_%1x%2_%3").arg(a: reqSz.width()).arg(a: reqSz.height()).arg(a: aspect);
119 qCDebug(lcSharedImage) << "KEY:" << key;
120 return key;
121 }
122};
123
124
125SharedImageProvider::SharedImageProvider()
126 : QQuickImageProviderWithOptions(QQuickImageProvider::Image), loader(new QuickSharedImageLoader)
127{
128}
129
130QImage SharedImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize, const QQuickImageProviderOptions &options)
131{
132 QFileInfo fi(QDir::root(), id);
133 QString path = fi.canonicalFilePath();
134 if (path.isEmpty())
135 return QImage();
136
137 QSharedImageLoader::ImageParameters params(QuickSharedImageLoader::NumImageParameters);
138 params[QuickSharedImageLoader::RequestedSize].setValue(requestedSize);
139 params[QuickSharedImageLoader::ProviderOptions].setValue(options);
140
141 QImage img = loader->load(path, params: &params);
142 if (img.isNull()) {
143 // May be sharing problem, fall back to normal local load
144 img = loader->loadFile(path, params: &params);
145 if (!img.isNull())
146 qCWarning(lcSharedImage) << "Sharing problem; loading" << id << "unshared";
147 }
148
149 //... QSize realSize = params.value(QSharedImageLoader::OriginalSize).toSize();
150 // quickpixmapcache's readImage() reports back the original size, prior to requestedSize scaling, in the *size
151 // parameter. That value is currently ignored by quick however, which only cares about the present size of the
152 // returned image. So handling and sharing of info on pre-scaled size is currently not implemented.
153 if (size) {
154 *size = img.size();
155 }
156
157 return img;
158}
159
160#include "sharedimageprovider.moc"
161

source code of qtdeclarative/src/imports/sharedimage/sharedimageprovider.cpp