use std::any::Any;
use std::fmt;
use std::future::Future;
use std::path::Path;
use std::pin::Pin;
use std::sync::Arc;
use std::time::Duration;
pub use valo::{AlphaType, PixelBuffer, PixelFormat, PixelLayout};
use crate::{HardwareBuffer, Size};
pub trait ImageCodecs {
fn decoded_formats(&self) -> Vec<String>;
fn encoded_formats(&self) -> Vec<String>;
fn open(&self, data: Arc<[u8]>) -> ImageFuture<Box<dyn ImageSource>>;
fn open_file(&self, path: &Path) -> ImageFuture<Box<dyn ImageSource>>;
fn encode_pixels(
&self,
pixels: PixelBuffer,
format: &str,
quality: Option<f32>,
) -> ImageFuture<Vec<u8>>;
fn hardware_image_codecs(&self) -> Option<&dyn HardwareImageCodecs>;
}
pub trait ImageSource: Any {
fn mime_type(&self) -> String;
fn size(&self) -> Size;
fn frames(&self) -> Vec<ImageFrame>;
fn orientation(&self) -> ImageOrientation;
fn metadata(&self) -> ImageMetadata;
fn decode_pixels(&self, frame: u32, max_size: Option<Size>) -> ImageFuture<PixelBuffer>;
}
pub trait HardwareImageCodecs {
fn decode(
&self,
image: &dyn ImageSource,
frame: u32,
max_size: Option<Size>,
) -> ImageFuture<Arc<dyn HardwareBuffer>>;
fn encode(
&self,
buffer: &dyn HardwareBuffer,
format: &str,
quality: Option<f32>,
) -> ImageFuture<Vec<u8>>;
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct ImageFrame {
pub duration: Option<Duration>,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct ImageOrientation {
pub mirrored: bool,
pub rotation: ImageRotation,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ImageRotation {
#[default]
None,
QuarterTurn,
HalfTurn,
ThreeQuarterTurn,
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct ImageMetadata {
pub taken: Option<DateTime>,
pub camera_make: Option<String>,
pub camera_model: Option<String>,
pub lens_model: Option<String>,
pub exposure_time: Option<f64>,
pub f_number: Option<f64>,
pub iso: Option<u32>,
pub focal_length: Option<f64>,
pub equivalent_focal_length: Option<f64>,
pub exposure_bias: Option<f64>,
pub flash_fired: Option<bool>,
pub location: Option<Location>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct DateTime {
pub year: i32,
pub month: u8,
pub day: u8,
pub hour: u8,
pub minute: u8,
pub second: u8,
pub utc_offset: Option<i16>,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Location {
pub latitude: f64,
pub longitude: f64,
pub altitude: Option<f64>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ImageError {
pub kind: ImageErrorKind,
pub message: String,
}
impl ImageError {
pub fn new(kind: ImageErrorKind, message: impl Into<String>) -> ImageError {
ImageError {
kind,
message: message.into(),
}
}
}
impl fmt::Display for ImageError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let kind = match self.kind {
ImageErrorKind::Unsupported => "unsupported",
ImageErrorKind::Damaged => "damaged",
ImageErrorKind::TooLarge => "too large",
ImageErrorKind::NotFound => "not found",
ImageErrorKind::OutOfMemory => "out of memory",
ImageErrorKind::Other => "failed",
};
write!(f, "image {kind}: {}", self.message)
}
}
impl std::error::Error for ImageError {}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ImageErrorKind {
Unsupported,
Damaged,
TooLarge,
NotFound,
OutOfMemory,
Other,
}
pub type ImageFuture<T> = Pin<Box<dyn Future<Output = Result<T, ImageError>>>>;