Skip to content

Commit

Permalink
Merge pull request #270 from taavit/clippy-fixes-p1
Browse files Browse the repository at this point in the history
Basic clippy fixes.
  • Loading branch information
fintelia authored Apr 29, 2023
2 parents 509ec66 + bc736a6 commit 269cd7d
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 43 deletions.
2 changes: 2 additions & 0 deletions src/arch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<unsafe fn(&[u8], &[u8], &[u8], &mut [u8]) -> usize>
{
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Expand All @@ -32,6 +33,7 @@ pub fn get_color_convert_line_ycbcr() -> Option<unsafe fn(&[u8], &[u8], &[u8], &
}

/// Arch-specific implementation of 8x8 IDCT.
#[allow(clippy::type_complexity)]
pub fn get_dequantize_and_idct_block_8x8(
) -> Option<unsafe fn(&[i16; 64], &[u16; 64], usize, &mut [u8])> {
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Expand Down
8 changes: 4 additions & 4 deletions src/arch/ssse3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 _),
Expand All @@ -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.
Expand All @@ -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 _,
Expand Down
31 changes: 17 additions & 14 deletions src/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -595,7 +595,7 @@ impl<R: Read> Decoder<R> {
}

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)
Expand All @@ -616,14 +616,13 @@ impl<R: Read> Decoder<R> {

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(),
));
Expand Down Expand Up @@ -779,6 +778,7 @@ impl<R: Read> Decoder<R> {
}
}

#[allow(clippy::type_complexity)]
fn decode_scan(
&mut self,
frame: &FrameInfo,
Expand Down Expand Up @@ -1009,10 +1009,11 @@ impl<R: Read> Decoder<R> {
// 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
Expand Down Expand Up @@ -1069,6 +1070,7 @@ impl<R: Read> Decoder<R> {
}
}

#[allow(clippy::too_many_arguments)]
fn decode_block<R: Read>(
reader: &mut R,
coefficients: &mut [i16; 64],
Expand Down Expand Up @@ -1321,6 +1323,7 @@ fn compute_image(
}
}

#[allow(clippy::type_complexity)]
pub(crate) fn choose_color_convert_func(
component_count: usize,
color_transform: ColorTransform,
Expand Down
3 changes: 2 additions & 1 deletion src/decoder/lossless.rs
Original file line number Diff line number Diff line change
@@ -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<R: Read> Decoder<R> {
/// decode_scan_lossless
Expand Down Expand Up @@ -185,6 +185,7 @@ impl<R: Read> Decoder<R> {
}

/// H.1.2.1
#[allow(clippy::too_many_arguments)]
fn predict(
ra: i32,
rb: i32,
Expand Down
8 changes: 4 additions & 4 deletions src/idct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ fn stbi_fsh(x: Wrapping<i32>) -> Wrapping<i32> {

#[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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -523,6 +524,7 @@ pub fn parse_dqt<R: Read>(reader: &mut R) -> Result<[Option<[u16; 64]>; 4]> {
}

// Section B.2.4.2
#[allow(clippy::type_complexity)]
pub fn parse_dht<R: Read>(reader: &mut R, is_baseline: Option<bool>) -> Result<(Vec<Option<HuffmanTable>>, Vec<Option<HuffmanTable>>)> {
let mut length = read_length(reader, DHT)?;
let mut dc_tables = vec![None, None, None, None];
Expand Down
32 changes: 15 additions & 17 deletions src/upsampler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
4 changes: 2 additions & 2 deletions src/worker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ enum WorkerScopeInner {
not(any(target_arch = "asmjs", target_arch = "wasm32")),
feature = "rayon"
))]
Rayon(rayon::Scoped),
Rayon(Box<rayon::Scoped>),
#[cfg(not(any(target_arch = "asmjs", target_arch = "wasm32")))]
Multithreaded(multithreaded::MpscWorker),
Immediate(immediate::ImmediateWorker),
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/worker/multithreaded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 269cd7d

Please sign in to comment.