| 1 |
extern crate resize; |
| 2 |
extern crate y4m; |
| 3 |
|
| 4 |
use resize::Pixel::Gray8; |
| 5 |
use resize::Type::Triangle; |
| 6 |
use std::env; |
| 7 |
use std::fs::File; |
| 8 |
use std::io; |
| 9 |
|
| 10 |
fn main() { |
| 11 |
let args: Vec<_> = env::args().collect(); |
| 12 |
if args.len() != 4 { |
| 13 |
return println!("Usage: {} in.y4m WxH out.y4m", args[0]); |
| 14 |
} |
| 15 |
|
| 16 |
let mut infh: Box<dyn io::Read> = if args[1] == "-" { |
| 17 |
Box::new(io::stdin()) |
| 18 |
} else { |
| 19 |
Box::new(File::open(&args[1]).unwrap()) |
| 20 |
}; |
| 21 |
let mut decoder = y4m::decode(&mut infh).unwrap(); |
| 22 |
|
| 23 |
if decoder.get_bit_depth() != 8 { |
| 24 |
panic!( |
| 25 |
"Unsupported bit depth {}, this example only supports 8.", |
| 26 |
decoder.get_bit_depth() |
| 27 |
); |
| 28 |
} |
| 29 |
let (w1, h1) = (decoder.get_width(), decoder.get_height()); |
| 30 |
let dst_dims: Vec<_> = args[2].split("x").map(|s| s.parse().unwrap()).collect(); |
| 31 |
let (w2, h2) = (dst_dims[0], dst_dims[1]); |
| 32 |
let mut resizer = resize::new(w1, h1, w2, h2, Gray8, Triangle); |
| 33 |
let mut dst = vec![0; w2 * h2]; |
| 34 |
|
| 35 |
let mut outfh: Box<dyn io::Write> = if args[3] == "-" { |
| 36 |
Box::new(io::stdout()) |
| 37 |
} else { |
| 38 |
Box::new(File::create(&args[3]).unwrap()) |
| 39 |
}; |
| 40 |
let mut encoder = y4m::encode(w2, h2, decoder.get_framerate()) |
| 41 |
.with_colorspace(y4m::Colorspace::Cmono) |
| 42 |
.write_header(&mut outfh) |
| 43 |
.unwrap(); |
| 44 |
|
| 45 |
loop { |
| 46 |
match decoder.read_frame() { |
| 47 |
Ok(frame) => { |
| 48 |
resizer.resize(frame.get_y_plane(), &mut dst); |
| 49 |
let out_frame = y4m::Frame::new([&dst, &[], &[]], None); |
| 50 |
if encoder.write_frame(&out_frame).is_err() { |
| 51 |
break; |
| 52 |
} |
| 53 |
} |
| 54 |
_ => break, |
| 55 |
} |
| 56 |
} |
| 57 |
} |