use std::path::{Path, PathBuf};
use std::time::Duration;
use image::RgbaImage;
use crate::color::Color;
use crate::config::DriverConfig;
use crate::error::{ImageError, Result};
use crate::exif::Exif;
use crate::format::{Format, MediaType};
use crate::geometry::Size;
use crate::manager::{ImageManager, Source};
#[derive(Debug, Clone)]
pub struct Frame {
pub(crate) buffer: RgbaImage,
pub(crate) delay: Duration,
}
impl Frame {
pub fn buffer(&self) -> &RgbaImage {
&self.buffer
}
pub fn delay(&self) -> Duration {
self.delay
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Origin {
media_type: String,
file_path: Option<PathBuf>,
}
impl Default for Origin {
fn default() -> Self {
Self {
media_type: "application/octet-stream".to_owned(),
file_path: None,
}
}
}
impl Origin {
pub(crate) fn new(media_type: MediaType, file_path: Option<PathBuf>) -> Self {
Self {
media_type: media_type.value().to_owned(),
file_path,
}
}
pub fn media_type(&self) -> &str {
&self.media_type
}
pub fn file_path(&self) -> Option<&Path> {
self.file_path.as_deref()
}
pub fn file_extension(&self) -> Option<&str> {
self.file_path
.as_deref()?
.extension()?
.to_str()
.filter(|extension| !extension.is_empty())
}
pub fn format(&self) -> Result<Format> {
MediaType::create(&self.media_type)
.map(MediaType::format)
.map_err(|_| ImageError::NotSupported(format!("Media type \"{}\" is not supported", self.media_type)))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Colorspace {
Rgb,
}
#[derive(Debug, Clone)]
pub struct Image {
pub(crate) frames: Vec<Frame>,
pub(crate) loops: u16,
pub(crate) origin: Origin,
pub(crate) exif: Exif,
pub(crate) config: DriverConfig,
}
impl Image {
pub fn decode(source: impl Into<Source>) -> Result<Image> {
ImageManager::configured().decode(source)
}
pub fn decode_path(path: impl AsRef<Path>) -> Result<Image> {
ImageManager::configured().decode_path(path)
}
pub fn decode_binary(binary: impl AsRef<[u8]>) -> Result<Image> {
ImageManager::configured().decode_binary(binary)
}
pub fn decode_base64(base64: &str) -> Result<Image> {
ImageManager::configured().decode_base64(base64)
}
pub fn decode_data_uri(data_uri: &str) -> Result<Image> {
ImageManager::configured().decode_data_uri(data_uri)
}
pub fn decode_uploaded_file(file: &laravel::http::UploadedFile) -> Result<Image> {
ImageManager::configured().decode_uploaded_file(file)
}
pub fn create_image(width: u32, height: u32) -> Result<Image> {
ImageManager::configured().create_image(width, height)
}
}
impl Image {
pub(crate) fn from_frames(frames: Vec<Frame>, config: DriverConfig) -> Image {
Image {
frames,
loops: 0,
origin: Origin::default(),
exif: Exif::default(),
config,
}
}
pub(crate) fn first(&self) -> &RgbaImage {
&self.frames[0].buffer
}
pub fn width(&self) -> u32 {
self.first().width()
}
pub fn height(&self) -> u32 {
self.first().height()
}
pub fn size(&self) -> Size {
Size::of(self.width(), self.height())
}
pub fn colorspace(&self) -> Colorspace {
Colorspace::Rgb
}
pub fn origin(&self) -> &Origin {
&self.origin
}
pub fn set_origin_media_type(&mut self, media_type: MediaType) -> &mut Self {
self.origin.media_type = media_type.value().to_owned();
self
}
pub fn exif(&self) -> &Exif {
&self.exif
}
pub fn set_exif(&mut self, exif: Exif) -> &mut Self {
self.exif = exif;
self
}
pub fn config(&self) -> &DriverConfig {
&self.config
}
pub fn frames(&self) -> &[Frame] {
&self.frames
}
pub fn count(&self) -> usize {
self.frames.len()
}
pub fn is_animated(&self) -> bool {
self.frames.len() > 1
}
pub fn loops(&self) -> u16 {
self.loops
}
pub fn set_loops(&mut self, loops: u16) -> &mut Self {
self.loops = loops;
self
}
pub fn remove_animation(&mut self, position: usize) -> Result<&mut Self> {
if position >= self.frames.len() {
return Err(ImageError::InvalidArgument(format!(
"Frame #{position} could not be found in the image"
)));
}
let frame = self.frames.swap_remove(position);
self.frames = vec![frame];
Ok(self)
}
pub fn color_at(&self, x: u32, y: u32, frame: usize) -> Option<Color> {
let buffer = &self.frames.get(frame)?.buffer;
(x < buffer.width() && y < buffer.height()).then(|| Color::from_pixel(*buffer.get_pixel(x, y)))
}
pub fn background_color(&self) -> Result<Color> {
self.config.background()
}
pub fn set_background_color(&mut self, color: &str) -> Result<&mut Self> {
Color::parse(color)?;
self.config.background_color = color.to_owned();
Ok(self)
}
pub(crate) fn map_frames(&mut self, mut f: impl FnMut(&RgbaImage) -> RgbaImage) {
for frame in &mut self.frames {
frame.buffer = f(&frame.buffer);
}
}
pub(crate) fn each_frame(&mut self, mut f: impl FnMut(&mut RgbaImage)) {
for frame in &mut self.frames {
f(&mut frame.buffer);
}
}
}