daily update on 20240828 by nitload youmu, autotexture added

This commit is contained in:
Nitload
2024-08-28 10:58:31 +08:00
parent d082071f2d
commit 1ee4e4a42f
586 changed files with 60910 additions and 123106 deletions
+151 -104
View File
@@ -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;
@@ -45,37 +46,82 @@ namespace StgSharp.Graphics
{
public class Image : ISSDSerializable, IImageProvider
{
internal byte[] data;
internal ImageInfo info;
private byte[] _data;
private ImageInfo info;
private int _pixelUpdateCount = 0;
internal Image(ImageInfo information)
{
info = information;
data = Array.Empty<byte>();
//int length = info.width * info.height * info.channel;
}
public ref byte[] DataStream
{
get => ref data;
}
public unsafe Image()
internal unsafe Image()
{
info = new ImageInfo();
}
public ImageChannel Channel => info.Channel;
public int ChannelCount => info.ChannelCount;
public int Height => info.Height;
internal Image(ImageInfo information)
{
info = information;
_data = Array.Empty<byte>();
//int length = info.width * info.height * info.channel;
}
internal ref byte[] Data
{
get => ref _data;
}
public (int width, int height) Size
{
get => (info.Width, info.Height);
}
/// <summary>
/// Hash code of time pixels updated last time.
/// </summary>
public int PixelUpdateCount
{
get => _pixelUpdateCount;
internal set => _pixelUpdateCount = value;
}
public byte[] PixelBuffer
{
get
{
if ((_data.Length == 0) || (_data == null))
{
GetBytes();
return _data!;
}
return _data;
}
}
public ImageChannel Channel => info.Channel;
public int ChannelCount => info.ChannelCount;
public int Height
{
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 PixelChannelLayout PixelLayout
{
get => info.pixelLayout;
private set => info.pixelLayout = value;
}
public SerializableTypeCode SSDTypeCode => SerializableTypeCode.PixelImage;
public int Width => info.Width;
public void FromBytes(byte[] stream)
{
}
public static unsafe Image FromFile(string route, ImageLoader loader)
{
@@ -87,76 +133,57 @@ 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, 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.data = stream;
ret.PixelLayout = PixelChannelLayout.Byte;
ret._data = stream;
return ret;
}
public glArrayHandle GetArrayHandle()
public static Image FromMemory((int width, int height) size, ImageChannel channel, PixelChannelLayout layout, 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 = layout;
ret._data = stream;
return ret;
}
public unsafe byte[] GetBytes()
{
if (data == null)
if (_data.Length == 0)
{
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);
}
}
int size = Width * Height * GlHelper.GetPixelSize(PixelLayout, Channel);
_data = new Span<byte>((byte*)info.StreamPtr, size).ToArray();
fixed (ImageInfo* pptr = &info)
{
InternalIO.InternalUnloadImage(pptr);
}
info.StreamPtr = IntPtr.Zero;
}
return data;
return _data;
}
public Image ProvideImage() => this;
public void FromBytes(byte[] stream)
{
}
public Image ProvideImage()
byte[] ISSDSerializable.GetBytes()
{
throw new NotImplementedException();
}
~Image()
{
if (info.StreamPtr != IntPtr.Zero)
{
// Marshal.FreeHGlobal(info.StreamPtr);
}
}
}
[StructLayout(LayoutKind.Sequential)]
@@ -164,53 +191,20 @@ namespace StgSharp.Graphics
{
private ImageChannel channel;
private int channelCount;
private int height;
private int width;
private IntPtr pixelPtr;
internal PixelChannelLayout pixelLayout;
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.");
}
}
set { channel = value; }
}
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
@@ -219,15 +213,9 @@ namespace StgSharp.Graphics
set => height = value;
}
public IntPtr StreamPtr
{
get => pixelPtr;
internal set => pixelPtr = value;
}
public int StreamSize
{
get => width * height * channelCount;
get => width * height * GlHelper.GetPixelSize(pixelLayout, channel);
}
public int Width
@@ -236,20 +224,79 @@ namespace StgSharp.Graphics
set => width = value;
}
public IntPtr StreamPtr
{
get => pixelPtr;
internal set => pixelPtr = value;
}
public override string ToString()
{
return $"{channel}\t{width}\t{height}\t{(ulong)StreamPtr}";
}
public override int GetHashCode()
{
int hash = 17;
hash = hash * 17 + width;
hash = hash * 17 + height;
hash = hash * 17 + (int)channel;
return hash;
}
}
public enum ImageChannel : uint
{
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"),
};
}
}
}