From: Jean-Philippe Bruyère Date: Mon, 1 Jun 2020 01:11:39 +0000 (+0200) Subject: optional stbimagesharp, not optimized but functional X-Git-Tag: v0.1.21~2 X-Git-Url: https://git.osiis.dedyn.io/?a=commitdiff_plain;h=2e42ce2084f7fcb63a755873b1b29325d00a0e11;p=jp%2Fvke.net.git optional stbimagesharp, not optimized but functional --- diff --git a/vke/src/StbImage.cs b/vke/src/StbImage.cs index 5da7534..d56ef18 100644 --- a/vke/src/StbImage.cs +++ b/vke/src/StbImage.cs @@ -2,11 +2,16 @@ // // This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) using System; +using System.IO; using System.Runtime.InteropServices; namespace vke { public class StbImage : IDisposable { - const string stblib = "stb"; +#if STB_SHARP + GCHandle gcHandle; + public IntPtr Handle => gcHandle.AddrOfPinnedObject (); +#else + const string stblib = "stb"; [DllImport (stblib, CallingConvention = CallingConvention.Cdecl, EntryPoint = "stbi_load")] static extern IntPtr Load ([MarshalAs (UnmanagedType.LPStr)] string filename, out int x, out int y, out int channels_in_file, int desired_channels); @@ -15,12 +20,14 @@ namespace vke { static extern IntPtr Load (IntPtr bitmap, int byteCount, out int x, out int y, out int channels_in_file, int desired_channels); [DllImport (stblib, CallingConvention = CallingConvention.Cdecl, EntryPoint = "stbi_load_from_memory")] - static extern IntPtr Load2 (ref byte bitmap, int byteCount, out int x, out int y, out int channels_in_file, int desired_channels); + static extern IntPtr Load (ref byte bitmap, int byteCount, out int x, out int y, out int channels_in_file, int desired_channels); [DllImport (stblib, CallingConvention = CallingConvention.Cdecl, EntryPoint = "stbi_image_free")] static extern void FreeImage (IntPtr img); - public readonly IntPtr Handle; + public IntPtr Handle { get; private set; } +#endif + public readonly int Width; public readonly int Height; public readonly int Channels; @@ -32,9 +39,20 @@ namespace vke { /// file path /// Force returned channels count, set 0 for original count public StbImage (string path, int requestedChannels = 4) { +#if STB_SHARP + using (Stream stream = new FileStream (path, FileMode.Open)) { + StbImageSharp.ImageResult stbi = + StbImageSharp.ImageResult.FromStream (stream, (StbImageSharp.ColorComponents)requestedChannels); + Width = stbi.Width; + Height = stbi.Height; + Channels = (int)stbi.Comp; + gcHandle = GCHandle.Alloc (stbi.Data, GCHandleType.Pinned); + } +#else Handle = StbImage.Load (path, out Width, out Height, out Channels, requestedChannels); if (Handle == IntPtr.Zero) throw new Exception ($"STBI image loading error."); +#endif if (requestedChannels > 0) Channels = requestedChannels; } @@ -45,9 +63,21 @@ namespace vke { /// Bitmap byte count. /// Force returned channels count, set 0 for original count public StbImage (IntPtr bitmap, ulong bitmapByteCount, int requestedChannels = 4) { +#if STB_SHARP + unsafe { + Span byteArray = new Span (bitmap.ToPointer (), (int)bitmapByteCount); + StbImageSharp.ImageResult stbi = + StbImageSharp.ImageResult.FromMemory (byteArray.ToArray (), (StbImageSharp.ColorComponents)requestedChannels); + Width = stbi.Width; + Height = stbi.Height; + Channels = (int)stbi.Comp; + gcHandle = GCHandle.Alloc (stbi.Data, GCHandleType.Pinned); + } +#else Handle = StbImage.Load (bitmap, (int)bitmapByteCount, out Width, out Height, out Channels, requestedChannels); if (Handle == IntPtr.Zero) throw new Exception ($"STBI image loading error."); +#endif if (requestedChannels > 0) Channels = requestedChannels; } @@ -57,9 +87,18 @@ namespace vke { /// raw bitmap datas /// Force returned channels count, set 0 for original count public StbImage (Memory bitmap, int requestedChannels = 4) { - Handle = StbImage.Load2 (ref MemoryMarshal.GetReference(bitmap.Span), bitmap.Length, out Width, out Height, out Channels, requestedChannels); +#if STB_SHARP + StbImageSharp.ImageResult stbi = + StbImageSharp.ImageResult.FromMemory (bitmap.ToArray (), (StbImageSharp.ColorComponents)requestedChannels); + Width = stbi.Width; + Height = stbi.Height; + Channels = (int)stbi.Comp; + gcHandle = GCHandle.Alloc (stbi.Data, GCHandleType.Pinned); +#else + Handle = StbImage.Load (ref MemoryMarshal.GetReference(bitmap.Span), bitmap.Length, out Width, out Height, out Channels, requestedChannels); if (Handle == IntPtr.Zero) throw new Exception ($"STBI image loading error."); +#endif if (requestedChannels > 0) Channels = requestedChannels; } @@ -73,7 +112,12 @@ namespace vke { } } public void Dispose () { + +#if STB_SHARP + gcHandle.Free (); +#else StbImage.FreeImage (Handle); +#endif } } } diff --git a/vke/src/base/Image.cs b/vke/src/base/Image.cs index 0b01b46..88ab2be 100644 --- a/vke/src/base/Image.cs +++ b/vke/src/base/Image.cs @@ -176,19 +176,6 @@ namespace vke { usage |= VkImageUsageFlags.TransferDst; if (generateMipmaps) usage |= (VkImageUsageFlags.TransferSrc | VkImageUsageFlags.TransferDst); - /*#if STB_SHARP - StbImageSharp.ImageResult stbi = StbImageSharp.ImageResult.FromMemory (stream, StbImageSharp.ColorComponents.RedGreenBlueAlpha); - uint mipLevels = generateMipmaps ? ComputeMipLevels (stbi.Width, stbi.Height) : 1; - image = new byte [stbi.Data.Length]; - //rgba to argb for cairo. - for (int i = 0; i < stbi.Data.Length; i += 4) { - image [i] = stbi.Data[i + 2]; - image [i + 1] = stbi.Data [i + 1]; - image [i + 2] = stbi.Data [i]; - image [i + 3] = stbi.Data [i + 3]; - } - Dimensions = new Size (stbi.Width, stbi.Height); - #else*/ using (StbImage stbi = new StbImage (bitmap)) { uint mipLevels = generateMipmaps ? ComputeMipLevels (stbi.Width, stbi.Height) : 1; @@ -200,7 +187,6 @@ namespace vke { return img; } -//#endif } /// /// create host visible linear image without command from data pointed by IntPtr pointer containing full image file (jpg, png,...) @@ -264,19 +250,7 @@ namespace vke { usage |= VkImageUsageFlags.TransferDst; if (generateMipmaps) usage |= (VkImageUsageFlags.TransferSrc | VkImageUsageFlags.TransferDst); -/*#if STB_SHARP - StbImageSharp.ImageResult stbi = StbImageSharp.ImageResult.FromMemory (stream, StbImageSharp.ColorComponents.RedGreenBlueAlpha); - uint mipLevels = generateMipmaps ? ComputeMipLevels (stbi.Width, stbi.Height) : 1; - image = new byte [stbi.Data.Length]; - //rgba to argb for cairo. - for (int i = 0; i < stbi.Data.Length; i += 4) { - image [i] = stbi.Data[i + 2]; - image [i + 1] = stbi.Data [i + 1]; - image [i + 2] = stbi.Data [i]; - image [i + 3] = stbi.Data [i + 3]; - } - Dimensions = new Size (stbi.Width, stbi.Height); -#else*/ + using (StbImage stbi = new StbImage (bitmap, bitmapByteCount)) { uint mipLevels = generateMipmaps ? ComputeMipLevels (stbi.Width, stbi.Height) : 1; @@ -287,7 +261,6 @@ namespace vke { return img; } -//#endif } /// diff --git a/vke/vke.csproj b/vke/vke.csproj index e920e5f..8def075 100644 --- a/vke/vke.csproj +++ b/vke/vke.csproj @@ -47,7 +47,7 @@ - +