]>
Commit | Line | Data |
---|---|---|
8424ddfd KS |
1 | //! Simple PNM image writers for RGB and YUV images. |
2 | use std::io::prelude::*; | |
3 | use std::fs::File; | |
4a521cd8 KS |
4 | use nihav_core::frame::{NABufferType, NAFrameRef, NAFrame, alloc_video_buffer}; |
5 | use nihav_core::scale::*; | |
6 | use nihav_core::formats::YUV420_FORMAT; | |
8424ddfd KS |
7 | |
8 | /// Writes PGMYUV for input frame. | |
9 | pub fn write_pgmyuv(name: &str, frm: NAFrameRef) -> std::io::Result<()> { | |
10 | if let NABufferType::None = frm.get_buffer() { return Ok(()); } | |
11 | let mut ofile = File::create(name)?; | |
12 | let buf = frm.get_buffer().get_vbuf().unwrap(); | |
13 | let is_flipped = buf.get_info().is_flipped(); | |
14 | let (w, h) = buf.get_dimensions(0); | |
15 | let (w2, h2) = buf.get_dimensions(1); | |
16 | let full_w = w2 * 2 > w; | |
17 | let has_alpha = buf.get_info().get_format().has_alpha(); | |
18 | let mut tot_h = h + h2; | |
19 | if has_alpha { | |
20 | tot_h += h; | |
21 | } | |
22 | if full_w { | |
23 | tot_h += h2; | |
24 | } | |
25 | let hdr = format!("P5\n{} {}\n255\n", w, tot_h); | |
26 | ofile.write_all(hdr.as_bytes())?; | |
27 | let dta = buf.get_data(); | |
28 | let ls = buf.get_stride(0); | |
29 | let pad: Vec<u8> = vec![0xFF; if !full_w { (w - w2 * 2) / 2 } else { w - w2 } ]; | |
30 | if !is_flipped { | |
31 | let ylines = dta.chunks(ls).take(h); | |
32 | for line in ylines { | |
33 | ofile.write_all(&line[..w])?; | |
34 | } | |
35 | } else { | |
36 | let ylines = dta[..h * ls].chunks(ls).rev(); | |
37 | for line in ylines { | |
38 | ofile.write_all(&line[..w])?; | |
39 | } | |
40 | } | |
41 | let base1 = buf.get_offset(1); | |
42 | let stride1 = buf.get_stride(1); | |
43 | let base2 = buf.get_offset(2); | |
44 | let stride2 = buf.get_stride(2); | |
45 | let u = &dta[base1..][..h2*stride1]; | |
46 | let v = &dta[base2..][..h2*stride2]; | |
47 | let has_chroma = stride1 > 0 && stride2 > 0; | |
48 | if !full_w && has_chroma { | |
49 | if !is_flipped { | |
50 | for (uline, vline) in u.chunks(stride1).zip(v.chunks(stride2)) { | |
51 | ofile.write_all(&uline[..w2])?; | |
52 | ofile.write_all(pad.as_slice())?; | |
53 | ||
54 | ofile.write_all(&vline[..w2])?; | |
55 | ofile.write_all(pad.as_slice())?; | |
56 | } | |
57 | } else { | |
58 | for (uline, vline) in u.chunks(stride1).rev().zip(v.chunks(stride2).rev()) { | |
59 | ofile.write_all(&uline[..w2])?; | |
60 | ofile.write_all(pad.as_slice())?; | |
61 | ||
62 | ofile.write_all(&vline[..w2])?; | |
63 | ofile.write_all(pad.as_slice())?; | |
64 | } | |
65 | } | |
66 | } else if has_chroma { | |
67 | if !is_flipped { | |
68 | for uline in u.chunks(stride1) { | |
69 | ofile.write_all(&uline[..w2])?; | |
70 | ofile.write_all(pad.as_slice())?; | |
71 | } | |
72 | for vline in v.chunks(stride2) { | |
73 | ofile.write_all(&vline[..w2])?; | |
74 | ofile.write_all(pad.as_slice())?; | |
75 | } | |
76 | } else { | |
77 | for uline in u.chunks(stride1).rev() { | |
78 | ofile.write_all(&uline[..w2])?; | |
79 | ofile.write_all(pad.as_slice())?; | |
80 | } | |
81 | for vline in v.chunks(stride2).rev() { | |
82 | ofile.write_all(&vline[..w2])?; | |
83 | ofile.write_all(pad.as_slice())?; | |
84 | } | |
85 | } | |
86 | } | |
87 | if has_alpha { | |
88 | let ls = buf.get_stride(3); | |
62578014 KS |
89 | if !is_flipped { |
90 | let alines = dta[buf.get_offset(3)..].chunks(ls).take(h); | |
91 | for line in alines { | |
92 | ofile.write_all(&line[..w])?; | |
93 | } | |
94 | } else { | |
95 | let alines = dta[buf.get_offset(3)..].chunks(ls).take(h).rev(); | |
96 | for line in alines { | |
97 | ofile.write_all(&line[..w])?; | |
98 | } | |
8424ddfd KS |
99 | } |
100 | } | |
101 | Ok(()) | |
102 | } | |
103 | ||
104 | /// Writes output PPM for input paletted input frame. | |
105 | pub fn write_palppm(name: &str, frm: NAFrameRef) -> std::io::Result<()> { | |
106 | if let NABufferType::None = frm.get_buffer() { return Ok(()); } | |
107 | let mut ofile = File::create(name)?; | |
108 | let buf = frm.get_buffer().get_vbuf().unwrap(); | |
109 | let (w, h) = buf.get_dimensions(0); | |
110 | let paloff = buf.get_offset(1); | |
111 | let hdr = format!("P6\n{} {}\n255\n", w, h); | |
112 | ofile.write_all(hdr.as_bytes())?; | |
113 | let dta = buf.get_data(); | |
114 | let ls = buf.get_stride(0); | |
115 | let offs: [usize; 3] = [ | |
116 | buf.get_info().get_format().get_chromaton(0).unwrap().get_offset() as usize, | |
117 | buf.get_info().get_format().get_chromaton(1).unwrap().get_offset() as usize, | |
118 | buf.get_info().get_format().get_chromaton(2).unwrap().get_offset() as usize | |
119 | ]; | |
120 | let flipped = buf.get_info().is_flipped(); | |
121 | let mut idx = if !flipped { 0 } else { ls * h }; | |
122 | let mut line: Vec<u8> = vec![0; w * 3]; | |
123 | for _ in 0..h { | |
124 | if flipped { | |
125 | idx -= ls; | |
126 | } | |
127 | let src = &dta[idx..(idx+w)]; | |
128 | for x in 0..w { | |
129 | let pix = src[x] as usize; | |
130 | line[x * 3 + 0] = dta[paloff + pix * 3 + offs[0]]; | |
131 | line[x * 3 + 1] = dta[paloff + pix * 3 + offs[1]]; | |
132 | line[x * 3 + 2] = dta[paloff + pix * 3 + offs[2]]; | |
133 | } | |
134 | ofile.write_all(line.as_slice())?; | |
135 | if !flipped { | |
136 | idx += ls; | |
137 | } | |
138 | } | |
139 | Ok(()) | |
140 | } | |
141 | ||
142 | /// Writes PPM file for RGB input. | |
143 | pub fn write_rgbppm(name: &str, frm: NAFrameRef) -> std::io::Result<()> { | |
144 | if let NABufferType::None = frm.get_buffer() { return Ok(()); } | |
145 | let mut ofile = File::create(name)?; | |
146 | let info = frm.get_buffer().get_video_info().unwrap(); | |
147 | let flipped = info.is_flipped(); | |
148 | let buffer = frm.get_buffer(); | |
149 | if let Some(ref buf) = buffer.get_vbuf() { | |
150 | let (w, h) = buf.get_dimensions(0); | |
151 | let hdr = format!("P6\n{} {}\n255\n", w, h); | |
152 | ofile.write_all(hdr.as_bytes())?; | |
153 | let dta = buf.get_data(); | |
154 | let stride = buf.get_stride(0); | |
155 | let offs: [usize; 3] = [ | |
156 | info.get_format().get_chromaton(0).unwrap().get_offset() as usize, | |
157 | info.get_format().get_chromaton(1).unwrap().get_offset() as usize, | |
158 | info.get_format().get_chromaton(2).unwrap().get_offset() as usize | |
159 | ]; | |
160 | let step = info.get_format().get_elem_size() as usize; | |
161 | let mut line: Vec<u8> = vec![0; w * 3]; | |
162 | if !flipped { | |
163 | for src in dta.chunks(stride) { | |
164 | for x in 0..w { | |
165 | line[x * 3 + 0] = src[x * step + offs[0]]; | |
166 | line[x * 3 + 1] = src[x * step + offs[1]]; | |
167 | line[x * 3 + 2] = src[x * step + offs[2]]; | |
168 | } | |
169 | ofile.write_all(line.as_slice())?; | |
170 | } | |
171 | } else { | |
172 | for src in dta[..stride * h].chunks(stride).rev() { | |
173 | for x in 0..w { | |
174 | line[x * 3 + 0] = src[x * step + offs[0]]; | |
175 | line[x * 3 + 1] = src[x * step + offs[1]]; | |
176 | line[x * 3 + 2] = src[x * step + offs[2]]; | |
177 | } | |
178 | ofile.write_all(line.as_slice())?; | |
179 | } | |
180 | } | |
181 | } else if let NABufferType::Video16(ref buf) = buffer { | |
182 | let (w, h) = buf.get_dimensions(0); | |
183 | let hdr = format!("P6\n{} {}\n255\n", w, h); | |
184 | ofile.write_all(hdr.as_bytes())?; | |
185 | let dta = buf.get_data(); | |
186 | let stride = buf.get_stride(0); | |
187 | let depths: [u8; 3] = [ | |
188 | info.get_format().get_chromaton(0).unwrap().get_depth(), | |
189 | info.get_format().get_chromaton(1).unwrap().get_depth(), | |
190 | info.get_format().get_chromaton(2).unwrap().get_depth() | |
191 | ]; | |
192 | let masks: [u16; 3] = [ | |
193 | (1 << depths[0]) - 1, | |
194 | (1 << depths[1]) - 1, | |
195 | (1 << depths[2]) - 1 | |
196 | ]; | |
197 | let shifts: [u8; 3] = [ | |
198 | info.get_format().get_chromaton(0).unwrap().get_shift(), | |
199 | info.get_format().get_chromaton(1).unwrap().get_shift(), | |
200 | info.get_format().get_chromaton(2).unwrap().get_shift() | |
201 | ]; | |
202 | let mut line: Vec<u8> = vec![0; w * 3]; | |
203 | if !flipped { | |
204 | for src in dta.chunks(stride) { | |
205 | for x in 0..w { | |
206 | let elem = src[x]; | |
207 | let r = ((elem >> shifts[0]) & masks[0]) << (8 - depths[0]); | |
208 | let g = ((elem >> shifts[1]) & masks[1]) << (8 - depths[1]); | |
209 | let b = ((elem >> shifts[2]) & masks[2]) << (8 - depths[2]); | |
210 | line[x * 3 + 0] = r as u8; | |
211 | line[x * 3 + 1] = g as u8; | |
212 | line[x * 3 + 2] = b as u8; | |
213 | } | |
214 | ofile.write_all(line.as_slice())?; | |
215 | } | |
216 | } else { | |
217 | for src in dta[..h * stride].chunks(stride).rev() { | |
218 | for x in 0..w { | |
219 | let elem = src[x]; | |
220 | let r = ((elem >> shifts[0]) & masks[0]) << (8 - depths[0]); | |
221 | let g = ((elem >> shifts[1]) & masks[1]) << (8 - depths[1]); | |
222 | let b = ((elem >> shifts[2]) & masks[2]) << (8 - depths[2]); | |
223 | line[x * 3 + 0] = r as u8; | |
224 | line[x * 3 + 1] = g as u8; | |
225 | line[x * 3 + 2] = b as u8; | |
226 | } | |
227 | ofile.write_all(line.as_slice())?; | |
228 | } | |
229 | } | |
230 | } else { | |
231 | panic!(" unhandled buf format"); | |
232 | } | |
233 | Ok(()) | |
234 | } | |
235 | ||
236 | /// Writes PNM file with a format depending on input format. | |
237 | pub fn write_pnm(pfx: &str, strno: usize, num: u64, frm: NAFrameRef) -> std::io::Result<()> { | |
238 | if let NABufferType::None = frm.get_buffer() { return Ok(()); } | |
239 | ||
240 | let vinfo = frm.get_buffer().get_video_info().unwrap(); | |
241 | if vinfo.get_format().is_paletted() { | |
242 | let name = format!("{}{:02}_{:06}.ppm", pfx, strno, num); | |
243 | write_palppm(name.as_str(), frm) | |
244 | } else if vinfo.get_format().get_model().is_yuv() { | |
245 | let name = format!("{}{:02}_{:06}.pgm", pfx, strno, num); | |
4a521cd8 KS |
246 | if vinfo.get_format().is_unpacked() { |
247 | write_pgmyuv(name.as_str(), frm) | |
248 | } else { | |
249 | let mut dst_vinfo = vinfo; | |
250 | dst_vinfo.format = YUV420_FORMAT; | |
251 | let mut cvt_buf = alloc_video_buffer(dst_vinfo, 2).unwrap(); | |
252 | let buf = frm.get_buffer(); | |
253 | ||
254 | let ifmt = get_scale_fmt_from_pic(&buf); | |
255 | let ofmt = get_scale_fmt_from_pic(&cvt_buf); | |
256 | let mut scaler = NAScale::new(ifmt, ofmt).unwrap(); | |
257 | scaler.convert(&buf, &mut cvt_buf).unwrap(); | |
258 | let frm = NAFrame::new(frm.get_time_information(), frm.frame_type, frm.key, frm.get_info(), cvt_buf); | |
259 | write_pgmyuv(name.as_str(), frm.into_ref()) | |
260 | } | |
8424ddfd KS |
261 | } else if vinfo.get_format().get_model().is_rgb() { |
262 | let name = format!("{}{:02}_{:06}.ppm", pfx, strno, num); | |
263 | write_rgbppm(name.as_str(), frm) | |
264 | } else { | |
265 | panic!(" unknown format"); | |
266 | } | |
267 | } | |
268 |