#image #sixel #encode #byte #graphics #libsixel #string

bin+lib sixel-bytes

Encode an image with sixel-sys/libsixel

5 releases

0.2.3 Oct 12, 2023
0.2.2 Jul 15, 2023
0.2.1 Jul 12, 2023
0.2.0 Jul 12, 2023
0.1.0 Jul 12, 2023

#309 in Images

Download history 3/week @ 2024-05-27 6/week @ 2024-07-01 33/week @ 2024-07-22

74 downloads per month
Used in 2 crates

MIT license

285KB
142 lines

sixel-bytes

Screenshot

Encode an image with sixel-sys.

⚠️ This is my first crate that uses unsafe and FFI. Please inspect the source code yourself, the crate is very small. PRs are welcome.

To write a sixel to a file, sixel-rs is safer and has more options.

Despite being called sixel-bytes, this crates produces a String.

Examples

Encode a generated image to sixel and print it:

let mut bytes: Vec<u8> = Vec::new();
for x in 0..255 {
    for y in 0..255 {
        bytes.append(&mut vec![x, 0, y]);
    }
}

let data = sixel_bytes::sixel_string(
    &bytes,
    255,
    255,
    sixel_bytes::PixelFormat::RGB888,
    sixel_bytes::DiffusionMethod::Atkinson,
).unwrap();
assert_eq!(&data[..3], "\u{1b}Pq");

Encode an image from the image crate to sixel and print it:

let image = image::io::Reader::open("./assets/Ada.png")
    .unwrap()
    .decode()
    .unwrap()
    .into_rgba8();
let bytes = image.as_raw();

match sixel_bytes::sixel_string(
    bytes,
    image.width() as _,
    image.height() as _,
    sixel_bytes::PixelFormat::RGBA8888,
    sixel_sys::DiffusionMethod::Stucki,
) {
    Err(err) => eprintln!("{err}"),
    Ok(data) => print!("{data}"),
}

Binaries

sixel <path/to/image> uses the image crate to load an image with supported formats, convert to RGBA8888, encode to sixel, and dump the resulting string to stdout. It must be built with the image feature.

test-sixel just generates some 255x255 image with a gradient and dumps it to stdout.

Only certain terminals / terminal emulators have the capability to render sixel graphics. See https://www.arewesixelyet.com/ for a list of programs that support sixels.

Try running xterm with -ti 340.

Features

The image feature is disabled by default but needed for the sixel binary.

Current version: 0.2.2

License: MIT

Dependencies

~5.5MB
~45K SLoC