mirror of
https://github.com/aelurum/AssetStudio.git
synced 2025-05-25 05:40:21 -04:00
59 lines
1.9 KiB
C#
59 lines
1.9 KiB
C#
using System;
|
|
using System.IO;
|
|
using SixLabors.ImageSharp;
|
|
using SixLabors.ImageSharp.PixelFormats;
|
|
using SixLabors.ImageSharp.Processing;
|
|
|
|
namespace AssetStudio
|
|
{
|
|
public static class Texture2DExtensions
|
|
{
|
|
public static Image<Bgra32> ConvertToImage(this Texture2D m_Texture2D, bool flip)
|
|
{
|
|
var converter = new Texture2DConverter(m_Texture2D);
|
|
var buff = BigArrayPool<byte>.Shared.Rent(converter.OutputDataSize);
|
|
var spanBuff = buff.AsSpan(0, converter.OutputDataSize);
|
|
try
|
|
{
|
|
if (!converter.DecodeTexture2D(buff))
|
|
return null;
|
|
|
|
Image<Bgra32> image;
|
|
if (converter.UsesSwitchSwizzle)
|
|
{
|
|
var uncroppedSize = converter.GetUncroppedSize();
|
|
image = Image.LoadPixelData<Bgra32>(spanBuff, uncroppedSize.Width, uncroppedSize.Height);
|
|
image.Mutate(x => x.Crop(m_Texture2D.m_Width, m_Texture2D.m_Height));
|
|
}
|
|
else
|
|
{
|
|
image = Image.LoadPixelData<Bgra32>(spanBuff, 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;
|
|
}
|
|
}
|
|
}
|