wtrenderlib 0.1.0

Dependency-free terminal rendering primitives for RGB and RGBA frames
Documentation
use std::io::Write;

use crate::{FrameView, PixelFormat, Result};

/// Encodes a frame as SIXEL using the optional `sixel` feature.
pub fn render_sixel<W: Write>(frame: FrameView<'_>, output: &mut W) -> Result<()> {
    let mut rgba = Vec::with_capacity(frame.width() * frame.height() * 4);
    for y in 0..frame.height() {
        for x in 0..frame.width() {
            let pixel = frame.pixel(x, y).expect("validated frame coordinate");
            match frame.format() {
                PixelFormat::Rgb8 => rgba.extend_from_slice(&[pixel[0], pixel[1], pixel[2], 255]),
                PixelFormat::Rgba8 => rgba.extend_from_slice(pixel),
            }
        }
    }
    let image = icy_sixel::SixelImage::from_rgba(rgba, frame.width(), frame.height());
    let encoded = image
        .encode()
        .map_err(|error| crate::Error::Unsupported(error.to_string()))?;
    output.write_all(encoded.as_bytes())?;
    Ok(())
}