1
0
mirror of synced 2024-09-24 03:28:21 +02:00

Fix bntx and switch bfres causing corruption or crashes

This commit is contained in:
KillzXGaming 2019-05-19 14:25:37 -04:00
parent 82d68152f9
commit 49314b2842
20 changed files with 26 additions and 23 deletions

Binary file not shown.

View File

@ -419,7 +419,7 @@ namespace Bfres.Structs
try
{
//Create image block from bitmap first
var data = GenerateMipsAndCompress(bitmap, Format);
var data = GenerateMipsAndCompress(bitmap, MipCount, Format);
//Swizzle and create surface
var surface = GX2.CreateGx2Texture(data, Text,

View File

@ -358,7 +358,7 @@ namespace FirstPlugin
try
{
//Create image block from bitmap first
var data = GenerateMipsAndCompress(bitmap, Format);
var data = GenerateMipsAndCompress(bitmap, MipCount, Format);
//Swizzle and create surface
var surface = GX2.CreateGx2Texture(data, Text,

View File

@ -377,7 +377,7 @@ namespace FirstPlugin
try
{
//Create image block from bitmap first
var data = GenerateMipsAndCompress(bitmap, Format);
var data = GenerateMipsAndCompress(bitmap, MipCount, Format);
//Swizzle and create surface
/* var surface = GX2.CreateGx2Texture(data, Text,

View File

@ -434,7 +434,7 @@ namespace FirstPlugin
try
{
//Create image block from bitmap first
var data = GenerateMipsAndCompress(bitmap, Format);
var data = GenerateMipsAndCompress(bitmap, MipCount, Format);
//Swizzle and create surface
var surface = GX2.CreateGx2Texture(data, Text,

View File

@ -347,11 +347,13 @@ namespace FirstPlugin
Cursor.Current = Cursors.WaitCursor;
foreach (var setting in settings)
{
if (setting.GenerateMipmaps)
if (setting.GenerateMipmaps && !setting.IsFinishedCompressing)
{
setting.DataBlockOutput.Clear();
setting.DataBlockOutput.Add(setting.GenerateMips());
}
if (setting.DataBlockOutput.Count <= 0)
throw new Exception("Data block is empty! Failed to compress!");
if (setting.DataBlockOutput != null)
{
@ -1096,7 +1098,7 @@ namespace FirstPlugin
{
Cursor.Current = Cursors.WaitCursor;
if (setting.GenerateMipmaps)
if (setting.GenerateMipmaps && !setting.IsFinishedCompressing)
{
setting.DataBlockOutput.Clear();
setting.DataBlockOutput.Add(setting.GenerateMips());
@ -1193,7 +1195,7 @@ namespace FirstPlugin
Texture.TextureData[ArrayLevel].Clear(); //Clear previous mip maps
var mipmaps = TextureImporterSettings.SwizzleSurfaceMipMaps(Texture,
GenerateMipsAndCompress(bitmap, Format), MipCount);
GenerateMipsAndCompress(bitmap, MipCount, Format), MipCount);
Texture.TextureData[ArrayLevel] = mipmaps;

View File

@ -463,7 +463,7 @@ namespace FirstPlugin
try
{
//Create image block from bitmap first
var data = GenerateMipsAndCompress(bitmap, Format);
var data = GenerateMipsAndCompress(bitmap, MipCount, Format);
//Swizzle and create surface
var NewSurface = GX2.CreateGx2Texture(data, Text,

View File

@ -540,7 +540,7 @@ namespace FirstPlugin
if (!IsSwizzled)
{
MipCount = GenerateMipCount(bitmap.Width, bitmap.Height);
ImageData = GenerateMipsAndCompress(bitmap, Format);
ImageData = GenerateMipsAndCompress(bitmap, MipCount, Format);
return;
}
@ -577,7 +577,7 @@ namespace FirstPlugin
tex.MipOffsets = new long[tex.MipCount];
var mipmaps = TextureImporterSettings.SwizzleSurfaceMipMaps(tex,
GenerateMipsAndCompress(bitmap, Format), MipCount);
GenerateMipsAndCompress(bitmap, MipCount, Format), MipCount);
ImageData = Utils.CombineByteArray(mipmaps.ToArray());
}

View File

@ -568,7 +568,7 @@ namespace FirstPlugin
tex.MipOffsets = new long[tex.MipCount];
var mipmaps = TextureImporterSettings.SwizzleSurfaceMipMaps(tex,
GenerateMipsAndCompress(bitmap, Format), MipCount);
GenerateMipsAndCompress(bitmap, MipCount, Format), MipCount);
ImageData = Utils.CombineByteArray(mipmaps.ToArray());
}

View File

@ -160,12 +160,14 @@ namespace FirstPlugin
Thread = new Thread((ThreadStart)(() =>
{
SelectedTexSettings.IsFinishedCompressing = false;
ToggleOkButton(false);
pictureBox1.Image = bitmap;
SelectedTexSettings.Compress(CompressionMode);
ToggleOkButton(true);
// SelectedTexSettings.IsFinishedCompressing = true;
if (SelectedTexSettings.DataBlockOutput.Count > 0) {
if (SelectedTexSettings.Format == SurfaceFormat.BC5_SNORM)

View File

@ -51,9 +51,11 @@ namespace FirstPlugin
public int sparseResidency = 0; //false
public int sparseBinding = 0; //false
public bool IsSRGB = true;
public bool GenerateMipmaps = false; //If bitmap and count more that 1 then geenrate
public bool GenerateMipmaps = false; //If bitmap and count more that 1 then generate
public float alphaRef = 0.5f;
public bool IsFinishedCompressing = false;
public void LoadDDS(string FileName, byte[] FileData = null, TextureData tree = null)
{
TexName = STGenericTexture.SetNameFromPath(FileName);

View File

@ -51,7 +51,7 @@ namespace Switch_Toolbox.Library
Height = (uint)Image.Height;
MipCount = 1;
ImageData = GenerateMipsAndCompress(Image, Format);
ImageData = GenerateMipsAndCompress(Image, MipCount, Format);
if (ImageData == null || ImageData.Length <= 0)
throw new Exception("Image failed to encode!");

View File

@ -512,7 +512,7 @@ namespace Switch_Toolbox.Library
return MipmapNum;
}
public byte[] GenerateMipsAndCompress(Bitmap bitmap, TEX_FORMAT Format, float alphaRef = 0.5f, STCompressionMode CompressionMode = STCompressionMode.Normal)
public static byte[] GenerateMipsAndCompress(Bitmap bitmap, uint MipCount, TEX_FORMAT Format, float alphaRef = 0.5f, STCompressionMode CompressionMode = STCompressionMode.Normal)
{
byte[] DecompressedData = BitmapExtension.ImageToByte(bitmap);
DecompressedData = ConvertBgraToRgba(DecompressedData);
@ -520,17 +520,14 @@ namespace Switch_Toolbox.Library
Bitmap Image = BitmapExtension.GetBitmap(DecompressedData, bitmap.Width, bitmap.Height);
List<byte[]> mipmaps = new List<byte[]>();
mipmaps.Add(STGenericTexture.CompressBlock(DecompressedData,
bitmap.Width, bitmap.Height, Format, alphaRef));
for (int mipLevel = 0; mipLevel < MipCount; mipLevel++)
{
if (Image.Width / 2 > 0 && Image.Height / 2 > 0)
{
Image = BitmapExtension.Resize(Image, Image.Width / 2, Image.Height / 2);
mipmaps.Add(STGenericTexture.CompressBlock(BitmapExtension.ImageToByte(Image),
Image.Width, Image.Height, Format, alphaRef, CompressionMode));
}
int width = Math.Max(1, bitmap.Width >> mipLevel);
int height = Math.Max(1, bitmap.Height >> mipLevel);
Image = BitmapExtension.Resize(Image, width, height);
mipmaps.Add(STGenericTexture.CompressBlock(BitmapExtension.ImageToByte(Image),
Image.Width, Image.Height, Format, alphaRef, CompressionMode));
}
Image.Dispose();