From d22035111a3270e087535bdfe740e4cf57159cbf Mon Sep 17 00:00:00 2001 From: routersys Date: Sat, 27 Jun 2026 09:33:34 +0900 Subject: [PATCH] =?UTF-8?q?=E5=85=B1=E6=9C=89=E3=83=A1=E3=83=A2=E3=83=AABG?= =?UTF-8?q?RA=E5=87=A6=E7=90=86=E3=83=AF=E3=83=BC=E3=82=AB=E3=83=BC(PoC)?= =?UTF-8?q?=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/native/worker_grayscale.lua | 42 +++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 LuaScript.Tests/native/worker_grayscale.lua diff --git a/LuaScript.Tests/native/worker_grayscale.lua b/LuaScript.Tests/native/worker_grayscale.lua new file mode 100644 index 0000000..72e5d56 --- /dev/null +++ b/LuaScript.Tests/native/worker_grayscale.lua @@ -0,0 +1,42 @@ +local ffi = require("ffi") + +ffi.cdef [[ +typedef void* HANDLE; +HANDLE OpenFileMappingA(unsigned long dwDesiredAccess, int bInheritHandle, const char* lpName); +void* MapViewOfFile(HANDLE hFileMappingObject, unsigned long dwDesiredAccess, unsigned long dwFileOffsetHigh, unsigned long dwFileOffsetLow, size_t dwNumberOfBytesToMap); +int UnmapViewOfFile(const void* lpBaseAddress); +int CloseHandle(HANDLE hObject); +unsigned long GetLastError(); +]] + +local k32 = ffi.load("kernel32") +local FILE_MAP_ALL_ACCESS = 0x000F001F + +local name = arg[1] +local size = tonumber(arg[2]) + +local h = k32.OpenFileMappingA(FILE_MAP_ALL_ACCESS, 0, name) +if h == nil then error("OpenFileMapping failed: " .. tonumber(k32.GetLastError())) end + +local p = k32.MapViewOfFile(h, FILE_MAP_ALL_ACCESS, 0, 0, size) +if p == nil then + k32.CloseHandle(h) + error("MapViewOfFile failed: " .. tonumber(k32.GetLastError())) +end + +local buf = ffi.cast("uint8_t*", p) +local pixels = size / 4 +for i = 0, pixels - 1 do + local b = buf[i * 4] + local g = buf[i * 4 + 1] + local r = buf[i * 4 + 2] + local gray = math.floor(r * 0.299 + g * 0.587 + b * 0.114 + 0.5) + if gray > 255 then gray = 255 end + buf[i * 4] = gray + buf[i * 4 + 1] = gray + buf[i * 4 + 2] = gray +end + +k32.UnmapViewOfFile(p) +k32.CloseHandle(h) +io.write("done")