1

I'm writing an application with rust-xcb. However, when I try to load a file into a pixmap I cannot find any way to do it. I also use image library to load image files (jpg).

But I am not familiar with xcb, is there a way for xcb to load pixel buffers or files into pixmap? Or I can find another library to do this?

I have searched for it. Some document of xcb pixmap and bitmap is full of TODO. I have tried xcb-util-image, but didn't find what I need.

My code is below:

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
YangKeao
  • 184
  • 6
  • One thing that looks wrong about your code: you're not checking your cookies. Tacking `.request_check().unwrap_or_else(|e| panic!("{}", e))` onto the end of your calls will help get you an error code if something goes wrong. – notriddle Feb 22 '19 at 20:46

1 Answers1

0

According to the XCB documentation, pixmaps and windows are both drawables: https://xcb.freedesktop.org/colorsandpixmaps/ ("The operations that work the same on a window or a pixmap take an xcb_drawable_t argument")

So, once you've created your Pixmap, you pass it as the third parameter to put_image. No need to worry about converting the Pixmap into a Drawable; they're both just type aliases for u32 on your end, and they both use the same ID space on the X server's end (as the same docs put it, "a pixmap is basically a window that isn't shown on the screen").

To generate the stuff that goes in the data parameter, probably just do the same thing as the libpng source does, but in Rust instead of C.

notriddle
  • 640
  • 4
  • 10
  • Actually, I use `image` library to decode image files and got raw pixel arrays (in rgb format). I have added my code in question description. Could you help me find where the bug is? – YangKeao Feb 21 '19 at 11:56