From 7086e23bdd73143529882e221b8f0963c8a7e82c Mon Sep 17 00:00:00 2001 From: tastytea Date: Mon, 27 Dec 2021 00:17:27 +0100 Subject: Request inline images in the right size and anti-alias them - If an inline image has specified a height, add parameters to the image:// URI. - Add scaled to the parameters, the images would be cropped otherwise. - Extract the height from image:// URI and use it for requestSize. - Use scaledToHeight instead of scaled. --- src/MxcImageProvider.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'src/MxcImageProvider.cpp') diff --git a/src/MxcImageProvider.cpp b/src/MxcImageProvider.cpp index 4834c2fc..97cbfff3 100644 --- a/src/MxcImageProvider.cpp +++ b/src/MxcImageProvider.cpp @@ -27,6 +27,7 @@ MxcImageProvider::requestImageResponse(const QString &id, const QSize &requested auto id_ = id; bool crop = true; double radius = 0; + auto size = requestedSize; auto queryStart = id.lastIndexOf('?'); if (queryStart != -1) { @@ -39,11 +40,14 @@ MxcImageProvider::requestImageResponse(const QString &id, const QSize &requested crop = false; } else if (b.startsWith("radius=")) { radius = b.mid(7).toDouble(); + } else if (b.startsWith("height=")) { + size.setHeight(b.mid(7).toInt()); + size.setWidth(0); } } } - return new MxcImageResponse(id_, crop, radius, requestedSize); + return new MxcImageResponse(id_, crop, radius, size); } void @@ -120,7 +124,9 @@ MxcImageProvider::download(const QString &id, if (fileInfo.exists()) { QImage image = utils::readImageFromFile(fileInfo.absoluteFilePath()); if (!image.isNull()) { - image = image.scaled(requestedSize, Qt::KeepAspectRatio, Qt::SmoothTransformation); + if (requestedSize != image.size()) { + image = image.scaledToHeight(requestedSize.height(), Qt::SmoothTransformation); + } if (radius != 0) { image = clipRadius(std::move(image), radius); @@ -151,8 +157,10 @@ MxcImageProvider::download(const QString &id, auto data = QByteArray(res.data(), (int)res.size()); QImage image = utils::readImage(data); if (!image.isNull()) { - image = - image.scaled(requestedSize, Qt::KeepAspectRatio, Qt::SmoothTransformation); + if (requestedSize != image.size()) { + image = + image.scaledToHeight(requestedSize.height(), Qt::SmoothTransformation); + } if (radius != 0) { image = clipRadius(std::move(image), radius); -- cgit 1.5.1 From 5ac11954027a3209bc5820db8219a76de7b798a5 Mon Sep 17 00:00:00 2001 From: tastytea Date: Mon, 27 Dec 2021 22:59:51 +0100 Subject: MxcImageProvider: Only use scaledToHeight if width <= 0. --- src/MxcImageProvider.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'src/MxcImageProvider.cpp') diff --git a/src/MxcImageProvider.cpp b/src/MxcImageProvider.cpp index 97cbfff3..74b4bedc 100644 --- a/src/MxcImageProvider.cpp +++ b/src/MxcImageProvider.cpp @@ -124,8 +124,11 @@ MxcImageProvider::download(const QString &id, if (fileInfo.exists()) { QImage image = utils::readImageFromFile(fileInfo.absoluteFilePath()); if (!image.isNull()) { - if (requestedSize != image.size()) { + if (requestedSize.width() <= 0) { image = image.scaledToHeight(requestedSize.height(), Qt::SmoothTransformation); + } else { + image = + image.scaled(requestedSize, Qt::KeepAspectRatio, Qt::SmoothTransformation); } if (radius != 0) { @@ -157,9 +160,12 @@ MxcImageProvider::download(const QString &id, auto data = QByteArray(res.data(), (int)res.size()); QImage image = utils::readImage(data); if (!image.isNull()) { - if (requestedSize != image.size()) { + if (requestedSize.width() <= 0) { image = image.scaledToHeight(requestedSize.height(), Qt::SmoothTransformation); + } else { + image = + image.scaled(requestedSize, Qt::KeepAspectRatio, Qt::SmoothTransformation); } if (radius != 0) { -- cgit 1.5.1