Skip to content

Commit

Permalink
Merge pull request #271 from yoichi/sixel-more-color
Browse files Browse the repository at this point in the history
Use more color than 16 in sixel graphics
  • Loading branch information
o2sh authored Oct 23, 2020
2 parents c2883d1 + 3648aaa commit 69306bd
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 4 deletions.
14 changes: 14 additions & 0 deletions src/onefetch/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub struct Cli {
pub no_bold: bool,
pub image: Option<DynamicImage>,
pub image_backend: Option<Box<dyn image_backends::ImageBackend>>,
pub image_colors: usize,
pub no_merges: bool,
pub no_color_blocks: bool,
pub number_of_authors: usize,
Expand Down Expand Up @@ -141,6 +142,16 @@ impl Cli {
.possible_values(&possible_backends)
.help("Which image BACKEND to use."),
)
.arg(
Arg::with_name("image-colors")
.long("image-colors")
.value_name("NUM")
.takes_value(true)
.max_values(1)
.possible_values(&["16", "32", "64", "128", "256"])
.default_value("16")
.help("NUM of colors [16, 32, 64, 128, 256] to use in image backend."),
)
.arg(
Arg::with_name("no-merge-commits")
.long("no-merge-commits")
Expand Down Expand Up @@ -215,6 +226,8 @@ impl Cli {
None
};

let image_colors: usize = matches.value_of("image-colors").unwrap().parse().unwrap();

let path = String::from(matches.value_of("input").unwrap());

let ascii_input = matches.value_of("ascii-input").map(String::from);
Expand Down Expand Up @@ -254,6 +267,7 @@ impl Cli {
no_bold,
image,
image_backend,
image_colors,
no_merges,
no_color_blocks,
number_of_authors,
Expand Down
1 change: 1 addition & 0 deletions src/onefetch/cli_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ impl<W: Write> Printer<W> {
buf.push_str(&image_backend.add_image(
info_lines.map(|s| format!("{}{}", center_pad, s)).collect(),
custom_image,
self.info.config.image_colors
));
} else {
panic!("No image backend found")
Expand Down
2 changes: 1 addition & 1 deletion src/onefetch/image_backends/kitty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl KittyBackend {
}

impl super::ImageBackend for KittyBackend {
fn add_image(&self, lines: Vec<String>, image: &DynamicImage) -> String {
fn add_image(&self, lines: Vec<String>, image: &DynamicImage, _colors: usize) -> String {
let tty_size = unsafe {
let tty_size: winsize = std::mem::zeroed();
ioctl(STDOUT_FILENO, TIOCGWINSZ, &tty_size);
Expand Down
2 changes: 1 addition & 1 deletion src/onefetch/image_backends/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub mod kitty;
pub mod sixel;

pub trait ImageBackend {
fn add_image(&self, lines: Vec<String>, image: &DynamicImage) -> String;
fn add_image(&self, lines: Vec<String>, image: &DynamicImage, colors: usize) -> String;
}

#[cfg(not(windows))]
Expand Down
4 changes: 2 additions & 2 deletions src/onefetch/image_backends/sixel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl SixelBackend {
}

impl super::ImageBackend for SixelBackend {
fn add_image(&self, lines: Vec<String>, image: &DynamicImage) -> String {
fn add_image(&self, lines: Vec<String>, image: &DynamicImage, colors: usize) -> String {
let tty_size = unsafe {
let tty_size: winsize = std::mem::zeroed();
ioctl(STDOUT_FILENO, TIOCGWINSZ, &tty_size);
Expand All @@ -97,7 +97,7 @@ impl super::ImageBackend for SixelBackend {
// reduce the amount of colors using dithering
colorops::dither(
&mut rgba_image,
&NeuQuant::new(10, 16, flat_samples.image_slice().unwrap()),
&NeuQuant::new(10, colors, flat_samples.image_slice().unwrap()),
);
let rgb_image = ImageBuffer::from_fn(rgba_image.width(), rgba_image.height(), |x, y| {
let rgba_pixel = rgba_image.get_pixel(x, y);
Expand Down

0 comments on commit 69306bd

Please sign in to comment.