Elffuss BraidHub

kikocis/ncnn public

master · 4.4 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
4//
5// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
6// in compliance with the License. You may obtain a copy of the License at
7//
8// https://opensource.org/licenses/BSD-3-Clause
9//
10// Unless required by applicable law or agreed to in writing, software distributed
11// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12// CONDITIONS OF ANY KIND, either express or implied. See the License for the
13// specific language governing permissions and limitations under the License.
14
15static void linear_coeffs(int w, int outw, int* xofs, float* alpha, int align_corner)
16{
17 double scale = (double)w / outw;
18 if (align_corner)
19 {
20 scale = (double)(w - 1) / (outw - 1);
21 }
22
23 for (int dx = 0; dx < outw; dx++)
24 {
25 float fx = (float)((dx + 0.5) * scale - 0.5);
26 if (align_corner)
27 {
28 fx = (float)(dx * scale);
29 }
30
31 int sx = floor(fx);
32 fx -= sx;
33
34 if (sx < 0)
35 {
36 sx = 0;
37 fx = 0.f;
38 }
39 if (sx >= w - 1)
40 {
41 sx = w - 2;
42 fx = 1.f;
43 }
44
45 xofs[dx] = sx;
46
47 alpha[dx * 2] = 1.f - fx;
48 alpha[dx * 2 + 1] = fx;
49 }
50}
51
52static void resize_bilinear_image(const Mat& src, Mat& dst, float* alpha, int* xofs, float* beta, int* yofs)
53{
54 int w = dst.w;
55 int h = dst.h;
56
57 // loop body
58 Mat rowsbuf0(w);
59 Mat rowsbuf1(w);
60 float* rows0 = rowsbuf0;
61 float* rows1 = rowsbuf1;
62
63 int prev_sy1 = -2;
64
65 for (int dy = 0; dy < h; dy++)
66 {
67 int sy = yofs[dy];
68
69 if (sy == prev_sy1)
70 {
71 // reuse all rows
72 }
73 else if (sy == prev_sy1 + 1)
74 {
75 // hresize one row
76 float* rows0_old = rows0;
77 rows0 = rows1;
78 rows1 = rows0_old;
79 const float* S1 = src.row(sy + 1);
80
81 const float* alphap = alpha;
82 float* rows1p = rows1;
83 int dx = 0;
84 for (; dx < w; dx++)
85 {
86 int sx = xofs[dx];
87 const float* S1p = S1 + sx;
88
89 float a0 = alphap[0];
90 float a1 = alphap[1];
91 rows1p[dx] = S1p[0] * a0 + S1p[1] * a1;
92
93 alphap += 2;
94 }
95 }
96 else
97 {
98 // hresize two rows
99 const float* S0 = src.row(sy);
100 const float* S1 = src.row(sy + 1);
101
102 const float* alphap = alpha;
103 float* rows0p = rows0;
104 float* rows1p = rows1;
105 int dx = 0;
106 for (; dx < w; dx++)
107 {
108 int sx = xofs[dx];
109 const float* S0p = S0 + sx;
110 const float* S1p = S1 + sx;
111
112 float a0 = alphap[0];
113 float a1 = alphap[1];
114 rows0p[dx] = S0p[0] * a0 + S0p[1] * a1;
115 rows1p[dx] = S1p[0] * a0 + S1p[1] * a1;
116
117 alphap += 2;
118 }
119 }
120
121 prev_sy1 = sy;
122
123 // vresize
124 float b0 = beta[0];
125 float b1 = beta[1];
126
127 float* rows0p = rows0;
128 float* rows1p = rows1;
129 float* Dp = dst.row(dy);
130
131#if __mips_msa
132 int nn = w >> 3;
133#else
134 int nn = 0;
135#endif
136 int remain = w - (nn << 3);
137
138#if __mips_msa
139 v4f32 _b0 = __msa_fill_w_f32(b0);
140 v4f32 _b1 = __msa_fill_w_f32(b1);
141 for (; nn > 0; nn--)
142 {
143 v4f32 _rows0 = (v4f32)__msa_ld_w(rows0p, 0);
144 v4f32 _rows1 = (v4f32)__msa_ld_w(rows1p, 0);
145
146 v4f32 _Dp = __msa_fmul_w(_rows0, _b0);
147 _Dp = __msa_fmadd_w(_Dp, _rows1, _b1);
148
149 __msa_st_w((v4i32)_Dp, Dp, 0);
150
151 v4f32 _rows0n = (v4f32)__msa_ld_w(rows0p + 4, 0);
152 v4f32 _rows1n = (v4f32)__msa_ld_w(rows1p + 4, 0);
153
154 v4f32 _Dpn = __msa_fmul_w(_rows0n, _b0);
155 _Dpn = __msa_fmadd_w(_Dpn, _rows1n, _b1);
156
157 __msa_st_w((v4i32)_Dpn, Dp + 4, 0);
158
159 Dp += 8;
160 rows0p += 8;
161 rows1p += 8;
162 }
163#endif // __mips_msa
164 for (; remain; --remain)
165 {
166 // D[x] = rows0[x]*b0 + rows1[x]*b1;
167 *Dp++ = *rows0p++ * b0 + *rows1p++ * b1;
168 }
169
170 beta += 2;
171 }
172}