我想用luajit录制我的屏幕的一小部分 .
除了http://luajit.org/ext_ffi.html之外,网上的Haven 't found any module for that. And there are barely any documentations/tutorials/examples about luajit' s ffi没有提供使用其他C库的任何示例 .
我有一个原生的C代码片段 . 你将如何实现luajit的ffi的C代码?
Luajit示例代码:
--ffi part
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; // Pixel x
int y=1080/2; // Pixel 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;
}
1 回答
基本上,您只需将标头中的所有声明复制到
ffi.cdef
部分,然后通过库的句柄调用这些名称 . 原则上,您可以将C代码一对一转换,但获取变量的地址除外 . 但是,这是在您链接的FFI教程中记录的(http://luajit.org/ext_ffi_tutorial.html#idioms)这是您在LuaJIT中的C代码 . 我没有复制
Display
和XImage
的定义,因为我们从不访问它们的成员,只使用它们的指针,因此它们仍然是不透明的struct
.