Skip to content

Commit 8775e85

Browse files
committed
fixup! feat: Xplat support for image loading and saving from memory
1 parent be1842b commit 8775e85

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

sources/engine/Stride/Graphics/StandardImageHelper.Desktop.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#if STRIDE_PLATFORM_DESKTOP
44
using System;
55
using System.Drawing;
6-
using System.Drawing.Imaging;
76
using System.IO;
87
using System.Runtime.CompilerServices;
98
using System.Runtime.InteropServices;
@@ -18,7 +17,8 @@ namespace Stride.Graphics
1817
partial class StandardImageHelper
1918
{
2019
public static unsafe Image LoadFromMemory(IntPtr pSource, int size, bool makeACopy, GCHandle? handle)
21-
{
20+
{
21+
NativeLibraryHelper.PreloadLibrary("freeimage", typeof(StandardImageHelper));
2222
using var memoryStream = new UnmanagedMemoryStream((byte*)pSource, size, capacity: size, access: FileAccess.Read);
2323
using var bitmap = FreeImageBitmap.FromStream(memoryStream);
2424
var sourceArea = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
@@ -33,7 +33,7 @@ public static unsafe Image LoadFromMemory(IntPtr pSource, int size, bool makeACo
3333
// Directly load image as RGBA instead of BGRA, because OpenGL ES devices don't support it out of the box (extension).
3434
//image.Description.Format = PixelFormat.R8G8B8A8_UNorm;
3535
//CopyMemoryBGRA(image.PixelBuffer[0].DataPointer, bitmapData.Scan0, image.PixelBuffer[0].BufferStride);
36-
Unsafe.CopyBlockUnaligned((void*)image.PixelBuffer[0].DataPointer, (void*)bitmapData.Scan0, (uint)image.PixelBuffer[0].BufferStride);
36+
Unsafe.CopyBlockUnaligned((void*)image.PixelBuffer[0].DataPointer, (void*)bitmapData.Scan0, (uint)bitmap.Width * 4);
3737
}
3838
finally
3939
{
@@ -51,36 +51,37 @@ public static unsafe Image LoadFromMemory(IntPtr pSource, int size, bool makeACo
5151

5252
public static void SaveGifFromMemory(PixelBuffer[] pixelBuffers, int count, ImageDescription description, Stream imageStream)
5353
{
54-
SaveFromMemory(pixelBuffers, count, description, imageStream, FREE_IMAGE_FORMAT.FIF_GIF);
54+
SaveFromMemory(pixelBuffers, description, imageStream, FREE_IMAGE_FORMAT.FIF_GIF);
5555
}
5656

5757
public static void SaveTiffFromMemory(PixelBuffer[] pixelBuffers, int count, ImageDescription description, Stream imageStream)
5858
{
59-
SaveFromMemory(pixelBuffers, count, description, imageStream, FREE_IMAGE_FORMAT.FIF_TIFF);
59+
SaveFromMemory(pixelBuffers, description, imageStream, FREE_IMAGE_FORMAT.FIF_TIFF);
6060
}
6161

6262
public static void SaveBmpFromMemory(PixelBuffer[] pixelBuffers, int count, ImageDescription description, Stream imageStream)
6363
{
64-
SaveFromMemory(pixelBuffers, count, description, imageStream, FREE_IMAGE_FORMAT.FIF_BMP);
64+
SaveFromMemory(pixelBuffers, description, imageStream, FREE_IMAGE_FORMAT.FIF_BMP);
6565
}
6666

6767
public static void SaveJpgFromMemory(PixelBuffer[] pixelBuffers, int count, ImageDescription description, Stream imageStream)
6868
{
69-
SaveFromMemory(pixelBuffers, count, description, imageStream, FREE_IMAGE_FORMAT.FIF_BMP);
69+
SaveFromMemory(pixelBuffers, description, imageStream, FREE_IMAGE_FORMAT.FIF_BMP);
7070
}
7171

7272
public static void SavePngFromMemory(PixelBuffer[] pixelBuffers, int count, ImageDescription description, Stream imageStream)
7373
{
74-
SaveFromMemory(pixelBuffers, count, description, imageStream, FREE_IMAGE_FORMAT.FIF_PNG);
74+
SaveFromMemory(pixelBuffers, description, imageStream, FREE_IMAGE_FORMAT.FIF_PNG);
7575
}
7676

7777
public static void SaveWmpFromMemory(PixelBuffer[] pixelBuffers, int count, ImageDescription description, Stream imageStream)
7878
{
7979
throw new NotImplementedException();
8080
}
8181

82-
private static unsafe void SaveFromMemory(PixelBuffer[] pixelBuffers, int count, ImageDescription description, Stream imageStream, FREE_IMAGE_FORMAT imageFormat)
82+
private static unsafe void SaveFromMemory(PixelBuffer[] pixelBuffers, ImageDescription description, Stream imageStream, FREE_IMAGE_FORMAT imageFormat)
8383
{
84+
NativeLibraryHelper.PreloadLibrary("freeimage", typeof(StandardImageHelper));
8485
using var bitmap = new FreeImageBitmap(description.Width, description.Height);
8586
var sourceArea = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
8687

@@ -93,17 +94,17 @@ private static unsafe void SaveFromMemory(PixelBuffer[] pixelBuffers, int count,
9394
var format = description.Format;
9495
if (format is PixelFormat.R8G8B8A8_UNorm or PixelFormat.R8G8B8A8_UNorm_SRgb)
9596
{
96-
CopyMemoryBGRA(bitmapData.Scan0, pixelBuffers[0].DataPointer, pixelBuffers[0].BufferStride);
97+
CopyMemoryBGRA(bitmapData.Scan0, pixelBuffers[0].DataPointer, bitmap.Width * 4);
9798
}
9899
else if (format is PixelFormat.B8G8R8A8_UNorm or PixelFormat.B8G8R8A8_UNorm_SRgb)
99100
{
100-
Unsafe.CopyBlockUnaligned((void*)bitmapData.Scan0, (void*)pixelBuffers[0].DataPointer, (uint)pixelBuffers[0].BufferStride);
101+
Unsafe.CopyBlockUnaligned((void*)bitmapData.Scan0, (void*)pixelBuffers[0].DataPointer, (uint)bitmap.Width * 4);
101102
}
102103
else if (format is PixelFormat.R8_UNorm or PixelFormat.A8_UNorm)
103104
{
104105
// TODO Ideally we will want to support grayscale images, but the SpriteBatch can only render RGBA for now
105106
// so convert the grayscale image as an RGBA and save it
106-
CopyMemoryRRR1(bitmapData.Scan0, pixelBuffers[0].DataPointer, pixelBuffers[0].BufferStride);
107+
CopyMemoryRRR1(bitmapData.Scan0, pixelBuffers[0].DataPointer, bitmap.Width * 4);
107108
}
108109
else
109110
{

0 commit comments

Comments
 (0)