我正在用rust-xcb
写一个应用程序。但是,当我尝试将文件加载到像素图中时,我找不到任何方法来执行此操作。我还使用image
库来加载图像文件(jpg)。
但我不熟悉xcb,有没有办法让xcb将像素缓冲区或文件加载到pixmap中?或者我可以找到另一个库来做到这一点?
我找了它。 xcb pixmap和bitmap的一些文档充满了TODO。我试过xcb-util-image
,但没找到我需要的东西。
我的代码如下:
let foreground = self.connection.generate_id();
xcb::create_gc(
&self.connection,
foreground,
screen.root(),
&[
(xcb::GC_FOREGROUND, screen.white_pixel()),
(xcb::GC_GRAPHICS_EXPOSURES, 0),
],
);
let mut img = image::open(background_src).unwrap();
let img_width = img.width();
let img_height = img.height();
xcb::create_pixmap(
&self.connection,
24,
pixmap,
self.window_id,
img_width as u16,
img_height as u16,
);
let img_buffer = img.to_rgb().into_raw();
xcb::put_image(
&self.connection,
xcb::IMAGE_FORMAT_Z_PIXMAP as u8,
pixmap,
foreground,
img_width as u16,
img_height as u16,
0,
0,
0,
24,
&img_buffer,
);
self.flush(); // Flush the connection
根据XCB文档,pixmaps和windows都是drawable:https://xcb.freedesktop.org/colorsandpixmaps/(“在窗口或pixmap上工作相同的操作采用xcb_drawable_t参数”)
所以,一旦你有了created你的Pixmap,你将它作为第三个参数传递给put_image
。无需担心将Pixmap转换为Drawable;它们只是u32
的类型别名,并且它们都在X服务器端使用相同的ID空间(正如相同的文档所说的那样,“pixmap基本上是一个未在屏幕上显示的窗口” )。
要生成data
参数中的内容,可能只是做与libpng源相同的事情,但在Rust而不是C.