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) -> ScaleResult<NABufferType> {
37 self.ncomps = in_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) -> ScaleResult<NABufferType> {
124 self.ncomps = in_fmt.fmt.components as usize;
125 let mut chr: Vec<Option<NAPixelChromaton>> = Vec::with_capacity(MAX_CHROMATONS);
126 for i in 0..self.ncomps {
127 let ichr = in_fmt.fmt.comp_info[i].unwrap();
128 let ochr = dest_fmt.fmt.comp_info[i].unwrap();
129 self.shifts[i] = ichr.shift;
130 self.masks[i] = (1 << ichr.depth) - 1;
131 if ochr.depth > ichr.depth {
132 self.osize[i] = ochr.depth;
134 self.osize[i] = (ichr.depth + 7) & !7;
136 self.depths[i] = ichr.depth;
139 dchr.depth = self.osize[i];
143 chr.push(Some(dchr));
145 let mut df = in_fmt.fmt;
146 for i in 0..self.ncomps {
147 df.comp_info[i] = chr[i];
149 println!(" [intermediate format {}]", df);
150 let res = alloc_video_buffer(NAVideoInfo::new(in_fmt.width, in_fmt.height, false, df), 3);
151 if res.is_err() { return Err(ScaleError::AllocError); }
154 fn process(&mut self, pic_in: &NABufferType, pic_out: &mut NABufferType) {
155 if let Some(ref buf) = pic_in.get_vbuf() {
156 let step = buf.get_info().get_format().get_chromaton(0).unwrap().get_step() as usize;
157 let mut soff: [usize; MAX_CHROMATONS] = [0; MAX_CHROMATONS];
158 for i in 0..self.ncomps {
159 soff[i] = buf.get_info().get_format().get_chromaton(i).unwrap().get_offset() as usize;
161 let (w, h) = buf.get_dimensions(0);
162 let ioff = buf.get_offset(0);
163 let istride = buf.get_stride(0);
164 let sdata1 = buf.get_data();
165 let sdata = &sdata1[ioff..];
166 if let Some(ref mut dbuf) = pic_out.get_vbuf() {
167 let mut ostride: [usize; MAX_CHROMATONS] = [0; MAX_CHROMATONS];
168 let mut offs: [usize; MAX_CHROMATONS] = [0; MAX_CHROMATONS];
169 for i in 0..self.ncomps {
170 ostride[i] = dbuf.get_stride(i);
171 offs[i] = dbuf.get_offset(i);
173 let dst = dbuf.get_data_mut().unwrap();
174 for src in sdata.chunks(istride).take(h) {
176 for i in 0..self.ncomps {
177 dst[offs[i] + x] = src[x * step + soff[i]];
180 for i in 0..self.ncomps { offs[i] += ostride[i]; }
185 } else if let Some(ref buf) = pic_in.get_vbuf16() {
186 let (w, h) = buf.get_dimensions(0);
187 let ioff = buf.get_offset(0);
188 let istride = buf.get_stride(0);
189 let sdata1 = buf.get_data();
190 let sdata = &sdata1[ioff..];
191 if let Some(ref mut dbuf) = pic_out.get_vbuf() {
192 let mut ostride: [usize; MAX_CHROMATONS] = [0; MAX_CHROMATONS];
193 let mut offs: [usize; MAX_CHROMATONS] = [0; MAX_CHROMATONS];
194 for i in 0..self.ncomps {
195 ostride[i] = dbuf.get_stride(i);
196 offs[i] = dbuf.get_offset(i);
198 let dst = dbuf.get_data_mut().unwrap();
199 for src in sdata.chunks(istride).take(h) {
201 let elem = u32::from(src[x]);
202 for i in 0..self.ncomps {
203 dst[offs[i] + x] = convert_depth((elem >> self.shifts[i]) & self.masks[i], self.depths[i], self.osize[i]) as u8;
206 for i in 0..self.ncomps { offs[i] += ostride[i]; }
211 } else if let Some(ref _buf) = pic_in.get_vbuf32() {
219 pub fn create_unpack() -> Box<dyn Kernel> {
220 Box::new(UnpackKernel::new())
225 depths: [u8; MAX_CHROMATONS],
226 coffs: [usize; MAX_CHROMATONS],
232 fn new() -> Self { Self::default() }
235 impl Kernel for DepalKernel {
236 fn init(&mut self, in_fmt: &ScaleInfo, _dest_fmt: &ScaleInfo) -> ScaleResult<NABufferType> {
237 //todo select output more fitting for dest_fmt if possible
238 self.ncomps = in_fmt.fmt.components as usize;
239 let mut chr: Vec<Option<NAPixelChromaton>> = Vec::with_capacity(MAX_CHROMATONS);
240 self.palstep = in_fmt.fmt.elem_size as usize;
241 for i in 0..self.ncomps {
242 let ichr = in_fmt.fmt.comp_info[i].unwrap();
243 self.coffs[i] = ichr.comp_offs as usize;
244 self.depths[i] = ichr.depth;
249 dchr.comp_offs = i as u8;
251 chr.push(Some(dchr));
253 let mut df = in_fmt.fmt;
255 for i in 0..self.ncomps {
256 df.comp_info[i] = chr[i];
258 println!(" [intermediate format {}]", df);
259 let res = alloc_video_buffer(NAVideoInfo::new(in_fmt.width, in_fmt.height, false, df), 3);
260 if res.is_err() { return Err(ScaleError::AllocError); }
263 fn process(&mut self, pic_in: &NABufferType, pic_out: &mut NABufferType) {
264 if let (Some(ref sbuf), Some(ref mut dbuf)) = (pic_in.get_vbuf(), pic_out.get_vbuf()) {
265 let ioff = sbuf.get_offset(0);
266 let paloff = sbuf.get_offset(1);
267 let (w, h) = sbuf.get_dimensions(0);
268 let istride = sbuf.get_stride(0);
269 let sdata1 = sbuf.get_data();
270 let sdata = &sdata1[ioff..];
271 let pal = &sdata1[paloff..];
273 let mut ostride: [usize; MAX_CHROMATONS] = [0; MAX_CHROMATONS];
274 let mut offs: [usize; MAX_CHROMATONS] = [0; MAX_CHROMATONS];
275 for i in 0..self.ncomps {
276 ostride[i] = dbuf.get_stride(i);
277 offs[i] = dbuf.get_offset(i);
279 let dst = dbuf.get_data_mut().unwrap();
280 for src in sdata.chunks(istride).take(h) {
282 let palidx = src[x] as usize;
283 for i in 0..self.ncomps {
284 let elem = u32::from(pal[palidx * self.palstep + self.coffs[i]]);
285 dst[offs[i] + x] = convert_depth(elem, self.depths[i], 8) as u8;
288 for i in 0..self.ncomps { offs[i] += ostride[i]; }
296 pub fn create_depal() -> Box<dyn Kernel> {
297 Box::new(DepalKernel::new())