-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
clean: move image dump code in a utils sub module
... of image module
- Loading branch information
Showing
2 changed files
with
83 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Target = [P::Subpixel]> + 'a, | ||
Img: IntoIterator<Item = &'a image::ImageBuffer<P, Container>>, | ||
{ | ||
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<P, Pix, Container>( | ||
filename: P, | ||
image: &image::ImageBuffer<Pix, Container>, // image::Luma<u8>, Vec<u8> | ||
) -> Result<(), image::ImageError> | ||
where | ||
P: AsRef<Path>, | ||
Pix: Pixel + PixelWithColorType, | ||
[Pix::Subpixel]: EncodableLayout, | ||
Container: Deref<Target = [Pix::Subpixel]>, | ||
{ | ||
image.save(filename) | ||
} | ||
pub use utils::{dump_images, DumpError}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Target = [P::Subpixel]> + 'a, | ||
Img: IntoIterator<Item = &'a image::ImageBuffer<P, Container>>, | ||
{ | ||
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<P, Pix, Container>( | ||
filename: P, | ||
image: &image::ImageBuffer<Pix, Container>, // image::Luma<u8>, Vec<u8> | ||
) -> Result<(), image::ImageError> | ||
where | ||
P: AsRef<Path>, | ||
Pix: Pixel + PixelWithColorType, | ||
[Pix::Subpixel]: EncodableLayout, | ||
Container: Deref<Target = [Pix::Subpixel]>, | ||
{ | ||
image.save(filename) | ||
} |