XRenderCreatePicture 将像素图(或任何可绘制对象)作为参数。为了使用 xrender 绘制像素,您需要:
- 创建像素图
- 使用 XPutImage 将图像数据传输到服务器
- 创建与您的像素图关联的图片对象
- 创建与您的窗口关联的图片对象
- 使用 XRenderComposite 组合它们
在此处查看 libXRender 文档
使用node-x11 的示例:
var x11 = require('x11');
var Exposure = x11.eventMask.Exposure;
var width = 300;
var height = 300;
var rgb = {
data:new Buffer(width*height*4),
width: width,
height: height
};
var index;
for (var x = 0; x < rgb.width; ++x)
{
for (var y = 0; y < rgb.height; ++y)
{
index = (x + y*rgb.width)*4;
rgb.data[index] = parseInt(Math.sin(13*x/rgb.width)*255);
rgb.data[index+1] = parseInt(Math.cos(15*y/rgb.height)*255);
rgb.data[index+2] = parseInt(Math.cos(16*y/rgb.height)*255);
}
}
x11.createClient(function(err, display)
{
var X = display.client;
X.require('render', function(Render) {
var root = display.screen[0].root;
var win, picWin, pic, gc;
win = X.AllocID();
X.CreateWindow(
win, root,
0, 0, rgb.width, rgb.height,
0, 0, 0, 0,
{ eventMask: Exposure }
);
X.MapWindow(win);
gc = X.AllocID();
X.CreateGC(gc, win);
var rgbPixmap = X.AllocID();
X.CreatePixmap(rgbPixmap, win, 24, rgb.width, rgb.height);
X.PutImage(2, rgbPixmap, gc, rgb.width, rgb.height, 0, 0, 0, 24, rgb.data);
var rgbPicture = X.AllocID();
Render.CreatePicture(rgbPicture, rgbPixmap, Render.rgb24);
var winPicture = X.AllocID();
Render.CreatePicture(winPicture, win, Render.rgb24);
X.on('event', function(ev) {
if (ev.name == 'Expose')
Render.Composite(3, rgbPicture, 0, winPicture, 0, 0, 0, 0, 0, 0, rgb.width, rgb.height);
});
});
});