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