From 0fc98b26920961f4cf9002f0413684d9c18671cc Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Sun, 1 Mar 2020 19:55:43 +0100 Subject: Experimental blurhash implementation (MXC2448) --- src/BlurhashProvider.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/BlurhashProvider.cpp (limited to 'src/BlurhashProvider.cpp') diff --git a/src/BlurhashProvider.cpp b/src/BlurhashProvider.cpp new file mode 100644 index 00000000..a5530a98 --- /dev/null +++ b/src/BlurhashProvider.cpp @@ -0,0 +1,42 @@ +#include "BlurhashProvider.h" + +#include + +#include + +#include "blurhash.hpp" + +QImage +BlurhashProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize) +{ + QSize sz = requestedSize; + if (sz.width() < 1 || sz.height() < 1) + return QImage(); + + if (size) + *size = sz; + + auto decoded = blurhash::decode( + QUrl::fromPercentEncoding(id.toUtf8()).toStdString(), sz.width(), sz.height()); + if (decoded.image.empty()) { + *size = QSize(); + return QImage(); + } + + QImage image(sz, QImage::Format_RGB888); + + for (int y = 0; y < sz.height(); y++) { + for (int x = 0; x < sz.width(); x++) { + int base = (y * sz.width() + x) * 3; + image.setPixel(x, + y, + qRgb(decoded.image[base], + decoded.image[base + 1], + decoded.image[base + 2])); + } + } + + // std::copy(decoded.image.begin(), decoded.image.end(), image.bits()); + + return image; +} -- cgit 1.5.1 From 5ac18f1f5f758c28316791dc9aed8e216291dd38 Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Wed, 4 Mar 2020 01:30:43 +0100 Subject: Speed up blurhash code --- src/BlurhashProvider.cpp | 19 +++---------------- src/ChatPage.cpp | 2 ++ third_party/blurhash/blurhash.cpp | 5 ++++- third_party/blurhash/blurhash.hpp | 2 +- 4 files changed, 10 insertions(+), 18 deletions(-) (limited to 'src/BlurhashProvider.cpp') diff --git a/src/BlurhashProvider.cpp b/src/BlurhashProvider.cpp index a5530a98..87d1f51b 100644 --- a/src/BlurhashProvider.cpp +++ b/src/BlurhashProvider.cpp @@ -17,26 +17,13 @@ BlurhashProvider::requestImage(const QString &id, QSize *size, const QSize &requ *size = sz; auto decoded = blurhash::decode( - QUrl::fromPercentEncoding(id.toUtf8()).toStdString(), sz.width(), sz.height()); + QUrl::fromPercentEncoding(id.toUtf8()).toStdString(), sz.width(), sz.height(), 4); if (decoded.image.empty()) { *size = QSize(); return QImage(); } - QImage image(sz, QImage::Format_RGB888); - - for (int y = 0; y < sz.height(); y++) { - for (int x = 0; x < sz.width(); x++) { - int base = (y * sz.width() + x) * 3; - image.setPixel(x, - y, - qRgb(decoded.image[base], - decoded.image[base + 1], - decoded.image[base + 2])); - } - } - - // std::copy(decoded.image.begin(), decoded.image.end(), image.bits()); + QImage image(decoded.image.data(), decoded.width, decoded.height, QImage::Format_RGB32); - return image; + return image.copy(); } diff --git a/src/ChatPage.cpp b/src/ChatPage.cpp index 6a7d984c..698a4ae2 100644 --- a/src/ChatPage.cpp +++ b/src/ChatPage.cpp @@ -332,6 +332,8 @@ ChatPage::ChatPage(QSharedPointer userSettings, QWidget *parent) QImage img; img.loadFromData(bin); + if (img.height() > 200 && img.width() > 360) + img = img.scaled(360, 200, Qt::KeepAspectRatioByExpanding); std::vector data; for (int y = 0; y < img.height(); y++) { for (int x = 0; x < img.width(); x++) { diff --git a/third_party/blurhash/blurhash.cpp b/third_party/blurhash/blurhash.cpp index 0ff6cb74..cd0a18a4 100644 --- a/third_party/blurhash/blurhash.cpp +++ b/third_party/blurhash/blurhash.cpp @@ -251,7 +251,7 @@ multiplyBasisFunction(Components components, int width, int height, unsigned cha namespace blurhash { Image -decode(std::string_view blurhash, size_t width, size_t height) +decode(std::string_view blurhash, size_t width, size_t height, size_t bytesPerPixel) { Image i{}; @@ -295,6 +295,9 @@ decode(std::string_view blurhash, size_t width, size_t height) i.image.push_back(static_cast(linearToSrgb(c.r))); i.image.push_back(static_cast(linearToSrgb(c.g))); i.image.push_back(static_cast(linearToSrgb(c.b))); + + for (size_t p = 3; p < bytesPerPixel; p++) + i.image.push_back(255); } } diff --git a/third_party/blurhash/blurhash.hpp b/third_party/blurhash/blurhash.hpp index 5077f0d5..e01b9b3f 100644 --- a/third_party/blurhash/blurhash.hpp +++ b/third_party/blurhash/blurhash.hpp @@ -13,7 +13,7 @@ struct Image // Decode a blurhash to an image with size width*height Image -decode(std::string_view blurhash, size_t width, size_t height); +decode(std::string_view blurhash, size_t width, size_t height, size_t bytesPerPixel = 3); // Encode an image of rgb pixels (without padding) with size width*height into a blurhash with x*y // components -- cgit 1.5.1 From b894ce4dcd58e337a14b954a0d34997264401df2 Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Wed, 4 Mar 2020 01:56:58 +0100 Subject: Make blurhash provider async --- CMakeLists.txt | 1 + resources/qml/delegates/ImageMessage.qml | 1 - src/BlurhashProvider.cpp | 35 +++++++++++++-------- src/BlurhashProvider.h | 52 ++++++++++++++++++++++++++++---- 4 files changed, 69 insertions(+), 20 deletions(-) (limited to 'src/BlurhashProvider.cpp') diff --git a/CMakeLists.txt b/CMakeLists.txt index 5561fc8d..8d0e4477 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -479,6 +479,7 @@ qt5_wrap_cpp(MOC_HEADERS src/notifications/Manager.h src/AvatarProvider.h + src/BlurhashProvider.h src/Cache_p.h src/ChatPage.h src/CommunitiesList.h diff --git a/resources/qml/delegates/ImageMessage.qml b/resources/qml/delegates/ImageMessage.qml index 62cae42c..65e1c454 100644 --- a/resources/qml/delegates/ImageMessage.qml +++ b/resources/qml/delegates/ImageMessage.qml @@ -20,7 +20,6 @@ Item { asynchronous: true fillMode: Image.PreserveAspectFit - sourceSize.width: parent.width sourceSize.height: parent.height } diff --git a/src/BlurhashProvider.cpp b/src/BlurhashProvider.cpp index 87d1f51b..08dc2d40 100644 --- a/src/BlurhashProvider.cpp +++ b/src/BlurhashProvider.cpp @@ -6,24 +6,33 @@ #include "blurhash.hpp" -QImage -BlurhashProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize) +void +BlurhashResponse::run() { - QSize sz = requestedSize; - if (sz.width() < 1 || sz.height() < 1) - return QImage(); - - if (size) - *size = sz; + if (m_requestedSize.width() < 0 || m_requestedSize.height() < 0) { + m_error = QStringLiteral("Blurhash needs size request"); + emit finished(); + return; + } + if (m_requestedSize.width() == 0 || m_requestedSize.height() == 0) { + m_image = QImage(m_requestedSize, QImage::Format_RGB32); + m_image.fill(QColor(0, 0, 0)); + emit finished(); + return; + } - auto decoded = blurhash::decode( - QUrl::fromPercentEncoding(id.toUtf8()).toStdString(), sz.width(), sz.height(), 4); + auto decoded = blurhash::decode(QUrl::fromPercentEncoding(m_id.toUtf8()).toStdString(), + m_requestedSize.width(), + m_requestedSize.height(), + 4); if (decoded.image.empty()) { - *size = QSize(); - return QImage(); + m_error = QStringLiteral("Failed decode!"); + emit finished(); + return; } QImage image(decoded.image.data(), decoded.width, decoded.height, QImage::Format_RGB32); - return image.copy(); + m_image = image.copy(); + emit finished(); } diff --git a/src/BlurhashProvider.h b/src/BlurhashProvider.h index b05fff59..48c945de 100644 --- a/src/BlurhashProvider.h +++ b/src/BlurhashProvider.h @@ -1,11 +1,51 @@ -#include +#pragma once -class BlurhashProvider : public QQuickImageProvider +#include +#include + +#include +#include + +class BlurhashResponse + : public QQuickImageResponse + , public QRunnable { public: - BlurhashProvider() - : QQuickImageProvider(QQuickImageProvider::Image) - {} + BlurhashResponse(const QString &id, const QSize &requestedSize) + + : m_id(id) + , m_requestedSize(requestedSize) + { + setAutoDelete(false); + } + + QQuickTextureFactory *textureFactory() const override + { + return QQuickTextureFactory::textureFactoryForImage(m_image); + } + QString errorString() const override { return m_error; } + + void run() override; + + QString m_id, m_error; + QSize m_requestedSize; + QImage m_image; +}; + +class BlurhashProvider + : public QObject + , public QQuickAsyncImageProvider +{ + Q_OBJECT +public slots: + QQuickImageResponse *requestImageResponse(const QString &id, + const QSize &requestedSize) override + { + BlurhashResponse *response = new BlurhashResponse(id, requestedSize); + pool.start(response); + return response; + } - QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize) override; +private: + QThreadPool pool; }; -- cgit 1.5.1