@@ -20,20 +20,23 @@ public class GetPixelMemory
2020 public void WhenMemoryIsOwned < TPixel > ( TestImageProvider < TPixel > provider )
2121 where TPixel : struct , IPixel < TPixel >
2222 {
23- using Image < TPixel > image0 = provider . GetImage ( ) ;
24- var targetBuffer = new TPixel [ image0 . Width * image0 . Height ] ;
25-
26- // Act:
27- Memory < TPixel > memory = image0 . GetPixelMemory ( ) ;
28-
29- // Assert:
30- Assert . Equal ( image0 . Width * image0 . Height , memory . Length ) ;
31- memory . Span . CopyTo ( targetBuffer ) ;
32-
33- using Image < TPixel > image1 = provider . GetImage ( ) ;
34-
35- // We are using a copy of the original image for assertion
36- image1 . ComparePixelBufferTo ( targetBuffer ) ;
23+ using ( Image < TPixel > image0 = provider . GetImage ( ) )
24+ {
25+ var targetBuffer = new TPixel [ image0 . Width * image0 . Height ] ;
26+
27+ // Act:
28+ Memory < TPixel > memory = image0 . GetPixelMemory ( ) ;
29+
30+ // Assert:
31+ Assert . Equal ( image0 . Width * image0 . Height , memory . Length ) ;
32+ memory . Span . CopyTo ( targetBuffer ) ;
33+
34+ using ( Image < TPixel > image1 = provider . GetImage ( ) )
35+ {
36+ // We are using a copy of the original image for assertion
37+ image1 . ComparePixelBufferTo ( targetBuffer ) ;
38+ }
39+ }
3740 }
3841
3942 [ Theory ]
@@ -42,23 +45,27 @@ public void WhenMemoryIsOwned<TPixel>(TestImageProvider<TPixel> provider)
4245 public void WhenMemoryIsConsumed < TPixel > ( TestImageProvider < TPixel > provider )
4346 where TPixel : struct , IPixel < TPixel >
4447 {
45- using Image < TPixel > image0 = provider . GetImage ( ) ;
46- var targetBuffer = new TPixel [ image0 . Width * image0 . Height ] ;
47- image0 . GetPixelSpan ( ) . CopyTo ( targetBuffer ) ;
48+ using ( Image < TPixel > image0 = provider . GetImage ( ) )
49+ {
50+ var targetBuffer = new TPixel [ image0 . Width * image0 . Height ] ;
51+ image0 . GetPixelSpan ( ) . CopyTo ( targetBuffer ) ;
4852
49- var managerOfExternalMemory = new TestMemoryManager < TPixel > ( targetBuffer ) ;
53+ var managerOfExternalMemory = new TestMemoryManager < TPixel > ( targetBuffer ) ;
5054
51- Memory < TPixel > externalMemory = managerOfExternalMemory . Memory ;
55+ Memory < TPixel > externalMemory = managerOfExternalMemory . Memory ;
5256
53- using var image1 = Image . WrapMemory ( externalMemory , image0 . Width , image0 . Height ) ;
54- Memory < TPixel > internalMemory = image1 . GetPixelMemory ( ) ;
55- Assert . Equal ( targetBuffer . Length , internalMemory . Length ) ;
56- Assert . True ( Unsafe . AreSame ( ref targetBuffer [ 0 ] , ref internalMemory . Span [ 0 ] ) ) ;
57+ using ( var image1 = Image . WrapMemory ( externalMemory , image0 . Width , image0 . Height ) )
58+ {
59+ Memory < TPixel > internalMemory = image1 . GetPixelMemory ( ) ;
60+ Assert . Equal ( targetBuffer . Length , internalMemory . Length ) ;
61+ Assert . True ( Unsafe . AreSame ( ref targetBuffer [ 0 ] , ref internalMemory . Span [ 0 ] ) ) ;
5762
58- image0 . ComparePixelBufferTo ( internalMemory . Span ) ;
63+ image0 . ComparePixelBufferTo ( internalMemory . Span ) ;
64+ }
5965
60- // Make sure externalMemory works after destruction:
61- image0 . ComparePixelBufferTo ( externalMemory . Span ) ;
66+ // Make sure externalMemory works after destruction:
67+ image0 . ComparePixelBufferTo ( externalMemory . Span ) ;
68+ }
6269 }
6370 }
6471
@@ -68,21 +75,24 @@ public void WhenMemoryIsConsumed<TPixel>(TestImageProvider<TPixel> provider)
6875 public void GetPixelRowMemory < TPixel > ( TestImageProvider < TPixel > provider )
6976 where TPixel : struct , IPixel < TPixel >
7077 {
71- using Image < TPixel > image = provider . GetImage ( ) ;
72- var targetBuffer = new TPixel [ image . Width * image . Height ] ;
73-
74- // Act:
75- for ( int y = 0 ; y < image . Height ; y ++ )
78+ using ( Image < TPixel > image = provider . GetImage ( ) )
7679 {
77- Memory < TPixel > rowMemory = image . GetPixelRowMemory ( y ) ;
78- rowMemory . Span . CopyTo ( targetBuffer . AsSpan ( image . Width * y ) ) ;
79- }
80+ var targetBuffer = new TPixel [ image . Width * image . Height ] ;
8081
81- // Assert:
82- using Image < TPixel > image1 = provider . GetImage ( ) ;
82+ // Act:
83+ for ( int y = 0 ; y < image . Height ; y ++ )
84+ {
85+ Memory < TPixel > rowMemory = image . GetPixelRowMemory ( y ) ;
86+ rowMemory . Span . CopyTo ( targetBuffer . AsSpan ( image . Width * y ) ) ;
87+ }
8388
84- // We are using a copy of the original image for assertion
85- image1 . ComparePixelBufferTo ( targetBuffer ) ;
89+ // Assert:
90+ using ( Image < TPixel > image1 = provider . GetImage ( ) )
91+ {
92+ // We are using a copy of the original image for assertion
93+ image1 . ComparePixelBufferTo ( targetBuffer ) ;
94+ }
95+ }
8696 }
8797
8898 [ Theory ]
@@ -91,21 +101,24 @@ public void GetPixelRowMemory<TPixel>(TestImageProvider<TPixel> provider)
91101 public void GetPixelRowSpan < TPixel > ( TestImageProvider < TPixel > provider )
92102 where TPixel : struct , IPixel < TPixel >
93103 {
94- using Image < TPixel > image = provider . GetImage ( ) ;
95- var targetBuffer = new TPixel [ image . Width * image . Height ] ;
96-
97- // Act:
98- for ( int y = 0 ; y < image . Height ; y ++ )
104+ using ( Image < TPixel > image = provider . GetImage ( ) )
99105 {
100- Span < TPixel > rowMemory = image . GetPixelRowSpan ( y ) ;
101- rowMemory . CopyTo ( targetBuffer . AsSpan ( image . Width * y ) ) ;
102- }
106+ var targetBuffer = new TPixel [ image . Width * image . Height ] ;
103107
104- // Assert:
105- using Image < TPixel > image1 = provider . GetImage ( ) ;
108+ // Act:
109+ for ( int y = 0 ; y < image . Height ; y ++ )
110+ {
111+ Span < TPixel > rowMemory = image . GetPixelRowSpan ( y ) ;
112+ rowMemory . CopyTo ( targetBuffer . AsSpan ( image . Width * y ) ) ;
113+ }
106114
107- // We are using a copy of the original image for assertion
108- image1 . ComparePixelBufferTo ( targetBuffer ) ;
115+ // Assert:
116+ using ( Image < TPixel > image1 = provider . GetImage ( ) )
117+ {
118+ // We are using a copy of the original image for assertion
119+ image1 . ComparePixelBufferTo ( targetBuffer ) ;
120+ }
121+ }
109122 }
110123
111124 #pragma warning disable 0618
@@ -115,19 +128,21 @@ public void GetPixelRowSpan<TPixel>(TestImageProvider<TPixel> provider)
115128 public unsafe void DangerousGetPinnableReference_CopyToBuffer < TPixel > ( TestImageProvider < TPixel > provider )
116129 where TPixel : struct , IPixel < TPixel >
117130 {
118- using Image < TPixel > image = provider . GetImage ( ) ;
119- var targetBuffer = new TPixel [ image . Width * image . Height ] ;
120-
121- ref byte source = ref Unsafe . As < TPixel , byte > ( ref targetBuffer [ 0 ] ) ;
122- ref byte dest = ref Unsafe . As < TPixel , byte > ( ref image . DangerousGetPinnableReferenceToPixelBuffer ( ) ) ;
123- fixed ( byte * targetPtr = & source )
124- fixed ( byte * pixelBasePtr = & dest )
131+ using ( Image < TPixel > image = provider . GetImage ( ) )
125132 {
126- uint dataSizeInBytes = ( uint ) ( image . Width * image . Height * Unsafe . SizeOf < TPixel > ( ) ) ;
127- Unsafe . CopyBlock ( targetPtr , pixelBasePtr , dataSizeInBytes ) ;
133+ var targetBuffer = new TPixel [ image . Width * image . Height ] ;
134+
135+ ref byte source = ref Unsafe . As < TPixel , byte > ( ref targetBuffer [ 0 ] ) ;
136+ ref byte dest = ref Unsafe . As < TPixel , byte > ( ref image . DangerousGetPinnableReferenceToPixelBuffer ( ) ) ;
137+ fixed ( byte * targetPtr = & source )
138+ fixed ( byte * pixelBasePtr = & dest )
139+ {
140+ uint dataSizeInBytes = ( uint ) ( image . Width * image . Height * Unsafe . SizeOf < TPixel > ( ) ) ;
141+ Unsafe . CopyBlock ( targetPtr , pixelBasePtr , dataSizeInBytes ) ;
142+ }
143+
144+ image . ComparePixelBufferTo ( targetBuffer ) ;
128145 }
129-
130- image . ComparePixelBufferTo ( targetBuffer ) ;
131146 }
132147 }
133148}
0 commit comments