1 extern crate nihav_core;
3 use nihav_core::frame::*;
4 use std::io::prelude::*;
7 pub fn write_pgmyuv(pfx: &str, strno: usize, num: u64, frm: NAFrameRef) {
8 if let NABufferType::None = frm.get_buffer() { return; }
9 let name = format!("{}out{:02}_{:08}.pgm", pfx, strno, num);
10 let mut ofile = File::create(name).unwrap();
11 let buf = frm.get_buffer().get_vbuf().unwrap();
12 let is_flipped = buf.get_info().is_flipped();
13 let (w, h) = buf.get_dimensions(0);
14 let (w2, h2) = buf.get_dimensions(1);
15 let full_w = w2 * 2 > w;
16 let has_alpha = buf.get_info().get_format().has_alpha();
17 let mut tot_h = h + h2;
24 let hdr = format!("P5\n{} {}\n255\n", w, tot_h);
25 ofile.write_all(hdr.as_bytes()).unwrap();
26 let dta = buf.get_data();
27 let ls = buf.get_stride(0);
28 let pad: Vec<u8> = vec![0xFF; if !full_w { (w - w2 * 2) / 2 } else { w - w2 } ];
30 let ylines = dta.chunks(ls).take(h);
32 ofile.write_all(&line[..w]).unwrap();
35 let ylines = dta[..h * ls].chunks(ls).rev();
37 ofile.write_all(&line[..w]).unwrap();
40 let base1 = buf.get_offset(1);
41 let stride1 = buf.get_stride(1);
42 let base2 = buf.get_offset(2);
43 let stride2 = buf.get_stride(2);
44 let u = &dta[base1..][..h2*stride1];
45 let v = &dta[base2..][..h2*stride2];
48 for (uline, vline) in u.chunks(stride1).zip(v.chunks(stride2)) {
49 ofile.write_all(&uline[..w2]).unwrap();
50 ofile.write_all(pad.as_slice()).unwrap();
52 ofile.write_all(&vline[..w2]).unwrap();
53 ofile.write_all(pad.as_slice()).unwrap();
56 for (uline, vline) in u.chunks(stride1).rev().zip(v.chunks(stride2).rev()) {
57 ofile.write_all(&uline[..w2]).unwrap();
58 ofile.write_all(pad.as_slice()).unwrap();
60 ofile.write_all(&vline[..w2]).unwrap();
61 ofile.write_all(pad.as_slice()).unwrap();
66 for uline in u.chunks(stride1) {
67 ofile.write_all(&uline[..w2]).unwrap();
68 ofile.write_all(pad.as_slice()).unwrap();
70 for vline in v.chunks(stride2) {
71 ofile.write_all(&vline[..w2]).unwrap();
72 ofile.write_all(pad.as_slice()).unwrap();
75 for uline in u.chunks(stride1).rev() {
76 ofile.write_all(&uline[..w2]).unwrap();
77 ofile.write_all(pad.as_slice()).unwrap();
79 for vline in v.chunks(stride2).rev() {
80 ofile.write_all(&vline[..w2]).unwrap();
81 ofile.write_all(pad.as_slice()).unwrap();
86 let ls = buf.get_stride(3);
87 let mut idx = buf.get_offset(3);
88 let mut idx2 = idx + w;
90 let line = &dta[idx..idx2];
91 ofile.write_all(line).unwrap();
98 pub fn write_palppm(pfx: &str, strno: usize, num: u64, frm: NAFrameRef) {
99 let name = format!("{}out{:02}_{:08}.ppm", pfx, strno, num);
100 let mut ofile = File::create(name).unwrap();
101 let buf = frm.get_buffer().get_vbuf().unwrap();
102 let (w, h) = buf.get_dimensions(0);
103 let paloff = buf.get_offset(1);
104 let hdr = format!("P6\n{} {}\n255\n", w, h);
105 ofile.write_all(hdr.as_bytes()).unwrap();
106 let dta = buf.get_data();
107 let ls = buf.get_stride(0);
108 let offs: [usize; 3] = [
109 buf.get_info().get_format().get_chromaton(0).unwrap().get_offset() as usize,
110 buf.get_info().get_format().get_chromaton(1).unwrap().get_offset() as usize,
111 buf.get_info().get_format().get_chromaton(2).unwrap().get_offset() as usize
113 let flipped = buf.get_info().is_flipped();
114 let mut idx = if !flipped { 0 } else { ls * (h - 1) };
115 let mut line: Vec<u8> = vec![0; w * 3];
117 let src = &dta[idx..(idx+w)];
119 let pix = src[x] as usize;
120 line[x * 3 + 0] = dta[paloff + pix * 3 + offs[0]];
121 line[x * 3 + 1] = dta[paloff + pix * 3 + offs[1]];
122 line[x * 3 + 2] = dta[paloff + pix * 3 + offs[2]];
124 ofile.write_all(line.as_slice()).unwrap();
133 pub fn write_ppm(pfx: &str, strno: usize, num: u64, frm: NAFrameRef) {
134 let name = format!("{}out{:02}_{:08}.ppm", pfx, strno, num);
135 let mut ofile = File::create(name).unwrap();
136 if let NABufferType::VideoPacked(ref buf) = frm.get_buffer() {
137 let (w, h) = buf.get_dimensions(0);
138 let hdr = format!("P6\n{} {}\n255\n", w, h);
139 ofile.write_all(hdr.as_bytes()).unwrap();
140 let dta = buf.get_data();
141 let stride = buf.get_stride(0);
142 let offs: [usize; 3] = [
143 buf.get_info().get_format().get_chromaton(0).unwrap().get_offset() as usize,
144 buf.get_info().get_format().get_chromaton(1).unwrap().get_offset() as usize,
145 buf.get_info().get_format().get_chromaton(2).unwrap().get_offset() as usize
147 let flipped = buf.get_info().is_flipped();
148 let step = buf.get_info().get_format().get_elem_size() as usize;
149 let mut line: Vec<u8> = vec![0; w * 3];
151 for src in dta.chunks(stride) {
153 line[x * 3 + 0] = src[x * step + offs[0]];
154 line[x * 3 + 1] = src[x * step + offs[1]];
155 line[x * 3 + 2] = src[x * step + offs[2]];
157 ofile.write_all(line.as_slice()).unwrap();
160 for src in dta[..stride * h].chunks(stride).rev() {
162 line[x * 3 + 0] = src[x * step + offs[0]];
163 line[x * 3 + 1] = src[x * step + offs[1]];
164 line[x * 3 + 2] = src[x * step + offs[2]];
166 ofile.write_all(line.as_slice()).unwrap();
169 } else if let NABufferType::Video16(ref buf) = frm.get_buffer() {
170 let (w, h) = buf.get_dimensions(0);
171 let hdr = format!("P6\n{} {}\n255\n", w, h);
172 ofile.write_all(hdr.as_bytes()).unwrap();
173 let dta = buf.get_data();
174 let stride = buf.get_stride(0);
175 let depths: [u8; 3] = [
176 buf.get_info().get_format().get_chromaton(0).unwrap().get_depth(),
177 buf.get_info().get_format().get_chromaton(1).unwrap().get_depth(),
178 buf.get_info().get_format().get_chromaton(2).unwrap().get_depth()
180 let masks: [u16; 3] = [
181 (1 << depths[0]) - 1,
182 (1 << depths[1]) - 1,
185 let shifts: [u8; 3] = [
186 buf.get_info().get_format().get_chromaton(0).unwrap().get_shift(),
187 buf.get_info().get_format().get_chromaton(1).unwrap().get_shift(),
188 buf.get_info().get_format().get_chromaton(2).unwrap().get_shift()
190 let mut line: Vec<u8> = vec![0; w * 3];
191 let flipped = buf.get_info().is_flipped();
193 for src in dta.chunks(stride) {
196 let r = ((elem >> shifts[0]) & masks[0]) << (8 - depths[0]);
197 let g = ((elem >> shifts[1]) & masks[1]) << (8 - depths[1]);
198 let b = ((elem >> shifts[2]) & masks[2]) << (8 - depths[2]);
199 line[x * 3 + 0] = r as u8;
200 line[x * 3 + 1] = g as u8;
201 line[x * 3 + 2] = b as u8;
203 ofile.write_all(line.as_slice()).unwrap();
206 for src in dta[..h * stride].chunks(stride).rev() {
209 let r = ((elem >> shifts[0]) & masks[0]) << (8 - depths[0]);
210 let g = ((elem >> shifts[1]) & masks[1]) << (8 - depths[1]);
211 let b = ((elem >> shifts[2]) & masks[2]) << (8 - depths[2]);
212 line[x * 3 + 0] = r as u8;
213 line[x * 3 + 1] = g as u8;
214 line[x * 3 + 2] = b as u8;
216 ofile.write_all(line.as_slice()).unwrap();
220 panic!(" unhandled buf format");