rtiow: new data.json format to support better debugging.
This commit is contained in:
parent
96e74b3ebf
commit
bda42922e4
@ -24,6 +24,23 @@ lazy_static! {
|
|||||||
static ref DEBUGGER: Arc<Mutex<Debugger>> = Arc::new(Mutex::new(Debugger::new()));
|
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 {
|
pub enum ImageType {
|
||||||
RGB01,
|
RGB01,
|
||||||
Grey01,
|
Grey01,
|
||||||
@ -103,13 +120,24 @@ pub fn write_images<P: AsRef<Path>>(output_dir: P) -> std::io::Result<()> {
|
|||||||
// Write out images in consistent order.
|
// Write out images in consistent order.
|
||||||
let mut names = debugger.images.keys().collect::<Vec<_>>();
|
let mut names = debugger.images.keys().collect::<Vec<_>>();
|
||||||
names.sort();
|
names.sort();
|
||||||
let mut ratio = 1.;
|
let mut image_metadata = Vec::new();
|
||||||
let mut size = (0, 0);
|
|
||||||
for name in &names {
|
for name in &names {
|
||||||
let (it, img) = debugger.images.get(*name).unwrap();
|
let (it, img) = debugger.images.get(*name).unwrap();
|
||||||
let filename = format!("{}.png", name);
|
let image = format!("{}.png", name);
|
||||||
let path = output_dir.join(filename);
|
let binary = format!("{}.json", name);
|
||||||
info!("Saving {}", path.to_string_lossy());
|
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 {
|
match it {
|
||||||
ImageType::RGB01 => {
|
ImageType::RGB01 => {
|
||||||
let mut out_img = image::RgbImage::new(img.w as u32, img.h as u32);
|
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,
|
(pixel[2] * 255.).min(255.) as u8,
|
||||||
])
|
])
|
||||||
});
|
});
|
||||||
if *name == MAIN_IMAGE {
|
out_img.save(image_path)?;
|
||||||
ratio = img.w as f32 / img.h as f32;
|
|
||||||
size = (img.w, img.h);
|
|
||||||
}
|
|
||||||
out_img.save(path)?;
|
|
||||||
}
|
}
|
||||||
ImageType::Grey01 => {
|
ImageType::Grey01 => {
|
||||||
let mut out_img = image::GrayImage::new(img.w as u32, img.h as u32);
|
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];
|
let pixel = img.pix[i];
|
||||||
*p = image::Luma([(pixel[0] * 255.).min(255.) as u8])
|
*p = image::Luma([(pixel[0] * 255.).min(255.) as u8])
|
||||||
});
|
});
|
||||||
out_img.save(path)?;
|
out_img.save(image_path)?;
|
||||||
}
|
}
|
||||||
ImageType::GreyNormalized => {
|
ImageType::GreyNormalized => {
|
||||||
let mut out_img = image::GrayImage::new(img.w as u32, img.h as u32);
|
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);
|
let max_val = img.pix.iter().map(|v| v.x).fold(0., f32::max);
|
||||||
dbg!(&max_val);
|
|
||||||
out_img
|
out_img
|
||||||
.enumerate_pixels_mut()
|
.enumerate_pixels_mut()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
@ -153,27 +176,33 @@ pub fn write_images<P: AsRef<Path>>(output_dir: P) -> std::io::Result<()> {
|
|||||||
let pixel = img.pix[i];
|
let pixel = img.pix[i];
|
||||||
*p = image::Luma([(pixel[0] / max_val * 255.).min(255.) as u8])
|
*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"))?;
|
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(
|
serde_json::ser::to_writer(
|
||||||
f,
|
f,
|
||||||
&Data {
|
&Data {
|
||||||
timestamp: now.timestamp(),
|
timestamp: now.timestamp(),
|
||||||
ratio,
|
image_metadata,
|
||||||
size,
|
|
||||||
names: names.iter().map(|s| s.to_string()).collect(),
|
|
||||||
images: names.iter().map(|s| format!("{}.png", s)).collect(),
|
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user