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
15 #include "mat.h"
16 #include "prng.h"
17
18 #include <math.h>
19 #include <string.h>
20
21 static struct prng_rand_t g_prng_rand_state;
22 #define SRAND(seed) prng_srand(seed, &g_prng_rand_state)
23 #define RAND() prng_rand(&g_prng_rand_state)
24
RandomMat(int w,int h,int elempack)25 static ncnn::Mat RandomMat(int w, int h, int elempack)
26 {
27 ncnn::Mat m(w, h, 1, (size_t)elempack, elempack);
28
29 unsigned char* p = m;
30 for (int i = 0; i < w * h * elempack; i++)
31 {
32 p[i] = RAND() % 256;
33 }
34
35 return m;
36 }
37
NearlyEqual(float a,float b,float epsilon)38 static bool NearlyEqual(float a, float b, float epsilon)
39 {
40 if (a == b)
41 return true;
42
43 float diff = fabs(a - b);
44 if (diff <= epsilon)
45 return true;
46
47 // relative error
48 return diff < epsilon * std::max(fabs(a), fabs(b));
49 }
50
Compare(const ncnn::Mat & a,const ncnn::Mat & b,float epsilon=0.001)51 static int Compare(const ncnn::Mat& a, const ncnn::Mat& b, float epsilon = 0.001)
52 {
53 #define CHECK_MEMBER(m) \
54 if (a.m != b.m) \
55 { \
56 fprintf(stderr, #m " not match expect %d but got %d\n", (int)a.m, (int)b.m); \
57 return -1; \
58 }
59
60 CHECK_MEMBER(dims)
61 CHECK_MEMBER(w)
62 CHECK_MEMBER(h)
63 CHECK_MEMBER(c)
64 CHECK_MEMBER(elemsize)
65 CHECK_MEMBER(elempack)
66
67 #undef CHECK_MEMBER
68
69 for (int q = 0; q < a.c; q++)
70 {
71 const ncnn::Mat ma = a.channel(q);
72 const ncnn::Mat mb = b.channel(q);
73 for (int i = 0; i < a.h; i++)
74 {
75 const float* pa = ma.row(i);
76 const float* pb = mb.row(i);
77 for (int j = 0; j < a.w; j++)
78 {
79 if (!NearlyEqual(pa[j], pb[j], epsilon))
80 {
81 fprintf(stderr, "value not match at c:%d h:%d w:%d expect %f but got %f\n", q, i, j, pa[j], pb[j]);
82 return -1;
83 }
84 }
85 }
86 }
87
88 return 0;
89 }
90
test_mat_pixel_resize(int w,int h,int ch,int target_width,int target_height)91 static int test_mat_pixel_resize(int w, int h, int ch, int target_width, int target_height)
92 {
93 ncnn::Mat a = RandomMat(w, h, ch);
94
95 ncnn::Mat b(target_width, target_height, 1, (size_t)ch, ch);
96
97 if (ch == 1) resize_bilinear_c1(a, w, h, b, target_width, target_height);
98 if (ch == 2) resize_bilinear_c2(a, w, h, b, target_width, target_height);
99 if (ch == 3) resize_bilinear_c3(a, w, h, b, target_width, target_height);
100 if (ch == 4) resize_bilinear_c4(a, w, h, b, target_width, target_height);
101
102 ncnn::Mat a2;
103 ncnn::convert_packing(a, a2, 1);
104
105 ncnn::Mat b2;
106 ncnn::convert_packing(b, b2, 1);
107
108 for (int i = 0; i < ch; i++)
109 {
110 ncnn::Mat c = ncnn::Mat::from_pixels(a2.channel(i), ncnn::Mat::PIXEL_GRAY, w, h);
111 ncnn::Mat d = ncnn::Mat::from_pixels(b2.channel(i), ncnn::Mat::PIXEL_GRAY, target_width, target_height);
112
113 ncnn::Mat e;
114 ncnn::resize_bilinear(c, e, target_width, target_height);
115
116 if (Compare(e, d, 0.5) != 0)
117 {
118 fprintf(stderr, "test_mat_pixel_resize failed w=%d h=%d ch=%d target_width=%d target_height=%d\n", w, h, ch, target_width, target_height);
119 return -1;
120 }
121 }
122
123 return 0;
124 }
125
test_mat_pixel_roi_resize_gray(int w,int h,int roix,int roiy,int roiw,int roih,int target_width,int target_height)126 static int test_mat_pixel_roi_resize_gray(int w, int h, int roix, int roiy, int roiw, int roih, int target_width, int target_height)
127 {
128 int pixel_type_from[5] = {ncnn::Mat::PIXEL_GRAY, ncnn::Mat::PIXEL_GRAY2RGB, ncnn::Mat::PIXEL_GRAY2BGR, ncnn::Mat::PIXEL_GRAY2RGBA, ncnn::Mat::PIXEL_GRAY2BGRA};
129 int pixel_type_to[5] = {ncnn::Mat::PIXEL_GRAY, ncnn::Mat::PIXEL_RGB2GRAY, ncnn::Mat::PIXEL_BGR2GRAY, ncnn::Mat::PIXEL_RGBA2GRAY, ncnn::Mat::PIXEL_BGRA2GRAY};
130
131 ncnn::Mat a = RandomMat(w, h, 1);
132
133 ncnn::Mat a2;
134 ncnn::convert_packing(a.reshape(w, h, 1), a2, 1);
135
136 // FIXME enable more convert types
137 for (int i = 0; i < 1; i++)
138 {
139 ncnn::Mat m = ncnn::Mat::from_pixels_roi_resize(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih, target_width, target_height);
140
141 ncnn::Mat b2;
142 ncnn::Mat c2;
143 ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw));
144 ncnn::convert_packing(b2, c2, 1);
145 ncnn::Mat d2 = ncnn::Mat::from_pixels_resize(c2, pixel_type_from[i], c2.w, c2.h, target_width, target_height);
146
147 if (memcmp(m, d2, target_width * target_height * d2.c) != 0)
148 {
149 fprintf(stderr, "test_mat_pixel_roi_resize_gray failed w=%d h=%d roi=[%d %d %d %d] target_width=%d target_height=%d pixel_type=%d\n", w, h, roix, roiy, roiw, roih, target_width, target_height, i);
150 return -1;
151 }
152 }
153
154 return 0;
155 }
156
test_mat_pixel_roi_resize_rgb(int w,int h,int roix,int roiy,int roiw,int roih,int target_width,int target_height)157 static int test_mat_pixel_roi_resize_rgb(int w, int h, int roix, int roiy, int roiw, int roih, int target_width, int target_height)
158 {
159 int pixel_type_from[4] = {ncnn::Mat::PIXEL_RGB, ncnn::Mat::PIXEL_RGB2BGR, ncnn::Mat::PIXEL_RGB2RGBA, ncnn::Mat::PIXEL_RGB2BGRA};
160 int pixel_type_to[4] = {ncnn::Mat::PIXEL_RGB, ncnn::Mat::PIXEL_BGR2RGB, ncnn::Mat::PIXEL_RGBA2RGB, ncnn::Mat::PIXEL_BGRA2RGB};
161
162 ncnn::Mat a = RandomMat(w, h, 3);
163
164 ncnn::Mat a2;
165 ncnn::convert_packing(a.reshape(w, h, 1), a2, 1);
166
167 // FIXME enable more convert types
168 for (int i = 0; i < 2; i++)
169 {
170 ncnn::Mat m = ncnn::Mat::from_pixels_roi_resize(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih, target_width, target_height);
171
172 ncnn::Mat b2;
173 ncnn::Mat c2;
174 ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw));
175 ncnn::convert_packing(b2, c2, 3);
176 ncnn::Mat d2 = ncnn::Mat::from_pixels_resize(c2, pixel_type_from[i], c2.w, c2.h, target_width, target_height);
177
178 if (memcmp(m, d2, target_width * target_height * d2.c) != 0)
179 {
180 fprintf(stderr, "test_mat_pixel_roi_resize_rgb failed w=%d h=%d roi=[%d %d %d %d] target_width=%d target_height=%d pixel_type=%d\n", w, h, roix, roiy, roiw, roih, target_width, target_height, i);
181 return -1;
182 }
183 }
184
185 return 0;
186 }
187
test_mat_pixel_roi_resize_bgr(int w,int h,int roix,int roiy,int roiw,int roih,int target_width,int target_height)188 static int test_mat_pixel_roi_resize_bgr(int w, int h, int roix, int roiy, int roiw, int roih, int target_width, int target_height)
189 {
190 int pixel_type_from[4] = {ncnn::Mat::PIXEL_BGR, ncnn::Mat::PIXEL_BGR2RGB, ncnn::Mat::PIXEL_BGR2RGBA, ncnn::Mat::PIXEL_BGR2BGRA};
191 int pixel_type_to[4] = {ncnn::Mat::PIXEL_BGR, ncnn::Mat::PIXEL_RGB2BGR, ncnn::Mat::PIXEL_RGBA2BGR, ncnn::Mat::PIXEL_BGRA2BGR};
192
193 ncnn::Mat a = RandomMat(w, h, 3);
194
195 ncnn::Mat a2;
196 ncnn::convert_packing(a.reshape(w, h, 1), a2, 1);
197
198 // FIXME enable more convert types
199 for (int i = 0; i < 2; i++)
200 {
201 ncnn::Mat m = ncnn::Mat::from_pixels_roi_resize(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih, target_width, target_height);
202
203 ncnn::Mat b2;
204 ncnn::Mat c2;
205 ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw));
206 ncnn::convert_packing(b2, c2, 3);
207 ncnn::Mat d2 = ncnn::Mat::from_pixels_resize(c2, pixel_type_from[i], c2.w, c2.h, target_width, target_height);
208
209 if (memcmp(m, d2, target_width * target_height * d2.c) != 0)
210 {
211 fprintf(stderr, "test_mat_pixel_roi_resize_bgr failed w=%d h=%d roi=[%d %d %d %d] target_width=%d target_height=%d pixel_type=%d\n", w, h, roix, roiy, roiw, roih, target_width, target_height, i);
212 return -1;
213 }
214 }
215
216 return 0;
217 }
218
test_mat_pixel_roi_resize_rgba(int w,int h,int roix,int roiy,int roiw,int roih,int target_width,int target_height)219 static int test_mat_pixel_roi_resize_rgba(int w, int h, int roix, int roiy, int roiw, int roih, int target_width, int target_height)
220 {
221 int pixel_type_from[2] = {ncnn::Mat::PIXEL_RGBA, ncnn::Mat::PIXEL_RGBA2BGRA};
222 int pixel_type_to[2] = {ncnn::Mat::PIXEL_RGBA, ncnn::Mat::PIXEL_BGRA2RGBA};
223
224 ncnn::Mat a = RandomMat(w, h, 4);
225
226 ncnn::Mat a2;
227 ncnn::convert_packing(a.reshape(w, h, 1), a2, 1);
228
229 for (int i = 0; i < 2; i++)
230 {
231 ncnn::Mat m = ncnn::Mat::from_pixels_roi_resize(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih, target_width, target_height);
232
233 ncnn::Mat b2;
234 ncnn::Mat c2;
235 ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw));
236 ncnn::convert_packing(b2, c2, 4);
237 ncnn::Mat d2 = ncnn::Mat::from_pixels_resize(c2, pixel_type_from[i], c2.w, c2.h, target_width, target_height);
238
239 if (memcmp(m, d2, target_width * target_height * d2.c) != 0)
240 {
241 fprintf(stderr, "test_mat_pixel_roi_resize_rgba failed w=%d h=%d roi=[%d %d %d %d] target_width=%d target_height=%d pixel_type=%d\n", w, h, roix, roiy, roiw, roih, target_width, target_height, i);
242 return -1;
243 }
244 }
245
246 return 0;
247 }
248
test_mat_pixel_roi_resize_bgra(int w,int h,int roix,int roiy,int roiw,int roih,int target_width,int target_height)249 static int test_mat_pixel_roi_resize_bgra(int w, int h, int roix, int roiy, int roiw, int roih, int target_width, int target_height)
250 {
251 int pixel_type_from[2] = {ncnn::Mat::PIXEL_BGRA, ncnn::Mat::PIXEL_BGRA2RGBA};
252 int pixel_type_to[2] = {ncnn::Mat::PIXEL_BGRA, ncnn::Mat::PIXEL_RGBA2BGRA};
253
254 ncnn::Mat a = RandomMat(w, h, 4);
255
256 ncnn::Mat a2;
257 ncnn::convert_packing(a.reshape(w, h, 1), a2, 1);
258
259 for (int i = 0; i < 2; i++)
260 {
261 ncnn::Mat m = ncnn::Mat::from_pixels_roi_resize(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih, target_width, target_height);
262
263 ncnn::Mat b2;
264 ncnn::Mat c2;
265 ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw));
266 ncnn::convert_packing(b2, c2, 4);
267 ncnn::Mat d2 = ncnn::Mat::from_pixels_resize(c2, pixel_type_from[i], c2.w, c2.h, target_width, target_height);
268
269 if (memcmp(m, d2, target_width * target_height * d2.c) != 0)
270 {
271 fprintf(stderr, "test_mat_pixel_roi_resize_bgra failed w=%d h=%d roi=[%d %d %d %d] target_width=%d target_height=%d pixel_type=%d\n", w, h, roix, roiy, roiw, roih, target_width, target_height, i);
272 return -1;
273 }
274 }
275
276 return 0;
277 }
278
test_mat_pixel_0()279 static int test_mat_pixel_0()
280 {
281 for (int c = 1; c <= 4; c++)
282 {
283 int ret = 0
284 || test_mat_pixel_resize(24, 48, c, 24, 48)
285 || test_mat_pixel_resize(13, 17, c, 11, 14)
286 || test_mat_pixel_resize(33, 23, c, 5, 6)
287 || test_mat_pixel_resize(5, 4, c, 11, 16)
288 || test_mat_pixel_resize(23, 11, c, 15, 21);
289
290 if (ret != 0)
291 return ret;
292 }
293
294 return 0;
295 }
296
test_mat_pixel_1()297 static int test_mat_pixel_1()
298 {
299 return 0
300 || test_mat_pixel_roi_resize_gray(16, 16, 1, 1, 13, 13, 10, 11)
301 || test_mat_pixel_roi_resize_rgb(16, 16, 2, 1, 11, 11, 2, 3)
302 || test_mat_pixel_roi_resize_bgr(16, 16, 1, 2, 11, 9, 22, 13)
303 || test_mat_pixel_roi_resize_rgba(16, 16, 3, 2, 9, 11, 12, 4)
304 || test_mat_pixel_roi_resize_bgra(16, 16, 2, 3, 9, 7, 7, 7);
305 }
306
test_mat_pixel_2()307 static int test_mat_pixel_2()
308 {
309 return 0
310 || test_mat_pixel_roi_resize_gray(15, 15, 2, 3, 2, 3, 2, 2)
311 || test_mat_pixel_roi_resize_rgb(15, 15, 3, 4, 5, 4, 5, 4)
312 || test_mat_pixel_roi_resize_bgr(15, 15, 4, 5, 6, 7, 4, 1)
313 || test_mat_pixel_roi_resize_rgba(15, 15, 6, 6, 3, 4, 1, 3)
314 || test_mat_pixel_roi_resize_bgra(15, 15, 7, 3, 1, 1, 1, 1);
315 }
316
main()317 int main()
318 {
319 SRAND(7767517);
320
321 return test_mat_pixel_0() || test_mat_pixel_1() || test_mat_pixel_2();
322 }
323