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::Option opt;
94     opt.num_threads = 1;
95 
96     ncnn::Mat a = RandomMat(w, h, ch);
97 
98     ncnn::Mat b(target_width, target_height, 1, (size_t)ch, ch);
99 
100     if (ch == 1) resize_bilinear_c1(a, w, h, b, target_width, target_height);
101     if (ch == 2) resize_bilinear_c2(a, w, h, b, target_width, target_height);
102     if (ch == 3) resize_bilinear_c3(a, w, h, b, target_width, target_height);
103     if (ch == 4) resize_bilinear_c4(a, w, h, b, target_width, target_height);
104 
105     ncnn::Mat a2;
106     ncnn::convert_packing(a, a2, 1, opt);
107 
108     ncnn::Mat b2;
109     ncnn::convert_packing(b, b2, 1, opt);
110 
111     for (int i = 0; i < ch; i++)
112     {
113         ncnn::Mat c = ncnn::Mat::from_pixels(a2.channel(i), ncnn::Mat::PIXEL_GRAY, w, h);
114         ncnn::Mat d = ncnn::Mat::from_pixels(b2.channel(i), ncnn::Mat::PIXEL_GRAY, target_width, target_height);
115 
116         ncnn::Mat e;
117         ncnn::resize_bilinear(c, e, target_width, target_height, opt);
118 
119         if (Compare(e, d, 0.5) != 0)
120         {
121             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);
122             return -1;
123         }
124     }
125 
126     return 0;
127 }
128 
test_mat_pixel_roi_resize_gray(int w,int h,int roix,int roiy,int roiw,int roih,int target_width,int target_height)129 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)
130 {
131     ncnn::Option opt;
132     opt.num_threads = 1;
133 
134     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};
135     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};
136 
137     ncnn::Mat a = RandomMat(w, h, 1);
138 
139     ncnn::Mat a2;
140     ncnn::convert_packing(a.reshape(w, h, 1), a2, 1, opt);
141 
142     // FIXME enable more convert types
143     for (int i = 0; i < 1; i++)
144     {
145         ncnn::Mat m = ncnn::Mat::from_pixels_roi_resize(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih, target_width, target_height);
146 
147         ncnn::Mat b2;
148         ncnn::Mat c2;
149         ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw), opt);
150         ncnn::convert_packing(b2, c2, 1, opt);
151         ncnn::Mat d2 = ncnn::Mat::from_pixels_resize(c2, pixel_type_from[i], c2.w, c2.h, target_width, target_height);
152 
153         if (memcmp(m, d2, target_width * target_height * d2.c) != 0)
154         {
155             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);
156             return -1;
157         }
158     }
159 
160     return 0;
161 }
162 
test_mat_pixel_roi_resize_rgb(int w,int h,int roix,int roiy,int roiw,int roih,int target_width,int target_height)163 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)
164 {
165     ncnn::Option opt;
166     opt.num_threads = 1;
167 
168     int pixel_type_from[4] = {ncnn::Mat::PIXEL_RGB, ncnn::Mat::PIXEL_RGB2BGR, ncnn::Mat::PIXEL_RGB2RGBA, ncnn::Mat::PIXEL_RGB2BGRA};
169     int pixel_type_to[4] = {ncnn::Mat::PIXEL_RGB, ncnn::Mat::PIXEL_BGR2RGB, ncnn::Mat::PIXEL_RGBA2RGB, ncnn::Mat::PIXEL_BGRA2RGB};
170 
171     ncnn::Mat a = RandomMat(w, h, 3);
172 
173     ncnn::Mat a2;
174     ncnn::convert_packing(a.reshape(w, h, 1), a2, 1, opt);
175 
176     // FIXME enable more convert types
177     for (int i = 0; i < 2; i++)
178     {
179         ncnn::Mat m = ncnn::Mat::from_pixels_roi_resize(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih, target_width, target_height);
180 
181         ncnn::Mat b2;
182         ncnn::Mat c2;
183         ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw), opt);
184         ncnn::convert_packing(b2, c2, 3, opt);
185         ncnn::Mat d2 = ncnn::Mat::from_pixels_resize(c2, pixel_type_from[i], c2.w, c2.h, target_width, target_height);
186 
187         if (memcmp(m, d2, target_width * target_height * d2.c) != 0)
188         {
189             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);
190             return -1;
191         }
192     }
193 
194     return 0;
195 }
196 
test_mat_pixel_roi_resize_bgr(int w,int h,int roix,int roiy,int roiw,int roih,int target_width,int target_height)197 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)
198 {
199     ncnn::Option opt;
200     opt.num_threads = 1;
201 
202     int pixel_type_from[4] = {ncnn::Mat::PIXEL_BGR, ncnn::Mat::PIXEL_BGR2RGB, ncnn::Mat::PIXEL_BGR2RGBA, ncnn::Mat::PIXEL_BGR2BGRA};
203     int pixel_type_to[4] = {ncnn::Mat::PIXEL_BGR, ncnn::Mat::PIXEL_RGB2BGR, ncnn::Mat::PIXEL_RGBA2BGR, ncnn::Mat::PIXEL_BGRA2BGR};
204 
205     ncnn::Mat a = RandomMat(w, h, 3);
206 
207     ncnn::Mat a2;
208     ncnn::convert_packing(a.reshape(w, h, 1), a2, 1, opt);
209 
210     // FIXME enable more convert types
211     for (int i = 0; i < 2; i++)
212     {
213         ncnn::Mat m = ncnn::Mat::from_pixels_roi_resize(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih, target_width, target_height);
214 
215         ncnn::Mat b2;
216         ncnn::Mat c2;
217         ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw), opt);
218         ncnn::convert_packing(b2, c2, 3, opt);
219         ncnn::Mat d2 = ncnn::Mat::from_pixels_resize(c2, pixel_type_from[i], c2.w, c2.h, target_width, target_height);
220 
221         if (memcmp(m, d2, target_width * target_height * d2.c) != 0)
222         {
223             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);
224             return -1;
225         }
226     }
227 
228     return 0;
229 }
230 
test_mat_pixel_roi_resize_rgba(int w,int h,int roix,int roiy,int roiw,int roih,int target_width,int target_height)231 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)
232 {
233     ncnn::Option opt;
234     opt.num_threads = 1;
235 
236     int pixel_type_from[2] = {ncnn::Mat::PIXEL_RGBA, ncnn::Mat::PIXEL_RGBA2BGRA};
237     int pixel_type_to[2] = {ncnn::Mat::PIXEL_RGBA, ncnn::Mat::PIXEL_BGRA2RGBA};
238 
239     ncnn::Mat a = RandomMat(w, h, 4);
240 
241     ncnn::Mat a2;
242     ncnn::convert_packing(a.reshape(w, h, 1), a2, 1, opt);
243 
244     for (int i = 0; i < 2; i++)
245     {
246         ncnn::Mat m = ncnn::Mat::from_pixels_roi_resize(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih, target_width, target_height);
247 
248         ncnn::Mat b2;
249         ncnn::Mat c2;
250         ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw), opt);
251         ncnn::convert_packing(b2, c2, 4, opt);
252         ncnn::Mat d2 = ncnn::Mat::from_pixels_resize(c2, pixel_type_from[i], c2.w, c2.h, target_width, target_height);
253 
254         if (memcmp(m, d2, target_width * target_height * d2.c) != 0)
255         {
256             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);
257             return -1;
258         }
259     }
260 
261     return 0;
262 }
263 
test_mat_pixel_roi_resize_bgra(int w,int h,int roix,int roiy,int roiw,int roih,int target_width,int target_height)264 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)
265 {
266     ncnn::Option opt;
267     opt.num_threads = 1;
268 
269     int pixel_type_from[2] = {ncnn::Mat::PIXEL_BGRA, ncnn::Mat::PIXEL_BGRA2RGBA};
270     int pixel_type_to[2] = {ncnn::Mat::PIXEL_BGRA, ncnn::Mat::PIXEL_RGBA2BGRA};
271 
272     ncnn::Mat a = RandomMat(w, h, 4);
273 
274     ncnn::Mat a2;
275     ncnn::convert_packing(a.reshape(w, h, 1), a2, 1, opt);
276 
277     for (int i = 0; i < 2; i++)
278     {
279         ncnn::Mat m = ncnn::Mat::from_pixels_roi_resize(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih, target_width, target_height);
280 
281         ncnn::Mat b2;
282         ncnn::Mat c2;
283         ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw), opt);
284         ncnn::convert_packing(b2, c2, 4, opt);
285         ncnn::Mat d2 = ncnn::Mat::from_pixels_resize(c2, pixel_type_from[i], c2.w, c2.h, target_width, target_height);
286 
287         if (memcmp(m, d2, target_width * target_height * d2.c) != 0)
288         {
289             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);
290             return -1;
291         }
292     }
293 
294     return 0;
295 }
296 
test_mat_pixel_0()297 static int test_mat_pixel_0()
298 {
299     for (int c = 1; c <= 4; c++)
300     {
301         int ret = 0
302                   || test_mat_pixel_resize(24, 48, c, 24, 48)
303                   || test_mat_pixel_resize(13, 17, c, 11, 14)
304                   || test_mat_pixel_resize(33, 23, c, 5, 6)
305                   || test_mat_pixel_resize(5, 4, c, 11, 16)
306                   || test_mat_pixel_resize(23, 11, c, 15, 21);
307 
308         if (ret != 0)
309             return ret;
310     }
311 
312     return 0;
313 }
314 
test_mat_pixel_1()315 static int test_mat_pixel_1()
316 {
317     return 0
318            || test_mat_pixel_roi_resize_gray(16, 16, 1, 1, 13, 13, 10, 11)
319            || test_mat_pixel_roi_resize_rgb(16, 16, 2, 1, 11, 11, 2, 3)
320            || test_mat_pixel_roi_resize_bgr(16, 16, 1, 2, 11, 9, 22, 13)
321            || test_mat_pixel_roi_resize_rgba(16, 16, 3, 2, 9, 11, 12, 4)
322            || test_mat_pixel_roi_resize_bgra(16, 16, 2, 3, 9, 7, 7, 7);
323 }
324 
test_mat_pixel_2()325 static int test_mat_pixel_2()
326 {
327     return 0
328            || test_mat_pixel_roi_resize_gray(15, 15, 2, 3, 2, 3, 2, 2)
329            || test_mat_pixel_roi_resize_rgb(15, 15, 3, 4, 5, 4, 5, 4)
330            || test_mat_pixel_roi_resize_bgr(15, 15, 4, 5, 6, 7, 4, 1)
331            || test_mat_pixel_roi_resize_rgba(15, 15, 6, 6, 3, 4, 1, 3)
332            || test_mat_pixel_roi_resize_bgra(15, 15, 7, 3, 1, 1, 1, 1);
333 }
334 
main()335 int main()
336 {
337     SRAND(7767517);
338 
339     return test_mat_pixel_0() || test_mat_pixel_1() || test_mat_pixel_2();
340 }
341