Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write hidden alpha in Wii textures if milo version is set to 25 #19

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions Src/Core/Mackiloha.App/Extensions/TextureExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,7 @@ public static HMXBitmap BitmapFromImage(string imagePath, SystemInfo info)

if (HasAlpha(rawData))
{
var (rgbData, alphaData) = SplitRGBAForTPL(rawData);
var (rgbData, alphaData) = SplitRGBAForTPL(rawData, info);

// Encode as two DXT1 images
var rgbDxData = EncodeDxImage(rgbData, width, height, 0, DxEncoding.DXGI_FORMAT_BC1_UNORM);
Expand All @@ -960,6 +960,20 @@ public static HMXBitmap BitmapFromImage(string imagePath, SystemInfo info)
Array.Copy(rgbDxData, 0, combinedImageData, 0, rgbDxData.Length);
Array.Copy(alphaDxData, 0, combinedImageData, rgbDxData.Length, alphaDxData.Length);

if (info.Version == 25)
{
return new HMXBitmap()
{
Bpp = 4,
Encoding = TPL_CMP,
MipMaps = 0,
Width = width,
Height = height,
BPL = (width * 4) / 8,
WiiAlphaNumber = 4,
RawData = combinedImageData
};
}
return new HMXBitmap()
{
Bpp = 8,
Expand Down Expand Up @@ -1160,7 +1174,7 @@ private static bool HasAlpha(byte[] rgbaData)
return false;
}

private static (byte[] rgb, byte[] alpha) SplitRGBAForTPL(byte[] data)
private static (byte[] rgb, byte[] alpha) SplitRGBAForTPL(byte[] data, SystemInfo info)
{
var rgb = new byte[data.Length];
var alpha = new byte[data.Length];
Expand All @@ -1172,8 +1186,18 @@ private static (byte[] rgb, byte[] alpha) SplitRGBAForTPL(byte[] data)
rgb[i + 2] = data[i + 2];
rgb[i + 3] = 0xFF;

// Put alpha on green channel
alpha[i + 1] = data[i + 3];
if (info.Version == 25)
{
// Put alpha on all channels
alpha[i] = data[i + 3];
alpha[i + 1] = data[i + 3];
alpha[i + 2] = data[i + 3];
}
else
{
// Put alpha on green channel
alpha[i + 1] = data[i + 3];
}
}

return (rgb, alpha);
Expand Down
25 changes: 20 additions & 5 deletions Src/Core/Mackiloha/IO/Serializers/HMXBitmapSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ public override void WriteToStream(AwesomeWriter aw, ISerializable data)
{
var bitmap = data as HMXBitmap;

aw.Write((byte)0x01);
if (bitmap.WiiAlphaNumber == 4)
aw.Write((byte)0x02);
else
aw.Write((byte)0x01);

aw.Write((byte)bitmap.Bpp);
aw.Write((int)bitmap.Encoding);
Expand All @@ -40,11 +43,23 @@ public override void WriteToStream(AwesomeWriter aw, ISerializable data)
aw.Write((short)bitmap.Height);
aw.Write((short)bitmap.BPL);

aw.Write(new byte[19]);
aw.Write((short)bitmap.WiiAlphaNumber);

byte[] bytes = new byte[CalculateTextureByteSize(bitmap.Encoding, bitmap.Width, bitmap.Height, bitmap.Bpp, bitmap.MipMaps)];
Array.Copy(bitmap.RawData, bytes, bytes.Length);
aw.Write(bytes);
aw.Write(new byte[17]);

if (bitmap.WiiAlphaNumber == 4)
{
byte[] bytes = new byte[CalculateTextureByteSize(bitmap.Encoding, bitmap.Width, bitmap.Height, 8, bitmap.MipMaps)];
Array.Copy(bitmap.RawData, bytes, bytes.Length);
aw.Write(bytes);
}
else
{
byte[] bytes = new byte[CalculateTextureByteSize(bitmap.Encoding, bitmap.Width, bitmap.Height, bitmap.Bpp, bitmap.MipMaps)];
Array.Copy(bitmap.RawData, bytes, bytes.Length);
aw.Write(bytes);
}

}

private int CalculateTextureByteSize(int encoding, int w, int h, int bpp, int mips, int wiiAlphaNum = 0)
Expand Down