about summary refs log tree commit diff
path: root/squash/src/main.rs
blob: 5437ea198c4c6cc4f8b474a60c94175f551cb980 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use cli::DifferenceFn;
use colorsquash::{Squasher, SquasherBuilder};

use crate::cli::{InType, OutType};

mod cli;
mod image;

fn main() -> Result<(), anyhow::Error> {
	//gen: I should use clap or at least getopt, but this is fine.
	//gen: I like experimenting with the cli :)
	let cli = cli::build();

	let mut image = match cli.in_type {
		InType::Png => image::get_png(cli.input)?,
		InType::Jpeg => image::get_jpg(cli.input)?,
	};

	let mut builder = SquasherBuilder::default().max_colors(cli.color_count);

	if let Some(tol) = cli.tolerance {
		builder = builder.tolerance(tol);
	}

	builder = match cli.difference {
		DifferenceFn::Rgb => builder.difference(&colorsquash::difference::rgb_difference),
		DifferenceFn::Redmean => builder.difference(&colorsquash::difference::redmean_difference),
	};

	let mut squasher = builder.build(&image.data);

	let size = squasher.map_over(&mut image.data);
	image.data.resize(size, 0);

	match cli.out_type {
		OutType::Png => image::save_png(image, squasher, cli.output),
		OutType::Gif => image::save_gif(image, squasher, cli.output),
	}
}