pub use self::raw::*;
mod raw;
pub unsafe trait Pixel<T>: Sized {
const CHANNELS: usize;
#[inline]
fn as_raw<P: RawPixel<T> + ?Sized>(&self) -> &P {
unsafe { P::from_raw_parts(self as *const Self as *const T, Self::CHANNELS) }
}
#[inline]
fn as_raw_mut<P: RawPixel<T> + ?Sized>(&mut self) -> &mut P {
unsafe { P::from_raw_parts_mut(self as *mut Self as *mut T, Self::CHANNELS) }
}
#[inline]
fn into_raw<P: RawPixelSized<T>>(self) -> P {
assert_eq!(P::CHANNELS, Self::CHANNELS);
assert_eq!(::core::mem::size_of::<P>(), ::core::mem::size_of::<Self>());
assert_eq!(::core::mem::align_of::<P>(), ::core::mem::align_of::<Self>());
let converted = unsafe { ::core::ptr::read(&self as *const Self as *const P) };
::core::mem::forget(self);
converted
}
#[inline]
fn from_raw<P: RawPixel<T> + ?Sized>(pixel: &P) -> &Self {
assert!(
pixel.channels() >= Self::CHANNELS,
"not enough color channels"
);
unsafe { &*(pixel.as_ptr() as *const Self) }
}
#[inline]
fn from_raw_mut<P: RawPixel<T> + ?Sized>(pixel: &mut P) -> &mut Self {
assert!(pixel.channels() >= Self::CHANNELS);
unsafe { &mut *(pixel.as_mut_ptr() as *mut Self) }
}
#[inline]
fn from_raw_slice(slice: &[T]) -> &[Self] {
assert_eq!(slice.len() % Self::CHANNELS, 0);
let new_length = slice.len() / Self::CHANNELS;
unsafe { ::core::slice::from_raw_parts(slice.as_ptr() as *const Self, new_length) }
}
#[inline]
fn from_raw_slice_mut(slice: &mut [T]) -> &mut [Self] {
assert_eq!(slice.len() % Self::CHANNELS, 0);
let new_length = slice.len() / Self::CHANNELS;
unsafe { ::core::slice::from_raw_parts_mut(slice.as_mut_ptr() as *mut Self, new_length) }
}
#[inline]
fn into_raw_slice(slice: &[Self]) -> &[T] {
let new_length = slice.len() * Self::CHANNELS;
unsafe { ::core::slice::from_raw_parts(slice.as_ptr() as *const T, new_length) }
}
#[inline]
fn into_raw_slice_mut(slice: &mut [Self]) -> &mut [T] {
let new_length = slice.len() * Self::CHANNELS;
unsafe { ::core::slice::from_raw_parts_mut(slice.as_mut_ptr() as *mut T, new_length) }
}
}