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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
use std::{fs::File, io::BufWriter};
use anyhow::{anyhow, bail};
use camino::{Utf8Path, Utf8PathBuf};
use colorsquash::Squasher;
use gifed::writer::{GifBuilder, ImageBuilder};
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 mut 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
);
match output_path.extension() {
None => {
eprintln!("can't determine output filetype! defaulting to png");
save_png(image, squasher, output_path)
}
Some("png") => save_png(image, squasher, output_path),
Some("gif") => save_gif(image, squasher, output_path),
Some(ext) => {
eprintln!("unknown filetype '{ext}'!\nSupport output types are: GIF, PNG");
std::process::exit(1);
}
}
}
struct Image {
width: usize,
height: usize,
data: Vec<u8>,
}
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 data = vec![0; reader.output_buffer_size()];
let info = reader.next_frame(&mut data)?;
data.resize(info.buffer_size(), 0);
let colors = info.color_type;
match colors {
ColorType::Grayscale | ColorType::GrayscaleAlpha | ColorType::Indexed => {
bail!("colortype {colors:?} not supported")
}
ColorType::Rgba => {
let pixels = info.width as usize * info.height as usize;
// the first RGB is fine, we don't need to touch it
for idx in 1..pixels {
data[idx * 3] = data[idx * 4];
data[idx * 3 + 1] = data[idx * 4 + 1];
data[idx * 3 + 2] = data[idx * 4 + 2];
}
data.resize(pixels * 3, 0);
Ok(Image {
width: info.width as usize,
height: info.height as usize,
data,
})
}
ColorType::Rgb => Ok(Image {
width: info.width as usize,
height: info.height as usize,
data,
}),
}
}
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,
})
}
fn save_png(image: Image, squasher: Squasher<u8>, path: Utf8PathBuf) -> Result<(), anyhow::Error> {
let file = File::create(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 save_gif(image: Image, squasher: Squasher<u8>, path: Utf8PathBuf) -> Result<(), anyhow::Error> {
// I don't think I like this API anymore. It's a builder API, that's fine.
// I should make it so you can mutate the Gif directly.
GifBuilder::new(image.width as u16, image.height as u16)
.palette(squasher.palette_bytes().as_slice().try_into().unwrap())
.image(ImageBuilder::new(image.width as u16, image.height as u16).build(image.data)?)
.build()?
.save(path)?;
Ok(())
}
|