From d432689cb99028e5f1642de4cbc9b878e4510170 Mon Sep 17 00:00:00 2001 From: Gwen Lg Date: Mon, 20 May 2024 23:14:20 +0200 Subject: [PATCH] clean: move image dump code in a utils sub module ... of image module --- src/image/mod.rs | 83 ++-------------------------------------------- src/image/utils.rs | 81 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 81 deletions(-) create mode 100644 src/image/utils.rs diff --git a/src/image/mod.rs b/src/image/mod.rs index f1e02a2..66f132d 100644 --- a/src/image/mod.rs +++ b/src/image/mod.rs @@ -1,83 +1,4 @@ //! Module for `Image` manipulation. +mod utils; -use image::{EncodableLayout, Pixel, PixelWithColorType}; -use std::{ - fs::create_dir_all, - io, - ops::Deref, - path::{Path, PathBuf}, -}; -use thiserror::Error; - -use crate::SubError; - -/// Handle Error for image dump. -#[derive(Error, Debug)] -pub enum DumpError { - /// Error with path creation - #[error("Could not create path for dump images '{}'", path.display())] - Folder { - /// Path of the folder - path: PathBuf, - /// Error source - source: io::Error, - }, - - /// Error during file dump - #[error("Could not write image dump file '{}'", filename.display())] - DumpImage { - /// Path of the file write failed - filename: PathBuf, - /// Error source - source: image::ImageError, - }, -} - -/// Dump some images in a folder specified by the path. -#[profiling::function] -pub fn dump_images<'a, Img, P, Container>(path: &str, images: Img) -> Result<(), SubError> -where - P: Pixel + PixelWithColorType + 'a, - [P::Subpixel]: EncodableLayout, - Container: Deref + 'a, - Img: IntoIterator>, -{ - let folder_path = PathBuf::from(path); - - // create path if not exist - if !folder_path.is_dir() { - create_dir_all(folder_path.as_path()).map_err(|source| DumpError::Folder { - path: folder_path.clone(), - source, - })?; - } - - images - .into_iter() - .enumerate() - .try_for_each(move |(i, img)| { - let mut filepath = folder_path.clone(); - filepath.push(format!("{i:06}.png")); - dump_image(&filepath, img).map_err(|source| DumpError::DumpImage { - filename: filepath, - source, - }) - })?; - - Ok(()) -} - -/// Dump one image -#[profiling::function] -fn dump_image( - filename: P, - image: &image::ImageBuffer, // image::Luma, Vec -) -> Result<(), image::ImageError> -where - P: AsRef, - Pix: Pixel + PixelWithColorType, - [Pix::Subpixel]: EncodableLayout, - Container: Deref, -{ - image.save(filename) -} +pub use utils::{dump_images, DumpError}; diff --git a/src/image/utils.rs b/src/image/utils.rs new file mode 100644 index 0000000..d8059cb --- /dev/null +++ b/src/image/utils.rs @@ -0,0 +1,81 @@ +use image::{EncodableLayout, Pixel, PixelWithColorType}; +use std::{ + fs::create_dir_all, + io, + ops::Deref, + path::{Path, PathBuf}, +}; +use thiserror::Error; + +use crate::SubError; + +/// Handle Error for image dump. +#[derive(Error, Debug)] +pub enum DumpError { + /// Error with path creation + #[error("Could not create path for dump images '{}'", path.display())] + Folder { + /// Path of the folder + path: PathBuf, + /// Error source + source: io::Error, + }, + + /// Error during file dump + #[error("Could not write image dump file '{}'", filename.display())] + DumpImage { + /// Path of the file write failed + filename: PathBuf, + /// Error source + source: image::ImageError, + }, +} + +/// Dump some images in a folder specified by the path. +#[profiling::function] +pub fn dump_images<'a, Img, P, Container>(path: &str, images: Img) -> Result<(), SubError> +where + P: Pixel + PixelWithColorType + 'a, + [P::Subpixel]: EncodableLayout, + Container: Deref + 'a, + Img: IntoIterator>, +{ + let folder_path = PathBuf::from(path); + + // create path if not exist + if !folder_path.is_dir() { + create_dir_all(folder_path.as_path()).map_err(|source| DumpError::Folder { + path: folder_path.clone(), + source, + })?; + } + + images + .into_iter() + .enumerate() + .try_for_each(move |(i, img)| { + let mut filepath = folder_path.clone(); + filepath.push(format!("{i:06}.png")); + dump_image(&filepath, img).map_err(|source| DumpError::DumpImage { + filename: filepath, + source, + }) + })?; + + Ok(()) +} + +/// Dump one image +#[profiling::function] +fn dump_image( + filename: P, + image: &image::ImageBuffer, // image::Luma, Vec +) -> Result<(), image::ImageError> +where + P: AsRef, + Pix: Pixel + PixelWithColorType, + [Pix::Subpixel]: EncodableLayout, + Container: Deref, +{ + image.save(filename) +}