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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
use std::{fs::File, io::BufWriter};
use anyhow::{anyhow, bail};
use camino::{Utf8Path, Utf8PathBuf};
use colorsquash::Squasher;
use png::{ColorType, Decoder, Encoder};
use zune_jpeg::{zune_core::colorspace::ColorSpace, JpegDecoder};
fn main() -> Result<(), anyhow::Error> {
// I should use clap or at least getopt, but this is fine. It's 20LOC.
let usage = || -> ! {
println!("usage: squash <color count> <input> <output>");
std::process::exit(0);
};
let mut argv = std::env::args().skip(1);
let color_count: u8 = if let Some(Ok(count)) = argv.next().map(|r| r.parse::<usize>()) {
if count > 256 {
eprintln!("max colour count must be 256 or below");
std::process::exit(1);
} else {
(count - 1) as u8
}
} else {
usage()
};
let input_path: Utf8PathBuf = if let Some(path) = argv.next() {
path.into()
} else {
usage();
};
let output_path: Utf8PathBuf = if let Some(path) = argv.next() {
path.into()
} else {
usage();
};
let mut image = match input_path.extension() {
None => {
eprintln!("can't determine input filetype!\nSupported input types: PNG, JPG");
std::process::exit(1);
}
Some("png") => get_png(input_path)?,
Some("jpg") | Some("jpeg") => get_jpg(input_path)?,
Some(ext) => {
eprintln!("unknown filetype '{ext}'!\nSupported input types: PNG, JPG");
std::process::exit(1);
}
};
let squasher = Squasher::new(color_count, &image.data);
let size = squasher.map_over(&mut image.data);
image.data.resize(size, 0);
println!(
"selected {} colours of max {}",
squasher.palette().len(),
color_count
);
// PNG Output
let file = File::create(output_path)?;
let bufw = BufWriter::new(file);
let mut enc = Encoder::new(bufw, image.width as u32, image.height as u32);
enc.set_color(ColorType::Indexed);
enc.set_depth(png::BitDepth::Eight);
enc.set_palette(squasher.palette_bytes());
enc.write_header()?.write_image_data(&image.data)?;
Ok(())
}
fn get_png<P: AsRef<Utf8Path>>(path: P) -> Result<Image, anyhow::Error> {
let decoder = Decoder::new(File::open(path.as_ref())?);
let mut reader = decoder.read_info()?;
let mut buf = vec![0; reader.output_buffer_size()];
let info = reader.next_frame(&mut buf)?;
let data = &buf[..info.buffer_size()];
println!(
"{}x{} * 3 = {} | out={}, bs={}",
info.width,
info.height,
info.width as usize * info.height as usize * 3,
buf.len(),
info.buffer_size()
);
let colors = info.color_type;
match colors {
ColorType::Grayscale | ColorType::GrayscaleAlpha | ColorType::Indexed | ColorType::Rgba => {
bail!("colortype {colors:?} not supported")
}
ColorType::Rgb => Ok(Image {
width: info.width as usize,
height: info.height as usize,
data: data.to_vec(),
}),
}
}
fn get_jpg<P: AsRef<Utf8Path>>(path: P) -> Result<Image, anyhow::Error> {
let content = std::fs::read(path.as_ref())?;
let mut dec = JpegDecoder::new(&content);
let pixels = dec.decode()?;
let info = dec
.info()
.ok_or(anyhow!("image had no info; this should be impossible"))?;
let colorspace = dec.get_output_colorspace();
match colorspace {
Some(ColorSpace::RGB) => (),
_ => bail!("colorspace {colorspace:?} not supported"),
}
Ok(Image {
width: info.width as usize,
height: info.height as usize,
data: pixels,
})
}
struct Image {
width: usize,
height: usize,
data: Vec<u8>,
}
|