1
// Tencent is pleased to support the open source community by making ncnn available.
3
// Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
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
8
// https://opensource.org/licenses/BSD-3-Clause
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.
20
static struct prng_rand_t g_prng_rand_state;
21
#define SRAND(seed) prng_srand(seed, &g_prng_rand_state)
22
#define RAND() prng_rand(&g_prng_rand_state)
24
static ncnn::Mat RandomMat(int w, int h, int elempack)
26
ncnn::Mat m(w, h, 1, (size_t)elempack, elempack);
29
for (int i = 0; i < w * h * elempack; i++)
37
static bool NearlyEqual(float a, float b, float epsilon)
42
float diff = fabs(a - b);
47
return diff < epsilon * std::max(fabs(a), fabs(b));
50
static int Compare(const ncnn::Mat& a, const ncnn::Mat& b, float epsilon = 0.001)
52
#define CHECK_MEMBER(m) \
55
fprintf(stderr, #m " not match expect %d but got %d\n", (int)a.m, (int)b.m); \
63
CHECK_MEMBER(elemsize)
64
CHECK_MEMBER(elempack)
68
for (int q = 0; q < a.c; q++)
70
const ncnn::Mat ma = a.channel(q);
71
const ncnn::Mat mb = b.channel(q);
72
for (int i = 0; i < a.h; i++)
74
const float* pa = ma.row(i);
75
const float* pb = mb.row(i);
76
for (int j = 0; j < a.w; j++)
78
if (!NearlyEqual(pa[j], pb[j], epsilon))
80
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]);
90
static int test_mat_pixel_resize(int w, int h, int ch, int target_width, int target_height)
95
ncnn::Mat a = RandomMat(w, h, ch);
97
ncnn::Mat b(target_width, target_height, 1, (size_t)ch, ch);
99
if (ch == 1) resize_bilinear_c1(a, w, h, b, target_width, target_height);
100
if (ch == 2) resize_bilinear_c2(a, w, h, b, target_width, target_height);
101
if (ch == 3) resize_bilinear_c3(a, w, h, b, target_width, target_height);
102
if (ch == 4) resize_bilinear_c4(a, w, h, b, target_width, target_height);
105
ncnn::convert_packing(a, a2, 1, opt);
108
ncnn::convert_packing(b, b2, 1, opt);
110
for (int i = 0; i < ch; i++)
112
ncnn::Mat c = ncnn::Mat::from_pixels(a2.channel(i), ncnn::Mat::PIXEL_GRAY, w, h);
113
ncnn::Mat d = ncnn::Mat::from_pixels(b2.channel(i), ncnn::Mat::PIXEL_GRAY, target_width, target_height);
116
ncnn::resize_bilinear(c, e, target_width, target_height, opt);
118
if (Compare(e, d, 0.5) != 0)
120
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);
128
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)
133
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};
134
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
ncnn::Mat a = RandomMat(w, h, 1);
139
ncnn::convert_packing(a.reshape(w, h, 1), a2, 1, opt);
141
// FIXME enable more convert types
142
for (int i = 0; i < 1; i++)
144
ncnn::Mat m = ncnn::Mat::from_pixels_roi_resize(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih, target_width, target_height);
148
ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw), opt);
149
ncnn::convert_packing(b2, c2, 1, opt);
150
ncnn::Mat d2 = ncnn::Mat::from_pixels_resize(c2, pixel_type_from[i], c2.w, c2.h, target_width, target_height);
152
if (memcmp(m, d2, target_width * target_height * d2.c) != 0)
154
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);
162
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)
167
int pixel_type_from[4] = {ncnn::Mat::PIXEL_RGB, ncnn::Mat::PIXEL_RGB2BGR, ncnn::Mat::PIXEL_RGB2RGBA, ncnn::Mat::PIXEL_RGB2BGRA};
168
int pixel_type_to[4] = {ncnn::Mat::PIXEL_RGB, ncnn::Mat::PIXEL_BGR2RGB, ncnn::Mat::PIXEL_RGBA2RGB, ncnn::Mat::PIXEL_BGRA2RGB};
170
ncnn::Mat a = RandomMat(w, h, 3);
173
ncnn::convert_packing(a.reshape(w, h, 1), a2, 1, opt);
175
// FIXME enable more convert types
176
for (int i = 0; i < 2; i++)
178
ncnn::Mat m = ncnn::Mat::from_pixels_roi_resize(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih, target_width, target_height);
182
ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw), opt);
183
ncnn::convert_packing(b2, c2, 3, opt);
184
ncnn::Mat d2 = ncnn::Mat::from_pixels_resize(c2, pixel_type_from[i], c2.w, c2.h, target_width, target_height);
186
if (memcmp(m, d2, target_width * target_height * d2.c) != 0)
188
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);
196
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)
201
int pixel_type_from[4] = {ncnn::Mat::PIXEL_BGR, ncnn::Mat::PIXEL_BGR2RGB, ncnn::Mat::PIXEL_BGR2RGBA, ncnn::Mat::PIXEL_BGR2BGRA};
202
int pixel_type_to[4] = {ncnn::Mat::PIXEL_BGR, ncnn::Mat::PIXEL_RGB2BGR, ncnn::Mat::PIXEL_RGBA2BGR, ncnn::Mat::PIXEL_BGRA2BGR};
204
ncnn::Mat a = RandomMat(w, h, 3);
207
ncnn::convert_packing(a.reshape(w, h, 1), a2, 1, opt);
209
// FIXME enable more convert types
210
for (int i = 0; i < 2; i++)
212
ncnn::Mat m = ncnn::Mat::from_pixels_roi_resize(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih, target_width, target_height);
216
ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw), opt);
217
ncnn::convert_packing(b2, c2, 3, opt);
218
ncnn::Mat d2 = ncnn::Mat::from_pixels_resize(c2, pixel_type_from[i], c2.w, c2.h, target_width, target_height);
220
if (memcmp(m, d2, target_width * target_height * d2.c) != 0)
222
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);
230
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)
235
int pixel_type_from[2] = {ncnn::Mat::PIXEL_RGBA, ncnn::Mat::PIXEL_RGBA2BGRA};
236
int pixel_type_to[2] = {ncnn::Mat::PIXEL_RGBA, ncnn::Mat::PIXEL_BGRA2RGBA};
238
ncnn::Mat a = RandomMat(w, h, 4);
241
ncnn::convert_packing(a.reshape(w, h, 1), a2, 1, opt);
243
for (int i = 0; i < 2; i++)
245
ncnn::Mat m = ncnn::Mat::from_pixels_roi_resize(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih, target_width, target_height);
249
ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw), opt);
250
ncnn::convert_packing(b2, c2, 4, opt);
251
ncnn::Mat d2 = ncnn::Mat::from_pixels_resize(c2, pixel_type_from[i], c2.w, c2.h, target_width, target_height);
253
if (memcmp(m, d2, target_width * target_height * d2.c) != 0)
255
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);
263
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)
268
int pixel_type_from[2] = {ncnn::Mat::PIXEL_BGRA, ncnn::Mat::PIXEL_BGRA2RGBA};
269
int pixel_type_to[2] = {ncnn::Mat::PIXEL_BGRA, ncnn::Mat::PIXEL_RGBA2BGRA};
271
ncnn::Mat a = RandomMat(w, h, 4);
274
ncnn::convert_packing(a.reshape(w, h, 1), a2, 1, opt);
276
for (int i = 0; i < 2; i++)
278
ncnn::Mat m = ncnn::Mat::from_pixels_roi_resize(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih, target_width, target_height);
282
ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw), opt);
283
ncnn::convert_packing(b2, c2, 4, opt);
284
ncnn::Mat d2 = ncnn::Mat::from_pixels_resize(c2, pixel_type_from[i], c2.w, c2.h, target_width, target_height);
286
if (memcmp(m, d2, target_width * target_height * d2.c) != 0)
288
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);
296
static int test_mat_pixel_0()
298
for (int c = 1; c <= 4; c++)
301
|| test_mat_pixel_resize(24, 48, c, 24, 48)
302
|| test_mat_pixel_resize(13, 17, c, 11, 14)
303
|| test_mat_pixel_resize(33, 23, c, 5, 6)
304
|| test_mat_pixel_resize(5, 4, c, 11, 16)
305
|| test_mat_pixel_resize(23, 11, c, 15, 21);
314
static int test_mat_pixel_1()
317
|| test_mat_pixel_roi_resize_gray(16, 16, 1, 1, 13, 13, 10, 11)
318
|| test_mat_pixel_roi_resize_rgb(16, 16, 2, 1, 11, 11, 2, 3)
319
|| test_mat_pixel_roi_resize_bgr(16, 16, 1, 2, 11, 9, 22, 13)
320
|| test_mat_pixel_roi_resize_rgba(16, 16, 3, 2, 9, 11, 12, 4)
321
|| test_mat_pixel_roi_resize_bgra(16, 16, 2, 3, 9, 7, 7, 7);
324
static int test_mat_pixel_2()
327
|| test_mat_pixel_roi_resize_gray(15, 15, 2, 3, 2, 3, 2, 2)
328
|| test_mat_pixel_roi_resize_rgb(15, 15, 3, 4, 5, 4, 5, 4)
329
|| test_mat_pixel_roi_resize_bgr(15, 15, 4, 5, 6, 7, 4, 1)
330
|| test_mat_pixel_roi_resize_rgba(15, 15, 6, 6, 3, 4, 1, 3)
331
|| test_mat_pixel_roi_resize_bgra(15, 15, 7, 3, 1, 1, 1, 1);
338
return test_mat_pixel_0() || test_mat_pixel_1() || test_mat_pixel_2();