Skip to content

Commit

Permalink
Qt: Load texture replacement images.
Browse files Browse the repository at this point in the history
Still doesn't save, but at least it can use packs.
  • Loading branch information
unknownbrackets committed Jun 6, 2018
1 parent 07e178a commit fcabc31
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
Expand Up @@ -15,7 +15,9 @@
// Official git repository and contact information can be found at // Official git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. // 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> #include <libpng17/png.h>
#endif #endif


Expand Down Expand Up @@ -279,39 +281,50 @@ void TextureReplacer::PopulateReplacement(ReplacedTexture *result, u64 cachekey,
break; break;
} }


bool good = false;
ReplacedTextureLevel level; ReplacedTextureLevel level;
level.fmt = ReplacedTextureFormat::F_8888; level.fmt = ReplacedTextureFormat::F_8888;
level.file = filename; level.file = filename;


#ifdef USING_QT_UI #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 #else
png_image png = {}; png_image png = {};
png.version = PNG_IMAGE_VERSION; png.version = PNG_IMAGE_VERSION;
FILE *fp = File::OpenCFile(filename, "rb"); FILE *fp = File::OpenCFile(filename, "rb");
bool bad = false;
if (png_image_begin_read_from_stdio(&png, fp)) { if (png_image_begin_read_from_stdio(&png, fp)) {
// We pad files that have been hashrange'd so they are the same texture size. // We pad files that have been hashrange'd so they are the same texture size.
level.w = (png.width * w) / newW; level.w = (png.width * w) / newW;
level.h = (png.height * h) / newH; level.h = (png.height * h) / newH;
if (i != 0) { good = true;
// 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);
} else { } else {
ERROR_LOG(G3D, "Could not load texture replacement info: %s - %s", filename.c_str(), png.message); ERROR_LOG(G3D, "Could not load texture replacement info: %s - %s", filename.c_str(), png.message);
} }
fclose(fp); fclose(fp);


png_image_free(&png); png_image_free(&png);
if (bad)
break; // Don't try to load any more mips.
#endif #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; 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]; const ReplacedTextureLevel &info = levels_[level];


#ifdef USING_QT_UI #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 #else
png_image png = {}; png_image png = {};
png.version = PNG_IMAGE_VERSION; png.version = PNG_IMAGE_VERSION;
Expand Down
1 change: 0 additions & 1 deletion Core/TextureReplacer.h
Expand Up @@ -43,7 +43,6 @@ enum class ReplacedTextureFormat {
enum class ReplacedTextureAlpha { enum class ReplacedTextureAlpha {
UNKNOWN = 0x04, UNKNOWN = 0x04,
FULL = 0x00, FULL = 0x00,
SIMPLE = 0x08,
}; };


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

0 comments on commit fcabc31

Please sign in to comment.