diff --git a/LuaScript.Tests/NativeWorkerRoundTripTests.cs b/LuaScript.Tests/NativeWorkerRoundTripTests.cs index 2585a71..4450d49 100644 --- a/LuaScript.Tests/NativeWorkerRoundTripTests.cs +++ b/LuaScript.Tests/NativeWorkerRoundTripTests.cs @@ -11,6 +11,7 @@ namespace LuaScript.Tests private static readonly Func NoResolver = (_, _) => null; private static readonly Func NoLoadFigure = (_, _, _, _, _) => ([], 1, 1); private static readonly Func NoLoadText = (_, _, _, _, _, _) => ([], 1, 1); + private static readonly Func NoLoadImage = _ => ([], 1, 1); private static readonly Action>> NoAddEffect = (_, _) => { }; private static readonly Action NoAddDraw = _ => { }; @@ -38,7 +39,7 @@ namespace LuaScript.Tests bool ok = _worker.Execute( "obj.rz = obj.time * 90\nobj.alpha = 128", - fields, pixels, 4, 4, 5000, NoResolver, NoLoadFigure, NoLoadText, NoAddEffect, NoAddDraw, out bool dirty, out _, out _, out _, out _, out string? error); + fields, pixels, 4, 4, 5000, NoResolver, NoLoadFigure, NoLoadText, NoLoadImage, NoAddEffect, NoAddDraw, out bool dirty, out _, out _, out _, out _, out string? error); Assert.True(ok, error); Assert.False(dirty); @@ -73,7 +74,7 @@ namespace LuaScript.Tests "local gray = r*0.299 + g*0.587 + b*0.114 " + "obj.setpixel(x,y,gray,gray,gray,a) end end"; - bool ok = _worker.Execute(script, fields, pixels, w, h, 5000, NoResolver, NoLoadFigure, NoLoadText, NoAddEffect, NoAddDraw, out bool dirty, out _, out _, out _, out _, out string? error); + bool ok = _worker.Execute(script, fields, pixels, w, h, 5000, NoResolver, NoLoadFigure, NoLoadText, NoLoadImage, NoAddEffect, NoAddDraw, out bool dirty, out _, out _, out _, out _, out string? error); Assert.True(ok, error); Assert.True(dirty); @@ -108,7 +109,7 @@ namespace LuaScript.Tests "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, NoResolver, NoLoadFigure, NoLoadText, NoAddEffect, NoAddDraw, out bool dirty, out _, out _, out _, out _, out string? error); + bool ok = _worker.Execute(script, fields, pixels, w, h, 5000, NoResolver, NoLoadFigure, NoLoadText, NoLoadImage, NoAddEffect, NoAddDraw, out bool dirty, out _, out _, out _, out _, out string? error); Assert.True(ok, error); Assert.True(dirty); @@ -144,7 +145,7 @@ namespace LuaScript.Tests for (int k = 1; k <= 5; k++) { var fields = Fields(2, 2, k); - bool ok = _worker.Execute("obj.x = obj.time * 10", fields, pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoLoadText, NoAddEffect, NoAddDraw, out _, out _, out _, out _, out _, out string? error); + bool ok = _worker.Execute("obj.x = obj.time * 10", fields, pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoLoadText, NoLoadImage, NoAddEffect, NoAddDraw, out _, out _, out _, out _, out _, out string? error); Assert.True(ok, error); Assert.Equal(k * 10d, fields[NativeProtocol.X]); } @@ -159,12 +160,12 @@ namespace LuaScript.Tests var fields = Fields(2, 2, 0d); bool timedOut = _worker.Execute( - "local x=0 while true do x=x+1 end", fields, pixels, 2, 2, 1500, NoResolver, NoLoadFigure, NoLoadText, NoAddEffect, NoAddDraw, out _, out _, out _, out _, out _, out string? error); + "local x=0 while true do x=x+1 end", fields, pixels, 2, 2, 1500, NoResolver, NoLoadFigure, NoLoadText, NoLoadImage, NoAddEffect, NoAddDraw, out _, out _, out _, out _, out _, out string? error); Assert.False(timedOut); Assert.Contains("timed out", error); var fields2 = Fields(2, 2, 3d); - bool ok = _worker.Execute("obj.x = obj.time", fields2, pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoLoadText, NoAddEffect, NoAddDraw, out _, out _, out _, out _, out _, out error); + bool ok = _worker.Execute("obj.x = obj.time", fields2, pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoLoadText, NoLoadImage, NoAddEffect, NoAddDraw, out _, out _, out _, out _, out _, out error); Assert.True(ok, error); Assert.Equal(3d, fields2[NativeProtocol.X]); } @@ -186,7 +187,7 @@ namespace LuaScript.Tests "obj.zoom = o.zoom obj.rz = o.rz obj.alpha = o.alpha " + "obj.sx = o.sy obj.rzr = o.rzr"; - bool ok = _worker.Execute(script, fields, pixels, 2, 2, 5000, resolver, NoLoadFigure, NoLoadText, NoAddEffect, NoAddDraw, out _, out _, out _, out _, out _, out string? error); + bool ok = _worker.Execute(script, fields, pixels, 2, 2, 5000, resolver, NoLoadFigure, NoLoadText, NoLoadImage, NoAddEffect, NoAddDraw, out _, out _, out _, out _, out _, out string? error); Assert.True(ok, error); Assert.Equal(12d, fields[NativeProtocol.X]); @@ -218,13 +219,13 @@ namespace LuaScript.Tests "local b = obj.getobject(\"a\") " + "obj.x = a.x + b.x"; - bool ok = _worker.Execute(script, Fields(2, 2, 0d), pixels, 2, 2, 5000, resolver, NoLoadFigure, NoLoadText, NoAddEffect, NoAddDraw, out _, out _, out _, out _, out _, out string? error); + bool ok = _worker.Execute(script, Fields(2, 2, 0d), pixels, 2, 2, 5000, resolver, NoLoadFigure, NoLoadText, NoLoadImage, NoAddEffect, NoAddDraw, out _, out _, out _, out _, out _, out string? error); Assert.True(ok, error); Assert.Equal(1, calls); var fields = Fields(2, 2, 0d); x = 20d; - ok = _worker.Execute(script, fields, pixels, 2, 2, 5000, resolver, NoLoadFigure, NoLoadText, NoAddEffect, NoAddDraw, out _, out _, out _, out _, out _, out error); + ok = _worker.Execute(script, fields, pixels, 2, 2, 5000, resolver, NoLoadFigure, NoLoadText, NoLoadImage, NoAddEffect, NoAddDraw, out _, out _, out _, out _, out _, out error); Assert.True(ok, error); Assert.Equal(2, calls); Assert.Equal(40d, fields[NativeProtocol.X]); @@ -241,7 +242,7 @@ namespace LuaScript.Tests const string script = "local o = obj.getobject(\"missing\") obj.x = o == nil and 1 or 0"; - bool ok = _worker.Execute(script, fields, pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoLoadText, NoAddEffect, NoAddDraw, out _, out _, out _, out _, out _, out string? error); + bool ok = _worker.Execute(script, fields, pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoLoadText, NoLoadImage, NoAddEffect, NoAddDraw, out _, out _, out _, out _, out _, out string? error); Assert.True(ok, error); Assert.Equal(1d, fields[NativeProtocol.X]); @@ -259,7 +260,7 @@ namespace LuaScript.Tests bool ok = _worker.Execute( "obj.draw() obj.draw(10, 20, 30, 2, 0.5, 0.25)", - fields, pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoLoadText, NoAddEffect, addDraw, + fields, pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoLoadText, NoLoadImage, NoAddEffect, addDraw, out _, out _, out _, out _, out _, out string? error); Assert.True(ok, error); @@ -280,7 +281,7 @@ namespace LuaScript.Tests bool ok = _worker.Execute( "obj.drawpoly(0,0,0, 10,0,0, 10,10,0, 0,10,0)", - fields, pixels, 4, 4, 5000, NoResolver, NoLoadFigure, NoLoadText, NoAddEffect, addDraw, + fields, pixels, 4, 4, 5000, NoResolver, NoLoadFigure, NoLoadText, NoLoadImage, NoAddEffect, addDraw, out _, out _, out _, out _, out _, out string? error); Assert.True(ok, error); @@ -302,7 +303,7 @@ namespace LuaScript.Tests bool ok = _worker.Execute( "obj.drawpoly(0,0,0, 8,0,0, 8,8,0, 0,8,0, 1,1, 3,1, 3,3, 1,3, 0.5)", - fields, pixels, 4, 4, 5000, NoResolver, NoLoadFigure, NoLoadText, NoAddEffect, addDraw, + fields, pixels, 4, 4, 5000, NoResolver, NoLoadFigure, NoLoadText, NoLoadImage, NoAddEffect, addDraw, out _, out _, out _, out _, out _, out string? error); Assert.True(ok, error); @@ -323,7 +324,7 @@ namespace LuaScript.Tests bool ok = _worker.Execute( "obj.setoption('antialias', 0) obj.x = obj.getoption('antialias') obj.y = obj.getoption('unset')", - fields, pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoLoadText, NoAddEffect, NoAddDraw, + fields, pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoLoadText, NoLoadImage, NoAddEffect, NoAddDraw, out _, out _, out _, out _, out _, out string? error); Assert.True(ok, error); @@ -342,7 +343,7 @@ namespace LuaScript.Tests bool ok = _worker.Execute( "obj.draw() obj.setoption('antialias', 0) obj.draw()", - Fields(2, 2, 0d), pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoLoadText, NoAddEffect, addDraw, + Fields(2, 2, 0d), pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoLoadText, NoLoadImage, NoAddEffect, addDraw, out _, out _, out _, out _, out _, out string? error); Assert.True(ok, error); @@ -379,7 +380,7 @@ namespace LuaScript.Tests bool ok = _worker.Execute( "obj.setfont('Meiryo', 40, 3, 0x112233) obj.load('text', 'Hi')", - fields, pixels, 4, 4, 5000, NoResolver, NoLoadFigure, loadText, NoAddEffect, NoAddDraw, + fields, pixels, 4, 4, 5000, NoResolver, NoLoadFigure, loadText, NoLoadImage, NoAddEffect, NoAddDraw, out bool dirty, out bool bufferReplaced, out byte[]? newPixels, out int rw, out int rh, out string? error); Assert.True(ok, error); @@ -397,6 +398,41 @@ namespace LuaScript.Tests Assert.Equal(rendered, newPixels!.AsSpan(0, rendered.Length).ToArray()); } + [Fact] + public void LoadImage_RoundTripsThroughCallback() + { + 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 * 23 + 9) & 0xFF); + + string? capturedPath = null; + Func loadImage = path => + { + capturedPath = path; + return (decoded, iw, ih); + }; + + var pixels = new byte[4 * 4 * 4]; + var fields = Fields(4, 4, 0d); + + bool ok = _worker.Execute( + "obj.load('image', 'C:/sample.png')", + fields, pixels, 4, 4, 5000, NoResolver, NoLoadFigure, NoLoadText, loadImage, NoAddEffect, NoAddDraw, + out bool dirty, out bool bufferReplaced, out byte[]? newPixels, out int rw, out int rh, out string? error); + + Assert.True(ok, error); + Assert.Equal("C:/sample.png", capturedPath); + Assert.True(dirty); + Assert.True(bufferReplaced); + Assert.Equal(iw, rw); + Assert.Equal(ih, rh); + Assert.NotNull(newPixels); + Assert.Equal(decoded, newPixels!.AsSpan(0, decoded.Length).ToArray()); + } + [Fact] public void GetValue_ReturnsObjFields() { @@ -408,7 +444,7 @@ namespace LuaScript.Tests bool ok = _worker.Execute( "obj.x = obj.getvalue('track0') obj.y = obj.getvalue('alpha') obj.z = obj.getvalue('unknown')", - fields, pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoLoadText, NoAddEffect, NoAddDraw, + fields, pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoLoadText, NoLoadImage, NoAddEffect, NoAddDraw, out _, out _, out _, out _, out _, out string? error); Assert.True(ok, error); @@ -435,7 +471,7 @@ namespace LuaScript.Tests "for y=0,obj.h-1 do for x=0,obj.w-1 do obj.setpixel(x,y,0,0,0,0) end end " + "obj.copybuffer('obj','tmp')"; - bool ok = _worker.Execute(script, fields, pixels, w, h, 5000, NoResolver, NoLoadFigure, NoLoadText, NoAddEffect, NoAddDraw, out bool dirty, out _, out _, out _, out _, out string? error); + bool ok = _worker.Execute(script, fields, pixels, w, h, 5000, NoResolver, NoLoadFigure, NoLoadText, NoLoadImage, NoAddEffect, NoAddDraw, out bool dirty, out _, out _, out _, out _, out string? error); Assert.True(ok, error); Assert.True(dirty); @@ -454,11 +490,11 @@ namespace LuaScript.Tests stored[3] = 255; stored[7] = 255; stored[11] = 255; stored[15] = 255; var save = (byte[])stored.Clone(); - bool ok = _worker.Execute("obj.copybuffer('cache:foo','obj')", Fields(w, h, 0d), save, w, h, 5000, NoResolver, NoLoadFigure, NoLoadText, NoAddEffect, NoAddDraw, out _, out _, out _, out _, out _, out string? error); + bool ok = _worker.Execute("obj.copybuffer('cache:foo','obj')", Fields(w, h, 0d), save, w, h, 5000, NoResolver, NoLoadFigure, NoLoadText, NoLoadImage, NoAddEffect, NoAddDraw, out _, out _, out _, out _, out _, out string? error); Assert.True(ok, error); var blank = new byte[w * h * 4]; - ok = _worker.Execute("obj.copybuffer('obj','cache:foo')", Fields(w, h, 0d), blank, w, h, 5000, NoResolver, NoLoadFigure, NoLoadText, NoAddEffect, NoAddDraw, out bool dirty, out _, out _, out _, out _, out error); + ok = _worker.Execute("obj.copybuffer('obj','cache:foo')", Fields(w, h, 0d), blank, w, h, 5000, NoResolver, NoLoadFigure, NoLoadText, NoLoadImage, NoAddEffect, NoAddDraw, out bool dirty, out _, out _, out _, out _, out error); Assert.True(ok, error); Assert.True(dirty); Assert.Equal(stored, blank); @@ -472,12 +508,12 @@ namespace LuaScript.Tests var pixels = new byte[16]; var fields = Fields(2, 2, 0d); - bool ok = _worker.Execute("obj.x = missing.value", fields, pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoLoadText, NoAddEffect, NoAddDraw, out _, out _, out _, out _, out _, out string? error); + bool ok = _worker.Execute("obj.x = missing.value", fields, pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoLoadText, NoLoadImage, NoAddEffect, NoAddDraw, out _, out _, out _, out _, out _, out string? error); Assert.False(ok); Assert.False(string.IsNullOrEmpty(error)); var fields2 = Fields(2, 2, 7d); - bool ok2 = _worker.Execute("obj.x = obj.time", fields2, pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoLoadText, NoAddEffect, NoAddDraw, out _, out _, out _, out _, out _, out error); + bool ok2 = _worker.Execute("obj.x = obj.time", fields2, pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoLoadText, NoLoadImage, NoAddEffect, NoAddDraw, out _, out _, out _, out _, out _, out error); Assert.True(ok2, error); Assert.Equal(7d, fields2[NativeProtocol.X]); } diff --git a/LuaScript/Engine/LuaJitWorker.cs b/LuaScript/Engine/LuaJitWorker.cs index 67207cf..bb009c6 100644 --- a/LuaScript/Engine/LuaJitWorker.cs +++ b/LuaScript/Engine/LuaJitWorker.cs @@ -50,6 +50,7 @@ namespace LuaScript.Engine Func resolveObject, Func loadFigure, Func loadText, + Func loadImage, Action>> addEffect, Action addDraw, out bool pixelsDirty, @@ -105,7 +106,7 @@ namespace LuaScript.Engine if (status != NativeProtocol.StatusCallback) break; - DispatchCallback(view, resolveObject, loadFigure, loadText, addEffect, addDraw); + DispatchCallback(view, resolveObject, loadFigure, loadText, loadImage, addEffect, addDraw); _workEvent.Set(); } @@ -150,6 +151,7 @@ namespace LuaScript.Engine Func resolveObject, Func loadFigure, Func loadText, + Func loadImage, Action>> addEffect, Action addDraw) { @@ -165,6 +167,9 @@ namespace LuaScript.Engine case NativeProtocol.CbKindLoadText: ResolveLoadTextCallback(view, loadText); break; + case NativeProtocol.CbKindLoadImage: + ResolveLoadImageCallback(view, loadImage); + break; case NativeProtocol.CbKindEffect: ResolveEffectCallback(view, addEffect); break; @@ -302,6 +307,32 @@ namespace LuaScript.Engine view.Write(NativeProtocol.OffCallbackFound, 1); } + private void ResolveLoadImageCallback( + MemoryMappedViewAccessor view, + Func loadImage) + { + int tagLen = Math.Clamp(view.ReadInt32(NativeProtocol.OffCallbackTagLen), 0, NativeProtocol.CallbackTagMax); + view.ReadArray(NativeProtocol.CallbackTagOffset, _callbackTag, 0, tagLen); + string path = Encoding.UTF8.GetString(_callbackTag, 0, tagLen); + + (byte[] buffer, int w, int h) result; + try { result = loadImage(path); } + catch { result = (new byte[4], 1, 1); } + + int pixelSize = result.w * result.h * 4; + long capacity = view.Capacity - NativeProtocol.PixelOffset; + if (pixelSize > capacity) + { + result = (new byte[4], 1, 1); + pixelSize = 4; + } + + view.Write(NativeProtocol.OffLoadResultWidth, result.w); + view.Write(NativeProtocol.OffLoadResultHeight, result.h); + view.WriteArray(NativeProtocol.PixelOffset, result.buffer, 0, pixelSize); + view.Write(NativeProtocol.OffCallbackFound, 1); + } + private void ResolveEffectCallback(MemoryMappedViewAccessor view, Action>> addEffect) { int tagLen = Math.Clamp(view.ReadInt32(NativeProtocol.OffCallbackTagLen), 0, NativeProtocol.CallbackTagMax); diff --git a/LuaScript/Engine/NativeProtocol.cs b/LuaScript/Engine/NativeProtocol.cs index 78f478b..02a690d 100644 --- a/LuaScript/Engine/NativeProtocol.cs +++ b/LuaScript/Engine/NativeProtocol.cs @@ -49,6 +49,7 @@ namespace LuaScript.Engine public const int CbKindDraw = 3; public const int CbKindDrawPoly = 4; public const int CbKindLoadText = 5; + public const int CbKindLoadImage = 6; public const int CbExist = 0; public const int CbX = 1; diff --git a/LuaScript/ImageDecoder.cs b/LuaScript/ImageDecoder.cs new file mode 100644 index 0000000..526e97d --- /dev/null +++ b/LuaScript/ImageDecoder.cs @@ -0,0 +1,97 @@ +using System; +using System.IO; +using Vortice.Mathematics; +using Vortice.WIC; + +namespace LuaScript +{ + internal sealed class ImageDecoder : IDisposable + { + private const int MaxDimension = 8192; + + private IWICImagingFactory? _factory; + + public unsafe byte[] Decode(string path, out int width, out int height) + { + width = 1; + height = 1; + if (string.IsNullOrEmpty(path) || !IsSupportedImage(path)) + return new byte[4]; + + try + { + _factory ??= new IWICImagingFactory(); + using var stream = _factory.CreateStream(path, FileAccess.Read); + using var decoder = _factory.CreateDecoderFromStream(stream, DecodeOptions.CacheOnDemand); + using var frame = decoder.GetFrame(0); + using var converter = _factory.CreateFormatConverter(); + converter.Initialize( + frame, + Vortice.WIC.PixelFormat.Format32bppPBGRA, + BitmapDitherType.None, + null, + 0d, + BitmapPaletteType.Custom); + + converter.GetSize(out int w, out int h); + if (w <= 0 || h <= 0 || w > MaxDimension || h > MaxDimension) + return new byte[4]; + + var buffer = new byte[w * h * 4]; + fixed (byte* p = buffer) + converter.CopyPixels(new RectI(0, 0, w, h), w * 4, buffer.Length, (nint)p); + + width = w; + height = h; + return buffer; + } + catch + { + width = 1; + height = 1; + return new byte[4]; + } + } + + private static bool IsSupportedImage(string path) + { + try + { + if (!File.Exists(path)) + return false; + Span head = stackalloc byte[12]; + using var stream = File.OpenRead(path); + int read = stream.Read(head); + if (read < 4) + return false; + + if (head[0] == 0x89 && head[1] == 0x50 && head[2] == 0x4E && head[3] == 0x47) + return true; + if (head[0] == 0xFF && head[1] == 0xD8 && head[2] == 0xFF) + return true; + if (head[0] == 0x47 && head[1] == 0x49 && head[2] == 0x46 && head[3] == 0x38) + return true; + if (head[0] == 0x42 && head[1] == 0x4D) + return true; + if ((head[0] == 0x49 && head[1] == 0x49 && head[2] == 0x2A && head[3] == 0x00) || + (head[0] == 0x4D && head[1] == 0x4D && head[2] == 0x00 && head[3] == 0x2A)) + return true; + if (read >= 12 && + head[0] == 0x52 && head[1] == 0x49 && head[2] == 0x46 && head[3] == 0x46 && + head[8] == 0x57 && head[9] == 0x45 && head[10] == 0x42 && head[11] == 0x50) + return true; + return false; + } + catch + { + return false; + } + } + + public void Dispose() + { + _factory?.Dispose(); + _factory = null; + } + } +} diff --git a/LuaScript/LuaScriptEffectProcessor.cs b/LuaScript/LuaScriptEffectProcessor.cs index aeaa5fc..627b59a 100644 --- a/LuaScript/LuaScriptEffectProcessor.cs +++ b/LuaScript/LuaScriptEffectProcessor.cs @@ -90,6 +90,7 @@ namespace LuaScript private VideoEffectChain? _effectChain; private DrawCompositor? _drawCompositor; private TextRenderer? _nativeTextRenderer; + private ImageDecoder? _nativeImageDecoder; protected override ID2D1Image? CreateEffect(IGraphicsDevicesAndContext devices) { @@ -448,6 +449,7 @@ namespace LuaScript (tag, frame) => ctx.ResolveObject(tag, frame, out var info) ? info : null, NativeLoadFigure, NativeLoadText, + NativeLoadImage, (name, args) => ctx.AddEffect(new AviUtlEffectRequest(name, args)), ctx.AddDraw, out bool dirty, out bool bufferReplaced, out byte[]? newPixels, @@ -507,6 +509,13 @@ namespace LuaScript return (buffer, w, h); } + private (byte[] buffer, int w, int h) NativeLoadImage(string path) + { + _nativeImageDecoder ??= new ImageDecoder(); + var buffer = _nativeImageDecoder.Decode(path, out int w, out int h); + return (buffer, w, h); + } + private static (byte[] buffer, int w, int h) NativeLoadFigure(string name, int color, double size, double lineWidth, double aspect) { aspect = Math.Clamp(aspect, -1d, 1d); @@ -541,6 +550,7 @@ namespace LuaScript _effectChain?.Dispose(); _drawCompositor?.Dispose(); _nativeTextRenderer?.Dispose(); + _nativeImageDecoder?.Dispose(); _pixelLoaderSemaphore.Dispose(); _pixelManager?.Dispose(); _pixelManager = null; diff --git a/LuaScript/LuaScriptEngine.cs b/LuaScript/LuaScriptEngine.cs index 76c7ae8..ac3296b 100644 --- a/LuaScript/LuaScriptEngine.cs +++ b/LuaScript/LuaScriptEngine.cs @@ -57,6 +57,7 @@ namespace LuaScript private readonly Dictionary _pixelOptions = new(StringComparer.Ordinal); private TextRenderer? _textRenderer; + private ImageDecoder? _imageDecoder; private string _fontFamily = string.Empty; private double _fontSize = 34d; private bool _fontBold; @@ -507,6 +508,9 @@ namespace LuaScript case "text": LoadText(args.Count > 1 && args[1].Type == DataType.String ? args[1].String : string.Empty); break; + case "image" when args.Count > 1 && args[1].Type == DataType.String: + LoadImage(args[1].String); + break; } return DynValue.Void; }); @@ -688,6 +692,14 @@ namespace LuaScript RefreshObjDimensions(); } + private void LoadImage(string path) + { + _imageDecoder ??= new ImageDecoder(); + var buffer = _imageDecoder.Decode(path, out int w, out int h); + _activeContext!.ReplaceBuffer(buffer, w, h); + RefreshObjDimensions(); + } + private void RefreshObjDimensions() { int w = _activeContext!.ImageWidth; @@ -757,6 +769,7 @@ namespace LuaScript _workSignal.Release(); _thread.Join(); _textRenderer?.Dispose(); + _imageDecoder?.Dispose(); _cts.Cancel(); _cts.Dispose(); _workSignal.Dispose(); @@ -771,6 +784,7 @@ namespace LuaScript { _thread.Join(); _textRenderer?.Dispose(); + _imageDecoder?.Dispose(); _cts.Dispose(); _workSignal.Dispose(); _doneSignal.Dispose(); diff --git a/native/worker.lua b/native/worker.lua index c2bd0e2..6bc04a5 100644 --- a/native/worker.lua +++ b/native/worker.lua @@ -64,6 +64,7 @@ local CB_KIND_EFFECT = 2 local CB_KIND_DRAW = 3 local CB_KIND_DRAWPOLY = 4 local CB_KIND_LOADTEXT = 5 +local CB_KIND_LOADIMAGE = 6 assert(loadfile(shimPath))() @@ -239,11 +240,26 @@ local function loadText(str) applyLoadResult() end +local function loadImage(path) + local payload = tostring(path or "") + local len = #payload + if len > CB_TAG_MAX then len = CB_TAG_MAX end + ffi.copy(base + CB_TAG_OFFSET, payload, len) + i32[OFF_CB_TAGLEN] = len + i32[OFF_CB_KIND] = CB_KIND_LOADIMAGE + i32[OFF_STATUS] = STATUS_CALLBACK + k32.SetEvent(doneEvent) + k32.WaitForSingleObject(workEvent, INFINITE) + applyLoadResult() +end + function obj.load(kind, ...) if kind == "figure" then loadFigure(...) elseif kind == "text" then loadText(...) + elseif kind == "image" then + loadImage(...) end end