2019-08-07 08:26:56 +05:30
|
|
|
// Copyright 2019 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <QImage>
|
2023-07-12 10:13:19 +05:30
|
|
|
#include <QImageReader>
|
2019-08-07 08:26:56 +05:30
|
|
|
#include <QString>
|
2019-08-14 10:34:50 +05:30
|
|
|
#include "citra_qt/qt_image_interface.h"
|
2019-08-07 08:26:56 +05:30
|
|
|
#include "common/logging/log.h"
|
|
|
|
|
2023-07-12 10:13:19 +05:30
|
|
|
QtImageInterface::QtImageInterface() {
|
|
|
|
QImageReader::setAllocationLimit(0);
|
|
|
|
}
|
|
|
|
|
2019-08-07 08:26:56 +05:30
|
|
|
bool QtImageInterface::DecodePNG(std::vector<u8>& dst, u32& width, u32& height,
|
2023-04-27 10:08:28 +05:30
|
|
|
std::span<const u8> src) {
|
|
|
|
QImage image(QImage::fromData(src.data(), static_cast<int>(src.size())));
|
2019-08-07 08:26:56 +05:30
|
|
|
if (image.isNull()) {
|
2023-04-27 10:08:28 +05:30
|
|
|
LOG_ERROR(Frontend, "Failed to decode png because image is null");
|
2019-08-07 08:26:56 +05:30
|
|
|
return false;
|
|
|
|
}
|
|
|
|
width = image.width();
|
|
|
|
height = image.height();
|
|
|
|
|
2019-08-14 10:34:50 +05:30
|
|
|
image = image.convertToFormat(QImage::Format_RGBA8888);
|
|
|
|
|
2019-08-07 08:26:56 +05:30
|
|
|
// Write RGBA8 to vector
|
2024-01-08 02:07:42 +05:30
|
|
|
const std::size_t image_size = width * height * 4;
|
2023-04-27 10:08:28 +05:30
|
|
|
dst.resize(image_size);
|
|
|
|
std::memcpy(dst.data(), image.constBits(), image_size);
|
2019-08-07 08:26:56 +05:30
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-04-27 10:08:28 +05:30
|
|
|
bool QtImageInterface::EncodePNG(const std::string& path, u32 width, u32 height,
|
|
|
|
std::span<const u8> src) {
|
2019-08-07 08:26:56 +05:30
|
|
|
QImage image(src.data(), width, height, QImage::Format_RGBA8888);
|
|
|
|
|
2019-08-07 23:51:47 +05:30
|
|
|
if (!image.save(QString::fromStdString(path), "PNG")) {
|
2019-08-07 08:26:56 +05:30
|
|
|
LOG_ERROR(Frontend, "Failed to save {}", path);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2019-10-12 20:55:27 +05:30
|
|
|
}
|