Skip to content

Use more color than 16 in sixel graphics #271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@CephalonRho Can you confirm that these are the possible values that should be used?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any number above 0 should work. These seem to be chosen arbitrarily, but they are values that should provide a reasonable looking output.
A solution using a custom validator would be perfect, but it's probably not worth the effort.

.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