1+ using System . Buffers . Binary ;
2+
13namespace AssetRipper . TextureDecoder . Astc
24{
35 public static partial class AstcDecoder
@@ -18,16 +20,13 @@ public static int DecodeASTC(ReadOnlySpan<byte> input, int width, int height, in
1820
1921 public unsafe static int DecodeASTC ( ReadOnlySpan < byte > input , int width , int height , int blockWidth , int blockHeight , Span < byte > output )
2022 {
21- fixed ( byte * inputPtr = input )
23+ fixed ( byte * outputPtr = output )
2224 {
23- fixed ( byte * outputPtr = output )
24- {
25- return DecodeASTC ( inputPtr , width , height , blockWidth , blockHeight , outputPtr ) ;
26- }
25+ return DecodeASTC ( input , width , height , blockWidth , blockHeight , outputPtr ) ;
2726 }
2827 }
2928
30- private unsafe static int DecodeASTC ( byte * input , int width , int height , int blockWidth , int blockHeight , byte * output )
29+ private unsafe static int DecodeASTC ( ReadOnlySpan < byte > input , int width , int height , int blockWidth , int blockHeight , byte * output )
3130 {
3231 ValidateBlockDimension ( blockWidth ) ;
3332 ValidateBlockDimension ( blockHeight ) ;
@@ -40,7 +39,7 @@ private unsafe static int DecodeASTC(byte* input, int width, int height, int blo
4039 {
4140 for ( int s = 0 ; s < bcw ; s ++ , inputOffset += 16 )
4241 {
43- DecodeBlock ( input + inputOffset , blockWidth , blockHeight , buf ) ;
42+ DecodeBlock ( input [ inputOffset .. ] , blockWidth , blockHeight , buf ) ;
4443 int clen = s < bcw - 1 ? blockWidth : clen_last ;
4544 uint * outputPtr = ( uint * ) ( output + ( t * blockHeight * 4 * width + s * 4 * blockWidth ) ) ;
4645 for ( int i = 0 , y = t * blockHeight ; i < blockHeight && y < height ; i ++ , y ++ )
@@ -57,7 +56,7 @@ private unsafe static int DecodeASTC(byte* input, int width, int height, int blo
5756 return inputOffset ;
5857 }
5958
60- private unsafe static void DecodeBlock ( byte * input , int blockWidth , int blockHeight , Span < uint > output )
59+ private unsafe static void DecodeBlock ( ReadOnlySpan < byte > input , int blockWidth , int blockHeight , Span < uint > output )
6160 {
6261 if ( input [ 0 ] == 0xfc && ( input [ 1 ] & 1 ) == 1 )
6362 {
@@ -83,7 +82,7 @@ private unsafe static void DecodeBlock(byte* input, int blockWidth, int blockHei
8382 }
8483 }
8584
86- private unsafe static void DecodeBlockParameters ( byte * input , ref BlockData pBlock )
85+ private unsafe static void DecodeBlockParameters ( ReadOnlySpan < byte > input , ref BlockData pBlock )
8786 {
8887 pBlock . dual_plane = ( input [ 1 ] & 4 ) >> 2 ;
8988 pBlock . weight_range = ( input [ 0 ] >> 4 & 1 ) | ( input [ 1 ] << 2 & 8 ) ;
@@ -94,16 +93,16 @@ private unsafe static void DecodeBlockParameters(byte* input, ref BlockData pBlo
9493 switch ( input [ 0 ] & 0xc )
9594 {
9695 case 0 :
97- pBlock . width = ( * ( ( int * ) input ) >> 7 & 3 ) + 4 ;
96+ pBlock . width = ( BinaryPrimitives . ReadInt32LittleEndian ( input ) >> 7 & 3 ) + 4 ;
9897 pBlock . height = ( input [ 0 ] >> 5 & 3 ) + 2 ;
9998 break ;
10099 case 4 :
101- pBlock . width = ( * ( ( int * ) input ) >> 7 & 3 ) + 8 ;
100+ pBlock . width = ( BinaryPrimitives . ReadInt32LittleEndian ( input ) >> 7 & 3 ) + 8 ;
102101 pBlock . height = ( input [ 0 ] >> 5 & 3 ) + 2 ;
103102 break ;
104103 case 8 :
105104 pBlock . width = ( input [ 0 ] >> 5 & 3 ) + 2 ;
106- pBlock . height = ( * ( ( int * ) input ) >> 7 & 3 ) + 8 ;
105+ pBlock . height = ( BinaryPrimitives . ReadInt32LittleEndian ( input ) >> 7 & 3 ) + 8 ;
107106 break ;
108107 case 12 :
109108 if ( ( input [ 1 ] & 1 ) != 0 )
@@ -122,7 +121,7 @@ private unsafe static void DecodeBlockParameters(byte* input, ref BlockData pBlo
122121 else
123122 {
124123 pBlock . weight_range |= input [ 0 ] >> 1 & 6 ;
125- switch ( ( * ( ( int * ) input ) ) & 0x180 )
124+ switch ( BinaryPrimitives . ReadInt32LittleEndian ( input ) & 0x180 )
126125 {
127126 case 0 :
128127 pBlock . width = 12 ;
@@ -162,12 +161,12 @@ private unsafe static void DecodeBlockParameters(byte* input, ref BlockData pBlo
162161 } ;
163162 if ( pBlock . part_num == 1 )
164163 {
165- pBlock . cem [ 0 ] = * ( ( int * ) ( input + 1 ) ) >> 5 & 0xf ;
164+ pBlock . cem [ 0 ] = BinaryPrimitives . ReadInt32LittleEndian ( input [ 1 .. ] ) >> 5 & 0xf ;
166165 config_bits = 17 ;
167166 }
168167 else
169168 {
170- cem_base = * ( ( int * ) ( input + 2 ) ) >> 7 & 3 ;
169+ cem_base = BinaryPrimitives . ReadInt32LittleEndian ( input [ 2 .. ] ) >> 7 & 3 ;
171170 if ( cem_base == 0 )
172171 {
173172 int cem = input [ 3 ] >> 1 & 0xf ;
@@ -236,7 +235,7 @@ private unsafe static void DecodeBlockParameters(byte* input, ref BlockData pBlo
236235 }
237236 }
238237
239- private unsafe static void DecodeEndpoints( byte * input , ref BlockData pBlock )
238+ private unsafe static void DecodeEndpoints ( ReadOnlySpan < byte > input , ref BlockData pBlock )
240239 {
241240 Span < IntSeqData > epSeq = stackalloc IntSeqData [ 32 ] ;
242241 DecodeIntseq ( input , pBlock . part_num == 1 ? 17 : 29 , CemTableA [ pBlock . cem_range ] , CemTableB [ pBlock . cem_range ] , pBlock . endpoint_value_num , false , epSeq ) ;
@@ -483,7 +482,7 @@ private unsafe static void DecodeEndpoints(byte* input, ref BlockData pBlock)
483482 }
484483 }
485484
486- private unsafe static void DecodeWeights( byte * input , ref BlockData block )
485+ private unsafe static void DecodeWeights ( ReadOnlySpan < byte > input , ref BlockData block )
487486 {
488487 Span < IntSeqData > wSeq = stackalloc IntSeqData [ 128 ] ;
489488 DecodeIntseq ( input , 128 , WeightPrecTableA [ block . weight_range ] , WeightPrecTableB [ block . weight_range ] , block . weight_num , true , wSeq ) ;
@@ -639,10 +638,10 @@ private unsafe static void DecodeWeights(byte* input, ref BlockData block)
639638 }
640639 }
641640
642- private unsafe static void SelectPartition ( byte * input , ref BlockData block )
641+ private unsafe static void SelectPartition ( ReadOnlySpan < byte > input , ref BlockData block )
643642 {
644643 bool small_block = block . bw * block . bh < 31 ;
645- int seed = ( * ( ( int * ) input ) >> 13 & 0x3ff ) | ( block . part_num - 1 ) << 10 ;
644+ int seed = ( BinaryPrimitives . ReadInt32LittleEndian ( input ) >> 13 & 0x3ff ) | ( block . part_num - 1 ) << 10 ;
646645
647646 uint rnum ;
648647 unchecked
@@ -776,7 +775,7 @@ private unsafe static void ApplicateColor(BlockData block, Span<uint> output)
776775 }
777776 }
778777
779- private unsafe static void DecodeIntseq ( byte * input , int offset , int a , int b , int count , bool reverse , Span < IntSeqData > _out )
778+ private unsafe static void DecodeIntseq ( ReadOnlySpan < byte > input , int offset , int a , int b , int count , bool reverse , Span < IntSeqData > _out )
780779 {
781780 if ( count <= 0 )
782781 {
@@ -928,13 +927,13 @@ private static ulong BitReverseU64(ulong d, int bits)
928927 }
929928
930929 [ MethodImpl ( MethodImplOptions . AggressiveInlining | MethodImplOptions . AggressiveOptimization ) ]
931- private unsafe static int GetBits( byte * input , int bit , int len )
930+ private unsafe static int GetBits ( ReadOnlySpan < byte > input , int bit , int len )
932931 {
933- return ( * ( ( int * ) ( input + bit / 8 ) ) >> ( bit % 8 ) ) & ( ( 1 << len ) - 1 ) ;
932+ return ( BinaryPrimitives . ReadInt32LittleEndian ( input [ ( bit / 8 ) .. ] ) >> ( bit % 8 ) ) & ( ( 1 << len ) - 1 ) ;
934933 }
935934
936935 [ MethodImpl ( MethodImplOptions . AggressiveInlining | MethodImplOptions . AggressiveOptimization ) ]
937- private unsafe static ulong GetBits64( byte * input , int bit , int len )
936+ private unsafe static ulong GetBits64 ( ReadOnlySpan < byte > input , int bit , int len )
938937 {
939938 ulong mask = len == 64 ? 0xffffffffffffffff : ( 1UL << len ) - 1 ;
940939 if ( len < 1 )
@@ -943,19 +942,19 @@ private unsafe static ulong GetBits64(byte* input, int bit, int len)
943942 }
944943 else if ( bit >= 64 )
945944 {
946- return * ( ( ulong * ) ( input + 8 ) ) >> ( bit - 64 ) & mask ;
945+ return BinaryPrimitives . ReadUInt64LittleEndian ( input [ sizeof ( ulong ) .. ] ) >> ( bit - 64 ) & mask ;
947946 }
948947 else if ( bit <= 0 )
949948 {
950- return * ( ( ulong * ) input ) << - bit & mask ;
949+ return BinaryPrimitives . ReadUInt64LittleEndian ( input ) << - bit & mask ;
951950 }
952951 else if ( bit + len <= 64 )
953952 {
954- return * ( ( ulong * ) input ) >> bit & mask ;
953+ return BinaryPrimitives . ReadUInt64LittleEndian ( input ) >> bit & mask ;
955954 }
956955 else
957956 {
958- return ( * ( ( ulong * ) input ) >> bit | * ( ( ulong * ) ( input + 8 ) ) << ( 64 - bit ) ) & mask ;
957+ return ( BinaryPrimitives . ReadUInt64LittleEndian ( input ) >> bit | BinaryPrimitives . ReadUInt64LittleEndian ( input [ sizeof ( ulong ) .. ] ) << ( 64 - bit ) ) & mask ;
959958 }
960959 }
961960
0 commit comments