MVVM frame work done, under unit test
This commit is contained in:
@@ -29,11 +29,12 @@
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
using StgSharp.Data;
|
||||
using StgSharp.Graphics.GL;
|
||||
using StgSharp.Graphics.OpenGL;
|
||||
using StgSharp.Math;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
@@ -49,6 +50,17 @@ namespace StgSharp.Graphics
|
||||
internal byte[] data;
|
||||
internal ImageInfo info;
|
||||
|
||||
public PixelChannelLayout PixelLayout
|
||||
{
|
||||
get => info.pixelLayout;
|
||||
private set => info.pixelLayout = value;
|
||||
}
|
||||
|
||||
internal unsafe Image()
|
||||
{
|
||||
info = new ImageInfo();
|
||||
}
|
||||
|
||||
internal Image(ImageInfo information)
|
||||
{
|
||||
info = information;
|
||||
@@ -56,16 +68,6 @@ namespace StgSharp.Graphics
|
||||
//int length = info.width * info.height * info.channel;
|
||||
}
|
||||
|
||||
public unsafe Image()
|
||||
{
|
||||
info = new ImageInfo();
|
||||
}
|
||||
|
||||
public ref byte[] DataStream
|
||||
{
|
||||
get => ref data;
|
||||
}
|
||||
|
||||
public (int width, int height) Size
|
||||
{
|
||||
get => (info.Width, info.Height);
|
||||
@@ -78,12 +80,15 @@ namespace StgSharp.Graphics
|
||||
get => info.Height;
|
||||
internal set => info.Height = value;
|
||||
}
|
||||
|
||||
public int Width
|
||||
{
|
||||
get => info.Width;
|
||||
internal set => info.Width = value;
|
||||
}
|
||||
|
||||
public int PixelSize => GlHelper.GetPixelSize(info.pixelLayout, info.Channel);
|
||||
|
||||
public SerializableTypeCode SSDTypeCode => SerializableTypeCode.PixelImage;
|
||||
|
||||
public void FromBytes(byte[] stream)
|
||||
@@ -101,71 +106,68 @@ namespace StgSharp.Graphics
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static Image FromMemory((int width, int height) size, int channelCount, byte[] stream)
|
||||
public static Image FromMemory((int width, int height) size, ImageChannel channel, PixelChannelLayout layout, byte[] stream)
|
||||
{
|
||||
ImageInfo _info = new ImageInfo
|
||||
{
|
||||
Height = size.height,
|
||||
Width = size.width,
|
||||
ChannelCount = channelCount,
|
||||
Channel = channel,
|
||||
StreamPtr = IntPtr.Zero
|
||||
};
|
||||
Image ret = new Image(_info);
|
||||
ret.PixelLayout = layout;
|
||||
ret.data = stream;
|
||||
return ret;
|
||||
}
|
||||
|
||||
public glArrayHandle GetArrayHandle()
|
||||
public static Image FromMemory((int width, int height) size, ImageChannel channel, byte[] stream)
|
||||
{
|
||||
if ((data == Array.Empty<byte>()) || (data == null))
|
||||
ImageInfo _info = new ImageInfo
|
||||
{
|
||||
this.GetBytes();
|
||||
return new glArrayHandle(data);
|
||||
}
|
||||
return new glArrayHandle(data);
|
||||
Height = size.height,
|
||||
Width = size.width,
|
||||
Channel = channel,
|
||||
StreamPtr = IntPtr.Zero
|
||||
};
|
||||
Image ret = new Image(_info);
|
||||
ret.PixelLayout = PixelChannelLayout.Byte;
|
||||
ret.data = stream;
|
||||
return ret;
|
||||
}
|
||||
|
||||
public unsafe byte[] GetBytes()
|
||||
{
|
||||
if (data == null)
|
||||
{
|
||||
int size = Width * Height * ChannelCount;
|
||||
data = new byte[size];
|
||||
int i = 0;
|
||||
fixed (byte* bptr = data)
|
||||
if (data.Length == 0)
|
||||
{
|
||||
byte* pptr = bptr;
|
||||
for (; i < size; i += 16)
|
||||
int size = Width * Height * GlHelper.GetPixelSize(PixelLayout, Channel);
|
||||
data = new Span<byte>((byte*)info.StreamPtr, size).ToArray();
|
||||
fixed (ImageInfo* pptr = &info)
|
||||
{
|
||||
*(M128*)(pptr + i) = *(M128*)(info.StreamPtr + i);
|
||||
}
|
||||
for (; i < size; i++)
|
||||
{
|
||||
*(pptr + i) = *(byte*)(info.StreamPtr + i);
|
||||
InternalIO.InternalUnloadImage(pptr);
|
||||
}
|
||||
info.StreamPtr = IntPtr.Zero;
|
||||
}
|
||||
fixed (ImageInfo* pptr = &info)
|
||||
{
|
||||
InternalIO.InternalUnloadImage(pptr);
|
||||
}
|
||||
info.StreamPtr = IntPtr.Zero;
|
||||
}
|
||||
return data;
|
||||
return data;
|
||||
}
|
||||
|
||||
public Image ProvideImage()
|
||||
public byte[] PixelBuffer
|
||||
{
|
||||
get{
|
||||
if ((data.Length == 0) || (data == null))
|
||||
{
|
||||
GetBytes();
|
||||
return data!;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
}
|
||||
public Image ProvideImage() => this;
|
||||
|
||||
byte[] ISSDSerializable.GetBytes()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
~Image()
|
||||
{
|
||||
if (info.StreamPtr != IntPtr.Zero)
|
||||
{
|
||||
// Marshal.FreeHGlobal(info.StreamPtr);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
@@ -173,10 +175,10 @@ namespace StgSharp.Graphics
|
||||
{
|
||||
|
||||
private ImageChannel channel;
|
||||
private int channelCount;
|
||||
private int height;
|
||||
private int width;
|
||||
private IntPtr pixelPtr;
|
||||
internal PixelChannelLayout pixelLayout;
|
||||
|
||||
public ImageChannel Channel
|
||||
{
|
||||
@@ -184,42 +186,12 @@ namespace StgSharp.Graphics
|
||||
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.");
|
||||
}
|
||||
}
|
||||
get => GlHelper.GetChannelCount(channel);
|
||||
}
|
||||
|
||||
public int Height
|
||||
@@ -230,7 +202,7 @@ namespace StgSharp.Graphics
|
||||
|
||||
public int StreamSize
|
||||
{
|
||||
get => width * height * channelCount;
|
||||
get => width * height * GlHelper.GetPixelSize(pixelLayout, channel);
|
||||
}
|
||||
|
||||
public int Width
|
||||
@@ -256,9 +228,53 @@ namespace StgSharp.Graphics
|
||||
{
|
||||
SingleColor = GLconst.RED,
|
||||
DoubleColor = GLconst.RG,
|
||||
FullColor = GLconst.RGB,
|
||||
TripleColor = GLconst.RGB,
|
||||
WithAlphaChannel = GLconst.RGBA,
|
||||
}
|
||||
}
|
||||
|
||||
namespace StgSharp.Graphics.OpenGL
|
||||
{
|
||||
public static partial class GlHelper
|
||||
{
|
||||
|
||||
internal static int GetChannelCount(ImageChannel channel)
|
||||
{
|
||||
return channel switch
|
||||
{
|
||||
ImageChannel.SingleColor => 1,
|
||||
ImageChannel.DoubleColor => 2,
|
||||
ImageChannel.TripleColor => 3,
|
||||
ImageChannel.WithAlphaChannel => 4,
|
||||
_ => throw new InvalidCastException("Unknown color channel decoding setting.")
|
||||
};
|
||||
}
|
||||
|
||||
internal static int GetPixelSize(PixelChannelLayout layout, ImageChannel cahnnel)
|
||||
{
|
||||
return layout switch
|
||||
{
|
||||
PixelChannelLayout.UByte => sizeof(byte) * GetChannelCount(cahnnel),
|
||||
PixelChannelLayout.Byte => sizeof(byte) * GetChannelCount(cahnnel),
|
||||
PixelChannelLayout.UShort => sizeof(short) * GetChannelCount(cahnnel),
|
||||
PixelChannelLayout.Short => sizeof(short) * GetChannelCount(cahnnel),
|
||||
PixelChannelLayout.UInt => sizeof(int) * GetChannelCount(cahnnel),
|
||||
PixelChannelLayout.Int => sizeof(int) * GetChannelCount(cahnnel),
|
||||
PixelChannelLayout.Float => sizeof(float) * GetChannelCount(cahnnel),
|
||||
PixelChannelLayout.UByte332 => sizeof(byte),
|
||||
PixelChannelLayout.UByte233Rev => sizeof(byte),
|
||||
PixelChannelLayout.UShort565 => sizeof(short),
|
||||
PixelChannelLayout.UShort565Rev => sizeof(short),
|
||||
PixelChannelLayout.UShort4444 => sizeof(short),
|
||||
PixelChannelLayout.UShort4444Rev => sizeof(short),
|
||||
PixelChannelLayout.UShort5551 => sizeof(short),
|
||||
PixelChannelLayout.UShort1555Rev => sizeof(short),
|
||||
PixelChannelLayout.UInt8888 => sizeof(int),
|
||||
PixelChannelLayout.UInt8888Rev => sizeof(int),
|
||||
PixelChannelLayout.UInt1010102 => sizeof(int),
|
||||
PixelChannelLayout.UInt2101010Rev => sizeof(int),
|
||||
_ => throw new ArgumentException("Unknown layout type"),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user