1 //! Simple PNM image writers for RGB and YUV images.
2 use std::io::prelude::*;
4 use nihav_core::frame::{NABufferType, NAFrameRef};
6 /// Writes PGMYUV for input frame.
7 pub fn write_pgmyuv(name: &str, frm: NAFrameRef) -> std::io::Result<()> {
8 if let NABufferType::None = frm.get_buffer() { return Ok(()); }
9 let mut ofile = File::create(name)?;
10 let buf = frm.get_buffer().get_vbuf().unwrap();
11 let is_flipped = buf.get_info().is_flipped();
12 let (w, h) = buf.get_dimensions(0);
13 let (w2, h2) = buf.get_dimensions(1);
14 let full_w = w2 * 2 > w;
15 let has_alpha = buf.get_info().get_format().has_alpha();
16 let mut tot_h = h + h2;
23 let hdr = format!("P5\n{} {}\n255\n", w, tot_h);
24 ofile.write_all(hdr.as_bytes())?;
25 let dta = buf.get_data();
26 let ls = buf.get_stride(0);
27 let pad: Vec<u8> = vec![0xFF; if !full_w { (w - w2 * 2) / 2 } else { w - w2 } ];
29 let ylines = dta.chunks(ls).take(h);
31 ofile.write_all(&line[..w])?;
34 let ylines = dta[..h * ls].chunks(ls).rev();
36 ofile.write_all(&line[..w])?;
39 let base1 = buf.get_offset(1);
40 let stride1 = buf.get_stride(1);
41 let base2 = buf.get_offset(2);
42 let stride2 = buf.get_stride(2);
43 let u = &dta[base1..][..h2*stride1];
44 let v = &dta[base2..][..h2*stride2];
45 let has_chroma = stride1 > 0 && stride2 > 0;
46 if !full_w && has_chroma {
48 for (uline, vline) in u.chunks(stride1).zip(v.chunks(stride2)) {
49 ofile.write_all(&uline[..w2])?;
50 ofile.write_all(pad.as_slice())?;
52 ofile.write_all(&vline[..w2])?;
53 ofile.write_all(pad.as_slice())?;
56 for (uline, vline) in u.chunks(stride1).rev().zip(v.chunks(stride2).rev()) {
57 ofile.write_all(&uline[..w2])?;
58 ofile.write_all(pad.as_slice())?;
60 ofile.write_all(&vline[..w2])?;
61 ofile.write_all(pad.as_slice())?;
64 } else if has_chroma {
66 for uline in u.chunks(stride1) {
67 ofile.write_all(&uline[..w2])?;
68 ofile.write_all(pad.as_slice())?;
70 for vline in v.chunks(stride2) {
71 ofile.write_all(&vline[..w2])?;
72 ofile.write_all(pad.as_slice())?;
75 for uline in u.chunks(stride1).rev() {
76 ofile.write_all(&uline[..w2])?;
77 ofile.write_all(pad.as_slice())?;
79 for vline in v.chunks(stride2).rev() {
80 ofile.write_all(&vline[..w2])?;
81 ofile.write_all(pad.as_slice())?;
86 let ls = buf.get_stride(3);
87 let mut idx = buf.get_offset(3);
88 let mut idx2 = idx + w;
90 let line = &dta[idx..idx2];
91 ofile.write_all(line)?;
99 /// Writes output PPM for input paletted input frame.
100 pub fn write_palppm(name: &str, frm: NAFrameRef) -> std::io::Result<()> {
101 if let NABufferType::None = frm.get_buffer() { return Ok(()); }
102 let mut ofile = File::create(name)?;
103 let buf = frm.get_buffer().get_vbuf().unwrap();
104 let (w, h) = buf.get_dimensions(0);
105 let paloff = buf.get_offset(1);
106 let hdr = format!("P6\n{} {}\n255\n", w, h);
107 ofile.write_all(hdr.as_bytes())?;
108 let dta = buf.get_data();
109 let ls = buf.get_stride(0);
110 let offs: [usize; 3] = [
111 buf.get_info().get_format().get_chromaton(0).unwrap().get_offset() as usize,
112 buf.get_info().get_format().get_chromaton(1).unwrap().get_offset() as usize,
113 buf.get_info().get_format().get_chromaton(2).unwrap().get_offset() as usize
115 let flipped = buf.get_info().is_flipped();
116 let mut idx = if !flipped { 0 } else { ls * h };
117 let mut line: Vec<u8> = vec![0; w * 3];
122 let src = &dta[idx..(idx+w)];
124 let pix = src[x] as usize;
125 line[x * 3 + 0] = dta[paloff + pix * 3 + offs[0]];
126 line[x * 3 + 1] = dta[paloff + pix * 3 + offs[1]];
127 line[x * 3 + 2] = dta[paloff + pix * 3 + offs[2]];
129 ofile.write_all(line.as_slice())?;
137 /// Writes PPM file for RGB input.
138 pub fn write_rgbppm(name: &str, frm: NAFrameRef) -> std::io::Result<()> {
139 if let NABufferType::None = frm.get_buffer() { return Ok(()); }
140 let mut ofile = File::create(name)?;
141 let info = frm.get_buffer().get_video_info().unwrap();
142 let flipped = info.is_flipped();
143 let buffer = frm.get_buffer();
144 if let Some(ref buf) = buffer.get_vbuf() {
145 let (w, h) = buf.get_dimensions(0);
146 let hdr = format!("P6\n{} {}\n255\n", w, h);
147 ofile.write_all(hdr.as_bytes())?;
148 let dta = buf.get_data();
149 let stride = buf.get_stride(0);
150 let offs: [usize; 3] = [
151 info.get_format().get_chromaton(0).unwrap().get_offset() as usize,
152 info.get_format().get_chromaton(1).unwrap().get_offset() as usize,
153 info.get_format().get_chromaton(2).unwrap().get_offset() as usize
155 let step = info.get_format().get_elem_size() as usize;
156 let mut line: Vec<u8> = vec![0; w * 3];
158 for src in dta.chunks(stride) {
160 line[x * 3 + 0] = src[x * step + offs[0]];
161 line[x * 3 + 1] = src[x * step + offs[1]];
162 line[x * 3 + 2] = src[x * step + offs[2]];
164 ofile.write_all(line.as_slice())?;
167 for src in dta[..stride * h].chunks(stride).rev() {
169 line[x * 3 + 0] = src[x * step + offs[0]];
170 line[x * 3 + 1] = src[x * step + offs[1]];
171 line[x * 3 + 2] = src[x * step + offs[2]];
173 ofile.write_all(line.as_slice())?;
176 } else if let NABufferType::Video16(ref buf) = buffer {
177 let (w, h) = buf.get_dimensions(0);
178 let hdr = format!("P6\n{} {}\n255\n", w, h);
179 ofile.write_all(hdr.as_bytes())?;
180 let dta = buf.get_data();
181 let stride = buf.get_stride(0);
182 let depths: [u8; 3] = [
183 info.get_format().get_chromaton(0).unwrap().get_depth(),
184 info.get_format().get_chromaton(1).unwrap().get_depth(),
185 info.get_format().get_chromaton(2).unwrap().get_depth()
187 let masks: [u16; 3] = [
188 (1 << depths[0]) - 1,
189 (1 << depths[1]) - 1,
192 let shifts: [u8; 3] = [
193 info.get_format().get_chromaton(0).unwrap().get_shift(),
194 info.get_format().get_chromaton(1).unwrap().get_shift(),
195 info.get_format().get_chromaton(2).unwrap().get_shift()
197 let mut line: Vec<u8> = vec![0; w * 3];
199 for src in dta.chunks(stride) {
202 let r = ((elem >> shifts[0]) & masks[0]) << (8 - depths[0]);
203 let g = ((elem >> shifts[1]) & masks[1]) << (8 - depths[1]);
204 let b = ((elem >> shifts[2]) & masks[2]) << (8 - depths[2]);
205 line[x * 3 + 0] = r as u8;
206 line[x * 3 + 1] = g as u8;
207 line[x * 3 + 2] = b as u8;
209 ofile.write_all(line.as_slice())?;
212 for src in dta[..h * stride].chunks(stride).rev() {
215 let r = ((elem >> shifts[0]) & masks[0]) << (8 - depths[0]);
216 let g = ((elem >> shifts[1]) & masks[1]) << (8 - depths[1]);
217 let b = ((elem >> shifts[2]) & masks[2]) << (8 - depths[2]);
218 line[x * 3 + 0] = r as u8;
219 line[x * 3 + 1] = g as u8;
220 line[x * 3 + 2] = b as u8;
222 ofile.write_all(line.as_slice())?;
226 panic!(" unhandled buf format");
231 /// Writes PNM file with a format depending on input format.
232 pub fn write_pnm(pfx: &str, strno: usize, num: u64, frm: NAFrameRef) -> std::io::Result<()> {
233 if let NABufferType::None = frm.get_buffer() { return Ok(()); }
235 let vinfo = frm.get_buffer().get_video_info().unwrap();
236 if vinfo.get_format().is_paletted() {
237 let name = format!("{}{:02}_{:06}.ppm", pfx, strno, num);
238 write_palppm(name.as_str(), frm)
239 } else if vinfo.get_format().get_model().is_yuv() {
240 let name = format!("{}{:02}_{:06}.pgm", pfx, strno, num);
241 write_pgmyuv(name.as_str(), frm)
242 } else if vinfo.get_format().get_model().is_rgb() {
243 let name = format!("{}{:02}_{:06}.ppm", pfx, strno, num);
244 write_rgbppm(name.as_str(), frm)
246 panic!(" unknown format");