#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(not(feature = "std"))]
extern crate core as std;
#[cfg(feature = "alloc")]
extern crate alloc;
#[cfg(feature = "alloc")]
use alloc::vec::Vec;
pub mod image_ref;
pub mod iter;
#[cfg(any(feature = "std", feature = "alloc"))]
pub mod owned;
#[cfg(any(feature = "std", feature = "alloc"))]
pub mod cow;
#[allow(non_camel_case_types)]
pub mod pixel_format;
pub use pixel_format::{PixFmt, PixelFormat};
#[derive(Clone)]
pub struct ImageBufferRef<'a, F> {
pub pixel_format: std::marker::PhantomData<F>,
pub data: &'a [u8],
}
impl<F: PixelFormat> std::fmt::Debug for ImageBufferRef<'_, F> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_struct("ImageBufferRef").finish_non_exhaustive()
}
}
impl<'a, F> ImageBufferRef<'a, F> {
#[inline]
pub fn new(data: &'a [u8]) -> Self {
Self {
pixel_format: std::marker::PhantomData,
data,
}
}
#[cfg(any(feature = "std", feature = "alloc"))]
#[inline]
pub fn to_buffer(&self) -> ImageBuffer<F> {
ImageBuffer::new(self.data.to_vec())
}
}
pub struct ImageBufferMutRef<'a, F> {
pub pixel_format: std::marker::PhantomData<F>,
pub data: &'a mut [u8],
}
impl<F: PixelFormat> std::fmt::Debug for ImageBufferMutRef<'_, F> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_struct("ImageBufferMutRef").finish_non_exhaustive()
}
}
impl<'a, F> ImageBufferMutRef<'a, F> {
#[inline]
pub fn new(data: &'a mut [u8]) -> Self {
Self {
pixel_format: std::marker::PhantomData,
data,
}
}
#[cfg(any(feature = "std", feature = "alloc"))]
#[inline]
pub fn to_buffer(&self) -> ImageBuffer<F> {
ImageBuffer::new(self.data.to_vec())
}
}
#[cfg(any(feature = "std", feature = "alloc"))]
#[derive(Clone)]
pub struct ImageBuffer<F> {
pub pixel_format: std::marker::PhantomData<F>,
pub data: Vec<u8>,
}
#[cfg(any(feature = "std", feature = "alloc"))]
impl<F: PixelFormat> std::fmt::Debug for ImageBuffer<F> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_struct("ImageBuffer").finish_non_exhaustive()
}
}
#[cfg(any(feature = "std", feature = "alloc"))]
impl<F> ImageBuffer<F> {
#[inline]
pub fn new(data: Vec<u8>) -> Self {
Self {
pixel_format: std::marker::PhantomData,
data,
}
}
}
pub trait ImageData<F> {
fn width(&self) -> u32;
fn height(&self) -> u32;
#[inline]
fn image_data(&self) -> &[u8] {
self.buffer_ref().data
}
fn buffer_ref(&self) -> ImageBufferRef<'_, F>;
#[cfg(any(feature = "std", feature = "alloc"))]
fn buffer(self) -> ImageBuffer<F>;
}
pub trait ImageMutData<F>: ImageData<F> {
fn buffer_mut_ref(&mut self) -> ImageBufferMutRef<'_, F>;
}
pub trait Stride {
fn stride(&self) -> usize;
}
pub trait AsImageData<F>: ImageData<F> {
fn as_image_data(&self) -> &dyn ImageData<F>;
}
impl<S: ImageData<F>, F> AsImageData<F> for S {
fn as_image_data(&self) -> &dyn ImageData<F> {
self
}
}
#[cfg(any(feature = "std", feature = "alloc"))]
pub trait OwnedImage<F>: AsImageData<F> + ImageData<F> + Into<Vec<u8>> {}
#[cfg(any(feature = "std", feature = "alloc"))]
impl<S, F> OwnedImage<F> for S
where
S: AsImageData<F> + ImageData<F>,
Vec<u8>: From<S>,
{
}
pub trait ImageStride<F>: ImageData<F> + Stride {}
impl<S, F> ImageStride<F> for S where S: ImageData<F> + Stride {}
pub trait AsImageStride<F>: ImageStride<F> {
fn as_image_stride(&self) -> &dyn ImageStride<F>;
}
impl<S: ImageStride<F>, F> AsImageStride<F> for S {
fn as_image_stride(&self) -> &dyn ImageStride<F> {
self
}
}
pub trait ImageMutStride<F>: ImageMutData<F> + Stride {}
impl<S, F> ImageMutStride<F> for S where S: ImageMutData<F> + Stride {}
pub trait AsImageMutStride<F>: ImageMutStride<F> {
fn as_image_mut_stride(&self) -> &dyn ImageMutStride<F>;
}
impl<S: ImageMutStride<F>, F> AsImageMutStride<F> for S {
fn as_image_mut_stride(&self) -> &dyn ImageMutStride<F> {
self
}
}
#[cfg(any(feature = "std", feature = "alloc"))]
pub trait OwnedImageStride<F>: AsImageStride<F> + ImageStride<F> + Into<Vec<u8>> {}
#[cfg(any(feature = "std", feature = "alloc"))]
impl<S, F> OwnedImageStride<F> for S
where
S: AsImageStride<F> + ImageStride<F>,
Vec<u8>: From<S>,
{
}