Skip to content

Commit

Permalink
Clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Aug 4, 2024
1 parent 0a808e0 commit d6514e8
Show file tree
Hide file tree
Showing 10 changed files with 17 additions and 24 deletions.
2 changes: 1 addition & 1 deletion imagequant-sys/c_test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ release = false
imagequant-sys = { version = "4.0.3", path = ".." }

[build-dependencies]
cc = "1.0.95"
cc = "1.1.7"
2 changes: 1 addition & 1 deletion imagequant-sys/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ pub extern "C" fn liq_image_quantize(img: &mut liq_image, attr: &mut liq_attr, w

#[no_mangle]
#[inline(never)]
pub fn liq_histogram_quantize(hist: &mut liq_histogram, attr: &liq_attr, write_only_output: &mut MaybeUninit<Option<Box<liq_result>>>) -> liq_error {
pub extern "C" fn liq_histogram_quantize(hist: &mut liq_histogram, attr: &liq_attr, write_only_output: &mut MaybeUninit<Option<Box<liq_result>>>) -> liq_error {
if bad_object!(attr, LIQ_ATTR_MAGIC) ||
bad_object!(hist, LIQ_HISTOGRAM_MAGIC) { return Error::InvalidPointer; }
let attr = &attr.inner;
Expand Down
5 changes: 1 addition & 4 deletions src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,7 @@ impl<'pixels> Image<'pixels> {
if self.edges.is_none() {
self.contrast_maps()?;
}
let mut edges = match self.edges.take() {
Some(e) => e,
None => return Ok(()),
};
let Some(mut edges) = self.edges.take() else { return Ok(()) };
let colors = palette.as_slice();

let width = self.width();
Expand Down
2 changes: 1 addition & 1 deletion src/kmeans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ fn replace_unused_colors(palette: &mut PalF, hist: &HistogramInternal) -> Result
let colors = palette.as_slice();
// the search is just for diff, ignoring adjusted_weight,
// because the palette already optimizes for the max weight, so it'd likely find another redundant entry.
for item in hist.items.iter() {
for item in &hist.items {
// the early reject avoids running full palette search for every entry
let may_be_worst = colors.get(item.likely_palette_index() as usize)
.map_or(true, |pal| pal.diff(&item.color) > worst_diff);
Expand Down
13 changes: 6 additions & 7 deletions src/mediancut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ impl<'hist> MBox<'hist> {
let mut avg_color = weighed_average_color(hist);
// It's possible that an average color will end up being bad for every entry,
// so prefer picking actual colors so that at least one histogram entry will be satisfied.
if (hist.len() < 500 && hist.len() > 2) || Self::is_useless_color(&avg_color, hist, other_boxes) {
if (hist.len() < 500 && hist.len() > 2) || Self::is_useless_color(avg_color, hist, other_boxes) {
avg_color = hist.iter().min_by_key(|a| OrdFloat::new(avg_color.diff(&a.color))).map(|a| a.color).unwrap_or_default();
}
Self::new_c(hist, adjusted_weight_sum, avg_color)
}

fn new_c(hist: &'hist mut [HistItem], adjusted_weight_sum: f64, avg_color: f_pixel) -> Self {
let (variance, max_error) = Self::box_stats(hist, &avg_color);
let (variance, max_error) = Self::box_stats(hist, avg_color);
Self {
variance,
max_error,
Expand All @@ -59,7 +59,7 @@ impl<'hist> MBox<'hist> {
}

/// It's possible that the average color is useless
fn is_useless_color(new_avg_color: &f_pixel, colors: &[HistItem], other_boxes: &[MBox<'_>]) -> bool {
fn is_useless_color(new_avg_color: f_pixel, colors: &[HistItem], other_boxes: &[MBox<'_>]) -> bool {
colors.iter().all(move |c| {
let own_box_diff = new_avg_color.diff(&c.color);
let other_box_is_better = other_boxes.iter()
Expand All @@ -69,7 +69,7 @@ impl<'hist> MBox<'hist> {
})
}

fn box_stats(hist: &[HistItem], avg_color: &f_pixel) -> (ARGBF, f32) {
fn box_stats(hist: &[HistItem], avg_color: f_pixel) -> (ARGBF, f32) {
let mut variance = ARGBF::default();
let mut max_error = 0.;
for a in hist {
Expand Down Expand Up @@ -278,9 +278,8 @@ impl<'hist> MedianCutter<'hist> {
// 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 = (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,
let Some(bi) = self.take_best_splittable_box(current_max_mse) else {
break
};

self.boxes.extend(bi.split(&self.boxes));
Expand Down
4 changes: 2 additions & 2 deletions src/pal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ impl PalF {
debug_assert!(PalIndex::MAX as usize + 1 >= MAX_COLORS);
debug_assert!(PalLen::MAX as usize >= MAX_COLORS);
Self {
colors: Default::default(),
pops: Default::default(),
colors: ArrayVec::default(),
pops: ArrayVec::default(),
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/quant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl QuantizationResult {
progress_callback: None,
int_palette: Palette {
count: 0,
entries: [Default::default(); MAX_COLORS],
entries: [RGBA::default(); MAX_COLORS],
},
dither_level: 1.,
single_threaded_dithering: attr.single_threaded_dithering,
Expand Down Expand Up @@ -441,7 +441,7 @@ fn refine_palette(palette: &mut PalF, attr: &Attributes, hist: &mut HistogramInt
#[cold]
fn palette_from_histogram(hist: &HistogramInternal, max_colors: PalLen) -> (PalF, Option<f64>) {
let mut hist_pal = PalF::new();
for item in hist.items.iter() {
for item in &hist.items {
hist_pal.push(item.color, PalPop::new(item.perceptual_weight));
}

Expand Down
5 changes: 1 addition & 4 deletions src/remap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,7 @@ pub(crate) fn remap_to_palette<'x, 'b: 'x>(px: &mut DynamicRows, background: Opt

let remapping_error = output_pixels.rows_mut().enumerate().par_bridge().map(|(row, output_pixels_row)| {
let mut remapping_error = 0.;
let tls_res = match tls.get_or_try(per_thread_buffers) {
Ok(res) => res,
Err(_) => return f64::NAN,
};
let Ok(tls_res) = tls.get_or_try(per_thread_buffers) else { return f64::NAN };
let (kmeans, temp_row, temp_row_f, temp_row_f_bg) = &mut *tls_res.0.borrow_mut();

let output_pixels_row = &mut output_pixels_row[..width];
Expand Down
2 changes: 1 addition & 1 deletion src/rows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ impl<'pixels, 'rows> DynamicRows<'pixels, 'rows> {
PixelsSource::Pixels { pixels, rows } => {
// the row with the lowest address is assumed to be at the start of the bitmap
let ptr = rows.as_slice().iter().map(|p| p.0).min().ok_or(Error::Unsupported)?;
*pixels = Some(SeaCow::c_owned(ptr as *mut _, len, free_fn));
*pixels = Some(SeaCow::c_owned(ptr.cast_mut(), len, free_fn));
},
PixelsSource::Callback(_) => return Err(Error::ValueOutOfRange),
}
Expand Down
2 changes: 1 addition & 1 deletion src/seacow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl<'a, T> SeaCow<'a, T> {
#[cfg(feature = "_internal_c_ffi")]
pub(crate) fn make_owned(&mut self, free_fn: unsafe extern fn(*mut c_void)) {
if let SeaCowInner::Borrowed(slice) = self.inner {
self.inner = SeaCowInner::Owned { ptr: slice.as_ptr() as *mut _, len: slice.len(), free_fn };
self.inner = SeaCowInner::Owned { ptr: slice.as_ptr().cast_mut(), len: slice.len(), free_fn };
}
}
}
Expand Down

0 comments on commit d6514e8

Please sign in to comment.