nashaofu / xcap

XCap is a cross-platform screen capture library written in Rust. It supports Linux (X11, Wayland), MacOS, and Windows. XCap supports screenshot and video recording (to be implemented).
https://docs.rs/xcap
Apache License 2.0
454 stars 54 forks source link

Feature: add capturing into raw BGR buffer #85

Open konstantinzolotarev opened 9 months ago

konstantinzolotarev commented 9 months ago

First of all,, thank you very much for a great work.

Is it possible to add ability to make screenshots into raw buffer rather than Result<ImageRgba> ?

it's very useful for work with opencv, but right now your lib does conversion from bgr into rgba and then to work properly with images in opencv code have to do backward conversion from rgba into bgr.

Just a side note. Result might be something like

pub struct RawImage {
    pub width: u32,
    pub height: u32,
    pub data: Vec<u8>, // even BGRA works very well in here. 
}

Then it's very easy to convert it into opencv::Mat

unsafe {
        Mat::new_rows_cols_with_data(
            image.height as i32,
            image.width as i32,
            core::CV_8UC4,
            image.data.as_mut_ptr().cast::<c_void>(),
            core::Mat_AUTO_STEP,
        )
        .unwrap()
    }
nashaofu commented 3 months ago

The current return value is RgbaImage, you see if you meet the need,By the way, the latest crate name is xcap

hakoptak commented 3 months ago

Hi, I have the same request.

Also: isn't it a bit strange to save a screen capture with transparancy while the screenshot itself only contains pixels without transparancy? Getting a RGB buffer would save cpu some cycles and memory. Good for the environment ;)

Thanks for this nice library.

konstantinzolotarev commented 3 months ago

@nashaofu Thank you very much ! Yep I already upgraded to new lib and tried working with RgbaImage, it has exactly same issue for me, to build RgbaImage lib have to walk through all pixels and swap R and B values (BGRA -> RGBA):

for bgra in buffer.chunks_exact_mut(4) {
        bgra.swap(0, 2);
}

And to convert it correctly into opencv.Mat format I have to swap it back form RGBA into BGRA. So every screenshot have to be processed twice. And because I have to read screen with max FPS possible, it's very costly for my case.

But don't worry I think it's very specific problem and I can easily solve it by just using parts of code from your lib in my experiments.

I can close issue if you want.

Anyway thank you very much for your hard work and help ! Very much appreciate all you have done !