//----------------------------------------------------------------------- //----------------------------------------------------------------------- // file="Image.cs" // Project: StgSharp // AuthorGroup: Nitload Space // Copyright (c) Nitload Space. All rights reserved. // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation // files (the “Software”), to deal in the Software without restriction, // including without limitation the rights to use, copy, modify, merge, // publish, distribute, sublicense, and/or sell copies of the Software, // and to permit persons to whom the Software is furnished to do so, // subject to the following conditions: // // The above copyright notice and // this permission notice shall be included in all copies // or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED “AS IS”, // WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, // ARISING FROM, OUT OF OR IN CONNECTION WITH // THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // //----------------------------------------------------------------------- //----------------------------------------------------------------------- using StgSharp.Data; using StgSharp.Graphics.GL; using StgSharp.Math; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; namespace StgSharp.Graphics { public class Image : ISSDSerializable, IImageProvider { internal byte[] data; internal ImageInfo info; internal Image(ImageInfo information) { info = information; data = Array.Empty(); //int length = info.width * info.height * info.channel; } public ref byte[] DataStream { get => ref data; } public unsafe Image() { info = new ImageInfo(); } public ImageChannel Channel => info.Channel; public int ChannelCount => info.ChannelCount; public int Height => info.Height; public (int width, int height) Size { get => (info.Width, info.Height); } public SerializableTypeCode SSDTypeCode => SerializableTypeCode.PixelImage; public int Width => info.Width; public static unsafe Image FromFile(string route, ImageLoader loader) { Image ret = new Image(); fixed (ImageInfo* iptr = &ret.info) { InternalIO.InternalLoadImage(route, iptr, loader); } return ret; } public static Image FromMemory((int width, int height) size, int channelCount, byte[] stream) { ImageInfo _info = new ImageInfo { Height = size.height, Width = size.width, ChannelCount = channelCount, StreamPtr = IntPtr.Zero }; Image ret = new Image(_info); ret.data = stream; return ret; } public glArrayHandle GetArrayHandle() { if ((data == Array.Empty()) || (data == null)) { this.GetBytes(); return new glArrayHandle(data); } return new glArrayHandle(data); } public unsafe byte[] GetBytes() { if (data == null) { int size = Width * Height * ChannelCount; data = new byte[size]; int i = 0; fixed (byte* bptr = data) { byte* pptr = bptr; for (; i < size; i += 16) { *(M128*)(pptr + i) = *(M128*)(info.StreamPtr + i); } for (; i < size; i++) { *(pptr + i) = *(byte*)(info.StreamPtr + i); } } fixed (ImageInfo* pptr = &info) { InternalIO.InternalUnloadImage(pptr); } info.StreamPtr = IntPtr.Zero; } return data; } public void FromBytes(byte[] stream) { } public Image ProvideImage() { throw new NotImplementedException(); } ~Image() { if (info.StreamPtr != IntPtr.Zero) { // Marshal.FreeHGlobal(info.StreamPtr); } } } [StructLayout(LayoutKind.Sequential)] internal struct ImageInfo { private ImageChannel channel; private int channelCount; private int height; private int width; private IntPtr pixelPtr; public ImageChannel Channel { get => channel; set { channel = value; switch (channel) { case ImageChannel.SingleColor: channelCount = 1; break; case ImageChannel.DoubleColor: channelCount = 2; break; case ImageChannel.FullColor: channelCount = 3; break; case ImageChannel.WithAlphaChannel: channelCount = 4; break; default: throw new InvalidCastException("Unknown color channel decoding setting."); } } } public int ChannelCount { get => channelCount; set { channelCount = value; switch (channelCount) { case 1: channel = ImageChannel.SingleColor; break; case 2: channel = ImageChannel.DoubleColor; break; case 3: channel = ImageChannel.FullColor; break; case 4: channel = ImageChannel.WithAlphaChannel; break; default: throw new InvalidCastException("Unknown color channel decoding setting."); } } } public int Height { get => height; set => height = value; } public IntPtr StreamPtr { get => pixelPtr; internal set => pixelPtr = value; } public int StreamSize { get => width * height * channelCount; } public int Width { get => width; set => width = value; } public override string ToString() { return $"{channel}\t{width}\t{height}\t{(ulong)StreamPtr}"; } } public enum ImageChannel : uint { SingleColor = GLconst.RED, DoubleColor = GLconst.RG, FullColor = GLconst.RGB, WithAlphaChannel = GLconst.RGBA, } }