make frame writer handle both variants of 8-bit video buffer
[nihav-tool.git] / src / frmwriter.rs
CommitLineData
98b31ef7 1extern crate nihav_core;
3660c127 2
98b31ef7 3use nihav_core::frame::*;
3660c127
KS
4use std::io::prelude::*;
5use std::fs::File;
3660c127 6
125ba9b3 7pub fn write_pgmyuv(pfx: &str, strno: usize, num: u64, frm: NAFrameRef) {
3660c127 8 if let NABufferType::None = frm.get_buffer() { return; }
cd9ece3c 9 let name = format!("{}out{:02}_{:08}.pgm", pfx, strno, num);
3660c127
KS
10 let mut ofile = File::create(name).unwrap();
11 let buf = frm.get_buffer().get_vbuf().unwrap();
0a90d212 12 let is_flipped = buf.get_info().is_flipped();
3660c127
KS
13 let (w, h) = buf.get_dimensions(0);
14 let (w2, h2) = buf.get_dimensions(1);
f15f3836 15 let full_w = w2 * 2 > w;
3daa5fbb 16 let has_alpha = buf.get_info().get_format().has_alpha();
f15f3836 17 let mut tot_h = h + h2;
3daa5fbb 18 if has_alpha {
f15f3836
KS
19 tot_h += h;
20 }
21 if full_w {
22 tot_h += h2;
3daa5fbb 23 }
3660c127
KS
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);
f15f3836 28 let pad: Vec<u8> = vec![0xFF; if !full_w { (w - w2 * 2) / 2 } else { w - w2 } ];
0a90d212
KS
29 if !is_flipped {
30 let ylines = dta.chunks(ls).take(h);
31 for line in ylines {
32 ofile.write_all(&line[..w]).unwrap();
33 }
34 } else {
35 let ylines = dta[..h * ls].chunks(ls).rev();
36 for line in ylines {
37 ofile.write_all(&line[..w]).unwrap();
38 }
3660c127 39 }
0a90d212 40 let base1 = buf.get_offset(1);
3660c127 41 let stride1 = buf.get_stride(1);
0a90d212 42 let base2 = buf.get_offset(2);
3660c127 43 let stride2 = buf.get_stride(2);
0a90d212
KS
44 let u = &dta[base1..][..h2*stride1];
45 let v = &dta[base2..][..h2*stride2];
f15f3836
KS
46 if !full_w {
47 if !is_flipped {
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();
3660c127 51
f15f3836
KS
52 ofile.write_all(&vline[..w2]).unwrap();
53 ofile.write_all(pad.as_slice()).unwrap();
54 }
55 } else {
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();
59
60 ofile.write_all(&vline[..w2]).unwrap();
61 ofile.write_all(pad.as_slice()).unwrap();
62 }
0a90d212
KS
63 }
64 } else {
f15f3836
KS
65 if !is_flipped {
66 for uline in u.chunks(stride1) {
67 ofile.write_all(&uline[..w2]).unwrap();
68 ofile.write_all(pad.as_slice()).unwrap();
69 }
70 for vline in v.chunks(stride2) {
71 ofile.write_all(&vline[..w2]).unwrap();
72 ofile.write_all(pad.as_slice()).unwrap();
73 }
74 } else {
75 for uline in u.chunks(stride1).rev() {
76 ofile.write_all(&uline[..w2]).unwrap();
77 ofile.write_all(pad.as_slice()).unwrap();
78 }
79 for vline in v.chunks(stride2).rev() {
80 ofile.write_all(&vline[..w2]).unwrap();
81 ofile.write_all(pad.as_slice()).unwrap();
82 }
0a90d212 83 }
3660c127 84 }
3daa5fbb
KS
85 if has_alpha {
86 let ls = buf.get_stride(3);
87 let mut idx = buf.get_offset(3);
88 let mut idx2 = idx + w;
89 for _ in 0..h {
90 let line = &dta[idx..idx2];
91 ofile.write_all(line).unwrap();
92 idx += ls;
93 idx2 += ls;
94 }
95 }
3660c127
KS
96}
97
125ba9b3 98pub fn write_palppm(pfx: &str, strno: usize, num: u64, frm: NAFrameRef) {
cd9ece3c 99 let name = format!("{}out{:02}_{:08}.ppm", pfx, strno, num);
3660c127
KS
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);
ffe69577
KS
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
112 ];
0a90d212
KS
113 let flipped = buf.get_info().is_flipped();
114 let mut idx = if !flipped { 0 } else { ls * (h - 1) };
e47ee411 115 let mut line: Vec<u8> = vec![0; w * 3];
3660c127
KS
116 for _ in 0..h {
117 let src = &dta[idx..(idx+w)];
118 for x in 0..w {
119 let pix = src[x] as usize;
ffe69577
KS
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]];
3660c127
KS
123 }
124 ofile.write_all(line.as_slice()).unwrap();
0a90d212
KS
125 if !flipped {
126 idx += ls;
127 } else {
128 idx -= ls;
129 }
3660c127
KS
130 }
131}
ffe69577 132
125ba9b3 133pub fn write_ppm(pfx: &str, strno: usize, num: u64, frm: NAFrameRef) {
ffe69577
KS
134 let name = format!("{}out{:02}_{:08}.ppm", pfx, strno, num);
135 let mut ofile = File::create(name).unwrap();
7c6134cc
KS
136 let info = frm.get_buffer().get_video_info().unwrap();
137 let flipped = info.is_flipped();
138 let buffer = frm.get_buffer();
139 if let Some(ref buf) = buffer.get_vbuf() {
ffe69577
KS
140 let (w, h) = buf.get_dimensions(0);
141 let hdr = format!("P6\n{} {}\n255\n", w, h);
142 ofile.write_all(hdr.as_bytes()).unwrap();
143 let dta = buf.get_data();
144 let stride = buf.get_stride(0);
145 let offs: [usize; 3] = [
7c6134cc
KS
146 info.get_format().get_chromaton(0).unwrap().get_offset() as usize,
147 info.get_format().get_chromaton(1).unwrap().get_offset() as usize,
148 info.get_format().get_chromaton(2).unwrap().get_offset() as usize
ffe69577 149 ];
7c6134cc 150 let step = info.get_format().get_elem_size() as usize;
e47ee411 151 let mut line: Vec<u8> = vec![0; w * 3];
0a90d212
KS
152 if !flipped {
153 for src in dta.chunks(stride) {
154 for x in 0..w {
155 line[x * 3 + 0] = src[x * step + offs[0]];
156 line[x * 3 + 1] = src[x * step + offs[1]];
157 line[x * 3 + 2] = src[x * step + offs[2]];
158 }
159 ofile.write_all(line.as_slice()).unwrap();
160 }
161 } else {
162 for src in dta[..stride * h].chunks(stride).rev() {
163 for x in 0..w {
164 line[x * 3 + 0] = src[x * step + offs[0]];
165 line[x * 3 + 1] = src[x * step + offs[1]];
166 line[x * 3 + 2] = src[x * step + offs[2]];
167 }
168 ofile.write_all(line.as_slice()).unwrap();
ffe69577 169 }
ffe69577 170 }
7c6134cc 171 } else if let NABufferType::Video16(ref buf) = buffer {
ffe69577
KS
172 let (w, h) = buf.get_dimensions(0);
173 let hdr = format!("P6\n{} {}\n255\n", w, h);
174 ofile.write_all(hdr.as_bytes()).unwrap();
175 let dta = buf.get_data();
176 let stride = buf.get_stride(0);
177 let depths: [u8; 3] = [
7c6134cc
KS
178 info.get_format().get_chromaton(0).unwrap().get_depth(),
179 info.get_format().get_chromaton(1).unwrap().get_depth(),
180 info.get_format().get_chromaton(2).unwrap().get_depth()
ffe69577
KS
181 ];
182 let masks: [u16; 3] = [
183 (1 << depths[0]) - 1,
184 (1 << depths[1]) - 1,
185 (1 << depths[2]) - 1
186 ];
187 let shifts: [u8; 3] = [
7c6134cc
KS
188 info.get_format().get_chromaton(0).unwrap().get_shift(),
189 info.get_format().get_chromaton(1).unwrap().get_shift(),
190 info.get_format().get_chromaton(2).unwrap().get_shift()
ffe69577 191 ];
e47ee411 192 let mut line: Vec<u8> = vec![0; w * 3];
0a90d212
KS
193 if !flipped {
194 for src in dta.chunks(stride) {
195 for x in 0..w {
196 let elem = src[x];
197 let r = ((elem >> shifts[0]) & masks[0]) << (8 - depths[0]);
198 let g = ((elem >> shifts[1]) & masks[1]) << (8 - depths[1]);
199 let b = ((elem >> shifts[2]) & masks[2]) << (8 - depths[2]);
200 line[x * 3 + 0] = r as u8;
201 line[x * 3 + 1] = g as u8;
202 line[x * 3 + 2] = b as u8;
203 }
204 ofile.write_all(line.as_slice()).unwrap();
205 }
206 } else {
207 for src in dta[..h * stride].chunks(stride).rev() {
208 for x in 0..w {
209 let elem = src[x];
210 let r = ((elem >> shifts[0]) & masks[0]) << (8 - depths[0]);
211 let g = ((elem >> shifts[1]) & masks[1]) << (8 - depths[1]);
212 let b = ((elem >> shifts[2]) & masks[2]) << (8 - depths[2]);
213 line[x * 3 + 0] = r as u8;
214 line[x * 3 + 1] = g as u8;
215 line[x * 3 + 2] = b as u8;
216 }
217 ofile.write_all(line.as_slice()).unwrap();
ffe69577 218 }
ffe69577
KS
219 }
220 } else {
221panic!(" unhandled buf format");
222 }
223}