handle flipped image output correctly
[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);
3daa5fbb
KS
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 }
3660c127
KS
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);
e47ee411 26 let pad: Vec<u8> = vec![0xFF; (w - w2 * 2) / 2];
0a90d212
KS
27 if !is_flipped {
28 let ylines = dta.chunks(ls).take(h);
29 for line in ylines {
30 ofile.write_all(&line[..w]).unwrap();
31 }
32 } else {
33 let ylines = dta[..h * ls].chunks(ls).rev();
34 for line in ylines {
35 ofile.write_all(&line[..w]).unwrap();
36 }
3660c127 37 }
0a90d212 38 let base1 = buf.get_offset(1);
3660c127 39 let stride1 = buf.get_stride(1);
0a90d212 40 let base2 = buf.get_offset(2);
3660c127 41 let stride2 = buf.get_stride(2);
0a90d212
KS
42 let u = &dta[base1..][..h2*stride1];
43 let v = &dta[base2..][..h2*stride2];
44 if !is_flipped {
45 for (uline, vline) in u.chunks(stride1).zip(v.chunks(stride2)) {
46 ofile.write_all(&uline[..w2]).unwrap();
47 ofile.write_all(pad.as_slice()).unwrap();
3660c127 48
0a90d212
KS
49 ofile.write_all(&vline[..w2]).unwrap();
50 ofile.write_all(pad.as_slice()).unwrap();
51 }
52 } else {
53 for (uline, vline) in u.chunks(stride1).rev().zip(v.chunks(stride2).rev()) {
54 ofile.write_all(&uline[..w2]).unwrap();
55 ofile.write_all(pad.as_slice()).unwrap();
3660c127 56
0a90d212
KS
57 ofile.write_all(&vline[..w2]).unwrap();
58 ofile.write_all(pad.as_slice()).unwrap();
59 }
3660c127 60 }
3daa5fbb
KS
61 if has_alpha {
62 let ls = buf.get_stride(3);
63 let mut idx = buf.get_offset(3);
64 let mut idx2 = idx + w;
65 for _ in 0..h {
66 let line = &dta[idx..idx2];
67 ofile.write_all(line).unwrap();
68 idx += ls;
69 idx2 += ls;
70 }
71 }
3660c127
KS
72}
73
125ba9b3 74pub fn write_palppm(pfx: &str, strno: usize, num: u64, frm: NAFrameRef) {
cd9ece3c 75 let name = format!("{}out{:02}_{:08}.ppm", pfx, strno, num);
3660c127
KS
76 let mut ofile = File::create(name).unwrap();
77 let buf = frm.get_buffer().get_vbuf().unwrap();
78 let (w, h) = buf.get_dimensions(0);
79 let paloff = buf.get_offset(1);
80 let hdr = format!("P6\n{} {}\n255\n", w, h);
81 ofile.write_all(hdr.as_bytes()).unwrap();
82 let dta = buf.get_data();
83 let ls = buf.get_stride(0);
ffe69577
KS
84 let offs: [usize; 3] = [
85 buf.get_info().get_format().get_chromaton(0).unwrap().get_offset() as usize,
86 buf.get_info().get_format().get_chromaton(1).unwrap().get_offset() as usize,
87 buf.get_info().get_format().get_chromaton(2).unwrap().get_offset() as usize
88 ];
0a90d212
KS
89 let flipped = buf.get_info().is_flipped();
90 let mut idx = if !flipped { 0 } else { ls * (h - 1) };
e47ee411 91 let mut line: Vec<u8> = vec![0; w * 3];
3660c127
KS
92 for _ in 0..h {
93 let src = &dta[idx..(idx+w)];
94 for x in 0..w {
95 let pix = src[x] as usize;
ffe69577
KS
96 line[x * 3 + 0] = dta[paloff + pix * 3 + offs[0]];
97 line[x * 3 + 1] = dta[paloff + pix * 3 + offs[1]];
98 line[x * 3 + 2] = dta[paloff + pix * 3 + offs[2]];
3660c127
KS
99 }
100 ofile.write_all(line.as_slice()).unwrap();
0a90d212
KS
101 if !flipped {
102 idx += ls;
103 } else {
104 idx -= ls;
105 }
3660c127
KS
106 }
107}
ffe69577 108
125ba9b3 109pub fn write_ppm(pfx: &str, strno: usize, num: u64, frm: NAFrameRef) {
ffe69577
KS
110 let name = format!("{}out{:02}_{:08}.ppm", pfx, strno, num);
111 let mut ofile = File::create(name).unwrap();
112 if let NABufferType::VideoPacked(ref buf) = frm.get_buffer() {
113 let (w, h) = buf.get_dimensions(0);
114 let hdr = format!("P6\n{} {}\n255\n", w, h);
115 ofile.write_all(hdr.as_bytes()).unwrap();
116 let dta = buf.get_data();
117 let stride = buf.get_stride(0);
118 let offs: [usize; 3] = [
119 buf.get_info().get_format().get_chromaton(0).unwrap().get_offset() as usize,
120 buf.get_info().get_format().get_chromaton(1).unwrap().get_offset() as usize,
121 buf.get_info().get_format().get_chromaton(2).unwrap().get_offset() as usize
122 ];
0a90d212 123 let flipped = buf.get_info().is_flipped();
ffe69577 124 let step = buf.get_info().get_format().get_elem_size() as usize;
e47ee411 125 let mut line: Vec<u8> = vec![0; w * 3];
0a90d212
KS
126 if !flipped {
127 for src in dta.chunks(stride) {
128 for x in 0..w {
129 line[x * 3 + 0] = src[x * step + offs[0]];
130 line[x * 3 + 1] = src[x * step + offs[1]];
131 line[x * 3 + 2] = src[x * step + offs[2]];
132 }
133 ofile.write_all(line.as_slice()).unwrap();
134 }
135 } else {
136 for src in dta[..stride * h].chunks(stride).rev() {
137 for x in 0..w {
138 line[x * 3 + 0] = src[x * step + offs[0]];
139 line[x * 3 + 1] = src[x * step + offs[1]];
140 line[x * 3 + 2] = src[x * step + offs[2]];
141 }
142 ofile.write_all(line.as_slice()).unwrap();
ffe69577 143 }
ffe69577
KS
144 }
145 } else if let NABufferType::Video16(ref buf) = frm.get_buffer() {
146 let (w, h) = buf.get_dimensions(0);
147 let hdr = format!("P6\n{} {}\n255\n", w, h);
148 ofile.write_all(hdr.as_bytes()).unwrap();
149 let dta = buf.get_data();
150 let stride = buf.get_stride(0);
151 let depths: [u8; 3] = [
152 buf.get_info().get_format().get_chromaton(0).unwrap().get_depth(),
153 buf.get_info().get_format().get_chromaton(1).unwrap().get_depth(),
154 buf.get_info().get_format().get_chromaton(2).unwrap().get_depth()
155 ];
156 let masks: [u16; 3] = [
157 (1 << depths[0]) - 1,
158 (1 << depths[1]) - 1,
159 (1 << depths[2]) - 1
160 ];
161 let shifts: [u8; 3] = [
162 buf.get_info().get_format().get_chromaton(0).unwrap().get_shift(),
163 buf.get_info().get_format().get_chromaton(1).unwrap().get_shift(),
164 buf.get_info().get_format().get_chromaton(2).unwrap().get_shift()
165 ];
e47ee411 166 let mut line: Vec<u8> = vec![0; w * 3];
0a90d212
KS
167 let flipped = buf.get_info().is_flipped();
168 if !flipped {
169 for src in dta.chunks(stride) {
170 for x in 0..w {
171 let elem = src[x];
172 let r = ((elem >> shifts[0]) & masks[0]) << (8 - depths[0]);
173 let g = ((elem >> shifts[1]) & masks[1]) << (8 - depths[1]);
174 let b = ((elem >> shifts[2]) & masks[2]) << (8 - depths[2]);
175 line[x * 3 + 0] = r as u8;
176 line[x * 3 + 1] = g as u8;
177 line[x * 3 + 2] = b as u8;
178 }
179 ofile.write_all(line.as_slice()).unwrap();
180 }
181 } else {
182 for src in dta[..h * stride].chunks(stride).rev() {
183 for x in 0..w {
184 let elem = src[x];
185 let r = ((elem >> shifts[0]) & masks[0]) << (8 - depths[0]);
186 let g = ((elem >> shifts[1]) & masks[1]) << (8 - depths[1]);
187 let b = ((elem >> shifts[2]) & masks[2]) << (8 - depths[2]);
188 line[x * 3 + 0] = r as u8;
189 line[x * 3 + 1] = g as u8;
190 line[x * 3 + 2] = b as u8;
191 }
192 ofile.write_all(line.as_slice()).unwrap();
ffe69577 193 }
ffe69577
KS
194 }
195 } else {
196panic!(" unhandled buf format");
197 }
198}