Skip to main content

Crate a_sixel

Crate a_sixel 

Source
Expand description

A sixel library for encoding images.

§Basic Usage

§Simple Encoding

use a_sixel::PaletteBuilder;
use a_sixel::SixelEncoder;
use a_sixel::dither::Dither;
use image::RgbaImage;

let img = RgbaImage::new(100, 100);
println!(
    "{}",
    SixelEncoder::new(PaletteBuilder::BitMergeBest, Dither::Sierra).encode(img)
);

§Loading and Encoding an Image File

use a_sixel::PaletteBuilder;
use a_sixel::SixelEncoder;
use a_sixel::dither::Dither;

// Load an image from file
let image = image::open("examples/transparent.png").unwrap().to_rgba8();

// Encode with 256 colors and Sierra dithering
let encoder = SixelEncoder::new(PaletteBuilder::KMeans, Dither::Sierra);
let sixel_output = encoder.encode(image);
println!("{}", sixel_output);

§Custom Palette Size and Dithering

use a_sixel::PaletteBuilder;
use a_sixel::SixelEncoder;
use a_sixel::dither::Dither;

let image = image::open("examples/transparent.png").unwrap().to_rgba8();

// Use 16 colors with no dithering for faster encoding
let encoder = SixelEncoder::new(PaletteBuilder::Bit, Dither::None);
let sixel_output = encoder.encode_with_palette_size(image, 16);
println!("{}", sixel_output);

§Transparency

By default, a-sixel handles transparency by setting any fully-transparent pixels to all-bits-zero. This translates to a transparent pixel in most sixel implementations, but some terminals may not support this.

Sixel does not natively support partial transparency, but this library does have some support for rendering images as if partial transparency was supported. If the partial-transparency feature is enabled, a-sixel will query the terminal and attempt to determine the background color. Partially transparent pixels will then be blended with this background color before encoding. Note that with this approach, changing the terminal background color will not update partially transparent pixels to match. You will need to re-encode the image if the background color changes.

§Choosing a PaletteBuilder

For a more detailed breakdown, here’s the encoders by average speed and quality against the test images (speed figures will vary) at 256 colors with Sierra dithering:

AlgorithmMSEDSSIMPHash DistanceMean ΔEMax ΔEΔE >2.3ΔE >5.0Execution Time (ms)
adu15.040.00528.861.7912.8031.8%4.4%1448
bit35.820.013231.143.1611.0364.5%15.1%468
bit-merge-low10.670.003813.971.959.9832.4%2.2%855
bit-merge10.370.003713.481.8910.0331.0%2.2%1034
bit-merge-better10.300.003713.071.8510.2230.6%2.2%1301
bit-merge-best10.280.003713.591.8310.2030.5%2.2%1532
focal31.100.009119.723.348.4173.9%13.1%821
k-means10.070.003613.281.8010.1429.1%2.2%3175
k-medians17.220.006719.102.569.9850.8%4.7%9088
median-cut19.640.005916.452.2410.3642.2%5.9%740
octree54.480.014826.033.8912.4978.6%25.4%754
wu17.890.006821.032.3410.2446.3%5.1%1984

Note: Execution time includes the time taken to compute error statistics - this is non-trivial. For example, exclusive of error statistics computation, bit-no-dither takes <100ms on average. Performance figures will vary based on machine, etc. They are only useful for comparing algorithms against each other within this dataset.

Here’s the encoders at 16 colors with Sierra dithering:

AlgorithmMSEDSSIMPHash DistanceMean ΔEMax ΔEΔE >2.3ΔE >5.0Execution Time (ms)
adu118.900.036440.864.0418.3865.7%33.8%357
bit178.470.049059.795.5316.6189.0%51.4%325
bit-merge-low95.610.030241.593.9716.2667.4%31.4%631
bit-merge94.530.030241.483.9516.1567.0%31.3%800
bit-merge-better96.110.029941.553.9616.1767.9%31.4%1078
bit-merge-best95.440.029741.693.9616.4667.0%31.4%1297
focal345.080.058556.667.2316.6595.6%74.8%433
k-means99.360.030942.833.9916.3966.9%31.4%702
k-medians173.950.053359.625.5716.2390.8%49.7%7255
median-cut164.520.037445.284.6816.7273.7%42.3%395
octree459.370.084575.037.6918.8798.3%73.5%477
wu125.840.038650.524.4816.7074.5%39.2%929

Modules§

aduadu
Use Adaptive Distributive Units to “learn” the image’s color properties and select palette entries.
bit
Encodes a palette by bucketing bit ranges into a power of two number of buckets. This is very fast and produces ok results for most images at larger palette sizes (e.g. 256).
bitmergebit-merge
Uses BitSixelEncoder with k-means and agglomerative merging to build a palette.
dither
A collection of various dithering algorithms that can be used with the SixelEncoder to dither the result.
focalfocal
Spectral-residual peak isolation for palette generation.
kmeansk-means
Use k-means clustering to determine a palette for the image.
kmediansk-medians
Uses k-medians to build a palette of colors.
median_cutmedian-cut
https://en.wikipedia.org/wiki/Median_cut
octreeoctree
Uses an octree to build a palette from an image.
wuwu
Uses Wu’s quantization algorithm to build a palette from an image.

Structs§

SixelEncoder
The main type for performing sixel encoding.

Enums§

PaletteBuilder
The palette building algorithm to use for color quantization.