fix legacy skin model

Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
Trial97
2025-12-16 15:12:54 +02:00
parent fc3c5cba30
commit f7111b6ec1

View File

@@ -22,29 +22,89 @@
#include "FileSystem.h"
static void setAlpha(QImage& image, const QRect& region, const int alpha)
{
for (int y = region.top(); y < region.bottom(); ++y) {
QRgb* line = reinterpret_cast<QRgb*>(image.scanLine(y));
for (int x = region.left(); x < region.right(); ++x) {
QRgb pixel = line[x];
line[x] = qRgba(qRed(pixel), qGreen(pixel), qBlue(pixel), alpha);
}
}
}
static void doNotchTransparencyHack(QImage& image)
{
for (int y = 0; y < 32; y++) {
QRgb* line = reinterpret_cast<QRgb*>(image.scanLine(y));
for (int x = 32; x < 64; x++) {
if (qAlpha(line[x]) < 128) {
return;
}
}
}
setAlpha(image, { 32, 0, 32, 32 }, 0);
}
static QImage improveSkin(QImage skin)
{
int height = skin.height();
int width = skin.width();
if (width != 64 || (height != 32 && height != 64)) { // this is no minecraft skin
return skin;
}
// It seems some older skins may use this format, which can't be drawn onto
// https://github.com/PrismLauncher/PrismLauncher/issues/4032
// https://doc.qt.io/qt-6/qpainter.html#begin
if (skin.format() == QImage::Format_Indexed8) {
skin = skin.convertToFormat(QImage::Format_RGB32);
skin = skin.convertToFormat(QImage::Format_ARGB32);
}
if (skin.size() == QSize(64, 32)) { // old format
auto isLegacy = height == 32; // old format
if (isLegacy) {
auto newSkin = QImage(QSize(64, 64), skin.format());
newSkin.fill(Qt::transparent);
QPainter p(&newSkin);
p.drawImage(QPoint(0, 0), skin.copy(QRect(0, 0, 64, 32))); // copy head
p.drawImage(0, 0, skin);
auto leg = skin.copy(QRect(0, 16, 16, 16));
p.drawImage(QPoint(16, 48), leg); // copy leg
auto copyRect = [&p, &newSkin](int startX, int startY, int offsetX, int offsetY, int sizeX, int sizeY) {
QImage region = newSkin.copy(startX, startY, sizeX, sizeY);
region = region.mirrored(true, false);
auto arm = skin.copy(QRect(40, 16, 16, 16));
p.drawImage(QPoint(32, 48), arm); // copy arm
return newSkin;
p.drawImage(startX + offsetX, startY + offsetY, region);
};
static const struct {
int x;
int y;
int offsetX;
int offsetY;
int width;
int height;
} faces[] = {
{ 4, 16, 16, 32, 4, 4 }, { 8, 16, 16, 32, 4, 4 }, { 0, 20, 24, 32, 4, 12 }, { 4, 20, 16, 32, 4, 12 },
{ 8, 20, 8, 32, 4, 12 }, { 12, 20, 16, 32, 4, 12 }, { 44, 16, -8, 32, 4, 4 }, { 48, 16, -8, 32, 4, 4 },
{ 40, 20, 0, 32, 4, 12 }, { 44, 20, -8, 32, 4, 12 }, { 48, 20, -16, 32, 4, 12 }, { 52, 20, -8, 32, 4, 12 },
};
for (const auto& face : faces) {
copyRect(face.x, face.y, face.offsetX, face.offsetY, face.width, face.height);
}
doNotchTransparencyHack(newSkin);
skin = newSkin;
}
static const QRect opaqueParts[] = {
{ 0, 0, 32, 16 },
{ 0, 16, 64, 16 },
{ 16, 48, 32, 16 },
};
for (const auto& p : opaqueParts) {
setAlpha(skin, p, 255);
}
return skin;
}
static QImage getSkin(const QString path)
{
return improveSkin(QImage(path));
@@ -66,8 +126,8 @@ static QImage generatePreviews(QImage texture, bool slim)
paint.drawImage(4, 22, texture.copy(4, 20, 4, 12));
paint.drawImage(4, 22, texture.copy(4, 36, 4, 12));
// left leg
paint.drawImage(8, 22, texture.copy(4, 52, 4, 12));
paint.drawImage(8, 22, texture.copy(20, 52, 4, 12));
paint.drawImage(8, 22, texture.copy(4, 52, 4, 12));
auto armWidth = slim ? 3 : 4;
auto armPosX = slim ? 1 : 0;
@@ -89,8 +149,8 @@ static QImage generatePreviews(QImage texture, bool slim)
paint.drawImage(24, 22, texture.copy(12, 20, 4, 12));
paint.drawImage(24, 22, texture.copy(12, 36, 4, 12));
// left leg
paint.drawImage(28, 22, texture.copy(12, 52, 4, 12));
paint.drawImage(28, 22, texture.copy(28, 52, 4, 12));
paint.drawImage(28, 22, texture.copy(12, 52, 4, 12));
// right arm
paint.drawImage(armPosX + 20, 10, texture.copy(48 + armWidth, 20, armWidth, 12));