Permalink
Browse files

Qt: Load texture replacement images.

Still doesn't save, but at least it can use packs.
  • Loading branch information...
unknownbrackets committed Jun 6, 2018
1 parent 07e178a commit fcabc319e15ce92554381d8f33b5b8e9ae27644b
Showing with 53 additions and 16 deletions.
  1. +53 −15 Core/TextureReplacer.cpp
  2. +0 −1 Core/TextureReplacer.h
View
@@ -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 <QtGui/QImage>
#else
#include <libpng17/png.h>
#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;
View
@@ -43,7 +43,6 @@ enum class ReplacedTextureFormat {
enum class ReplacedTextureAlpha {
UNKNOWN = 0x04,
FULL = 0x00,
SIMPLE = 0x08,
};
// For forward comatibility, we specify the hash.

0 comments on commit fcabc31

Please sign in to comment.