Implement a mp4 renderer

This commit is contained in:
2020-09-03 22:34:56 -07:00
parent 4cd9bb86bb
commit 99cb824a36
4 changed files with 65 additions and 3 deletions

View File

@@ -1,6 +1,9 @@
use ansi_term::Color::RGB;
use crate::{consts, Material as _, SimState};
use std::fmt::Write as _;
use std::fs::File;
use std::path::PathBuf;
use y4m::{Colorspace, encode, Encoder, Frame, Ratio};
pub struct NumericTermRenderer;
@@ -63,6 +66,57 @@ impl ColorTermRenderer {
}
write!(&mut buf, "\n");
}
println!("{}\ntime: {:.3e}", buf, state.time());
println!("{}\ntime: {:.3e} (fr {})", buf, state.time(), state.step_no());
}
}
pub struct Y4MRenderer {
out_path: PathBuf,
encoder: Option<Encoder<File>>,
}
impl Y4MRenderer {
pub fn new<S: Into<PathBuf>>(output: S) -> Self {
Self {
out_path: output.into(),
encoder: None,
}
}
pub fn render(&mut self, state: &SimState) {
if self.encoder.is_none() {
let writer = File::create(&self.out_path).unwrap();
self.encoder = Some(encode(state.width(), state.height(), Ratio::new(30, 1))
.with_colorspace(Colorspace::C444)
.write_header(writer)
.unwrap()
);
}
let mut pix_y = Vec::new();
let mut pix_u = Vec::new();
let mut pix_v = Vec::new();
for y in 0..state.height() {
for x in 0..state.width() {
let cell = state.get(x, y);
//let r = norm_color(cell.bz() * consts::C);
//let r = 0;
let r = norm_color(cell.mat().mz()*1.0e-2);
let b = (55.0*cell.mat().conductivity()).min(255.0) as u8;
//let b = 0;
//let b = norm_color(cell.ey());
//let g = 0;
//let g = norm_color(cell.ex());
//let g = norm_color(curl(cell.ex(), cell.ey()));
let g = norm_color((cell.bz() * 1.0e4).into());
//let g = norm_color(cell.ey().into());
pix_y.push(r);
pix_u.push(g);
pix_v.push(b);
}
}
let frame = Frame::new([&*pix_y, &*pix_u, &*pix_v], None);
let enc = self.encoder.as_mut().unwrap();
enc.write_frame(&frame).unwrap()
}
}