2019-08-06 22:56:56 -04: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 01:04:50 -04:00
|
|
|
#include "citra_qt/qt_image_interface.h"
|
2019-08-06 22:56:56 -04:00
|
|
|
#include "common/logging/log.h"
|
|
|
|
|
|
|
|
bool QtImageInterface::DecodePNG(std::vector<u8>& dst, u32& width, u32& height,
|
2023-04-27 07:38:28 +03:00
|
|
|
std::span<const u8> src) {
|
|
|
|
QImage image(QImage::fromData(src.data(), static_cast<int>(src.size())));
|
2019-08-06 22:56:56 -04:00
|
|
|
if (image.isNull()) {
|
2023-04-27 07:38:28 +03:00
|
|
|
LOG_ERROR(Frontend, "Failed to decode png because image is null");
|
2019-08-06 22:56:56 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
width = image.width();
|
|
|
|
height = image.height();
|
|
|
|
|
2019-08-14 01:04:50 -04:00
|
|
|
image = image.convertToFormat(QImage::Format_RGBA8888);
|
|
|
|
|
2019-08-06 22:56:56 -04:00
|
|
|
// Write RGBA8 to vector
|
2023-04-27 07:38:28 +03:00
|
|
|
const size_t image_size = width * height * 4;
|
|
|
|
dst.resize(image_size);
|
|
|
|
std::memcpy(dst.data(), image.constBits(), image_size);
|
2019-08-06 22:56:56 -04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-04-27 07:38:28 +03:00
|
|
|
bool QtImageInterface::EncodePNG(const std::string& path, u32 width, u32 height,
|
|
|
|
std::span<const u8> src) {
|
2019-08-06 22:56:56 -04:00
|
|
|
QImage image(src.data(), width, height, QImage::Format_RGBA8888);
|
|
|
|
|
2019-08-07 14:21:47 -04:00
|
|
|
if (!image.save(QString::fromStdString(path), "PNG")) {
|
2019-08-06 22:56:56 -04:00
|
|
|
LOG_ERROR(Frontend, "Failed to save {}", path);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2019-10-12 11:25:27 -04:00
|
|
|
}
|