From 95786a1f0a91409d07bdad93da1dc56bfa08aef3 Mon Sep 17 00:00:00 2001 From: routersys Date: Thu, 2 Jul 2026 19:03:23 +0900 Subject: [PATCH] =?UTF-8?q?getpixeldata=E3=81=AE=E9=81=85=E5=BB=B6?= =?UTF-8?q?=E6=9B=B8=E3=81=8D=E8=BE=BC=E3=81=BF=E3=82=92=E3=83=95=E3=83=AC?= =?UTF-8?q?=E3=83=BC=E3=83=A0=E7=BD=AE=E6=8F=9B=E6=99=82=E3=81=AB=E7=A0=B4?= =?UTF-8?q?=E6=A3=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LuaScript.Tests/NativeWorkerRoundTripTests.cs | 58 +++++++++++++++++++ native/worker.lua | 2 + 2 files changed, 60 insertions(+) diff --git a/LuaScript.Tests/NativeWorkerRoundTripTests.cs b/LuaScript.Tests/NativeWorkerRoundTripTests.cs index 72a0d84..013efe0 100644 --- a/LuaScript.Tests/NativeWorkerRoundTripTests.cs +++ b/LuaScript.Tests/NativeWorkerRoundTripTests.cs @@ -231,6 +231,64 @@ namespace LuaScript.Tests Assert.Equal(255d, fields[NativeProtocol.Alpha]); } + [Fact] + public void PixelData_StaleWritesDoNotResurrect_AfterLoad() + { + Assert.True(LuaJitWorker.IsAvailable(NativeDir), "native/luajit.exe must be present"); + + const int iw = 2, ih = 2; + var decoded = new byte[iw * ih * 4]; + for (int i = 0; i < decoded.Length; i++) + decoded[i] = (byte)((i * 19 + 7) & 0xFF); + Func loadImage = _ => (decoded, iw, ih); + + var pixels = new byte[4 * 4 * 4]; + for (int i = 3; i < pixels.Length; i += 4) + pixels[i] = 255; + + const string script = + "local pd = obj.getpixeldata() " + + "pd:set(1, 11) pd:set(2, 22) pd:set(3, 33) " + + "obj.load('image', 'p')"; + + bool ok = _worker.Execute(script, Fields(4, 4, 0d), NoStringParams, () => pixels, 4, 4, 5000, NoResolver, NoLoadFigure, NoLoadText, loadImage, NoLoadMovie, NoAddEffect, NoAddDraw, NoSetAnchor, + out bool dirty, out bool bufferReplaced, out byte[]? result, out int rw, out int rh, out string? error); + + Assert.True(ok, error); + Assert.True(dirty); + Assert.True(bufferReplaced); + Assert.Equal(iw, rw); + Assert.Equal(ih, rh); + Assert.NotNull(result); + Assert.Equal(decoded, result!.AsSpan(0, decoded.Length).ToArray()); + } + + [Fact] + public void PixelData_StaleWritesDoNotResurrect_AfterCopyBuffer() + { + Assert.True(LuaJitWorker.IsAvailable(NativeDir), "native/luajit.exe must be present"); + + const int w = 2, h = 2; + var pixels = new byte[w * h * 4]; + for (int i = 0; i < pixels.Length; i++) + pixels[i] = (byte)((i * 29 + 3) & 0xFF); + pixels[3] = 255; pixels[7] = 255; pixels[11] = 255; pixels[15] = 255; + var original = (byte[])pixels.Clone(); + + const string script = + "obj.copybuffer('tmp','obj') " + + "local pd = obj.getpixeldata() " + + "pd:set(1, 99) pd:set(2, 98) pd:set(3, 97) " + + "obj.copybuffer('obj','tmp')"; + + bool ok = _worker.Execute(script, Fields(w, h, 0d), NoStringParams, () => pixels, w, h, 5000, NoResolver, NoLoadFigure, NoLoadText, NoLoadImage, NoLoadMovie, NoAddEffect, NoAddDraw, NoSetAnchor, + out bool dirty, out _, out _, out _, out _, out string? error); + + Assert.True(ok, error); + Assert.True(dirty); + Assert.Equal(original, pixels); + } + [Fact] public void PersistentWorker_HandlesMultipleCalls() { diff --git a/native/worker.lua b/native/worker.lua index 54d409a..d155c5c 100644 --- a/native/worker.lua +++ b/native/worker.lua @@ -387,6 +387,7 @@ local function applyLoadResult() dirty = true pixelsValid = true pdValid = false + pdDirty = false obj.w = newW; obj.h = newH obj.hw = newW / 2; obj.hh = newH / 2 obj.cx = newW / 2; obj.cy = newH / 2 @@ -661,6 +662,7 @@ function obj.copybuffer(dst, src) dirty = true pixelsValid = true pdValid = false + pdDirty = false obj.w = w; obj.h = h obj.hw = w / 2; obj.hh = h / 2 obj.cx = w / 2; obj.cy = h / 2