@@ -28,17 +28,46 @@ public static unsafe Image<ColorRGB> ToImage(Native.sd_image_t sdImage)
2828 return image ;
2929 }
3030
31- public static unsafe void Dispose ( Native . sd_image_t image )
31+ public static unsafe void Dispose ( Native . sd_image_t image ) => Marshal . FreeHGlobal ( ( nint ) image . data ) ;
32+
33+ public static unsafe Native . sd_image_t ToSdImage ( this IImage image , out nint dataPtr )
3234 {
33- Marshal . FreeHGlobal ( ( nint ) image . data ) ;
35+ int sizeInBytes = image . SizeInBytes ;
36+
37+ dataPtr = Marshal . AllocHGlobal ( sizeInBytes ) ;
38+ image . CopyTo ( new Span < byte > ( ( void * ) dataPtr , sizeInBytes ) ) ;
39+
40+ return image . ToSdImage ( ( byte * ) dataPtr ) ;
3441 }
3542
36- public static unsafe Native . sd_image_t ToSdImage ( this IImage < ColorRGB > image , byte * pinnedReference )
43+ public static unsafe Native . sd_image_t ToSdImage ( this IImage image , byte * pinnedReference )
3744 => new ( )
3845 {
3946 width = ( uint ) image . Width ,
4047 height = ( uint ) image . Height ,
4148 channel = ( uint ) image . ColorFormat . BytesPerPixel ,
4249 data = pinnedReference
4350 } ;
51+
52+ public static unsafe Native . sd_image_t * ToSdImagePtr ( this IImage image , out nint dataPtr )
53+ {
54+ int sizeInBytes = image . SizeInBytes ;
55+
56+ dataPtr = Marshal . AllocHGlobal ( sizeInBytes ) ;
57+ image . CopyTo ( new Span < byte > ( ( void * ) dataPtr , sizeInBytes ) ) ;
58+
59+ return image . ToSdImagePtr ( ( byte * ) dataPtr ) ;
60+ }
61+
62+ public static unsafe Native . sd_image_t * ToSdImagePtr ( this IImage image , byte * pinnedReference )
63+ {
64+ Native . sd_image_t * nativeImage = ( Native . sd_image_t * ) Marshal . AllocHGlobal ( sizeof ( Native . sd_image_t ) ) ;
65+
66+ nativeImage ->width = ( uint ) image . Width ;
67+ nativeImage ->height = ( uint ) image . Height ;
68+ nativeImage ->channel = ( uint ) image . ColorFormat . BytesPerPixel ;
69+ nativeImage ->data = pinnedReference ;
70+
71+ return nativeImage ;
72+ }
4473}
0 commit comments