| 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 <string.h> |
| 19 | |
| 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) |
| 23 | |
| 24 | static ncnn::Mat RandomMat(int w, int h, int elempack) |
| 25 | { |
| 26 | ncnn::Mat m(w, h, 1, (size_t)elempack, elempack); |
| 27 | |
| 28 | unsigned char* p = m; |
| 29 | for (int i = 0; i < w * h * elempack; i++) |
| 30 | { |
| 31 | p[i] = RAND() % 256; |
| 32 | } |
| 33 | |
| 34 | return m; |
| 35 | } |
| 36 | |
| 37 | static bool NearlyEqual(float a, float b, float epsilon) |
| 38 | { |
| 39 | if (a == b) |
| 40 | return true; |
| 41 | |
| 42 | float diff = fabs(a - b); |
| 43 | if (diff <= epsilon) |
| 44 | return true; |
| 45 | |
| 46 | // relative error |
| 47 | return diff < epsilon * std::max(fabs(a), fabs(b)); |
| 48 | } |
| 49 | |
| 50 | static int Compare(const ncnn::Mat& a, const ncnn::Mat& b, float epsilon = 0.001) |
| 51 | { |
| 52 | #define CHECK_MEMBER(m) \ |
| 53 | if (a.m != b.m) \ |
| 54 | { \ |
| 55 | fprintf(stderr, #m " not match expect %d but got %d\n", (int)a.m, (int)b.m); \ |
| 56 | return -1; \ |
| 57 | } |
| 58 | |
| 59 | CHECK_MEMBER(dims) |
| 60 | CHECK_MEMBER(w) |
| 61 | CHECK_MEMBER(h) |
| 62 | CHECK_MEMBER(c) |
| 63 | CHECK_MEMBER(elemsize) |
| 64 | CHECK_MEMBER(elempack) |
| 65 | |
| 66 | #undef CHECK_MEMBER |
| 67 | |
| 68 | for (int q = 0; q < a.c; q++) |
| 69 | { |
| 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++) |
| 73 | { |
| 74 | const float* pa = ma.row(i); |
| 75 | const float* pb = mb.row(i); |
| 76 | for (int j = 0; j < a.w; j++) |
| 77 | { |
| 78 | if (!NearlyEqual(pa[j], pb[j], epsilon)) |
| 79 | { |
| 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]); |
| 81 | return -1; |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | static int test_mat_pixel_resize(int w, int h, int ch, int target_width, int target_height) |
| 91 | { |
| 92 | ncnn::Option opt; |
| 93 | opt.num_threads = 1; |
| 94 | |
| 95 | ncnn::Mat a = RandomMat(w, h, ch); |
| 96 | |
| 97 | ncnn::Mat b(target_width, target_height, 1, (size_t)ch, ch); |
| 98 | |
| 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); |
| 103 | |
| 104 | ncnn::Mat a2; |
| 105 | ncnn::convert_packing(a, a2, 1, opt); |
| 106 | |
| 107 | ncnn::Mat b2; |
| 108 | ncnn::convert_packing(b, b2, 1, opt); |
| 109 | |
| 110 | for (int i = 0; i < ch; i++) |
| 111 | { |
| 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); |
| 114 | |
| 115 | ncnn::Mat e; |
| 116 | ncnn::resize_bilinear(c, e, target_width, target_height, opt); |
| 117 | |
| 118 | if (Compare(e, d, 0.5) != 0) |
| 119 | { |
| 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); |
| 121 | return -1; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 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) |
| 129 | { |
| 130 | ncnn::Option opt; |
| 131 | opt.num_threads = 1; |
| 132 | |
| 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}; |
| 135 | |
| 136 | ncnn::Mat a = RandomMat(w, h, 1); |
| 137 | |
| 138 | ncnn::Mat a2; |
| 139 | ncnn::convert_packing(a.reshape(w, h, 1), a2, 1, opt); |
| 140 | |
| 141 | // FIXME enable more convert types |
| 142 | for (int i = 0; i < 1; i++) |
| 143 | { |
| 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); |
| 145 | |
| 146 | ncnn::Mat b2; |
| 147 | ncnn::Mat c2; |
| 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); |
| 151 | |
| 152 | if (memcmp(m, d2, target_width * target_height * d2.c) != 0) |
| 153 | { |
| 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); |
| 155 | return -1; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 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) |
| 163 | { |
| 164 | ncnn::Option opt; |
| 165 | opt.num_threads = 1; |
| 166 | |
| 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}; |
| 169 | |
| 170 | ncnn::Mat a = RandomMat(w, h, 3); |
| 171 | |
| 172 | ncnn::Mat a2; |
| 173 | ncnn::convert_packing(a.reshape(w, h, 1), a2, 1, opt); |
| 174 | |
| 175 | // FIXME enable more convert types |
| 176 | for (int i = 0; i < 2; i++) |
| 177 | { |
| 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); |
| 179 | |
| 180 | ncnn::Mat b2; |
| 181 | ncnn::Mat c2; |
| 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); |
| 185 | |
| 186 | if (memcmp(m, d2, target_width * target_height * d2.c) != 0) |
| 187 | { |
| 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); |
| 189 | return -1; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 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) |
| 197 | { |
| 198 | ncnn::Option opt; |
| 199 | opt.num_threads = 1; |
| 200 | |
| 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}; |
| 203 | |
| 204 | ncnn::Mat a = RandomMat(w, h, 3); |
| 205 | |
| 206 | ncnn::Mat a2; |
| 207 | ncnn::convert_packing(a.reshape(w, h, 1), a2, 1, opt); |
| 208 | |
| 209 | // FIXME enable more convert types |
| 210 | for (int i = 0; i < 2; i++) |
| 211 | { |
| 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); |
| 213 | |
| 214 | ncnn::Mat b2; |
| 215 | ncnn::Mat c2; |
| 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); |
| 219 | |
| 220 | if (memcmp(m, d2, target_width * target_height * d2.c) != 0) |
| 221 | { |
| 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); |
| 223 | return -1; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 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) |
| 231 | { |
| 232 | ncnn::Option opt; |
| 233 | opt.num_threads = 1; |
| 234 | |
| 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}; |
| 237 | |
| 238 | ncnn::Mat a = RandomMat(w, h, 4); |
| 239 | |
| 240 | ncnn::Mat a2; |
| 241 | ncnn::convert_packing(a.reshape(w, h, 1), a2, 1, opt); |
| 242 | |
| 243 | for (int i = 0; i < 2; i++) |
| 244 | { |
| 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); |
| 246 | |
| 247 | ncnn::Mat b2; |
| 248 | ncnn::Mat c2; |
| 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); |
| 252 | |
| 253 | if (memcmp(m, d2, target_width * target_height * d2.c) != 0) |
| 254 | { |
| 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); |
| 256 | return -1; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 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) |
| 264 | { |
| 265 | ncnn::Option opt; |
| 266 | opt.num_threads = 1; |
| 267 | |
| 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}; |
| 270 | |
| 271 | ncnn::Mat a = RandomMat(w, h, 4); |
| 272 | |
| 273 | ncnn::Mat a2; |
| 274 | ncnn::convert_packing(a.reshape(w, h, 1), a2, 1, opt); |
| 275 | |
| 276 | for (int i = 0; i < 2; i++) |
| 277 | { |
| 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); |
| 279 | |
| 280 | ncnn::Mat b2; |
| 281 | ncnn::Mat c2; |
| 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); |
| 285 | |
| 286 | if (memcmp(m, d2, target_width * target_height * d2.c) != 0) |
| 287 | { |
| 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); |
| 289 | return -1; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | static int test_mat_pixel_0() |
| 297 | { |
| 298 | for (int c = 1; c <= 4; c++) |
| 299 | { |
| 300 | int ret = 0 |
| 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); |
| 306 | |
| 307 | if (ret != 0) |
| 308 | return ret; |
| 309 | } |
| 310 | |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | static int test_mat_pixel_1() |
| 315 | { |
| 316 | return 0 |
| 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); |
| 322 | } |
| 323 | |
| 324 | static int test_mat_pixel_2() |
| 325 | { |
| 326 | return 0 |
| 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); |
| 332 | } |
| 333 | |
| 334 | int main() |
| 335 | { |
| 336 | SRAND(7767517); |
| 337 | |
| 338 | return test_mat_pixel_0() || test_mat_pixel_1() || test_mat_pixel_2(); |
| 339 | } |