wtrenderlib 0.1.0

Dependency-free terminal rendering primitives for RGB and RGBA frames
Documentation
use crate::{Error, Result};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PixelFormat {
    Rgb8,
    Rgba8,
}

impl PixelFormat {
    pub const fn bytes_per_pixel(self) -> usize {
        match self {
            Self::Rgb8 => 3,
            Self::Rgba8 => 4,
        }
    }
}

#[derive(Debug, Clone, Copy)]
pub struct FrameView<'a> {
    data: &'a [u8],
    width: usize,
    height: usize,
    stride: usize,
    format: PixelFormat,
}

impl<'a> FrameView<'a> {
    pub fn new(
        data: &'a [u8],
        width: usize,
        height: usize,
        stride: usize,
        format: PixelFormat,
    ) -> Result<Self> {
        if width == 0 || height == 0 {
            return Err(Error::EmptyFrame);
        }
        let row_bytes = width
            .checked_mul(format.bytes_per_pixel())
            .ok_or(Error::DimensionOverflow)?;
        if stride < row_bytes {
            return Err(Error::InvalidStride {
                stride,
                minimum: row_bytes,
            });
        }
        let required = stride.checked_mul(height).ok_or(Error::DimensionOverflow)?;
        if data.len() < required {
            return Err(Error::BufferTooSmall {
                actual: data.len(),
                required,
            });
        }
        Ok(Self {
            data,
            width,
            height,
            stride,
            format,
        })
    }

    pub const fn width(self) -> usize {
        self.width
    }

    pub const fn height(self) -> usize {
        self.height
    }

    pub const fn stride(self) -> usize {
        self.stride
    }

    pub const fn format(self) -> PixelFormat {
        self.format
    }

    pub fn row(self, y: usize) -> Option<&'a [u8]> {
        if y >= self.height {
            return None;
        }
        let start = y * self.stride;
        Some(&self.data[start..start + self.width * self.format.bytes_per_pixel()])
    }

    pub fn pixel(self, x: usize, y: usize) -> Option<&'a [u8]> {
        if x >= self.width || y >= self.height {
            return None;
        }
        let bpp = self.format.bytes_per_pixel();
        let start = y * self.stride + x * bpp;
        Some(&self.data[start..start + bpp])
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OwnedFrame {
    data: Vec<u8>,
    width: usize,
    height: usize,
    format: PixelFormat,
}

impl OwnedFrame {
    pub fn new(data: Vec<u8>, width: usize, height: usize, format: PixelFormat) -> Result<Self> {
        let stride = width
            .checked_mul(format.bytes_per_pixel())
            .ok_or(Error::DimensionOverflow)?;
        FrameView::new(&data, width, height, stride, format)?;
        Ok(Self {
            data,
            width,
            height,
            format,
        })
    }

    pub fn as_view(&self) -> FrameView<'_> {
        FrameView {
            data: &self.data,
            width: self.width,
            height: self.height,
            stride: self.width * self.format.bytes_per_pixel(),
            format: self.format,
        }
    }

    pub fn into_inner(self) -> Vec<u8> {
        self.data
    }
}