image/codecs/ico/
encoder.rsuse byteorder::{LittleEndian, WriteBytesExt};
use std::borrow::Cow;
use std::io::{self, Write};
use crate::color::ColorType;
use crate::error::{ImageError, ImageResult, ParameterError, ParameterErrorKind};
use crate::image::ImageEncoder;
use crate::codecs::png::PngEncoder;
const ICO_IMAGE_TYPE: u16 = 1;
const ICO_ICONDIR_SIZE: u32 = 6;
const ICO_DIRENTRY_SIZE: u32 = 16;
pub struct IcoEncoder<W: Write> {
w: W,
}
pub struct IcoFrame<'a> {
encoded_image: Cow<'a, [u8]>,
width: u8,
height: u8,
color_type: ColorType,
}
impl<'a> IcoFrame<'a> {
pub fn with_encoded(
encoded_image: impl Into<Cow<'a, [u8]>>,
width: u32,
height: u32,
color_type: ColorType,
) -> ImageResult<Self> {
let encoded_image = encoded_image.into();
if !(1..=256).contains(&width) {
return Err(ImageError::Parameter(ParameterError::from_kind(
ParameterErrorKind::Generic(format!(
"the image width must be `1..=256`, instead width {} was provided",
width,
)),
)));
}
if !(1..=256).contains(&height) {
return Err(ImageError::Parameter(ParameterError::from_kind(
ParameterErrorKind::Generic(format!(
"the image height must be `1..=256`, instead height {} was provided",
height,
)),
)));
}
Ok(Self {
encoded_image,
width: width as u8,
height: height as u8,
color_type,
})
}
pub fn as_png(buf: &[u8], width: u32, height: u32, color_type: ColorType) -> ImageResult<Self> {
let mut image_data: Vec<u8> = Vec::new();
PngEncoder::new(&mut image_data).write_image(buf, width, height, color_type)?;
let frame = Self::with_encoded(image_data, width, height, color_type)?;
Ok(frame)
}
}
impl<W: Write> IcoEncoder<W> {
pub fn new(w: W) -> IcoEncoder<W> {
IcoEncoder { w }
}
#[deprecated = "Use `IcoEncoder::write_image` instead. Beware that `write_image` has a different endianness convention"]
pub fn encode(self, data: &[u8], width: u32, height: u32, color: ColorType) -> ImageResult<()> {
let mut image_data: Vec<u8> = Vec::new();
#[allow(deprecated)]
PngEncoder::new(&mut image_data).encode(data, width, height, color)?;
let image = IcoFrame::with_encoded(&image_data, width, height, color)?;
self.encode_images(&[image])
}
pub fn encode_images(mut self, images: &[IcoFrame<'_>]) -> ImageResult<()> {
if !(1..=usize::from(u16::MAX)).contains(&images.len()) {
return Err(ImageError::Parameter(ParameterError::from_kind(
ParameterErrorKind::Generic(format!(
"the number of images must be `1..=u16::MAX`, instead {} images were provided",
images.len(),
)),
)));
}
let num_images = images.len() as u16;
let mut offset = ICO_ICONDIR_SIZE + (ICO_DIRENTRY_SIZE * (images.len() as u32));
write_icondir(&mut self.w, num_images)?;
for image in images {
write_direntry(
&mut self.w,
image.width,
image.height,
image.color_type,
offset,
image.encoded_image.len() as u32,
)?;
offset += image.encoded_image.len() as u32;
}
for image in images {
self.w.write_all(&image.encoded_image)?;
}
Ok(())
}
}
impl<W: Write> ImageEncoder for IcoEncoder<W> {
#[track_caller]
fn write_image(
self,
buf: &[u8],
width: u32,
height: u32,
color_type: ColorType,
) -> ImageResult<()> {
let expected_buffer_len =
(width as u64 * height as u64).saturating_mul(color_type.bytes_per_pixel() as u64);
assert_eq!(
expected_buffer_len,
buf.len() as u64,
"Invalid buffer length: expected {expected_buffer_len} got {} for {width}x{height} image",
buf.len(),
);
let image = IcoFrame::as_png(buf, width, height, color_type)?;
self.encode_images(&[image])
}
}
fn write_icondir<W: Write>(w: &mut W, num_images: u16) -> io::Result<()> {
w.write_u16::<LittleEndian>(0)?;
w.write_u16::<LittleEndian>(ICO_IMAGE_TYPE)?;
w.write_u16::<LittleEndian>(num_images)?;
Ok(())
}
fn write_direntry<W: Write>(
w: &mut W,
width: u8,
height: u8,
color: ColorType,
data_start: u32,
data_size: u32,
) -> io::Result<()> {
w.write_u8(width)?;
w.write_u8(height)?;
w.write_u8(0)?;
w.write_u8(0)?;
w.write_u16::<LittleEndian>(0)?;
w.write_u16::<LittleEndian>(color.bits_per_pixel())?;
w.write_u32::<LittleEndian>(data_size)?;
w.write_u32::<LittleEndian>(data_start)?;
Ok(())
}