From 5cfddd386f99060eeeede33b35caf4ff87c7bcc7 Mon Sep 17 00:00:00 2001 From: Philippe Llerena Date: Sun, 9 Jul 2023 14:09:10 +0200 Subject: [PATCH] refactor exr metadata module --- src/exr_metadata.rs | 18 ++++++++++++++---- src/lib.rs | 25 +++++++++++++++++-------- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/exr_metadata.rs b/src/exr_metadata.rs index 0699fe6..3ddf353 100644 --- a/src/exr_metadata.rs +++ b/src/exr_metadata.rs @@ -1,7 +1,8 @@ use exr::prelude::*; -/// Print the metadatas of a an EXR using a path as an input -pub fn read_meta(path: String) { +/// Print the metadatas of a an EXR using a path as an input, the output is +/// trying to the closer as `rvls -l` without the formating +pub fn read_meta(path: String) -> String { let msg = format!("run path `{}` to generate the required file", &path); let meta_data = MetaData::read_from_file( &path, @@ -9,10 +10,19 @@ pub fn read_meta(path: String) { ) .expect(&msg); // Iterate over the headers + let mut metadata: Vec = Vec::new(); for (layer_index, image_layer) in meta_data.headers.iter().enumerate() { - println!( + metadata.push(format!( "{} layer #{} size:{:?}; channels:{:?}", &path, layer_index, image_layer.layer_size, image_layer.channels - ); + )); } + // Join the headers + metadata.join(",") +} +#[test] +fn test_read_meta() { + let source = "./samples/big/RenderPass_Beauty_1_00000.exr".to_string(); + let expect = "./samples/big/RenderPass_Beauty_1_00000.exr layer #0 size:Vec2(320, 143); channels:ChannelList { list: [ChannelDescription { name: exr::Text(\"A\"), sample_type: F16, quantize_linearly: false, sampling: Vec2(1, 1) }, ChannelDescription { name: exr::Text(\"B\"), sample_type: F16, quantize_linearly: false, sampling: Vec2(1, 1) }, ChannelDescription { name: exr::Text(\"G\"), sample_type: F16, quantize_linearly: false, sampling: Vec2(1, 1) }, ChannelDescription { name: exr::Text(\"Plane_Beauty.A\"), sample_type: F16, quantize_linearly: false, sampling: Vec2(1, 1) }, ChannelDescription { name: exr::Text(\"Plane_Beauty.B\"), sample_type: F16, quantize_linearly: false, sampling: Vec2(1, 1) }, ChannelDescription { name: exr::Text(\"Plane_Beauty.G\"), sample_type: F16, quantize_linearly: false, sampling: Vec2(1, 1) }, ChannelDescription { name: exr::Text(\"Plane_Beauty.R\"), sample_type: F16, quantize_linearly: false, sampling: Vec2(1, 1) }, ChannelDescription { name: exr::Text(\"R\"), sample_type: F16, quantize_linearly: false, sampling: Vec2(1, 1) }, ChannelDescription { name: exr::Text(\"Spheres_Beauty.A\"), sample_type: F16, quantize_linearly: false, sampling: Vec2(1, 1) }, ChannelDescription { name: exr::Text(\"Spheres_Beauty.B\"), sample_type: F16, quantize_linearly: false, sampling: Vec2(1, 1) }, ChannelDescription { name: exr::Text(\"Spheres_Beauty.G\"), sample_type: F16, quantize_linearly: false, sampling: Vec2(1, 1) }, ChannelDescription { name: exr::Text(\"Spheres_Beauty.R\"), sample_type: F16, quantize_linearly: false, sampling: Vec2(1, 1) }], bytes_per_pixel: 24, uniform_sample_type: Some(F16) }".to_string(); + assert_eq!(expect, read_meta(source)); } diff --git a/src/lib.rs b/src/lib.rs index b723622..bf6c43b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -196,9 +196,14 @@ fn create_frame_string(value: Vec) -> String { convert_vec_to_str(group_continuity) } #[test] -fn test_create_frame_string(){ - let source: Vec = vec!["001".to_string(), "005".to_string(), "003".to_string(),"002".to_string()]; - let expected: String ="1-3,5".to_string(); +fn test_create_frame_string() { + let source: Vec = vec![ + "001".to_string(), + "005".to_string(), + "003".to_string(), + "002".to_string(), + ]; + let expected: String = "1-3,5".to_string(); assert_eq!(expected, create_frame_string(source)); } /// ## Basic listing of the library @@ -224,11 +229,13 @@ pub fn basic_listing(frames: Vec) -> Vec { } /// This function is intented to check if a file is an exr to call exr module /// and print the exr metadata of the file -fn get_exr_metada(re: &Regex, root_path: &String, path: &String){ +fn get_exr_metada(re: &Regex, root_path: &String, path: &String) -> String { if re.is_match(&path) { let path = format!("{}{}", root_path, path); - read_meta(path); - }; + read_meta(path) + } else { + "Not an exr".to_string() + } } /// ## Extended function of the Library /// ### Description @@ -244,17 +251,19 @@ pub fn extended_listing(root_path: String, frames: Vec) -> Vec { let re: Regex = Regex::new(r".*.exr$").unwrap(); let frames_dict: HashMap> = parse_result(frames); let mut out_frames: Vec = Vec::new(); + let mut medata: Vec = Vec::new(); for (key, value) in frames_dict { if value[0] == "None" && value.len() == 1 { - get_exr_metada(&re, &root_path, &key); + medata.push(get_exr_metada(&re, &root_path, &key)); out_frames.push(key); } else { let to = value.first().unwrap(); let from = String::from_utf8(vec![b'*'; to.len()]).unwrap(); let new_path = &key.replace(&from, to); - get_exr_metada(&re, &root_path, &new_path); + medata.push(get_exr_metada(&re, &root_path, &new_path)); out_frames.push(format!("{}@{}", key, create_frame_string(value))); } } + out_frames.append(&mut medata); out_frames }