@@ -15,7 +15,6 @@ namespace Zeiss.PiWeb.Volume.Block;
15
15
16
16
using System ;
17
17
using System . Buffers ;
18
- using System . IO ;
19
18
using System . Runtime . InteropServices ;
20
19
using System . Runtime . Intrinsics ;
21
20
using System . Threading ;
@@ -42,37 +41,49 @@ internal static void Decode( byte[] data,
42
41
IProgress < VolumeSliceDefinition > ? progress = null ,
43
42
CancellationToken ct = default )
44
43
{
45
- var reader = new BinaryReader ( new MemoryStream ( data ) ) ;
46
- var ( _, sizeX , sizeY , sizeZ , quantization ) = BlockVolumeMetaData . Read ( reader ) ;
44
+ var header = BlockVolumeMetaData . Create ( data ) ;
45
+ var ( _, sizeX , sizeY , sizeZ , quantization ) = header ;
47
46
var ( bcx , bcy , bcz ) = BlockVolume . GetBlockCount ( sizeX , sizeY , sizeZ ) ;
48
47
var blockCount = bcx * bcy ;
49
48
var encodedBlockInfos = new EncodedBlockInfo [ blockCount ] ;
49
+ var position = BlockVolumeMetaData . HeaderLength ;
50
+ var dataSpan = data . AsSpan ( ) ;
51
+
52
+ Quantization . Invert ( quantization ) ;
50
53
51
54
for ( ushort biz = 0 ; biz < bcz ; biz ++ )
52
55
{
53
56
ct . ThrowIfCancellationRequested ( ) ;
54
57
55
- var layerLength = reader . ReadInt32 ( ) ;
58
+ var layerLength = MemoryMarshal . Read < int > ( dataSpan . Slice ( position , sizeof ( int ) ) ) ;
59
+ position += sizeof ( int ) ;
56
60
if ( layerPredicate ? . Invoke ( biz ) is false )
57
61
{
58
- reader . BaseStream . Seek ( layerLength , SeekOrigin . Current ) ;
62
+ position += layerLength ;
59
63
continue ;
60
64
}
61
65
62
- ReadBlockInfos ( reader , blockCount , encodedBlockInfos ) ;
66
+ ReadLayer ( dataSpan , position , blockCount , encodedBlockInfos ) ;
63
67
DecodeLayer ( data , encodedBlockInfos , bcx , bcy , biz , quantization , blockPredicate , blockAction ) ;
64
68
65
69
progress ? . Report ( new VolumeSliceDefinition ( Direction . Z , ( ushort ) ( biz * BlockVolume . N ) ) ) ;
70
+
71
+ position += layerLength ;
66
72
}
67
73
}
68
74
69
- private static void ReadBlockInfos ( BinaryReader reader , int blockCount , EncodedBlockInfo [ ] encodedBlockInfos )
75
+ private static void ReadLayer ( ReadOnlySpan < byte > dataSpan , int position , int blockCount , EncodedBlockInfo [ ] encodedBlockInfos )
70
76
{
71
77
for ( var i = 0 ; i < blockCount ; i ++ )
72
78
{
73
- var encodedBlockInfo = EncodedBlockInfo . Read ( reader ) ;
74
- reader . BaseStream . Seek ( encodedBlockInfo . Info . Length , SeekOrigin . Current ) ;
79
+ var value = MemoryMarshal . Read < ushort > ( dataSpan [ position .. ] ) ;
80
+
81
+ position += sizeof ( ushort ) ;
82
+
83
+ var encodedBlockInfo = new EncodedBlockInfo ( position , BlockInfo . Create ( value ) ) ;
75
84
encodedBlockInfos [ i ] = encodedBlockInfo ;
85
+
86
+ position += encodedBlockInfo . Info . Length ;
76
87
}
77
88
}
78
89
@@ -203,23 +214,7 @@ private static void ReadBlock( ReadOnlySpan<byte> data, EncodedBlockInfo blockIn
203
214
204
215
#endregion
205
216
206
- private readonly record struct EncodedBlockInfo ( int StartIndex , BlockInfo Info )
207
- {
208
- #region methods
209
-
210
- /// <summary>
211
- /// Reads the encoded block info from the specified <paramref name="reader"/>
212
- /// </summary>
213
- public static EncodedBlockInfo Read ( BinaryReader reader )
214
- {
215
- var info = BlockInfo . Read ( reader ) ;
216
- var startIndex = ( int ) reader . BaseStream . Position ;
217
-
218
- return new EncodedBlockInfo ( startIndex , info ) ;
219
- }
220
-
221
- #endregion
222
- }
217
+ private readonly record struct EncodedBlockInfo ( int StartIndex , BlockInfo Info ) ;
223
218
224
219
internal delegate void BlockAction ( ReadOnlySpan < byte > data , BlockIndex index ) ;
225
220
0 commit comments