summary refs log tree commit diff
path: root/src/BlurhashProvider.cpp
diff options
context:
space:
mode:
authorNicolas Werner <nicolas.werner@hotmail.de>2020-03-01 19:55:43 +0100
committerNicolas Werner <nicolas.werner@hotmail.de>2020-03-03 02:34:26 +0100
commit0fc98b26920961f4cf9002f0413684d9c18671cc (patch)
tree150152fdeaf1398a64618d77fc9ada6d48bed3ec /src/BlurhashProvider.cpp
parentFix avatar layering in room list (diff)
downloadnheko-0fc98b26920961f4cf9002f0413684d9c18671cc.tar.xz
Experimental blurhash implementation (MXC2448)
Diffstat (limited to 'src/BlurhashProvider.cpp')
-rw-r--r--src/BlurhashProvider.cpp42
1 files changed, 42 insertions, 0 deletions
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 <algorithm>
+
+#include <QUrl>
+
+#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;
+}