| 1 | // SPDX-FileCopyrightText: Nheko Contributors |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #include "BlurhashProvider.h" |
| 6 | |
| 7 | #include <QUrl> |
| 8 | |
| 9 | #include "blurhash.hpp" |
| 10 | |
| 11 | void |
| 12 | BlurhashRunnable::run() |
| 13 | { |
| 14 | if (m_requestedSize.width() < 0 || m_requestedSize.height() < 0) { |
| 15 | emit error(QStringLiteral("Blurhash needs size request")); |
| 16 | return; |
| 17 | } |
| 18 | if (m_requestedSize.width() == 0 || m_requestedSize.height() == 0) { |
| 19 | auto image = QImage(m_requestedSize, QImage::Format_RGB32); |
| 20 | image.fill(QColor(0, 0, 0)); |
| 21 | emit done(image); |
| 22 | return; |
| 23 | } |
| 24 | |
| 25 | auto blurhashDecodeSize = m_requestedSize; |
| 26 | if (blurhashDecodeSize.height() > 100 && blurhashDecodeSize.width() > 100) { |
| 27 | blurhashDecodeSize.scale(100, 100, Qt::AspectRatioMode::KeepAspectRatio); |
| 28 | } |
| 29 | |
| 30 | auto decoded = blurhash::decode(QUrl::fromPercentEncoding(m_id.toUtf8()).toStdString(), |
| 31 | blurhashDecodeSize.width(), |
| 32 | blurhashDecodeSize.height()); |
| 33 | if (decoded.image.empty()) { |
| 34 | emit error(QStringLiteral("Failed decode!")); |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | QImage image(decoded.image.data(), |
| 39 | (int)decoded.width, |
| 40 | (int)decoded.height, |
| 41 | (int)decoded.width * 3, |
| 42 | QImage::Format_RGB888); |
| 43 | |
| 44 | image = image.scaled(m_requestedSize); |
| 45 | |
| 46 | emit done(image.convertToFormat(QImage::Format_RGB32)); |
| 47 | } |
| 48 | |
| 49 | #include "moc_BlurhashProvider.cpp" |