Add ImageLoader and file discovery modules

This commit is contained in:
2026-03-06 01:50:46 +02:00
parent 34cac53eb8
commit 1fc958f068
5 changed files with 253 additions and 0 deletions

View File

@@ -0,0 +1,69 @@
use std::path::Path;
use crate::error::{PixstripError, Result};
use crate::types::{Dimensions, ImageFormat};
#[derive(Debug, Clone)]
pub struct ImageInfo {
pub dimensions: Dimensions,
pub format: Option<ImageFormat>,
pub file_size: u64,
}
pub struct ImageLoader;
impl ImageLoader {
pub fn new() -> Self {
Self
}
pub fn load_info(&self, path: &Path) -> Result<ImageInfo> {
let metadata = std::fs::metadata(path).map_err(|e| PixstripError::ImageLoad {
path: path.to_path_buf(),
reason: e.to_string(),
})?;
let reader =
image::ImageReader::open(path).map_err(|e| PixstripError::ImageLoad {
path: path.to_path_buf(),
reason: e.to_string(),
})?;
let reader = reader.with_guessed_format().map_err(|e| PixstripError::ImageLoad {
path: path.to_path_buf(),
reason: e.to_string(),
})?;
let img = reader.decode().map_err(|e| PixstripError::ImageLoad {
path: path.to_path_buf(),
reason: e.to_string(),
})?;
let format = path
.extension()
.and_then(|ext| ext.to_str())
.and_then(ImageFormat::from_extension);
Ok(ImageInfo {
dimensions: Dimensions {
width: img.width(),
height: img.height(),
},
format,
file_size: metadata.len(),
})
}
pub fn load_pixels(&self, path: &Path) -> Result<image::DynamicImage> {
image::open(path).map_err(|e| PixstripError::ImageLoad {
path: path.to_path_buf(),
reason: e.to_string(),
})
}
}
impl Default for ImageLoader {
fn default() -> Self {
Self::new()
}
}