daily update on 20240528 by nitload youmu
This commit is contained in:
+208
-85
@@ -29,11 +29,13 @@
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
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;
|
||||
@@ -41,18 +43,21 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Graphics
|
||||
{
|
||||
public class Image : SSDSerializable
|
||||
public class Image : SSDSerializable, IImageProvider
|
||||
{
|
||||
|
||||
internal ImageInfo info;
|
||||
internal byte[] data;
|
||||
internal glHandle pixelArrayHandle;
|
||||
internal ImageInfo info;
|
||||
|
||||
internal Image(ImageInfo information)
|
||||
{
|
||||
info = information;
|
||||
int length = info.width * info.height * info.channel;
|
||||
pixelArrayHandle = glHandle.FromPtr(information.pixelPtr);
|
||||
data = Array.Empty<byte>();
|
||||
//int length = info.width * info.height * info.channel;
|
||||
}
|
||||
|
||||
public ref byte[] DataStream
|
||||
{
|
||||
get => ref data;
|
||||
}
|
||||
|
||||
public unsafe Image()
|
||||
@@ -60,104 +65,222 @@ namespace StgSharp.Graphics
|
||||
info = new ImageInfo();
|
||||
}
|
||||
|
||||
public int Channel => info.channel;
|
||||
public int Height => info.height;
|
||||
public ImageChannel Channel => info.Channel;
|
||||
public int ChannelCount => info.ChannelCount;
|
||||
public int Height => info.Height;
|
||||
|
||||
public int Width => info.width;
|
||||
public (int width, int height) Size
|
||||
{
|
||||
get => (info.Width, info.Height);
|
||||
}
|
||||
|
||||
public override 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<byte>()) || (data == null))
|
||||
{
|
||||
this.GetBytes();
|
||||
return new glArrayHandle(data);
|
||||
}
|
||||
return new glArrayHandle(data);
|
||||
}
|
||||
|
||||
public override 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 override unsafe byte[] GetBytes(out int length)
|
||||
{
|
||||
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;
|
||||
}
|
||||
length = data.Length;
|
||||
return data;
|
||||
}
|
||||
|
||||
internal override unsafe void BuildFromByteStream(byte[] stream)
|
||||
{
|
||||
throw new NotSupportedException(
|
||||
$"{($"{typeof(Image).Name} does not support BuildFromByteStream,")}{($"please use Image.FromMemory instead.")}"
|
||||
);
|
||||
}
|
||||
|
||||
public Image ProvideImage()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
~Image()
|
||||
{
|
||||
if (info.pixelPtr!= IntPtr.Zero)
|
||||
if (info.StreamPtr != IntPtr.Zero)
|
||||
{
|
||||
// Marshal.FreeHGlobal(info.pixelPtr);
|
||||
// Marshal.FreeHGlobal(info.StreamPtr);
|
||||
}
|
||||
}
|
||||
|
||||
public unsafe override byte[] GetBytes()
|
||||
{
|
||||
if (data == null)
|
||||
{
|
||||
int size = Width * Height * Channel + 1 + 8;
|
||||
data = new byte[size];
|
||||
int i = 0;
|
||||
fixed (byte* bptr = data)
|
||||
{
|
||||
*(int*)bptr = Width;
|
||||
*(int*)(bptr + 4) = Height;
|
||||
*(bptr + 8) = (byte)Channel;
|
||||
byte* pptr = bptr;
|
||||
for (; i < size; i += 16)
|
||||
{
|
||||
*(M128*)(pptr + i) = *(M128*)(info.pixelPtr + i);
|
||||
}
|
||||
for (; i < size; i++)
|
||||
{
|
||||
*(pptr + i) = *(byte*)(info.pixelPtr + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
Marshal.FreeHGlobal(info.pixelPtr);
|
||||
info.pixelPtr = IntPtr.Zero;
|
||||
return data;
|
||||
}
|
||||
|
||||
public unsafe override byte[] GetBytes(out int length)
|
||||
{
|
||||
if (data == null)
|
||||
{
|
||||
int size = Width * Height * Channel + 1 + 8;
|
||||
data = new byte[size];
|
||||
int i = 0;
|
||||
fixed (byte* bptr = data)
|
||||
{
|
||||
*(int*)bptr = Width;
|
||||
*(int*)(bptr + 4) = Height;
|
||||
*(bptr + 8) = (byte)Channel;
|
||||
byte* pptr = bptr;
|
||||
for (; i < size; i += 16)
|
||||
{
|
||||
*(M128*)(pptr + i) = *(M128*)(info.pixelPtr + i);
|
||||
}
|
||||
for (; i < size; i++)
|
||||
{
|
||||
*(pptr + i) = *(byte*)(info.pixelPtr + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
length = data.Length;
|
||||
Marshal.FreeHGlobal(info.pixelPtr);
|
||||
info.pixelPtr = IntPtr.Zero;
|
||||
return data;
|
||||
}
|
||||
|
||||
internal unsafe override void BuildFromByteStream(byte[] stream)
|
||||
{
|
||||
info = new ImageInfo();
|
||||
fixed (byte* bptr = stream)
|
||||
{
|
||||
info.width = *(int*)bptr;
|
||||
info.height = *(int*)(bptr + 4);
|
||||
info.channel = *(bptr + 8);
|
||||
info.pixelPtr = IntPtr.Zero;
|
||||
}
|
||||
data = stream;
|
||||
}
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal struct ImageInfo
|
||||
{
|
||||
internal int width;
|
||||
internal int height;
|
||||
internal int channel;
|
||||
internal IntPtr pixelPtr;
|
||||
|
||||
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{pixelPtr}";
|
||||
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,
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user