Some minor fixes

This commit is contained in:
VaDiM
2025-05-11 17:57:33 +03:00
parent b0a051fc47
commit 55406553f6
17 changed files with 84 additions and 57 deletions

View File

@ -130,8 +130,8 @@ namespace AssetStudio
debugLog += "[Legacy wav converter] Generating wav header..\n";
var buffer = new byte[audioSize + 44];
m_AudioClip.m_AudioData.GetData(buffer, out var read, 44);
if (read > 0)
var dataLen = m_AudioClip.m_AudioData.GetData(buffer, 44);
if (dataLen > 0)
{
var wavHeader = new WavHeader(audioSize, audioFormat, channels, (uint)sampleRate, bits);
wavHeader.WriteToArray(buffer);
@ -141,16 +141,16 @@ namespace AssetStudio
private static void ReadAsPcm16(IntPtr srcPtr, byte[] destBuffer, int offset, uint pcmDataLen, ref string debugLog)
{
var pcmFloatSample = new byte[4];
var pcmFloatVal = new byte[4];
for (var i = 0; i < pcmDataLen; i += 4)
{
for (var j = 0; j < 4; j++)
{
pcmFloatSample[j] = Marshal.ReadByte(srcPtr, i + j);
pcmFloatVal[j] = Marshal.ReadByte(srcPtr, i + j);
}
var pcm16Sample = (short)MathHelper.Clamp(BitConverter.ToSingle(pcmFloatSample, 0) * short.MaxValue, short.MinValue, short.MaxValue);
destBuffer[offset] = (byte)(pcm16Sample & 255);
destBuffer[offset + 1] = (byte)(pcm16Sample >> 8);
var pcm16Val = (short)MathHelper.Clamp(BitConverter.ToSingle(pcmFloatVal, 0) * short.MaxValue, short.MinValue, short.MaxValue);
destBuffer[offset] = (byte)(pcm16Val & 255);
destBuffer[offset + 1] = (byte)(pcm16Val >> 8);
offset += 2;
}
debugLog += "Finished PCMFLOAT -> PCM16 converting\n";

View File

@ -89,7 +89,7 @@ namespace AssetStudio
var buff = BigArrayPool<byte>.Shared.Rent(reader.Size);
try
{
reader.GetData(buff, out _);
_ = reader.GetData(buff);
if (switchSwizzled)
{
var unswizzledData = BigArrayPool<byte>.Shared.Rent(reader.Size);

View File

@ -1,7 +1,8 @@
using SixLabors.ImageSharp;
using System;
using System.IO;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using System.IO;
namespace AssetStudio
{
@ -10,8 +11,8 @@ namespace AssetStudio
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);
var spanBuff = buff.AsSpan(0, converter.OutputDataSize);
try
{
if (!converter.DecodeTexture2D(buff))
@ -20,12 +21,13 @@ namespace AssetStudio
Image<Bgra32> image;
if (converter.UsesSwitchSwizzle)
{
image = Image.LoadPixelData<Bgra32>(buff, uncroppedSize.Width, uncroppedSize.Height);
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>(buff, m_Texture2D.m_Width, m_Texture2D.m_Height);
image = Image.LoadPixelData<Bgra32>(spanBuff, m_Texture2D.m_Width, m_Texture2D.m_Height);
}
if (flip)

View File

@ -43,7 +43,7 @@ namespace AssetStudio
return (a + b - 1) / b;
}
internal static void Unswizzle(byte[] data, Size imageSize, Size blockSize, int gobsPerBlock, byte[] newData)
internal static void Unswizzle(ReadOnlySpan<byte> data, Size imageSize, Size blockSize, int gobsPerBlock, Span<byte> newData)
{
int width = imageSize.Width;
int height = imageSize.Height;
@ -69,7 +69,7 @@ namespace AssetStudio
int gobDstY = (i * gobsPerBlock + k) * GOB_Y_TEXEL_COUNT + gobY;
int gobDstLinPos = gobDstY * blockCountX * TEXEL_BYTE_SIZE + gobDstX * TEXEL_BYTE_SIZE;
Buffer.BlockCopy(data, srcPos, newData, gobDstLinPos, TEXEL_BYTE_SIZE);
data.Slice(srcPos, TEXEL_BYTE_SIZE).CopyTo(newData.Slice(gobDstLinPos));
srcPos += TEXEL_BYTE_SIZE;
}