feat(0.4.0): add support for GIF
This commit is contained in:
parent
0d33bd4833
commit
eb5f867f05
5 changed files with 39 additions and 4 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -1525,7 +1525,7 @@ checksum = "7bd3e3206899af3f8b12af284fafc038cc1dc2b41d1b89dd17297221c5d225de"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "site_icons"
|
name = "site_icons"
|
||||||
version = "0.3.10"
|
version = "0.4.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"byteorder",
|
"byteorder",
|
||||||
"clap",
|
"clap",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "site_icons"
|
name = "site_icons"
|
||||||
version = "0.3.10"
|
version = "0.4.0"
|
||||||
authors = ["Sam Denty <sam@samdenty.com>"]
|
authors = ["Sam Denty <sam@samdenty.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
license = "GPL-3.0"
|
license = "GPL-3.0"
|
||||||
|
|
|
@ -17,6 +17,7 @@ enum IconKind {
|
||||||
PNG,
|
PNG,
|
||||||
JPEG,
|
JPEG,
|
||||||
ICO,
|
ICO,
|
||||||
|
GIF,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
|
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
|
||||||
|
@ -26,6 +27,7 @@ pub enum IconInfo {
|
||||||
PNG { size: IconSize },
|
PNG { size: IconSize },
|
||||||
JPEG { size: IconSize },
|
JPEG { size: IconSize },
|
||||||
ICO { sizes: IconSizes },
|
ICO { sizes: IconSizes },
|
||||||
|
GIF { size: IconSize },
|
||||||
SVG,
|
SVG,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,6 +52,10 @@ impl IconInfo {
|
||||||
let size = get_jpeg_size(reader).await?;
|
let size = get_jpeg_size(reader).await?;
|
||||||
Ok(IconInfo::JPEG { size })
|
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()),
|
_ => Err(format!("unknown icon type ({:?})", header).into()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -140,7 +146,7 @@ impl IconInfo {
|
||||||
pub fn size(&self) -> Option<&IconSize> {
|
pub fn size(&self) -> Option<&IconSize> {
|
||||||
match self {
|
match self {
|
||||||
IconInfo::ICO { sizes } => Some(sizes.largest()),
|
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,
|
IconInfo::SVG => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -148,7 +154,9 @@ impl IconInfo {
|
||||||
pub fn sizes(&self) -> Option<IconSizes> {
|
pub fn sizes(&self) -> Option<IconSizes> {
|
||||||
match self {
|
match self {
|
||||||
IconInfo::ICO { sizes } => Some((*sizes).clone()),
|
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,
|
IconInfo::SVG => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -159,6 +167,7 @@ impl Display for IconInfo {
|
||||||
match self {
|
match self {
|
||||||
IconInfo::PNG { size } => write!(f, "png {}", size),
|
IconInfo::PNG { size } => write!(f, "png {}", size),
|
||||||
IconInfo::JPEG { size } => write!(f, "jpeg {}", size),
|
IconInfo::JPEG { size } => write!(f, "jpeg {}", size),
|
||||||
|
IconInfo::GIF { size } => write!(f, "gif {}", size),
|
||||||
IconInfo::ICO { sizes } => write!(f, "ico {}", sizes),
|
IconInfo::ICO { sizes } => write!(f, "ico {}", sizes),
|
||||||
IconInfo::SVG => write!(f, "svg"),
|
IconInfo::SVG => write!(f, "svg"),
|
||||||
}
|
}
|
||||||
|
|
24
src/icon_size/gif.rs
Normal file
24
src/icon_size/gif.rs
Normal 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))
|
||||||
|
}
|
|
@ -1,8 +1,10 @@
|
||||||
|
mod gif;
|
||||||
mod ico;
|
mod ico;
|
||||||
mod icon_sizes;
|
mod icon_sizes;
|
||||||
mod jpeg;
|
mod jpeg;
|
||||||
mod png;
|
mod png;
|
||||||
|
|
||||||
|
pub use gif::*;
|
||||||
pub use ico::*;
|
pub use ico::*;
|
||||||
pub use icon_sizes::*;
|
pub use icon_sizes::*;
|
||||||
pub use jpeg::*;
|
pub use jpeg::*;
|
||||||
|
|
Reference in a new issue