summary refs log tree commit diff
path: root/src/BlurhashProvider.cpp
blob: 1fdc931996bf77d7dfa1d130cec28199624ba3c0 (plain) (blame)
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
// SPDX-FileCopyrightText: Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include "BlurhashProvider.h"

#include <QUrl>

#include "blurhash.hpp"

void
BlurhashRunnable::run()
{
    if (m_requestedSize.width() < 0 || m_requestedSize.height() < 0) {
        emit error(QStringLiteral("Blurhash needs size request"));
        return;
    }
    if (m_requestedSize.width() == 0 || m_requestedSize.height() == 0) {
        auto image = QImage(m_requestedSize, QImage::Format_RGB32);
        image.fill(QColor(0, 0, 0));
        emit done(image);
        return;
    }

    auto blurhashDecodeSize = m_requestedSize;
    if (blurhashDecodeSize.height() > 100 && blurhashDecodeSize.width() > 100) {
        blurhashDecodeSize.scale(100, 100, Qt::AspectRatioMode::KeepAspectRatio);
    }

    auto decoded = blurhash::decode(QUrl::fromPercentEncoding(m_id.toUtf8()).toStdString(),
                                    blurhashDecodeSize.width(),
                                    blurhashDecodeSize.height());
    if (decoded.image.empty()) {
        emit error(QStringLiteral("Failed decode!"));
        return;
    }

    QImage image(decoded.image.data(),
                 (int)decoded.width,
                 (int)decoded.height,
                 (int)decoded.width * 3,
                 QImage::Format_RGB888);

    image = image.scaled(m_requestedSize);

    emit done(image.convertToFormat(QImage::Format_RGB32));
}