use image::RgbaImage;
use crate::color::Color;
use crate::options::canvas_bytes;
pub(crate) use crate::format::Format;
impl Format {
pub(crate) fn magick(self) -> &'static str {
self.value()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Page {
pub width: u32,
pub height: u32,
pub x: i64,
pub y: i64,
}
impl Page {
pub(crate) fn at_origin(width: u32, height: u32) -> Self {
Page { width, height, x: 0, y: 0 }
}
}
pub(crate) fn retained(frames: &[Frame]) -> u64 {
frames.iter().map(Frame::bytes).fold(0, u64::saturating_add)
}
#[derive(Debug, Clone)]
pub(crate) struct Frame {
pub pixels: RgbaImage,
pub alpha: bool,
pub delay: u32,
pub page: Page,
pub format: Format,
pub background: Color,
pub quality: Option<i64>,
pub jpeg_sampling: Option<(u8, u8)>,
}
impl Frame {
pub(crate) fn new(pixels: RgbaImage, alpha: bool, delay: u32, format: Format) -> Self {
let page = Page::at_origin(pixels.width(), pixels.height());
Frame { pixels, alpha, delay, page, format, background: Color::WHITE, quality: None, jpeg_sampling: None }
}
pub(crate) fn width(&self) -> u32 {
self.pixels.width()
}
pub(crate) fn height(&self) -> u32 {
self.pixels.height()
}
pub(crate) fn bytes(&self) -> u64 {
canvas_bytes(self.width().into(), self.height().into())
}
pub(crate) fn with_pixels(&self, pixels: RgbaImage) -> Self {
Frame { pixels, ..self.clone_meta() }
}
fn clone_meta(&self) -> Frame {
Frame {
pixels: RgbaImage::new(0, 0),
alpha: self.alpha,
delay: self.delay,
page: self.page,
format: self.format,
background: self.background,
quality: self.quality,
jpeg_sampling: self.jpeg_sampling,
}
}
}