I create a Picture from static RGB bytes via Pixbuf, and I hope the Picture can be re-rendered when RGB bytes change. Seems Picture can’t do this automatically, is there any way to do that?
Here was my code:
const SIZE: usize = 300 * 200;
const LEN: usize = SIZE * 3;
static mut RGB_BYTES: [u8; LEN] = [0; LEN];
fn main() {
let app = Application::builder().application_id("com.pixbuf").build();
app.connect_activate(build_ui);
app.run();
}
fn change_rgb() {
unsafe {
for i in 0..LEN {
RGB_BYTES[i] = match RGB_BYTES[i] {
255 => 0,
rgb => rgb + 1,
};
}
println!("Rgb bytes changed.")
}
}
fn build_ui(app: &Application) {
unsafe {
let pixbuf = Pixbuf::from_bytes(
&Bytes::from_static(&RGB_BYTES),
gtk::gdk_pixbuf::Colorspace::Rgb,
false,
8,
300,
200,
300 * 3,
);
let picture = Picture::for_pixbuf(&pixbuf);
picture.set_can_shrink(false);
picture.set_halign(gtk::Align::Start);
picture.set_valign(gtk::Align::Start);
let window = ApplicationWindow::builder()
.application(app)
.title("Pixbuf")
.child(&picture)
.build();
timeout_add_local(Duration::from_millis(100), || {
change_rgb();
Continue(true)
});
window.present();
}
}