1
1
mirror of https://github.com/wez/wezterm.git synced 2024-11-27 12:23:46 +03:00

add some structs for holding image data

This commit is contained in:
Wez Furlong 2018-08-06 21:54:17 -07:00
parent 2f8ffab213
commit 5d27265b7c
4 changed files with 132 additions and 25 deletions

View File

@ -1,38 +1,43 @@
[package]
authors = ["Wez Furlong"]
name = "termwiz"
version = "0.1.0"
authors = ["Wez Furlong"]
[dependencies]
terminfo = "~0.6"
palette = "~0.4"
regex = "~0.2"
serde = "~1.0"
serde_derive = "~1.0"
failure = "~0.1"
vte = "~0.3"
num = "~0.2"
num-traits = "~0.2"
derive_builder = "~0.5"
semver = "0.9"
libc = "~0.2"
base64 = "~0.9"
bitflags = "~1.0"
cassowary = "~0.3"
memmem = "~0.1"
derive_builder = "~0.5"
failure = "~0.1"
fnv = "~1.0"
unicode-segmentation = "~1.2"
smallvec = "~0.6"
unicode-width = "~0.1"
base64 = "~0.9"
image = "~0.19"
libc = "~0.2"
memmem = "~0.1"
num = "~0.2"
num-traits = "~0.2"
ordered-float = "~0.5"
palette = "~0.4"
regex = "~0.2"
semver = "0.9"
serde = "~1.0"
serde_derive = "~1.0"
smallvec = "~0.6"
terminfo = "~0.6"
unicode-segmentation = "~1.2"
unicode-width = "~0.1"
vte = "~0.3"
[dependencies.num-derive]
version = "~0.2"
features = ["full-syntax"]
[target.'cfg(windows)'.dependencies]
winapi = {version = "~0.3", features = ["winuser", "consoleapi", "handleapi", "fileapi"]}
[target.'cfg(unix)'.dependencies]
termios = "~0.3"
version = "~0.2"
[target."cfg(unix)".dependencies]
signal-hook = "~0.1"
termios = "~0.3"
[target."cfg(windows)".dependencies.winapi]
features = [
"winuser",
"consoleapi",
"handleapi",
"fileapi",
]
version = "~0.3"

View File

@ -1,6 +1,7 @@
//! Model a cell in the terminal display
use color::ColorAttribute;
pub use escape::osc::Hyperlink;
use image::ImageCell;
use smallvec::SmallVec;
use std;
use std::mem;
@ -21,6 +22,8 @@ pub struct CellAttributes {
pub background: ColorAttribute,
/// The hyperlink content, if any
pub hyperlink: Option<Rc<Hyperlink>>,
/// The image data, if any
pub image: Option<Box<ImageCell>>,
}
/// Define getter and setter for the attributes bitfield.
@ -168,6 +171,7 @@ impl CellAttributes {
foreground: self.foreground,
background: self.background,
hyperlink: None,
image: None,
}
}
}

96
termwiz/src/image.rs Normal file
View File

@ -0,0 +1,96 @@
//! Images.
//! This module has some helpers for modeling terminal cells that are filled
//! with image data.
//! We're targeting the iTerm image protocol initially, with sixel as an obvious
//! follow up.
// Kitty has an extensive and complex graphics protocol that seems difficult
// to model. Its docs are here:
// <https://github.com/kovidgoyal/kitty/blob/master/docs/graphics-protocol.rst>
// Both iTerm2 and Sixel appear to have semantics that allow replacing the
// contents of a single chararcter cell with image data, whereas the kitty
// protocol appears to track the images out of band as attachments with
// z-order.
use failure::Error;
use image_crate::load_from_memory;
use ordered_float::NotNaN;
use std::rc::Rc;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TextureCoordinate {
x: NotNaN<f32>,
y: NotNaN<f32>,
}
/// Tracks data for displaying an image in the place of the normal cell
/// character data. Since an Image can span multiple cells, we need to logically
/// carve up the image and track each slice of it. Each cell needs to know
/// its "texture coordinates" within that image so that we can render the
/// right slice.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ImageCell {
/// Texture coordinate for the top left of this cell.
/// (0,0) is the top left of the ImageData. (1, 1) is
/// the bottom right.
top_left: TextureCoordinate,
/// Texture coordinates for the bottom right of this cell.
bottom_right: TextureCoordinate,
/// References the underlying image data
data: Rc<ImageData>,
}
static IMAGE_ID: ::std::sync::atomic::AtomicUsize = ::std::sync::atomic::ATOMIC_USIZE_INIT;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ImageData {
id: usize,
/// Width of the image, in pixels
width: usize,
/// Height of the image, in pixels,
height: usize,
/// The image data bytes. Data is SRGBA, 32 bits per pixel
data: Vec<u8>,
}
impl ImageData {
/// Guess the image format from the contained buffer and return the
/// decoded image data.
pub fn load_from_memory(buffer: &[u8]) -> Result<ImageData, Error> {
let img = load_from_memory(buffer)?.to_rgba();
let width = img.width() as usize;
let height = img.height() as usize;
let data = img.into_raw();
Ok(Self::with_raw_data(width, height, data))
}
pub fn with_raw_data(width: usize, height: usize, data: Vec<u8>) -> Self {
let id = IMAGE_ID.fetch_add(1, ::std::sync::atomic::Ordering::Relaxed);
Self {
id,
width,
height,
data,
}
}
#[inline]
pub fn data(&self) -> &[u8] {
&self.data
}
#[inline]
pub fn id(&self) -> usize {
self.id
}
#[inline]
pub fn width(&self) -> usize {
self.width
}
#[inline]
pub fn height(&self) -> usize {
self.height
}
}

View File

@ -59,6 +59,7 @@ extern crate derive_builder;
extern crate bitflags;
extern crate cassowary;
extern crate image as image_crate;
extern crate memmem;
extern crate ordered_float;
extern crate regex;
@ -72,6 +73,7 @@ pub mod cellcluster;
pub mod color;
pub mod escape;
pub mod hyperlink;
pub mod image;
pub mod input;
pub mod istty;
pub mod keymap;