Exemple #1
0
        /// <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;
            }
        }
Exemple #2
0
        /// <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;
            }
        }
Exemple #3
0
        /// <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;
            }
        }