3 use super::kernel::Kernel;
5 fn convert_depth(val: u32, indepth: u8, outdepth: u8) -> u32 {
6 if indepth >= outdepth {
7 val >> (indepth - outdepth)
9 let mut val2 = val << (outdepth - indepth);
10 let mut shift = outdepth - indepth;
11 while shift >= indepth {
16 val2 |= val >> (indepth - shift);
24 shifts: [u8; MAX_CHROMATONS],
25 depths: [u8; MAX_CHROMATONS],
27 osize: [u8; MAX_CHROMATONS],
28 ooff: [usize; MAX_CHROMATONS],
32 fn new() -> Self { Self::default() }
35 impl Kernel for PackKernel {
36 fn init(&mut self, in_fmt: &ScaleInfo, dest_fmt: &ScaleInfo, _options: &[(String, String)]) -> ScaleResult<NABufferType> {
37 self.ncomps = in_fmt.fmt.components.min(dest_fmt.fmt.components) as usize;
38 for i in 0..self.ncomps {
39 let ichr = in_fmt.fmt.comp_info[i].unwrap();
40 let ochr = dest_fmt.fmt.comp_info[i].unwrap();
41 self.shifts[i] = ochr.shift;
42 self.osize[i] = ochr.depth;
43 self.depths[i] = ichr.depth;
44 self.ooff[i] = ochr.comp_offs as usize;
46 let res = alloc_video_buffer(NAVideoInfo::new(in_fmt.width, in_fmt.height, false, dest_fmt.fmt), 3);
47 if res.is_err() { return Err(ScaleError::AllocError); }
50 fn process(&mut self, pic_in: &NABufferType, pic_out: &mut NABufferType) {
51 if let Some(ref buf) = pic_in.get_vbuf() {
52 if let Some(ref mut dbuf) = pic_out.get_vbuf() {
53 let dstride = dbuf.get_stride(0);
54 for comp in 0..self.ncomps {
55 let ioff = buf.get_offset(comp);
56 let istride = buf.get_stride(comp);
57 let step = dbuf.get_info().get_format().get_chromaton(comp).unwrap().get_step() as usize;
58 let (w, h) = dbuf.get_dimensions(comp);
59 let sdata = buf.get_data();
60 let sdata = &sdata[ioff..];
61 let ddata = dbuf.get_data_mut().unwrap();
62 for (src, dst) in sdata.chunks(istride).zip(ddata.chunks_mut(dstride)).take(h) {
64 dst[x * step + self.ooff[comp]] = convert_depth(u32::from(src[x]), self.depths[comp], self.osize[comp]) as u8;
68 } else if let Some(ref mut dbuf) = pic_out.get_vbuf16() {
69 let (w, h) = dbuf.get_dimensions(0);
70 let dstride = dbuf.get_stride(0);
71 let ddata = dbuf.get_data_mut().unwrap();
72 let src = buf.get_data();
73 let mut ioff: [usize; MAX_CHROMATONS] = [0; MAX_CHROMATONS];
74 let mut istride: [usize; MAX_CHROMATONS] = [0; MAX_CHROMATONS];
75 for comp in 0..self.ncomps {
76 ioff[comp] = buf.get_offset(comp);
77 istride[comp] = buf.get_stride(comp);
79 for dst in ddata.chunks_mut(dstride).take(h) {
81 let mut elem: u32 = 0;
82 for comp in 0..self.ncomps {
83 let c = u32::from(src[ioff[comp] + x]);
84 elem |= convert_depth(c, self.depths[comp], self.osize[comp]) << self.shifts[comp];
88 for comp in 0..self.ncomps {
89 ioff[comp] += istride[comp];
95 } else if let Some(ref _buf) = pic_in.get_vbuf16() {
97 } else if let Some(ref _buf) = pic_in.get_vbuf32() {
105 pub fn create_pack() -> Box<dyn Kernel> {
106 Box::new(PackKernel::new())
110 struct UnpackKernel {
111 shifts: [u8; MAX_CHROMATONS],
112 masks: [u32; MAX_CHROMATONS],
113 depths: [u8; MAX_CHROMATONS],
115 osize: [u8; MAX_CHROMATONS],
119 fn new() -> Self { Self::default() }
122 impl Kernel for UnpackKernel {
123 fn init(&mut self, in_fmt: &ScaleInfo, dest_fmt: &ScaleInfo, options: &[(String, String)]) -> ScaleResult<NABufferType> {
124 let mut debug = false;
125 for (name, value) in options.iter() {
126 match (name.as_str(), value.as_str()) {
127 ("debug", "") => { debug = true; },
128 ("debug", "true") => { debug = true; },
133 self.ncomps = in_fmt.fmt.components.min(dest_fmt.fmt.components) as usize;
134 let mut chr: Vec<Option<NAPixelChromaton>> = Vec::with_capacity(MAX_CHROMATONS);
135 for i in 0..self.ncomps {
136 let ichr = in_fmt.fmt.comp_info[i].unwrap();
137 let ochr = dest_fmt.fmt.comp_info[i].unwrap();
138 self.shifts[i] = ichr.shift;
139 self.masks[i] = (1 << ichr.depth) - 1;
140 if ochr.depth > ichr.depth {
141 self.osize[i] = ochr.depth;
143 self.osize[i] = (ichr.depth + 7) & !7;
145 self.depths[i] = ichr.depth;
148 dchr.depth = self.osize[i];
152 chr.push(Some(dchr));
154 let mut df = in_fmt.fmt;
155 df.comp_info[..self.ncomps].clone_from_slice(&chr[..self.ncomps]);
156 df.components = self.ncomps as u8;
159 println!(" [intermediate format {}]", df);
161 let res = alloc_video_buffer(NAVideoInfo::new(in_fmt.width, in_fmt.height, false, df), 3);
162 if res.is_err() { return Err(ScaleError::AllocError); }
165 fn process(&mut self, pic_in: &NABufferType, pic_out: &mut NABufferType) {
166 if let Some(ref buf) = pic_in.get_vbuf() {
167 let step = buf.get_info().get_format().elem_size as usize;
168 let mut soff: [usize; MAX_CHROMATONS] = [0; MAX_CHROMATONS];
169 for i in 0..self.ncomps {
170 soff[i] = buf.get_info().get_format().get_chromaton(i).unwrap().get_offset() as usize;
172 let (w, h) = buf.get_dimensions(0);
173 let ioff = buf.get_offset(0);
174 let istride = buf.get_stride(0);
175 let sdata1 = buf.get_data();
176 let sdata = &sdata1[ioff..];
177 let ychr = buf.get_info().get_format().get_chromaton(0).unwrap();
178 if let Some(ref mut dbuf) = pic_out.get_vbuf() {
179 let mut ostride: [usize; MAX_CHROMATONS] = [0; MAX_CHROMATONS];
180 let mut offs: [usize; MAX_CHROMATONS] = [0; MAX_CHROMATONS];
181 for i in 0..self.ncomps {
182 ostride[i] = dbuf.get_stride(i);
183 offs[i] = dbuf.get_offset(i);
185 let dst = dbuf.get_data_mut().unwrap();
186 if ychr.next_elem == 0 || usize::from(ychr.next_elem) == step {
187 for src in sdata.chunks(istride).take(h) {
189 for i in 0..self.ncomps {
190 dst[offs[i] + x] = src[x * step + soff[i]];
193 for i in 0..self.ncomps { offs[i] += ostride[i]; }
196 let mut steps: [usize; MAX_CHROMATONS] = [0; MAX_CHROMATONS];
197 for i in 0..self.ncomps {
198 steps[i] = buf.get_info().get_format().get_chromaton(i).unwrap().next_elem as usize;
201 for src in sdata.chunks(istride).take(h) {
203 for piece in src.chunks(step) {
204 for i in 0..self.ncomps {
205 let mut co = soff[i];
207 dst[x[i]] = piece[co];
213 for i in 0..self.ncomps { offs[i] += ostride[i]; }
219 } else if let Some(ref buf) = pic_in.get_vbuf16() {
220 let (w, h) = buf.get_dimensions(0);
221 let ioff = buf.get_offset(0);
222 let istride = buf.get_stride(0);
223 let sdata1 = buf.get_data();
224 let sdata = &sdata1[ioff..];
225 if let Some(ref mut dbuf) = pic_out.get_vbuf() {
226 let mut ostride: [usize; MAX_CHROMATONS] = [0; MAX_CHROMATONS];
227 let mut offs: [usize; MAX_CHROMATONS] = [0; MAX_CHROMATONS];
228 for i in 0..self.ncomps {
229 ostride[i] = dbuf.get_stride(i);
230 offs[i] = dbuf.get_offset(i);
232 let dst = dbuf.get_data_mut().unwrap();
233 for src in sdata.chunks(istride).take(h) {
235 let elem = u32::from(src[x]);
236 for i in 0..self.ncomps {
237 dst[offs[i] + x] = convert_depth((elem >> self.shifts[i]) & self.masks[i], self.depths[i], self.osize[i]) as u8;
240 for i in 0..self.ncomps { offs[i] += ostride[i]; }
245 } else if let Some(ref _buf) = pic_in.get_vbuf32() {
253 pub fn create_unpack() -> Box<dyn Kernel> {
254 Box::new(UnpackKernel::new())
259 depths: [u8; MAX_CHROMATONS],
260 coffs: [usize; MAX_CHROMATONS],
266 fn new() -> Self { Self::default() }
269 impl Kernel for DepalKernel {
270 fn init(&mut self, in_fmt: &ScaleInfo, _dest_fmt: &ScaleInfo, options: &[(String, String)]) -> ScaleResult<NABufferType> {
271 let mut debug = false;
272 for (name, value) in options.iter() {
273 match (name.as_str(), value.as_str()) {
274 ("debug", "") => { debug = true; },
275 ("debug", "true") => { debug = true; },
280 //todo select output more fitting for dest_fmt if possible
281 self.ncomps = in_fmt.fmt.components as usize;
282 let mut chr: Vec<Option<NAPixelChromaton>> = Vec::with_capacity(MAX_CHROMATONS);
283 self.palstep = in_fmt.fmt.elem_size as usize;
284 for i in 0..self.ncomps {
285 let ichr = in_fmt.fmt.comp_info[i].unwrap();
286 self.coffs[i] = ichr.comp_offs as usize;
287 self.depths[i] = ichr.depth;
292 dchr.comp_offs = i as u8;
294 chr.push(Some(dchr));
296 let mut df = in_fmt.fmt;
298 df.comp_info[..self.ncomps].clone_from_slice(&chr[..self.ncomps]);
300 println!(" [intermediate format {}]", df);
302 let res = alloc_video_buffer(NAVideoInfo::new(in_fmt.width, in_fmt.height, false, df), 3);
303 if res.is_err() { return Err(ScaleError::AllocError); }
306 fn process(&mut self, pic_in: &NABufferType, pic_out: &mut NABufferType) {
307 if let (Some(ref sbuf), Some(ref mut dbuf)) = (pic_in.get_vbuf(), pic_out.get_vbuf()) {
308 let ioff = sbuf.get_offset(0);
309 let paloff = sbuf.get_offset(1);
310 let (w, h) = sbuf.get_dimensions(0);
311 let istride = sbuf.get_stride(0);
312 let sdata1 = sbuf.get_data();
313 let sdata = &sdata1[ioff..];
314 let pal = &sdata1[paloff..];
316 let mut ostride: [usize; MAX_CHROMATONS] = [0; MAX_CHROMATONS];
317 let mut offs: [usize; MAX_CHROMATONS] = [0; MAX_CHROMATONS];
318 for i in 0..self.ncomps {
319 ostride[i] = dbuf.get_stride(i);
320 offs[i] = dbuf.get_offset(i);
322 let dst = dbuf.get_data_mut().unwrap();
323 for src in sdata.chunks(istride).take(h) {
325 let palidx = src[x] as usize;
326 for i in 0..self.ncomps {
327 let elem = u32::from(pal[palidx * self.palstep + self.coffs[i]]);
328 dst[offs[i] + x] = convert_depth(elem, self.depths[i], 8) as u8;
331 for i in 0..self.ncomps { offs[i] += ostride[i]; }
339 pub fn create_depal() -> Box<dyn Kernel> {
340 Box::new(DepalKernel::new())