matrixion/src/BlurhashProvider.cpp

43 lines
1.2 KiB
C++
Raw Normal View History

// SPDX-FileCopyrightText: Nheko Contributors
2021-03-05 02:35:15 +03:00
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "BlurhashProvider.h"
#include <algorithm>
#include <QUrl>
#include "blurhash.hpp"
2020-03-04 03:56:58 +03:00
void
BlurhashRunnable::run()
{
2021-09-18 01:22:33 +03:00
if (m_requestedSize.width() < 0 || m_requestedSize.height() < 0) {
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) {
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;
}
auto decoded = blurhash::decode(QUrl::fromPercentEncoding(m_id.toUtf8()).toStdString(),
m_requestedSize.width(),
m_requestedSize.height());
if (decoded.image.empty()) {
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);
emit done(image.convertToFormat(QImage::Format_RGB32));
}