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