lb 0.6.0

A TUI library with ASCII/Unicode graphics.
Documentation
use crate::color;
use crate::mat;
use crate::mat::MatrixMut;

pub type ImgVec = mat::MatrixVec<color::Rgb>;
pub type ImgRef<'a> = mat::MatrixRef<'a, color::Rgb>;
pub type ImgRefMut<'a> = mat::MatrixRefMut<'a, color::Rgb>;

impl ImgVec {
    #[inline]
    pub fn from_bytes(rgb: &[u8], size: mat::Size) -> Result<ImgVec, mat::Error> {
        if rgb.len() == size.width * size.height * 3 {
            let mut vec = Vec::with_capacity(size.width * size.height);

            let mut chunks = rgb.chunks(3);
            while let Some(&[r, g, b]) = chunks.next() {
                vec.push(color::Rgb::new(r, g, b));
            }

            ImgVec::new(vec, size)
        } else {
            Err(mat::Error::InvalidSize)
        }
    }
}

pub trait Img: mat::Matrix<Item = color::Rgb> {
    #[inline]
    fn resize(&self, size: mat::Size) -> ImgVec {
        let mut new = ImgVec::with_default(size);

        let w_ratio = self.width() as f32 / size.width as f32;
        let h_ratio = self.height() as f32 / size.height as f32;

        for (y, row) in new.rows_mut().enumerate() {
            let old_row = (y as f32 * h_ratio).floor() as usize;

            for (x, px) in row.iter_mut().enumerate() {
                let old_col = (x as f32 * w_ratio).floor() as usize;

                *px = self[(old_col, old_row)];
            }
        }

        new
    }
}

impl<M> Img for M where M: mat::Matrix<Item = color::Rgb> {}