1 extern crate nihav_core;
3 use nihav_core::frame::*;
4 use std::io::prelude::*;
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);
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);
22 let mut pad: Vec<u8> = Vec::with_capacity((w - w2 * 2) / 2);
23 pad.resize((w - w2 * 2) / 2, 0xFF);
25 let line = &dta[idx..idx2];
26 ofile.write_all(line).unwrap();
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);
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();
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();
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 offs: [usize; 3] = [
61 buf.get_info().get_format().get_chromaton(0).unwrap().get_offset() as usize,
62 buf.get_info().get_format().get_chromaton(1).unwrap().get_offset() as usize,
63 buf.get_info().get_format().get_chromaton(2).unwrap().get_offset() as usize
66 let mut line: Vec<u8> = Vec::with_capacity(w * 3);
67 line.resize(w * 3, 0);
69 let src = &dta[idx..(idx+w)];
71 let pix = src[x] as usize;
72 line[x * 3 + 0] = dta[paloff + pix * 3 + offs[0]];
73 line[x * 3 + 1] = dta[paloff + pix * 3 + offs[1]];
74 line[x * 3 + 2] = dta[paloff + pix * 3 + offs[2]];
76 ofile.write_all(line.as_slice()).unwrap();
81 pub fn write_ppm(pfx: &str, strno: usize, num: u64, frm: Ref<NAFrame>) {
82 let name = format!("{}out{:02}_{:08}.ppm", pfx, strno, num);
83 let mut ofile = File::create(name).unwrap();
84 if let NABufferType::VideoPacked(ref buf) = frm.get_buffer() {
85 let (w, h) = buf.get_dimensions(0);
86 let hdr = format!("P6\n{} {}\n255\n", w, h);
87 ofile.write_all(hdr.as_bytes()).unwrap();
88 let dta = buf.get_data();
89 let stride = buf.get_stride(0);
90 let offs: [usize; 3] = [
91 buf.get_info().get_format().get_chromaton(0).unwrap().get_offset() as usize,
92 buf.get_info().get_format().get_chromaton(1).unwrap().get_offset() as usize,
93 buf.get_info().get_format().get_chromaton(2).unwrap().get_offset() as usize
95 let step = buf.get_info().get_format().get_elem_size() as usize;
96 let mut line: Vec<u8> = Vec::with_capacity(w * 3);
97 line.resize(w * 3, 0);
98 for src in dta.chunks(stride) {
100 line[x * 3 + 0] = src[x * step + offs[0]];
101 line[x * 3 + 1] = src[x * step + offs[1]];
102 line[x * 3 + 2] = src[x * step + offs[2]];
104 ofile.write_all(line.as_slice()).unwrap();
106 } else if let NABufferType::Video16(ref buf) = frm.get_buffer() {
107 let (w, h) = buf.get_dimensions(0);
108 let hdr = format!("P6\n{} {}\n255\n", w, h);
109 ofile.write_all(hdr.as_bytes()).unwrap();
110 let dta = buf.get_data();
111 let stride = buf.get_stride(0);
112 let depths: [u8; 3] = [
113 buf.get_info().get_format().get_chromaton(0).unwrap().get_depth(),
114 buf.get_info().get_format().get_chromaton(1).unwrap().get_depth(),
115 buf.get_info().get_format().get_chromaton(2).unwrap().get_depth()
117 let masks: [u16; 3] = [
118 (1 << depths[0]) - 1,
119 (1 << depths[1]) - 1,
122 let shifts: [u8; 3] = [
123 buf.get_info().get_format().get_chromaton(0).unwrap().get_shift(),
124 buf.get_info().get_format().get_chromaton(1).unwrap().get_shift(),
125 buf.get_info().get_format().get_chromaton(2).unwrap().get_shift()
127 let mut line: Vec<u8> = Vec::with_capacity(w * 3);
128 line.resize(w * 3, 0);
129 for src in dta.chunks(stride) {
132 let r = ((elem >> shifts[0]) & masks[0]) << (8 - depths[0]);
133 let g = ((elem >> shifts[1]) & masks[1]) << (8 - depths[1]);
134 let b = ((elem >> shifts[2]) & masks[2]) << (8 - depths[2]);
135 line[x * 3 + 0] = r as u8;
136 line[x * 3 + 1] = g as u8;
137 line[x * 3 + 2] = b as u8;
139 ofile.write_all(line.as_slice()).unwrap();
142 panic!(" unhandled buf format");