-
Notifications
You must be signed in to change notification settings - Fork 20
/
resize.rs
61 lines (55 loc) · 1.77 KB
/
resize.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
extern crate resize;
extern crate rgb;
extern crate y4m;
use resize::Pixel::Gray8;
use resize::Type::Triangle;
use rgb::FromSlice;
use std::env;
use std::fs::File;
use std::io;
fn main() {
let args: Vec<_> = env::args().collect();
if args.len() != 4 {
return println!("Usage: {} in.y4m WxH out.y4m", args[0]);
}
let mut infh: Box<dyn io::Read> = if args[1] == "-" {
Box::new(io::stdin())
} else {
Box::new(File::open(&args[1]).unwrap())
};
let mut decoder = y4m::decode(&mut infh).unwrap();
if decoder.get_bit_depth() != 8 {
panic!(
"Unsupported bit depth {}, this example only supports 8.",
decoder.get_bit_depth()
);
}
let (w1, h1) = (decoder.get_width(), decoder.get_height());
let dst_dims: Vec<_> = args[2].split("x").map(|s| s.parse().unwrap()).collect();
let (w2, h2) = (dst_dims[0], dst_dims[1]);
let mut resizer = resize::new(w1, h1, w2, h2, Gray8, Triangle).unwrap();
let mut dst = vec![0; w2 * h2];
let mut outfh: Box<dyn io::Write> = if args[3] == "-" {
Box::new(io::stdout())
} else {
Box::new(File::create(&args[3]).unwrap())
};
let mut encoder = y4m::encode(w2, h2, decoder.get_framerate())
.with_colorspace(y4m::Colorspace::Cmono)
.write_header(&mut outfh)
.unwrap();
loop {
match decoder.read_frame() {
Ok(frame) => {
resizer
.resize(frame.get_y_plane().as_gray(), dst.as_gray_mut())
.unwrap();
let out_frame = y4m::Frame::new([&dst, &[], &[]], None);
if encoder.write_frame(&out_frame).is_err() {
break;
}
}
_ => break,
}
}
}