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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
use std::collections::{HashMap, HashSet};
#[cfg(not(feature = "simd-kmeans"))]
use crate::nih_kmeans::KMeans;
#[cfg(feature = "simd-kmeans")]
use kmeans::{KMeans, KMeansConfig};
use rgb::RGB8;
use crate::{
difference::{self, DiffFn},
ImageData,
};
pub trait Selector {
// wanted Into<ImageData> here but rustc got mad about vtable building
// because we store this as Box<dyn Selector> in Squasher and it's builder
fn select(&mut self, max_colors: usize, image: ImageData) -> Vec<RGB8>;
}
pub struct SortSelect {
tolerance: f32,
difference_fn: Box<DiffFn>,
}
impl Selector for SortSelect {
/// Pick the colors in the palette from a Vec of colors sorted by number
/// of times they occur, high to low.
fn select(&mut self, max_colours: usize, image: ImageData) -> Vec<RGB8> {
let sorted = Self::unique_and_sort(image);
let tolerance = (self.tolerance / 100.0) * 765.0;
let mut selected_colors: Vec<RGB8> = Vec::with_capacity(max_colours);
for sorted_color in sorted {
if max_colours <= selected_colors.len() {
break;
} else if selected_colors.iter().all(|selected_color| {
(self.difference_fn)(selected_color, &sorted_color) > tolerance
}) {
selected_colors.push(sorted_color);
}
}
selected_colors
}
}
impl SortSelect {
/// How different colours have to be to enter the palette. Should be between
/// 0.0 and 100.0, but is unchecked.
pub fn tolerance(mut self, percent: f32) -> Self {
self.tolerance = percent;
self
}
/// The function to use to compare colours while selecting the palette.
///
/// see the [difference] module for functions included with the crate and
/// information on implementing your own.
pub fn difference(mut self, diff_fn: &'static DiffFn) -> Self {
self.difference_fn = Box::new(diff_fn);
self
}
/// Takes an image buffer of RGB data and fill the color map
fn unique_and_sort<'a, Img>(buffer: Img) -> Vec<RGB8>
where
Img: Into<ImageData<'a>>,
{
let ImageData(rgb) = buffer.into();
let mut colors: HashMap<RGB8, usize> = HashMap::default();
//count pixels
for px in rgb {
match colors.get_mut(px) {
None => {
colors.insert(*px, 1);
}
Some(n) => *n += 1,
}
}
Self::sort(colors)
}
fn sort(map: HashMap<RGB8, usize>) -> Vec<RGB8> {
let mut sorted: Vec<(RGB8, usize)> = map.into_iter().collect();
sorted.sort_by(|(colour1, freq1), (colour2, freq2)| {
freq2
.cmp(freq1)
.then(colour2.r.cmp(&colour1.r))
.then(colour2.g.cmp(&colour1.g))
.then(colour2.b.cmp(&colour1.b))
});
sorted.into_iter().map(|(color, _count)| color).collect()
}
}
impl Default for SortSelect {
fn default() -> Self {
Self {
tolerance: 3.0,
difference_fn: Box::new(difference::rgb),
}
}
}
#[derive(Debug, Default)]
pub struct Kmeans {
pub max_iter: usize,
}
#[cfg(not(feature = "simd-kmeans"))]
impl Selector for Kmeans {
fn select(&mut self, max_colors: usize, image: ImageData) -> Vec<RGB8> {
let ImageData(rgb) = image;
let kmean = KMeans::new(rgb.to_vec());
kmean.get_k_colors(max_colors, self.max_iter)
}
}
#[cfg(feature = "simd-kmeans")]
impl Selector for Kmeans {
fn select(&mut self, max_colors: usize, image: ImageData) -> Vec<RGB8> {
use rgb::ComponentBytes;
let ImageData(rgb) = image;
let kmean = KMeans::new(
rgb.as_bytes()
.iter()
.map(|u| *u as f32)
.collect::<Vec<f32>>(),
rgb.as_bytes().len() / 3,
3,
);
let result = kmean.kmeans_lloyd(
max_colors,
self.max_iter,
KMeans::init_kmeanplusplus,
&KMeansConfig::default(),
);
result
.centroids
.chunks_exact(3)
.map(|rgb| {
RGB8::new(
rgb[0].round() as u8,
rgb[1].round() as u8,
rgb[2].round() as u8,
)
})
.collect()
}
}
pub struct HeuristicSorsel {
tolerance: f32,
variance: f32,
max_attempts: usize,
difference_fn: Box<DiffFn>,
}
impl Selector for HeuristicSorsel {
/// Pick the colors in the palette from a Vec of colors sorted by number
/// of times they occur, high to low.
fn select(&mut self, max_colours: usize, image: ImageData) -> Vec<RGB8> {
let colors = Self::unique_and_sort(image);
let mut best = RunData {
score: f32::MAX,
palette: vec![],
};
let mut attempts = 0;
//let mut current_tolerance = 9.0 - (max_colours as f32).log2();
let mut current_tolerance = self.tolerance;
let mut current_variance = self.variance;
while attempts < self.max_attempts {
attempts += 1;
let higher = current_tolerance + current_variance;
let lower = current_tolerance - current_variance;
let run_up = Self::compute_once(&colors, max_colours, higher, &self.difference_fn);
let run_down = Self::compute_once(&colors, max_colours, lower, &self.difference_fn);
if run_up.score >= best.score && run_down.score >= best.score {
// neither was better than the previous best. can we cut the
// variance to try and fine tune?
if current_variance > 0.01 {
// Yes, cut it in half and run again.
current_variance /= 2.0;
} else {
// No, we've reached our limit. Break from the loop
break;
}
} else if run_up.score < run_down.score {
current_tolerance = higher;
best = run_up;
} else {
current_tolerance = lower;
best = run_down;
}
}
println!("final tolerance {:.2}", current_tolerance);
best.palette
}
}
struct RunData {
palette: Vec<RGB8>,
score: f32,
}
impl HeuristicSorsel {
fn compute_once(
colors: &[(RGB8, usize)],
max_colours: usize,
tolerance: f32,
diff_fn: &DiffFn,
) -> RunData {
let tolerance = (tolerance / 100.0) * 765.0;
let mut selected_colors: Vec<RGB8> = Vec::with_capacity(max_colours);
for (sorted_color, _) in colors {
if max_colours <= selected_colors.len() {
break;
} else if selected_colors
.iter()
.all(|selected_color| (diff_fn)(selected_color, sorted_color) > tolerance)
{
selected_colors.push(*sorted_color);
}
}
// Calculate a score for this tolerance. The total score is the sum of
// the color scores. The color score is the number of times that colour
// occures multiplied with the least difference.
let mut score = 0.0;
for (color, count) in colors {
let mut min_diff = f32::MAX;
for selected in &selected_colors {
let diff = (diff_fn)(selected, color);
if diff.max(0.0) < min_diff {
min_diff = diff;
}
}
score += min_diff * (*count as f32);
}
RunData {
palette: selected_colors,
score,
}
}
pub fn tolerance(mut self, tolerance: f32) -> Self {
self.tolerance = tolerance;
self
}
pub fn variance(mut self, vary: f32) -> Self {
self.variance = vary;
self
}
pub fn max_attempts(mut self, count: usize) -> Self {
self.max_attempts = count;
self
}
/// The function to use to compare colours while selecting the palette.
///
/// see the [difference] module for functions included with the crate and
/// information on implementing your own.
pub fn difference(mut self, diff_fn: &'static DiffFn) -> Self {
self.difference_fn = Box::new(diff_fn);
self
}
/// Takes an image buffer of RGB data and fill the color map
fn unique_and_sort<'a, Img>(buffer: Img) -> Vec<(RGB8, usize)>
where
Img: Into<ImageData<'a>>,
{
let ImageData(rgb) = buffer.into();
let mut colors: HashMap<RGB8, usize> = HashMap::default();
//count pixels
for px in rgb {
match colors.get_mut(px) {
None => {
colors.insert(*px, 1);
}
Some(n) => *n += 1,
}
}
Self::sort(colors)
}
fn sort(map: HashMap<RGB8, usize>) -> Vec<(RGB8, usize)> {
let mut sorted: Vec<(RGB8, usize)> = map.into_iter().collect();
sorted.sort_by(|(colour1, freq1), (colour2, freq2)| {
freq2
.cmp(freq1)
.then(colour2.r.cmp(&colour1.r))
.then(colour2.g.cmp(&colour1.g))
.then(colour2.b.cmp(&colour1.b))
});
sorted.into_iter().collect()
}
}
impl Default for HeuristicSorsel {
fn default() -> Self {
Self {
tolerance: 3.0,
variance: 0.25,
max_attempts: 10,
difference_fn: Box::new(difference::rgb),
}
}
}
pub struct HighestBits {}
impl Selector for HighestBits {
fn select(&mut self, max_colors: usize, image: ImageData) -> Vec<RGB8> {
let bits = max_colors.next_power_of_two().ilog2();
let leftover = bits % 3;
let shift = 8 - (bits / 3);
//TODO: gen- we're taking red/green here because, as i remember, they
// are the colours to which we are most sensetive? but it would be cool
// if this was selectable
let (rshift, gshift, bshift) = match leftover {
0 => (shift, shift, shift),
1 => (shift, shift - 1, shift),
2 => (shift - 1, shift - 1, shift),
_ => unreachable!(),
};
image
.0
.iter()
.map(|color| {
RGB8::new(
color.r >> rshift << rshift,
color.g >> gshift << gshift,
color.b >> bshift << bshift,
)
})
.collect::<HashSet<_>>()
.into_iter()
.collect()
}
}
|