1
0
mirror of synced 2024-11-30 18:24:39 +01:00

Fix some artifact bugs with BC5 decompression.

This commit is contained in:
KillzXGaming 2020-09-01 19:25:55 -04:00
parent 549f23153f
commit 5e7f0d9f2f

View File

@ -200,6 +200,9 @@ namespace Toolbox.Library
Red[0] = data[IOffs + 0];
Red[1] = data[IOffs + 1];
if (IsSNORM)
CalculateBC3AlphaS(Red);
else
CalculateBC3Alpha(Red);
int RedLow = Get32(data, IOffs + 2);
@ -232,6 +235,7 @@ namespace Toolbox.Library
return BitmapExtension.GetBitmap(Output, W * 4, H * 4);
}
public static byte[] DecompressBC5(Byte[] data, int width, int height, bool IsSNORM, bool IsByteArray)
{
int W = (width + 3) / 4;
@ -433,6 +437,7 @@ namespace Toolbox.Library
return BitmapExtension.GetBitmap(Output, W * 4, H * 4);
}
public static unsafe byte[] CompressBlock(Byte[] data, int width, int height, DDS.DXGI_FORMAT format, bool multiThread, float AlphaRef = 0.5f, STCompressionMode CompressionMode = STCompressionMode.Normal)
{
long inputRowPitch = width * 4;
@ -682,47 +687,33 @@ namespace Toolbox.Library
}
private static void CalculateBC3Alpha(byte[] Alpha)
{
for (int i = 2; i < 8; i++)
{
if (Alpha[0] > Alpha[1])
{
Alpha[i] = (byte)(((8 - i) * Alpha[0] + (i - 1) * Alpha[1]) / 7);
for (int i = 2; i < 8; i++)
Alpha[i] = (byte)(Alpha[0] + ((Alpha[1] - Alpha[0]) * (i - 1)) / 7);
}
else if (i < 6)
else
{
Alpha[i] = (byte)(((6 - i) * Alpha[0] + (i - 1) * Alpha[1]) / 7);
}
else if (i == 6)
{
Alpha[i] = 0;
}
else /* i == 7 */
{
Alpha[i] = 0xff;
}
for (int i = 2; i < 6; i++)
Alpha[i] = (byte)(Alpha[0] + ((Alpha[1] - Alpha[0]) * (i - 1)) / 5);
Alpha[6] = 0;
Alpha[7] = 255;
}
}
private static void CalculateBC3AlphaS(byte[] Alpha)
{
for (int i = 2; i < 8; i++)
{
if ((sbyte)Alpha[0] > (sbyte)Alpha[1])
{
Alpha[i] = (byte)(((8 - i) * (sbyte)Alpha[0] + (i - 1) * (sbyte)Alpha[1]) / 7);
for (int i = 2; i < 8; i++)
Alpha[i] = (byte)(Alpha[0] + (((sbyte)Alpha[1] - (sbyte)Alpha[0]) * (i - 1)) / 7);
}
else if (i < 6)
else
{
Alpha[i] = (byte)(((6 - i) * (sbyte)Alpha[0] + (i - 1) * (sbyte)Alpha[1]) / 7);
}
else if (i == 6)
{
Alpha[i] = 0x80;
}
else /* i == 7 */
{
Alpha[i] = 0x7f;
}
for (int i = 2; i < 6; i++)
Alpha[i] = (byte)(Alpha[0] + (((sbyte)Alpha[1] - (sbyte)Alpha[0]) * (i - 1)) / 5);
Alpha[6] = 0x80;
Alpha[7] = 0x7f;
}
}
}