Skip to content

Commit

Permalink
Merge pull request #11145 from unknownbrackets/qt-texreplace
Browse files Browse the repository at this point in the history
Qt: Load texture replacement images
  • Loading branch information
hrydgard authored Jun 6, 2018
2 parents 347721f + fcabc31 commit 67fec95
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 16 deletions.
68 changes: 53 additions & 15 deletions Core/TextureReplacer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion Core/TextureReplacer.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ enum class ReplacedTextureFormat {
enum class ReplacedTextureAlpha {
UNKNOWN = 0x04,
FULL = 0x00,
SIMPLE = 0x08,
};

// For forward comatibility, we specify the hash.
Expand Down

0 comments on commit 67fec95

Please sign in to comment.