image_data.rs

 1use crate::geometry::vector::{vec2i, Vector2I};
 2use image::{Bgra, ImageBuffer};
 3use std::{
 4    fmt,
 5    sync::{
 6        atomic::{AtomicUsize, Ordering::SeqCst},
 7        Arc,
 8    },
 9};
10
11pub struct ImageData {
12    pub id: usize,
13    data: ImageBuffer<Bgra<u8>, Vec<u8>>,
14}
15
16impl ImageData {
17    pub fn new(data: ImageBuffer<Bgra<u8>, Vec<u8>>) -> Arc<Self> {
18        static NEXT_ID: AtomicUsize = AtomicUsize::new(0);
19
20        Arc::new(Self {
21            id: NEXT_ID.fetch_add(1, SeqCst),
22            data,
23        })
24    }
25
26    pub fn as_bytes(&self) -> &[u8] {
27        &self.data
28    }
29
30    pub fn size(&self) -> Vector2I {
31        let (width, height) = self.data.dimensions();
32        vec2i(width as i32, height as i32)
33    }
34}
35
36impl fmt::Debug for ImageData {
37    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38        f.debug_struct("ImageData")
39            .field("id", &self.id)
40            .field("size", &self.data.dimensions())
41            .finish()
42    }
43}