1 //! Simple PNM image writers for RGB and YUV images.
2 use std::io::prelude::*;
4 use nihav_core::frame::{NABufferType, NAFrameRef, NAFrame, alloc_video_buffer};
5 use nihav_core::scale::*;
6 use nihav_core::formats::YUV420_FORMAT;
8 /// Writes PGMYUV for input frame.
9 pub fn write_pgmyuv(name: &str, frm: NAFrameRef) -> std::io::Result<()> {
10 if let NABufferType::None = frm.get_buffer() { return Ok(()); }
11 let mut ofile = File::create(name)?;
12 let buf = frm.get_buffer().get_vbuf().unwrap();
13 let is_flipped = buf.get_info().is_flipped();
14 let (w, h) = buf.get_dimensions(0);
15 let (w2, h2) = buf.get_dimensions(1);
16 let full_w = w2 * 2 > w;
17 let has_alpha = buf.get_info().get_format().has_alpha();
18 let mut tot_h = h + h2;
25 let hdr = format!("P5\n{} {}\n255\n", w, tot_h);
26 ofile.write_all(hdr.as_bytes())?;
27 let dta = buf.get_data();
28 let ls = buf.get_stride(0);
29 let pad: Vec<u8> = vec![0xFF; if !full_w { (w - w2 * 2) / 2 } else { w - w2 } ];
31 let ylines = dta.chunks(ls).take(h);
33 ofile.write_all(&line[..w])?;
36 let ylines = dta[..h * ls].chunks(ls).rev();
38 ofile.write_all(&line[..w])?;
41 let base1 = buf.get_offset(1);
42 let stride1 = buf.get_stride(1);
43 let base2 = buf.get_offset(2);
44 let stride2 = buf.get_stride(2);
45 let u = &dta[base1..][..h2*stride1];
46 let v = &dta[base2..][..h2*stride2];
47 let has_chroma = stride1 > 0 && stride2 > 0;
48 if !full_w && has_chroma {
50 for (uline, vline) in u.chunks(stride1).zip(v.chunks(stride2)) {
51 ofile.write_all(&uline[..w2])?;
52 ofile.write_all(pad.as_slice())?;
54 ofile.write_all(&vline[..w2])?;
55 ofile.write_all(pad.as_slice())?;
58 for (uline, vline) in u.chunks(stride1).rev().zip(v.chunks(stride2).rev()) {
59 ofile.write_all(&uline[..w2])?;
60 ofile.write_all(pad.as_slice())?;
62 ofile.write_all(&vline[..w2])?;
63 ofile.write_all(pad.as_slice())?;
66 } else if has_chroma {
68 for uline in u.chunks(stride1) {
69 ofile.write_all(&uline[..w2])?;
70 ofile.write_all(pad.as_slice())?;
72 for vline in v.chunks(stride2) {
73 ofile.write_all(&vline[..w2])?;
74 ofile.write_all(pad.as_slice())?;
77 for uline in u.chunks(stride1).rev() {
78 ofile.write_all(&uline[..w2])?;
79 ofile.write_all(pad.as_slice())?;
81 for vline in v.chunks(stride2).rev() {
82 ofile.write_all(&vline[..w2])?;
83 ofile.write_all(pad.as_slice())?;
88 let ls = buf.get_stride(3);
90 let alines = dta[buf.get_offset(3)..].chunks(ls).take(h);
92 ofile.write_all(&line[..w])?;
95 let alines = dta[buf.get_offset(3)..].chunks(ls).take(h).rev();
97 ofile.write_all(&line[..w])?;
104 /// Writes output PPM for input paletted input frame.
105 pub fn write_palppm(name: &str, frm: NAFrameRef) -> std::io::Result<()> {
106 if let NABufferType::None = frm.get_buffer() { return Ok(()); }
107 let mut ofile = File::create(name)?;
108 let buf = frm.get_buffer().get_vbuf().unwrap();
109 let (w, h) = buf.get_dimensions(0);
110 let paloff = buf.get_offset(1);
111 let hdr = format!("P6\n{} {}\n255\n", w, h);
112 ofile.write_all(hdr.as_bytes())?;
113 let dta = buf.get_data();
114 let ls = buf.get_stride(0);
115 let offs: [usize; 3] = [
116 buf.get_info().get_format().get_chromaton(0).unwrap().get_offset() as usize,
117 buf.get_info().get_format().get_chromaton(1).unwrap().get_offset() as usize,
118 buf.get_info().get_format().get_chromaton(2).unwrap().get_offset() as usize
120 let flipped = buf.get_info().is_flipped();
121 let mut idx = if !flipped { 0 } else { ls * h };
122 let mut line: Vec<u8> = vec![0; w * 3];
127 let src = &dta[idx..(idx+w)];
129 let pix = src[x] as usize;
130 line[x * 3 + 0] = dta[paloff + pix * 3 + offs[0]];
131 line[x * 3 + 1] = dta[paloff + pix * 3 + offs[1]];
132 line[x * 3 + 2] = dta[paloff + pix * 3 + offs[2]];
134 ofile.write_all(line.as_slice())?;
142 /// Writes PPM file for RGB input.
143 pub fn write_rgbppm(name: &str, frm: NAFrameRef) -> std::io::Result<()> {
144 if let NABufferType::None = frm.get_buffer() { return Ok(()); }
145 let mut ofile = File::create(name)?;
146 let info = frm.get_buffer().get_video_info().unwrap();
147 let flipped = info.is_flipped();
148 let buffer = frm.get_buffer();
149 if let Some(ref buf) = buffer.get_vbuf() {
150 let (w, h) = buf.get_dimensions(0);
151 let hdr = format!("P6\n{} {}\n255\n", w, h);
152 ofile.write_all(hdr.as_bytes())?;
153 let dta = buf.get_data();
154 let stride = buf.get_stride(0);
155 let offs: [usize; 3] = [
156 info.get_format().get_chromaton(0).unwrap().get_offset() as usize,
157 info.get_format().get_chromaton(1).unwrap().get_offset() as usize,
158 info.get_format().get_chromaton(2).unwrap().get_offset() as usize
160 let step = info.get_format().get_elem_size() as usize;
161 let mut line: Vec<u8> = vec![0; w * 3];
163 for src in dta.chunks(stride) {
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]];
169 ofile.write_all(line.as_slice())?;
172 for src in dta[..stride * h].chunks(stride).rev() {
174 line[x * 3 + 0] = src[x * step + offs[0]];
175 line[x * 3 + 1] = src[x * step + offs[1]];
176 line[x * 3 + 2] = src[x * step + offs[2]];
178 ofile.write_all(line.as_slice())?;
181 } else if let NABufferType::Video16(ref buf) = buffer {
182 let (w, h) = buf.get_dimensions(0);
183 let hdr = format!("P6\n{} {}\n255\n", w, h);
184 ofile.write_all(hdr.as_bytes())?;
185 let dta = buf.get_data();
186 let stride = buf.get_stride(0);
187 let depths: [u8; 3] = [
188 info.get_format().get_chromaton(0).unwrap().get_depth(),
189 info.get_format().get_chromaton(1).unwrap().get_depth(),
190 info.get_format().get_chromaton(2).unwrap().get_depth()
192 let masks: [u16; 3] = [
193 (1 << depths[0]) - 1,
194 (1 << depths[1]) - 1,
197 let shifts: [u8; 3] = [
198 info.get_format().get_chromaton(0).unwrap().get_shift(),
199 info.get_format().get_chromaton(1).unwrap().get_shift(),
200 info.get_format().get_chromaton(2).unwrap().get_shift()
202 let mut line: Vec<u8> = vec![0; w * 3];
204 for src in dta.chunks(stride) {
207 let r = ((elem >> shifts[0]) & masks[0]) << (8 - depths[0]);
208 let g = ((elem >> shifts[1]) & masks[1]) << (8 - depths[1]);
209 let b = ((elem >> shifts[2]) & masks[2]) << (8 - depths[2]);
210 line[x * 3 + 0] = r as u8;
211 line[x * 3 + 1] = g as u8;
212 line[x * 3 + 2] = b as u8;
214 ofile.write_all(line.as_slice())?;
217 for src in dta[..h * stride].chunks(stride).rev() {
220 let r = ((elem >> shifts[0]) & masks[0]) << (8 - depths[0]);
221 let g = ((elem >> shifts[1]) & masks[1]) << (8 - depths[1]);
222 let b = ((elem >> shifts[2]) & masks[2]) << (8 - depths[2]);
223 line[x * 3 + 0] = r as u8;
224 line[x * 3 + 1] = g as u8;
225 line[x * 3 + 2] = b as u8;
227 ofile.write_all(line.as_slice())?;
231 panic!(" unhandled buf format");
236 /// Writes PNM file with a format depending on input format.
237 pub fn write_pnm(pfx: &str, strno: usize, num: u64, frm: NAFrameRef) -> std::io::Result<()> {
238 if let NABufferType::None = frm.get_buffer() { return Ok(()); }
240 let vinfo = frm.get_buffer().get_video_info().unwrap();
241 if vinfo.get_format().is_paletted() {
242 let name = format!("{}{:02}_{:06}.ppm", pfx, strno, num);
243 write_palppm(name.as_str(), frm)
244 } else if vinfo.get_format().get_model().is_yuv() {
245 let name = format!("{}{:02}_{:06}.pgm", pfx, strno, num);
246 if vinfo.get_format().is_unpacked() {
247 write_pgmyuv(name.as_str(), frm)
249 let mut dst_vinfo = vinfo;
250 dst_vinfo.format = YUV420_FORMAT;
251 let mut cvt_buf = alloc_video_buffer(dst_vinfo, 2).unwrap();
252 let buf = frm.get_buffer();
254 let ifmt = get_scale_fmt_from_pic(&buf);
255 let ofmt = get_scale_fmt_from_pic(&cvt_buf);
256 let mut scaler = NAScale::new(ifmt, ofmt).unwrap();
257 scaler.convert(&buf, &mut cvt_buf).unwrap();
258 let frm = NAFrame::new(frm.get_time_information(), frm.frame_type, frm.key, frm.get_info(), cvt_buf);
259 write_pgmyuv(name.as_str(), frm.into_ref())
261 } else if vinfo.get_format().get_model().is_rgb() {
262 let name = format!("{}{:02}_{:06}.ppm", pfx, strno, num);
263 write_rgbppm(name.as_str(), frm)
265 panic!(" unknown format");