53 lines
1.3 KiB
Rust
53 lines
1.3 KiB
Rust
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<TextureHandle>,
|
|
}
|
|
|
|
impl Canvas {
|
|
|
|
pub fn new(path: impl AsRef<Path>) -> 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
|
|
}
|
|
|
|
}
|