use std::fmt::Write as _;
use super::base64;
use super::deflate;
use super::render::Image;
pub(crate) fn encode(image: &Image) -> String {
if image.width == 0 || image.height == 0 {
return String::new();
}
let mut rgba = Vec::with_capacity(image.pixels.len() * 4);
for pixel in &image.pixels {
match pixel {
Some((r, g, b)) => rgba.extend_from_slice(&[*r, *g, *b, 255]),
None => rgba.extend_from_slice(&[0, 0, 0, 0]),
}
}
let payload = base64::encode(&deflate::zlib_compress(&rgba));
let mut out = String::new();
let mut chunks = payload.as_bytes().chunks(4096).peekable();
let mut first = true;
while let Some(chunk) = chunks.next() {
let more = u8::from(chunks.peek().is_some());
out.push_str("\x1b_G");
if first {
let _ = write!(
out,
"a=T,f=32,o=z,s={},v={},C=1,q=2,",
image.width, image.height
);
first = false;
}
let _ = write!(out, "m={more};");
out.push_str(std::str::from_utf8(chunk).expect("base64 is ASCII"));
out.push_str("\x1b\\");
}
out
}
#[cfg(test)]
#[path = "tests/kitty_tests.rs"]
mod tests;