rtiow: new data.json format to support better debugging.

This commit is contained in:
Bill Thiede 2019-10-26 12:00:28 -07:00
parent 96e74b3ebf
commit bda42922e4

View File

@ -24,6 +24,23 @@ lazy_static! {
static ref DEBUGGER: Arc<Mutex<Debugger>> = 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<ImageMetadata>,
}
#[derive(Clone, Copy, Serialize)]
pub enum ImageType {
RGB01,
Grey01,
@ -103,13 +120,24 @@ pub fn write_images<P: AsRef<Path>>(output_dir: P) -> std::io::Result<()> {
// Write out images in consistent order.
let mut names = debugger.images.keys().collect::<Vec<_>>();
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<P: AsRef<Path>>(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<P: AsRef<Path>>(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<P: AsRef<Path>>(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::<Vec<[f32; 3]>>(),
)?;
}
ImageType::Grey01 | ImageType::GreyNormalized => {
// TODO(wathiede): make grey scale
serde_json::ser::to_writer(f, &img.pix.iter().map(|v| v.x).collect::<Vec<f32>>())?;
}
};
}
let f = File::create(output_dir.join("data.json"))?;
#[derive(Serialize)]
struct Data {
timestamp: i64,
ratio: f32,
size: (usize, usize),
names: Vec<String>,
images: Vec<String>,
}
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(())