output alpha part of the PGMYUV too
[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 has_alpha = buf.get_info().get_format().has_alpha();
16 let tot_h;
17 if has_alpha {
18 tot_h = h * 2 + h2;
19 } else {
20 tot_h = h + h2;
21 }
22 let hdr = format!("P5\n{} {}\n255\n", w, tot_h);
23 ofile.write_all(hdr.as_bytes()).unwrap();
24 let dta = buf.get_data();
25 let ls = buf.get_stride(0);
26 let mut idx = 0;
27 let mut idx2 = w;
28 let mut pad: Vec<u8> = Vec::with_capacity((w - w2 * 2) / 2);
29 pad.resize((w - w2 * 2) / 2, 0xFF);
30 for _ in 0..h {
31 let line = &dta[idx..idx2];
32 ofile.write_all(line).unwrap();
33 idx += ls;
34 idx2 += ls;
35 }
36 let mut base1 = buf.get_offset(1);
37 let stride1 = buf.get_stride(1);
38 let mut base2 = buf.get_offset(2);
39 let stride2 = buf.get_stride(2);
40 for _ in 0..h2 {
41 let bend1 = base1 + w2;
42 let line = &dta[base1..bend1];
43 ofile.write_all(line).unwrap();
44 ofile.write_all(pad.as_slice()).unwrap();
45
46 let bend2 = base2 + w2;
47 let line = &dta[base2..bend2];
48 ofile.write_all(line).unwrap();
49 ofile.write_all(pad.as_slice()).unwrap();
50
51 base1 += stride1;
52 base2 += stride2;
53 }
54 if has_alpha {
55 let ls = buf.get_stride(3);
56 let mut idx = buf.get_offset(3);
57 let mut idx2 = idx + w;
58 for _ in 0..h {
59 let line = &dta[idx..idx2];
60 ofile.write_all(line).unwrap();
61 idx += ls;
62 idx2 += ls;
63 }
64 }
65 }
66
67 pub fn write_palppm(pfx: &str, strno: usize, num: u64, frm: Ref<NAFrame>) {
68 let name = format!("{}out{:02}_{:08}.ppm", pfx, strno, num);
69 let mut ofile = File::create(name).unwrap();
70 let buf = frm.get_buffer().get_vbuf().unwrap();
71 let (w, h) = buf.get_dimensions(0);
72 let paloff = buf.get_offset(1);
73 let hdr = format!("P6\n{} {}\n255\n", w, h);
74 ofile.write_all(hdr.as_bytes()).unwrap();
75 let dta = buf.get_data();
76 let ls = buf.get_stride(0);
77 let offs: [usize; 3] = [
78 buf.get_info().get_format().get_chromaton(0).unwrap().get_offset() as usize,
79 buf.get_info().get_format().get_chromaton(1).unwrap().get_offset() as usize,
80 buf.get_info().get_format().get_chromaton(2).unwrap().get_offset() as usize
81 ];
82 let mut idx = 0;
83 let mut line: Vec<u8> = Vec::with_capacity(w * 3);
84 line.resize(w * 3, 0);
85 for _ in 0..h {
86 let src = &dta[idx..(idx+w)];
87 for x in 0..w {
88 let pix = src[x] as usize;
89 line[x * 3 + 0] = dta[paloff + pix * 3 + offs[0]];
90 line[x * 3 + 1] = dta[paloff + pix * 3 + offs[1]];
91 line[x * 3 + 2] = dta[paloff + pix * 3 + offs[2]];
92 }
93 ofile.write_all(line.as_slice()).unwrap();
94 idx += ls;
95 }
96 }
97
98 pub fn write_ppm(pfx: &str, strno: usize, num: u64, frm: Ref<NAFrame>) {
99 let name = format!("{}out{:02}_{:08}.ppm", pfx, strno, num);
100 let mut ofile = File::create(name).unwrap();
101 if let NABufferType::VideoPacked(ref buf) = frm.get_buffer() {
102 let (w, h) = buf.get_dimensions(0);
103 let hdr = format!("P6\n{} {}\n255\n", w, h);
104 ofile.write_all(hdr.as_bytes()).unwrap();
105 let dta = buf.get_data();
106 let stride = buf.get_stride(0);
107 let offs: [usize; 3] = [
108 buf.get_info().get_format().get_chromaton(0).unwrap().get_offset() as usize,
109 buf.get_info().get_format().get_chromaton(1).unwrap().get_offset() as usize,
110 buf.get_info().get_format().get_chromaton(2).unwrap().get_offset() as usize
111 ];
112 let step = buf.get_info().get_format().get_elem_size() as usize;
113 let mut line: Vec<u8> = Vec::with_capacity(w * 3);
114 line.resize(w * 3, 0);
115 for src in dta.chunks(stride) {
116 for x in 0..w {
117 line[x * 3 + 0] = src[x * step + offs[0]];
118 line[x * 3 + 1] = src[x * step + offs[1]];
119 line[x * 3 + 2] = src[x * step + offs[2]];
120 }
121 ofile.write_all(line.as_slice()).unwrap();
122 }
123 } else if let NABufferType::Video16(ref buf) = frm.get_buffer() {
124 let (w, h) = buf.get_dimensions(0);
125 let hdr = format!("P6\n{} {}\n255\n", w, h);
126 ofile.write_all(hdr.as_bytes()).unwrap();
127 let dta = buf.get_data();
128 let stride = buf.get_stride(0);
129 let depths: [u8; 3] = [
130 buf.get_info().get_format().get_chromaton(0).unwrap().get_depth(),
131 buf.get_info().get_format().get_chromaton(1).unwrap().get_depth(),
132 buf.get_info().get_format().get_chromaton(2).unwrap().get_depth()
133 ];
134 let masks: [u16; 3] = [
135 (1 << depths[0]) - 1,
136 (1 << depths[1]) - 1,
137 (1 << depths[2]) - 1
138 ];
139 let shifts: [u8; 3] = [
140 buf.get_info().get_format().get_chromaton(0).unwrap().get_shift(),
141 buf.get_info().get_format().get_chromaton(1).unwrap().get_shift(),
142 buf.get_info().get_format().get_chromaton(2).unwrap().get_shift()
143 ];
144 let mut line: Vec<u8> = Vec::with_capacity(w * 3);
145 line.resize(w * 3, 0);
146 for src in dta.chunks(stride) {
147 for x in 0..w {
148 let elem = src[x];
149 let r = ((elem >> shifts[0]) & masks[0]) << (8 - depths[0]);
150 let g = ((elem >> shifts[1]) & masks[1]) << (8 - depths[1]);
151 let b = ((elem >> shifts[2]) & masks[2]) << (8 - depths[2]);
152 line[x * 3 + 0] = r as u8;
153 line[x * 3 + 1] = g as u8;
154 line[x * 3 + 2] = b as u8;
155 }
156 ofile.write_all(line.as_slice()).unwrap();
157 }
158 } else {
159 panic!(" unhandled buf format");
160 }
161 }