From bda42922e4eafd76e792067a0a4f13c4fa9cfa47 Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Sat, 26 Oct 2019 12:00:28 -0700 Subject: [PATCH] rtiow: new data.json format to support better debugging. --- rtiow/src/output.rs | 79 +++++++++++++++++++++++++++++++-------------- 1 file changed, 54 insertions(+), 25 deletions(-) diff --git a/rtiow/src/output.rs b/rtiow/src/output.rs index 9d29d5f..d4f9a61 100644 --- a/rtiow/src/output.rs +++ b/rtiow/src/output.rs @@ -24,6 +24,23 @@ lazy_static! { static ref DEBUGGER: Arc> = Arc::new(Mutex::new(Debugger::new())); } +#[derive(Serialize)] +struct ImageMetadata { + name: String, + image: String, + binary: String, + ratio: f32, + size: (usize, usize), + format: ImageType, +} +#[derive(Serialize)] +#[serde(rename_all = "camelCase")] +struct Data { + timestamp: i64, + image_metadata: Vec, +} + +#[derive(Clone, Copy, Serialize)] pub enum ImageType { RGB01, Grey01, @@ -103,13 +120,24 @@ pub fn write_images>(output_dir: P) -> std::io::Result<()> { // Write out images in consistent order. let mut names = debugger.images.keys().collect::>(); names.sort(); - let mut ratio = 1.; - let mut size = (0, 0); + let mut image_metadata = Vec::new(); for name in &names { let (it, img) = debugger.images.get(*name).unwrap(); - let filename = format!("{}.png", name); - let path = output_dir.join(filename); - info!("Saving {}", path.to_string_lossy()); + let image = format!("{}.png", name); + let binary = format!("{}.json", name); + let ratio = img.w as f32 / img.h as f32; + let size = (img.w, img.h); + let image_path = output_dir.join(&image); + let binary_path = output_dir.join(&binary); + image_metadata.push(ImageMetadata { + name: name.to_string(), + image, + binary, + ratio, + size, + format: *it, + }); + info!("Saving {}", image_path.to_string_lossy()); match it { ImageType::RGB01 => { let mut out_img = image::RgbImage::new(img.w as u32, img.h as u32); @@ -124,11 +152,7 @@ pub fn write_images>(output_dir: P) -> std::io::Result<()> { (pixel[2] * 255.).min(255.) as u8, ]) }); - if *name == MAIN_IMAGE { - ratio = img.w as f32 / img.h as f32; - size = (img.w, img.h); - } - out_img.save(path)?; + out_img.save(image_path)?; } ImageType::Grey01 => { let mut out_img = image::GrayImage::new(img.w as u32, img.h as u32); @@ -139,13 +163,12 @@ pub fn write_images>(output_dir: P) -> std::io::Result<()> { let pixel = img.pix[i]; *p = image::Luma([(pixel[0] * 255.).min(255.) as u8]) }); - out_img.save(path)?; + out_img.save(image_path)?; } ImageType::GreyNormalized => { let mut out_img = image::GrayImage::new(img.w as u32, img.h as u32); let max_val = img.pix.iter().map(|v| v.x).fold(0., f32::max); - dbg!(&max_val); out_img .enumerate_pixels_mut() .enumerate() @@ -153,27 +176,33 @@ pub fn write_images>(output_dir: P) -> std::io::Result<()> { let pixel = img.pix[i]; *p = image::Luma([(pixel[0] / max_val * 255.).min(255.) as u8]) }); - out_img.save(path)?; + out_img.save(image_path)?; + } + }; + info!("Saving {}", binary_path.to_string_lossy()); + let f = File::create(output_dir.join(binary_path))?; + match it { + ImageType::RGB01 => { + serde_json::ser::to_writer( + f, + &img.pix + .iter() + .map(|v| [v.x, v.y, v.z]) + .collect::>(), + )?; + } + ImageType::Grey01 | ImageType::GreyNormalized => { + // TODO(wathiede): make grey scale + serde_json::ser::to_writer(f, &img.pix.iter().map(|v| v.x).collect::>())?; } }; } let f = File::create(output_dir.join("data.json"))?; - #[derive(Serialize)] - struct Data { - timestamp: i64, - ratio: f32, - size: (usize, usize), - names: Vec, - images: Vec, - } serde_json::ser::to_writer( f, &Data { timestamp: now.timestamp(), - ratio, - size, - names: names.iter().map(|s| s.to_string()).collect(), - images: names.iter().map(|s| format!("{}.png", s)).collect(), + image_metadata, }, )?; Ok(())