Skip to content

Commit b3fc1af

Browse files
committed
Clippy
1 parent 34223fa commit b3fc1af

File tree

11 files changed

+190
-181
lines changed

11 files changed

+190
-181
lines changed

.rustfmt.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# reset to default

src/alpha_blending.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Optimized alpha blending routines based on libwebp
22
//!
3-
//! https://github.com/webmproject/libwebp/blob/e4f7a9f0c7c9fbfae1568bc7fa5c94b989b50872/src/demux/anim_decode.c#L215-L267
3+
//! <https://github.com/webmproject/libwebp/blob/e4f7a9f0c7c9fbfae1568bc7fa5c94b989b50872/src/demux/anim_decode.c#L215-L267>
44
5-
fn channel_shift(i: u32) -> u32 {
5+
const fn channel_shift(i: u32) -> u32 {
66
i * 8
77
}
88

@@ -18,8 +18,9 @@ fn blend_channel_nonpremult(
1818
) -> u8 {
1919
let src_channel = ((src >> shift) & 0xff) as u8;
2020
let dst_channel = ((dst >> shift) & 0xff) as u8;
21-
let blend_unscaled = (src_channel as u32 * src_a as u32) + (dst_channel as u32 * dst_a as u32);
22-
debug_assert!(u64::from(blend_unscaled) < (1u64 << 32) / scale as u64);
21+
let blend_unscaled =
22+
(u32::from(src_channel) * u32::from(src_a)) + (u32::from(dst_channel) * u32::from(dst_a));
23+
debug_assert!(u64::from(blend_unscaled) < (1u64 << 32) / u64::from(scale));
2324
((blend_unscaled * scale) >> channel_shift(3)) as u8
2425
}
2526

@@ -35,8 +36,8 @@ fn blend_pixel_nonpremult(src: u32, dst: u32) -> u32 {
3536
// libwebp used the following formula here:
3637
//let dst_factor_a = (dst_a as u32 * (256 - src_a as u32)) >> 8;
3738
// however, we've found that we can use a more precise approximation without losing performance:
38-
let dst_factor_a = div_by_255(dst_a as u32 * (255 - src_a as u32));
39-
let blend_a = src_a as u32 + dst_factor_a;
39+
let dst_factor_a = div_by_255(u32::from(dst_a) * (255 - u32::from(src_a)));
40+
let blend_a = u32::from(src_a) + dst_factor_a;
4041
let scale = (1u32 << 24) / blend_a;
4142

4243
let blend_r =
@@ -45,11 +46,11 @@ fn blend_pixel_nonpremult(src: u32, dst: u32) -> u32 {
4546
blend_channel_nonpremult(src, src_a, dst, dst_factor_a as u8, scale, channel_shift(1));
4647
let blend_b =
4748
blend_channel_nonpremult(src, src_a, dst, dst_factor_a as u8, scale, channel_shift(2));
48-
debug_assert!(src_a as u32 + dst_factor_a < 256);
49+
debug_assert!(u32::from(src_a) + dst_factor_a < 256);
4950

50-
((blend_r as u32) << channel_shift(0))
51-
| ((blend_g as u32) << channel_shift(1))
52-
| ((blend_b as u32) << channel_shift(2))
51+
(u32::from(blend_r) << channel_shift(0))
52+
| (u32::from(blend_g) << channel_shift(1))
53+
| (u32::from(blend_b) << channel_shift(2))
5354
| (blend_a << channel_shift(3))
5455
}
5556
}
@@ -69,7 +70,7 @@ pub(crate) fn do_alpha_blending(buffer: [u8; 4], canvas: [u8; 4]) -> [u8; 4] {
6970
// https://arxiv.org/pdf/2202.02864
7071
// https://github.com/image-rs/image-webp/issues/119#issuecomment-2544007820
7172
#[inline]
72-
fn div_by_255(v: u32) -> u32 {
73+
const fn div_by_255(v: u32) -> u32 {
7374
(((v + 0x80) >> 8) + v + 0x80) >> 8
7475
}
7576

@@ -116,9 +117,10 @@ mod tests {
116117
let slow = do_alpha_blending_reference([r1, 0, 0, a1], [r2, 0, 0, a2]);
117118
// libwebp doesn't do exact blending and so we don't either
118119
for (o, s) in opt.iter().zip(slow.iter()) {
119-
if o.abs_diff(*s) > 3 {
120-
panic!("Mismatch in results! opt: {opt:?}, slow: {slow:?}, blended values: [{r1}, 0, 0, {a1}], [{r2}, 0, 0, {a2}]");
121-
}
120+
assert!(
121+
o.abs_diff(*s) <= 3,
122+
"Mismatch in results! opt: {opt:?}, slow: {slow:?}, blended values: [{r1}, 0, 0, {a1}], [{r2}, 0, 0, {a2}]"
123+
);
122124
}
123125
}
124126
}

src/decoder.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ pub(crate) enum WebPRiffChunk {
186186
}
187187

188188
impl WebPRiffChunk {
189-
pub(crate) fn from_fourcc(chunk_fourcc: [u8; 4]) -> Self {
189+
pub(crate) const fn from_fourcc(chunk_fourcc: [u8; 4]) -> Self {
190190
match &chunk_fourcc {
191191
b"RIFF" => Self::RIFF,
192192
b"WEBP" => Self::WEBP,
@@ -203,7 +203,7 @@ impl WebPRiffChunk {
203203
}
204204
}
205205

206-
pub(crate) fn to_fourcc(self) -> [u8; 4] {
206+
pub(crate) const fn to_fourcc(self) -> [u8; 4] {
207207
match self {
208208
Self::RIFF => *b"RIFF",
209209
Self::WEBP => *b"WEBP",
@@ -220,7 +220,7 @@ impl WebPRiffChunk {
220220
}
221221
}
222222

223-
pub(crate) fn is_unknown(&self) -> bool {
223+
pub(crate) const fn is_unknown(self) -> bool {
224224
matches!(self, Self::Unknown(_))
225225
}
226226
}
@@ -292,10 +292,10 @@ pub struct WebPDecoder<R> {
292292
}
293293

294294
impl<R: BufRead + Seek> WebPDecoder<R> {
295-
/// Create a new WebPDecoder from the reader `r`. The decoder performs many small reads, so the
295+
/// Create a new `WebPDecoder` from the reader `r`. The decoder performs many small reads, so the
296296
/// reader should be buffered.
297-
pub fn new(r: R) -> Result<WebPDecoder<R>, DecodingError> {
298-
let mut decoder = WebPDecoder {
297+
pub fn new(r: R) -> Result<Self, DecodingError> {
298+
let mut decoder = Self {
299299
r,
300300
width: 0,
301301
height: 0,
@@ -346,8 +346,8 @@ impl<R: BufRead + Seek> WebPDecoder<R> {
346346
let w = self.r.read_u16::<LittleEndian>()?;
347347
let h = self.r.read_u16::<LittleEndian>()?;
348348

349-
self.width = (w & 0x3FFF) as u32;
350-
self.height = (h & 0x3FFF) as u32;
349+
self.width = u32::from(w & 0x3FFF);
350+
self.height = u32::from(h & 0x3FFF);
351351
if self.width == 0 || self.height == 0 {
352352
return Err(DecodingError::InconsistentImageSizes);
353353
}
@@ -402,7 +402,7 @@ impl<R: BufRead + Seek> WebPDecoder<R> {
402402
self.chunks.entry(chunk).or_insert(range);
403403
}
404404

405-
if let WebPRiffChunk::ANMF = chunk {
405+
if chunk == WebPRiffChunk::ANMF {
406406
self.num_frames += 1;
407407
if chunk_size < 24 {
408408
return Err(DecodingError::InvalidChunkSize);
@@ -603,7 +603,7 @@ impl<R: BufRead + Seek> WebPDecoder<R> {
603603
}
604604

605605
/// Returns the number of bytes required to store the image or a single frame, or None if that
606-
/// would take more than usize::MAX bytes.
606+
/// would take more than `usize::MAX` bytes.
607607
pub fn output_buffer_size(&self) -> Option<usize> {
608608
let bytes_per_pixel = if self.has_alpha() { 4 } else { 3 };
609609
(self.width as usize)
@@ -656,7 +656,7 @@ impl<R: BufRead + Seek> WebPDecoder<R> {
656656
.ok_or(DecodingError::ChunkMissing)?
657657
.clone();
658658
let alpha_chunk = read_alpha_chunk(
659-
&mut range_reader(&mut self.r, range.start..range.end)?,
659+
&mut range_reader(&mut self.r, range)?,
660660
self.width as u16,
661661
self.height as u16,
662662
)?;
@@ -748,7 +748,8 @@ impl<R: BufRead + Seek> WebPDecoder<R> {
748748
let reader = (&mut self.r).take(chunk_size);
749749
let mut vp8_decoder = Vp8Decoder::new(reader);
750750
let raw_frame = vp8_decoder.decode_frame()?;
751-
if raw_frame.width as u32 != frame_width || raw_frame.height as u32 != frame_height
751+
if u32::from(raw_frame.width) != frame_width
752+
|| u32::from(raw_frame.height) != frame_height
752753
{
753754
return Err(DecodingError::InconsistentImageSizes);
754755
}

src/encoder.rs

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl<W: Write> BitWriter<W> {
5757
if self.nbits >= 64 {
5858
self.writer.write_all(&self.buffer.to_le_bytes())?;
5959
self.nbits -= 64;
60-
self.buffer = bits.checked_shr((nbits - self.nbits) as u32).unwrap_or(0);
60+
self.buffer = bits.checked_shr(u32::from(nbits - self.nbits)).unwrap_or(0);
6161
}
6262
debug_assert!(self.nbits < 64);
6363
Ok(())
@@ -82,10 +82,10 @@ fn write_single_entry_huffman_tree<W: Write>(w: &mut BitWriter<W>, symbol: u8) -
8282
w.write_bits(1, 2)?;
8383
if symbol <= 1 {
8484
w.write_bits(0, 1)?;
85-
w.write_bits(symbol as u64, 1)?;
85+
w.write_bits(u64::from(symbol), 1)?;
8686
} else {
8787
w.write_bits(1, 1)?;
88-
w.write_bits(symbol as u64, 8)?;
88+
w.write_bits(u64::from(symbol), 8)?;
8989
}
9090
Ok(())
9191
}
@@ -188,7 +188,7 @@ fn build_huffman_tree(
188188
let mut len = length_limit;
189189
let mut indexes = frequencies.iter().copied().enumerate().collect::<Vec<_>>();
190190
indexes.sort_unstable_by_key(|&(_, frequency)| frequency);
191-
for &(i, frequency) in indexes.iter() {
191+
for &(i, frequency) in &indexes {
192192
if frequency > 0 {
193193
while counts[len as usize] == 0 {
194194
len -= 1;
@@ -251,13 +251,13 @@ fn write_huffman_tree<W: Write>(
251251
w.write_bits(0, 1)?; // normal huffman tree
252252
w.write_bits(19 - 4, 4)?; // num_code_lengths - 4
253253

254-
for &i in CODE_LENGTH_ORDER.iter() {
254+
for i in CODE_LENGTH_ORDER {
255255
if i > 15 || code_length_frequencies[i] == 0 {
256256
w.write_bits(0, 3)?;
257257
} else if single_code_length_length {
258258
w.write_bits(1, 3)?;
259259
} else {
260-
w.write_bits(code_length_lengths[i] as u64, 3)?;
260+
w.write_bits(u64::from(code_length_lengths[i]), 3)?;
261261
}
262262
}
263263

@@ -275,7 +275,7 @@ fn write_huffman_tree<W: Write>(
275275
if !single_code_length_length {
276276
for &len in lengths.iter() {
277277
w.write_bits(
278-
code_length_codes[len as usize] as u64,
278+
u64::from(code_length_codes[len as usize]),
279279
code_length_lengths[len as usize],
280280
)?;
281281
}
@@ -284,7 +284,7 @@ fn write_huffman_tree<W: Write>(
284284
Ok(())
285285
}
286286

287-
fn length_to_symbol(len: u16) -> (u16, u8) {
287+
const fn length_to_symbol(len: u16) -> (u16, u8) {
288288
let len = len - 1;
289289
let highest_bit = 15 - len.leading_zeros() as u16; // TODO: use ilog2 once MSRV >= 1.67
290290
let second_highest_bit = (len >> (highest_bit - 1)) & 1;
@@ -331,11 +331,11 @@ fn write_run<W: Write>(
331331
if run_length > 0 {
332332
if run_length <= 4 {
333333
let symbol = 256 + run_length - 1;
334-
w.write_bits(codes1[symbol] as u64, lengths1[symbol])?;
334+
w.write_bits(u64::from(codes1[symbol]), lengths1[symbol])?;
335335
} else {
336336
let (symbol, extra_bits) = length_to_symbol(run_length as u16);
337337
w.write_bits(
338-
codes1[256 + symbol as usize] as u64,
338+
u64::from(codes1[256 + symbol as usize]),
339339
lengths1[256 + symbol as usize],
340340
)?;
341341
w.write_bits(
@@ -392,7 +392,7 @@ fn encode_frame<W: Write>(
392392
};
393393

394394
assert_eq!(
395-
(width as u64 * height as u64).saturating_mul(bytes_per_pixel),
395+
(u64::from(width) * u64::from(height)).saturating_mul(bytes_per_pixel),
396396
data.len() as u64
397397
);
398398

@@ -401,10 +401,10 @@ fn encode_frame<W: Write>(
401401
}
402402

403403
w.write_bits(0x2f, 8)?; // signature
404-
w.write_bits(width as u64 - 1, 14)?;
405-
w.write_bits(height as u64 - 1, 14)?;
404+
w.write_bits(u64::from(width) - 1, 14)?;
405+
w.write_bits(u64::from(height) - 1, 14)?;
406406

407-
w.write_bits(is_alpha as u64, 1)?; // alpha used
407+
w.write_bits(u64::from(is_alpha), 1)?; // alpha used
408408
w.write_bits(0x0, 3)?; // version
409409

410410
// subtract green transform
@@ -542,7 +542,7 @@ fn encode_frame<W: Write>(
542542
ColorType::L8 => {
543543
while let Some(pixel) = it.next() {
544544
w.write_bits(
545-
codes1[pixel[1] as usize] as u64,
545+
u64::from(codes1[pixel[1] as usize]),
546546
lengths1[pixel[1] as usize],
547547
)?;
548548
write_run(w, pixel, &mut it, &codes1, &lengths1)?;
@@ -553,8 +553,8 @@ fn encode_frame<W: Write>(
553553
let len1 = lengths1[pixel[1] as usize];
554554
let len3 = lengths3[pixel[3] as usize];
555555

556-
let code =
557-
codes1[pixel[1] as usize] as u64 | (codes3[pixel[3] as usize] as u64) << len1;
556+
let code = u64::from(codes1[pixel[1] as usize])
557+
| (u64::from(codes3[pixel[3] as usize]) << len1);
558558

559559
w.write_bits(code, len1 + len3)?;
560560
write_run(w, pixel, &mut it, &codes1, &lengths1)?;
@@ -566,9 +566,9 @@ fn encode_frame<W: Write>(
566566
let len0 = lengths0[pixel[0] as usize];
567567
let len2 = lengths2[pixel[2] as usize];
568568

569-
let code = codes1[pixel[1] as usize] as u64
570-
| (codes0[pixel[0] as usize] as u64) << len1
571-
| (codes2[pixel[2] as usize] as u64) << (len1 + len0);
569+
let code = u64::from(codes1[pixel[1] as usize])
570+
| (u64::from(codes0[pixel[0] as usize]) << len1)
571+
| (u64::from(codes2[pixel[2] as usize]) << (len1 + len0));
572572

573573
w.write_bits(code, len1 + len0 + len2)?;
574574
write_run(w, pixel, &mut it, &codes1, &lengths1)?;
@@ -581,10 +581,10 @@ fn encode_frame<W: Write>(
581581
let len2 = lengths2[pixel[2] as usize];
582582
let len3 = lengths3[pixel[3] as usize];
583583

584-
let code = codes1[pixel[1] as usize] as u64
585-
| (codes0[pixel[0] as usize] as u64) << len1
586-
| (codes2[pixel[2] as usize] as u64) << (len1 + len0)
587-
| (codes3[pixel[3] as usize] as u64) << (len1 + len0 + len2);
584+
let code = u64::from(codes1[pixel[1] as usize])
585+
| (u64::from(codes0[pixel[0] as usize]) << len1)
586+
| (u64::from(codes2[pixel[2] as usize]) << (len1 + len0))
587+
| (u64::from(codes3[pixel[3] as usize]) << (len1 + len0 + len2));
588588

589589
w.write_bits(code, len1 + len0 + len2 + len3)?;
590590
write_run(w, pixel, &mut it, &codes1, &lengths1)?;
@@ -596,7 +596,7 @@ fn encode_frame<W: Write>(
596596
Ok(())
597597
}
598598

599-
fn chunk_size(inner_bytes: usize) -> u32 {
599+
const fn chunk_size(inner_bytes: usize) -> u32 {
600600
if inner_bytes % 2 == 1 {
601601
(inner_bytes + 1) as u32 + 8
602602
} else {
@@ -809,7 +809,7 @@ mod tests {
809809
.encode(&img[..256 * 256 * 3], 256, 256, crate::ColorType::Rgb8)
810810
.unwrap();
811811
let decoded = webp::Decoder::new(&output).decode().unwrap();
812-
assert!(&img[..256 * 256 * 3] == &*decoded);
812+
assert_eq!(img[..256 * 256 * 3], *decoded);
813813

814814
let mut output = Vec::new();
815815
let mut encoder = WebPEncoder::new(&mut output);
@@ -818,7 +818,7 @@ mod tests {
818818
.encode(&img, 256, 256, crate::ColorType::Rgba8)
819819
.unwrap();
820820
let decoded = webp::Decoder::new(&output).decode().unwrap();
821-
assert!(&img == &*decoded);
821+
assert_eq!(img, *decoded);
822822

823823
let mut output = Vec::new();
824824
let mut encoder = WebPEncoder::new(&mut output);
@@ -828,7 +828,7 @@ mod tests {
828828
.encode(&img, 256, 256, crate::ColorType::Rgba8)
829829
.unwrap();
830830
let decoded = webp::Decoder::new(&output).decode().unwrap();
831-
assert!(&img == &*decoded);
831+
assert_eq!(img, *decoded);
832832

833833
let mut output = Vec::new();
834834
let mut encoder = WebPEncoder::new(&mut output);
@@ -838,18 +838,18 @@ mod tests {
838838
.encode(&img, 256, 256, crate::ColorType::Rgba8)
839839
.unwrap();
840840
let decoded = webp::Decoder::new(&output).decode().unwrap();
841-
assert!(&img == &*decoded);
841+
assert_eq!(img, *decoded);
842842

843843
let mut output = Vec::new();
844844
let mut encoder = WebPEncoder::new(&mut output);
845-
encoder.set_params(params.clone());
845+
encoder.set_params(params);
846846
encoder.set_xmp_metadata(vec![0; 7]);
847847
encoder.set_icc_profile(vec![0; 8]);
848848
encoder.set_icc_profile(vec![0; 9]);
849849
encoder
850850
.encode(&img, 256, 256, crate::ColorType::Rgba8)
851851
.unwrap();
852852
let decoded = webp::Decoder::new(&output).decode().unwrap();
853-
assert!(&img == &*decoded);
853+
assert_eq!(img, *decoded);
854854
}
855855
}

src/extended.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ pub(crate) fn read_alpha_chunk<R: BufRead>(
301301
let mut decoder = LosslessDecoder::new(reader);
302302

303303
let mut data = vec![0; usize::from(width) * usize::from(height) * 4];
304-
decoder.decode_frame(width as u32, height as u32, true, &mut data)?;
304+
decoder.decode_frame(u32::from(width), u32::from(height), true, &mut data)?;
305305

306306
let mut green = vec![0; usize::from(width) * usize::from(height)];
307307
for (rgba_val, green_val) in data.chunks_exact(4).zip(green.iter_mut()) {

0 commit comments

Comments
 (0)