2023-02-22 01:48:49 +03:00
|
|
|
// SPDX-FileCopyrightText: Nheko Contributors
|
2021-03-05 02:35:15 +03:00
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2020-03-01 21:55:43 +03:00
|
|
|
#include "BlurhashProvider.h"
|
|
|
|
|
|
|
|
#include <QUrl>
|
|
|
|
|
|
|
|
#include "blurhash.hpp"
|
|
|
|
|
2020-03-04 03:56:58 +03:00
|
|
|
void
|
2021-12-21 14:17:12 +03:00
|
|
|
BlurhashRunnable::run()
|
2020-03-01 21:55:43 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
if (m_requestedSize.width() < 0 || m_requestedSize.height() < 0) {
|
2021-12-29 06:28:08 +03:00
|
|
|
emit error(QStringLiteral("Blurhash needs size request"));
|
2021-09-18 01:22:33 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (m_requestedSize.width() == 0 || m_requestedSize.height() == 0) {
|
2021-12-21 14:17:12 +03:00
|
|
|
auto image = QImage(m_requestedSize, QImage::Format_RGB32);
|
|
|
|
image.fill(QColor(0, 0, 0));
|
|
|
|
emit done(image);
|
2021-09-18 01:22:33 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-01-07 23:52:20 +03:00
|
|
|
auto blurhashDecodeSize = m_requestedSize;
|
|
|
|
if (blurhashDecodeSize.height() > 100 && blurhashDecodeSize.width() > 100) {
|
|
|
|
blurhashDecodeSize.scale(100, 100, Qt::AspectRatioMode::KeepAspectRatio);
|
|
|
|
}
|
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto decoded = blurhash::decode(QUrl::fromPercentEncoding(m_id.toUtf8()).toStdString(),
|
2024-01-07 23:52:20 +03:00
|
|
|
blurhashDecodeSize.width(),
|
|
|
|
blurhashDecodeSize.height());
|
2021-09-18 01:22:33 +03:00
|
|
|
if (decoded.image.empty()) {
|
2021-12-21 14:17:12 +03:00
|
|
|
emit error(QStringLiteral("Failed decode!"));
|
2021-09-18 01:22:33 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QImage image(decoded.image.data(),
|
|
|
|
(int)decoded.width,
|
|
|
|
(int)decoded.height,
|
|
|
|
(int)decoded.width * 3,
|
|
|
|
QImage::Format_RGB888);
|
|
|
|
|
2024-01-07 23:52:20 +03:00
|
|
|
image = image.scaled(m_requestedSize);
|
|
|
|
|
2021-12-30 04:25:47 +03:00
|
|
|
emit done(image.convertToFormat(QImage::Format_RGB32));
|
2020-03-01 21:55:43 +03:00
|
|
|
}
|