/// <summary> /// create host visible linear image without command from data pointed by IntPtr pointer containing full image file (jpg, png,...) /// </summary> public static Image Load(Device dev, IntPtr bitmap, ulong bitmapByteCount, VkImageUsageFlags usage = VkImageUsageFlags.TransferSrc, VkFormat format = VkFormat.Undefined, VkMemoryPropertyFlags memoryProps = VkMemoryPropertyFlags.HostVisible | VkMemoryPropertyFlags.HostCoherent, VkImageTiling tiling = VkImageTiling.Linear, bool generateMipmaps = false, VkImageType imageType = VkImageType.Image2D) { if (format == VkFormat.Undefined) { format = DefaultTextureFormat; } if (generateMipmaps) { usage |= (VkImageUsageFlags.TransferSrc | VkImageUsageFlags.TransferDst); } using (StbImage stbi = new StbImage(bitmap, bitmapByteCount)) { uint mipLevels = generateMipmaps ? ComputeMipLevels(stbi.Width, stbi.Height) : 1; Image img = new Image(dev, format, usage, memoryProps, (uint)stbi.Width, (uint)stbi.Height, imageType, VkSampleCountFlags.SampleCount1, tiling, mipLevels); img.Map(); stbi.CoptyTo(img.MappedData); img.Unmap(); return(img); } }
/// <summary> /// Load bitmap into Image with stagging and mipmap generation if necessary /// and usage. /// </summary> public static Image Load(Device dev, Queue staggingQ, CommandPool staggingCmdPool, string path, VkFormat format = VkFormat.Undefined, VkMemoryPropertyFlags memoryProps = VkMemoryPropertyFlags.DeviceLocal, VkImageTiling tiling = VkImageTiling.Optimal, bool generateMipmaps = true, VkImageType imageType = VkImageType.Image2D, VkImageUsageFlags usage = VkImageUsageFlags.Sampled | VkImageUsageFlags.TransferSrc | VkImageUsageFlags.TransferDst) { if (format == VkFormat.Undefined) { format = DefaultTextureFormat; } if (tiling == VkImageTiling.Optimal) { usage |= VkImageUsageFlags.TransferDst; } if (generateMipmaps) { usage |= (VkImageUsageFlags.TransferSrc | VkImageUsageFlags.TransferDst); } using (StbImage stbi = new StbImage(path)) { uint mipLevels = generateMipmaps ? ComputeMipLevels(stbi.Width, stbi.Height) : 1; Image img = new Image(dev, format, usage, memoryProps, (uint)stbi.Width, (uint)stbi.Height, imageType, VkSampleCountFlags.SampleCount1, tiling, mipLevels); img.load(staggingQ, staggingCmdPool, stbi.Handle, generateMipmaps); return(img); } }
public void Dispose() { #if STB_SHARP gcHandle.Free(); #else StbImage.FreeImage(Handle); #endif }
/// <summary> /// Open image with STBI library /// </summary> /// <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) { #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; } }
/// <summary> /// Open image with STBI library /// </summary> /// <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; } }
/// <summary> /// create host visible linear image without command from path /// </summary> public static Image Load(Device dev, string path, VkImageUsageFlags usage = VkImageUsageFlags.Sampled, bool reserveSpaceForMipmaps = true, VkFormat format = VkFormat.Undefined, VkMemoryPropertyFlags memoryProps = VkMemoryPropertyFlags.HostVisible | VkMemoryPropertyFlags.HostCoherent, VkImageTiling tiling = VkImageTiling.Linear, VkImageType imageType = VkImageType.Image2D) { if (format == VkFormat.Undefined) { format = DefaultTextureFormat; } using (StbImage stbi = new StbImage(path)) { uint mipLevels = reserveSpaceForMipmaps ? ComputeMipLevels(stbi.Width, stbi.Height) : 1; Image img = new Image(dev, format, usage, memoryProps, (uint)stbi.Width, (uint)stbi.Height, imageType, VkSampleCountFlags.SampleCount1, tiling, mipLevels); img.Map(); stbi.CoptyTo(img.MappedData); img.Unmap(); return(img); } }
/// <summary> /// Open image with STBI library /// </summary> /// <param name="bitmap">raw bitmap datas</param> /// <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; } }