More spans

This commit is contained in:
VaDiM
2025-08-08 19:00:08 +03:00
parent 35324083e1
commit 054906a426
6 changed files with 120 additions and 110 deletions

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Buffers.Binary;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
@ -808,7 +809,8 @@ namespace AssetStudio
for (var d = 0; d < m_Channel.dimension; d++) for (var d = 0; d < m_Channel.dimension; d++)
{ {
var componentOffset = vertexOffset + componentByteSize * d; var componentOffset = vertexOffset + componentByteSize * d;
Buffer.BlockCopy(m_VertexData.m_DataSize, componentOffset, componentBytes, componentByteSize * (v * m_Channel.dimension + d), componentByteSize); var dstOffset = componentByteSize * (v * m_Channel.dimension + d);
m_VertexData.m_DataSize.AsSpan(componentOffset, componentByteSize).CopyTo(componentBytes.AsSpan(dstOffset));
} }
} }
@ -816,10 +818,7 @@ namespace AssetStudio
{ {
for (var i = 0; i < componentBytes.Length / componentByteSize; i++) for (var i = 0; i < componentBytes.Length / componentByteSize; i++)
{ {
var buff = new byte[componentByteSize]; componentBytes.AsSpan(i * componentByteSize, componentByteSize).Reverse();
Buffer.BlockCopy(componentBytes, i * componentByteSize, buff, 0, componentByteSize);
buff = buff.Reverse().ToArray();
Buffer.BlockCopy(buff, 0, componentBytes, i * componentByteSize, componentByteSize);
} }
} }
@ -897,6 +896,9 @@ namespace AssetStudio
} }
} }
break; break;
default:
Logger.Warning($"Unknown vertex attribute: {chn}");
break;
} }
} }
else else
@ -934,6 +936,9 @@ namespace AssetStudio
case 7: //kShaderChannelTangent case 7: //kShaderChannelTangent
m_Tangents = componentsFloatArray; m_Tangents = componentsFloatArray;
break; break;
default:
Logger.Warning($"Unknown vertex attribute: {chn}");
break;
} }
} }
} }
@ -990,11 +995,9 @@ namespace AssetStudio
{ {
m_BindPose = new Matrix4x4[m_CompressedMesh.m_BindPoses.m_NumItems / 16]; m_BindPose = new Matrix4x4[m_CompressedMesh.m_BindPoses.m_NumItems / 16];
var m_BindPoses_Unpacked = m_CompressedMesh.m_BindPoses.UnpackFloats(16, 4 * 16); var m_BindPoses_Unpacked = m_CompressedMesh.m_BindPoses.UnpackFloats(16, 4 * 16);
var buffer = new float[16];
for (var i = 0; i < m_BindPose.Length; i++) for (var i = 0; i < m_BindPose.Length; i++)
{ {
Array.Copy(m_BindPoses_Unpacked, i * 16, buffer, 0, 16); m_BindPose[i] = new Matrix4x4(m_BindPoses_Unpacked.AsSpan(i * 16, 16));
m_BindPose[i] = new Matrix4x4(buffer);
} }
} }
} }
@ -1415,7 +1418,11 @@ namespace AssetStudio
switch (format) switch (format)
{ {
case VertexFormat.Float: case VertexFormat.Float:
#if NET
result[i] = BinaryPrimitives.ReadSingleLittleEndian(inputBytes.AsSpan(i * 4));
#else
result[i] = BitConverter.ToSingle(inputBytes, i * 4); result[i] = BitConverter.ToSingle(inputBytes, i * 4);
#endif
break; break;
case VertexFormat.Float16: case VertexFormat.Float16:
result[i] = (float)HalfHelper.ToHalf(inputBytes, i * 2); result[i] = (float)HalfHelper.ToHalf(inputBytes, i * 2);
@ -1427,17 +1434,17 @@ namespace AssetStudio
result[i] = Math.Max((sbyte)inputBytes[i] / 127f, -1f); result[i] = Math.Max((sbyte)inputBytes[i] / 127f, -1f);
break; break;
case VertexFormat.UNorm16: case VertexFormat.UNorm16:
result[i] = BitConverter.ToUInt16(inputBytes, i * 2) / 65535f; result[i] = BinaryPrimitives.ReadUInt16LittleEndian(inputBytes.AsSpan(i * 2)) / 65535f;
break; break;
case VertexFormat.SNorm16: case VertexFormat.SNorm16:
result[i] = Math.Max(BitConverter.ToInt16(inputBytes, i * 2) / 32767f, -1f); result[i] = Math.Max(BinaryPrimitives.ReadInt16LittleEndian(inputBytes.AsSpan(i * 2)) / 32767f, -1f);
break; break;
} }
} }
return result; return result;
} }
public static int[] BytesToIntArray(byte[] inputBytes, VertexFormat format) public static int[] BytesToIntArray(ReadOnlySpan<byte> inputBytes, VertexFormat format)
{ {
var size = GetFormatSize(format); var size = GetFormatSize(format);
var len = inputBytes.Length / size; var len = inputBytes.Length / size;
@ -1451,12 +1458,16 @@ namespace AssetStudio
result[i] = inputBytes[i]; result[i] = inputBytes[i];
break; break;
case VertexFormat.UInt16: case VertexFormat.UInt16:
result[i] = BinaryPrimitives.ReadUInt16LittleEndian(inputBytes.Slice(i * 2));
break;
case VertexFormat.SInt16: case VertexFormat.SInt16:
result[i] = BitConverter.ToInt16(inputBytes, i * 2); result[i] = BinaryPrimitives.ReadInt16LittleEndian(inputBytes.Slice(i * 2));
break; break;
case VertexFormat.UInt32: case VertexFormat.UInt32:
result[i] = (int)BinaryPrimitives.ReadUInt32LittleEndian(inputBytes.Slice(i * 4));
break;
case VertexFormat.SInt32: case VertexFormat.SInt32:
result[i] = BitConverter.ToInt32(inputBytes, i * 4); result[i] = BinaryPrimitives.ReadInt32LittleEndian(inputBytes.Slice(i * 4));
break; break;
} }
} }

View File

@ -88,7 +88,7 @@ namespace AssetStudio
if (Endian == EndianType.BigEndian) if (Endian == EndianType.BigEndian)
{ {
Read(buffer, 0, 4); Read(buffer, 0, 4);
Array.Reverse(buffer, 0, 4); buffer.AsSpan(0, 4).Reverse();
return BitConverter.ToSingle(buffer, 0); return BitConverter.ToSingle(buffer, 0);
} }
return base.ReadSingle(); return base.ReadSingle();
@ -99,7 +99,7 @@ namespace AssetStudio
if (Endian == EndianType.BigEndian) if (Endian == EndianType.BigEndian)
{ {
Read(buffer, 0, 8); Read(buffer, 0, 8);
Array.Reverse(buffer); buffer.AsSpan().Reverse();
return BitConverter.ToDouble(buffer, 0); return BitConverter.ToDouble(buffer, 0);
} }
return base.ReadDouble(); return base.ReadDouble();

View File

@ -26,9 +26,9 @@ namespace AssetStudio
public float M23; public float M23;
public float M33; public float M33;
public Matrix4x4(float[] values) public Matrix4x4(Span<float> values)
{ {
if (values == null) if (values.IsEmpty)
throw new ArgumentNullException(nameof(values)); throw new ArgumentNullException(nameof(values));
if (values.Length != 16) if (values.Length != 16)
throw new ArgumentOutOfRangeException(nameof(values), "There must be sixteen and only sixteen input values for Matrix."); throw new ArgumentOutOfRangeException(nameof(values), "There must be sixteen and only sixteen input values for Matrix.");

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Buffers.Binary;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using Texture2DDecoder; using Texture2DDecoder;
@ -289,22 +290,21 @@ namespace AssetStudio
return flag; return flag;
} }
private void SwapBytesForXbox(byte[] image_data) private void SwapBytesForXbox(Span<byte> image_data)
{ {
if (platform == BuildTarget.XBOX360) if (platform == BuildTarget.XBOX360)
{ {
for (var i = 0; i < reader.Size / 2; i++) for (var i = 0; i < reader.Size / 2; i++)
{ {
(image_data[i * 2 + 1], image_data[i * 2]) = (image_data[i * 2], image_data[i * 2 + 1]); image_data.Slice(i * 2, 2).Reverse();
} }
} }
} }
private bool DecodeAlpha8(byte[] image_data, byte[] buff) private bool DecodeAlpha8(ReadOnlySpan<byte> image_data, Span<byte> buff)
{ {
var size = m_Width * m_Height; var size = m_Width * m_Height;
var span = new Span<byte>(buff); buff.Fill(0xFF);
span.Fill(0xFF);
for (var i = 0; i < size; i++) for (var i = 0; i < size; i++)
{ {
buff[i * 4 + 3] = image_data[i]; buff[i * 4 + 3] = image_data[i];
@ -312,25 +312,25 @@ namespace AssetStudio
return true; return true;
} }
private bool DecodeARGB4444(byte[] image_data, byte[] buff) private bool DecodeARGB4444(ReadOnlySpan<byte> image_data, Span<byte> buff)
{ {
var size = m_Width * m_Height; var size = m_Width * m_Height;
var pixelNew = new byte[4]; var pixelNew = new byte[4].AsSpan();
for (var i = 0; i < size; i++) for (var i = 0; i < size; i++)
{ {
var pixelOldShort = BitConverter.ToUInt16(image_data, i * 2); var pixelOldShort = BinaryPrimitives.ReadUInt16LittleEndian(image_data.Slice(i * 2));
pixelNew[0] = (byte)(pixelOldShort & 0x000f); pixelNew[0] = (byte)(pixelOldShort & 0x000f);
pixelNew[1] = (byte)((pixelOldShort & 0x00f0) >> 4); pixelNew[1] = (byte)((pixelOldShort & 0x00f0) >> 4);
pixelNew[2] = (byte)((pixelOldShort & 0x0f00) >> 8); pixelNew[2] = (byte)((pixelOldShort & 0x0f00) >> 8);
pixelNew[3] = (byte)((pixelOldShort & 0xf000) >> 12); pixelNew[3] = (byte)((pixelOldShort & 0xf000) >> 12);
for (var j = 0; j < 4; j++) for (var j = 0; j < 4; j++)
pixelNew[j] = (byte)((pixelNew[j] << 4) | pixelNew[j]); pixelNew[j] = (byte)((pixelNew[j] << 4) | pixelNew[j]);
pixelNew.CopyTo(buff, i * 4); pixelNew.CopyTo(buff.Slice(i * 4));
} }
return true; return true;
} }
private bool DecodeRGB24(byte[] image_data, byte[] buff) private bool DecodeRGB24(ReadOnlySpan<byte> image_data, Span<byte> buff)
{ {
var size = m_Width * m_Height; var size = m_Width * m_Height;
for (var i = 0; i < size; i++) for (var i = 0; i < size; i++)
@ -343,7 +343,7 @@ namespace AssetStudio
return true; return true;
} }
private bool DecodeRGBA32(byte[] image_data, byte[] buff) private bool DecodeRGBA32(ReadOnlySpan<byte> image_data, Span<byte> buff)
{ {
for (var i = 0; i < outPutDataSize; i += 4) for (var i = 0; i < outPutDataSize; i += 4)
{ {
@ -355,7 +355,7 @@ namespace AssetStudio
return true; return true;
} }
private bool DecodeARGB32(byte[] image_data, byte[] buff) private bool DecodeARGB32(ReadOnlySpan<byte> image_data, Span<byte> buff)
{ {
for (var i = 0; i < outPutDataSize; i += 4) for (var i = 0; i < outPutDataSize; i += 4)
{ {
@ -367,12 +367,12 @@ namespace AssetStudio
return true; return true;
} }
private bool DecodeRGB565(byte[] image_data, byte[] buff) private bool DecodeRGB565(ReadOnlySpan<byte> image_data, Span<byte> buff)
{ {
var size = m_Width * m_Height; var size = m_Width * m_Height;
for (var i = 0; i < size; i++) for (var i = 0; i < size; i++)
{ {
var p = BitConverter.ToUInt16(image_data, i * 2); var p = BinaryPrimitives.ReadUInt16LittleEndian(image_data.Slice(i * 2));
buff[i * 4] = (byte)((p << 3) | (p >> 2 & 7)); buff[i * 4] = (byte)((p << 3) | (p >> 2 & 7));
buff[i * 4 + 1] = (byte)((p >> 3 & 0xfc) | (p >> 9 & 3)); buff[i * 4 + 1] = (byte)((p >> 3 & 0xfc) | (p >> 9 & 3));
buff[i * 4 + 2] = (byte)((p >> 8 & 0xf8) | (p >> 13)); buff[i * 4 + 2] = (byte)((p >> 8 & 0xf8) | (p >> 13));
@ -381,48 +381,48 @@ namespace AssetStudio
return true; return true;
} }
private bool DecodeR16(byte[] image_data, byte[] buff) private bool DecodeR16(ReadOnlySpan<byte> image_data, Span<byte> buff)
{ {
var size = m_Width * m_Height; var size = m_Width * m_Height;
for (var i = 0; i < size; i++) for (var i = 0; i < size; i++)
{ {
buff[i * 4] = 0; //b buff[i * 4] = 0; //b
buff[i * 4 + 1] = 0; //g buff[i * 4 + 1] = 0; //g
buff[i * 4 + 2] = DownScaleFrom16BitTo8Bit(BitConverter.ToUInt16(image_data, i * 2)); //r buff[i * 4 + 2] = DownScaleFrom16BitTo8Bit(BinaryPrimitives.ReadUInt16LittleEndian(image_data.Slice(i * 2))); //r
buff[i * 4 + 3] = 255; //a buff[i * 4 + 3] = 255; //a
} }
return true; return true;
} }
private bool DecodeDXT1(byte[] image_data, byte[] buff) private bool DecodeDXT1(ReadOnlySpan<byte> image_data, Span<byte> buff)
{ {
return TextureDecoder.DecodeDXT1(image_data, m_Width, m_Height, buff); return TextureDecoder.DecodeDXT1(image_data, m_Width, m_Height, buff);
} }
private bool DecodeDXT5(byte[] image_data, byte[] buff) private bool DecodeDXT5(ReadOnlySpan<byte> image_data, Span<byte> buff)
{ {
return TextureDecoder.DecodeDXT5(image_data, m_Width, m_Height, buff); return TextureDecoder.DecodeDXT5(image_data, m_Width, m_Height, buff);
} }
private bool DecodeRGBA4444(byte[] image_data, byte[] buff) private bool DecodeRGBA4444(ReadOnlySpan<byte> image_data, Span<byte> buff)
{ {
var size = m_Width * m_Height; var size = m_Width * m_Height;
var pixelNew = new byte[4]; var pixelNew = new byte[4].AsSpan();
for (var i = 0; i < size; i++) for (var i = 0; i < size; i++)
{ {
var pixelOldShort = BitConverter.ToUInt16(image_data, i * 2); var pixelOldShort = BinaryPrimitives.ReadUInt16LittleEndian(image_data.Slice(i * 2));
pixelNew[0] = (byte)((pixelOldShort & 0x00f0) >> 4); pixelNew[0] = (byte)((pixelOldShort & 0x00f0) >> 4);
pixelNew[1] = (byte)((pixelOldShort & 0x0f00) >> 8); pixelNew[1] = (byte)((pixelOldShort & 0x0f00) >> 8);
pixelNew[2] = (byte)((pixelOldShort & 0xf000) >> 12); pixelNew[2] = (byte)((pixelOldShort & 0xf000) >> 12);
pixelNew[3] = (byte)(pixelOldShort & 0x000f); pixelNew[3] = (byte)(pixelOldShort & 0x000f);
for (var j = 0; j < 4; j++) for (var j = 0; j < 4; j++)
pixelNew[j] = (byte)((pixelNew[j] << 4) | pixelNew[j]); pixelNew[j] = (byte)((pixelNew[j] << 4) | pixelNew[j]);
pixelNew.CopyTo(buff, i * 4); pixelNew.CopyTo(buff.Slice(i * 4));
} }
return true; return true;
} }
private bool DecodeBGRA32(byte[] image_data, byte[] buff) private bool DecodeBGRA32(ReadOnlySpan<byte> image_data, Span<byte> buff)
{ {
for (var i = 0; i < outPutDataSize; i += 4) for (var i = 0; i < outPutDataSize; i += 4)
{ {
@ -434,7 +434,7 @@ namespace AssetStudio
return true; return true;
} }
private bool DecodeRHalf(byte[] image_data, byte[] buff) private bool DecodeRHalf(ReadOnlySpan<byte> image_data, Span<byte> buff)
{ {
for (var i = 0; i < outPutDataSize; i += 4) for (var i = 0; i < outPutDataSize; i += 4)
{ {
@ -446,7 +446,7 @@ namespace AssetStudio
return true; return true;
} }
private bool DecodeRGHalf(byte[] image_data, byte[] buff) private bool DecodeRGHalf(ReadOnlySpan<byte> image_data, Span<byte> buff)
{ {
for (var i = 0; i < outPutDataSize; i += 4) for (var i = 0; i < outPutDataSize; i += 4)
{ {
@ -458,7 +458,7 @@ namespace AssetStudio
return true; return true;
} }
private bool DecodeRGBAHalf(byte[] image_data, byte[] buff) private bool DecodeRGBAHalf(ReadOnlySpan<byte> image_data, Span<byte> buff)
{ {
for (var i = 0; i < outPutDataSize; i += 4) for (var i = 0; i < outPutDataSize; i += 4)
{ {
@ -470,7 +470,7 @@ namespace AssetStudio
return true; return true;
} }
private bool DecodeRFloat(byte[] image_data, byte[] buff) private bool DecodeRFloat(byte[] image_data, Span<byte> buff)
{ {
for (var i = 0; i < outPutDataSize; i += 4) for (var i = 0; i < outPutDataSize; i += 4)
{ {
@ -482,7 +482,7 @@ namespace AssetStudio
return true; return true;
} }
private bool DecodeRGFloat(byte[] image_data, byte[] buff) private bool DecodeRGFloat(byte[] image_data, Span<byte> buff)
{ {
for (var i = 0; i < outPutDataSize; i += 4) for (var i = 0; i < outPutDataSize; i += 4)
{ {
@ -494,7 +494,7 @@ namespace AssetStudio
return true; return true;
} }
private bool DecodeRGBAFloat(byte[] image_data, byte[] buff) private bool DecodeRGBAFloat(byte[] image_data, Span<byte> buff)
{ {
for (var i = 0; i < outPutDataSize; i += 4) for (var i = 0; i < outPutDataSize; i += 4)
{ {
@ -512,7 +512,7 @@ namespace AssetStudio
return (byte)(byte.MaxValue < x ? byte.MaxValue : (x > byte.MinValue ? x : byte.MinValue)); return (byte)(byte.MaxValue < x ? byte.MaxValue : (x > byte.MinValue ? x : byte.MinValue));
} }
private bool DecodeYUY2(byte[] image_data, byte[] buff) private bool DecodeYUY2(ReadOnlySpan<byte> image_data, Span<byte> buff)
{ {
int p = 0; int p = 0;
int o = 0; int o = 0;
@ -542,11 +542,11 @@ namespace AssetStudio
return true; return true;
} }
private bool DecodeRGB9e5Float(byte[] image_data, byte[] buff) private bool DecodeRGB9e5Float(ReadOnlySpan<byte> image_data, Span<byte> buff)
{ {
for (var i = 0; i < outPutDataSize; i += 4) for (var i = 0; i < outPutDataSize; i += 4)
{ {
var n = BitConverter.ToInt32(image_data, i); var n = BinaryPrimitives.ReadInt32LittleEndian(image_data.Slice(i));
var scale = n >> 27 & 0x1f; var scale = n >> 27 & 0x1f;
var scalef = MathF.Pow(2, scale - 24); var scalef = MathF.Pow(2, scale - 24);
var b = n >> 18 & 0x1ff; var b = n >> 18 & 0x1ff;
@ -560,27 +560,27 @@ namespace AssetStudio
return true; return true;
} }
private bool DecodeBC4(byte[] image_data, byte[] buff) private bool DecodeBC4(ReadOnlySpan<byte> image_data, Span<byte> buff)
{ {
return TextureDecoder.DecodeBC4(image_data, m_Width, m_Height, buff); return TextureDecoder.DecodeBC4(image_data, m_Width, m_Height, buff);
} }
private bool DecodeBC5(byte[] image_data, byte[] buff) private bool DecodeBC5(ReadOnlySpan<byte> image_data, Span<byte> buff)
{ {
return TextureDecoder.DecodeBC5(image_data, m_Width, m_Height, buff); return TextureDecoder.DecodeBC5(image_data, m_Width, m_Height, buff);
} }
private bool DecodeBC6H(byte[] image_data, byte[] buff) private bool DecodeBC6H(ReadOnlySpan<byte> image_data, Span<byte> buff)
{ {
return TextureDecoder.DecodeBC6(image_data, m_Width, m_Height, buff); return TextureDecoder.DecodeBC6(image_data, m_Width, m_Height, buff);
} }
private bool DecodeBC7(byte[] image_data, byte[] buff) private bool DecodeBC7(ReadOnlySpan<byte> image_data, Span<byte> buff)
{ {
return TextureDecoder.DecodeBC7(image_data, m_Width, m_Height, buff); return TextureDecoder.DecodeBC7(image_data, m_Width, m_Height, buff);
} }
private bool DecodeDXT1Crunched(byte[] image_data, byte[] buff) private bool DecodeDXT1Crunched(ReadOnlySpan<byte> image_data, Span<byte> buff)
{ {
if (UnpackCrunch(image_data, out var result)) if (UnpackCrunch(image_data, out var result))
{ {
@ -592,7 +592,7 @@ namespace AssetStudio
return false; return false;
} }
private bool DecodeDXT5Crunched(byte[] image_data, byte[] buff) private bool DecodeDXT5Crunched(ReadOnlySpan<byte> image_data, Span<byte> buff)
{ {
if (UnpackCrunch(image_data, out var result)) if (UnpackCrunch(image_data, out var result))
{ {
@ -604,67 +604,67 @@ namespace AssetStudio
return false; return false;
} }
private bool DecodePVRTC(byte[] image_data, byte[] buff, bool is2bpp) private bool DecodePVRTC(ReadOnlySpan<byte> image_data, Span<byte> buff, bool is2bpp)
{ {
return TextureDecoder.DecodePVRTC(image_data, m_Width, m_Height, buff, is2bpp); return TextureDecoder.DecodePVRTC(image_data, m_Width, m_Height, buff, is2bpp);
} }
private bool DecodeETC1(byte[] image_data, byte[] buff) private bool DecodeETC1(ReadOnlySpan<byte> image_data, Span<byte> buff)
{ {
return TextureDecoder.DecodeETC1(image_data, m_Width, m_Height, buff); return TextureDecoder.DecodeETC1(image_data, m_Width, m_Height, buff);
} }
private bool DecodeATCRGB4(byte[] image_data, byte[] buff) private bool DecodeATCRGB4(ReadOnlySpan<byte> image_data, Span<byte> buff)
{ {
return TextureDecoder.DecodeATCRGB4(image_data, m_Width, m_Height, buff); return TextureDecoder.DecodeATCRGB4(image_data, m_Width, m_Height, buff);
} }
private bool DecodeATCRGBA8(byte[] image_data, byte[] buff) private bool DecodeATCRGBA8(ReadOnlySpan<byte> image_data, Span<byte> buff)
{ {
return TextureDecoder.DecodeATCRGBA8(image_data, m_Width, m_Height, buff); return TextureDecoder.DecodeATCRGBA8(image_data, m_Width, m_Height, buff);
} }
private bool DecodeEACR(byte[] image_data, byte[] buff) private bool DecodeEACR(ReadOnlySpan<byte> image_data, Span<byte> buff)
{ {
return TextureDecoder.DecodeEACR(image_data, m_Width, m_Height, buff); return TextureDecoder.DecodeEACR(image_data, m_Width, m_Height, buff);
} }
private bool DecodeEACRSigned(byte[] image_data, byte[] buff) private bool DecodeEACRSigned(ReadOnlySpan<byte> image_data, Span<byte> buff)
{ {
return TextureDecoder.DecodeEACRSigned(image_data, m_Width, m_Height, buff); return TextureDecoder.DecodeEACRSigned(image_data, m_Width, m_Height, buff);
} }
private bool DecodeEACRG(byte[] image_data, byte[] buff) private bool DecodeEACRG(ReadOnlySpan<byte> image_data, Span<byte> buff)
{ {
return TextureDecoder.DecodeEACRG(image_data, m_Width, m_Height, buff); return TextureDecoder.DecodeEACRG(image_data, m_Width, m_Height, buff);
} }
private bool DecodeEACRGSigned(byte[] image_data, byte[] buff) private bool DecodeEACRGSigned(ReadOnlySpan<byte> image_data, Span<byte> buff)
{ {
return TextureDecoder.DecodeEACRGSigned(image_data, m_Width, m_Height, buff); return TextureDecoder.DecodeEACRGSigned(image_data, m_Width, m_Height, buff);
} }
private bool DecodeETC2(byte[] image_data, byte[] buff) private bool DecodeETC2(ReadOnlySpan<byte> image_data, Span<byte> buff)
{ {
return TextureDecoder.DecodeETC2(image_data, m_Width, m_Height, buff); return TextureDecoder.DecodeETC2(image_data, m_Width, m_Height, buff);
} }
private bool DecodeETC2A1(byte[] image_data, byte[] buff) private bool DecodeETC2A1(ReadOnlySpan<byte> image_data, Span<byte> buff)
{ {
return TextureDecoder.DecodeETC2A1(image_data, m_Width, m_Height, buff); return TextureDecoder.DecodeETC2A1(image_data, m_Width, m_Height, buff);
} }
private bool DecodeETC2A8(byte[] image_data, byte[] buff) private bool DecodeETC2A8(ReadOnlySpan<byte> image_data, Span<byte> buff)
{ {
return TextureDecoder.DecodeETC2A8(image_data, m_Width, m_Height, buff); return TextureDecoder.DecodeETC2A8(image_data, m_Width, m_Height, buff);
} }
private bool DecodeASTC(byte[] image_data, byte[] buff, int blocksize) private bool DecodeASTC(ReadOnlySpan<byte> image_data, Span<byte> buff, int blocksize)
{ {
return TextureDecoder.DecodeASTC(image_data, m_Width, m_Height, blocksize, blocksize, buff); return TextureDecoder.DecodeASTC(image_data, m_Width, m_Height, blocksize, blocksize, buff);
} }
private bool DecodeRG16(byte[] image_data, byte[] buff) private bool DecodeRG16(ReadOnlySpan<byte> image_data, Span<byte> buff)
{ {
var size = m_Width * m_Height; var size = m_Width * m_Height;
for (var i = 0; i < size; i++) for (var i = 0; i < size; i++)
@ -677,7 +677,7 @@ namespace AssetStudio
return true; return true;
} }
private bool DecodeR8(byte[] image_data, byte[] buff) private bool DecodeR8(ReadOnlySpan<byte> image_data, Span<byte> buff)
{ {
var size = m_Width * m_Height; var size = m_Width * m_Height;
for (var i = 0; i < size; i++) for (var i = 0; i < size; i++)
@ -690,7 +690,7 @@ namespace AssetStudio
return true; return true;
} }
private bool DecodeETC1Crunched(byte[] image_data, byte[] buff) private bool DecodeETC1Crunched(ReadOnlySpan<byte> image_data, Span<byte> buff)
{ {
if (UnpackCrunch(image_data, out var result)) if (UnpackCrunch(image_data, out var result))
{ {
@ -702,7 +702,7 @@ namespace AssetStudio
return false; return false;
} }
private bool DecodeETC2A8Crunched(byte[] image_data, byte[] buff) private bool DecodeETC2A8Crunched(ReadOnlySpan<byte> image_data, Span<byte> buff)
{ {
if (UnpackCrunch(image_data, out var result)) if (UnpackCrunch(image_data, out var result))
{ {
@ -720,44 +720,44 @@ namespace AssetStudio
return (byte)(((component * 255) + 32895) >> 16); return (byte)(((component * 255) + 32895) >> 16);
} }
private bool DecodeRG32(byte[] image_data, byte[] buff) private bool DecodeRG32(ReadOnlySpan<byte> image_data, Span<byte> buff)
{ {
for (var i = 0; i < outPutDataSize; i += 4) for (var i = 0; i < outPutDataSize; i += 4)
{ {
buff[i] = 0; //b buff[i] = 0; //b
buff[i + 1] = DownScaleFrom16BitTo8Bit(BitConverter.ToUInt16(image_data, i + 2)); //g buff[i + 1] = DownScaleFrom16BitTo8Bit(BinaryPrimitives.ReadUInt16LittleEndian(image_data.Slice(i + 2))); //g
buff[i + 2] = DownScaleFrom16BitTo8Bit(BitConverter.ToUInt16(image_data, i)); //r buff[i + 2] = DownScaleFrom16BitTo8Bit(BinaryPrimitives.ReadUInt16LittleEndian(image_data.Slice(i))); //r
buff[i + 3] = byte.MaxValue; //a buff[i + 3] = byte.MaxValue; //a
} }
return true; return true;
} }
private bool DecodeRGB48(byte[] image_data, byte[] buff) private bool DecodeRGB48(ReadOnlySpan<byte> image_data, Span<byte> buff)
{ {
var size = m_Width * m_Height; var size = m_Width * m_Height;
for (var i = 0; i < size; i++) for (var i = 0; i < size; i++)
{ {
buff[i * 4] = DownScaleFrom16BitTo8Bit(BitConverter.ToUInt16(image_data, i * 6 + 4)); //b buff[i * 4] = DownScaleFrom16BitTo8Bit(BinaryPrimitives.ReadUInt16LittleEndian(image_data.Slice(i * 6 + 4))); //b
buff[i * 4 + 1] = DownScaleFrom16BitTo8Bit(BitConverter.ToUInt16(image_data, i * 6 + 2)); //g buff[i * 4 + 1] = DownScaleFrom16BitTo8Bit(BinaryPrimitives.ReadUInt16LittleEndian(image_data.Slice(i * 6 + 2))); //g
buff[i * 4 + 2] = DownScaleFrom16BitTo8Bit(BitConverter.ToUInt16(image_data, i * 6)); //r buff[i * 4 + 2] = DownScaleFrom16BitTo8Bit(BinaryPrimitives.ReadUInt16LittleEndian(image_data.Slice(i * 6))); //r
buff[i * 4 + 3] = byte.MaxValue; //a buff[i * 4 + 3] = byte.MaxValue; //a
} }
return true; return true;
} }
private bool DecodeRGBA64(byte[] image_data, byte[] buff) private bool DecodeRGBA64(ReadOnlySpan<byte> image_data, Span<byte> buff)
{ {
for (var i = 0; i < outPutDataSize; i += 4) for (var i = 0; i < outPutDataSize; i += 4)
{ {
buff[i] = DownScaleFrom16BitTo8Bit(BitConverter.ToUInt16(image_data, i * 2 + 4)); //b buff[i] = DownScaleFrom16BitTo8Bit(BinaryPrimitives.ReadUInt16LittleEndian(image_data.Slice(i * 2 + 4))); //b
buff[i + 1] = DownScaleFrom16BitTo8Bit(BitConverter.ToUInt16(image_data, i * 2 + 2)); //g buff[i + 1] = DownScaleFrom16BitTo8Bit(BinaryPrimitives.ReadUInt16LittleEndian(image_data.Slice(i * 2 + 2))); //g
buff[i + 2] = DownScaleFrom16BitTo8Bit(BitConverter.ToUInt16(image_data, i * 2)); //r buff[i + 2] = DownScaleFrom16BitTo8Bit(BinaryPrimitives.ReadUInt16LittleEndian(image_data.Slice(i * 2))); //r
buff[i + 3] = DownScaleFrom16BitTo8Bit(BitConverter.ToUInt16(image_data, i * 2 + 6)); //a buff[i + 3] = DownScaleFrom16BitTo8Bit(BinaryPrimitives.ReadUInt16LittleEndian(image_data.Slice(i * 2 + 6))); //a
} }
return true; return true;
} }
private bool UnpackCrunch(byte[] image_data, out byte[] result) private bool UnpackCrunch(ReadOnlySpan<byte> image_data, out ReadOnlySpan<byte> result)
{ {
if (version >= (2017, 3) //2017.3 and up if (version >= (2017, 3) //2017.3 and up
|| m_TextureFormat == TextureFormat.ETC_RGB4Crunched || m_TextureFormat == TextureFormat.ETC_RGB4Crunched
@ -769,11 +769,7 @@ namespace AssetStudio
{ {
result = TextureDecoder.UnpackCrunch(image_data); result = TextureDecoder.UnpackCrunch(image_data);
} }
if (result != null) return !result.IsEmpty;
{
return true;
}
return false;
} }
} }
} }

View File

@ -8,6 +8,10 @@
<DebugType>embedded</DebugType> <DebugType>embedded</DebugType>
</PropertyGroup> </PropertyGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net472' ">
<PackageReference Include="System.Memory" Version="4.5.5" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\AssetStudio.PInvoke\AssetStudio.PInvoke.csproj" /> <ProjectReference Include="..\AssetStudio.PInvoke\AssetStudio.PInvoke.csproj" />
</ItemGroup> </ItemGroup>

View File

@ -16,7 +16,7 @@ namespace Texture2DDecoder
} }
#endif #endif
public static bool DecodeDXT1(byte[] data, int width, int height, byte[] image) public static bool DecodeDXT1(ReadOnlySpan<byte> data, int width, int height, Span<byte> image)
{ {
fixed (byte* pData = data) fixed (byte* pData = data)
{ {
@ -27,7 +27,7 @@ namespace Texture2DDecoder
} }
} }
public static bool DecodeDXT5(byte[] data, int width, int height, byte[] image) public static bool DecodeDXT5(ReadOnlySpan<byte> data, int width, int height, Span<byte> image)
{ {
fixed (byte* pData = data) fixed (byte* pData = data)
{ {
@ -38,7 +38,7 @@ namespace Texture2DDecoder
} }
} }
public static bool DecodePVRTC(byte[] data, int width, int height, byte[] image, bool is2bpp) public static bool DecodePVRTC(ReadOnlySpan<byte> data, int width, int height, Span<byte> image, bool is2bpp)
{ {
fixed (byte* pData = data) fixed (byte* pData = data)
{ {
@ -49,7 +49,7 @@ namespace Texture2DDecoder
} }
} }
public static bool DecodeETC1(byte[] data, int width, int height, byte[] image) public static bool DecodeETC1(ReadOnlySpan<byte> data, int width, int height, Span<byte> image)
{ {
fixed (byte* pData = data) fixed (byte* pData = data)
{ {
@ -60,7 +60,7 @@ namespace Texture2DDecoder
} }
} }
public static bool DecodeETC2(byte[] data, int width, int height, byte[] image) public static bool DecodeETC2(ReadOnlySpan<byte> data, int width, int height, Span<byte> image)
{ {
fixed (byte* pData = data) fixed (byte* pData = data)
{ {
@ -71,7 +71,7 @@ namespace Texture2DDecoder
} }
} }
public static bool DecodeETC2A1(byte[] data, int width, int height, byte[] image) public static bool DecodeETC2A1(ReadOnlySpan<byte> data, int width, int height, Span<byte> image)
{ {
fixed (byte* pData = data) fixed (byte* pData = data)
{ {
@ -82,7 +82,7 @@ namespace Texture2DDecoder
} }
} }
public static bool DecodeETC2A8(byte[] data, int width, int height, byte[] image) public static bool DecodeETC2A8(ReadOnlySpan<byte> data, int width, int height, Span<byte> image)
{ {
fixed (byte* pData = data) fixed (byte* pData = data)
{ {
@ -93,7 +93,7 @@ namespace Texture2DDecoder
} }
} }
public static bool DecodeEACR(byte[] data, int width, int height, byte[] image) public static bool DecodeEACR(ReadOnlySpan<byte> data, int width, int height, Span<byte> image)
{ {
fixed (byte* pData = data) fixed (byte* pData = data)
{ {
@ -104,7 +104,7 @@ namespace Texture2DDecoder
} }
} }
public static bool DecodeEACRSigned(byte[] data, int width, int height, byte[] image) public static bool DecodeEACRSigned(ReadOnlySpan<byte> data, int width, int height, Span<byte> image)
{ {
fixed (byte* pData = data) fixed (byte* pData = data)
{ {
@ -115,7 +115,7 @@ namespace Texture2DDecoder
} }
} }
public static bool DecodeEACRG(byte[] data, int width, int height, byte[] image) public static bool DecodeEACRG(ReadOnlySpan<byte> data, int width, int height, Span<byte> image)
{ {
fixed (byte* pData = data) fixed (byte* pData = data)
{ {
@ -126,7 +126,7 @@ namespace Texture2DDecoder
} }
} }
public static bool DecodeEACRGSigned(byte[] data, int width, int height, byte[] image) public static bool DecodeEACRGSigned(ReadOnlySpan<byte> data, int width, int height, Span<byte> image)
{ {
fixed (byte* pData = data) fixed (byte* pData = data)
{ {
@ -137,7 +137,7 @@ namespace Texture2DDecoder
} }
} }
public static bool DecodeBC4(byte[] data, int width, int height, byte[] image) public static bool DecodeBC4(ReadOnlySpan<byte> data, int width, int height, Span<byte> image)
{ {
fixed (byte* pData = data) fixed (byte* pData = data)
{ {
@ -148,7 +148,7 @@ namespace Texture2DDecoder
} }
} }
public static bool DecodeBC5(byte[] data, int width, int height, byte[] image) public static bool DecodeBC5(ReadOnlySpan<byte> data, int width, int height, Span<byte> image)
{ {
fixed (byte* pData = data) fixed (byte* pData = data)
{ {
@ -159,7 +159,7 @@ namespace Texture2DDecoder
} }
} }
public static bool DecodeBC6(byte[] data, int width, int height, byte[] image) public static bool DecodeBC6(ReadOnlySpan<byte> data, int width, int height, Span<byte> image)
{ {
fixed (byte* pData = data) fixed (byte* pData = data)
{ {
@ -170,7 +170,7 @@ namespace Texture2DDecoder
} }
} }
public static bool DecodeBC7(byte[] data, int width, int height, byte[] image) public static bool DecodeBC7(ReadOnlySpan<byte> data, int width, int height, Span<byte> image)
{ {
fixed (byte* pData = data) fixed (byte* pData = data)
{ {
@ -181,7 +181,7 @@ namespace Texture2DDecoder
} }
} }
public static bool DecodeATCRGB4(byte[] data, int width, int height, byte[] image) public static bool DecodeATCRGB4(ReadOnlySpan<byte> data, int width, int height, Span<byte> image)
{ {
fixed (byte* pData = data) fixed (byte* pData = data)
{ {
@ -192,7 +192,7 @@ namespace Texture2DDecoder
} }
} }
public static bool DecodeATCRGBA8(byte[] data, int width, int height, byte[] image) public static bool DecodeATCRGBA8(ReadOnlySpan<byte> data, int width, int height, Span<byte> image)
{ {
fixed (byte* pData = data) fixed (byte* pData = data)
{ {
@ -203,7 +203,7 @@ namespace Texture2DDecoder
} }
} }
public static bool DecodeASTC(byte[] data, int width, int height, int blockWidth, int blockHeight, byte[] image) public static bool DecodeASTC(ReadOnlySpan<byte> data, int width, int height, int blockWidth, int blockHeight, Span<byte> image)
{ {
fixed (byte* pData = data) fixed (byte* pData = data)
{ {
@ -214,7 +214,7 @@ namespace Texture2DDecoder
} }
} }
public static byte[] UnpackCrunch(byte[] data) public static byte[] UnpackCrunch(ReadOnlySpan<byte> data)
{ {
void* pBuffer; void* pBuffer;
uint bufferSize; uint bufferSize;
@ -238,7 +238,7 @@ namespace Texture2DDecoder
return result; return result;
} }
public static byte[] UnpackUnityCrunch(byte[] data) public static byte[] UnpackUnityCrunch(ReadOnlySpan<byte> data)
{ {
void* pBuffer; void* pBuffer;
uint bufferSize; uint bufferSize;
@ -261,6 +261,5 @@ namespace Texture2DDecoder
return result; return result;
} }
} }
} }