| 1 | //! Simple PNM image writers for RGB and YUV images. |
| 2 | use std::io::prelude::*; |
| 3 | use std::fs::File; |
| 4 | use nihav_core::frame::{NABufferType, NAFrameRef}; |
| 5 | |
| 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; |
| 17 | if has_alpha { |
| 18 | tot_h += h; |
| 19 | } |
| 20 | if full_w { |
| 21 | tot_h += h2; |
| 22 | } |
| 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 } ]; |
| 28 | if !is_flipped { |
| 29 | let ylines = dta.chunks(ls).take(h); |
| 30 | for line in ylines { |
| 31 | ofile.write_all(&line[..w])?; |
| 32 | } |
| 33 | } else { |
| 34 | let ylines = dta[..h * ls].chunks(ls).rev(); |
| 35 | for line in ylines { |
| 36 | ofile.write_all(&line[..w])?; |
| 37 | } |
| 38 | } |
| 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 { |
| 47 | if !is_flipped { |
| 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())?; |
| 51 | |
| 52 | ofile.write_all(&vline[..w2])?; |
| 53 | ofile.write_all(pad.as_slice())?; |
| 54 | } |
| 55 | } else { |
| 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())?; |
| 59 | |
| 60 | ofile.write_all(&vline[..w2])?; |
| 61 | ofile.write_all(pad.as_slice())?; |
| 62 | } |
| 63 | } |
| 64 | } else if has_chroma { |
| 65 | if !is_flipped { |
| 66 | for uline in u.chunks(stride1) { |
| 67 | ofile.write_all(&uline[..w2])?; |
| 68 | ofile.write_all(pad.as_slice())?; |
| 69 | } |
| 70 | for vline in v.chunks(stride2) { |
| 71 | ofile.write_all(&vline[..w2])?; |
| 72 | ofile.write_all(pad.as_slice())?; |
| 73 | } |
| 74 | } else { |
| 75 | for uline in u.chunks(stride1).rev() { |
| 76 | ofile.write_all(&uline[..w2])?; |
| 77 | ofile.write_all(pad.as_slice())?; |
| 78 | } |
| 79 | for vline in v.chunks(stride2).rev() { |
| 80 | ofile.write_all(&vline[..w2])?; |
| 81 | ofile.write_all(pad.as_slice())?; |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | if has_alpha { |
| 86 | let ls = buf.get_stride(3); |
| 87 | if !is_flipped { |
| 88 | let alines = dta[buf.get_offset(3)..].chunks(ls).take(h); |
| 89 | for line in alines { |
| 90 | ofile.write_all(&line[..w])?; |
| 91 | } |
| 92 | } else { |
| 93 | let alines = dta[buf.get_offset(3)..].chunks(ls).take(h).rev(); |
| 94 | for line in alines { |
| 95 | ofile.write_all(&line[..w])?; |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | Ok(()) |
| 100 | } |
| 101 | |
| 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 |
| 117 | ]; |
| 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]; |
| 121 | for _ in 0..h { |
| 122 | if flipped { |
| 123 | idx -= ls; |
| 124 | } |
| 125 | let src = &dta[idx..(idx+w)]; |
| 126 | for x in 0..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]]; |
| 131 | } |
| 132 | ofile.write_all(line.as_slice())?; |
| 133 | if !flipped { |
| 134 | idx += ls; |
| 135 | } |
| 136 | } |
| 137 | Ok(()) |
| 138 | } |
| 139 | |
| 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 |
| 157 | ]; |
| 158 | let step = info.get_format().get_elem_size() as usize; |
| 159 | let mut line: Vec<u8> = vec![0; w * 3]; |
| 160 | if !flipped { |
| 161 | for src in dta.chunks(stride) { |
| 162 | for x in 0..w { |
| 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]]; |
| 166 | } |
| 167 | ofile.write_all(line.as_slice())?; |
| 168 | } |
| 169 | } else { |
| 170 | for src in dta[..stride * h].chunks(stride).rev() { |
| 171 | for x in 0..w { |
| 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]]; |
| 175 | } |
| 176 | ofile.write_all(line.as_slice())?; |
| 177 | } |
| 178 | } |
| 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() |
| 189 | ]; |
| 190 | let masks: [u16; 3] = [ |
| 191 | (1 << depths[0]) - 1, |
| 192 | (1 << depths[1]) - 1, |
| 193 | (1 << depths[2]) - 1 |
| 194 | ]; |
| 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() |
| 199 | ]; |
| 200 | let mut line: Vec<u8> = vec![0; w * 3]; |
| 201 | if !flipped { |
| 202 | for src in dta.chunks(stride) { |
| 203 | for x in 0..w { |
| 204 | let elem = src[x]; |
| 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; |
| 211 | } |
| 212 | ofile.write_all(line.as_slice())?; |
| 213 | } |
| 214 | } else { |
| 215 | for src in dta[..h * stride].chunks(stride).rev() { |
| 216 | for x in 0..w { |
| 217 | let elem = src[x]; |
| 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; |
| 224 | } |
| 225 | ofile.write_all(line.as_slice())?; |
| 226 | } |
| 227 | } |
| 228 | } else { |
| 229 | panic!(" unhandled buf format"); |
| 230 | } |
| 231 | Ok(()) |
| 232 | } |
| 233 | |
| 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(()); } |
| 237 | |
| 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) |
| 248 | } else { |
| 249 | panic!(" unknown format"); |
| 250 | } |
| 251 | } |
| 252 | |