summaryrefslogtreecommitdiffstats
path: root/src/MxcImageProvider.cpp
diff options
context:
space:
mode:
authorbrausepulver <34420739+brausepulver@users.noreply.github.com>2022-06-26 20:32:43 +0200
committerGitHub <noreply@github.com>2022-06-26 18:32:43 +0000
commit98de51042a4a28ddc8de48a185ab7626def32a8d (patch)
tree440dad7b7c99daf22efd727bfacf1311b7a6bce7 /src/MxcImageProvider.cpp
parente16d297d0c7c57099e26c38b25ac94da46a8e63c (diff)
Get large avatar images as scale and crop locally (#1107)
Resolves #1069 The Matrix spec requires servers to provide thumbnails at (96x96, crop) and (320x240, scale) among others. [1] The avatars in Nheko's global/room profile and room settings are sized 130x130 on normal scaling and 260x260 on 2x scaling like on a HiDPI device. In both cases the avatar is requested as cropped and that way displayed at 96x96, making it look blurry. This can be solved by requesting scaled avatars rather than cropped where appropriate, and cropping to the requested size afterwards. HiDPI can be simulated in Qt by setting QT_SCALE_FACTOR=2. [1] https://spec.matrix.org/v1.3/client-server-api/#thumbnails
Diffstat (limited to 'src/MxcImageProvider.cpp')
-rw-r--r--src/MxcImageProvider.cpp36
1 files changed, 29 insertions, 7 deletions
diff --git a/src/MxcImageProvider.cpp b/src/MxcImageProvider.cpp
index 6098f0c4..3022ca3d 100644
--- a/src/MxcImageProvider.cpp
+++ b/src/MxcImageProvider.cpp
@@ -110,6 +110,12 @@ MxcImageProvider::download(const QString &id,
return;
}
+ bool cropLocally = false;
+ if (crop && requestedSize.width() > 96) {
+ crop = false;
+ cropLocally = true;
+ }
+
std::optional<mtx::crypto::EncryptedFile> encryptionInfo;
auto temp = infos.find("mxc://" + id);
if (temp != infos.end())
@@ -126,7 +132,7 @@ MxcImageProvider::download(const QString &id,
.arg(requestedSize.width())
.arg(requestedSize.height())
.arg(crop ? "crop" : "scale")
- .arg(radius);
+ .arg(cropLocally ? 0 : radius);
QFileInfo fileInfo(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) +
"/media_cache",
fileName);
@@ -138,8 +144,16 @@ MxcImageProvider::download(const QString &id,
if (requestedSize.width() <= 0) {
image = image.scaledToHeight(requestedSize.height(), Qt::SmoothTransformation);
} else {
- image =
- image.scaled(requestedSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
+ image = image.scaled(requestedSize,
+ cropLocally ? Qt::KeepAspectRatioByExpanding
+ : Qt::KeepAspectRatio,
+ Qt::SmoothTransformation);
+ if (cropLocally) {
+ image = image.copy((image.width() - requestedSize.width()) / 2,
+ (image.height() - requestedSize.height()) / 2,
+ requestedSize.width(),
+ requestedSize.height());
+ }
}
if (radius != 0) {
@@ -160,8 +174,8 @@ MxcImageProvider::download(const QString &id,
opts.method = crop ? "crop" : "scale";
http::client()->get_thumbnail(
opts,
- [fileInfo, requestedSize, radius, then, id, crop](const std::string &res,
- mtx::http::RequestErr err) {
+ [fileInfo, requestedSize, radius, then, id, crop, cropLocally](
+ const std::string &res, mtx::http::RequestErr err) {
if (err || res.empty()) {
download(id, QSize(), then, crop, radius);
return;
@@ -174,8 +188,16 @@ MxcImageProvider::download(const QString &id,
image =
image.scaledToHeight(requestedSize.height(), Qt::SmoothTransformation);
} else {
- image =
- image.scaled(requestedSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
+ image = image.scaled(requestedSize,
+ cropLocally ? Qt::KeepAspectRatioByExpanding
+ : Qt::KeepAspectRatio,
+ Qt::SmoothTransformation);
+ if (cropLocally) {
+ image = image.copy((image.width() - requestedSize.width()) / 2,
+ (image.height() - requestedSize.height()) / 2,
+ requestedSize.width(),
+ requestedSize.height());
+ }
}
if (radius != 0) {