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