Skip to content

Commit 302601a

Browse files
committed
Readability
1 parent e3007a7 commit 302601a

File tree

1 file changed

+27
-27
lines changed

1 file changed

+27
-27
lines changed

src/mediancut.rs

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,26 @@ struct MBox<'hist> {
2727

2828
impl<'hist> MBox<'hist> {
2929
pub fn new(hist: &'hist mut [HistItem]) -> Self {
30-
let weight_sum = hist.iter().map(|a| {
31-
debug_assert!(a.adjusted_weight.is_finite());
32-
debug_assert!(a.adjusted_weight > 0.);
33-
f64::from(a.adjusted_weight)
30+
let weight_sum = hist.iter().map(|item| {
31+
debug_assert!(item.adjusted_weight.is_finite());
32+
debug_assert!(item.adjusted_weight > 0.);
33+
f64::from(item.adjusted_weight)
3434
}).sum();
35-
Self::new_c(hist, weight_sum, weighed_average_color(hist))
35+
Self::new_inner(hist, weight_sum, weighed_average_color(hist))
3636
}
3737

38-
fn new_s(hist: &'hist mut [HistItem], adjusted_weight_sum: f64, other_boxes: &[MBox<'_>]) -> Self {
38+
fn from_split(hist: &'hist mut [HistItem], adjusted_weight_sum: f64, other_boxes: &[MBox<'_>]) -> Self {
3939
debug_assert!(!hist.is_empty());
4040
let mut avg_color = weighed_average_color(hist);
4141
// It's possible that an average color will end up being bad for every entry,
4242
// so prefer picking actual colors so that at least one histogram entry will be satisfied.
4343
if (hist.len() < 500 && hist.len() > 2) || Self::is_useless_color(avg_color, hist, other_boxes) {
4444
avg_color = hist.iter().min_by_key(|a| OrdFloat::new(avg_color.diff(&a.color))).map(|a| a.color).unwrap_or_default();
4545
}
46-
Self::new_c(hist, adjusted_weight_sum, avg_color)
46+
Self::new_inner(hist, adjusted_weight_sum, avg_color)
4747
}
4848

49-
fn new_c(hist: &'hist mut [HistItem], adjusted_weight_sum: f64, avg_color: f_pixel) -> Self {
49+
fn new_inner(hist: &'hist mut [HistItem], adjusted_weight_sum: f64, avg_color: f_pixel) -> Self {
5050
let (variance, max_error) = Self::box_stats(hist, avg_color);
5151
Self {
5252
variance,
@@ -72,9 +72,9 @@ impl<'hist> MBox<'hist> {
7272
fn box_stats(hist: &[HistItem], avg_color: f_pixel) -> (ARGBF, f32) {
7373
let mut variance = ARGBF::default();
7474
let mut max_error = 0.;
75-
for a in hist {
76-
variance += (avg_color.0 - a.color.0).map(|c| c * c) * a.adjusted_weight;
77-
let diff = avg_color.diff(&a.color);
75+
for item in hist {
76+
variance += (avg_color.0 - item.color.0).map(|c| c * c) * item.adjusted_weight;
77+
let diff = avg_color.diff(&item.color);
7878
if diff > max_error {
7979
max_error = diff;
8080
}
@@ -103,13 +103,13 @@ impl<'hist> MBox<'hist> {
103103
ChanVariance { chan: 2, variance: vars[2] },
104104
ChanVariance { chan: 3, variance: vars[3] },
105105
];
106-
channels.sort_unstable_by_key(|a| Reverse(OrdFloat::new(a.variance)));
106+
channels.sort_unstable_by_key(|ch| Reverse(OrdFloat::new(ch.variance)));
107107

108-
for a in self.colors.iter_mut() {
109-
let chans: [f32; 4] = rgb::bytemuck::cast(a.color.0);
108+
for item in self.colors.iter_mut() {
109+
let chans: [f32; 4] = rgb::bytemuck::cast(item.color.0);
110110
// Only the first channel really matters. But other channels are included, because when trying median cut
111111
// many times with different histogram weights, I don't want sort randomness to influence the outcome.
112-
a.tmp.mc_sort_value = (u32::from((chans[channels[0].chan] * 65535.) as u16) << 16)
112+
item.tmp.mc_sort_value = (u32::from((chans[channels[0].chan] * 65535.) as u16) << 16)
113113
| u32::from(((chans[channels[2].chan] + chans[channels[1].chan] / 2. + chans[channels[3].chan] / 4.) * 65535.) as u16); // box will be split to make color_weight of each side even
114114
}
115115
}
@@ -142,8 +142,8 @@ impl<'hist> MBox<'hist> {
142142
let left_sum = left.iter().map(|a| f64::from(a.adjusted_weight)).sum();
143143
let right_sum = self.adjusted_weight_sum - left_sum;
144144

145-
[MBox::new_s(left, left_sum, other_boxes),
146-
MBox::new_s(right, right_sum, other_boxes)]
145+
[MBox::from_split(left, left_sum, other_boxes),
146+
MBox::from_split(right, right_sum, other_boxes)]
147147
}
148148
}
149149

@@ -264,12 +264,12 @@ impl<'hist> MedianCutter<'hist> {
264264
fn into_palette(mut self) -> PalF {
265265
let mut palette = PalF::new();
266266

267-
for (i, b) in self.boxes.iter_mut().enumerate() {
268-
b.colors.iter_mut().for_each(move |a| a.tmp.likely_palette_index = i as _);
267+
for (i, mbox) in self.boxes.iter_mut().enumerate() {
268+
mbox.colors.iter_mut().for_each(move |a| a.tmp.likely_palette_index = i as _);
269269

270270
// store total color popularity (perceptual_weight is approximation of it)
271-
let pop = b.colors.iter().map(|a| f64::from(a.perceptual_weight)).sum::<f64>();
272-
palette.push(b.avg_color, PalPop::new(pop as f32));
271+
let pop = mbox.colors.iter().map(|a| f64::from(a.perceptual_weight)).sum::<f64>();
272+
palette.push(mbox.avg_color, PalPop::new(pop as f32));
273273
}
274274
palette
275275
}
@@ -298,12 +298,12 @@ impl<'hist> MedianCutter<'hist> {
298298

299299
fn take_best_splittable_box(&mut self, max_mse: f64) -> Option<MBox<'hist>> {
300300
self.boxes.iter().enumerate()
301-
.filter(|(_, b)| b.colors.len() > 1)
302-
.map(move |(i, b)| {
303-
let cv = b.variance.r.max(b.variance.g).max(b.variance.b);
304-
let mut thissum = b.adjusted_weight_sum * f64::from(cv.max(b.variance.a));
305-
if f64::from(b.max_error) > max_mse {
306-
thissum = thissum * f64::from(b.max_error) / max_mse;
301+
.filter(|(_, mbox)| mbox.colors.len() > 1)
302+
.map(move |(i, mbox)| {
303+
let cv = mbox.variance.r.max(mbox.variance.g).max(mbox.variance.b);
304+
let mut thissum = mbox.adjusted_weight_sum * f64::from(cv.max(mbox.variance.a));
305+
if f64::from(mbox.max_error) > max_mse {
306+
thissum = thissum * f64::from(mbox.max_error) / max_mse;
307307
}
308308
(i, thissum)
309309
})

0 commit comments

Comments
 (0)