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
use image::{ImageBuffer, Rgba, RgbaImage};
fn sort_widest(pixels: &mut [Rgba<u8>]) {
let r_range =
pixels.iter().map(|p| p[0]).max().unwrap() - pixels.iter().map(|p| p[0]).min().unwrap();
let g_range =
pixels.iter().map(|p| p[1]).max().unwrap() - pixels.iter().map(|p| p[1]).min().unwrap();
let b_range =
pixels.iter().map(|p| p[2]).max().unwrap() - pixels.iter().map(|p| p[2]).min().unwrap();
let a_range =
pixels.iter().map(|p| p[3]).max().unwrap() - pixels.iter().map(|p| p[3]).min().unwrap();
if r_range > g_range && r_range > b_range && r_range > a_range {
pixels.sort_by_key(|p| p[0]);
} else if g_range > b_range && g_range > a_range {
pixels.sort_by_key(|p| p[1]);
} else if b_range > a_range {
pixels.sort_by_key(|p| p[2]);
} else {
pixels.sort_by_key(|p| p[3]);
}
}
pub fn build_palette<I>(n: usize, pixels: I) -> Vec<Rgba<u8>>
where
I: IntoIterator<Item = Rgba<u8>>,
{
let n = n.next_power_of_two();
let mut pixels: Vec<_> = pixels.into_iter().collect();
pixels.sort_by_key(|p| p.data);
pixels.dedup();
if n >= pixels.len() {
return pixels;
}
for i in 0..n.trailing_zeros() {
let i = 1 << i;
for j in 0..i {
let pixels_a = j * pixels.len() / i;
let pixels_b = (j + 1) * pixels.len() / i;
sort_widest(&mut pixels[pixels_a..pixels_b]);
}
}
let mut output = Vec::with_capacity(n);
for i in 0..n {
let pixels_a = i * pixels.len() / n;
let pixels_b = (i + 1) * pixels.len() / n;
let sub_pixels = &pixels[pixels_a..pixels_b];
let avg_r: usize =
sub_pixels.iter().map(|p| p[0] as usize).sum::<usize>() / sub_pixels.len();
let avg_g: usize =
sub_pixels.iter().map(|p| p[1] as usize).sum::<usize>() / sub_pixels.len();
let avg_b: usize =
sub_pixels.iter().map(|p| p[2] as usize).sum::<usize>() / sub_pixels.len();
let avg_a: usize =
sub_pixels.iter().map(|p| p[3] as usize).sum::<usize>() / sub_pixels.len();
output.push(Rgba {
data: [avg_r as u8, avg_g as u8, avg_b as u8, avg_a as u8],
});
}
output
}
pub fn quantize(image: &mut RgbaImage, palette: &[Rgba<u8>]) {
let mut fimage: ImageBuffer<Rgba<f32>, Vec<f32>> = ImageBuffer::from_raw(
image.width(),
image.height(),
vec![0.0; 4 * image.width() as usize * image.height() as usize],
)
.unwrap();
for x in 0..image.width() {
for y in 0..image.height() {
let [r, g, b, a] = image.get_pixel(x, y).data;
fimage.put_pixel(
x,
y,
Rgba {
data: [
r as f32 / 255.0,
g as f32 / 255.0,
b as f32 / 255.0,
a as f32 / 255.0,
],
},
);
}
}
let palette: Vec<Rgba<f32>> = palette
.iter()
.map(|p| {
let [r, g, b, a] = p.data;
Rgba {
data: [
r as f32 / 255.0,
g as f32 / 255.0,
b as f32 / 255.0,
a as f32 / 255.0,
],
}
})
.collect();
for x in 0..fimage.width() {
for y in 0..image.height() {
let old_pixel = fimage.get_pixel(x, y);
let [or, og, ob, oa] = old_pixel.data;
let new_pixel = palette
.iter()
.min_by_key(|p| {
let [nr, ng, nb, na] = p.data;
(((nr - or).powi(2)
+ (ng - og).powi(2)
+ (nb - ob).powi(2)
+ (na - oa).powi(2))
* 1000000.0) as usize
})
.unwrap();
let [nr, ng, nb, na] = new_pixel.data;
let er = or - nr;
let eg = og - ng;
let eb = ob - nb;
let ea = oa - na;
if x < fimage.width() - 1 {
let [hr, hg, hb, ha] = &mut fimage.get_pixel_mut(x + 1, y).data;
*hr += er * 0.4375;
*hg += eg * 0.4375;
*hb += eb * 0.4375;
*ha += ea * 0.4375;
}
if y < fimage.height() - 1 {
let [hr, hg, hb, ha] = &mut fimage.get_pixel_mut(x, y + 1).data;
*hr += er * 0.1875;
*hg += eg * 0.1875;
*hb += eb * 0.1875;
*ha += ea * 0.1875;
if x < fimage.width() - 1 {
let [hr, hg, hb, ha] = &mut fimage.get_pixel_mut(x + 1, y + 1).data;
*hr += er * 0.3125;
*hg += eg * 0.3125;
*hb += eb * 0.3125;
*ha += ea * 0.3125;
}
if x > 0 {
let [hr, hg, hb, ha] = &mut fimage.get_pixel_mut(x - 1, y + 1).data;
*hr += er * 0.0625;
*hg += eg * 0.0625;
*hb += eb * 0.0625;
*ha += ea * 0.0625;
}
}
fimage.put_pixel(x, y, *new_pixel);
image.put_pixel(
x,
y,
Rgba {
data: [
(nr * 255.0) as u8,
(ng * 255.0) as u8,
(nb * 255.0) as u8,
(na * 255.0) as u8,
],
},
);
}
}
}