| 1 | //! Bounded RGBA avatar tiles painted with terminal half-blocks. This renderer |
| 2 | //! neither loads plugins nor owns a timer. Hosts supply reviewed pixels and |
| 3 | //! sample an action with the same Director used by the rest of their UI. |
| 4 | use crate::{ |
| 5 | Paint, Role, Theme, |
| 6 | color::{self, ColorDepth}, |
| 7 | }; |
| 8 | use ratatui::{ |
| 9 | buffer::Buffer, |
| 10 | layout::Rect, |
| 11 | style::{Color, Style}, |
| 12 | }; |
| 13 | |
| 14 | /// Row-major RGBA pixels within each tile, tiles in manifest order. |
| 15 | /// Hosts decode PNG once and concatenate its tiles; terminal authors can also |
| 16 | /// ship predecoded tiles. Construction validates the complete byte geometry. |
| 17 | #[derive(Clone, Copy)] |
| 18 | pub struct Sprite<'a> { |
| 19 | pixels: &'a [u8], |
| 20 | width: usize, |
| 21 | height: usize, |
| 22 | frame: usize, |
| 23 | } |
| 24 | impl<'a> Sprite<'a> { |
| 25 | /// One already-decoded RGBA image, without an avatar manifest. Useful for |
| 26 | /// live vector scenes; shares the same half-block and capability painter. |
| 27 | pub fn image(pixels: &'a [u8], width: usize, height: usize) -> Result<Self, String> { |
| 28 | Self::tiles(pixels, width, height, 0, 1) |
| 29 | } |
| 30 | |
| 31 | pub fn new( |
| 32 | pack: &crate::avatar::Pack, |
| 33 | pixels: &'a [u8], |
| 34 | width: usize, |
| 35 | height: usize, |
| 36 | frame: usize, |
| 37 | ) -> Result<Self, String> { |
| 38 | pack.validate()?; |
| 39 | let count = usize::from(pack.columns) * usize::from(pack.rows) * pack.atlases.len(); |
| 40 | Self::tiles(pixels, width, height, frame, count) |
| 41 | } |
| 42 | pub fn page( |
| 43 | pack: &crate::avatar::Pack, |
| 44 | pixels: &'a [u8], |
| 45 | width: usize, |
| 46 | height: usize, |
| 47 | frame: usize, |
| 48 | ) -> Result<Self, String> { |
| 49 | pack.validate()?; |
| 50 | if pack.page(frame) >= pack.atlases.len() { |
| 51 | return Err("avatar frame exceeds page count".into()); |
| 52 | } |
| 53 | Self::tiles( |
| 54 | pixels, |
| 55 | width, |
| 56 | height, |
| 57 | pack.local_frame(frame), |
| 58 | usize::from(pack.columns) * usize::from(pack.rows), |
| 59 | ) |
| 60 | } |
| 61 | fn tiles( |
| 62 | pixels: &'a [u8], |
| 63 | width: usize, |
| 64 | height: usize, |
| 65 | frame: usize, |
| 66 | count: usize, |
| 67 | ) -> Result<Self, String> { |
| 68 | if width == 0 |
| 69 | || height == 0 |
| 70 | || width > 2048 |
| 71 | || height > 2048 |
| 72 | || frame >= count |
| 73 | || width |
| 74 | .checked_mul(height) |
| 75 | .and_then(|n| n.checked_mul(4)) |
| 76 | .and_then(|n| n.checked_mul(count)) |
| 77 | != Some(pixels.len()) |
| 78 | || pixels.len() > 16 * 1024 * 1024 |
| 79 | { |
| 80 | return Err("avatar pixels do not match bounded tile dimensions".into()); |
| 81 | } |
| 82 | Ok(Self { |
| 83 | pixels, |
| 84 | width, |
| 85 | height, |
| 86 | frame, |
| 87 | }) |
| 88 | } |
| 89 | fn pixel(&self, x: usize, y: usize) -> [u8; 4] { |
| 90 | let offset = ((self.frame * self.height + y) * self.width + x) * 4; |
| 91 | self.pixels[offset..offset + 4] |
| 92 | .try_into() |
| 93 | .expect("validated RGBA pixel") |
| 94 | } |
| 95 | |
| 96 | fn sample(&self, x: usize, y: usize, up: usize, down: usize) -> [u8; 4] { |
| 97 | self.pixel( |
| 98 | ((2 * x + 1) * down / (2 * up)).min(self.width - 1), |
| 99 | ((2 * y + 1) * down / (2 * up)).min(self.height - 1), |
| 100 | ) |
| 101 | } |
| 102 | } |
| 103 | impl Paint for Sprite<'_> { |
| 104 | fn paint(&self, area: Rect, buf: &mut Buffer, theme: &Theme) { |
| 105 | let area = area.intersection(buf.area); |
| 106 | if area.is_empty() { |
| 107 | return; |
| 108 | } |
| 109 | // One cell is approximately twice as tall as wide. Fit without changing |
| 110 | // the artwork's aspect ratio and never allocate from terminal geometry. |
| 111 | let (columns, pixel_rows) = (usize::from(area.width), usize::from(area.height) * 2); |
| 112 | let (up, down) = if self.width <= columns && self.height <= pixel_rows { |
| 113 | ((columns / self.width).min(pixel_rows / self.height), 1) |
| 114 | } else { |
| 115 | ( |
| 116 | 1, |
| 117 | self.width |
| 118 | .div_ceil(columns) |
| 119 | .max(self.height.div_ceil(pixel_rows)), |
| 120 | ) |
| 121 | }; |
| 122 | let height = (self.height * up).div_ceil(down); |
| 123 | let width = (self.width * up).div_ceil(down) as u16; |
| 124 | let rows = height.div_ceil(2) as u16; |
| 125 | let left = area.x + (area.width - width) / 2; |
| 126 | let top = area.y + (area.height - rows) / 2; |
| 127 | let colored = theme.caps().paints_tokens() && !theme.ascii(); |
| 128 | for y in 0..rows { |
| 129 | for x in 0..width { |
| 130 | let sample = |half: usize| { |
| 131 | let y = usize::from(y) * 2 + half; |
| 132 | if y < height { |
| 133 | self.sample(usize::from(x), y, up, down) |
| 134 | } else { |
| 135 | [0; 4] |
| 136 | } |
| 137 | }; |
| 138 | let a = sample(0); |
| 139 | let b = sample(1); |
| 140 | if a[3] < 40 && b[3] < 40 { |
| 141 | continue; |
| 142 | } |
| 143 | let cell = &mut buf[(left + x, top + y)]; |
| 144 | if !colored { |
| 145 | // A legible one-ink density rendering for NO_COLOR, unknown |
| 146 | // ground and ASCII-safe profiles; no forced palette escapes. |
| 147 | let alpha = u16::from(a[3]) + u16::from(b[3]); |
| 148 | let shade = (u16::from(a[0]) |
| 149 | + u16::from(a[1]) |
| 150 | + u16::from(a[2]) |
| 151 | + u16::from(b[0]) |
| 152 | + u16::from(b[1]) |
| 153 | + u16::from(b[2])) |
| 154 | / 6; |
| 155 | let symbol = if alpha < 160 { |
| 156 | "." |
| 157 | } else if shade > 220 { |
| 158 | ":" |
| 159 | } else if shade > 130 { |
| 160 | "+" |
| 161 | } else { |
| 162 | "#" |
| 163 | }; |
| 164 | cell.set_symbol(symbol).set_style(theme.fg(Role::Accent)); |
| 165 | continue; |
| 166 | } |
| 167 | let bg = color::resolvable_rgb(cell.bg); |
| 168 | let blend = |p: [u8; 4]| { |
| 169 | let rgb = if let Some((r, g, b)) = bg { |
| 170 | let mix = |fg: u8, bg: u8| { |
| 171 | ((u32::from(fg) * u32::from(p[3]) |
| 172 | + u32::from(bg) * (255 - u32::from(p[3]))) |
| 173 | / 255) as u8 |
| 174 | }; |
| 175 | (mix(p[0], r), mix(p[1], g), mix(p[2], b)) |
| 176 | } else { |
| 177 | (p[0], p[1], p[2]) |
| 178 | }; |
| 179 | if theme.caps().depth == ColorDepth::Ansi256 { |
| 180 | Color::Indexed(color::rgb_to_ansi256(rgb.0, rgb.1, rgb.2)) |
| 181 | } else { |
| 182 | Color::Rgb(rgb.0, rgb.1, rgb.2) |
| 183 | } |
| 184 | }; |
| 185 | let background = cell.bg; |
| 186 | match (a[3] >= 40, b[3] >= 40) { |
| 187 | (true, true) => { |
| 188 | cell.set_symbol("▀") |
| 189 | .set_style(Style::default().fg(blend(a)).bg(blend(b))); |
| 190 | } |
| 191 | (true, false) => { |
| 192 | cell.set_symbol("▀") |
| 193 | .set_style(Style::default().fg(blend(a)).bg(background)); |
| 194 | } |
| 195 | (false, true) => { |
| 196 | cell.set_symbol("▄") |
| 197 | .set_style(Style::default().fg(blend(b)).bg(background)); |
| 198 | } |
| 199 | _ => {} |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 |