如何在 luajit 的 ffi 中实现 X11(返回屏幕像素颜色)C 代码?
2018-12-9 2:3:11
收藏:0
阅读:135
评论:1
我想用 luajit 录制我的屏幕的一个小部分。
没有找到任何可以用的模块。除了 http://luajit.org/ext_ffi.html 之外,几乎没有任何关于 luajit's ffi 的文档/教程/示例,且该链接没有提供使用其他 C 库的示例。
我有一个原生的 C 代码片段可以工作。您如何在 luajit 的 ffi 中实现 C 代码?
Luajit 示例代码:
--ffi 部分
local screen = {}
for y = 1, 100 do
for x = 1, 100 do
local r, g, b = ffi.C.getpixel(x, y)
table.insert(screen, r)
end
end
C 代码片段:
#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
int main()
{
XColor c;
Display *d = XOpenDisplay((char *) NULL);
int x=1920/2; // 像素 x
int y=1080/2; // 像素 y
XImage *image;
image = XGetImage (d, XRootWindow (d, XDefaultScreen (d)), x, y, 1, 1, AllPlanes, XYPixmap);
c.pixel = XGetPixel (image, 0, 0);
XFree (image);
XQueryColor (d, XDefaultColormap(d, XDefaultScreen (d)), &c);
printf("%d %d %d\n", c.red/256, c.green/256, c.blue/256);
return 0;
}
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?

基本上您只需要将头文件中的所有声明复制到
ffi.cdef部分中,然后通过库句柄调用这些名称。原则上您可以一对一地将C代码翻译为Lua代码,但需要注意取变量地址的情况。然而,这在您链接的FFI教程中有详细说明(http://luajit.org/ext_ffi_tutorial.html#idioms)C代码 Lua代码 带有输出参数的函数 int len = x; local len = ffi.new("int[1]", x) void foo(int *inoutlen); foo(&len); foo(len) y = len; y = len[0]这是您的C代码在LuaJIT中的表示。我没有复制
Display和XImage的定义,因为我们从未访问它们的成员,只是使用它们的指针,因此它们保持为不透明的structs。local ffi = assert(require("ffi")) ffi.cdef[[ //来自多个头文件的类型 typedef struct _Display Display; typedef struct _XImage XImage; typedef struct { unsigned long pixel; unsigned short red, green, blue; char flags; /* do_red, do_green, do_blue */ char pad; } XColor; // Xlib.h typedef unsigned long XID; // Xdefs.h typedef XID Window; // X.h typedef XID Drawable; // X.h typedef XID Colormap; // X.h //来自Xlib.h的函数 Display *XOpenDisplay( char* /* display_name */ ); int XDefaultScreen( Display* /* display */ ); Window XRootWindow( Display* /* display */, int /* screen_number */ ); XImage *XGetImage( Display* /* display */, Drawable /* d */, int /* x */, int /* y */, unsigned int /* width */, unsigned int /* height */, unsigned long /* plane_mask */, int /* format */ ); int XFree( void* /* data */ ); int XQueryColor( Display* /* display */, Colormap /* colormap */, XColor* /* def_in_out */ ); Colormap XDefaultColormap( Display* /* display */, int /* screen_number */ ); //来自Xutil.h的函数 unsigned long XGetPixel( XImage *ximage, int x, int y); ]] local X11 = assert(ffi.load("X11")) local AllPlanes = -1 -- Xlib.h: #define AllPlanes ((unsigned long)~0L) local XYPixmap = 1 -- X.h: #define XYPixmap 1 local c = ffi.new("XColor[1]") local d = X11.XOpenDisplay(ffi.NULL) local x = 1920 / 2 local y = 1080 / 2 local image = X11.XGetImage(d, X11.XRootWindow(d, X11.XDefaultScreen(d)), x, y, 1, 1, AllPlanes, XYPixmap) c[0].pixel = X11.XGetPixel(image, 0, 0) X11.XFree(image) X11.XQueryColor(d, X11.XDefaultColormap(d, X11.XDefaultScreen(d)), c) print(string.format("%d %d %d", c[0].red/256, c[0].green/256, c[0].blue/256))