From 74f86ad37a7b4b907fa8b28bcb70004644cd81cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Kr=C3=B3lak?= Date: Fri, 28 Apr 2023 22:28:07 +0200 Subject: [PATCH 1/4] Replaced deprecated fmt attributes with rustfmt::skip --- src/decoder.rs | 2 +- src/idct.rs | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/decoder.rs b/src/decoder.rs index 795ad1e4..640e557c 100644 --- a/src/decoder.rs +++ b/src/decoder.rs @@ -24,7 +24,7 @@ pub const MAX_COMPONENTS: usize = 4; mod lossless; use self::lossless::compute_image_lossless; -#[cfg_attr(rustfmt, rustfmt_skip)] +#[rustfmt::skip] static UNZIGZAG: [u8; 64] = [ 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, diff --git a/src/idct.rs b/src/idct.rs index ad8dc4c5..99f10ffa 100644 --- a/src/idct.rs +++ b/src/idct.rs @@ -579,7 +579,7 @@ fn stbi_fsh(x: Wrapping) -> Wrapping { #[test] fn test_dequantize_and_idct_block_8x8() { - #[cfg_attr(rustfmt, rustfmt_skip)] + #[rustfmt::skip] let coefficients: [i16; 8 * 8] = [ -14, -39, 58, -2, 3, 3, 0, 1, 11, 27, 4, -3, 3, 0, 1, 0, @@ -591,7 +591,7 @@ fn test_dequantize_and_idct_block_8x8() { 0, 0, 0, 0, 0, 0, 0, 0 ]; - #[cfg_attr(rustfmt, rustfmt_skip)] + #[rustfmt::skip] let quantization_table: [u16; 8 * 8] = [ 8, 6, 5, 8, 12, 20, 26, 31, 6, 6, 7, 10, 13, 29, 30, 28, @@ -610,7 +610,7 @@ fn test_dequantize_and_idct_block_8x8() { output_linestride, &mut output, ); - #[cfg_attr(rustfmt, rustfmt_skip)] + #[rustfmt::skip] let expected_output = [ 118, 92, 110, 83, 77, 93, 144, 198, 172, 116, 114, 87, 78, 93, 146, 191, @@ -642,7 +642,7 @@ fn test_dequantize_and_idct_block_8x8_saturated() { } let mut output = [0u8; 8 * 8]; dequantize_and_idct_block_8x8(&[i16::MAX; 8 * 8], &[u16::MAX; 8 * 8], 8, &mut output); - #[cfg_attr(rustfmt, rustfmt_skip)] + #[rustfmt::skip] let expected = [ 0, 0, 0, 255, 255, 0, 0, 255, 0, 0, 215, 0, 0, 255, 255, 0, From b2d214dac8c4c1e8ac9c87518ce927a6cd4368ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Kr=C3=B3lak?= Date: Fri, 28 Apr 2023 23:20:05 +0200 Subject: [PATCH 2/4] Replace direct array access with iterator approach --- src/arch/ssse3.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/arch/ssse3.rs b/src/arch/ssse3.rs index 374a70c8..e0bd2e43 100644 --- a/src/arch/ssse3.rs +++ b/src/arch/ssse3.rs @@ -148,8 +148,8 @@ pub unsafe fn dequantize_and_idct_block_8x8( // Read the DCT coefficients, scale them up and dequantize them. let mut data = [_mm_setzero_si128(); 8]; - for i in 0..8 { - data[i] = _mm_slli_epi16( + for (i, item) in data.iter_mut().enumerate() { + *item = _mm_slli_epi16( _mm_mullo_epi16( _mm_loadu_si128(coefficients.as_ptr().wrapping_add(i * 8) as *const _), _mm_loadu_si128(quantization_table.as_ptr().wrapping_add(i * 8) as *const _), @@ -164,7 +164,7 @@ pub unsafe fn dequantize_and_idct_block_8x8( idct8(&mut data); transpose8(&mut data); - for i in 0..8 { + for (i, item) in data.iter_mut().enumerate() { let mut buf = [0u8; 16]; // The two passes of the IDCT algorithm give us a factor of 8, so the shift here is // increased by 3. @@ -174,7 +174,7 @@ pub unsafe fn dequantize_and_idct_block_8x8( // We want rounding right shift, so we should add (1/2) << (SHIFT+3) before shifting. const ROUNDING_BIAS: i16 = (1 << (SHIFT + 3)) >> 1; - let data_with_offset = _mm_adds_epi16(data[i], _mm_set1_epi16(OFFSET + ROUNDING_BIAS)); + let data_with_offset = _mm_adds_epi16(*item, _mm_set1_epi16(OFFSET + ROUNDING_BIAS)); _mm_storeu_si128( buf.as_mut_ptr() as *mut _, From 053873558b68f5b710d58dd04796b2ea313542a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Kr=C3=B3lak?= Date: Fri, 28 Apr 2023 23:33:07 +0200 Subject: [PATCH 3/4] Fixes --- src/decoder.rs | 2 +- src/worker/multithreaded.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/decoder.rs b/src/decoder.rs index 640e557c..50e64156 100644 --- a/src/decoder.rs +++ b/src/decoder.rs @@ -595,7 +595,7 @@ impl Decoder { } let frame = self.frame.as_ref().unwrap(); - let preference = Self::select_worker(&frame, PreferWorkerKind::Multithreaded); + let preference = Self::select_worker(frame, PreferWorkerKind::Multithreaded); worker_scope.get_or_init_worker(preference, |worker| { self.decode_planes(worker, planes, planes_u16) diff --git a/src/worker/multithreaded.rs b/src/worker/multithreaded.rs index c8207025..8a9f5c6b 100644 --- a/src/worker/multithreaded.rs +++ b/src/worker/multithreaded.rs @@ -32,7 +32,7 @@ impl MpscWorker { ) -> Result<()> { // if there is no worker thread for this component yet, start one let component = row_data.index; - if let None = self.senders[component] { + if self.senders[component].is_none() { let sender = spawn_worker(component)?; self.senders[component] = Some(sender); } From bc736a6b09ad27891698febd6480fc67e1184b94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Kr=C3=B3lak?= Date: Sat, 29 Apr 2023 17:39:54 +0200 Subject: [PATCH 4/4] Clippy fixes --- src/arch/mod.rs | 2 ++ src/decoder.rs | 27 +++++++++++++++------------ src/decoder/lossless.rs | 3 ++- src/parser.rs | 2 ++ src/upsampler.rs | 32 +++++++++++++++----------------- src/worker/mod.rs | 4 ++-- 6 files changed, 38 insertions(+), 32 deletions(-) diff --git a/src/arch/mod.rs b/src/arch/mod.rs index 42f19f4d..df1fc6c5 100644 --- a/src/arch/mod.rs +++ b/src/arch/mod.rs @@ -9,6 +9,7 @@ use std::is_x86_feature_detected; /// Arch-specific implementation of YCbCr conversion. Returns the number of pixels that were /// converted. +#[allow(clippy::type_complexity)] pub fn get_color_convert_line_ycbcr() -> Option usize> { #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] @@ -32,6 +33,7 @@ pub fn get_color_convert_line_ycbcr() -> Option Option { #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] diff --git a/src/decoder.rs b/src/decoder.rs index 50e64156..40115690 100644 --- a/src/decoder.rs +++ b/src/decoder.rs @@ -616,14 +616,13 @@ impl Decoder { let frame = self.frame.as_ref().unwrap(); - if { - let required_mem = frame - .components - .len() - .checked_mul(frame.output_size.width.into()) - .and_then(|m| m.checked_mul(frame.output_size.height.into())); - required_mem.map_or(true, |m| self.decoding_buffer_size_limit < m) - } { + if frame + .components + .len() + .checked_mul(frame.output_size.width.into()) + .and_then(|m| m.checked_mul(frame.output_size.height.into())) + .map_or(true, |m| self.decoding_buffer_size_limit < m) + { return Err(Error::Format( "size of decoded image exceeds maximum allowed size".to_owned(), )); @@ -779,6 +778,7 @@ impl Decoder { } } +#[allow(clippy::type_complexity)] fn decode_scan( &mut self, frame: &FrameInfo, @@ -1009,10 +1009,11 @@ impl Decoder { // In the event of non-interleaved streams, if we're still building the buffer out, // keep going; don't send it yet. We also need to ensure we don't skip over the last // row(s) of the image. - if !is_interleaved && (mcu_y + 1) * 8 < frame.image_size.height { - if (mcu_y + 1) % component.vertical_sampling_factor as u16 > 0 { - continue; - } + if !is_interleaved + && (mcu_y + 1) * 8 < frame.image_size.height + && (mcu_y + 1) % component.vertical_sampling_factor as u16 > 0 + { + continue; } let coefficients_per_mcu_row = component.block_size.width as usize @@ -1069,6 +1070,7 @@ impl Decoder { } } +#[allow(clippy::too_many_arguments)] fn decode_block( reader: &mut R, coefficients: &mut [i16; 64], @@ -1321,6 +1323,7 @@ fn compute_image( } } +#[allow(clippy::type_complexity)] pub(crate) fn choose_color_convert_func( component_count: usize, color_transform: ColorTransform, diff --git a/src/decoder/lossless.rs b/src/decoder/lossless.rs index 64222201..cd00476f 100644 --- a/src/decoder/lossless.rs +++ b/src/decoder/lossless.rs @@ -1,10 +1,10 @@ -use std::io::Read; use crate::decoder::{Decoder, MAX_COMPONENTS}; use crate::error::{Error, Result}; use crate::huffman::HuffmanDecoder; use crate::marker::Marker; use crate::parser::Predictor; use crate::parser::{Component, FrameInfo, ScanInfo}; +use std::io::Read; impl Decoder { /// decode_scan_lossless @@ -185,6 +185,7 @@ impl Decoder { } /// H.1.2.1 +#[allow(clippy::too_many_arguments)] fn predict( ra: i32, rb: i32, diff --git a/src/parser.rs b/src/parser.rs index 72ba00dc..bf13aba0 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -98,6 +98,7 @@ pub enum AppData { } // http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/JPEG.html#Adobe +#[allow(clippy::upper_case_acronyms)] #[derive(Clone, Copy, Debug, PartialEq)] pub enum AdobeColorTransform { // RGB or CMYK @@ -523,6 +524,7 @@ pub fn parse_dqt(reader: &mut R) -> Result<[Option<[u16; 64]>; 4]> { } // Section B.2.4.2 +#[allow(clippy::type_complexity)] pub fn parse_dht(reader: &mut R, is_baseline: Option) -> Result<(Vec>, Vec>)> { let mut length = read_length(reader, DHT)?; let mut dc_tables = vec![None, None, None, None]; diff --git a/src/upsampler.rs b/src/upsampler.rs index a5c39d4e..10640fe2 100644 --- a/src/upsampler.rs +++ b/src/upsampler.rs @@ -84,29 +84,27 @@ fn choose_upsampler(sampling_factors: (u8, u8), if h1 && v1 { Ok(Box::new(UpsamplerH1V1)) - } - else if h2 && v1 { + } else if h2 && v1 { Ok(Box::new(UpsamplerH2V1)) - } - else if h1 && v2 { + } else if h1 && v2 { Ok(Box::new(UpsamplerH1V2)) - } - else if h2 && v2 { + } else if h2 && v2 { Ok(Box::new(UpsamplerH2V2)) - } - else { - if max_sampling_factors.0 % sampling_factors.0 != 0 || max_sampling_factors.1 % sampling_factors.1 != 0 { - Err(Error::Unsupported(UnsupportedFeature::NonIntegerSubsamplingRatio)) - } - else { - Ok(Box::new(UpsamplerGeneric { - horizontal_scaling_factor: max_sampling_factors.0 / sampling_factors.0, - vertical_scaling_factor: max_sampling_factors.1 / sampling_factors.1 - })) - } + } else if max_sampling_factors.0 % sampling_factors.0 != 0 + || max_sampling_factors.1 % sampling_factors.1 != 0 + { + Err(Error::Unsupported( + UnsupportedFeature::NonIntegerSubsamplingRatio, + )) + } else { + Ok(Box::new(UpsamplerGeneric { + horizontal_scaling_factor: max_sampling_factors.0 / sampling_factors.0, + vertical_scaling_factor: max_sampling_factors.1 / sampling_factors.1, + })) } } +#[allow(clippy::too_many_arguments)] trait Upsample { fn upsample_row(&self, input: &[u8], diff --git a/src/worker/mod.rs b/src/worker/mod.rs index d6c2b10e..abf9488c 100644 --- a/src/worker/mod.rs +++ b/src/worker/mod.rs @@ -50,7 +50,7 @@ enum WorkerScopeInner { not(any(target_arch = "asmjs", target_arch = "wasm32")), feature = "rayon" ))] - Rayon(rayon::Scoped), + Rayon(Box), #[cfg(not(any(target_arch = "asmjs", target_arch = "wasm32")))] Multithreaded(multithreaded::MpscWorker), Immediate(immediate::ImmediateWorker), @@ -86,7 +86,7 @@ impl WorkerScope { not(any(target_arch = "asmjs", target_arch = "wasm32")), feature = "rayon" ))] - WorkerScopeInner::Rayon(worker) => worker, + WorkerScopeInner::Rayon(worker) => worker.as_mut(), #[cfg(not(any(target_arch = "asmjs", target_arch = "wasm32")))] WorkerScopeInner::Multithreaded(worker) => worker, WorkerScopeInner::Immediate(worker) => worker,