| 1 | extern crate nihav_core; |
| 2 | |
| 3 | use nihav_core::frame::*; |
| 4 | use std::io::prelude::*; |
| 5 | use std::fs::File; |
| 6 | |
| 7 | pub fn write_pgmyuv(pfx: &str, strno: usize, num: u64, frm: NAFrameRef) { |
| 8 | if let NABufferType::None = frm.get_buffer() { return; } |
| 9 | let name = format!("{}out{:02}_{:08}.pgm", pfx, strno, num); |
| 10 | let mut ofile = File::create(name).unwrap(); |
| 11 | let buf = frm.get_buffer().get_vbuf().unwrap(); |
| 12 | let is_flipped = buf.get_info().is_flipped(); |
| 13 | let (w, h) = buf.get_dimensions(0); |
| 14 | let (w2, h2) = buf.get_dimensions(1); |
| 15 | let has_alpha = buf.get_info().get_format().has_alpha(); |
| 16 | let tot_h; |
| 17 | if has_alpha { |
| 18 | tot_h = h * 2 + h2; |
| 19 | } else { |
| 20 | tot_h = h + h2; |
| 21 | } |
| 22 | let hdr = format!("P5\n{} {}\n255\n", w, tot_h); |
| 23 | ofile.write_all(hdr.as_bytes()).unwrap(); |
| 24 | let dta = buf.get_data(); |
| 25 | let ls = buf.get_stride(0); |
| 26 | let pad: Vec<u8> = vec![0xFF; (w - w2 * 2) / 2]; |
| 27 | if !is_flipped { |
| 28 | let ylines = dta.chunks(ls).take(h); |
| 29 | for line in ylines { |
| 30 | ofile.write_all(&line[..w]).unwrap(); |
| 31 | } |
| 32 | } else { |
| 33 | let ylines = dta[..h * ls].chunks(ls).rev(); |
| 34 | for line in ylines { |
| 35 | ofile.write_all(&line[..w]).unwrap(); |
| 36 | } |
| 37 | } |
| 38 | let base1 = buf.get_offset(1); |
| 39 | let stride1 = buf.get_stride(1); |
| 40 | let base2 = buf.get_offset(2); |
| 41 | let stride2 = buf.get_stride(2); |
| 42 | let u = &dta[base1..][..h2*stride1]; |
| 43 | let v = &dta[base2..][..h2*stride2]; |
| 44 | if !is_flipped { |
| 45 | for (uline, vline) in u.chunks(stride1).zip(v.chunks(stride2)) { |
| 46 | ofile.write_all(&uline[..w2]).unwrap(); |
| 47 | ofile.write_all(pad.as_slice()).unwrap(); |
| 48 | |
| 49 | ofile.write_all(&vline[..w2]).unwrap(); |
| 50 | ofile.write_all(pad.as_slice()).unwrap(); |
| 51 | } |
| 52 | } else { |
| 53 | for (uline, vline) in u.chunks(stride1).rev().zip(v.chunks(stride2).rev()) { |
| 54 | ofile.write_all(&uline[..w2]).unwrap(); |
| 55 | ofile.write_all(pad.as_slice()).unwrap(); |
| 56 | |
| 57 | ofile.write_all(&vline[..w2]).unwrap(); |
| 58 | ofile.write_all(pad.as_slice()).unwrap(); |
| 59 | } |
| 60 | } |
| 61 | if has_alpha { |
| 62 | let ls = buf.get_stride(3); |
| 63 | let mut idx = buf.get_offset(3); |
| 64 | let mut idx2 = idx + w; |
| 65 | for _ in 0..h { |
| 66 | let line = &dta[idx..idx2]; |
| 67 | ofile.write_all(line).unwrap(); |
| 68 | idx += ls; |
| 69 | idx2 += ls; |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | pub fn write_palppm(pfx: &str, strno: usize, num: u64, frm: NAFrameRef) { |
| 75 | let name = format!("{}out{:02}_{:08}.ppm", pfx, strno, num); |
| 76 | let mut ofile = File::create(name).unwrap(); |
| 77 | let buf = frm.get_buffer().get_vbuf().unwrap(); |
| 78 | let (w, h) = buf.get_dimensions(0); |
| 79 | let paloff = buf.get_offset(1); |
| 80 | let hdr = format!("P6\n{} {}\n255\n", w, h); |
| 81 | ofile.write_all(hdr.as_bytes()).unwrap(); |
| 82 | let dta = buf.get_data(); |
| 83 | let ls = buf.get_stride(0); |
| 84 | let offs: [usize; 3] = [ |
| 85 | buf.get_info().get_format().get_chromaton(0).unwrap().get_offset() as usize, |
| 86 | buf.get_info().get_format().get_chromaton(1).unwrap().get_offset() as usize, |
| 87 | buf.get_info().get_format().get_chromaton(2).unwrap().get_offset() as usize |
| 88 | ]; |
| 89 | let flipped = buf.get_info().is_flipped(); |
| 90 | let mut idx = if !flipped { 0 } else { ls * (h - 1) }; |
| 91 | let mut line: Vec<u8> = vec![0; w * 3]; |
| 92 | for _ in 0..h { |
| 93 | let src = &dta[idx..(idx+w)]; |
| 94 | for x in 0..w { |
| 95 | let pix = src[x] as usize; |
| 96 | line[x * 3 + 0] = dta[paloff + pix * 3 + offs[0]]; |
| 97 | line[x * 3 + 1] = dta[paloff + pix * 3 + offs[1]]; |
| 98 | line[x * 3 + 2] = dta[paloff + pix * 3 + offs[2]]; |
| 99 | } |
| 100 | ofile.write_all(line.as_slice()).unwrap(); |
| 101 | if !flipped { |
| 102 | idx += ls; |
| 103 | } else { |
| 104 | idx -= ls; |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | pub fn write_ppm(pfx: &str, strno: usize, num: u64, frm: NAFrameRef) { |
| 110 | let name = format!("{}out{:02}_{:08}.ppm", pfx, strno, num); |
| 111 | let mut ofile = File::create(name).unwrap(); |
| 112 | if let NABufferType::VideoPacked(ref buf) = frm.get_buffer() { |
| 113 | let (w, h) = buf.get_dimensions(0); |
| 114 | let hdr = format!("P6\n{} {}\n255\n", w, h); |
| 115 | ofile.write_all(hdr.as_bytes()).unwrap(); |
| 116 | let dta = buf.get_data(); |
| 117 | let stride = buf.get_stride(0); |
| 118 | let offs: [usize; 3] = [ |
| 119 | buf.get_info().get_format().get_chromaton(0).unwrap().get_offset() as usize, |
| 120 | buf.get_info().get_format().get_chromaton(1).unwrap().get_offset() as usize, |
| 121 | buf.get_info().get_format().get_chromaton(2).unwrap().get_offset() as usize |
| 122 | ]; |
| 123 | let flipped = buf.get_info().is_flipped(); |
| 124 | let step = buf.get_info().get_format().get_elem_size() as usize; |
| 125 | let mut line: Vec<u8> = vec![0; w * 3]; |
| 126 | if !flipped { |
| 127 | for src in dta.chunks(stride) { |
| 128 | for x in 0..w { |
| 129 | line[x * 3 + 0] = src[x * step + offs[0]]; |
| 130 | line[x * 3 + 1] = src[x * step + offs[1]]; |
| 131 | line[x * 3 + 2] = src[x * step + offs[2]]; |
| 132 | } |
| 133 | ofile.write_all(line.as_slice()).unwrap(); |
| 134 | } |
| 135 | } else { |
| 136 | for src in dta[..stride * h].chunks(stride).rev() { |
| 137 | for x in 0..w { |
| 138 | line[x * 3 + 0] = src[x * step + offs[0]]; |
| 139 | line[x * 3 + 1] = src[x * step + offs[1]]; |
| 140 | line[x * 3 + 2] = src[x * step + offs[2]]; |
| 141 | } |
| 142 | ofile.write_all(line.as_slice()).unwrap(); |
| 143 | } |
| 144 | } |
| 145 | } else if let NABufferType::Video16(ref buf) = frm.get_buffer() { |
| 146 | let (w, h) = buf.get_dimensions(0); |
| 147 | let hdr = format!("P6\n{} {}\n255\n", w, h); |
| 148 | ofile.write_all(hdr.as_bytes()).unwrap(); |
| 149 | let dta = buf.get_data(); |
| 150 | let stride = buf.get_stride(0); |
| 151 | let depths: [u8; 3] = [ |
| 152 | buf.get_info().get_format().get_chromaton(0).unwrap().get_depth(), |
| 153 | buf.get_info().get_format().get_chromaton(1).unwrap().get_depth(), |
| 154 | buf.get_info().get_format().get_chromaton(2).unwrap().get_depth() |
| 155 | ]; |
| 156 | let masks: [u16; 3] = [ |
| 157 | (1 << depths[0]) - 1, |
| 158 | (1 << depths[1]) - 1, |
| 159 | (1 << depths[2]) - 1 |
| 160 | ]; |
| 161 | let shifts: [u8; 3] = [ |
| 162 | buf.get_info().get_format().get_chromaton(0).unwrap().get_shift(), |
| 163 | buf.get_info().get_format().get_chromaton(1).unwrap().get_shift(), |
| 164 | buf.get_info().get_format().get_chromaton(2).unwrap().get_shift() |
| 165 | ]; |
| 166 | let mut line: Vec<u8> = vec![0; w * 3]; |
| 167 | let flipped = buf.get_info().is_flipped(); |
| 168 | if !flipped { |
| 169 | for src in dta.chunks(stride) { |
| 170 | for x in 0..w { |
| 171 | let elem = src[x]; |
| 172 | let r = ((elem >> shifts[0]) & masks[0]) << (8 - depths[0]); |
| 173 | let g = ((elem >> shifts[1]) & masks[1]) << (8 - depths[1]); |
| 174 | let b = ((elem >> shifts[2]) & masks[2]) << (8 - depths[2]); |
| 175 | line[x * 3 + 0] = r as u8; |
| 176 | line[x * 3 + 1] = g as u8; |
| 177 | line[x * 3 + 2] = b as u8; |
| 178 | } |
| 179 | ofile.write_all(line.as_slice()).unwrap(); |
| 180 | } |
| 181 | } else { |
| 182 | for src in dta[..h * stride].chunks(stride).rev() { |
| 183 | for x in 0..w { |
| 184 | let elem = src[x]; |
| 185 | let r = ((elem >> shifts[0]) & masks[0]) << (8 - depths[0]); |
| 186 | let g = ((elem >> shifts[1]) & masks[1]) << (8 - depths[1]); |
| 187 | let b = ((elem >> shifts[2]) & masks[2]) << (8 - depths[2]); |
| 188 | line[x * 3 + 0] = r as u8; |
| 189 | line[x * 3 + 1] = g as u8; |
| 190 | line[x * 3 + 2] = b as u8; |
| 191 | } |
| 192 | ofile.write_all(line.as_slice()).unwrap(); |
| 193 | } |
| 194 | } |
| 195 | } else { |
| 196 | panic!(" unhandled buf format"); |
| 197 | } |
| 198 | } |