7
7
using System . IO ;
8
8
using System . Runtime . CompilerServices ;
9
9
using System . Runtime . InteropServices ;
10
+ using FreeImageAPI ;
10
11
using Stride . Core ;
11
12
12
13
namespace Stride . Graphics
13
14
{
14
15
/// <summary>
15
16
/// This class is responsible to provide image loader for png, gif, bmp.
16
- /// TODO: Replace using System.Drawing, as it is not available on all platforms (not on Windows 8/WP8).
17
17
/// </summary>
18
18
partial class StandardImageHelper
19
19
{
20
20
public static unsafe Image LoadFromMemory ( IntPtr pSource , int size , bool makeACopy , GCHandle ? handle )
21
21
{
22
22
using var memoryStream = new UnmanagedMemoryStream ( ( byte * ) pSource , size , capacity : size , access : FileAccess . Read ) ;
23
- using var bitmap = ( Bitmap ) System . Drawing . Image . FromStream ( memoryStream ) ;
23
+ using var bitmap = FreeImageBitmap . FromStream ( memoryStream ) ;
24
24
var sourceArea = new Rectangle ( 0 , 0 , bitmap . Width , bitmap . Height ) ;
25
- // Lock System.Drawing.Bitmap
25
+ //Temp copy of FreeImageBitmap
26
26
27
- var bitmapData = bitmap . LockBits ( sourceArea , ImageLockMode . ReadOnly , System . Drawing . Imaging . PixelFormat . Format32bppArgb ) ;
27
+ var bitmapData = bitmap . Copy ( sourceArea ) . ConvertTo32Bits ( ) ;
28
28
var image = Image . New2D ( bitmap . Width , bitmap . Height , 1 , PixelFormat . B8G8R8A8_UNorm , 1 , bitmapData . Stride ) ;
29
- // var dataRect = new DataRectangle(bitmapData.Stride, bitmapData.Scan0);
30
29
31
30
try
32
31
{
@@ -38,7 +37,8 @@ public static unsafe Image LoadFromMemory(IntPtr pSource, int size, bool makeACo
38
37
}
39
38
finally
40
39
{
41
- bitmap . UnlockBits ( bitmapData ) ;
40
+ bitmap . Paste ( bitmapData , new ( sourceArea . X , sourceArea . Y ) , 255 ) ;
41
+ bitmapData . Dispose ( ) ;
42
42
43
43
if ( handle != null )
44
44
handle . Value . Free ( ) ;
@@ -51,55 +51,55 @@ public static unsafe Image LoadFromMemory(IntPtr pSource, int size, bool makeACo
51
51
52
52
public static void SaveGifFromMemory ( PixelBuffer [ ] pixelBuffers , int count , ImageDescription description , Stream imageStream )
53
53
{
54
- SaveFromMemory ( pixelBuffers , count , description , imageStream , ImageFormat . Gif ) ;
54
+ SaveFromMemory ( pixelBuffers , count , description , imageStream , FREE_IMAGE_FORMAT . FIF_GIF ) ;
55
55
}
56
56
57
57
public static void SaveTiffFromMemory ( PixelBuffer [ ] pixelBuffers , int count , ImageDescription description , Stream imageStream )
58
58
{
59
- SaveFromMemory ( pixelBuffers , count , description , imageStream , ImageFormat . Tiff ) ;
59
+ SaveFromMemory ( pixelBuffers , count , description , imageStream , FREE_IMAGE_FORMAT . FIF_TIFF ) ;
60
60
}
61
61
62
62
public static void SaveBmpFromMemory ( PixelBuffer [ ] pixelBuffers , int count , ImageDescription description , Stream imageStream )
63
63
{
64
- SaveFromMemory ( pixelBuffers , count , description , imageStream , ImageFormat . Bmp ) ;
64
+ SaveFromMemory ( pixelBuffers , count , description , imageStream , FREE_IMAGE_FORMAT . FIF_BMP ) ;
65
65
}
66
66
67
67
public static void SaveJpgFromMemory ( PixelBuffer [ ] pixelBuffers , int count , ImageDescription description , Stream imageStream )
68
68
{
69
- SaveFromMemory ( pixelBuffers , count , description , imageStream , ImageFormat . Jpeg ) ;
69
+ SaveFromMemory ( pixelBuffers , count , description , imageStream , FREE_IMAGE_FORMAT . FIF_BMP ) ;
70
70
}
71
71
72
72
public static void SavePngFromMemory ( PixelBuffer [ ] pixelBuffers , int count , ImageDescription description , Stream imageStream )
73
73
{
74
- SaveFromMemory ( pixelBuffers , count , description , imageStream , ImageFormat . Png ) ;
74
+ SaveFromMemory ( pixelBuffers , count , description , imageStream , FREE_IMAGE_FORMAT . FIF_PNG ) ;
75
75
}
76
76
77
77
public static void SaveWmpFromMemory ( PixelBuffer [ ] pixelBuffers , int count , ImageDescription description , Stream imageStream )
78
78
{
79
79
throw new NotImplementedException ( ) ;
80
80
}
81
81
82
- private static unsafe void SaveFromMemory ( PixelBuffer [ ] pixelBuffers , int count , ImageDescription description , Stream imageStream , ImageFormat imageFormat )
82
+ private static unsafe void SaveFromMemory ( PixelBuffer [ ] pixelBuffers , int count , ImageDescription description , Stream imageStream , FREE_IMAGE_FORMAT imageFormat )
83
83
{
84
- using var bitmap = new Bitmap ( description . Width , description . Height ) ;
84
+ using var bitmap = new FreeImageBitmap ( description . Width , description . Height ) ;
85
85
var sourceArea = new Rectangle ( 0 , 0 , bitmap . Width , bitmap . Height ) ;
86
86
87
- // Lock System.Drawing.Bitmap
88
- var bitmapData = bitmap . LockBits ( sourceArea , ImageLockMode . WriteOnly , System . Drawing . Imaging . PixelFormat . Format32bppArgb ) ;
87
+ //Temp copy of FreeImageBitmap
88
+ var bitmapData = bitmap . Copy ( sourceArea ) . ConvertTo32Bits ( ) ;
89
89
90
90
try
91
91
{
92
92
// Copy memory
93
93
var format = description . Format ;
94
- if ( format == PixelFormat . R8G8B8A8_UNorm || format == PixelFormat . R8G8B8A8_UNorm_SRgb )
94
+ if ( format is PixelFormat . R8G8B8A8_UNorm or PixelFormat . R8G8B8A8_UNorm_SRgb )
95
95
{
96
96
CopyMemoryBGRA ( bitmapData . Scan0 , pixelBuffers [ 0 ] . DataPointer , pixelBuffers [ 0 ] . BufferStride ) ;
97
97
}
98
- else if ( format == PixelFormat . B8G8R8A8_UNorm || format == PixelFormat . B8G8R8A8_UNorm_SRgb )
98
+ else if ( format is PixelFormat . B8G8R8A8_UNorm or PixelFormat . B8G8R8A8_UNorm_SRgb )
99
99
{
100
100
Unsafe . CopyBlockUnaligned ( ( void * ) bitmapData . Scan0 , ( void * ) pixelBuffers [ 0 ] . DataPointer , ( uint ) pixelBuffers [ 0 ] . BufferStride ) ;
101
101
}
102
- else if ( format == PixelFormat . R8_UNorm || format == PixelFormat . A8_UNorm )
102
+ else if ( format is PixelFormat . R8_UNorm or PixelFormat . A8_UNorm )
103
103
{
104
104
// TODO Ideally we will want to support grayscale images, but the SpriteBatch can only render RGBA for now
105
105
// so convert the grayscale image as an RGBA and save it
@@ -114,7 +114,8 @@ private static unsafe void SaveFromMemory(PixelBuffer[] pixelBuffers, int count,
114
114
}
115
115
finally
116
116
{
117
- bitmap . UnlockBits ( bitmapData ) ;
117
+ bitmap . Paste ( bitmapData , new ( sourceArea . X , sourceArea . Y ) , 255 ) ;
118
+ bitmapData . Dispose ( ) ;
118
119
}
119
120
120
121
// Save
0 commit comments