Skip to content

Commit 1d9b00b

Browse files
committed
Clippy
1 parent 39cb27e commit 1d9b00b

File tree

7 files changed

+27
-27
lines changed

7 files changed

+27
-27
lines changed

src/attr.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,14 @@ impl Attributes {
218218
///
219219
/// To share data with the callback, use `Arc` or `Atomic*` types and `move ||` closures.
220220
#[inline]
221-
pub fn set_log_callback<F: Fn(&Attributes, &str) + Send + Sync + 'static>(&mut self, callback: F) {
221+
pub fn set_log_callback<F: Fn(&Self, &str) + Send + Sync + 'static>(&mut self, callback: F) {
222222
self.verbose_printf_flush();
223223
self.log_callback = Some(Arc::new(callback));
224224
}
225225

226226
/// Callback for flushing output (if you buffer messages, that's the time to flush those buffers)
227227
#[inline]
228-
pub fn set_log_flush_callback<F: Fn(&Attributes) + Send + Sync + 'static>(&mut self, callback: F) {
228+
pub fn set_log_flush_callback<F: Fn(&Self) + Send + Sync + 'static>(&mut self, callback: F) {
229229
self.verbose_printf_flush();
230230
self.log_flush_callback = Some(Arc::new(callback));
231231
}
@@ -250,7 +250,7 @@ impl Attributes {
250250
// true == abort
251251
#[inline]
252252
#[must_use]
253-
pub(crate) fn progress(self: &Attributes, percent: f32) -> bool {
253+
pub(crate) fn progress(&self, percent: f32) -> bool {
254254
if let Some(f) = &self.progress_callback {
255255
f(percent) == ControlFlow::Break
256256
} else {
@@ -259,7 +259,7 @@ impl Attributes {
259259
}
260260

261261
#[inline(always)]
262-
pub(crate) fn verbose_print(self: &Attributes, msg: impl AsRef<str>) {
262+
pub(crate) fn verbose_print(&self, msg: impl AsRef<str>) {
263263
fn _print(a: &Attributes, msg: &str) {
264264
if let Some(f) = &a.log_callback {
265265
f(a, msg);
@@ -269,7 +269,7 @@ impl Attributes {
269269
}
270270

271271
#[inline]
272-
pub(crate) fn verbose_printf_flush(self: &Attributes) {
272+
pub(crate) fn verbose_printf_flush(&self) {
273273
if let Some(f) = &self.log_flush_callback {
274274
f(self);
275275
}
@@ -343,8 +343,8 @@ impl Drop for Attributes {
343343

344344
impl Default for Attributes {
345345
#[inline(always)]
346-
fn default() -> Attributes {
347-
Attributes::new()
346+
fn default() -> Self {
347+
Self::new()
348348
}
349349
}
350350

src/image.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ impl<'pixels> Image<'pixels> {
193193
/// Pixels that match the background color will be made transparent if there's a fully transparent color available in the palette.
194194
///
195195
/// The background image's pixels must outlive this image.
196-
pub fn set_background(&mut self, background: Image<'pixels>) -> Result<(), Error> {
196+
pub fn set_background(&mut self, background: Self) -> Result<(), Error> {
197197
if background.background.is_some() {
198198
return Err(Unsupported);
199199
}
@@ -282,12 +282,12 @@ impl<'pixels> Image<'pixels> {
282282
let horiz = horiz.a.max(horiz.r).max(horiz.g.max(horiz.b));
283283
let vert = vert.a.max(vert.r).max(vert.g.max(vert.b));
284284
let edge = horiz.max(vert);
285-
let mut z = edge - (horiz - vert).abs() * 0.5;
285+
let mut z = (horiz - vert).abs().mul_add(-0.5, edge);
286286
z = 1. - z.max(horiz.min(vert));
287287
z *= z;
288288
z *= z;
289289
// 85 is about 1/3rd of weight (not 0, because noisy pixels still need to be included, just not as precisely).
290-
noise_row[i] = (80. + z * 176.) as u8;
290+
noise_row[i] = z.mul_add(176., 80.) as u8;
291291
edges_row[i] = ((1. - edge) * 256.) as u8;
292292
}
293293
}

src/kmeans.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@ impl Kmeans {
6767
// chunk size is a trade-off between parallelization and overhead
6868
hist.items.par_chunks_mut(256).for_each({
6969
let tls = &tls; move |batch| {
70-
let kmeans = tls.get_or(move || CacheLineAlign(RefCell::new(Kmeans::new(len))));
70+
let kmeans = tls.get_or(move || CacheLineAlign(RefCell::new(Self::new(len))));
7171
if let Ok(ref mut kmeans) = *kmeans.0.borrow_mut() {
7272
kmeans.iterate_batch(batch, &n, colors, adjust_weight);
7373
}
7474
}});
7575

7676
let diff = tls.into_iter()
7777
.map(|c| c.0.into_inner())
78-
.reduce(Kmeans::try_merge)
78+
.reduce(Self::try_merge)
7979
.transpose()?
8080
.map_or(0., |kmeans| {
8181
kmeans.finalize(palette) / total
@@ -94,7 +94,7 @@ impl Kmeans {
9494
let remapped = colors[matched as usize];
9595
let (_, new_diff) = n.search(&f_pixel(px.0 + px.0 - remapped.0), matched);
9696
diff = new_diff;
97-
item.adjusted_weight = (item.perceptual_weight + 2. * item.adjusted_weight) * (0.5 + diff);
97+
item.adjusted_weight = 2.0f32.mul_add(item.adjusted_weight, item.perceptual_weight) * (0.5 + diff);
9898
}
9999
debug_assert!(f64::from(diff) < 1e20);
100100
self.update_color(px, item.adjusted_weight, matched);
@@ -103,7 +103,7 @@ impl Kmeans {
103103
}
104104

105105
#[inline]
106-
pub fn merge(mut self, new: Kmeans) -> Kmeans {
106+
pub fn merge(mut self, new: Self) -> Self {
107107
self.weighed_diff_sum += new.weighed_diff_sum;
108108
self.averages.iter_mut().zip(new.averages).for_each(|(p, n)| {
109109
p.sum += n.sum;
@@ -115,7 +115,7 @@ impl Kmeans {
115115
#[inline]
116116
pub fn try_merge<E>(old: Result<Self, E>, new: Result<Self, E>) -> Result<Self, E> {
117117
match (old, new) {
118-
(Ok(old), Ok(new)) => Ok(Kmeans::merge(old, new)),
118+
(Ok(old), Ok(new)) => Ok(Self::merge(old, new)),
119119
(Err(e), _) | (_, Err(e)) => Err(e),
120120
}
121121
}

src/mediancut.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ impl<'hist> MedianCutter<'hist> {
279279
// first splits boxes that exceed quality limit (to have colors for things like odd green pixel),
280280
// later raises the limit to allow large smooth areas/gradients get colors.
281281
let fraction_done = self.boxes.len() as f64 / f64::from(self.target_colors);
282-
let current_max_mse = max_mse + fraction_done * 16. * max_mse;
282+
let current_max_mse = (fraction_done * 16.).mul_add(max_mse, max_mse);
283283
let bi = match self.take_best_splittable_box(current_max_mse) {
284284
Some(bi) => bi,
285285
None => break,

src/pal.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ impl PalF {
255255
}
256256

257257
// this is max colors allowed by the user, not just max in the current (candidate/low-quality) palette
258-
pub(crate) fn with_fixed_colors(mut self, max_colors: PalLen, fixed_colors: &[f_pixel]) -> PalF {
258+
pub(crate) fn with_fixed_colors(mut self, max_colors: PalLen, fixed_colors: &[f_pixel]) -> Self {
259259
if fixed_colors.is_empty() {
260260
return self;
261261
}

src/quant.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl QuantizationResult {
3636
if freeze_result_colors {
3737
palette.iter_mut().for_each(|(_, p)| *p = p.to_fixed());
3838
}
39-
if attr.progress(f32::from(attr.progress_stage1) + f32::from(attr.progress_stage2) + f32::from(attr.progress_stage3) * 0.95) {
39+
if attr.progress(f32::from(attr.progress_stage3).mul_add(0.95, f32::from(attr.progress_stage1) + f32::from(attr.progress_stage2))) {
4040
return Err(Aborted);
4141
}
4242
if let (Some(palette_error), Some(max_mse)) = (palette_error, max_mse) {
@@ -377,8 +377,8 @@ pub(crate) fn find_best_palette(attr: &Attributes, target_mse: f64, target_mse_i
377377
let mut new_palette = mediancut(&mut hist, max_colors, target_mse * target_mse_overshoot, max_mse_per_color)?
378378
.with_fixed_colors(attr.max_colors, &hist.fixed_colors);
379379

380-
let stage_done = 1. - (f32::from(trials_left.max(0)) / f32::from(total_trials + 1)).powi(2);
381-
let overall_done = f32::from(attr.progress_stage1) + stage_done * f32::from(attr.progress_stage2);
380+
let stage_done = (f32::from(trials_left.max(0)) / f32::from(total_trials + 1)).mul_add(-(f32::from(trials_left.max(0)) / f32::from(total_trials + 1)), 1.);
381+
let overall_done = stage_done.mul_add(f32::from(attr.progress_stage2), f32::from(attr.progress_stage1));
382382
attr.verbose_print(format!(" selecting colors...{}%", (100. * stage_done) as u8));
383383

384384
if trials_left <= 0 { break Some(new_palette); }
@@ -417,7 +417,7 @@ fn refine_palette(palette: &mut PalF, attr: &Attributes, hist: &mut HistogramInt
417417
let mut i = 0;
418418
while i < iterations {
419419
let stage_done = f32::from(i) / f32::from(iterations);
420-
let overall_done = f32::from(attr.progress_stage1) + f32::from(attr.progress_stage2) + stage_done * f32::from(attr.progress_stage3) * 0.89;
420+
let overall_done = (stage_done * f32::from(attr.progress_stage3)).mul_add(0.89, f32::from(attr.progress_stage1) + f32::from(attr.progress_stage2));
421421
if attr.progress(overall_done) {
422422
break;
423423
}

src/remap.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ pub(crate) fn remap_to_palette<'x, 'b: 'x>(px: &mut DynamicRows, background: Opt
107107
fn get_dithered_pixel(dither_level: f32, max_dither_error: f32, thiserr: f_pixel, px: f_pixel) -> f_pixel {
108108
let s = thiserr.0 * dither_level;
109109
// This prevents gaudy green pixels popping out of the blue (or red or black! ;)
110-
let dither_error = s.r * s.r + s.g * s.g + s.b * s.b + s.a * s.a;
110+
let dither_error = s.r.mul_add(s.r, s.g * s.g) + s.b.mul_add(s.b, s.a * s.a);
111111
if dither_error < 2. / 256. / 256. {
112112
// don't dither areas that don't have noticeable error — makes file smaller
113113
return px;
@@ -137,9 +137,9 @@ fn get_dithered_pixel(dither_level: f32, max_dither_error: f32, thiserr: f_pixel
137137
}
138138
f_pixel(ARGBF {
139139
a: (px.a + s.a).clamp(0., 1.),
140-
r: px.r + s.r * ratio,
141-
g: px.g + s.g * ratio,
142-
b: px.b + s.b * ratio,
140+
r: s.r.mul_add(ratio, px.r),
141+
g: s.g.mul_add(ratio, px.g),
142+
b: s.b.mul_add(ratio, px.b),
143143
})
144144
}
145145

@@ -174,7 +174,7 @@ pub(crate) fn remap_to_palette_floyd(input_image: &mut Image, mut output_pixels:
174174
background = None;
175175
}
176176
// response to this value is non-linear and without it any value < 0.8 would give almost no dithering
177-
let mut base_dithering_level = (1. - (1. - quant.dither_level) * (1. - quant.dither_level)) * (15. / 16.); // prevent small errors from accumulating
177+
let mut base_dithering_level = (1. - quant.dither_level).mul_add(-(1. - quant.dither_level), 1.) * (15. / 16.); // prevent small errors from accumulating
178178
if !dither_map.is_empty() {
179179
base_dithering_level *= 1. / 255.; // dither_map is in 0-255 scale
180180
}
@@ -304,7 +304,7 @@ fn dither_row(row_pixels: &[f_pixel], output_pixels_row: &mut [MaybeUninit<PalIn
304304
output_pixels_row[col].write(matched);
305305
let mut err = spx.0 - output_px.0;
306306
// This prevents weird green pixels popping out of the blue (or red or black! ;)
307-
if err.r * err.r + err.g * err.g + err.b * err.b + err.a * err.a > max_dither_error {
307+
if err.r.mul_add(err.r, err.g * err.g) + err.b.mul_add(err.b, err.a * err.a) > max_dither_error {
308308
err *= 0.75;
309309
}
310310
if even_row {

0 commit comments

Comments
 (0)