mrk-cli 0.11.0

Render Markdown beautifully in the terminal: syntax highlighting, tables, themes, and Mermaid diagrams as images.
Documentation
use std::cmp::Reverse;
use std::collections::BTreeMap;
use std::io::Cursor;
use std::ops::Range;

use super::color::distance;
use crate::document::Rgb;

/// Sixel has 256 colour registers on the terminals that matter.
const MAX_COLORS: usize = 256;
/// Below half opacity a pixel is left out, so the terminal background shows through: Sixel has no partial alpha.
const MIN_ALPHA: u8 = 128;
/// One sixel is a column of six pixels.
const BAND_PX: u32 = 6;
const SIXEL_OFFSET: u8 = 0x3f;
/// Colours are counted on 5 bits per channel, so the near-identical shades of anti-aliasing share one bucket.
const BUCKET_BITS: u8 = 5;
const BUCKETS: usize = 1 << (3 * BUCKET_BITS);
/// Shorter runs cost fewer bytes written out than as `!count`.
const MIN_REPEAT: usize = 4;
/// `P2=1`: pixels no colour sets stay transparent.
const BEGIN: &str = "\x1bP0;1;0q";
const END: &str = "\x1b\\";

/// A picture's pixels reduced to at most 256 colours, ready to be sent as Sixel, whole or a band of rows at a time.
#[derive(Clone, Debug)]
pub struct Image {
    width: u32,
    height: u32,
    /// The palette as Sixel colour definitions, sent ahead of the bands every time.
    colors: String,
    /// One palette index per pixel, row after row; `None` where the pixel is transparent.
    pixels: Vec<Option<u8>>,
}

impl Image {
    /// `None` unless the PNG is 8-bit RGBA, the only kind mrk draws.
    pub fn from_png(png: &[u8]) -> Option<Self> {
        let (width, height, rgba) = decode(png)?;
        Some(quantize(width, height, &rgba))
    }

    /// The pixel `rows` as one Sixel sequence, the last band's rows past them left transparent: no row goes missing,
    /// and none outside them is drawn.
    pub fn encode(&self, rows: Range<u32>) -> String {
        let top = rows.start.min(self.height);
        let bottom = rows.end.clamp(top, self.height);
        let bands: String =
            (top..bottom).step_by(BAND_PX as usize).map(|band_top| self.band(band_top..bottom.min(band_top + BAND_PX))).collect();
        format!("{BEGIN}\"1;1;{};{}{}{bands}{END}", self.width, bottom - top, self.colors)
    }

    fn row(&self, y: u32) -> &[Option<u8>] {
        let width = self.width as usize;
        &self.pixels[y as usize * width..][..width]
    }

    /// Up to six rows: each colour's sixels in turn, back to the band's start (`$`) between colours, then down (`-`).
    fn band(&self, rows: Range<u32>) -> String {
        let mut masks: BTreeMap<u8, Vec<u8>> = BTreeMap::new();
        for (bit, y) in rows.enumerate() {
            for (x, index) in self.row(y).iter().enumerate().filter_map(|(x, index)| Some((x, (*index)?))) {
                masks.entry(index).or_insert_with(|| vec![0; self.width as usize])[x] |= 1 << bit;
            }
        }
        let colors: Vec<String> = masks.iter().map(|(index, mask)| format!("#{index}{}", run_length(mask))).collect();
        format!("{}-", colors.join("$"))
    }
}

/// Draws the pixel `rows` of the image, all of them when `None`, with Sixel from the cursor, then puts the cursor back
/// where it was: where it ends up after an image differs between terminals.
pub fn draw(image: &Image, rows: Option<Range<u32>>) -> String {
    format!("\x1b7{}\x1b8", image.encode(rows.unwrap_or(0..image.height)))
}

fn decode(png: &[u8]) -> Option<(u32, u32, Vec<u8>)> {
    let mut reader = png::Decoder::new(Cursor::new(png)).read_info().ok()?;
    let mut buffer = vec![0; reader.output_buffer_size()?];
    let info = reader.next_frame(&mut buffer).ok()?;
    let is_rgba = info.color_type == png::ColorType::Rgba && info.bit_depth == png::BitDepth::Eight;
    buffer.truncate(info.buffer_size());
    is_rgba.then_some((info.width, info.height, buffer))
}

fn is_opaque(pixel: [u8; 4]) -> bool {
    pixel[3] >= MIN_ALPHA
}

fn bucket(pixel: [u8; 4]) -> usize {
    let shift = 8 - BUCKET_BITS;
    pixel[..3].iter().fold(0, |bucket, channel| (bucket << BUCKET_BITS) | usize::from(channel >> shift))
}

#[derive(Clone, Copy, Default)]
struct Tally {
    count: u32,
    sums: [u64; 3],
}

impl Tally {
    fn mean(self) -> Rgb {
        let channel = |sum: u64| u8::try_from(sum / u64::from(self.count.max(1))).unwrap_or(u8::MAX);
        Rgb(channel(self.sums[0]), channel(self.sums[1]), channel(self.sums[2]))
    }
}

fn tallies(rgba: &[u8]) -> Vec<Tally> {
    let mut tallies = vec![Tally::default(); BUCKETS];
    for pixel in rgba.as_chunks::<4>().0.iter().filter(|pixel| is_opaque(**pixel)) {
        let tally = &mut tallies[bucket(*pixel)];
        tally.count += 1;
        tally.sums.iter_mut().zip(&pixel[..3]).for_each(|(sum, channel)| *sum += u64::from(*channel));
    }
    tallies
}

fn nearest(color: Rgb, palette: &[Rgb]) -> u8 {
    let index = (0..palette.len()).min_by_key(|&index| distance(color, palette[index])).unwrap_or_default();
    u8::try_from(index).unwrap_or(u8::MAX)
}

/// The most common colour buckets become the palette, each at the mean of its pixels; every other bucket takes the
/// nearest of them. Diagrams use a handful of flat colours, which this keeps exact.
fn quantize(width: u32, height: u32, rgba: &[u8]) -> Image {
    let tallies = tallies(rgba);
    let mut used: Vec<usize> = (0..BUCKETS).filter(|&bucket| tallies[bucket].count > 0).collect();
    used.sort_by_key(|&bucket| (Reverse(tallies[bucket].count), bucket));
    let (common, rare) = used.split_at(used.len().min(MAX_COLORS));
    let palette: Vec<Rgb> = common.iter().map(|&bucket| tallies[bucket].mean()).collect();

    let mut lookup = vec![0; BUCKETS];
    for (index, &bucket) in common.iter().enumerate() {
        lookup[bucket] = u8::try_from(index).unwrap_or(u8::MAX);
    }
    for &bucket in rare {
        lookup[bucket] = nearest(tallies[bucket].mean(), &palette);
    }
    let pixels = rgba.as_chunks::<4>().0.iter().map(|pixel| is_opaque(*pixel).then(|| lookup[bucket(*pixel)])).collect();
    Image { width, height, colors: colors(&palette), pixels }
}

fn colors(palette: &[Rgb]) -> String {
    let percent = |channel: u8| u32::from(channel) * 100 / 255;
    let define =
        |(index, Rgb(red, green, blue)): (usize, &Rgb)| format!("#{index};2;{};{};{}", percent(*red), percent(*green), percent(*blue));
    palette.iter().enumerate().map(define).collect()
}

fn run_length(mask: &[u8]) -> String {
    let end = mask.iter().rposition(|&bits| bits != 0).map_or(0, |last| last + 1);
    let mut runs = Vec::new();
    let mut rest = &mask[..end];
    while let Some(&bits) = rest.first() {
        let run = rest.iter().take_while(|&&each| each == bits).count();
        let sixel = char::from(SIXEL_OFFSET + bits);
        runs.push(if run >= MIN_REPEAT { format!("!{run}{sixel}") } else { sixel.to_string().repeat(run) });
        rest = &rest[run..];
    }
    runs.concat()
}

/// An 8-bit RGBA PNG of these pixels, row after row, for tests.
#[cfg(test)]
pub fn test_png(width: u32, height: u32, pixels: &[[u8; 4]]) -> Vec<u8> {
    let mut bytes = Vec::new();
    let mut encoder = png::Encoder::new(&mut bytes, width, height);
    encoder.set_color(png::ColorType::Rgba);
    encoder.set_depth(png::BitDepth::Eight);
    let written = encoder.write_header().and_then(|mut writer| writer.write_image_data(&pixels.concat()));
    assert!(written.is_ok(), "{written:?}");
    bytes
}

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used)]

    use super::*;

    const RED: [u8; 4] = [255, 0, 0, 255];
    const BLUE: [u8; 4] = [0, 0, 255, 255];
    const CLEAR: [u8; 4] = [0, 0, 0, 0];

    fn image(width: u32, height: u32, pixels: &[[u8; 4]]) -> Image {
        Image::from_png(&test_png(width, height, pixels)).unwrap()
    }

    fn readable(sixel: &str) -> String {
        sixel.replace('\x1b', "␛")
    }

    #[test]
    fn a_two_colour_image_is_one_band_per_six_rows() {
        let left_red_right_blue: Vec<[u8; 4]> = (0..12).flat_map(|_| [RED, RED, RED, RED, BLUE]).collect();

        insta::assert_snapshot!(readable(&image(5, 12, &left_red_right_blue).encode(0..12)));
    }

    #[test]
    fn transparent_pixels_get_no_colour() {
        let image = image(2, 6, &[[RED, CLEAR]; 6].concat());

        assert_eq!(image.encode(0..6), "\x1bP0;1;0q\"1;1;2;6#0;2;100;0;0#0~-\x1b\\");
    }

    #[test]
    fn half_transparent_pixels_count_as_opaque() {
        let image = image(1, 6, &[[255, 0, 0, 128], [255, 0, 0, 127], RED, RED, RED, RED]);

        assert_eq!(image.encode(0..6), "\x1bP0;1;0q\"1;1;1;6#0;2;100;0;0#0|-\x1b\\");
    }

    #[test]
    fn the_last_band_is_padded_with_transparent_rows_and_nothing_past_the_image_is_drawn() {
        let image = image(1, 14, &[RED; 14]);

        assert_eq!(image.encode(0..14), "\x1bP0;1;0q\"1;1;1;14#0;2;100;0;0#0~-#0~-#0B-\x1b\\");
        assert_eq!(image.encode(3..10), "\x1bP0;1;0q\"1;1;1;7#0;2;100;0;0#0~-#0@-\x1b\\");
        assert_eq!(image.encode(10..40), "\x1bP0;1;0q\"1;1;1;4#0;2;100;0;0#0N-\x1b\\");
        assert_eq!(image.encode(20..40), "\x1bP0;1;0q\"1;1;1;0#0;2;100;0;0\x1b\\");
    }

    #[test]
    fn long_runs_are_repeated_and_trailing_blanks_dropped() {
        assert_eq!(run_length(&[1, 1, 1, 1, 1, 2, 2, 0, 0]), "!5@AA");
        assert_eq!(run_length(&[0, 0, 0]), "");
    }

    #[test]
    fn past_256_colours_the_rarest_takes_the_nearest_common_one() {
        let grid = (0..255u8).map(|index| [index % 32 * 8, index / 32 * 8, 0, 255]);
        let common: Vec<[u8; 4]> = grid.chain([BLUE]).flat_map(|pixel| [pixel, pixel]).collect();
        let rare = [0, 0, 200, 255];

        let image = image(513, 1, &[common, vec![rare]].concat());

        assert_eq!(image.colors.matches(";2;").count(), MAX_COLORS);
        assert_eq!(image.pixels[512], image.pixels[510]);
    }

    #[test]
    fn a_rare_colour_takes_the_nearest_common_one() {
        let palette = [Rgb(255, 0, 0), Rgb(0, 0, 255)];

        assert_eq!(nearest(Rgb(200, 10, 30), &palette), 0);
        assert_eq!(nearest(Rgb(20, 10, 220), &palette), 1);
    }

    #[test]
    fn only_rgba_pngs_are_read() {
        assert!(Image::from_png(b"not a png").is_none());
        let mut gray = Vec::new();
        let mut encoder = png::Encoder::new(&mut gray, 1, 1);
        encoder.set_color(png::ColorType::Grayscale);
        encoder.write_header().unwrap().write_image_data(&[0]).unwrap();

        assert!(Image::from_png(&gray).is_none());
    }

    #[test]
    fn a_drawn_picture_puts_the_cursor_back() {
        let image = image(1, 12, &[RED; 12]);

        let drawn = draw(&image, None);

        assert!(drawn.starts_with("\x1b7\x1bP") && drawn.ends_with("\x1b\\\x1b8"), "{drawn:?}");
        assert!(drawn.contains("\"1;1;1;12#"), "{drawn:?}");
        assert!(draw(&image, Some(6..11)).contains("\"1;1;1;5#0;2;100;0;0#0^-"));
    }
}