From 7edbe189a25134a2e4f1c35a5c13e9f6a90dcfbd Mon Sep 17 00:00:00 2001 From: routersys Date: Sat, 27 Jun 2026 12:23:08 +0900 Subject: [PATCH] =?UTF-8?q?getpixeldata=E5=BE=80=E5=BE=A9=E3=83=86?= =?UTF-8?q?=E3=82=B9=E3=83=88=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LuaScript.Tests/NativeWorkerRoundTripTests.cs | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/LuaScript.Tests/NativeWorkerRoundTripTests.cs b/LuaScript.Tests/NativeWorkerRoundTripTests.cs index 979ddd5..3a10e27 100644 --- a/LuaScript.Tests/NativeWorkerRoundTripTests.cs +++ b/LuaScript.Tests/NativeWorkerRoundTripTests.cs @@ -74,6 +74,41 @@ namespace LuaScript.Tests Assert.Equal(expected, pixels); } + [Fact] + public void PixelDataProxy_MatchesExpectedGrayscale() + { + Assert.True(LuaJitWorker.IsAvailable(NativeDir), "native/luajit.exe must be present"); + + const int w = 2, h = 2; + var pixels = new byte[w * h * 4]; + void SetBgra(int i, byte b, byte g, byte r, byte a) + { + pixels[i * 4 + 0] = b; pixels[i * 4 + 1] = g; pixels[i * 4 + 2] = r; pixels[i * 4 + 3] = a; + } + SetBgra(0, 10, 20, 30, 255); + SetBgra(1, 0, 0, 0, 0); + SetBgra(2, 50, 100, 200, 128); + SetBgra(3, 255, 255, 255, 255); + + var expected = (byte[])pixels.Clone(); + for (int i = 0; i < w * h; i++) + Grayscale(expected, i); + + var fields = Fields(w, h, 0d); + const string script = + "local pd = obj.getpixeldata() local w,h = pd.width, pd.height " + + "for y=0,h-1 do for x=0,w-1 do local base=(y*w+x)*4 " + + "local r=pd:get(base+1) local g=pd:get(base+2) local b=pd:get(base+3) " + + "local gray=r*0.299+g*0.587+b*0.114 " + + "pd:set(base+1,gray) pd:set(base+2,gray) pd:set(base+3,gray) end end"; + + bool ok = _worker.Execute(script, fields, pixels, w, h, 5000, out bool dirty, out string? error); + + Assert.True(ok, error); + Assert.True(dirty); + Assert.Equal(expected, pixels); + } + private static void Grayscale(byte[] px, int i) { double b = px[i * 4 + 0], g = px[i * 4 + 1], r = px[i * 4 + 2], a = px[i * 4 + 3];