From 7a882fc1dde47200e30b6a420b3627d905744f17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volker=20Str=C3=B6bel?= Date: Tue, 20 Jan 2026 06:42:49 +0100 Subject: [PATCH 1/2] Add cargo fmt --check to github actions --- .github/workflows/rust.yml | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 50c1f55..5e85eff 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -49,3 +49,15 @@ jobs: run: cargo build --verbose --no-default-features - name: Run tests run: cargo test --verbose --no-default-features --tests + + rustfmt: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@stable + with: + components: rustfmt + - name: Run rustfmt check + run: cargo fmt -- --check \ No newline at end of file From b21d6de25d82532b2e972b2fadc1a9009a709ae8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volker=20Str=C3=B6bel?= Date: Tue, 20 Jan 2026 06:45:50 +0100 Subject: [PATCH 2/2] Run cargo fmt --- src/avx2/fdct.rs | 2 +- src/avx2/ycbcr.rs | 10 ++++---- src/encoder.rs | 14 ++++-------- src/error.rs | 2 -- src/fdct.rs | 10 ++------ src/image_buffer.rs | 34 +++++++++++---------------- src/lib.rs | 56 +++++++++++++++++++++++++++++---------------- src/writer.rs | 2 +- 8 files changed, 63 insertions(+), 67 deletions(-) diff --git a/src/avx2/fdct.rs b/src/avx2/fdct.rs index ec12b72..f4a067a 100644 --- a/src/avx2/fdct.rs +++ b/src/avx2/fdct.rs @@ -481,4 +481,4 @@ fn avx_store(input: __m256i, output: &mut [i16]) { assert!(core::mem::size_of::<[i16; 16]>() == core::mem::size_of::<__m256i>()); // SAFETY: we've checked sizes above. The load is unaligned, so no alignment requirements. unsafe { _mm256_storeu_si256(output.as_mut_ptr() as *mut __m256i, input) } -} \ No newline at end of file +} diff --git a/src/avx2/ycbcr.rs b/src/avx2/ycbcr.rs index 29d9d4f..b35d329 100644 --- a/src/avx2/ycbcr.rs +++ b/src/avx2/ycbcr.rs @@ -1,18 +1,18 @@ #[cfg(target_arch = "x86")] use core::arch::x86::{ - __m256i, _mm256_add_epi32, _mm256_mullo_epi32, _mm256_set1_epi32, _mm256_set_epi32, + __m256i, _mm256_add_epi32, _mm256_mullo_epi32, _mm256_set_epi32, _mm256_set1_epi32, _mm256_srli_epi32, _mm256_sub_epi32, }; #[cfg(target_arch = "x86_64")] use core::arch::x86_64::{ - __m256i, _mm256_add_epi32, _mm256_mullo_epi32, _mm256_set1_epi32, _mm256_set_epi32, + __m256i, _mm256_add_epi32, _mm256_mullo_epi32, _mm256_set_epi32, _mm256_set1_epi32, _mm256_srli_epi32, _mm256_sub_epi32, }; use alloc::vec::Vec; -use crate::{rgb_to_ycbcr, ImageBuffer, JpegColorType}; +use crate::{ImageBuffer, JpegColorType, rgb_to_ycbcr}; macro_rules! ycbcr_image_avx2 { ($name:ident, $num_colors:expr, $o1:expr, $o2:expr, $o3:expr) => { @@ -229,7 +229,9 @@ mod tests { for (i, pixel) in scalar_result.iter().copied().enumerate() { let avx_pixel: [u8; 3] = [buffers[0][i], buffers[1][i], buffers[2][i]]; if pixel != avx_pixel { - panic!("Mismatch at index {i}: scalar result is {pixel:?}, avx result is {avx_pixel:?}"); + panic!( + "Mismatch at index {i}: scalar result is {pixel:?}, avx result is {avx_pixel:?}" + ); } } } diff --git a/src/encoder.rs b/src/encoder.rs index 5320011..778fe69 100644 --- a/src/encoder.rs +++ b/src/encoder.rs @@ -4,7 +4,7 @@ use crate::image_buffer::*; use crate::marker::Marker; use crate::quantization::{QuantizationTable, QuantizationTableType}; use crate::writer::{JfifWrite, JfifWriter, ZIGZAG}; -use crate::{PixelDensity, EncodingError}; +use crate::{EncodingError, PixelDensity}; use alloc::vec; use alloc::vec::Vec; @@ -353,11 +353,7 @@ impl Encoder { /// # Errors /// /// Returns an error if the segment number is invalid or data exceeds the allowed size - pub fn add_app_segment( - &mut self, - segment_nr: u8, - data: Vec, - ) -> Result<(), EncodingError> { + pub fn add_app_segment(&mut self, segment_nr: u8, data: Vec) -> Result<(), EncodingError> { if segment_nr == 0 || segment_nr > 15 { Err(EncodingError::InvalidAppSegment(segment_nr)) } else if data.len() > 65533 { @@ -749,10 +745,8 @@ impl Encoder { &row[i], block_x * 8 * max_h_sampling + (h_offset * 8), v_offset * 8, - max_h_sampling - / component.horizontal_sampling_factor as usize, - max_v_sampling - / component.vertical_sampling_factor as usize, + max_h_sampling / component.horizontal_sampling_factor as usize, + max_v_sampling / component.vertical_sampling_factor as usize, buffer_width, ); diff --git a/src/error.rs b/src/error.rs index 40d9e04..c856257 100644 --- a/src/error.rs +++ b/src/error.rs @@ -65,7 +65,6 @@ impl Display for EncodingError { } impl Error for EncodingError { - #[cfg(feature = "std")] fn source(&self) -> Option<&(dyn Error + 'static)> { match self { @@ -74,4 +73,3 @@ impl Error for EncodingError { } } } - diff --git a/src/fdct.rs b/src/fdct.rs index 7d0273e..956c46e 100644 --- a/src/fdct.rs +++ b/src/fdct.rs @@ -134,14 +134,8 @@ pub fn fdct(data: &mut [i16; 64]) { data2[offset + 4] = (tmp10 - tmp11) << PASS1_BITS; let z1 = (tmp12 + tmp13) * FIX_0_541196100; - data2[offset + 2] = descale( - z1 + (tmp13 * FIX_0_765366865), - CONST_BITS - PASS1_BITS, - ); - data2[offset + 6] = descale( - z1 + (tmp12 * -FIX_1_847759065), - CONST_BITS - PASS1_BITS, - ); + data2[offset + 2] = descale(z1 + (tmp13 * FIX_0_765366865), CONST_BITS - PASS1_BITS); + data2[offset + 6] = descale(z1 + (tmp12 * -FIX_1_847759065), CONST_BITS - PASS1_BITS); /* Odd part per figure 8 --- note paper omits factor of sqrt(2). * cK represents cos(K*pi/16). diff --git a/src/image_buffer.rs b/src/image_buffer.rs index 26e9be4..2da8273 100644 --- a/src/image_buffer.rs +++ b/src/image_buffer.rs @@ -122,11 +122,11 @@ impl<'a> ImageBuffer for GrayImage<'a> { } #[inline(always)] -fn get_line(data: &[u8], y: u16, width:u16, num_colors: usize) -> &[u8] { - let width= usize::from(width); +fn get_line(data: &[u8], y: u16, width: u16, num_colors: usize) -> &[u8] { + let width = usize::from(width); let y = usize::from(y); - let start = y *width * num_colors; + let start = y * width * num_colors; let end = start + width * num_colors; &data[start..end] @@ -155,13 +155,13 @@ macro_rules! ycbcr_image { // Doing the convertion in chunks allows the compiler to vectorize the code // A size of 16 seems optimal for SSE and AVX capable hardware - const CHUNK_SIZE:usize = 16; + const CHUNK_SIZE: usize = 16; - let mut y_buffer = [0;CHUNK_SIZE]; - let mut cb_buffer = [0;CHUNK_SIZE]; - let mut cr_buffer = [0;CHUNK_SIZE]; + let mut y_buffer = [0; CHUNK_SIZE]; + let mut cb_buffer = [0; CHUNK_SIZE]; + let mut cr_buffer = [0; CHUNK_SIZE]; - for chuck in line.chunks_exact($num_colors*CHUNK_SIZE) { + for chuck in line.chunks_exact($num_colors * CHUNK_SIZE) { for i in (0..CHUNK_SIZE) { let (y, cb, cr) = rgb_to_ycbcr( chuck[i * $num_colors + $o1], @@ -182,11 +182,11 @@ macro_rules! ycbcr_image { // Add the remaining pixels in case the number of // pixels is not a multiple of CHUNK_SIZE let pixel = line.len() / $num_colors; - for i in pixel/CHUNK_SIZE*CHUNK_SIZE..pixel { + for i in pixel / CHUNK_SIZE * CHUNK_SIZE..pixel { let (y, cb, cr) = rgb_to_ycbcr( - line[i*$num_colors + $o1], - line[i*$num_colors + $o2], - line[i*$num_colors + $o3], + line[i * $num_colors + $o1], + line[i * $num_colors + $o2], + line[i * $num_colors + $o3], ); buffers[0].push(y); @@ -275,13 +275,7 @@ impl<'a> ImageBuffer for CmykAsYcckImage<'a> { let line = get_line(self.0, y, self.width(), 4); for pixel in line.chunks_exact(4) { - - let (y, cb, cr, k) = cmyk_to_ycck( - pixel[0], - pixel[1], - pixel[2], - pixel[3], - ); + let (y, cb, cr, k) = cmyk_to_ycck(pixel[0], pixel[1], pixel[2], pixel[3]); buffers[0].push(y); buffers[1].push(cb); @@ -310,7 +304,6 @@ impl<'a> ImageBuffer for YcckImage<'a> { let line = get_line(self.0, y, self.width(), 4); for pixel in line.chunks_exact(4) { - buffers[0].push(pixel[0]); buffers[1].push(pixel[1]); buffers[2].push(pixel[2]); @@ -330,7 +323,6 @@ mod tests { #[test] fn test_rgb_to_ycbcr() { - assert_rgb_to_ycbcr([0, 0, 0], [0, 128, 128]); assert_rgb_to_ycbcr([255, 255, 255], [255, 128, 128]); assert_rgb_to_ycbcr([255, 0, 0], [76, 85, 255]); diff --git a/src/lib.rs b/src/lib.rs index cdfa85e..f3553bf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -44,9 +44,9 @@ mod writer; pub use encoder::{ColorType, Encoder, JpegColorType, SamplingFactor}; pub use error::EncodingError; -pub use image_buffer::{cmyk_to_ycck, rgb_to_ycbcr, ImageBuffer}; +pub use image_buffer::{ImageBuffer, cmyk_to_ycck, rgb_to_ycbcr}; pub use quantization::QuantizationTableType; -pub use writer::{PixelDensity, PixelDensityUnit, JfifWrite}; +pub use writer::{JfifWrite, PixelDensity, PixelDensityUnit}; #[cfg(feature = "benchmark")] pub use fdct::fdct; @@ -54,10 +54,18 @@ pub use fdct::fdct; #[cfg(feature = "benchmark")] pub use image_buffer::RgbImage; -#[cfg(all(feature = "benchmark", feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))] +#[cfg(all( + feature = "benchmark", + feature = "simd", + any(target_arch = "x86", target_arch = "x86_64") +))] pub use avx2::fdct_avx2; -#[cfg(all(feature = "benchmark", feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))] +#[cfg(all( + feature = "benchmark", + feature = "simd", + any(target_arch = "x86", target_arch = "x86_64") +))] pub use avx2::RgbImageAVX2; #[cfg(test)] @@ -412,10 +420,12 @@ mod tests { .encode(&data, width, height, ColorType::Rgb) .unwrap(); - assert!(result - .as_slice() - .windows(DRI_DATA.len()) - .any(|w| w == DRI_DATA)); + assert!( + result + .as_slice() + .windows(DRI_DATA.len()) + .any(|w| w == DRI_DATA) + ); check_result(data, width, height, &mut result, PixelFormat::RGB24); } @@ -435,10 +445,12 @@ mod tests { .encode(&data, width, height, ColorType::Rgb) .unwrap(); - assert!(result - .as_slice() - .windows(DRI_DATA.len()) - .any(|w| w == DRI_DATA)); + assert!( + result + .as_slice() + .windows(DRI_DATA.len()) + .any(|w| w == DRI_DATA) + ); check_result(data, width, height, &mut result, PixelFormat::RGB24); } @@ -458,10 +470,12 @@ mod tests { .encode(&data, width, height, ColorType::Rgb) .unwrap(); - assert!(result - .as_slice() - .windows(DRI_DATA.len()) - .any(|w| w == DRI_DATA)); + assert!( + result + .as_slice() + .windows(DRI_DATA.len()) + .any(|w| w == DRI_DATA) + ); check_result(data, width, height, &mut result, PixelFormat::RGB24); } @@ -481,10 +495,12 @@ mod tests { let segment_data = b"\xEF\0\x09HOHOHO\0"; - assert!(result - .as_slice() - .windows(segment_data.len()) - .any(|w| w == segment_data)); + assert!( + result + .as_slice() + .windows(segment_data.len()) + .any(|w| w == segment_data) + ); } #[test] diff --git a/src/writer.rs b/src/writer.rs index 6490e98..d0dc9cc 100644 --- a/src/writer.rs +++ b/src/writer.rs @@ -1,8 +1,8 @@ +use crate::EncodingError; use crate::encoder::Component; use crate::huffman::{CodingClass, HuffmanTable}; use crate::marker::{Marker, SOFType}; use crate::quantization::QuantizationTable; -use crate::EncodingError; /// Represents the pixel density of an image ///