]> O.S.I.I.S - jp/vke.net.git/commitdiff
optional stbimagesharp, not optimized but functional
authorJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Mon, 1 Jun 2020 01:11:39 +0000 (03:11 +0200)
committerJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Mon, 1 Jun 2020 01:11:39 +0000 (03:11 +0200)
vke/src/StbImage.cs
vke/src/base/Image.cs
vke/vke.csproj

index 5da7534469c16617aa3d975849e874ebfa1b82de..d56ef18637de728c7b112c3eee3124e3c0ad8c5c 100644 (file)
@@ -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 {
                /// <param name="path">file path</param>
                /// <param name="requestedChannels">Force returned channels count, set 0 for original count</param>
                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 {
                /// <param name="bitmapByteCount">Bitmap byte count.</param>
                /// <param name="requestedChannels">Force returned channels count, set 0 for original count</param>
                public StbImage (IntPtr bitmap, ulong bitmapByteCount, int requestedChannels = 4) {
+#if STB_SHARP
+                       unsafe {
+                               Span<byte> byteArray = new Span<byte> (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 {
                /// <param name="bitmap">raw bitmap datas</param>
                /// <param name="requestedChannels">Force returned channels count, set 0 for original count</param>
                public StbImage (Memory<byte> 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
                }
        }
 }
index 0b01b4605e5fa529a38b492b6197f4082cf424a8..88ab2be28d010275cd041800cb108bf507bb5ab6 100644 (file)
@@ -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
                }
                /// <summary>
                /// 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
                }
 
                /// <summary>
index e920e5fc89fa1786309de79dff08fb517bbe3910..8def075071bf569c0ee40ccd2338746e85fc3451 100644 (file)
@@ -47,7 +47,7 @@
 
        <ItemGroup>
                <PackageReference Include="SpirVTasks" Version="$(SpirVTasksPackageVersion)" />
-               <PackageReference Include="Vulkan" Version="0.2.20-beta" />
+               <PackageReference Include="Vulkan" Version="0.2.3-beta" />
                <PackageReference Include="shaderc.net" Version="0.1.0" />
                <PackageReference Include="glfw-sharp" Version="0.2.6-beta" />
        </ItemGroup>