Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clippy/fmt #194

Merged
merged 4 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions benches/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ fn main() {
BenchmarkId::new(def.id, def.data.len()),
def.data,
|b, input| {
b.iter(|| read_image(input))
}
b.iter(|| read_image(input));
},
);
}

Expand Down Expand Up @@ -71,7 +71,7 @@ fn main() {
"extract-metadata-note",
include_bytes!("note.gif"),
|b, input| {
b.iter(|| read_metadata(input))
b.iter(|| read_metadata(input));
}
);

Expand Down
6 changes: 2 additions & 4 deletions benches/rgb_frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ use std::fs;

use criterion::{Criterion, Throughput};
use gif::{Encoder, Frame, Repeat};
use png;

const DIR: &str = "benches/samples";

fn main()
{
fn main() {
let mut c = Criterion::default().configure_from_args();
let mut group = c.benchmark_group("rgb_frame");

Expand Down Expand Up @@ -47,7 +45,7 @@ fn main()
Frame::from_rgba_speed(w, h, &mut buf[..size], 30)
}),
c => {
println!("Image has wrong color type: {:?}", c);
println!("Image has wrong color type: {c:?}");
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion examples/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn main() {
Ok(Some(frame)) => frame,
Ok(None) => break,
Err(error) => {
println!("Error: {:?}", error);
println!("Error: {error:?}");
break;
}
};
Expand Down
4 changes: 2 additions & 2 deletions examples/explode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let output_file_stem = input_path.file_stem().unwrap().to_str().unwrap();
let mut frame_number = 1;
while let Some(frame) = decoder.read_next_frame()? {
let output_path = format!("{}.{:03}.gif", output_file_stem, frame_number);
let output_path = format!("{output_file_stem}.{frame_number:03}.gif");
let mut output = File::create(&output_path)?;
let mut encoder = gif::Encoder::new(&mut output, screen_width, screen_height, &global_pal)?;
encoder.write_frame(frame)?;
frame_number += 1;

use gif::DisposalMethod::*;
use gif::DisposalMethod::{Any, Background, Keep, Previous};
let disposal = match frame.dispose {
Any => "any",
Keep => "keep",
Expand Down
62 changes: 34 additions & 28 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::collections::{HashMap, HashSet};
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[repr(u8)]
pub enum DisposalMethod {
/// StreamingDecoder is not required to take any action.
/// `StreamingDecoder` is not required to take any action.
Any = 0,
/// Do not dispose.
Keep = 1,
Expand All @@ -19,12 +19,12 @@ pub enum DisposalMethod {
impl DisposalMethod {
/// Converts `u8` to `Option<Self>`
#[must_use]
pub fn from_u8(n: u8) -> Option<DisposalMethod> {
pub const fn from_u8(n: u8) -> Option<Self> {
match n {
0 => Some(DisposalMethod::Any),
1 => Some(DisposalMethod::Keep),
2 => Some(DisposalMethod::Background),
3 => Some(DisposalMethod::Previous),
0 => Some(Self::Any),
1 => Some(Self::Keep),
2 => Some(Self::Background),
3 => Some(Self::Previous),
_ => None,
}
}
Expand Down Expand Up @@ -54,11 +54,11 @@ pub enum Block {
impl Block {
/// Converts `u8` to `Option<Self>`
#[must_use]
pub fn from_u8(n: u8) -> Option<Block> {
pub const fn from_u8(n: u8) -> Option<Self> {
match n {
0x2C => Some(Block::Image),
0x21 => Some(Block::Extension),
0x3B => Some(Block::Trailer),
0x2C => Some(Self::Image),
0x21 => Some(Self::Extension),
0x3B => Some(Self::Trailer),
_ => None,
}
}
Expand Down Expand Up @@ -105,26 +105,26 @@ pub enum Extension {
impl AnyExtension {
/// Decode the label as a known extension.
#[must_use]
pub fn into_known(self) -> Option<Extension> {
pub const fn into_known(self) -> Option<Extension> {
Extension::from_u8(self.0)
}
}

impl From<Extension> for AnyExtension {
fn from(ext: Extension) -> Self {
AnyExtension(ext as u8)
Self(ext as u8)
}
}

impl Extension {
/// Converts `u8` to a `Extension` if it is known.
#[must_use]
pub fn from_u8(n: u8) -> Option<Extension> {
pub const fn from_u8(n: u8) -> Option<Self> {
match n {
0x01 => Some(Extension::Text),
0xF9 => Some(Extension::Control),
0xFE => Some(Extension::Comment),
0xFF => Some(Extension::Application),
0x01 => Some(Self::Text),
0xF9 => Some(Self::Control),
0xFE => Some(Self::Comment),
0xFF => Some(Self::Application),
_ => None,
}
}
Expand Down Expand Up @@ -158,8 +158,8 @@ pub struct Frame<'a> {
pub buffer: Cow<'a, [u8]>,
}

impl<'a> Default for Frame<'a> {
fn default() -> Frame<'a> {
impl Default for Frame<'_> {
fn default() -> Self {
Frame {
delay: 0,
dispose: DisposalMethod::Keep,
Expand Down Expand Up @@ -189,7 +189,8 @@ impl Frame<'static> {
/// # Panics:
/// * If the length of pixels does not equal `width * height * 4`.
#[cfg(feature = "color_quant")]
pub fn from_rgba(width: u16, height: u16, pixels: &mut [u8]) -> Frame<'static> {
#[track_caller]
pub fn from_rgba(width: u16, height: u16, pixels: &mut [u8]) -> Self {
Frame::from_rgba_speed(width, height, pixels, 1)
}

Expand All @@ -208,7 +209,8 @@ impl Frame<'static> {
/// * If the length of pixels does not equal `width * height * 4`.
/// * If `speed < 1` or `speed > 30`
#[cfg(feature = "color_quant")]
pub fn from_rgba_speed(width: u16, height: u16, pixels: &mut [u8], speed: i32) -> Frame<'static> {
#[track_caller]
pub fn from_rgba_speed(width: u16, height: u16, pixels: &mut [u8], speed: i32) -> Self {
assert_eq!(width as usize * height as usize * 4, pixels.len(), "Too much or too little pixel data for the given width and height to create a GIF Frame");
assert!(speed >= 1 && speed <= 30, "speed needs to be in the range [1, 30]");
let mut transparent = None;
Expand Down Expand Up @@ -248,22 +250,23 @@ impl Frame<'static> {
let index_of = | pixel: &[u8] |
colors_lookup.get(&(pixel[0], pixel[1], pixel[2], pixel[3])).copied().unwrap_or(0);

return Frame {
Frame {
width,
height,
buffer: Cow::Owned(pixels.chunks_exact(4).map(index_of).collect()),
palette: Some(palette),
transparent: transparent.map(|t| index_of(&t)),
..Frame::default()
};
}
}

/// Creates a frame from a palette and indexed pixels.
///
/// # Panics:
/// * If the length of pixels does not equal `width * height`.
/// * If the length of palette > `256 * 3`.
pub fn from_palette_pixels(width: u16, height: u16, pixels: impl Into<Vec<u8>>, palette: impl Into<Vec<u8>>, transparent: Option<u8>) -> Frame<'static> {
#[track_caller]
pub fn from_palette_pixels(width: u16, height: u16, pixels: impl Into<Vec<u8>>, palette: impl Into<Vec<u8>>, transparent: Option<u8>) -> Self {
let pixels = pixels.into();
let palette = palette.into();
assert_eq!(width as usize * height as usize, pixels.len(), "Too many or too little pixels for the given width and height to create a GIF Frame");
Expand All @@ -283,7 +286,8 @@ impl Frame<'static> {
///
/// # Panics:
/// * If the length of pixels does not equal `width * height`.
pub fn from_indexed_pixels(width: u16, height: u16, pixels: impl Into<Vec<u8>>, transparent: Option<u8>) -> Frame<'static> {
#[track_caller]
pub fn from_indexed_pixels(width: u16, height: u16, pixels: impl Into<Vec<u8>>, transparent: Option<u8>) -> Self {
let pixels = pixels.into();
assert_eq!(width as usize * height as usize, pixels.len(), "Too many or too little pixels for the given width and height to create a GIF Frame");

Expand All @@ -309,7 +313,8 @@ impl Frame<'static> {
/// * If the length of pixels does not equal `width * height * 3`.
#[cfg(feature = "color_quant")]
#[must_use]
pub fn from_rgb(width: u16, height: u16, pixels: &[u8]) -> Frame<'static> {
#[track_caller]
pub fn from_rgb(width: u16, height: u16, pixels: &[u8]) -> Self {
Frame::from_rgb_speed(width, height, pixels, 1)
}

Expand All @@ -329,7 +334,8 @@ impl Frame<'static> {
/// * If `speed < 1` or `speed > 30`
#[cfg(feature = "color_quant")]
#[must_use]
pub fn from_rgb_speed(width: u16, height: u16, pixels: &[u8], speed: i32) -> Frame<'static> {
#[track_caller]
pub fn from_rgb_speed(width: u16, height: u16, pixels: &[u8], speed: i32) -> Self {
assert_eq!(width as usize * height as usize * 3, pixels.len(), "Too much or too little pixel data for the given width and height to create a GIF Frame");
let mut vec: Vec<u8> = Vec::new();
vec.try_reserve_exact(pixels.len() + width as usize * height as usize).expect("OOM");
Expand Down Expand Up @@ -365,6 +371,6 @@ impl Frame<'static> {
// Changing .zip(0_u8..) to .zip(0_u8..=255) fixes this issue.
fn rgba_speed_avoid_panic_256_colors() {
let side = 16;
let pixel_data: Vec<u8> = (0..=255).map(|a| vec![a, a, a]).flatten().collect();
let pixel_data: Vec<u8> = (0..=255).flat_map(|a| [a, a, a]).collect();
let _ = Frame::from_rgb(side, side, &pixel_data);
}
Loading
Loading