frmwriter: fix handling of flipped paletted images
[nihav-tool.git] / src / frmwriter.rs
index f3221d09a2b440b8d8556616b34cd966c9f641a2..f882a7fa266f9bcd5dac659c134f23929b347125 100644 (file)
@@ -6,7 +6,7 @@ use std::fs::File;
 
 pub fn write_pgmyuv(pfx: &str, strno: usize, num: u64, frm: NAFrameRef) {
     if let NABufferType::None = frm.get_buffer() { return; }
-    let name = format!("{}out{:02}_{:08}.pgm", pfx, strno, num);
+    let name = format!("{}{:02}_{:08}.pgm", pfx, strno, num);
     let mut ofile = File::create(name).unwrap();
     let buf = frm.get_buffer().get_vbuf().unwrap();
     let is_flipped = buf.get_info().is_flipped();
@@ -43,7 +43,8 @@ pub fn write_pgmyuv(pfx: &str, strno: usize, num: u64, frm: NAFrameRef) {
     let stride2 = buf.get_stride(2);
     let u = &dta[base1..][..h2*stride1];
     let v = &dta[base2..][..h2*stride2];
-    if !full_w {
+    let has_chroma = stride1 > 0 && stride2 > 0;
+    if !full_w && has_chroma {
         if !is_flipped {
             for (uline, vline) in u.chunks(stride1).zip(v.chunks(stride2)) {
                 ofile.write_all(&uline[..w2]).unwrap();
@@ -61,7 +62,7 @@ pub fn write_pgmyuv(pfx: &str, strno: usize, num: u64, frm: NAFrameRef) {
                 ofile.write_all(pad.as_slice()).unwrap();
             }
         }
-    } else {
+    } else if has_chroma {
         if !is_flipped {
             for uline in u.chunks(stride1) {
                 ofile.write_all(&uline[..w2]).unwrap();
@@ -96,7 +97,7 @@ pub fn write_pgmyuv(pfx: &str, strno: usize, num: u64, frm: NAFrameRef) {
 }
 
 pub fn write_palppm(pfx: &str, strno: usize, num: u64, frm: NAFrameRef) {
-    let name = format!("{}out{:02}_{:08}.ppm", pfx, strno, num);
+    let name = format!("{}{:02}_{:08}.ppm", pfx, strno, num);
     let mut ofile = File::create(name).unwrap();
     let buf = frm.get_buffer().get_vbuf().unwrap();
     let (w, h) = buf.get_dimensions(0);
@@ -111,9 +112,12 @@ pub fn write_palppm(pfx: &str, strno: usize, num: u64, frm: NAFrameRef) {
             buf.get_info().get_format().get_chromaton(2).unwrap().get_offset() as usize
         ];
     let flipped = buf.get_info().is_flipped();
-    let mut idx  = if !flipped { 0 } else { ls * (h - 1) };
+    let mut idx  = if !flipped { 0 } else { ls * h };
     let mut line: Vec<u8> = vec![0; w * 3];
     for _ in 0..h {
+        if flipped {
+            idx -= ls;
+        }
         let src = &dta[idx..(idx+w)];
         for x in 0..w {
             let pix = src[x] as usize;
@@ -124,14 +128,12 @@ pub fn write_palppm(pfx: &str, strno: usize, num: u64, frm: NAFrameRef) {
         ofile.write_all(line.as_slice()).unwrap();
         if !flipped {
             idx += ls;
-        } else {
-            idx -= ls;
         }
     }
 }
 
 pub fn write_ppm(pfx: &str, strno: usize, num: u64, frm: NAFrameRef) {
-    let name = format!("{}out{:02}_{:08}.ppm", pfx, strno, num);
+    let name = format!("{}{:02}_{:08}.ppm", pfx, strno, num);
     let mut ofile = File::create(name).unwrap();
     let info = frm.get_buffer().get_video_info().unwrap();
     let flipped = info.is_flipped();