89 行
2.9 KiB
C#
89 行
2.9 KiB
C#
using System.Diagnostics;
|
|
using MoonSharp.Interpreter;
|
|
|
|
namespace LuaScript
|
|
{
|
|
[MoonSharpUserData]
|
|
internal sealed class PixelDataProxy(AviUtlScriptContext ctx)
|
|
{
|
|
public int width => ctx.ImageWidth;
|
|
public int height => ctx.ImageHeight;
|
|
|
|
public unsafe double get(int index)
|
|
{
|
|
ctx.EnsurePixelBuffer();
|
|
var buf = ctx.GetPixelBuffer();
|
|
if (buf is null) return 0d;
|
|
|
|
int zeroBasedIndex = index - 1;
|
|
if ((uint)zeroBasedIndex >= (uint)ctx.TotalChannels) return 0d;
|
|
|
|
int pixelIndex = Math.DivRem(zeroBasedIndex, 4, out int channel);
|
|
|
|
fixed (byte* p = buf)
|
|
{
|
|
byte* pixel = p + pixelIndex * 4;
|
|
double a = pixel[3];
|
|
if (channel == 3)
|
|
return a;
|
|
if (a <= 0d)
|
|
return 0d;
|
|
|
|
double scale = 255d / a;
|
|
return channel switch
|
|
{
|
|
0 => Math.Clamp(pixel[2] * scale, 0d, 255d),
|
|
1 => Math.Clamp(pixel[1] * scale, 0d, 255d),
|
|
2 => Math.Clamp(pixel[0] * scale, 0d, 255d),
|
|
_ => throw new UnreachableException()
|
|
};
|
|
}
|
|
}
|
|
|
|
public unsafe void set(int index, double value)
|
|
{
|
|
ctx.EnsurePixelBuffer();
|
|
var buf = ctx.GetPixelBuffer();
|
|
if (buf is null) return;
|
|
|
|
int zeroBasedIndex = index - 1;
|
|
if ((uint)zeroBasedIndex >= (uint)ctx.TotalChannels) return;
|
|
|
|
ctx.MarkPixelsDirty();
|
|
|
|
int pixelIndex = Math.DivRem(zeroBasedIndex, 4, out int channel);
|
|
double clamped = Math.Clamp(value, 0d, 255d);
|
|
|
|
fixed (byte* p = buf)
|
|
{
|
|
byte* pixel = p + pixelIndex * 4;
|
|
|
|
if (channel == 3)
|
|
{
|
|
double oldA = pixel[3];
|
|
double newA = clamped;
|
|
if (oldA > 0d && newA > 0d)
|
|
{
|
|
double factor = newA / oldA;
|
|
pixel[0] = (byte)Math.Clamp(pixel[0] * factor, 0d, 255d);
|
|
pixel[1] = (byte)Math.Clamp(pixel[1] * factor, 0d, 255d);
|
|
pixel[2] = (byte)Math.Clamp(pixel[2] * factor, 0d, 255d);
|
|
}
|
|
else if (newA <= 0d)
|
|
{
|
|
pixel[0] = pixel[1] = pixel[2] = 0;
|
|
}
|
|
pixel[3] = (byte)newA;
|
|
}
|
|
else
|
|
{
|
|
double a = pixel[3];
|
|
double premultiplied = clamped * (a / 255d);
|
|
int byteOffset = channel switch { 0 => 2, 1 => 1, 2 => 0, _ => throw new UnreachableException() };
|
|
pixel[byteOffset] = (byte)Math.Clamp(premultiplied, 0d, 255d);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|