Graphite/node-graph/gcore/src/raster.rs

78 lines
1.6 KiB
Rust

/// as to not yet rename all references
pub mod color {
pub use super::*;
}
pub mod image;
pub use self::image::Image;
pub use crate::color::*;
use crate::raster_types::CPU;
use std::fmt::Debug;
pub trait Bitmap {
type Pixel: Pixel;
fn width(&self) -> u32;
fn height(&self) -> u32;
fn dimensions(&self) -> (u32, u32) {
(self.width(), self.height())
}
fn dim(&self) -> (u32, u32) {
self.dimensions()
}
fn get_pixel(&self, x: u32, y: u32) -> Option<Self::Pixel>;
}
impl<T: Bitmap> Bitmap for &T {
type Pixel = T::Pixel;
fn width(&self) -> u32 {
(**self).width()
}
fn height(&self) -> u32 {
(**self).height()
}
fn get_pixel(&self, x: u32, y: u32) -> Option<Self::Pixel> {
(**self).get_pixel(x, y)
}
}
impl<T: Bitmap> Bitmap for &mut T {
type Pixel = T::Pixel;
fn width(&self) -> u32 {
(**self).width()
}
fn height(&self) -> u32 {
(**self).height()
}
fn get_pixel(&self, x: u32, y: u32) -> Option<Self::Pixel> {
(**self).get_pixel(x, y)
}
}
pub trait BitmapMut: Bitmap {
fn get_pixel_mut(&mut self, x: u32, y: u32) -> Option<&mut Self::Pixel>;
fn set_pixel(&mut self, x: u32, y: u32, pixel: Self::Pixel) {
*self.get_pixel_mut(x, y).unwrap() = pixel;
}
fn map_pixels<F: Fn(Self::Pixel) -> Self::Pixel>(&mut self, map_fn: F) {
for y in 0..self.height() {
for x in 0..self.width() {
let pixel = self.get_pixel(x, y).unwrap();
self.set_pixel(x, y, map_fn(pixel));
}
}
}
}
impl<T: BitmapMut + Bitmap> BitmapMut for &mut T {
fn get_pixel_mut(&mut self, x: u32, y: u32) -> Option<&mut Self::Pixel> {
(*self).get_pixel_mut(x, y)
}
}