use std::path::Path; use std::sync::Arc; use eframe::egui::{Color32, ColorImage, Context, ImageData, TextureHandle, TextureOptions}; use eframe::egui::load::SizedTexture; use image::{RgbImage}; mod _draw; #[derive(Clone)] pub struct Canvas { image: RgbImage, handle: Option, } impl Canvas { pub fn new(path: impl AsRef) -> Self { Self { image: image::open(path) .unwrap() .to_rgb8(), handle: None, } } pub fn rgba(&self) -> &RgbImage { &self.image } pub fn rgba_mut(&mut self) -> &RgbImage { &mut self.image } pub fn image(&mut self, ctx: &Context) -> SizedTexture { let handle = self.handle.get_or_insert_with(|| { let img = ColorImage::new( [self.image.width() as _, self.image.height() as _], self.image .pixels() .map(|a| Color32::from_rgb(a.0[0], a.0[1], a.0[2])) .collect(), ); ctx.load_texture( "_temp", ImageData::Color(Arc::new(img)), TextureOptions::LINEAR ) }); SizedTexture::new(handle.id(), handle.size_vec2()) } pub fn refresh(&mut self) { self.handle = None } }