Skip to content
This repository was archived by the owner on Jan 19, 2025. It is now read-only.

Commit 7f07ffd

Browse files
committed
Read hidden alpha image in wii textures
1 parent 2cad404 commit 7f07ffd

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

Src/Core/Mackiloha.App/Extensions/TextureExtensions.cs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,47 @@ public static byte[] ToRGBA(this HMXBitmap bitmap, SystemInfo info)
4545
case TPL_CMP_2:
4646
case TPL_CMP_ALPHA:
4747
// Wii textures
48-
var tempData = new byte[((bitmap.Width * bitmap.Height) * bitmap.Bpp) / 8]; // Just ignore mips
49-
Array.Copy(bitmap.RawData, tempData, tempData.Length);
48+
if (bitmap.Bpp == 4 && bitmap.WiiAlphaNumber == 4)
49+
{
50+
// Hidden alpha texture after mips... ugh
51+
var dxSize = (bitmap.Width * bitmap.Height * bitmap.Bpp) / 8;
52+
53+
var rgbData = new byte[dxSize];
54+
var alphaData = new byte[dxSize];
55+
56+
Array.Copy(bitmap.RawData, 0, rgbData, 0, rgbData.Length);
57+
Array.Copy(bitmap.RawData, bitmap.RawData.Length - rgbData.Length, alphaData, 0, alphaData.Length);
58+
59+
// Remap blocks to DXT1
60+
Texture.TPL.TPLToDXT1(bitmap.Width, bitmap.Height, rgbData);
61+
Texture.TPL.TPLToDXT1(bitmap.Width, bitmap.Height, alphaData);
62+
63+
// Decode both images
64+
var decodedImage = DecodeDxImage(rgbData, bitmap.Width, bitmap.Height, 0, DxEncoding.DXGI_FORMAT_BC1_UNORM);
65+
var decodedAlpha = DecodeDxImage(alphaData, bitmap.Width, bitmap.Height, 0, DxEncoding.DXGI_FORMAT_BC1_UNORM);
5066

51-
if (bitmap.Bpp == 4)
67+
// Combine alpha channel
68+
for (int i = 0; i < decodedImage.Length; i += 4)
69+
{
70+
// Load from green channel
71+
decodedImage[i + 3] = decodedAlpha[i + 1];
72+
}
73+
74+
return decodedImage;
75+
}
76+
else if (bitmap.Bpp == 4)
5277
{
78+
var tempData = new byte[((bitmap.Width * bitmap.Height) * bitmap.Bpp) / 8]; // Just ignore mips
79+
Array.Copy(bitmap.RawData, tempData, tempData.Length);
80+
5381
Texture.TPL.TPLToDXT1(bitmap.Width, bitmap.Height, tempData);
5482
return DecodeDxImage(tempData, bitmap.Width, bitmap.Height, 0, DxEncoding.DXGI_FORMAT_BC1_UNORM);
5583
}
5684
else
5785
{
86+
var tempData = new byte[((bitmap.Width * bitmap.Height) * bitmap.Bpp) / 8]; // Just ignore mips
87+
Array.Copy(bitmap.RawData, tempData, tempData.Length);
88+
5889
// 8bpp wii texture is actually two DXT1 textures
5990
var rgbData = tempData.AsSpan(0, tempData.Length / 2);
6091
var alphaData = tempData.AsSpan(tempData.Length / 2, tempData.Length / 2);
@@ -70,6 +101,7 @@ public static byte[] ToRGBA(this HMXBitmap bitmap, SystemInfo info)
70101
// Combine alpha channel
71102
for (int i = 0; i < decodedImage.Length; i += 4)
72103
{
104+
// Load from green channel
73105
decodedImage[i + 3] = decodedAlpha[i + 1];
74106
}
75107

Src/Core/Mackiloha/HMXBitmap.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class HMXBitmap : ISerializable
99
public int Width { get; set; }
1010
public int Height { get; set; }
1111
public int BPL { get; set; }
12+
public int WiiAlphaNumber { get; set; }
1213

1314
public byte[] RawData { get; set; }
1415
}

Src/Core/Mackiloha/IO/Serializers/HMXBitmapSerializer.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ public override void ReadFromStream(AwesomeReader ar, ISerializable data)
2020
bitmap.Width = ar.ReadUInt16();
2121
bitmap.Height = ar.ReadUInt16();
2222
bitmap.BPL = ar.ReadUInt16();
23+
bitmap.WiiAlphaNumber = ar.ReadUInt16(); // Assuming u16
2324

24-
ar.BaseStream.Position += 19; // Skips zeros
25-
bitmap.RawData = ar.ReadBytes(CalculateTextureByteSize(bitmap.Encoding, bitmap.Width, bitmap.Height, bitmap.Bpp, bitmap.MipMaps));
25+
ar.BaseStream.Position += 17; // Skips zeros
26+
bitmap.RawData = ar.ReadBytes(CalculateTextureByteSize(bitmap.Encoding, bitmap.Width, bitmap.Height, bitmap.Bpp, bitmap.MipMaps, bitmap.WiiAlphaNumber));
2627
}
2728

2829
public override void WriteToStream(AwesomeWriter aw, ISerializable data)
@@ -46,7 +47,7 @@ public override void WriteToStream(AwesomeWriter aw, ISerializable data)
4647
aw.Write(bytes);
4748
}
4849

49-
private int CalculateTextureByteSize(int encoding, int w, int h, int bpp, int mips)
50+
private int CalculateTextureByteSize(int encoding, int w, int h, int bpp, int mips, int wiiAlphaNum = 0)
5051
{
5152
int bytes = 0;
5253

@@ -62,6 +63,12 @@ private int CalculateTextureByteSize(int encoding, int w, int h, int bpp, int mi
6263
// Each color is 32 bits
6364
bytes += (bpp == 4 || bpp == 8) ? 1 << (bpp + 2) : 0;
6465
break;
66+
case 72: // Wii 4bpp texture
67+
if (wiiAlphaNum == 4)
68+
{
69+
bytes += (w * h * bpp) / 8;
70+
}
71+
break;
6572
}
6673

6774
while (mips >= 0)

0 commit comments

Comments
 (0)