Skip to content

Commit

Permalink
Clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Jan 22, 2023
1 parent 819d36f commit 9f15d1c
Show file tree
Hide file tree
Showing 16 changed files with 118 additions and 122 deletions.
12 changes: 6 additions & 6 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn histogram(b: &mut Bencher) {
let mut img = liq.new_image(&*img.buffer, img.width, img.height, 0.).unwrap();
let mut hist = Histogram::new(&liq);
hist.add_image(&liq, &mut img).unwrap();
})
});
}

#[bench]
Expand All @@ -28,12 +28,12 @@ fn remap_ord(b: &mut Bencher) {
b.iter(move || {
res.remap_into(&mut img, &mut buf).unwrap();
res.remap_into(&mut img, &mut buf).unwrap();
})
});
}

#[bench]
fn kmeans(b: &mut Bencher) {
b.iter(_unstable_internal_kmeans_bench())
b.iter(_unstable_internal_kmeans_bench());
}

#[bench]
Expand All @@ -48,7 +48,7 @@ fn remap_floyd(b: &mut Bencher) {
b.iter(move || {
res.remap_into(&mut img, &mut buf).unwrap();
res.remap_into(&mut img, &mut buf).unwrap();
})
});
}

#[bench]
Expand All @@ -59,7 +59,7 @@ fn quantize_s8(b: &mut Bencher) {
b.iter(move || {
let mut img = liq.new_image(&*img.buffer, img.width, img.height, 0.).unwrap();
liq.quantize(&mut img).unwrap();
})
});
}

#[bench]
Expand All @@ -70,5 +70,5 @@ fn quantize_s1(b: &mut Bencher) {
b.iter(move || {
let mut img = liq.new_image(&*img.buffer, img.width, img.height, 0.).unwrap();
liq.quantize(&mut img).unwrap();
})
});
}
24 changes: 12 additions & 12 deletions imagequant-sys/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
#![allow(non_camel_case_types)]
#![allow(clippy::missing_safety_doc)]

use imagequant::*;
use imagequant::capi::*;
use imagequant::Error::LIQ_OK;
use imagequant::*;
use std::ffi::CString;
use std::mem::ManuallyDrop;
use std::mem::MaybeUninit;
Expand Down Expand Up @@ -378,7 +378,7 @@ pub unsafe extern "C" fn liq_image_set_importance_map(img: &mut liq_image, impor
img.set_importance_map(importance_map_slice).err().unwrap_or(LIQ_OK)
} else if ownership == liq_ownership::LIQ_OWN_PIXELS {
let copy: Box<[u8]> = importance_map_slice.into();
free_fn(importance_map as *mut _);
free_fn(importance_map.cast());
img.set_importance_map(copy).err().unwrap_or(LIQ_OK);
LIQ_OK
} else {
Expand Down Expand Up @@ -425,7 +425,7 @@ pub extern "C" fn liq_attr_create_with_allocator(_unused: *mut c_void, free: uns
inner: Attributes::new(),
c_api_free: free,
});
debug_assert_eq!((&*attr) as *const liq_attr, unsafe { attr_to_liq_attr_ptr(&attr.inner) } as *const liq_attr);
debug_assert_eq!(std::ptr::addr_of!(*attr), unsafe { attr_to_liq_attr_ptr(&attr.inner) } as *const liq_attr);
Some(attr)
}

Expand Down Expand Up @@ -494,14 +494,14 @@ pub extern "C" fn liq_get_remapping_error(result: &liq_result) -> f64 {
#[inline(never)]
pub extern "C" fn liq_get_quantization_quality(result: &liq_result) -> c_int {
if bad_object!(result, LIQ_RESULT_MAGIC) { return -1; }
result.inner.quantization_quality().map(c_int::from).unwrap_or(-1)
result.inner.quantization_quality().map_or(-1, c_int::from)
}

#[no_mangle]
#[inline(never)]
pub extern "C" fn liq_get_remapping_quality(result: &liq_result) -> c_int {
if bad_object!(result, LIQ_RESULT_MAGIC) { return -1; }
result.inner.remapping_quality().map(c_int::from).unwrap_or(-1)
result.inner.remapping_quality().map_or(-1, c_int::from)
}

#[no_mangle]
Expand Down Expand Up @@ -644,14 +644,14 @@ fn links_and_runs() {
unsafe {
assert!(liq_version() >= 40000);
let attr = liq_attr_create().unwrap();
let mut hist = liq_histogram_create(&*attr).unwrap();
assert_eq!(LIQ_OK, liq_histogram_add_fixed_color(&mut *hist, liq_color {r: 0, g: 0, b: 0, a: 0}, 0.));
liq_histogram_add_colors(&mut *hist, &*attr, ptr::null(), 0, 0.);
let mut hist = liq_histogram_create(&attr).unwrap();
assert_eq!(LIQ_OK, liq_histogram_add_fixed_color(&mut hist, liq_color {r: 0, g: 0, b: 0, a: 0}, 0.));
liq_histogram_add_colors(&mut hist, &attr, ptr::null(), 0, 0.);

let mut res = MaybeUninit::uninit();

// this is fine, because there is 1 fixed color to generate
assert_eq!(LIQ_OK, liq_histogram_quantize(&mut *hist, &*attr, &mut res));
assert_eq!(LIQ_OK, liq_histogram_quantize(&mut hist, &attr, &mut res));
let res = res.assume_init().unwrap();

liq_result_destroy(Some(res));
Expand Down Expand Up @@ -728,12 +728,12 @@ fn c_callback_test_c() {
assert_eq!(123, width);
for i in 0..width as isize {
let n = i as u8;
(*output_row.offset(i as isize)).write(RGBA::new(n, n, n, n));
(*output_row.offset(i)).write(RGBA::new(n, n, n, n));
}
let user_data = user_data.0 as *mut i32;
let user_data = user_data.0.cast::<i32>();
*user_data += 1;
}
let mut img = liq_image_create_custom(&a, get_row, AnySyncSendPtr((&mut called) as *mut _ as *mut c_void), 123, 5, 0.).unwrap();
let mut img = liq_image_create_custom(&a, get_row, AnySyncSendPtr(std::ptr::addr_of_mut!(called) as *mut c_void), 123, 5, 0.).unwrap();
liq_quantize_image(&mut a, &mut img).unwrap()
};
assert!(called > 5 && called < 50);
Expand Down
16 changes: 8 additions & 8 deletions src/attr.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::error::Error;
use crate::hist::Histogram;
use crate::image::Image;
use crate::pal::MAX_COLORS;
use crate::pal::PalLen;
use crate::pal::MAX_COLORS;
use crate::pal::RGBA;
use crate::quant::{mse_to_quality, quality_to_mse, QuantizationResult};
use crate::remap::DitherMapMode;
Expand Down Expand Up @@ -129,7 +129,7 @@ impl Attributes {
let mut iterations = (8 - value).max(0) as u16;
iterations += iterations * iterations / 2;
self.kmeans_iterations = iterations;
self.kmeans_iteration_limit = 1. / ((1 << (23 - value)) as f64);
self.kmeans_iteration_limit = 1. / f64::from(1 << (23 - value));
self.feedback_loop_trials = (56 - 9 * value).max(0) as _;
self.max_histogram_entries = ((1 << 17) + (1 << 18) * (10 - value)) as _;
self.min_posterization_input = if value >= 8 { 1 } else { 0 };
Expand Down Expand Up @@ -186,7 +186,7 @@ impl Attributes {
#[must_use]
pub fn quality(&self) -> (u8, u8) {
(
self.max_mse.map(mse_to_quality).unwrap_or(0),
self.max_mse.map_or(0, mse_to_quality),
mse_to_quality(self.target_mse),
)
}
Expand Down Expand Up @@ -285,24 +285,24 @@ impl Attributes {
if hist_items > 50000 {
feedback_loop_trials = (feedback_loop_trials * 3 + 3) / 4;
}
if hist_items > 100000 {
if hist_items > 100_000 {
feedback_loop_trials = (feedback_loop_trials * 3 + 3) / 4;
}
feedback_loop_trials
}

/// max_mse, target_mse, user asked for perfect quality
/// `max_mse`, `target_mse`, user asked for perfect quality
pub(crate) fn target_mse(&self, hist_items_len: usize) -> (Option<f64>, f64, bool) {
let max_mse = self.max_mse.map(|mse| mse * if hist_items_len <= MAX_COLORS { 0.33 } else { 1. });
let aim_for_perfect_quality = self.target_mse == 0.;
let mut target_mse = self.target_mse.max(((1 << self.min_posterization_output) as f64 / 1024.).powi(2));
let mut target_mse = self.target_mse.max((f64::from(1 << self.min_posterization_output) / 1024.).powi(2));
if let Some(max_mse) = max_mse {
target_mse = target_mse.min(max_mse);
}
(max_mse, target_mse, aim_for_perfect_quality)
}

/// returns iterations, iteration_limit
/// returns iterations, `iteration_limit`
pub(crate) fn kmeans_iterations(&self, hist_items_len: usize, palette_error_is_known: bool) -> (u16, f64) {
let mut iteration_limit = self.kmeans_iteration_limit;
let mut iterations = self.kmeans_iterations;
Expand All @@ -315,7 +315,7 @@ impl Attributes {
if hist_items_len > 50000 {
iterations = (iterations * 3 + 3) / 4;
}
if hist_items_len > 100000 {
if hist_items_len > 100_000 {
iterations = (iterations * 3 + 3) / 4;
iteration_limit *= 2.;
}
Expand Down
16 changes: 8 additions & 8 deletions src/blur.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ fn transposing_1d_blur(src: &[u8], dst: &mut [u8], width: usize, height: usize,
}

for (j, row) in src.chunks_exact(width).enumerate() {
let mut sum = row[0] as u16 * size;
let mut sum = u16::from(row[0]) * size;
for &v in &row[0..size as usize] {
sum += v as u16;
sum += u16::from(v);
}
for i in 0..size as usize {
sum -= row[0] as u16;
sum += row[i + size as usize] as u16;
sum -= u16::from(row[0]);
sum += u16::from(row[i + size as usize]);
dst[i * height + j] = (sum / (size * 2)) as u8;
}
for i in size as usize..width - size as usize {
sum -= row[i - size as usize] as u16;
sum += row[i + size as usize] as u16;
sum -= u16::from(row[i - size as usize]);
sum += u16::from(row[i + size as usize]);
dst[i * height + j] = (sum / (size * 2)) as u8;
}
for i in width - size as usize..width {
sum -= row[i - size as usize] as u16;
sum += row[width - 1] as u16;
sum -= u16::from(row[i - size as usize]);
sum += u16::from(row[width - 1]);
dst[i * height + j] = (sum / (size * 2)) as u8;
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/capi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
//! For public stable a C FFI interface, see imagequant-sys crate instead.
#![allow(missing_docs)]

use crate::Error;
use crate::RGBA;
use crate::Attributes;
use crate::Palette;
use crate::QuantizationResult;
use crate::Image;
use crate::rows::RowCallback;
use crate::seacow::Pointer;
use crate::seacow::RowBitmapMut;
use crate::seacow::SeaCow;
use crate::seacow::Pointer;
use crate::Attributes;
use crate::Error;
use crate::Image;
use crate::Palette;
use crate::QuantizationResult;
use crate::RGBA;
use std::mem::MaybeUninit;

pub const LIQ_VERSION: u32 = 40000;
Expand Down
26 changes: 10 additions & 16 deletions src/hist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl Histogram {
self.fixed_colors.insert(HashColor(c, idx as _));
}

if attr.progress(attr.progress_stage1 as f32 * 0.40) {
if attr.progress(f32::from(attr.progress_stage1) * 0.40) {
return Err(Aborted); // bow can free the RGBA source if copy has been made in f_pixels
}

Expand Down Expand Up @@ -190,7 +190,7 @@ impl Histogram {
}

if attr.progress(0.) { return Err(Aborted); }
if attr.progress(attr.progress_stage1 as f32 * 0.89) {
if attr.progress(f32::from(attr.progress_stage1) * 0.89) {
return Err(Aborted);
}

Expand All @@ -210,8 +210,8 @@ impl Histogram {
} else { 0 };

self.hashmap.entry(px_int)
.and_modify(move |e| e.0 += boost as u32)
.or_insert((boost as u32, rgba));
.and_modify(move |e| e.0 += u32::from(boost))
.or_insert((u32::from(boost), rgba));
}

fn reserve(&mut self, entries: usize) {
Expand Down Expand Up @@ -253,7 +253,7 @@ impl Histogram {
let pixels_row = &image_iter.row_rgba(&mut temp_row, row)[..width];
let importance_map = importance_map.next().map(move |m| &m[..width]).unwrap_or(&[]);
for (col, px) in pixels_row.iter().copied().enumerate() {
self.add_color(px, importance_map.get(col).copied().unwrap_or(255) as u16);
self.add_color(px, u16::from(importance_map.get(col).copied().unwrap_or(255)));
}
}
self.init_posterize_bits(posterize_bits);
Expand Down Expand Up @@ -301,10 +301,8 @@ impl Histogram {

counts[cluster_index as usize] += 1;

temp.push(TempHistItem {
color, cluster_index, weight,
});
weight as f64
temp.push(TempHistItem { color, weight, cluster_index });
f64::from(weight)
}).sum::<f64>();

let mut clusters = [Cluster { begin: 0, end: 0 }; LIQ_MAXCLUSTER];
Expand Down Expand Up @@ -336,11 +334,7 @@ impl Histogram {
items[next_index].adjusted_weight = temp_item.weight;
}

Ok(HistogramInternal {
items,
clusters,
total_perceptual_weight,
})
Ok(HistogramInternal { items, total_perceptual_weight, clusters })
}
}

Expand Down Expand Up @@ -404,12 +398,12 @@ impl std::hash::Hasher for RgbaHasher {
fn write_isize(&mut self, _i: isize) { unimplemented!() }
}

/// libstd's HashSet is afraid of NaN.
/// libstd's `HashSet` is afraid of NaN.
/// contains color + original index (since hashmap forgets order)
#[derive(PartialEq, Debug)]
pub(crate) struct HashColor(pub f_pixel, pub u32);

#[allow(clippy::derive_hash_xor_eq)]
#[allow(clippy::derived_hash_with_manual_eq)]
impl Hash for HashColor {
#[inline]
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
Expand Down
11 changes: 5 additions & 6 deletions src/image.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use crate::attr::Attributes;
use crate::blur::{liq_blur, liq_max3, liq_min3};
use crate::error::*;
use crate::LIQ_HIGH_MEMORY_LIMIT;
use crate::pal::{PalIndex, MAX_COLORS, MIN_OPAQUE_A, PalF, RGBA, f_pixel, gamma_lut};
use crate::PushInCapacity;
use crate::pal::{f_pixel, gamma_lut, PalF, PalIndex, MAX_COLORS, MIN_OPAQUE_A, RGBA};
use crate::remap::DitherMapMode;
use crate::rows::{DynamicRows, PixelsSource};
use crate::seacow::Pointer;
use crate::seacow::RowBitmap;
use crate::seacow::SeaCow;
use crate::PushInCapacity;
use crate::LIQ_HIGH_MEMORY_LIMIT;
use rgb::ComponentMap;
use std::mem::MaybeUninit;

Expand All @@ -27,7 +27,6 @@ pub struct Image<'pixels> {
}

impl<'pixels> Image<'pixels> {

/// Makes an image from RGBA pixels.
///
/// See the [`rgb`] and [`bytemuck`](//lib.rs/bytemuck) crates for making `[RGBA]` slices from `[u8]` slices.
Expand Down Expand Up @@ -160,7 +159,7 @@ impl<'pixels> Image<'pixels> {
i += 1;
}
while lastcol <= col {
edges[lastcol] = ((edges[lastcol] as u16 + 128) as f32
edges[lastcol] = ((u16::from(edges[lastcol]) + 128) as f32
* (255. / (255 + 128) as f32)
* (1. - 20. / (20 + neighbor_count) as f32))
as u8;
Expand Down Expand Up @@ -234,7 +233,7 @@ impl<'pixels> Image<'pixels> {
}

/// Builds two maps:
/// importance_map - approximation of areas with high-frequency noise, except straight edges. 1=flat, 0=noisy.
/// `importance_map` - approximation of areas with high-frequency noise, except straight edges. 1=flat, 0=noisy.
/// edges - noise map including all edges
pub(crate) fn contrast_maps(&mut self) -> Result<(), Error> {
let width = self.width();
Expand Down
Loading

0 comments on commit 9f15d1c

Please sign in to comment.