-
Notifications
You must be signed in to change notification settings - Fork 0
/
trace.rs
92 lines (81 loc) · 2.97 KB
/
trace.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//!
//! Runs some handwritten blur functions using the TraceImage type to generate replay
//! visualisations.
//!
//! Example command line:
//!
//! $ cargo run --release --example trace -- -o /some/directory
//!
use std::path::PathBuf;
use std::fs::File;
use std::io::Write;
use structopt::StructOpt;
use prism::{image::*, tracing::*, blur3::*};
#[derive(StructOpt, Debug)]
struct Opts {
/// Example files are written to this directory.
#[structopt(short = "o", long = "output", parse(from_os_str))]
output_dir: PathBuf
}
fn write_html_page(dir: &PathBuf, path: &str, images: &[(&str, PathBuf)]) -> std::io::Result<()> {
let mut html = File::create(dir.join(path))?;
writeln!(html, "<html>")?;
writeln!(html, "<body>")?;
for image in images {
writeln!(html, "<p>{}<p/>", image.0)?;
writeln!(html, "<img src='{}'/>", image.1.to_string_lossy())?;
writeln!(html, "<br><br>")?;
}
writeln!(html, "</body>")?;
writeln!(html, "</html>")?;
Ok(())
}
fn gradient_image(width: usize, height: usize) -> GrayImage {
let mut image = GrayImage::new(width, height);
for y in 0..image.height() {
for x in 0..image.width() {
image.set(x, y, (10 * (x % 10 + y % 10) as u8) + 50);
}
}
image
}
// Appplies function to input image, creates visualistion from the trace, writes replay
// to disk and returns the path to this file
fn visualise<F>(dir: &PathBuf, name: &str, image: &GrayImage, f: F, delay_in_ms: u16) -> std::io::Result<PathBuf>
where F: Fn(&mut TraceImageFactory, &TraceImage) -> TraceImage
{
let mut t = TraceImageFactory::new();
let image = t.create_from_image(&image);
let _ = f(&mut t, &image);
let image_path = dir.join(name.to_owned() + ".gif");
write_replay_animation(&image_path, &t.trace, delay_in_ms)?;
Ok(image_path)
}
fn main() -> std::io::Result<()> {
let opts = Opts::from_args();
let dir = &opts.output_dir;
let replays = vec![
(
"Dimensions: y, x. Compute at blur_v.x, store at blur_v.x",
visualise(dir, "inline", &gradient_image(5, 6), |t, i| blur3_inline(t, i), 60)?
),
(
"Dimensions: y, x. Compute at root, store at root",
visualise(dir, "intermediate", &gradient_image(5, 6), |t, i| blur3_intermediate(t, i), 60)?
),
(
"Dimensions: y, x. Compute at blur_v.x, store at root",
visualise(dir, "local_intermediate", &gradient_image(5, 6), |t, i| blur3_local_intermediate(t, i), 60)?
),
(
"Dimensions: yo, y, x. Compute at blur_v.yo, store at blur_v.yo",
visualise(dir, "stripped", &gradient_image(5, 6), |t, i| blur3_split_y(t, i, 2), 60)?
),
(
"Dimension: yo, xo, y, x. Compute at blur_v.xo, store at blur_v.xo",
visualise(dir, "tiled", &gradient_image(9, 6), |t, i| blur3_tiled(t, i, 3, 3), 20)?
),
];
write_html_page(dir, "traces.html", &replays)?;
Ok(())
}