]> git.nihav.org Git - nihav-tool.git/blob - src/frmwriter.rs
adapt to new split crates
[nihav-tool.git] / src / frmwriter.rs
1 extern crate nihav_core;
2
3 use nihav_core::frame::*;
4 use std::io::prelude::*;
5 use std::fs::File;
6 use std::cell::Ref;
7
8 pub fn write_pgmyuv(pfx: &str, strno: usize, num: u64, frm: Ref<NAFrame>) {
9 if let NABufferType::None = frm.get_buffer() { return; }
10 let name = format!("{}out{:02}_{:08}.pgm", pfx, strno, num);
11 let mut ofile = File::create(name).unwrap();
12 let buf = frm.get_buffer().get_vbuf().unwrap();
13 let (w, h) = buf.get_dimensions(0);
14 let (w2, h2) = buf.get_dimensions(1);
15 let tot_h = h + h2;
16 let hdr = format!("P5\n{} {}\n255\n", w, tot_h);
17 ofile.write_all(hdr.as_bytes()).unwrap();
18 let dta = buf.get_data();
19 let ls = buf.get_stride(0);
20 let mut idx = 0;
21 let mut idx2 = w;
22 let mut pad: Vec<u8> = Vec::with_capacity((w - w2 * 2) / 2);
23 pad.resize((w - w2 * 2) / 2, 0xFF);
24 for _ in 0..h {
25 let line = &dta[idx..idx2];
26 ofile.write_all(line).unwrap();
27 idx += ls;
28 idx2 += ls;
29 }
30 let mut base1 = buf.get_offset(1);
31 let stride1 = buf.get_stride(1);
32 let mut base2 = buf.get_offset(2);
33 let stride2 = buf.get_stride(2);
34 for _ in 0..h2 {
35 let bend1 = base1 + w2;
36 let line = &dta[base1..bend1];
37 ofile.write_all(line).unwrap();
38 ofile.write_all(pad.as_slice()).unwrap();
39
40 let bend2 = base2 + w2;
41 let line = &dta[base2..bend2];
42 ofile.write_all(line).unwrap();
43 ofile.write_all(pad.as_slice()).unwrap();
44
45 base1 += stride1;
46 base2 += stride2;
47 }
48 }
49
50 pub fn write_palppm(pfx: &str, strno: usize, num: u64, frm: Ref<NAFrame>) {
51 let name = format!("{}out{:02}_{:08}.ppm", pfx, strno, num);
52 let mut ofile = File::create(name).unwrap();
53 let buf = frm.get_buffer().get_vbuf().unwrap();
54 let (w, h) = buf.get_dimensions(0);
55 let paloff = buf.get_offset(1);
56 let hdr = format!("P6\n{} {}\n255\n", w, h);
57 ofile.write_all(hdr.as_bytes()).unwrap();
58 let dta = buf.get_data();
59 let ls = buf.get_stride(0);
60 let mut idx = 0;
61 let mut line: Vec<u8> = Vec::with_capacity(w * 3);
62 line.resize(w * 3, 0);
63 for _ in 0..h {
64 let src = &dta[idx..(idx+w)];
65 for x in 0..w {
66 let pix = src[x] as usize;
67 line[x * 3 + 0] = dta[paloff + pix * 3 + 2];
68 line[x * 3 + 1] = dta[paloff + pix * 3 + 1];
69 line[x * 3 + 2] = dta[paloff + pix * 3 + 0];
70 }
71 ofile.write_all(line.as_slice()).unwrap();
72 idx += ls;
73 }
74 }