From fcabc319e15ce92554381d8f33b5b8e9ae27644b Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Tue, 5 Jun 2018 21:53:32 -0700 Subject: [PATCH] Qt: Load texture replacement images. Still doesn't save, but at least it can use packs. --- Core/TextureReplacer.cpp | 68 +++++++++++++++++++++++++++++++--------- Core/TextureReplacer.h | 1 - 2 files changed, 53 insertions(+), 16 deletions(-) diff --git a/Core/TextureReplacer.cpp b/Core/TextureReplacer.cpp index 3090df864b..fdb9254ec4 100644 --- a/Core/TextureReplacer.cpp +++ b/Core/TextureReplacer.cpp @@ -15,7 +15,9 @@ // Official git repository and contact information can be found at // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. -#ifndef USING_QT_UI +#ifdef USING_QT_UI +#include +#else #include #endif @@ -279,39 +281,50 @@ void TextureReplacer::PopulateReplacement(ReplacedTexture *result, u64 cachekey, break; } + bool good = false; ReplacedTextureLevel level; level.fmt = ReplacedTextureFormat::F_8888; level.file = filename; #ifdef USING_QT_UI - ERROR_LOG(G3D, "Replacement texture loading not implemented for Qt"); + QImage image(filename.c_str(), "PNG"); + if (image.isNull()) { + ERROR_LOG(G3D, "Could not load texture replacement info: %s", filename.c_str()); + } else { + level.w = (image.width() * w) / newW; + level.h = (image.height() * h) / newH; + good = true; + } #else png_image png = {}; png.version = PNG_IMAGE_VERSION; FILE *fp = File::OpenCFile(filename, "rb"); - bool bad = false; if (png_image_begin_read_from_stdio(&png, fp)) { // We pad files that have been hashrange'd so they are the same texture size. level.w = (png.width * w) / newW; level.h = (png.height * h) / newH; - if (i != 0) { - // Check that the mipmap size is correct. Can't load mips of the wrong size. - if (level.w != (result->levels_[0].w >> i) || level.h != (result->levels_[0].h >> i)) { - WARN_LOG(G3D, "Replacement mipmap invalid: size=%dx%d, expected=%dx%d (level %d, '%s')", level.w, level.h, result->levels_[0].w >> i, result->levels_[0].h >> i, i, filename.c_str()); - bad = true; - } - } - if (!bad) - result->levels_.push_back(level); + good = true; } else { ERROR_LOG(G3D, "Could not load texture replacement info: %s - %s", filename.c_str(), png.message); } fclose(fp); png_image_free(&png); - if (bad) - break; // Don't try to load any more mips. #endif + + if (good && i != 0) { + // Check that the mipmap size is correct. Can't load mips of the wrong size. + if (level.w != (result->levels_[0].w >> i) || level.h != (result->levels_[0].h >> i)) { + WARN_LOG(G3D, "Replacement mipmap invalid: size=%dx%d, expected=%dx%d (level %d, '%s')", level.w, level.h, result->levels_[0].w >> i, result->levels_[0].h >> i, i, filename.c_str()); + good = false; + } + } + + if (good) + result->levels_.push_back(level); + // Otherwise, we're done loading mips (bad PNG or bad size, either way.) + else + break; } result->alphaStatus_ = ReplacedTextureAlpha::UNKNOWN; @@ -540,7 +553,32 @@ void ReplacedTexture::Load(int level, void *out, int rowPitch) { const ReplacedTextureLevel &info = levels_[level]; #ifdef USING_QT_UI - ERROR_LOG(G3D, "Replacement texture loading not implemented for Qt"); + QImage image(info.file.c_str(), "PNG"); + if (image.isNull()) { + ERROR_LOG(G3D, "Could not load texture replacement info: %s", info.file.c_str()); + return; + } + + image = image.convertToFormat(QImage::Format_ARGB32); + bool alphaFull = true; + for (int y = 0; y < image.height(); ++y) { + const QRgb *src = (const QRgb *)image.constScanLine(y); + uint8_t *outLine = (uint8_t *)out + y * rowPitch; + for (int x = 0; x < image.width(); ++x) { + outLine[x * 4 + 0] = qRed(src[x]); + outLine[x * 4 + 1] = qGreen(src[x]); + outLine[x * 4 + 2] = qBlue(src[x]); + outLine[x * 4 + 3] = qAlpha(src[x]); + // We're already scanning each pixel... + if (qAlpha(src[x]) != 255) { + alphaFull = false; + } + } + } + + if (level == 0 || !alphaFull) { + alphaStatus_ = alphaFull ? ReplacedTextureAlpha::FULL : ReplacedTextureAlpha::UNKNOWN; + } #else png_image png = {}; png.version = PNG_IMAGE_VERSION; diff --git a/Core/TextureReplacer.h b/Core/TextureReplacer.h index 49606c0164..1e4b7cdae0 100644 --- a/Core/TextureReplacer.h +++ b/Core/TextureReplacer.h @@ -43,7 +43,6 @@ enum class ReplacedTextureFormat { enum class ReplacedTextureAlpha { UNKNOWN = 0x04, FULL = 0x00, - SIMPLE = 0x08, }; // For forward comatibility, we specify the hash.