skunk2d/src/image32.rs

65 lines
1.4 KiB
Rust

use crate::image::Image;
use crate::vec_util::IVec2Helper;
use crate::IRect;
use glam::IVec2;
pub type Pixel32 = [u8; 4];
pub struct Image32 {
data: Vec<u8>,
size: IVec2,
}
impl Image32 {
pub fn fill(&mut self, color: Pixel32) {
//todo: performance
for i in (0..self.size.size() * 4).step_by(4) {
self.data[i..i + 4].copy_from_slice(&color);
}
}
pub fn fill_rect(&mut self, rect: IRect, color: Pixel32) {
for pos in rect.iter() {
self.set_pixel(pos, color);
}
}
pub fn get_pixel(&self, pos: IVec2) -> Pixel32 {
//todo: check if performance?
let i = (pos.x + self.size.x * pos.y * 4) as usize;
let mut a = [0u8; 4];
a.copy_from_slice(&self.data[i..i + 4]);
a
}
pub fn set_pixel(&mut self, pos: IVec2, color: Pixel32) {
let i = (pos.x + self.size.x * pos.y * 4) as usize;
self.data[i..i + 4].copy_from_slice(&color);
}
}
impl Image for Image32 {
fn new(size: IVec2) -> Self {
Image32 {
data: vec![0u8; (size.x * size.y) as usize * 4],
size,
}
}
fn clear(&mut self) {
self.data.fill(0);
}
fn data_mut(&mut self) -> &mut [u8] {
self.data.as_mut_slice()
}
fn data(&self) -> &[u8] {
self.data.as_slice()
}
fn size(&self) -> IVec2 {
self.size
}
}