diff --git a/AssetStudioUtility/Texture2DConverter.cs b/AssetStudioUtility/Texture2DConverter.cs index d8314586..3df0956a 100644 --- a/AssetStudioUtility/Texture2DConverter.cs +++ b/AssetStudioUtility/Texture2DConverter.cs @@ -92,7 +92,18 @@ public bool DecodeTexture2D(byte[] bytes) reader.GetData(buff); if (switchSwizzled) { - buff = Texture2DSwitchDeswizzler.Unswizzle(buff, GetUncroppedSize(), blockSize, gobsPerBlock); + var unswizzledData = BigArrayPool.Shared.Rent(reader.Size); + try + { + Texture2DSwitchDeswizzler.Unswizzle(buff, GetUncroppedSize(), blockSize, gobsPerBlock, unswizzledData); + BigArrayPool.Shared.Return(buff, clearArray: true); + buff = unswizzledData; + } + catch (Exception e) + { + BigArrayPool.Shared.Return(unswizzledData, clearArray: true); + Logger.Error(e.Message, e); + } } switch (m_TextureFormat) diff --git a/AssetStudioUtility/Texture2DSwitchDeswizzler.cs b/AssetStudioUtility/Texture2DSwitchDeswizzler.cs index 07f27672..58d9c7f9 100644 --- a/AssetStudioUtility/Texture2DSwitchDeswizzler.cs +++ b/AssetStudioUtility/Texture2DSwitchDeswizzler.cs @@ -43,10 +43,8 @@ private static int CeilDivide(int a, int b) return (a + b - 1) / b; } - internal static byte[] Unswizzle(byte[] data, Size imageSize, Size blockSize, int gobsPerBlock) + internal static void Unswizzle(byte[] data, Size imageSize, Size blockSize, int gobsPerBlock, byte[] newData) { - byte[] newData = new byte[data.Length]; - int width = imageSize.Width; int height = imageSize.Height; @@ -71,14 +69,13 @@ internal static byte[] Unswizzle(byte[] data, Size imageSize, Size blockSize, in int gobDstY = (i * gobsPerBlock + k) * GOB_Y_TEXEL_COUNT + gobY; int gobDstLinPos = gobDstY * blockCountX * TEXEL_BYTE_SIZE + gobDstX * TEXEL_BYTE_SIZE; - Array.Copy(data, srcPos, newData, gobDstLinPos, TEXEL_BYTE_SIZE); + Buffer.BlockCopy(data, srcPos, newData, gobDstLinPos, TEXEL_BYTE_SIZE); srcPos += TEXEL_BYTE_SIZE; } } } } - return newData; } //this should be the amount of pixels that can fit 16 bytes