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