feat: Adjust freeimage to all platforms (#2303)

* feat: Adjust freeimage to all platforms

* Adjust FreeImage methods
This commit is contained in:
Jakub Ławreszuk 2024-06-18 05:35:27 +00:00 коммит произвёл GitHub
Родитель 5558558d65
Коммит 15d7dc32f1
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
9 изменённых файлов: 72 добавлений и 94 удалений

Просмотреть файл

Просмотреть файл

Просмотреть файл

@ -12,13 +12,12 @@ namespace Stride.Core
public static class NativeLibraryHelper
{
private const string UNIX_LIB_PREFIX = "lib";
private static readonly Dictionary<string, IntPtr> LoadedLibraries = new Dictionary<string, IntPtr>(StringComparer.OrdinalIgnoreCase);
private static readonly Dictionary<string, string> NativeDependencies = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
private static readonly Dictionary<string, IntPtr> LoadedLibraries = new(StringComparer.OrdinalIgnoreCase);
private static readonly Dictionary<string, string> NativeDependencies = new(StringComparer.OrdinalIgnoreCase);
/// <summary>
/// Try to preload the library.
/// This is useful when we want to have AnyCPU .NET and CPU-specific native code.
/// Only available on Windows for now.
/// </summary>
/// <param name="libraryName">Name of the library, without the extension.</param>
/// <param name="owner">Type whose assembly location is related to the native library (we can't use GetCallingAssembly as it might be wrong due to optimizations).</param>
@ -52,54 +51,22 @@ namespace Stride.Core
}
}
string cpu;
string platform;
string extension;
switch (RuntimeInformation.ProcessArchitecture)
var cpu = RuntimeInformation.ProcessArchitecture switch
{
case Architecture.X86:
cpu = "x86";
break;
case Architecture.X64:
cpu = "x64";
break;
case Architecture.Arm:
cpu = "ARM";
break;
default:
throw new PlatformNotSupportedException();
}
Architecture.X86 => "x86",
Architecture.X64 => "x64",
Architecture.Arm => "ARM",
_ => throw new PlatformNotSupportedException(),
};
switch (Platform.Type)
string platform, extension;
(platform, extension) = Platform.Type switch
{
case PlatformType.Windows:
platform = "win";
break;
case PlatformType.Linux:
platform = "linux";
break;
case PlatformType.macOS:
platform = "osx";
break;
default:
throw new PlatformNotSupportedException();
}
switch (Platform.Type)
{
case PlatformType.Windows:
extension = ".dll";
break;
case PlatformType.Linux:
extension = ".so";
break;
case PlatformType.macOS:
extension = ".dylib";
break;
default:
throw new PlatformNotSupportedException();
}
PlatformType.Windows => ("win",".dll"),
PlatformType.Linux => ("linux",".so"),
PlatformType.macOS => ("osx",".dylib"),
_ => throw new PlatformNotSupportedException(),
};
var libraryNameWithExtension = libraryName + extension;
@ -127,7 +94,7 @@ namespace Stride.Core
{
Path.Combine(Path.GetDirectoryName(owner.GetTypeInfo().Assembly.Location) ?? string.Empty, platformNativeLibsFolder),
Path.Combine(Environment.CurrentDirectory ?? string.Empty, platformNativeLibsFolder),
Path.Combine(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) ?? string.Empty, platformNativeLibsFolder),
Path.Combine(Path.GetDirectoryName(Environment.ProcessPath) ?? string.Empty, platformNativeLibsFolder),
// Also try without platform for Windows-only packages (backward compat for editor packages)
Path.Combine(Path.GetDirectoryName(owner.GetTypeInfo().Assembly.Location) ?? string.Empty, cpu),
Path.Combine(Environment.CurrentDirectory ?? string.Empty, cpu),
@ -167,8 +134,7 @@ namespace Stride.Core
#if STRIDE_PLATFORM_DESKTOP
lock (LoadedLibraries)
{
IntPtr libHandle;
if (LoadedLibraries.TryGetValue(libraryName, out libHandle))
if (LoadedLibraries.TryGetValue(libraryName, out var libHandle))
{
NativeLibrary.Free(libHandle);
LoadedLibraries.Remove(libraryName);

Просмотреть файл

@ -47,7 +47,7 @@ namespace FreeImageAPI
/// <summary>
/// Filename of the FreeImage library.
/// </summary>
private const string FreeImageLibrary = "FreeImage";
private const string FreeImageLibrary = "freeimage";
/// <summary>
/// Number of bytes to shift left within a 4 byte block.
@ -490,7 +490,7 @@ namespace FreeImageAPI
/// <summary>
/// Registers a new plugin to be used in FreeImage. The plugin is residing in a DLL.
/// The Init function must be called “Init” and must use the stdcall calling convention.
/// The Init function must be called "Init" and must use the stdcall calling convention.
/// </summary>
/// <param name="path">Complete path to the dll file hosting the plugin.</param>
/// <param name="format">A string describing the format of the plugin.</param>
@ -1835,7 +1835,7 @@ namespace FreeImageAPI
/// <summary>
/// This function rotates a 1-, 8-bit greyscale or a 24-, 32-bit color image by means of 3 shears.
/// 1-bit images rotation is limited to integer multiple of 90°.
/// 1-bit images rotation is limited to integer multiple of 90°.
/// <c>null</c> is returned for other values.
/// </summary>
/// <param name="dib">Handle to a FreeImage bitmap.</param>

Просмотреть файл

@ -4198,7 +4198,7 @@ namespace FreeImageAPI
/// <summary>
/// This function rotates a 1-, 8-bit greyscale or a 24-, 32-bit color image by means of 3 shears.
/// 1-bit images rotation is limited to integer multiple of 90<EFBFBD>.
/// 1-bit images rotation is limited to integer multiple of 90°.
/// <c>null</c> is returned for other values.
/// </summary>
/// <param name="dib">Handle to a FreeImage bitmap.</param>
@ -4211,7 +4211,7 @@ namespace FreeImageAPI
/// <summary>
/// This function rotates a 1-, 8-bit greyscale or a 24-, 32-bit color image by means of 3 shears.
/// 1-bit images rotation is limited to integer multiple of 90<EFBFBD>.
/// 1-bit images rotation is limited to integer multiple of 90°.
/// <c>null</c> is returned for other values.
/// </summary>
/// <typeparam name="T">The type of the color to use as background.</typeparam>

Просмотреть файл

@ -44,17 +44,10 @@ namespace Stride.TextureConverter.TexLibraries
{
private static Logger Log = GlobalLogger.GetLogger("DxtTexLib");
private static HashSet<string> SupportedExtensions = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase)
private static HashSet<string> SupportedExtensions = new(StringComparer.InvariantCultureIgnoreCase)
{
".dds",
".bmp",
".tga",
".jpg",
".jpeg",
".jpe",
".png",
".tiff",
".tif",
};
/// <summary>
@ -229,11 +222,7 @@ namespace Stride.TextureConverter.TexLibraries
{
hr = Utilities.LoadTGAFile(loader.FilePath, out libraryData.Metadata, libraryData.Image);
}
else
{
hr = Utilities.LoadWICFile(loader.FilePath, WIC_FLAGS.WIC_FLAGS_NONE, out libraryData.Metadata, libraryData.Image);
}
if (hr != HRESULT.S_OK)
{
Log.Error("Loading dds file " + loader.FilePath + " failed: " + hr);

Просмотреть файл

@ -47,7 +47,7 @@ namespace FreeImageAPI
/// <summary>
/// Filename of the FreeImage library.
/// </summary>
private const string FreeImageLibrary = "FreeImage";
private const string FreeImageLibrary = "freeimage";
/// <summary>
/// Number of bytes to shift left within a 4 byte block.
@ -192,14 +192,14 @@ namespace FreeImageAPI
/// </summary>
[DllImport(FreeImageLibrary, EntryPoint = "FreeImage_DeInitialise")]
private static extern void DeInitialise();
/// <summary>
/// Returns a string containing the current version of the library.
/// </summary>
/// <returns>The current version of the library.</returns>
public static unsafe string GetVersion() { return PtrToStr(GetVersion_()); }
[DllImport(FreeImageLibrary, CharSet = CharSet.Ansi, EntryPoint = "FreeImage_GetVersion")]
private static unsafe extern byte* GetVersion_();
private static extern unsafe byte* GetVersion_();
/// <summary>
/// Returns a string containing a standard copyright message.
@ -302,8 +302,8 @@ namespace FreeImageAPI
/// <param name="filename">Name of the file to decode.</param>
/// <param name="flags">Flags to enable or disable plugin-features.</param>
/// <returns>Handle to a FreeImage bitmap.</returns>
[DllImport(FreeImageLibrary, CharSet = CharSet.Unicode, EntryPoint = "FreeImage_LoadU")]
public static extern FIBITMAP Load(FREE_IMAGE_FORMAT fif, string filename, FREE_IMAGE_LOAD_FLAGS flags);
[DllImport(FreeImageLibrary, CharSet = CharSet.Auto, EntryPoint = "FreeImage_Load")]
private static extern FIBITMAP LoadNU(FREE_IMAGE_FORMAT fif, string filename, FREE_IMAGE_LOAD_FLAGS flags);
/// <summary>
/// Decodes a bitmap, allocates memory for it and returns it as a FIBITMAP.
@ -316,6 +316,11 @@ namespace FreeImageAPI
[DllImport(FreeImageLibrary, CharSet = CharSet.Unicode, EntryPoint = "FreeImage_LoadU")]
private static extern FIBITMAP LoadU(FREE_IMAGE_FORMAT fif, string filename, FREE_IMAGE_LOAD_FLAGS flags);
public static FIBITMAP Load(FREE_IMAGE_FORMAT fif, string filename, FREE_IMAGE_LOAD_FLAGS flags)
{
return OperatingSystem.IsWindows() ? LoadU(fif, filename, flags) : LoadNU(fif, filename, flags);
}
/// <summary>
/// Loads a bitmap from an arbitrary source.
/// </summary>
@ -335,8 +340,8 @@ namespace FreeImageAPI
/// <param name="filename">Name of the file to save to.</param>
/// <param name="flags">Flags to enable or disable plugin-features.</param>
/// <returns>Returns true on success, false on failure.</returns>
[DllImport(FreeImageLibrary, CharSet = CharSet.Unicode, EntryPoint = "FreeImage_SaveU")]
public static extern bool Save(FREE_IMAGE_FORMAT fif, FIBITMAP dib, string filename, FREE_IMAGE_SAVE_FLAGS flags);
[DllImport(FreeImageLibrary, CharSet = CharSet.Auto, EntryPoint = "FreeImage_Save")]
private static extern bool SaveNU(FREE_IMAGE_FORMAT fif, FIBITMAP dib, string filename, FREE_IMAGE_SAVE_FLAGS flags);
/// <summary>
/// Saves a previosly loaded FIBITMAP to a file.
@ -349,6 +354,11 @@ namespace FreeImageAPI
/// <returns>Returns true on success, false on failure.</returns>
[DllImport(FreeImageLibrary, CharSet = CharSet.Unicode, EntryPoint = "FreeImage_SaveU")]
private static extern bool SaveU(FREE_IMAGE_FORMAT fif, FIBITMAP dib, string filename, FREE_IMAGE_SAVE_FLAGS flags);
public static bool Save(FREE_IMAGE_FORMAT fif, FIBITMAP dib, string filename, FREE_IMAGE_SAVE_FLAGS flags)
{
return OperatingSystem.IsWindows() ? SaveU(fif, dib, filename, flags) : SaveNU(fif, dib, filename, flags);
}
/// <summary>
/// Saves a bitmap to an arbitrary source.
@ -596,8 +606,8 @@ namespace FreeImageAPI
/// </summary>
/// <param name="filename">The filename or -extension.</param>
/// <returns>The <see cref="FREE_IMAGE_FORMAT"/> of the plugin.</returns>
[DllImport(FreeImageLibrary, CharSet = CharSet.Unicode, EntryPoint = "FreeImage_GetFIFFromFilenameU")]
public static extern FREE_IMAGE_FORMAT GetFIFFromFilename(string filename);
[DllImport(FreeImageLibrary, CharSet = CharSet.Auto, EntryPoint = "FreeImage_GetFIFFromFilename")]
private static extern FREE_IMAGE_FORMAT GetFIFFromFilenameNU(string filename);
/// <summary>
/// This function takes a filename or a file-extension and returns the plugin that can
@ -609,6 +619,11 @@ namespace FreeImageAPI
[DllImport(FreeImageLibrary, CharSet = CharSet.Unicode, EntryPoint = "FreeImage_GetFIFFromFilenameU")]
private static extern FREE_IMAGE_FORMAT GetFIFFromFilenameU(string filename);
public static FREE_IMAGE_FORMAT GetFIFFromFilename(string filename)
{
return OperatingSystem.IsWindows() ? GetFIFFromFilenameU(filename) : GetFIFFromFilenameNU(filename);
}
/// <summary>
/// Checks if a plugin can load bitmaps.
/// </summary>
@ -785,8 +800,8 @@ namespace FreeImageAPI
/// <param name="filename">Name of the file to analyze.</param>
/// <param name="size">Reserved parameter - use 0.</param>
/// <returns>Type of the bitmap.</returns>
[DllImport(FreeImageLibrary, CharSet = CharSet.Unicode, EntryPoint = "FreeImage_GetFileTypeU")]
public static extern FREE_IMAGE_FORMAT GetFileType(string filename, int size);
[DllImport(FreeImageLibrary, CharSet = CharSet.Auto, EntryPoint = "FreeImage_GetFileType")]
private static extern FREE_IMAGE_FORMAT GetFileTypeNU(string filename, int size);
/// <summary>
@ -799,6 +814,11 @@ namespace FreeImageAPI
[DllImport(FreeImageLibrary, CharSet = CharSet.Unicode, EntryPoint = "FreeImage_GetFileTypeU")]
private static extern FREE_IMAGE_FORMAT GetFileTypeU(string filename, int size);
public static FREE_IMAGE_FORMAT GetFileType(string filename, int size)
{
return OperatingSystem.IsWindows() ? GetFIFFromFilenameU(filename) : GetFIFFromFilenameNU(filename);
}
/// <summary>
/// Uses the <see cref="FreeImageIO"/> structure as described in the topic bitmap management functions
/// to identify a bitmap type.
@ -1835,7 +1855,7 @@ namespace FreeImageAPI
/// <summary>
/// This function rotates a 1-, 8-bit greyscale or a 24-, 32-bit color image by means of 3 shears.
/// 1-bit images rotation is limited to integer multiple of 90°.
/// 1-bit images rotation is limited to integer multiple of 90°.
/// <c>null</c> is returned for other values.
/// </summary>
/// <param name="dib">Handle to a FreeImage bitmap.</param>

Просмотреть файл

@ -1063,15 +1063,16 @@ namespace FreeImageAPI
return FREE_IMAGE_FORMAT.FIF_UNKNOWN;
var extention = filename.Substring(filename.LastIndexOf('.'));
switch (extention)
{
case ".tga":
return FREE_IMAGE_FORMAT.FIF_TARGA;
default:
// Note: other format met so far seems to be properly handled by "GetFileType".
// -> no need to add the extension/format association here.
return FREE_IMAGE_FORMAT.FIF_UNKNOWN;
}
return extention switch
{
".tga" => FREE_IMAGE_FORMAT.FIF_TARGA,
".png" => FREE_IMAGE_FORMAT.FIF_PNG,
".bmp" => FREE_IMAGE_FORMAT.FIF_BMP,
".tiff" or ".tif" => FREE_IMAGE_FORMAT.FIF_TIFF,
".jpg" or ".jpeg" => FREE_IMAGE_FORMAT.FIF_JPEG,
_ => FREE_IMAGE_FORMAT.FIF_UNKNOWN,// Note: other format met so far seems to be properly handled by "GetFileType".
// -> no need to add the extension/format association here.
};
}
/// <summary>
@ -4378,7 +4379,7 @@ namespace FreeImageAPI
/// <summary>
/// This function rotates a 1-, 8-bit greyscale or a 24-, 32-bit color image by means of 3 shears.
/// 1-bit images rotation is limited to integer multiple of 90<EFBFBD>.
/// 1-bit images rotation is limited to integer multiple of 90°.
/// <c>null</c> is returned for other values.
/// </summary>
/// <param name="dib">Handle to a FreeImage bitmap.</param>
@ -4391,7 +4392,7 @@ namespace FreeImageAPI
/// <summary>
/// This function rotates a 1-, 8-bit greyscale or a 24-, 32-bit color image by means of 3 shears.
/// 1-bit images rotation is limited to integer multiple of 90<EFBFBD>.
/// 1-bit images rotation is limited to integer multiple of 90°.
/// <c>null</c> is returned for other values.
/// </summary>
/// <typeparam name="T">The type of the color to use as background.</typeparam>

Просмотреть файл

@ -40,8 +40,10 @@ namespace Stride.TextureConverter
NativeLibraryHelper.PreloadLibrary("DxtWrapper", type);
NativeLibraryHelper.PreloadLibrary("PVRTexLib", type);
NativeLibraryHelper.PreloadLibrary("PvrttWrapper", type);
NativeLibraryHelper.PreloadLibrary("FreeImage", type);
NativeLibraryHelper.PreloadLibrary("FreeImageNET", type);
NativeLibraryHelper.PreloadLibrary("freeimage", type);
//TODO: needs to explain why FreeImageNET is loaded as a dll instead of directly referencing the C# project (this does not affect the compilation process on Linux).
if (OperatingSystem.IsWindows())
NativeLibraryHelper.PreloadLibrary("FreeImageNET", type);
}
/// <summary>