Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question : Render Widget to Image #1499

Closed
sachaarbonel opened this issue Jan 2, 2021 · 4 comments
Closed

Question : Render Widget to Image #1499

sachaarbonel opened this issue Jan 2, 2021 · 4 comments
Labels
enhancement adds or requests a new feature question causes uncertainty shell concerns the shell abstraction
Milestone

Comments

@sachaarbonel
Copy link

Hi thank's for this great library, is it currently possible to render a druid widget to an image? I understand it is possible to do this something like this in piet :

 let mut device = Device::new().unwrap();
    let width = 400;
    let height = 400;
    let mut bitmap = device.bitmap_target(width, height, 1.0).unwrap();
    let mut rc = bitmap.render_context();
    rc.clear(Color::WHITE);
    // ...
    //draw operations
   // ...
    rc.finish().unwrap();
    std::mem::drop(rc);

    bitmap
        .save_to_file("temp-image.png")
        .expect("file save error");

Also, I know I can access ctx.render_ctx in the paint method of the widget but didn't help much.
Piet is too low level for my use case (mainly generating images from a layout composed of Columns and Rows and passing them to a video pipeline (gstreamer or ffmpeg)) and I was wondering if I could do this with Druid, without the gtk application shell.

I see you did something similar with the Harness struct in tests but it is not exposed.

@richard-uk1
Copy link
Collaborator

richard-uk1 commented Jan 2, 2021

It's certainly possible, I don't know if the required functionality is available yet. Perhaps it should be. I'll let others weigh in.

@richard-uk1 richard-uk1 added enhancement adds or requests a new feature question causes uncertainty shell concerns the shell abstraction labels Jan 2, 2021
@cmyr
Copy link
Member

cmyr commented Jan 2, 2021

So.... yes, I think you should be able to currently do this, I just never have.

Oh actually: maybe not quite possible. Basically what you'll want to do, for this, is to create a custom PaintCtx using a bitmap target instead of the display that is usually the target; and then you would want to call your widget's paint method.

I would suggest playing around with this by cloning druid itself, and adding a custom example. The thing you'll need to figure out is how to create a PaintCtx; it shouldn't be too hard, but you'll need to mark some things as pub that are currently private.

If we wanted to actually expose this in druid, the way to do it would be to have a method on PaintCtx that let you pass a reference to a different render context, and ge back a new PaintCtx with the same state as the original; then you could paint into this, and things like is_hot and is_active would all be correct for your widget, but the result would be painted to the bitmap.

The code would otherwise look like your example.

Feel free to play around with this, and if you come up with a reasonably small patch that enables this I would be happy to merge.

@sachaarbonel
Copy link
Author

Thank's a lot for this detailed explanation. I'll look into it and eventually come up with a PR

@cmyr
Copy link
Member

cmyr commented Mar 30, 2021

this should be possible as of #1677, which brings in the RenderContext::capture_image_area method.

@cmyr cmyr closed this as completed Mar 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement adds or requests a new feature question causes uncertainty shell concerns the shell abstraction
Projects
None yet
Development

No branches or pull requests

3 participants