1
//! A PNG drawn through ratatui's buffer: kitty unicode placeholders or halfblocks.
2
//!
3
//! The `Picker` of ratatui-image is not used: its font-size constructor is
4
//! deprecated and its stdio query cannot work over an ssh channel, so the
5
//! protocol structs are built directly from the caller's cell pixels.
6

            
7
mod halves;
8

            
9
use halves::Halves;
10
use image::ImageFormat;
11
use ratatui::buffer::Buffer;
12
use ratatui::layout::{Rect, Size};
13
use ratatui::style::Color;
14
use ratatui::widgets::Widget;
15
use ratatui_image::protocol::Protocol;
16
use ratatui_image::protocol::kitty::Kitty;
17
use ratatui_image::{FontSize, Resize};
18

            
19
/// How the picture reaches the terminal.
20
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21
pub enum ImageBackend {
22
    Kitty { id: u32 },
23
    HalfBlocks,
24
}
25

            
26
#[derive(Debug, thiserror::Error)]
27
pub enum ImageError {
28
    #[error("the image area has no room")]
29
    NoRoom,
30
    #[error("the image is not a PNG: {0}")]
31
    NotPng(#[source] image::ImageError),
32
    #[error("the image cannot be drawn: {0}")]
33
    Protocol(#[source] ratatui_image::errors::Errors),
34
}
35

            
36
/// A decoded PNG ready to draw into an area of the size it was built for.
37
pub struct Image {
38
    drawn: Drawn,
39
}
40

            
41
enum Drawn {
42
    Kitty(Protocol),
43
    Halves(Halves),
44
}
45

            
46
impl Image {
47
    /// # Errors
48
    /// The area has no cells, the PNG cannot be decoded, or the protocol rejects the image.
49
169
    pub fn new(
50
169
        png: &[u8],
51
169
        backend: ImageBackend,
52
169
        size: Size,
53
169
        cell_pixels: (u16, u16),
54
169
    ) -> Result<Self, ImageError> {
55
169
        if size.width == 0 || size.height == 0 {
56
4
            return Err(ImageError::NoRoom);
57
165
        }
58
165
        let decoded = image::load_from_memory_with_format(png, ImageFormat::Png)
59
165
            .map_err(ImageError::NotPng)?;
60
158
        let font = FontSize::from(cell_pixels);
61
158
        let fitted = Resize::Scale(None).resize(&decoded, font, size, None);
62
158
        let drawn = match backend {
63
128
            ImageBackend::Kitty { id } => Kitty::new(fitted, size, id, false, true)
64
128
                .map(|kitty| Drawn::Kitty(Protocol::Kitty(kitty)))
65
128
                .map_err(ImageError::Protocol)?,
66
30
            ImageBackend::HalfBlocks => Drawn::Halves(Halves::new(&fitted, size)),
67
        };
68
158
        Ok(Self { drawn })
69
169
    }
70

            
71
    /// Transparent pixels show `background`.
72
129
    pub fn render(&self, area: Rect, buffer: &mut Buffer, background: Color) {
73
129
        match &self.drawn {
74
102
            Drawn::Kitty(protocol) => ratatui_image::Image::new(protocol).render(area, buffer),
75
27
            Drawn::Halves(halves) => halves.render(area, buffer, background),
76
        }
77
129
    }
78
}
79

            
80
/// The kitty command that deletes image `id` and frees its data.
81
#[must_use]
82
67
pub fn kitty_delete(id: u32) -> Vec<u8> {
83
67
    format!("\x1b_Ga=d,d=I,i={id},q=2\x1b\\").into_bytes()
84
67
}
85

            
86
#[cfg(test)]
87
mod tests;