frmwriter: handle grayscale formats too
[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; }
7437145d 9 let name = format!("{}{: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];
70183711
KS
46 let has_chroma = stride1 > 0 && stride2 > 0;
47 if !full_w && has_chroma {
f15f3836
KS
48 if !is_flipped {
49 for (uline, vline) in u.chunks(stride1).zip(v.chunks(stride2)) {
50 ofile.write_all(&uline[..w2]).unwrap();
51 ofile.write_all(pad.as_slice()).unwrap();
3660c127 52
f15f3836
KS
53 ofile.write_all(&vline[..w2]).unwrap();
54 ofile.write_all(pad.as_slice()).unwrap();
55 }
56 } else {
57 for (uline, vline) in u.chunks(stride1).rev().zip(v.chunks(stride2).rev()) {
58 ofile.write_all(&uline[..w2]).unwrap();
59 ofile.write_all(pad.as_slice()).unwrap();
60
61 ofile.write_all(&vline[..w2]).unwrap();
62 ofile.write_all(pad.as_slice()).unwrap();
63 }
0a90d212 64 }
70183711 65 } else if has_chroma {
f15f3836
KS
66 if !is_flipped {
67 for uline in u.chunks(stride1) {
68 ofile.write_all(&uline[..w2]).unwrap();
69 ofile.write_all(pad.as_slice()).unwrap();
70 }
71 for vline in v.chunks(stride2) {
72 ofile.write_all(&vline[..w2]).unwrap();
73 ofile.write_all(pad.as_slice()).unwrap();
74 }
75 } else {
76 for uline in u.chunks(stride1).rev() {
77 ofile.write_all(&uline[..w2]).unwrap();
78 ofile.write_all(pad.as_slice()).unwrap();
79 }
80 for vline in v.chunks(stride2).rev() {
81 ofile.write_all(&vline[..w2]).unwrap();
82 ofile.write_all(pad.as_slice()).unwrap();
83 }
0a90d212 84 }
3660c127 85 }
3daa5fbb
KS
86 if has_alpha {
87 let ls = buf.get_stride(3);
88 let mut idx = buf.get_offset(3);
89 let mut idx2 = idx + w;
90 for _ in 0..h {
91 let line = &dta[idx..idx2];
92 ofile.write_all(line).unwrap();
93 idx += ls;
94 idx2 += ls;
95 }
96 }
3660c127
KS
97}
98
125ba9b3 99pub fn write_palppm(pfx: &str, strno: usize, num: u64, frm: NAFrameRef) {
7437145d 100 let name = format!("{}{:02}_{:08}.ppm", pfx, strno, num);
3660c127
KS
101 let mut ofile = File::create(name).unwrap();
102 let buf = frm.get_buffer().get_vbuf().unwrap();
103 let (w, h) = buf.get_dimensions(0);
104 let paloff = buf.get_offset(1);
105 let hdr = format!("P6\n{} {}\n255\n", w, h);
106 ofile.write_all(hdr.as_bytes()).unwrap();
107 let dta = buf.get_data();
108 let ls = buf.get_stride(0);
ffe69577
KS
109 let offs: [usize; 3] = [
110 buf.get_info().get_format().get_chromaton(0).unwrap().get_offset() as usize,
111 buf.get_info().get_format().get_chromaton(1).unwrap().get_offset() as usize,
112 buf.get_info().get_format().get_chromaton(2).unwrap().get_offset() as usize
113 ];
0a90d212
KS
114 let flipped = buf.get_info().is_flipped();
115 let mut idx = if !flipped { 0 } else { ls * (h - 1) };
e47ee411 116 let mut line: Vec<u8> = vec![0; w * 3];
3660c127
KS
117 for _ in 0..h {
118 let src = &dta[idx..(idx+w)];
119 for x in 0..w {
120 let pix = src[x] as usize;
ffe69577
KS
121 line[x * 3 + 0] = dta[paloff + pix * 3 + offs[0]];
122 line[x * 3 + 1] = dta[paloff + pix * 3 + offs[1]];
123 line[x * 3 + 2] = dta[paloff + pix * 3 + offs[2]];
3660c127
KS
124 }
125 ofile.write_all(line.as_slice()).unwrap();
0a90d212
KS
126 if !flipped {
127 idx += ls;
128 } else {
129 idx -= ls;
130 }
3660c127
KS
131 }
132}
ffe69577 133
125ba9b3 134pub fn write_ppm(pfx: &str, strno: usize, num: u64, frm: NAFrameRef) {
7437145d 135 let name = format!("{}{:02}_{:08}.ppm", pfx, strno, num);
ffe69577 136 let mut ofile = File::create(name).unwrap();
7c6134cc
KS
137 let info = frm.get_buffer().get_video_info().unwrap();
138 let flipped = info.is_flipped();
139 let buffer = frm.get_buffer();
140 if let Some(ref buf) = buffer.get_vbuf() {
ffe69577
KS
141 let (w, h) = buf.get_dimensions(0);
142 let hdr = format!("P6\n{} {}\n255\n", w, h);
143 ofile.write_all(hdr.as_bytes()).unwrap();
144 let dta = buf.get_data();
145 let stride = buf.get_stride(0);
146 let offs: [usize; 3] = [
7c6134cc
KS
147 info.get_format().get_chromaton(0).unwrap().get_offset() as usize,
148 info.get_format().get_chromaton(1).unwrap().get_offset() as usize,
149 info.get_format().get_chromaton(2).unwrap().get_offset() as usize
ffe69577 150 ];
7c6134cc 151 let step = info.get_format().get_elem_size() as usize;
e47ee411 152 let mut line: Vec<u8> = vec![0; w * 3];
0a90d212
KS
153 if !flipped {
154 for src in dta.chunks(stride) {
155 for x in 0..w {
156 line[x * 3 + 0] = src[x * step + offs[0]];
157 line[x * 3 + 1] = src[x * step + offs[1]];
158 line[x * 3 + 2] = src[x * step + offs[2]];
159 }
160 ofile.write_all(line.as_slice()).unwrap();
161 }
162 } else {
163 for src in dta[..stride * h].chunks(stride).rev() {
164 for x in 0..w {
165 line[x * 3 + 0] = src[x * step + offs[0]];
166 line[x * 3 + 1] = src[x * step + offs[1]];
167 line[x * 3 + 2] = src[x * step + offs[2]];
168 }
169 ofile.write_all(line.as_slice()).unwrap();
ffe69577 170 }
ffe69577 171 }
7c6134cc 172 } else if let NABufferType::Video16(ref buf) = buffer {
ffe69577
KS
173 let (w, h) = buf.get_dimensions(0);
174 let hdr = format!("P6\n{} {}\n255\n", w, h);
175 ofile.write_all(hdr.as_bytes()).unwrap();
176 let dta = buf.get_data();
177 let stride = buf.get_stride(0);
178 let depths: [u8; 3] = [
7c6134cc
KS
179 info.get_format().get_chromaton(0).unwrap().get_depth(),
180 info.get_format().get_chromaton(1).unwrap().get_depth(),
181 info.get_format().get_chromaton(2).unwrap().get_depth()
ffe69577
KS
182 ];
183 let masks: [u16; 3] = [
184 (1 << depths[0]) - 1,
185 (1 << depths[1]) - 1,
186 (1 << depths[2]) - 1
187 ];
188 let shifts: [u8; 3] = [
7c6134cc
KS
189 info.get_format().get_chromaton(0).unwrap().get_shift(),
190 info.get_format().get_chromaton(1).unwrap().get_shift(),
191 info.get_format().get_chromaton(2).unwrap().get_shift()
ffe69577 192 ];
e47ee411 193 let mut line: Vec<u8> = vec![0; w * 3];
0a90d212
KS
194 if !flipped {
195 for src in dta.chunks(stride) {
196 for x in 0..w {
197 let elem = src[x];
198 let r = ((elem >> shifts[0]) & masks[0]) << (8 - depths[0]);
199 let g = ((elem >> shifts[1]) & masks[1]) << (8 - depths[1]);
200 let b = ((elem >> shifts[2]) & masks[2]) << (8 - depths[2]);
201 line[x * 3 + 0] = r as u8;
202 line[x * 3 + 1] = g as u8;
203 line[x * 3 + 2] = b as u8;
204 }
205 ofile.write_all(line.as_slice()).unwrap();
206 }
207 } else {
208 for src in dta[..h * stride].chunks(stride).rev() {
209 for x in 0..w {
210 let elem = src[x];
211 let r = ((elem >> shifts[0]) & masks[0]) << (8 - depths[0]);
212 let g = ((elem >> shifts[1]) & masks[1]) << (8 - depths[1]);
213 let b = ((elem >> shifts[2]) & masks[2]) << (8 - depths[2]);
214 line[x * 3 + 0] = r as u8;
215 line[x * 3 + 1] = g as u8;
216 line[x * 3 + 2] = b as u8;
217 }
218 ofile.write_all(line.as_slice()).unwrap();
ffe69577 219 }
ffe69577
KS
220 }
221 } else {
222panic!(" unhandled buf format");
223 }
224}