/rust/registry/src/index.crates.io-1949cf8c6b5b557f/zune-jpeg-0.4.21/src/upsampler/scalar.rs
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2023. |
3 | | * |
4 | | * This software is free software; |
5 | | * |
6 | | * You can redistribute it or modify it under terms of the MIT, Apache License or Zlib license |
7 | | */ |
8 | | |
9 | 73.6k | pub fn upsample_horizontal( |
10 | 73.6k | input: &[i16], _ref: &[i16], _in_near: &[i16], _scratch: &mut [i16], output: &mut [i16] |
11 | 73.6k | ) { |
12 | 73.6k | assert_eq!( |
13 | 73.6k | input.len() * 2, |
14 | 73.6k | output.len(), |
15 | 0 | "Input length is not half the size of the output length" |
16 | | ); |
17 | 73.6k | assert!( |
18 | 73.6k | output.len() > 4 && input.len() > 2, |
19 | 0 | "Too Short of a vector, cannot upsample" |
20 | | ); |
21 | | |
22 | 73.6k | output[0] = input[0]; |
23 | 73.6k | output[1] = (input[0] * 3 + input[1] + 2) >> 2; |
24 | | |
25 | | // This code is written for speed and not readability |
26 | | // |
27 | | // The readable code is |
28 | | // |
29 | | // for i in 1..input.len() - 1{ |
30 | | // let sample = 3 * input[i] + 2; |
31 | | // out[i * 2] = (sample + input[i - 1]) >> 2; |
32 | | // out[i * 2 + 1] = (sample + input[i + 1]) >> 2; |
33 | | // } |
34 | | // |
35 | | // The output of a pixel is determined by it's surrounding neighbours but we attach more weight to it's nearest |
36 | | // neighbour (input[i]) than to the next nearest neighbour. |
37 | | |
38 | 4.53M | for (output_window, input_window) in output[2..].chunks_exact_mut(2).zip(input.windows(3)) { |
39 | 4.53M | let sample = 3 * input_window[1] + 2; |
40 | 4.53M | |
41 | 4.53M | output_window[0] = (sample + input_window[0]) >> 2; |
42 | 4.53M | output_window[1] = (sample + input_window[2]) >> 2; |
43 | 4.53M | } |
44 | | // Get lengths |
45 | 73.6k | let out_len = output.len() - 2; |
46 | 73.6k | let input_len = input.len() - 2; |
47 | | |
48 | | // slice the output vector |
49 | 73.6k | let f_out = &mut output[out_len..]; |
50 | 73.6k | let i_last = &input[input_len..]; |
51 | | |
52 | | // write out manually.. |
53 | 73.6k | f_out[0] = (3 * i_last[0] + i_last[1] + 2) >> 2; |
54 | 73.6k | f_out[1] = i_last[1]; |
55 | 73.6k | } |
56 | 142k | pub fn upsample_vertical( |
57 | 142k | input: &[i16], in_near: &[i16], in_far: &[i16], _scratch_space: &mut [i16], output: &mut [i16] |
58 | 142k | ) { |
59 | 142k | assert_eq!(input.len() * 2, output.len()); |
60 | 142k | assert_eq!(in_near.len(), input.len()); |
61 | 142k | assert_eq!(in_far.len(), input.len()); |
62 | | |
63 | 142k | let middle = output.len() / 2; |
64 | | |
65 | 142k | let (out_top, out_bottom) = output.split_at_mut(middle); |
66 | | |
67 | | // for the first row, closest row is in_near |
68 | 39.6M | for ((near, far), x) in input.iter().zip(in_near.iter()).zip(out_top) { |
69 | 39.6M | *x = (((3 * near) + 2) + far) >> 2; |
70 | 39.6M | } |
71 | | // for the second row, the closest row to input is in_far |
72 | 39.6M | for ((near, far), x) in input.iter().zip(in_far.iter()).zip(out_bottom) { |
73 | 39.6M | *x = (((3 * near) + 2) + far) >> 2; |
74 | 39.6M | } |
75 | 142k | } |
76 | | |
77 | 0 | pub fn upsample_hv( |
78 | 0 | input: &[i16], in_near: &[i16], in_far: &[i16], scratch_space: &mut [i16], output: &mut [i16] |
79 | 0 | ) { |
80 | 0 | assert_eq!(input.len() * 4, output.len()); |
81 | | |
82 | 0 | let mut t = [0]; |
83 | 0 | upsample_vertical(input, in_near, in_far, &mut t, scratch_space); |
84 | | // horizontal upsampling must be done separate for every line |
85 | | // Otherwise it introduces artifacts that may cause the edge colors |
86 | | // to appear on the other line. |
87 | | |
88 | | // Since this is called for two scanlines/widths currently |
89 | | // splitting the inputs and outputs into half ensures we only handle |
90 | | // one scanline per iteration |
91 | 0 | let scratch_half = scratch_space.len() / 2; |
92 | | |
93 | 0 | let output_half = output.len() / 2; |
94 | | |
95 | 0 | upsample_horizontal( |
96 | 0 | &scratch_space[..scratch_half], |
97 | 0 | &[], |
98 | 0 | &[], |
99 | 0 | &mut t, |
100 | 0 | &mut output[..output_half] |
101 | | ); |
102 | | |
103 | 0 | upsample_horizontal( |
104 | 0 | &scratch_space[scratch_half..], |
105 | 0 | &[], |
106 | 0 | &[], |
107 | 0 | &mut t, |
108 | 0 | &mut output[output_half..] |
109 | | ); |
110 | 0 | } |
111 | | |
112 | 709k | pub fn upsample_generic( |
113 | 709k | input: &[i16], _in_near: &[i16], _in_far: &[i16], _scratch_space: &mut [i16], |
114 | 709k | output: &mut [i16] |
115 | 709k | ) { |
116 | | // use nearest sample |
117 | 709k | let difference = output.len() / input.len(); |
118 | 709k | if difference > 0 { |
119 | | // nearest neighbour |
120 | 109M | for (input, chunk_output) in input.iter().zip(output.chunks_exact_mut(difference)) { |
121 | 460M | chunk_output.iter_mut().for_each(|x| *x = *input); |
122 | | } |
123 | 0 | } |
124 | 709k | } |