]>
Commit | Line | Data |
---|---|---|
03accf76 KS |
1 | use crate::formats::*; |
2 | use super::*; | |
3 | use super::kernel::Kernel; | |
4 | ||
5 | fn convert_depth(val: u32, indepth: u8, outdepth: u8) -> u32 { | |
6 | if indepth >= outdepth { | |
7 | val >> (indepth - outdepth) | |
8 | } else { | |
9 | let mut val2 = val << (outdepth - indepth); | |
10 | let mut shift = outdepth - indepth; | |
11 | while shift >= indepth { | |
12 | shift -= indepth; | |
13 | val2 |= val << shift; | |
14 | } | |
15 | if shift > 0 { | |
16 | val2 |= val >> (indepth - shift); | |
17 | } | |
18 | val2 | |
19 | } | |
20 | } | |
21 | ||
22 | #[derive(Default)] | |
23 | struct PackKernel { | |
24 | shifts: [u8; MAX_CHROMATONS], | |
25 | depths: [u8; MAX_CHROMATONS], | |
26 | ncomps: usize, | |
27 | osize: [u8; MAX_CHROMATONS], | |
28 | ooff: [usize; MAX_CHROMATONS], | |
29 | } | |
30 | ||
31 | impl PackKernel { | |
32 | fn new() -> Self { Self::default() } | |
33 | } | |
34 | ||
35 | impl Kernel for PackKernel { | |
36 | fn init(&mut self, in_fmt: &ScaleInfo, dest_fmt: &ScaleInfo) -> ScaleResult<NABufferType> { | |
4997179d | 37 | self.ncomps = in_fmt.fmt.components.min(dest_fmt.fmt.components) as usize; |
03accf76 KS |
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; | |
45 | } | |
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); } | |
48 | Ok(res.unwrap()) | |
49 | } | |
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) { | |
63 | for x in 0..w { | |
e243ceb4 | 64 | dst[x * step + self.ooff[comp]] = convert_depth(u32::from(src[x]), self.depths[comp], self.osize[comp]) as u8; |
03accf76 KS |
65 | } |
66 | } | |
67 | } | |
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); | |
78 | } | |
79 | for dst in ddata.chunks_mut(dstride).take(h) { | |
80 | for x in 0..w { | |
81 | let mut elem: u32 = 0; | |
82 | for comp in 0..self.ncomps { | |
e243ceb4 | 83 | let c = u32::from(src[ioff[comp] + x]); |
03accf76 KS |
84 | elem |= convert_depth(c, self.depths[comp], self.osize[comp]) << self.shifts[comp]; |
85 | } | |
86 | dst[x] = elem as u16; | |
87 | } | |
88 | for comp in 0..self.ncomps { | |
89 | ioff[comp] += istride[comp]; | |
90 | } | |
91 | } | |
92 | } else { | |
93 | unimplemented!(); | |
94 | } | |
95 | } else if let Some(ref _buf) = pic_in.get_vbuf16() { | |
96 | unimplemented!(); | |
97 | } else if let Some(ref _buf) = pic_in.get_vbuf32() { | |
98 | unimplemented!(); | |
99 | } else { | |
100 | unreachable!(); | |
101 | } | |
102 | } | |
103 | } | |
104 | ||
6011e201 | 105 | pub fn create_pack() -> Box<dyn Kernel> { |
03accf76 KS |
106 | Box::new(PackKernel::new()) |
107 | } | |
108 | ||
109 | #[derive(Default)] | |
110 | struct UnpackKernel { | |
111 | shifts: [u8; MAX_CHROMATONS], | |
112 | masks: [u32; MAX_CHROMATONS], | |
113 | depths: [u8; MAX_CHROMATONS], | |
114 | ncomps: usize, | |
115 | osize: [u8; MAX_CHROMATONS], | |
116 | } | |
117 | ||
118 | impl UnpackKernel { | |
119 | fn new() -> Self { Self::default() } | |
120 | } | |
121 | ||
122 | impl Kernel for UnpackKernel { | |
123 | fn init(&mut self, in_fmt: &ScaleInfo, dest_fmt: &ScaleInfo) -> ScaleResult<NABufferType> { | |
4997179d | 124 | self.ncomps = in_fmt.fmt.components.min(dest_fmt.fmt.components) as usize; |
03accf76 KS |
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; | |
133 | } else { | |
134 | self.osize[i] = (ichr.depth + 7) & !7; | |
135 | } | |
136 | self.depths[i] = ichr.depth; | |
137 | let mut dchr = ichr; | |
138 | dchr.packed = false; | |
139 | dchr.depth = self.osize[i]; | |
140 | dchr.shift = 0; | |
141 | dchr.comp_offs = 0; | |
142 | dchr.next_elem = 0; | |
143 | chr.push(Some(dchr)); | |
144 | } | |
145 | let mut df = in_fmt.fmt; | |
b36f412c | 146 | df.comp_info[..self.ncomps].clone_from_slice(&chr[..self.ncomps]); |
4997179d | 147 | df.components = self.ncomps as u8; |
4b459d0b | 148 | df.palette = false; |
03accf76 KS |
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); } | |
152 | Ok(res.unwrap()) | |
153 | } | |
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; | |
160 | } | |
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); | |
172 | } | |
173 | let dst = dbuf.get_data_mut().unwrap(); | |
174 | for src in sdata.chunks(istride).take(h) { | |
175 | for x in 0..w { | |
176 | for i in 0..self.ncomps { | |
177 | dst[offs[i] + x] = src[x * step + soff[i]]; | |
178 | } | |
179 | } | |
180 | for i in 0..self.ncomps { offs[i] += ostride[i]; } | |
181 | } | |
182 | } else { | |
183 | unimplemented!(); | |
184 | } | |
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); | |
197 | } | |
198 | let dst = dbuf.get_data_mut().unwrap(); | |
199 | for src in sdata.chunks(istride).take(h) { | |
200 | for x in 0..w { | |
e243ceb4 | 201 | let elem = u32::from(src[x]); |
03accf76 KS |
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; | |
204 | } | |
205 | } | |
206 | for i in 0..self.ncomps { offs[i] += ostride[i]; } | |
207 | } | |
208 | } else { | |
209 | unimplemented!(); | |
210 | } | |
211 | } else if let Some(ref _buf) = pic_in.get_vbuf32() { | |
212 | unimplemented!(); | |
213 | } else { | |
214 | unreachable!(); | |
215 | } | |
216 | } | |
217 | } | |
218 | ||
6011e201 | 219 | pub fn create_unpack() -> Box<dyn Kernel> { |
03accf76 KS |
220 | Box::new(UnpackKernel::new()) |
221 | } | |
222 | ||
223 | #[derive(Default)] | |
224 | struct DepalKernel { | |
225 | depths: [u8; MAX_CHROMATONS], | |
226 | coffs: [usize; MAX_CHROMATONS], | |
227 | ncomps: usize, | |
228 | palstep: usize, | |
229 | } | |
230 | ||
231 | impl DepalKernel { | |
232 | fn new() -> Self { Self::default() } | |
233 | } | |
234 | ||
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; | |
245 | let mut dchr = ichr; | |
246 | dchr.packed = false; | |
247 | dchr.depth = 8; | |
248 | dchr.shift = 0; | |
249 | dchr.comp_offs = i as u8; | |
250 | dchr.next_elem = 0; | |
251 | chr.push(Some(dchr)); | |
252 | } | |
253 | let mut df = in_fmt.fmt; | |
254 | df.palette = false; | |
b36f412c | 255 | df.comp_info[..self.ncomps].clone_from_slice(&chr[..self.ncomps]); |
03accf76 KS |
256 | println!(" [intermediate format {}]", df); |
257 | let res = alloc_video_buffer(NAVideoInfo::new(in_fmt.width, in_fmt.height, false, df), 3); | |
258 | if res.is_err() { return Err(ScaleError::AllocError); } | |
259 | Ok(res.unwrap()) | |
260 | } | |
261 | fn process(&mut self, pic_in: &NABufferType, pic_out: &mut NABufferType) { | |
262 | if let (Some(ref sbuf), Some(ref mut dbuf)) = (pic_in.get_vbuf(), pic_out.get_vbuf()) { | |
263 | let ioff = sbuf.get_offset(0); | |
264 | let paloff = sbuf.get_offset(1); | |
265 | let (w, h) = sbuf.get_dimensions(0); | |
266 | let istride = sbuf.get_stride(0); | |
267 | let sdata1 = sbuf.get_data(); | |
268 | let sdata = &sdata1[ioff..]; | |
269 | let pal = &sdata1[paloff..]; | |
270 | ||
271 | let mut ostride: [usize; MAX_CHROMATONS] = [0; MAX_CHROMATONS]; | |
272 | let mut offs: [usize; MAX_CHROMATONS] = [0; MAX_CHROMATONS]; | |
273 | for i in 0..self.ncomps { | |
274 | ostride[i] = dbuf.get_stride(i); | |
275 | offs[i] = dbuf.get_offset(i); | |
276 | } | |
277 | let dst = dbuf.get_data_mut().unwrap(); | |
278 | for src in sdata.chunks(istride).take(h) { | |
279 | for x in 0..w { | |
280 | let palidx = src[x] as usize; | |
281 | for i in 0..self.ncomps { | |
e243ceb4 | 282 | let elem = u32::from(pal[palidx * self.palstep + self.coffs[i]]); |
03accf76 KS |
283 | dst[offs[i] + x] = convert_depth(elem, self.depths[i], 8) as u8; |
284 | } | |
285 | } | |
286 | for i in 0..self.ncomps { offs[i] += ostride[i]; } | |
287 | } | |
288 | } else { | |
289 | unreachable!(); | |
290 | } | |
291 | } | |
292 | } | |
293 | ||
6011e201 | 294 | pub fn create_depal() -> Box<dyn Kernel> { |
03accf76 KS |
295 | Box::new(DepalKernel::new()) |
296 | } |