use crate::util::*;
use crate::{ImageError, ImageResult, ImageSize};
use std::io::{BufRead, Seek, SeekFrom};
pub fn size<R: BufRead + Seek>(reader: &mut R) -> ImageResult<ImageSize> {
reader.seek(SeekFrom::Start(4))?;
let img_count = read_u16(reader, &Endian::Little)?;
let mut sizes = Vec::with_capacity(img_count as usize);
for _ in 0..img_count {
if let Ok(size) = ico_image_size(reader) {
sizes.push(size)
} else {
break;
}
reader.seek(SeekFrom::Current(14))?;
}
sizes.into_iter().max().ok_or(ImageError::CorruptedImage)
}
pub fn matches(header: &[u8]) -> bool {
header.starts_with(&[0, 0, 1, 0])
}
fn ico_image_size<R: BufRead + Seek>(reader: &mut R) -> ImageResult<ImageSize> {
Ok(ImageSize {
width: read_u8(reader)?.wrapping_sub(1) as usize + 1,
height: read_u8(reader)?.wrapping_sub(1) as usize + 1,
})
}