Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions build/Stride.sln
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Stride.Importer.3D", "..\so
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Stride.BepuPhysics.Tests", "..\sources\engine\Stride.BepuPhysics\Stride.BepuPhysics.Tests\Stride.BepuPhysics.Tests.csproj", "{7B70C783-4085-4702-B3C6-6570FD85CB8F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Stride.FreeImage", "..\sources\tools\Stride.FreeImage\Stride.FreeImage.csproj", "{75CAB5A3-3494-49E6-AB91-91C329160A17}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -1523,6 +1525,18 @@ Global
{7B70C783-4085-4702-B3C6-6570FD85CB8F}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{7B70C783-4085-4702-B3C6-6570FD85CB8F}.Release|Win32.ActiveCfg = Release|Any CPU
{7B70C783-4085-4702-B3C6-6570FD85CB8F}.Release|Win32.Build.0 = Release|Any CPU
{75CAB5A3-3494-49E6-AB91-91C329160A17}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{75CAB5A3-3494-49E6-AB91-91C329160A17}.Debug|Any CPU.Build.0 = Debug|Any CPU
{75CAB5A3-3494-49E6-AB91-91C329160A17}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{75CAB5A3-3494-49E6-AB91-91C329160A17}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{75CAB5A3-3494-49E6-AB91-91C329160A17}.Debug|Win32.ActiveCfg = Debug|Any CPU
{75CAB5A3-3494-49E6-AB91-91C329160A17}.Debug|Win32.Build.0 = Debug|Any CPU
{75CAB5A3-3494-49E6-AB91-91C329160A17}.Release|Any CPU.ActiveCfg = Release|Any CPU
{75CAB5A3-3494-49E6-AB91-91C329160A17}.Release|Any CPU.Build.0 = Release|Any CPU
{75CAB5A3-3494-49E6-AB91-91C329160A17}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{75CAB5A3-3494-49E6-AB91-91C329160A17}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{75CAB5A3-3494-49E6-AB91-91C329160A17}.Release|Win32.ActiveCfg = Release|Any CPU
{75CAB5A3-3494-49E6-AB91-91C329160A17}.Release|Win32.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -1651,6 +1665,7 @@ Global
{7715D094-DF59-4D91-BC9A-9A5118039ECB} = {DE048114-9AE4-467E-A879-188DC0D88A59}
{66EFFDE4-24F0-4E57-9618-0F5577E20A1E} = {6F473FA6-4F8B-4FBA-AE33-EE5AF997D50C}
{7B70C783-4085-4702-B3C6-6570FD85CB8F} = {DE048114-9AE4-467E-A879-188DC0D88A59}
{75CAB5A3-3494-49E6-AB91-91C329160A17} = {1AE1AC60-5D2F-4CA7-AE20-888F44551185}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {FF877973-604D-4EA7-B5F5-A129961F9EF2}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,42 @@
#if STRIDE_PLATFORM_DESKTOP
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using FreeImageAPI;
using Stride.Core;

namespace Stride.Graphics
{
/// <summary>
/// This class is responsible to provide image loader for png, gif, bmp.
/// TODO: Replace using System.Drawing, as it is not available on all platforms (not on Windows 8/WP8).
/// </summary>
partial class StandardImageHelper
{
public static unsafe Image LoadFromMemory(IntPtr pSource, int size, bool makeACopy, GCHandle? handle)
{
{
NativeLibraryHelper.PreloadLibrary("freeimage", typeof(StandardImageHelper));
using var memoryStream = new UnmanagedMemoryStream((byte*)pSource, size, capacity: size, access: FileAccess.Read);
using var bitmap = (Bitmap)System.Drawing.Image.FromStream(memoryStream);
using var bitmap = FreeImageBitmap.FromStream(memoryStream);
var sourceArea = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
// Lock System.Drawing.Bitmap
//Temp copy of FreeImageBitmap

var bitmapData = bitmap.LockBits(sourceArea, ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
var bitmapData = bitmap.Copy(sourceArea).ConvertTo32Bits();
var image = Image.New2D(bitmap.Width, bitmap.Height, 1, PixelFormat.B8G8R8A8_UNorm, 1, bitmapData.Stride);
// var dataRect = new DataRectangle(bitmapData.Stride, bitmapData.Scan0);

try
{
// TODO: Test if still necessary
// Directly load image as RGBA instead of BGRA, because OpenGL ES devices don't support it out of the box (extension).
//image.Description.Format = PixelFormat.R8G8B8A8_UNorm;
//CopyMemoryBGRA(image.PixelBuffer[0].DataPointer, bitmapData.Scan0, image.PixelBuffer[0].BufferStride);
Unsafe.CopyBlockUnaligned((void*)image.PixelBuffer[0].DataPointer, (void*)bitmapData.Scan0, (uint)image.PixelBuffer[0].BufferStride);
Unsafe.CopyBlockUnaligned((void*)image.PixelBuffer[0].DataPointer, (void*)bitmapData.Scan0, (uint)bitmap.Width * 4);
}
finally
{
bitmap.UnlockBits(bitmapData);
bitmap.Paste(bitmapData, new (sourceArea.X, sourceArea.Y), 255);
bitmapData.Dispose();

if (handle != null)
handle.Value.Free();
Expand All @@ -51,59 +51,60 @@ public static unsafe Image LoadFromMemory(IntPtr pSource, int size, bool makeACo

public static void SaveGifFromMemory(PixelBuffer[] pixelBuffers, int count, ImageDescription description, Stream imageStream)
{
SaveFromMemory(pixelBuffers, count, description, imageStream, ImageFormat.Gif);
SaveFromMemory(pixelBuffers, description, imageStream, FREE_IMAGE_FORMAT.FIF_GIF);
}

public static void SaveTiffFromMemory(PixelBuffer[] pixelBuffers, int count, ImageDescription description, Stream imageStream)
{
SaveFromMemory(pixelBuffers, count, description, imageStream, ImageFormat.Tiff);
SaveFromMemory(pixelBuffers, description, imageStream, FREE_IMAGE_FORMAT.FIF_TIFF);
}

public static void SaveBmpFromMemory(PixelBuffer[] pixelBuffers, int count, ImageDescription description, Stream imageStream)
{
SaveFromMemory(pixelBuffers, count, description, imageStream, ImageFormat.Bmp);
SaveFromMemory(pixelBuffers, description, imageStream, FREE_IMAGE_FORMAT.FIF_BMP);
}

public static void SaveJpgFromMemory(PixelBuffer[] pixelBuffers, int count, ImageDescription description, Stream imageStream)
{
SaveFromMemory(pixelBuffers, count, description, imageStream, ImageFormat.Jpeg);
SaveFromMemory(pixelBuffers, description, imageStream, FREE_IMAGE_FORMAT.FIF_BMP);
}

public static void SavePngFromMemory(PixelBuffer[] pixelBuffers, int count, ImageDescription description, Stream imageStream)
{
SaveFromMemory(pixelBuffers, count, description, imageStream, ImageFormat.Png);
SaveFromMemory(pixelBuffers, description, imageStream, FREE_IMAGE_FORMAT.FIF_PNG);
}

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

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

// Lock System.Drawing.Bitmap
var bitmapData = bitmap.LockBits(sourceArea, ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
//Temp copy of FreeImageBitmap
var bitmapData = bitmap.Copy(sourceArea).ConvertTo32Bits();

try
{
// Copy memory
var format = description.Format;
if (format == PixelFormat.R8G8B8A8_UNorm || format == PixelFormat.R8G8B8A8_UNorm_SRgb)
if (format is PixelFormat.R8G8B8A8_UNorm or PixelFormat.R8G8B8A8_UNorm_SRgb)
{
CopyMemoryBGRA(bitmapData.Scan0, pixelBuffers[0].DataPointer, pixelBuffers[0].BufferStride);
CopyMemoryBGRA(bitmapData.Scan0, pixelBuffers[0].DataPointer, bitmap.Width * 4);
}
else if (format == PixelFormat.B8G8R8A8_UNorm || format == PixelFormat.B8G8R8A8_UNorm_SRgb)
else if (format is PixelFormat.B8G8R8A8_UNorm or PixelFormat.B8G8R8A8_UNorm_SRgb)
{
Unsafe.CopyBlockUnaligned((void*)bitmapData.Scan0, (void*)pixelBuffers[0].DataPointer, (uint)pixelBuffers[0].BufferStride);
Unsafe.CopyBlockUnaligned((void*)bitmapData.Scan0, (void*)pixelBuffers[0].DataPointer, (uint)bitmap.Width * 4);
}
else if (format == PixelFormat.R8_UNorm || format == PixelFormat.A8_UNorm)
else if (format is PixelFormat.R8_UNorm or PixelFormat.A8_UNorm)
{
// TODO Ideally we will want to support grayscale images, but the SpriteBatch can only render RGBA for now
// so convert the grayscale image as an RGBA and save it
CopyMemoryRRR1(bitmapData.Scan0, pixelBuffers[0].DataPointer, pixelBuffers[0].BufferStride);
CopyMemoryRRR1(bitmapData.Scan0, pixelBuffers[0].DataPointer, bitmap.Width * 4);
}
else
{
Expand All @@ -114,7 +115,8 @@ private static unsafe void SaveFromMemory(PixelBuffer[] pixelBuffers, int count,
}
finally
{
bitmap.UnlockBits(bitmapData);
bitmap.Paste(bitmapData, new (sourceArea.X, sourceArea.Y), 255);
bitmapData.Dispose();
}

// Save
Expand Down
1 change: 1 addition & 0 deletions sources/engine/Stride/Stride.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<!-- SharpDX is needed for WIC -->
<PackageReference Include="SharpDX.Direct2D1" Condition="'$(TargetFramework)' == '$(StrideFrameworkUWP)'" />
<PackageReference Include="System.Drawing.Common" Condition="'$(TargetFramework)' == '$(StrideFramework)'" />
<ProjectReference Include="..\..\tools\Stride.FreeImage\Stride.FreeImage.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading