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);
88 let alines = dta[buf.get_offset(3)..].chunks(ls).take(h);
90 ofile.write_all(&line[..w])?;
93 let alines = dta[buf.get_offset(3)..].chunks(ls).take(h).rev();
95 ofile.write_all(&line[..w])?;
102 /// Writes output PPM for input paletted input frame.
103 pub fn write_palppm(name: &str, frm: NAFrameRef) -> std::io::Result<()> {
104 if let NABufferType::None = frm.get_buffer() { return Ok(()); }
105 let mut ofile = File::create(name)?;
106 let buf = frm.get_buffer().get_vbuf().unwrap();
107 let (w, h) = buf.get_dimensions(0);
108 let paloff = buf.get_offset(1);
109 let hdr = format!("P6\n{} {}\n255\n", w, h);
110 ofile.write_all(hdr.as_bytes())?;
111 let dta = buf.get_data();
112 let ls = buf.get_stride(0);
113 let offs: [usize; 3] = [
114 buf.get_info().get_format().get_chromaton(0).unwrap().get_offset() as usize,
115 buf.get_info().get_format().get_chromaton(1).unwrap().get_offset() as usize,
116 buf.get_info().get_format().get_chromaton(2).unwrap().get_offset() as usize
118 let flipped = buf.get_info().is_flipped();
119 let mut idx = if !flipped { 0 } else { ls * h };
120 let mut line: Vec<u8> = vec![0; w * 3];
125 let src = &dta[idx..(idx+w)];
127 let pix = src[x] as usize;
128 line[x * 3 + 0] = dta[paloff + pix * 3 + offs[0]];
129 line[x * 3 + 1] = dta[paloff + pix * 3 + offs[1]];
130 line[x * 3 + 2] = dta[paloff + pix * 3 + offs[2]];
132 ofile.write_all(line.as_slice())?;
140 /// Writes PPM file for RGB input.
141 pub fn write_rgbppm(name: &str, frm: NAFrameRef) -> std::io::Result<()> {
142 if let NABufferType::None = frm.get_buffer() { return Ok(()); }
143 let mut ofile = File::create(name)?;
144 let info = frm.get_buffer().get_video_info().unwrap();
145 let flipped = info.is_flipped();
146 let buffer = frm.get_buffer();
147 if let Some(ref buf) = buffer.get_vbuf() {
148 let (w, h) = buf.get_dimensions(0);
149 let hdr = format!("P6\n{} {}\n255\n", w, h);
150 ofile.write_all(hdr.as_bytes())?;
151 let dta = buf.get_data();
152 let stride = buf.get_stride(0);
153 let offs: [usize; 3] = [
154 info.get_format().get_chromaton(0).unwrap().get_offset() as usize,
155 info.get_format().get_chromaton(1).unwrap().get_offset() as usize,
156 info.get_format().get_chromaton(2).unwrap().get_offset() as usize
158 let step = info.get_format().get_elem_size() as usize;
159 let mut line: Vec<u8> = vec![0; w * 3];
161 for src in dta.chunks(stride) {
163 line[x * 3 + 0] = src[x * step + offs[0]];
164 line[x * 3 + 1] = src[x * step + offs[1]];
165 line[x * 3 + 2] = src[x * step + offs[2]];
167 ofile.write_all(line.as_slice())?;
170 for src in dta[..stride * h].chunks(stride).rev() {
172 line[x * 3 + 0] = src[x * step + offs[0]];
173 line[x * 3 + 1] = src[x * step + offs[1]];
174 line[x * 3 + 2] = src[x * step + offs[2]];
176 ofile.write_all(line.as_slice())?;
179 } else if let NABufferType::Video16(ref buf) = buffer {
180 let (w, h) = buf.get_dimensions(0);
181 let hdr = format!("P6\n{} {}\n255\n", w, h);
182 ofile.write_all(hdr.as_bytes())?;
183 let dta = buf.get_data();
184 let stride = buf.get_stride(0);
185 let depths: [u8; 3] = [
186 info.get_format().get_chromaton(0).unwrap().get_depth(),
187 info.get_format().get_chromaton(1).unwrap().get_depth(),
188 info.get_format().get_chromaton(2).unwrap().get_depth()
190 let masks: [u16; 3] = [
191 (1 << depths[0]) - 1,
192 (1 << depths[1]) - 1,
195 let shifts: [u8; 3] = [
196 info.get_format().get_chromaton(0).unwrap().get_shift(),
197 info.get_format().get_chromaton(1).unwrap().get_shift(),
198 info.get_format().get_chromaton(2).unwrap().get_shift()
200 let mut line: Vec<u8> = vec![0; w * 3];
202 for src in dta.chunks(stride) {
205 let r = ((elem >> shifts[0]) & masks[0]) << (8 - depths[0]);
206 let g = ((elem >> shifts[1]) & masks[1]) << (8 - depths[1]);
207 let b = ((elem >> shifts[2]) & masks[2]) << (8 - depths[2]);
208 line[x * 3 + 0] = r as u8;
209 line[x * 3 + 1] = g as u8;
210 line[x * 3 + 2] = b as u8;
212 ofile.write_all(line.as_slice())?;
215 for src in dta[..h * stride].chunks(stride).rev() {
218 let r = ((elem >> shifts[0]) & masks[0]) << (8 - depths[0]);
219 let g = ((elem >> shifts[1]) & masks[1]) << (8 - depths[1]);
220 let b = ((elem >> shifts[2]) & masks[2]) << (8 - depths[2]);
221 line[x * 3 + 0] = r as u8;
222 line[x * 3 + 1] = g as u8;
223 line[x * 3 + 2] = b as u8;
225 ofile.write_all(line.as_slice())?;
229 panic!(" unhandled buf format");
234 /// Writes PNM file with a format depending on input format.
235 pub fn write_pnm(pfx: &str, strno: usize, num: u64, frm: NAFrameRef) -> std::io::Result<()> {
236 if let NABufferType::None = frm.get_buffer() { return Ok(()); }
238 let vinfo = frm.get_buffer().get_video_info().unwrap();
239 if vinfo.get_format().is_paletted() {
240 let name = format!("{}{:02}_{:06}.ppm", pfx, strno, num);
241 write_palppm(name.as_str(), frm)
242 } else if vinfo.get_format().get_model().is_yuv() {
243 let name = format!("{}{:02}_{:06}.pgm", pfx, strno, num);
244 write_pgmyuv(name.as_str(), frm)
245 } else if vinfo.get_format().get_model().is_rgb() {
246 let name = format!("{}{:02}_{:06}.ppm", pfx, strno, num);
247 write_rgbppm(name.as_str(), frm)
249 panic!(" unknown format");