feat(0.4.0): add support for GIF

This commit is contained in:
Sam Denty 2022-12-23 23:20:28 +00:00
parent 0d33bd4833
commit eb5f867f05
No known key found for this signature in database
GPG key ID: 7B4EAF7B9E291B79
5 changed files with 39 additions and 4 deletions

2
Cargo.lock generated
View file

@ -1525,7 +1525,7 @@ checksum = "7bd3e3206899af3f8b12af284fafc038cc1dc2b41d1b89dd17297221c5d225de"
[[package]]
name = "site_icons"
version = "0.3.10"
version = "0.4.0"
dependencies = [
"byteorder",
"clap",

View file

@ -1,6 +1,6 @@
[package]
name = "site_icons"
version = "0.3.10"
version = "0.4.0"
authors = ["Sam Denty <sam@samdenty.com>"]
edition = "2018"
license = "GPL-3.0"

View file

@ -17,6 +17,7 @@ enum IconKind {
PNG,
JPEG,
ICO,
GIF,
}
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
@ -26,6 +27,7 @@ pub enum IconInfo {
PNG { size: IconSize },
JPEG { size: IconSize },
ICO { sizes: IconSizes },
GIF { size: IconSize },
SVG,
}
@ -50,6 +52,10 @@ impl IconInfo {
let size = get_jpeg_size(reader).await?;
Ok(IconInfo::JPEG { size })
}
(Some(IconKind::GIF), _) | (_, &[0x47, 0x49]) => {
let size = get_gif_size(reader).await?;
Ok(IconInfo::GIF { size })
}
_ => Err(format!("unknown icon type ({:?})", header).into()),
}
}
@ -140,7 +146,7 @@ impl IconInfo {
pub fn size(&self) -> Option<&IconSize> {
match self {
IconInfo::ICO { sizes } => Some(sizes.largest()),
IconInfo::PNG { size } | IconInfo::JPEG { size } => Some(size),
IconInfo::PNG { size } | IconInfo::JPEG { size } | IconInfo::GIF { size } => Some(size),
IconInfo::SVG => None,
}
}
@ -148,7 +154,9 @@ impl IconInfo {
pub fn sizes(&self) -> Option<IconSizes> {
match self {
IconInfo::ICO { sizes } => Some((*sizes).clone()),
IconInfo::PNG { size } | IconInfo::JPEG { size } => Some((*size).into()),
IconInfo::PNG { size } | IconInfo::JPEG { size } | IconInfo::GIF { size } => {
Some((*size).into())
}
IconInfo::SVG => None,
}
}
@ -159,6 +167,7 @@ impl Display for IconInfo {
match self {
IconInfo::PNG { size } => write!(f, "png {}", size),
IconInfo::JPEG { size } => write!(f, "jpeg {}", size),
IconInfo::GIF { size } => write!(f, "gif {}", size),
IconInfo::ICO { sizes } => write!(f, "ico {}", sizes),
IconInfo::SVG => write!(f, "svg"),
}

24
src/icon_size/gif.rs Normal file
View file

@ -0,0 +1,24 @@
use super::IconSize;
use byteorder::{LittleEndian, ReadBytesExt};
use futures::prelude::*;
use std::{
error::Error,
io::{Cursor, Seek, SeekFrom},
};
pub async fn get_gif_size<R: AsyncRead + Unpin>(
reader: &mut R,
) -> Result<IconSize, Box<dyn Error>> {
let mut header = [0; 8];
reader.read_exact(&mut header).await?;
let header = &mut Cursor::new(header);
assert_slice_eq!(header, 0, b"F8", "bad header");
header.seek(SeekFrom::Start(4))?;
let width = header.read_u16::<LittleEndian>()? as u32;
let height = header.read_u16::<LittleEndian>()? as u32;
Ok(IconSize::new(width, height))
}

View file

@ -1,8 +1,10 @@
mod gif;
mod ico;
mod icon_sizes;
mod jpeg;
mod png;
pub use gif::*;
pub use ico::*;
pub use icon_sizes::*;
pub use jpeg::*;