Skip to content

Commit 0c6e401

Browse files
committed
Format Rust code
1 parent e70564b commit 0c6e401

File tree

16 files changed

+579
-285
lines changed

16 files changed

+579
-285
lines changed

benches/decoding_benchmark.rs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,28 @@ fn read_metadata(image: &[u8]) -> ImageInfo {
1818

1919
fn main() {
2020
let mut c = Criterion::default().configure_from_args();
21-
c.bench_function("decode a 512x512 JPEG", |b| b.iter(|| {
22-
read_image(include_bytes!("tower.jpg"))
23-
}));
24-
25-
c.bench_function("decode a 512x512 progressive JPEG", |b| b.iter(|| {
26-
read_image(include_bytes!("tower_progressive.jpg"))
27-
}));
28-
29-
c.bench_function("decode a 512x512 grayscale JPEG", |b| b.iter(|| {
30-
read_image(include_bytes!("tower_grayscale.jpg"))
31-
}));
32-
33-
c.bench_function("extract metadata from an image", |b| b.iter(|| {
34-
read_metadata(include_bytes!("tower.jpg"))
35-
}));
36-
37-
c.bench_function("decode a 3072x2048 RGB Lossless JPEG", |b| b.iter(|| {
38-
read_image(include_bytes!("../tests/reftest/images/lossless/1/jpeg_lossless_sel1-rgb.jpg"))
39-
}));
21+
c.bench_function("decode a 512x512 JPEG", |b| {
22+
b.iter(|| read_image(include_bytes!("tower.jpg")))
23+
});
24+
25+
c.bench_function("decode a 512x512 progressive JPEG", |b| {
26+
b.iter(|| read_image(include_bytes!("tower_progressive.jpg")))
27+
});
28+
29+
c.bench_function("decode a 512x512 grayscale JPEG", |b| {
30+
b.iter(|| read_image(include_bytes!("tower_grayscale.jpg")))
31+
});
32+
33+
c.bench_function("extract metadata from an image", |b| {
34+
b.iter(|| read_metadata(include_bytes!("tower.jpg")))
35+
});
36+
37+
c.bench_function("decode a 3072x2048 RGB Lossless JPEG", |b| {
38+
b.iter(|| {
39+
read_image(include_bytes!(
40+
"../tests/reftest/images/lossless/1/jpeg_lossless_sel1-rgb.jpg"
41+
))
42+
})
43+
});
4044
c.final_summary();
41-
}
45+
}

examples/decode.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,27 @@ fn main() {
3333
jpeg::PixelFormat::L16 => {
3434
encoder.set_depth(png::BitDepth::Sixteen);
3535
encoder.set_color(png::ColorType::Grayscale);
36-
},
37-
jpeg::PixelFormat::RGB24 => {
36+
}
37+
jpeg::PixelFormat::RGB24 => {
3838
encoder.set_depth(png::BitDepth::Eight);
3939
encoder.set_color(png::ColorType::RGB);
40-
},
40+
}
4141
jpeg::PixelFormat::CMYK32 => {
4242
data = cmyk_to_rgb(&mut data);
4343
encoder.set_depth(png::BitDepth::Eight);
4444
encoder.set_color(png::ColorType::RGB)
45-
},
45+
}
4646
jpeg::PixelFormat::L8 => {
4747
encoder.set_depth(png::BitDepth::Eight);
4848
encoder.set_color(png::ColorType::Grayscale);
49-
},
49+
}
5050
}
51-
52-
encoder.write_header()
53-
.expect("writing png header failed")
54-
.write_image_data(&data)
55-
.expect("png encoding failed");
51+
52+
encoder
53+
.write_header()
54+
.expect("writing png header failed")
55+
.write_image_data(&data)
56+
.expect("png encoding failed");
5657
}
5758

5859
fn cmyk_to_rgb(input: &[u8]) -> Vec<u8> {

src/arch/wasm.rs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,14 @@ fn transpose8(data: &mut [v128; 8]) {
8989
// A0 B0 A1 B1 ...
9090
// dABCDll contains elements from the lower quarter (ll) of vectors A, B, C, D, interleaved -
9191
// A0 B0 C0 D0 A1 B1 C1 D1 ...
92-
let d01l = i16x8_shuffle::<0, 8, 1, 9, 2, 10, 3, 11>(data[0], data[1]);
93-
let d23l = i16x8_shuffle::<0, 8, 1, 9, 2, 10, 3, 11>(data[2], data[3]);
94-
let d45l = i16x8_shuffle::<0, 8, 1, 9, 2, 10, 3, 11>(data[4], data[5]);
95-
let d67l = i16x8_shuffle::<0, 8, 1, 9, 2, 10, 3, 11>(data[6], data[7]);
96-
let d01h = i16x8_shuffle::<4, 12, 5, 13, 6, 14, 7, 15>(data[0], data[1]);
97-
let d23h = i16x8_shuffle::<4, 12, 5, 13, 6, 14, 7, 15>(data[2], data[3]);
98-
let d45h = i16x8_shuffle::<4, 12, 5, 13, 6, 14, 7, 15>(data[4], data[5]);
99-
let d67h = i16x8_shuffle::<4, 12, 5, 13, 6, 14, 7, 15>(data[6], data[7]);
92+
let d01l = i16x8_shuffle::<0, 8, 1, 9, 2, 10, 3, 11>(data[0], data[1]);
93+
let d23l = i16x8_shuffle::<0, 8, 1, 9, 2, 10, 3, 11>(data[2], data[3]);
94+
let d45l = i16x8_shuffle::<0, 8, 1, 9, 2, 10, 3, 11>(data[4], data[5]);
95+
let d67l = i16x8_shuffle::<0, 8, 1, 9, 2, 10, 3, 11>(data[6], data[7]);
96+
let d01h = i16x8_shuffle::<4, 12, 5, 13, 6, 14, 7, 15>(data[0], data[1]);
97+
let d23h = i16x8_shuffle::<4, 12, 5, 13, 6, 14, 7, 15>(data[2], data[3]);
98+
let d45h = i16x8_shuffle::<4, 12, 5, 13, 6, 14, 7, 15>(data[4], data[5]);
99+
let d67h = i16x8_shuffle::<4, 12, 5, 13, 6, 14, 7, 15>(data[6], data[7]);
100100

101101
// Operating on 32-bits will interleave *consecutive pairs* of 16-bit integers.
102102
let d0123ll = i32x4_shuffle::<0, 4, 1, 5>(d01l, d23l);
@@ -176,10 +176,7 @@ pub fn dequantize_and_idct_block_8x8(
176176
// `output_linestride * i + 7` < output.len(), so all accesses are in-bounds.
177177
unsafe {
178178
v128_store64_lane::<0>(
179-
u8x16_narrow_i16x8(
180-
i16x8_shr(data_with_offset, SHIFT + 3),
181-
i16x8_splat(0),
182-
),
179+
u8x16_narrow_i16x8(i16x8_shr(data_with_offset, SHIFT + 3), i16x8_splat(0)),
183180
output.as_mut_ptr().wrapping_add(output_linestride * i) as *mut _,
184181
);
185182
}
@@ -188,8 +185,12 @@ pub fn dequantize_and_idct_block_8x8(
188185

189186
#[cfg(target_arch = "wasm32")]
190187
#[target_feature(enable = "simd128")]
191-
pub fn color_convert_line_ycbcr(y_slice: &[u8], cb_slice: &[u8], cr_slice: &[u8], output: &mut [u8]) -> usize {
192-
188+
pub fn color_convert_line_ycbcr(
189+
y_slice: &[u8],
190+
cb_slice: &[u8],
191+
cr_slice: &[u8],
192+
output: &mut [u8],
193+
) -> usize {
193194
assert!(output.len() % 3 == 0);
194195
let num = output.len() / 3;
195196
assert!(num <= y_slice.len());
@@ -242,6 +243,7 @@ pub fn color_convert_line_ycbcr(y_slice: &[u8], cb_slice: &[u8], cr_slice: &[u8]
242243

243244
// Shuffle rrrrrrrrggggggggbbbbbbbb to rgbrgbrgb...
244245

246+
#[rustfmt::skip]
245247
let rg_lanes = i8x16_shuffle::<0, 16,
246248
1, 17,
247249
2, 18,
@@ -251,13 +253,15 @@ pub fn color_convert_line_ycbcr(y_slice: &[u8], cb_slice: &[u8], cr_slice: &[u8]
251253
6, 22,
252254
7, 23>(r, g);
253255

256+
#[rustfmt::skip]
254257
let rgb_low = i8x16_shuffle::<0, 1, 16, // r0, g0, b0
255258
2, 3, 17, // r1, g1, b1
256259
4, 5, 18, // r2, g2, b2
257260
6, 7, 19, // r3, g3, b3
258261
8, 9, 20, // r4, g4, b4
259262
10>(rg_lanes, b); // r5
260263

264+
#[rustfmt::skip]
261265
let rgb_hi = i8x16_shuffle::<11, 21, 12, // g5, b5, r6
262266
13, 22, 14, // g6, b6, r7
263267
15, 23, 0, // g7, b7, --
@@ -269,7 +273,10 @@ pub fn color_convert_line_ycbcr(y_slice: &[u8], cb_slice: &[u8], cr_slice: &[u8]
269273
// `output.len() - 1`.
270274
unsafe {
271275
v128_store(output.as_mut_ptr().wrapping_add(24 * i) as *mut _, rgb_low);
272-
v128_store64_lane::<0>(rgb_hi, output.as_mut_ptr().wrapping_add(24 * i + 16) as *mut _);
276+
v128_store64_lane::<0>(
277+
rgb_hi,
278+
output.as_mut_ptr().wrapping_add(24 * i + 16) as *mut _,
279+
);
273280
}
274281
}
275282

src/decoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ impl<R: Read> Decoder<R> {
790790
}
791791
}
792792

793-
#[allow(clippy::type_complexity)]
793+
#[allow(clippy::type_complexity)]
794794
fn decode_scan(
795795
&mut self,
796796
frame: &FrameInfo,

src/huffman.rs

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ use alloc::vec;
33
use alloc::vec::Vec;
44
use core::iter;
55
use std::io::Read;
6-
use crate::read_u8;
6+
77
use crate::error::{Error, Result};
88
use crate::marker::Marker;
99
use crate::parser::ScanInfo;
10+
use crate::read_u8;
1011

1112
const LUT_BITS: u8 = 8;
1213

@@ -38,11 +39,10 @@ impl HuffmanDecoder {
3839
if size > 0 {
3940
self.consume_bits(size);
4041
Ok(value)
41-
}
42-
else {
42+
} else {
4343
let bits = self.peek_bits(16);
4444

45-
for i in LUT_BITS .. 16 {
45+
for i in LUT_BITS..16 {
4646
let code = (bits >> (15 - i)) as i32;
4747

4848
if code <= table.maxcode[i as usize] {
@@ -57,7 +57,11 @@ impl HuffmanDecoder {
5757
}
5858
}
5959

60-
pub fn decode_fast_ac<R: Read>(&mut self, reader: &mut R, table: &HuffmanTable) -> Result<Option<(i16, u8)>> {
60+
pub fn decode_fast_ac<R: Read>(
61+
&mut self,
62+
reader: &mut R,
63+
table: &HuffmanTable,
64+
) -> Result<Option<(i16, u8)>> {
6165
if let Some(ref ac_lut) = table.ac_lut {
6266
if self.num_bits < LUT_BITS {
6367
self.read_bits(reader)?;
@@ -144,8 +148,12 @@ impl HuffmanDecoder {
144148
}
145149

146150
match next_byte {
147-
0x00 => return Err(Error::Format("FF 00 found where marker was expected".to_owned())),
148-
_ => self.marker = Some(Marker::from_u8(next_byte).unwrap()),
151+
0x00 => {
152+
return Err(Error::Format(
153+
"FF 00 found where marker was expected".to_owned(),
154+
))
155+
}
156+
_ => self.marker = Some(Marker::from_u8(next_byte).unwrap()),
149157
}
150158

151159
continue;
@@ -198,7 +206,7 @@ impl HuffmanTable {
198206
let mut maxcode = [-1i32; 16];
199207
let mut j = 0;
200208

201-
for i in 0 .. 16 {
209+
for i in 0..16 {
202210
if bits[i] != 0 {
203211
delta[i] = j as i32 - huffcode[j] as i32;
204212
j += bits[i] as usize;
@@ -209,7 +217,11 @@ impl HuffmanTable {
209217
// Build a lookup table for faster decoding.
210218
let mut lut = [(0u8, 0u8); 1 << LUT_BITS];
211219

212-
for (i, &size) in huffsize.iter().enumerate().filter(|&(_, &size)| size <= LUT_BITS) {
220+
for (i, &size) in huffsize
221+
.iter()
222+
.enumerate()
223+
.filter(|&(_, &size)| size <= LUT_BITS)
224+
{
213225
let bits_remaining = LUT_BITS - size;
214226
let start = (huffcode[i] << bits_remaining) as usize;
215227

@@ -231,15 +243,17 @@ impl HuffmanTable {
231243
let magnitude_category = value & 0x0f;
232244

233245
if magnitude_category > 0 && size + magnitude_category <= LUT_BITS {
234-
let unextended_ac_value = (((i << size) & ((1 << LUT_BITS) - 1)) >> (LUT_BITS - magnitude_category)) as u16;
246+
let unextended_ac_value = (((i << size) & ((1 << LUT_BITS) - 1))
247+
>> (LUT_BITS - magnitude_category))
248+
as u16;
235249
let ac_value = extend(unextended_ac_value, magnitude_category);
236250

237251
table[i] = (ac_value, (run_length << 4) | (size + magnitude_category));
238252
}
239253
}
240254

241255
Some(table)
242-
},
256+
}
243257
};
244258

245259
Ok(HuffmanTable {
@@ -255,12 +269,13 @@ impl HuffmanTable {
255269
// Section C.2
256270
fn derive_huffman_codes(bits: &[u8; 16]) -> Result<(Vec<u16>, Vec<u8>)> {
257271
// Figure C.1
258-
let huffsize = bits.iter()
259-
.enumerate()
260-
.fold(Vec::new(), |mut acc, (i, &value)| {
261-
acc.extend(iter::repeat((i + 1) as u8).take(value as usize));
262-
acc
263-
});
272+
let huffsize = bits
273+
.iter()
274+
.enumerate()
275+
.fold(Vec::new(), |mut acc, (i, &value)| {
276+
acc.extend(iter::repeat((i + 1) as u8).take(value as usize));
277+
acc
278+
});
264279

265280
// Figure C.2
266281
let mut huffcode = vec![0u16; huffsize.len()];
@@ -292,9 +307,12 @@ fn derive_huffman_codes(bits: &[u8; 16]) -> Result<(Vec<u16>, Vec<u8>)> {
292307
// MJPEG frames and decode them with a regular JPEG decoder, but you have to prepend the DHT
293308
// segment to them, or else the decoder won't have any idea how to decompress the data.
294309
// The exact table necessary is given in the OpenDML spec.""
295-
pub fn fill_default_mjpeg_tables(scan: &ScanInfo,
296-
dc_huffman_tables: &mut[Option<HuffmanTable>],
297-
ac_huffman_tables: &mut[Option<HuffmanTable>]) {
310+
#[rustfmt::skip]
311+
pub fn fill_default_mjpeg_tables(
312+
scan: &ScanInfo,
313+
dc_huffman_tables: &mut[Option<HuffmanTable>],
314+
ac_huffman_tables: &mut[Option<HuffmanTable>],
315+
) {
298316
// Section K.3.3
299317

300318
if dc_huffman_tables[0].is_none() && scan.dc_table_indices.iter().any(|&i| i == 0) {

src/idct.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,8 @@ fn dequantize_and_idct_block_1x1(
560560
) {
561561
debug_assert_eq!(coefficients.len(), 64);
562562

563-
let s0 = (Wrapping(coefficients[0] as i32 * quantization_table[0] as i32) + Wrapping(128 * 8)) / Wrapping(8);
563+
let s0 = (Wrapping(coefficients[0] as i32 * quantization_table[0] as i32) + Wrapping(128 * 8))
564+
/ Wrapping(8);
564565
output[0] = stbi_clamp(s0);
565566
}
566567

src/marker.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ pub enum Marker {
5858
impl Marker {
5959
pub fn has_length(self) -> bool {
6060
use self::Marker::*;
61-
! matches!(self, RST(..) | SOI | EOI | TEM)
61+
!matches!(self, RST(..) | SOI | EOI | TEM)
6262
}
6363

6464
pub fn from_u8(n: u8) -> Option<Marker> {
6565
use self::Marker::*;
6666
match n {
6767
0x00 => None, // Byte stuffing
6868
0x01 => Some(TEM),
69-
0x02 ..= 0xBF => Some(RES),
69+
0x02..=0xBF => Some(RES),
7070
0xC0 => Some(SOF(0)),
7171
0xC1 => Some(SOF(1)),
7272
0xC2 => Some(SOF(2)),

0 commit comments

Comments
 (0)