-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageloader.cpp
More file actions
157 lines (140 loc) · 4.66 KB
/
Copy pathimageloader.cpp
File metadata and controls
157 lines (140 loc) · 4.66 KB
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
#include "imageloader.h"
#include <QBuffer>
#include <QDebug>
#include <QFile>
#include <QFileInfo>
#include <QImageReader>
#include <QMutexLocker>
#include <libraw/libraw.h>
#include <memory>
namespace {
constexpr int kMaxDim = 1024;
QImage makePlaceholder()
{
QImage img(kMaxDim, (kMaxDim * 2) / 3, QImage::Format_RGB32);
img.fill(Qt::black);
return img;
}
}
imageLoader::imageLoader(QObject *parent)
: QObject(parent)
{
// ~100 MB of decoded previews (cost unit is KB); a 1024px RGB32 image
// is ~4 MB, so roughly the last 25 images stay instant.
m_cache.setMaxCost(100 * 1024);
}
void imageLoader::requestImage(const QString &path)
{
QMutexLocker lock(&m_mutex);
m_pending = path;
scheduleWake();
}
void imageLoader::requestPrefetch(const QStringList &paths)
{
QMutexLocker lock(&m_mutex);
m_prefetch = paths;
scheduleWake();
}
void imageLoader::clearCache()
{
m_cache.clear();
}
void imageLoader::scheduleWake()
{
if (m_wakeQueued)
return;
m_wakeQueued = true;
QMetaObject::invokeMethod(this, &imageLoader::processRequests, Qt::QueuedConnection);
}
void imageLoader::processRequests()
{
for (;;) {
QString path;
bool prefetchOnly = false;
{
QMutexLocker lock(&m_mutex);
if (!m_pending.isEmpty()) {
path = m_pending;
m_pending.clear();
} else if (!m_prefetch.isEmpty()) {
path = m_prefetch.takeFirst();
prefetchOnly = true;
} else {
m_wakeQueued = false;
return;
}
}
if (QImage *cached = m_cache.object(path)) {
if (!prefetchOnly)
emit imageLoaded(path, *cached, false);
continue;
}
bool failed = false;
const QImage img = loadFromDisk(path, &failed);
if (!failed) {
const qsizetype costKb = qMax<qsizetype>(1, img.sizeInBytes() / 1024);
m_cache.insert(path, new QImage(img), costKb);
}
if (!prefetchOnly)
emit imageLoaded(path, img, failed);
}
}
QImage imageLoader::loadFromDisk(const QString &path, bool *failed)
{
*failed = false;
QImage thumbnail;
const QString suffix = QFileInfo(path).suffix().toLower();
const QList<QByteArray> supported = QImageReader::supportedImageFormats();
if (supported.contains(suffix.toLocal8Bit())) {
QImageReader reader(path);
reader.setAutoTransform(true); // honor EXIF orientation
// Read at most kMaxDim in either dimension to save memory/time
const QSize origSize = reader.size();
if (origSize.isValid()) {
QSize target = origSize;
target.scale(kMaxDim, kMaxDim, Qt::KeepAspectRatio);
if (target != origSize)
reader.setScaledSize(target);
}
if (!reader.read(&thumbnail)) {
// Fallback: try basic QImage load
QImage img(path);
if (!img.isNull())
thumbnail = img;
}
} else {
std::unique_ptr<LibRaw> rawProc(new LibRaw());
const QByteArray encodedPath = QFile::encodeName(path);
if (LIBRAW_SUCCESS == rawProc->open_file(encodedPath.constData())) {
int ret = LIBRAW_SUCCESS;
if (!rawProc->imgdata.thumbnail.thumb)
ret = rawProc->unpack_thumb();
if (ret == LIBRAW_SUCCESS && rawProc->imgdata.thumbnail.thumb
&& rawProc->imgdata.thumbnail.tlength > 0) {
QByteArray thumbData(reinterpret_cast<const char *>(
rawProc->imgdata.thumbnail.thumb),
static_cast<int>(rawProc->imgdata.thumbnail.tlength));
QBuffer buf(&thumbData);
buf.open(QIODevice::ReadOnly);
QImageReader r(&buf);
r.setDecideFormatFromContent(true);
r.setAutoTransform(true);
if (!r.read(&thumbnail)) {
QImage tmp;
if (tmp.loadFromData(reinterpret_cast<const uchar *>(thumbData.constData()),
thumbData.size()))
thumbnail = tmp;
}
}
}
}
if (thumbnail.isNull()) {
*failed = true;
return makePlaceholder();
}
// Scale once, and only when actually larger than the preview size
if (thumbnail.width() > kMaxDim || thumbnail.height() > kMaxDim)
thumbnail = thumbnail.scaled(kMaxDim, kMaxDim, Qt::KeepAspectRatio,
Qt::SmoothTransformation);
return thumbnail;
}