Skip to content

Commit

Permalink
Clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Apr 27, 2024
1 parent 39cb27e commit 1d9b00b
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 27 deletions.
14 changes: 7 additions & 7 deletions src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,14 @@ impl Attributes {
///
/// To share data with the callback, use `Arc` or `Atomic*` types and `move ||` closures.
#[inline]
pub fn set_log_callback<F: Fn(&Attributes, &str) + Send + Sync + 'static>(&mut self, callback: F) {
pub fn set_log_callback<F: Fn(&Self, &str) + Send + Sync + 'static>(&mut self, callback: F) {
self.verbose_printf_flush();
self.log_callback = Some(Arc::new(callback));
}

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

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

#[inline]
pub(crate) fn verbose_printf_flush(self: &Attributes) {
pub(crate) fn verbose_printf_flush(&self) {
if let Some(f) = &self.log_flush_callback {
f(self);
}
Expand Down Expand Up @@ -343,8 +343,8 @@ impl Drop for Attributes {

impl Default for Attributes {
#[inline(always)]
fn default() -> Attributes {
Attributes::new()
fn default() -> Self {
Self::new()
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ impl<'pixels> Image<'pixels> {
/// Pixels that match the background color will be made transparent if there's a fully transparent color available in the palette.
///
/// The background image's pixels must outlive this image.
pub fn set_background(&mut self, background: Image<'pixels>) -> Result<(), Error> {
pub fn set_background(&mut self, background: Self) -> Result<(), Error> {
if background.background.is_some() {
return Err(Unsupported);
}
Expand Down Expand Up @@ -282,12 +282,12 @@ impl<'pixels> Image<'pixels> {
let horiz = horiz.a.max(horiz.r).max(horiz.g.max(horiz.b));
let vert = vert.a.max(vert.r).max(vert.g.max(vert.b));
let edge = horiz.max(vert);
let mut z = edge - (horiz - vert).abs() * 0.5;
let mut z = (horiz - vert).abs().mul_add(-0.5, edge);
z = 1. - z.max(horiz.min(vert));
z *= z;
z *= z;
// 85 is about 1/3rd of weight (not 0, because noisy pixels still need to be included, just not as precisely).
noise_row[i] = (80. + z * 176.) as u8;
noise_row[i] = z.mul_add(176., 80.) as u8;
edges_row[i] = ((1. - edge) * 256.) as u8;
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/kmeans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ impl Kmeans {
// chunk size is a trade-off between parallelization and overhead
hist.items.par_chunks_mut(256).for_each({
let tls = &tls; move |batch| {
let kmeans = tls.get_or(move || CacheLineAlign(RefCell::new(Kmeans::new(len))));
let kmeans = tls.get_or(move || CacheLineAlign(RefCell::new(Self::new(len))));
if let Ok(ref mut kmeans) = *kmeans.0.borrow_mut() {
kmeans.iterate_batch(batch, &n, colors, adjust_weight);
}
}});

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

#[inline]
pub fn merge(mut self, new: Kmeans) -> Kmeans {
pub fn merge(mut self, new: Self) -> Self {
self.weighed_diff_sum += new.weighed_diff_sum;
self.averages.iter_mut().zip(new.averages).for_each(|(p, n)| {
p.sum += n.sum;
Expand All @@ -115,7 +115,7 @@ impl Kmeans {
#[inline]
pub fn try_merge<E>(old: Result<Self, E>, new: Result<Self, E>) -> Result<Self, E> {
match (old, new) {
(Ok(old), Ok(new)) => Ok(Kmeans::merge(old, new)),
(Ok(old), Ok(new)) => Ok(Self::merge(old, new)),
(Err(e), _) | (_, Err(e)) => Err(e),
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/mediancut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ impl<'hist> MedianCutter<'hist> {
// first splits boxes that exceed quality limit (to have colors for things like odd green pixel),
// later raises the limit to allow large smooth areas/gradients get colors.
let fraction_done = self.boxes.len() as f64 / f64::from(self.target_colors);
let current_max_mse = max_mse + fraction_done * 16. * max_mse;
let current_max_mse = (fraction_done * 16.).mul_add(max_mse, max_mse);
let bi = match self.take_best_splittable_box(current_max_mse) {
Some(bi) => bi,
None => break,
Expand Down
2 changes: 1 addition & 1 deletion src/pal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ impl PalF {
}

// this is max colors allowed by the user, not just max in the current (candidate/low-quality) palette
pub(crate) fn with_fixed_colors(mut self, max_colors: PalLen, fixed_colors: &[f_pixel]) -> PalF {
pub(crate) fn with_fixed_colors(mut self, max_colors: PalLen, fixed_colors: &[f_pixel]) -> Self {
if fixed_colors.is_empty() {
return self;
}
Expand Down
8 changes: 4 additions & 4 deletions src/quant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl QuantizationResult {
if freeze_result_colors {
palette.iter_mut().for_each(|(_, p)| *p = p.to_fixed());
}
if attr.progress(f32::from(attr.progress_stage1) + f32::from(attr.progress_stage2) + f32::from(attr.progress_stage3) * 0.95) {
if attr.progress(f32::from(attr.progress_stage3).mul_add(0.95, f32::from(attr.progress_stage1) + f32::from(attr.progress_stage2))) {
return Err(Aborted);
}
if let (Some(palette_error), Some(max_mse)) = (palette_error, max_mse) {
Expand Down Expand Up @@ -377,8 +377,8 @@ pub(crate) fn find_best_palette(attr: &Attributes, target_mse: f64, target_mse_i
let mut new_palette = mediancut(&mut hist, max_colors, target_mse * target_mse_overshoot, max_mse_per_color)?
.with_fixed_colors(attr.max_colors, &hist.fixed_colors);

let stage_done = 1. - (f32::from(trials_left.max(0)) / f32::from(total_trials + 1)).powi(2);
let overall_done = f32::from(attr.progress_stage1) + stage_done * f32::from(attr.progress_stage2);
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.);
let overall_done = stage_done.mul_add(f32::from(attr.progress_stage2), f32::from(attr.progress_stage1));
attr.verbose_print(format!(" selecting colors...{}%", (100. * stage_done) as u8));

if trials_left <= 0 { break Some(new_palette); }
Expand Down Expand Up @@ -417,7 +417,7 @@ fn refine_palette(palette: &mut PalF, attr: &Attributes, hist: &mut HistogramInt
let mut i = 0;
while i < iterations {
let stage_done = f32::from(i) / f32::from(iterations);
let overall_done = f32::from(attr.progress_stage1) + f32::from(attr.progress_stage2) + stage_done * f32::from(attr.progress_stage3) * 0.89;
let overall_done = (stage_done * f32::from(attr.progress_stage3)).mul_add(0.89, f32::from(attr.progress_stage1) + f32::from(attr.progress_stage2));
if attr.progress(overall_done) {
break;
}
Expand Down
12 changes: 6 additions & 6 deletions src/remap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ pub(crate) fn remap_to_palette<'x, 'b: 'x>(px: &mut DynamicRows, background: Opt
fn get_dithered_pixel(dither_level: f32, max_dither_error: f32, thiserr: f_pixel, px: f_pixel) -> f_pixel {
let s = thiserr.0 * dither_level;
// This prevents gaudy green pixels popping out of the blue (or red or black! ;)
let dither_error = s.r * s.r + s.g * s.g + s.b * s.b + s.a * s.a;
let dither_error = s.r.mul_add(s.r, s.g * s.g) + s.b.mul_add(s.b, s.a * s.a);
if dither_error < 2. / 256. / 256. {
// don't dither areas that don't have noticeable error — makes file smaller
return px;
Expand Down Expand Up @@ -137,9 +137,9 @@ fn get_dithered_pixel(dither_level: f32, max_dither_error: f32, thiserr: f_pixel
}
f_pixel(ARGBF {
a: (px.a + s.a).clamp(0., 1.),
r: px.r + s.r * ratio,
g: px.g + s.g * ratio,
b: px.b + s.b * ratio,
r: s.r.mul_add(ratio, px.r),
g: s.g.mul_add(ratio, px.g),
b: s.b.mul_add(ratio, px.b),
})
}

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

0 comments on commit 1d9b00b

Please sign in to comment.