From 9b21bb837bf0464e2ea65961027787fcdc5e54f2 Mon Sep 17 00:00:00 2001 From: "H. Utku Maden" Date: Wed, 19 Jun 2024 11:31:51 +0300 Subject: [PATCH] Move native calls to Stb.Native --- StbImage.cs | 1 + Stbi.Manual.cs | 25 +++++++++++++++++++------ Stbi.cs | 2 +- StbiImageFormat.cs | 5 +++++ StbiStreamWrapper.cs | 27 ++++++++++++++++++++++++++- StbiWrite.cs | 14 ++++++++++---- 6 files changed, 62 insertions(+), 12 deletions(-) diff --git a/StbImage.cs b/StbImage.cs index ff9f496..c3256fc 100644 --- a/StbImage.cs +++ b/StbImage.cs @@ -1,3 +1,4 @@ +using ReFuel.Stb.Native; using System; using System.Diagnostics.CodeAnalysis; using System.IO; diff --git a/Stbi.Manual.cs b/Stbi.Manual.cs index 2a14ad9..beb2146 100644 --- a/Stbi.Manual.cs +++ b/Stbi.Manual.cs @@ -1,14 +1,16 @@ using System; -using System.IO; -using System.Diagnostics; using System.Runtime.InteropServices; using System.Reflection; -namespace ReFuel.Stb +namespace ReFuel.Stb.Native { + /// + /// Direct access to the native STBI function calls. + /// public unsafe static partial class Stbi { private delegate void FailedAssertProc(byte *expression, byte *file, int line, byte *function); + private static IntPtr stbiHandle; private static readonly string[] LibraryNames = new string[] { @@ -29,22 +31,33 @@ namespace ReFuel.Stb static Stbi() { NativeLibrary.SetDllImportResolver(Assembly.GetExecutingAssembly(), Resolver); + + // Dummy call to fail_reason so we have a handle to STBI. + failure_reason(); + + // Load global address pointers. + + _tga_with_rle_ptr = (int*)NativeLibrary.GetExport(stbiHandle, "stbi_write_tga_with_rle"); + _png_compression_level_ptr = (int*)NativeLibrary.GetExport(stbiHandle, "stbi_write_png_compression_level"); + _force_png_filter_ptr = (int*)NativeLibrary.GetExport(stbiHandle, "stbi_write_force_png_filter"); } private static IntPtr Resolver(string libraryName, Assembly assembly, DllImportSearchPath? searchPath) { if (libraryName != "stbi") return IntPtr.Zero; + else if (stbiHandle != IntPtr.Zero) + return stbiHandle; foreach (string name in LibraryNames) { - if (NativeLibrary.TryLoad(name, assembly, searchPath, out IntPtr handle)) + if (NativeLibrary.TryLoad(name, assembly, searchPath, out stbiHandle)) { - return handle; + return stbiHandle; } } - return NativeLibrary.Load(libraryName); + return stbiHandle = NativeLibrary.Load(libraryName); } } } \ No newline at end of file diff --git a/Stbi.cs b/Stbi.cs index 66481ad..7ab5b95 100644 --- a/Stbi.cs +++ b/Stbi.cs @@ -1,7 +1,7 @@ using System; using System.Runtime.InteropServices; -namespace ReFuel.Stb +namespace ReFuel.Stb.Native { public enum StbiEnum : uint { diff --git a/StbiImageFormat.cs b/StbiImageFormat.cs index adbad9a..8c56759 100644 --- a/StbiImageFormat.cs +++ b/StbiImageFormat.cs @@ -1,5 +1,10 @@ +using ReFuel.Stb.Native; + namespace ReFuel.Stb { + /// + /// Enumeration of supported STBI image formats. + /// public enum StbiImageFormat { Default = (int)StbiEnum.STBI_default, diff --git a/StbiStreamWrapper.cs b/StbiStreamWrapper.cs index ecec932..c1900ca 100644 --- a/StbiStreamWrapper.cs +++ b/StbiStreamWrapper.cs @@ -1,13 +1,34 @@ +using ReFuel.Stb.Native; using System; using System.IO; using System.Runtime.InteropServices; namespace ReFuel.Stb { + /// + /// Pointer to STBI stream read function. + /// + /// User provided userdata pointer. + /// C array to read into. + /// Size of the C array in bytes. + /// The number of bytes read from the stream. public unsafe delegate int StbiReadProc(void *userdata, byte* buffer, int count); + /// + /// Pointer to STBI stream skip function. + /// + /// User provided userdata pointer. + /// Number of bytes to skip. public unsafe delegate void StbiSkipProc(void *userdata, int count); + /// + /// Pointer to STBI stream end of file function. + /// + /// User provided userdata pointer. + /// Non-zero value if the end of the stream has been reached. public unsafe delegate int StbiEofProc(void *userdata); + /// + /// An easy to use stream wrapper for use with STBI image load functions. + /// public unsafe class StbiStreamWrapper : IDisposable { private readonly stbi_io_callbacks _callbacks; @@ -74,7 +95,11 @@ namespace ReFuel.Stb } } - internal struct StbiWriteStreamWrapper + /// + /// An easy to use stream wrapper for STBI image write functions. + /// + /// Keep struct alive for the duration of the write operation. + public struct StbiWriteStreamWrapper { private readonly Stream _stream; private readonly StbiWriteProc _cb; diff --git a/StbiWrite.cs b/StbiWrite.cs index 074d20b..94a3420 100644 --- a/StbiWrite.cs +++ b/StbiWrite.cs @@ -1,15 +1,21 @@ using System; using System.Runtime.InteropServices; -namespace ReFuel.Stb +namespace ReFuel.Stb.Native { + /// + /// Procedure to STBI image write function. + /// + /// User provided context pointer. + /// C Array of data to write. + /// Size of the C array in bytes. public unsafe delegate void StbiWriteProc(void* context, void* data, int size); public unsafe partial class Stbi { private static readonly int* _tga_with_rle_ptr; private static readonly int* _png_compression_level_ptr; - private static readonly int* _forced_png_filter_ptr; + private static readonly int* _force_png_filter_ptr; public static int write_tga_with_rle { @@ -25,8 +31,8 @@ namespace ReFuel.Stb public static int write_force_png_filter { - get => *_forced_png_filter_ptr; - set => *_forced_png_filter_ptr = value; + get => *_force_png_filter_ptr; + set => *_force_png_filter_ptr = value; } [DllImport("stbi", CallingConvention = CallingConvention.Cdecl, EntryPoint = "stbi_write_png_to_func")]