AssetStudio/AssetStudioUtility/Texture2DExtensions.cs
VaDiM ae4548f1c3 Add support for swizzled Switch textures
Co-Authored-By: nesrak1 <12544505+nesrak1@users.noreply.github.com>
2024-03-09 02:35:23 +03:00

57 lines
1.8 KiB
C#

using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using System.IO;
namespace AssetStudio
{
public static class Texture2DExtensions
{
public static Image<Bgra32> ConvertToImage(this Texture2D m_Texture2D, bool flip)
{
var converter = new Texture2DConverter(m_Texture2D);
var uncroppedSize = converter.GetUncroppedSize();
var buff = BigArrayPool<byte>.Shared.Rent(converter.OutputDataSize);
try
{
if (!converter.DecodeTexture2D(buff))
return null;
Image<Bgra32> image;
if (converter.UsesSwitchSwizzle)
{
image = Image.LoadPixelData<Bgra32>(buff, uncroppedSize.Width, uncroppedSize.Height);
image.Mutate(x => x.Crop(m_Texture2D.m_Width, m_Texture2D.m_Height));
}
else
{
image = Image.LoadPixelData<Bgra32>(buff, m_Texture2D.m_Width, m_Texture2D.m_Height);
}
if (flip)
{
image.Mutate(x => x.Flip(FlipMode.Vertical));
}
return image;
}
finally
{
BigArrayPool<byte>.Shared.Return(buff, clearArray: true);
}
}
public static MemoryStream ConvertToStream(this Texture2D m_Texture2D, ImageFormat imageFormat, bool flip)
{
var image = ConvertToImage(m_Texture2D, flip);
if (image != null)
{
using (image)
{
return image.ConvertToStream(imageFormat);
}
}
return null;
}
}
}