Commit | Line | Data |
---|---|---|
98b31ef7 | 1 | extern crate nihav_core; |
3660c127 | 2 | |
98b31ef7 | 3 | use nihav_core::frame::*; |
3660c127 KS |
4 | use std::io::prelude::*; |
5 | use std::fs::File; | |
6 | use std::cell::Ref; | |
7 | ||
8 | pub fn write_pgmyuv(pfx: &str, strno: usize, num: u64, frm: Ref<NAFrame>) { | |
9 | if let NABufferType::None = frm.get_buffer() { return; } | |
cd9ece3c | 10 | let name = format!("{}out{:02}_{:08}.pgm", pfx, strno, num); |
3660c127 KS |
11 | let mut ofile = File::create(name).unwrap(); |
12 | let buf = frm.get_buffer().get_vbuf().unwrap(); | |
13 | let (w, h) = buf.get_dimensions(0); | |
14 | let (w2, h2) = buf.get_dimensions(1); | |
15 | let tot_h = h + h2; | |
16 | let hdr = format!("P5\n{} {}\n255\n", w, tot_h); | |
17 | ofile.write_all(hdr.as_bytes()).unwrap(); | |
18 | let dta = buf.get_data(); | |
19 | let ls = buf.get_stride(0); | |
20 | let mut idx = 0; | |
21 | let mut idx2 = w; | |
22 | let mut pad: Vec<u8> = Vec::with_capacity((w - w2 * 2) / 2); | |
23 | pad.resize((w - w2 * 2) / 2, 0xFF); | |
24 | for _ in 0..h { | |
25 | let line = &dta[idx..idx2]; | |
26 | ofile.write_all(line).unwrap(); | |
27 | idx += ls; | |
28 | idx2 += ls; | |
29 | } | |
30 | let mut base1 = buf.get_offset(1); | |
31 | let stride1 = buf.get_stride(1); | |
32 | let mut base2 = buf.get_offset(2); | |
33 | let stride2 = buf.get_stride(2); | |
34 | for _ in 0..h2 { | |
35 | let bend1 = base1 + w2; | |
36 | let line = &dta[base1..bend1]; | |
37 | ofile.write_all(line).unwrap(); | |
38 | ofile.write_all(pad.as_slice()).unwrap(); | |
39 | ||
40 | let bend2 = base2 + w2; | |
41 | let line = &dta[base2..bend2]; | |
42 | ofile.write_all(line).unwrap(); | |
43 | ofile.write_all(pad.as_slice()).unwrap(); | |
44 | ||
45 | base1 += stride1; | |
46 | base2 += stride2; | |
47 | } | |
48 | } | |
49 | ||
50 | pub fn write_palppm(pfx: &str, strno: usize, num: u64, frm: Ref<NAFrame>) { | |
cd9ece3c | 51 | let name = format!("{}out{:02}_{:08}.ppm", pfx, strno, num); |
3660c127 KS |
52 | let mut ofile = File::create(name).unwrap(); |
53 | let buf = frm.get_buffer().get_vbuf().unwrap(); | |
54 | let (w, h) = buf.get_dimensions(0); | |
55 | let paloff = buf.get_offset(1); | |
56 | let hdr = format!("P6\n{} {}\n255\n", w, h); | |
57 | ofile.write_all(hdr.as_bytes()).unwrap(); | |
58 | let dta = buf.get_data(); | |
59 | let ls = buf.get_stride(0); | |
ffe69577 KS |
60 | let offs: [usize; 3] = [ |
61 | buf.get_info().get_format().get_chromaton(0).unwrap().get_offset() as usize, | |
62 | buf.get_info().get_format().get_chromaton(1).unwrap().get_offset() as usize, | |
63 | buf.get_info().get_format().get_chromaton(2).unwrap().get_offset() as usize | |
64 | ]; | |
3660c127 KS |
65 | let mut idx = 0; |
66 | let mut line: Vec<u8> = Vec::with_capacity(w * 3); | |
67 | line.resize(w * 3, 0); | |
68 | for _ in 0..h { | |
69 | let src = &dta[idx..(idx+w)]; | |
70 | for x in 0..w { | |
71 | let pix = src[x] as usize; | |
ffe69577 KS |
72 | line[x * 3 + 0] = dta[paloff + pix * 3 + offs[0]]; |
73 | line[x * 3 + 1] = dta[paloff + pix * 3 + offs[1]]; | |
74 | line[x * 3 + 2] = dta[paloff + pix * 3 + offs[2]]; | |
3660c127 KS |
75 | } |
76 | ofile.write_all(line.as_slice()).unwrap(); | |
77 | idx += ls; | |
78 | } | |
79 | } | |
ffe69577 KS |
80 | |
81 | pub fn write_ppm(pfx: &str, strno: usize, num: u64, frm: Ref<NAFrame>) { | |
82 | let name = format!("{}out{:02}_{:08}.ppm", pfx, strno, num); | |
83 | let mut ofile = File::create(name).unwrap(); | |
84 | if let NABufferType::VideoPacked(ref buf) = frm.get_buffer() { | |
85 | let (w, h) = buf.get_dimensions(0); | |
86 | let hdr = format!("P6\n{} {}\n255\n", w, h); | |
87 | ofile.write_all(hdr.as_bytes()).unwrap(); | |
88 | let dta = buf.get_data(); | |
89 | let stride = buf.get_stride(0); | |
90 | let offs: [usize; 3] = [ | |
91 | buf.get_info().get_format().get_chromaton(0).unwrap().get_offset() as usize, | |
92 | buf.get_info().get_format().get_chromaton(1).unwrap().get_offset() as usize, | |
93 | buf.get_info().get_format().get_chromaton(2).unwrap().get_offset() as usize | |
94 | ]; | |
95 | let step = buf.get_info().get_format().get_elem_size() as usize; | |
96 | let mut line: Vec<u8> = Vec::with_capacity(w * 3); | |
97 | line.resize(w * 3, 0); | |
98 | for src in dta.chunks(stride) { | |
99 | for x in 0..w { | |
100 | line[x * 3 + 0] = src[x * step + offs[0]]; | |
101 | line[x * 3 + 1] = src[x * step + offs[1]]; | |
102 | line[x * 3 + 2] = src[x * step + offs[2]]; | |
103 | } | |
104 | ofile.write_all(line.as_slice()).unwrap(); | |
105 | } | |
106 | } else if let NABufferType::Video16(ref buf) = frm.get_buffer() { | |
107 | let (w, h) = buf.get_dimensions(0); | |
108 | let hdr = format!("P6\n{} {}\n255\n", w, h); | |
109 | ofile.write_all(hdr.as_bytes()).unwrap(); | |
110 | let dta = buf.get_data(); | |
111 | let stride = buf.get_stride(0); | |
112 | let depths: [u8; 3] = [ | |
113 | buf.get_info().get_format().get_chromaton(0).unwrap().get_depth(), | |
114 | buf.get_info().get_format().get_chromaton(1).unwrap().get_depth(), | |
115 | buf.get_info().get_format().get_chromaton(2).unwrap().get_depth() | |
116 | ]; | |
117 | let masks: [u16; 3] = [ | |
118 | (1 << depths[0]) - 1, | |
119 | (1 << depths[1]) - 1, | |
120 | (1 << depths[2]) - 1 | |
121 | ]; | |
122 | let shifts: [u8; 3] = [ | |
123 | buf.get_info().get_format().get_chromaton(0).unwrap().get_shift(), | |
124 | buf.get_info().get_format().get_chromaton(1).unwrap().get_shift(), | |
125 | buf.get_info().get_format().get_chromaton(2).unwrap().get_shift() | |
126 | ]; | |
127 | let mut line: Vec<u8> = Vec::with_capacity(w * 3); | |
128 | line.resize(w * 3, 0); | |
129 | for src in dta.chunks(stride) { | |
130 | for x in 0..w { | |
131 | let elem = src[x]; | |
132 | let r = ((elem >> shifts[0]) & masks[0]) << (8 - depths[0]); | |
133 | let g = ((elem >> shifts[1]) & masks[1]) << (8 - depths[1]); | |
134 | let b = ((elem >> shifts[2]) & masks[2]) << (8 - depths[2]); | |
135 | line[x * 3 + 0] = r as u8; | |
136 | line[x * 3 + 1] = g as u8; | |
137 | line[x * 3 + 2] = b as u8; | |
138 | } | |
139 | ofile.write_all(line.as_slice()).unwrap(); | |
140 | } | |
141 | } else { | |
142 | panic!(" unhandled buf format"); | |
143 | } | |
144 | } |