-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathqcimagepoolprovider.cpp
279 lines (209 loc) · 7.35 KB
/
qcimagepoolprovider.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* QuickCross Project
* License: APACHE-2.0
* Author: Ben Lau
* Project Site: https://github.com/benlau/quickcross
*
*/
#include <QtCore>
#include <QUrlQuery>
#include <QGuiApplication>
#include <QPainter>
#include "qcimagepoolprovider.h"
#include "qcimageloader.h"
#include "QCImagePool"
class QCImageProviderQueryID {
public:
QCImageProviderQueryID() {
scaleToFitDpi = false;
}
bool hasModification() const {
return scaleToFitDpi || tintColor.isValid() || !clipRect.isEmpty();
}
QString removeSuffix(const QString& input) const {
QStringList token = input.split(".");
return token[0];
}
void parse(QString id) {
QUrl inputUrl(id);
QUrl outputUrl;
fileName = removeSuffix(inputUrl.path());
QUrlQuery inputQuery (inputUrl);
QUrlQuery outputQuery;
outputUrl.setPath(fileName);
if (inputQuery.hasQueryItem("scaleToFitDpi")) {
QVariant v = inputQuery.queryItemValue("scaleToFitDpi");
scaleToFitDpi = v.toBool();
v = scaleToFitDpi;
outputQuery.addQueryItem("scaleToFitDpi", v.toString());
}
if (inputQuery.hasQueryItem("clip")) {
QVariant v= inputQuery.queryItemValue("clip");
QStringList token = v.toString().split(",");
if (token.size() != 4) {
qWarning() << "Invalid clip parameter:" << token;
} else {
QList<int> coordination;
foreach(QString str, token) {
bool ok;
int i = str.toInt(&ok);
if (ok) {
coordination << i;
} else {
qWarning() << "Invalid clip parameter:" << token;
}
}
if (coordination.size() == 4) {
clipRect = QRect(coordination[0],
coordination[1],
coordination[2],
coordination[3]);
outputQuery.addQueryItem("clip", v.toString());
}
}
}
if (inputQuery.hasQueryItem("tintColor")) {
QVariant v = inputQuery.queryItemValue("tintColor");
if (QColor::isValidColor(v.toString())) {
tintColor = QColor(v.toString());
outputQuery.addQueryItem("tintColor", inputQuery.queryItemValue("tintColor"));
}
}
outputUrl.setQuery(outputQuery.query());
cacheKey = outputUrl.toString();
}
QString fileName;
bool scaleToFitDpi;
QString cacheKey;
QColor tintColor;
QRect clipRect;
};
/// Copy from QuickAndroid project
static void gray(QImage& dest,QImage& src)
{
for (int y = 0; y < src.height(); ++y) {
unsigned int *data = (unsigned int *)src.scanLine(y);
unsigned int *outData = (unsigned int*)dest.scanLine(y);
for (int x = 0 ; x < src.width(); x++) {
int val = qGray(data[x]);
outData[x] = qRgba(val,val,val,qAlpha(data[x]));
}
}
}
/// Copy from QuickAndroid project
static QImage colorize(QImage src, QColor tintColor) {
if (src.format() != QImage::Format_ARGB32) {
src = src.convertToFormat(QImage::Format_ARGB32);
}
QImage dst = QImage(src.size(), src.format());
gray(dst,src);
QPainter painter(&dst);
QColor pureColor = tintColor;
pureColor.setAlpha(255);
painter.setCompositionMode(QPainter::CompositionMode_Screen);
painter.fillRect(0,0,dst.width(),dst.height(),pureColor);
painter.end();
if (tintColor.alpha() != 255) {
QImage buffer = QImage(src.size(), src.format());
buffer.fill(QColor("transparent"));
QPainter bufPainter(&buffer);
qreal opacity = tintColor.alpha() / 255.0;
bufPainter.setOpacity(opacity);
bufPainter.drawImage(0,0,dst);
bufPainter.end();
dst = buffer;
}
if (src.hasAlphaChannel()) {
dst.setAlphaChannel(src.alphaChannel());
}
dst.setDevicePixelRatio(src.devicePixelRatio());
return dst;
}
static QImage process(const QCImageProviderQueryID& query, QImage image, qreal devicePixelRatio) {
QImage output = image;
if (query.scaleToFitDpi) {
if (output.devicePixelRatio() != devicePixelRatio) {
qreal ratio = devicePixelRatio / output.devicePixelRatio();
output = output.scaled(output.width() * ratio, output.height() * ratio, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
output.setDevicePixelRatio(devicePixelRatio);
}
}
if (!query.clipRect.isEmpty() ) {
output = output.copy(query.clipRect);
}
if (query.tintColor.isValid()) {
output = colorize(output, query.tintColor);
}
return output;
}
/*! \class QCImageProvider
\inmodule QuickCross
QCImageProvider is an implementation of QQuickImageProvider that provides an interface for supporint image data in QML.
This class offeres following features:
1. Preload all image at startup via QCImageLoader
2. Support @2x , @3x image format
3. Perform image transformation by query string.
3.1. tintColor - Alters the colors of the source item by applying an tint color.
3.2. scaleToFitDpi - Scale up / down the source image to fit current DPI
3.3. clip - Returns a sub-area of the source image
Example:
\code
Image {
// Convert a logo to white color
source: "image://custom/qt-logo-medium?tintColor=" + escape("#FFFFFF");
}
Image {
// Scale the image to fit current DPI. It is useful for handling BorderImage
source: "image://custom/qt-logo-medium?scaleToFitDpi=true"
}
Image {
// clip part of the image (x,y,width,height)
source: "image://custom/qt-logo-medium?clip=10,20,100,100";
}
\endcode
Setup:
\code
QCImageLoader *loader = QCImageLoader::instance();
loader->load(":/img"); // preload all image from ":/img"
loader->waitForLoaded();
QQmlApplicationEngine engine;
engine.addImageProvider("custom", new QCImageProvider());
\endcode
*/
QCImagePoolProvider::QCImagePoolProvider() : QQuickImageProvider(QQmlImageProviderBase::Image)
{
m_cache.setMaxCost(1024 * 1024 * 10);
QGuiApplication* app = qobject_cast<QGuiApplication*>(QGuiApplication::instance());
devicePixelRatio = app->devicePixelRatio();
}
QImage QCImagePoolProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
{
Q_UNUSED(requestedSize);
QCImagePool* pool = QCImagePool::instance();
QImage result;
QCImageProviderQueryID query;
query.parse(id);
if (query.hasModification()) {
mutex.lock();
if (m_cache.contains(query.cacheKey)) {
result = *m_cache.object((query.cacheKey));
} else if (pool->contains(query.fileName)){
result = process(query,pool->image(query.fileName), devicePixelRatio);
m_cache.insert(query.cacheKey, new QImage(result), result.bytesPerLine() * result.height());
}
mutex.unlock();
} else if (pool->contains(query.fileName)) {
result = pool->image(query.fileName);
}
if (!result.isNull()) {
*size = result.size();
}
return result;
}
int QCImagePoolProvider::cacheSize() const
{
return m_cache.maxCost();
}
void QCImagePoolProvider::setCacheSize(int cacheSize)
{
m_cache.setMaxCost(cacheSize);
}