Elffuss BraidHub

kikocis/ncnn public

master · 13.7 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
15#include "mat.h"
16#include "prng.h"
17
18#include <string.h>
19
20static 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
24static ncnn::Mat RandomMat(int w, int h, int elempack)
25{
26 ncnn::Mat m(w, h, (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
37static ncnn::Mat FilledMat(int w, int h, int elempack, unsigned char v)
38{
39 ncnn::Mat m(w, h, (size_t)elempack, elempack);
40
41 unsigned char* p = m;
42 for (int i = 0; i < w * h * elempack; i++)
43 {
44 p[i] = v;
45 }
46
47 return m;
48}
49
50static int test_mat_pixel_gray(int w, int h)
51{
52 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};
53 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};
54
55 ncnn::Mat a = RandomMat(w, h, 1);
56
57 // FIXME enable more convert types
58 for (int i = 0; i < 1; i++)
59 {
60 ncnn::Mat m = ncnn::Mat::from_pixels(a, pixel_type_from[i], w, h);
61 ncnn::Mat b(w, h, (size_t)1u, 1);
62 m.to_pixels(b, pixel_type_to[i]);
63
64 if (memcmp(a, b, w * h * 1) != 0)
65 {
66 fprintf(stderr, "test_mat_pixel_gray failed w=%d h=%d pixel_type=%d\n", w, h, i);
67 return -1;
68 }
69 }
70
71 return 0;
72}
73
74static int test_mat_pixel_rgb(int w, int h)
75{
76 int pixel_type_from[4] = {ncnn::Mat::PIXEL_RGB, ncnn::Mat::PIXEL_RGB2BGR, ncnn::Mat::PIXEL_RGB2RGBA, ncnn::Mat::PIXEL_RGB2BGRA};
77 int pixel_type_to[4] = {ncnn::Mat::PIXEL_RGB, ncnn::Mat::PIXEL_BGR2RGB, ncnn::Mat::PIXEL_RGBA2RGB, ncnn::Mat::PIXEL_BGRA2RGB};
78
79 ncnn::Mat a = RandomMat(w, h, 3);
80
81 // FIXME enable more convert types
82 for (int i = 0; i < 2; i++)
83 {
84 ncnn::Mat m = ncnn::Mat::from_pixels(a, pixel_type_from[i], w, h);
85 ncnn::Mat b(w, h, (size_t)3u, 3);
86 m.to_pixels(b, pixel_type_to[i]);
87
88 if (memcmp(a, b, w * h * 3) != 0)
89 {
90 fprintf(stderr, "test_mat_pixel_rgb failed w=%d h=%d pixel_type=%d\n", w, h, i);
91 return -1;
92 }
93 }
94
95 return 0;
96}
97
98static int test_mat_pixel_bgr(int w, int h)
99{
100 int pixel_type_from[4] = {ncnn::Mat::PIXEL_BGR, ncnn::Mat::PIXEL_BGR2RGB, ncnn::Mat::PIXEL_BGR2RGBA, ncnn::Mat::PIXEL_BGR2BGRA};
101 int pixel_type_to[4] = {ncnn::Mat::PIXEL_BGR, ncnn::Mat::PIXEL_RGB2BGR, ncnn::Mat::PIXEL_RGBA2BGR, ncnn::Mat::PIXEL_BGRA2BGR};
102
103 ncnn::Mat a = RandomMat(w, h, 3);
104
105 // FIXME enable more convert types
106 for (int i = 0; i < 2; i++)
107 {
108 ncnn::Mat m = ncnn::Mat::from_pixels(a, pixel_type_from[i], w, h);
109 ncnn::Mat b(w, h, (size_t)3u, 3);
110 m.to_pixels(b, pixel_type_to[i]);
111
112 if (memcmp(a, b, w * h * 3) != 0)
113 {
114 fprintf(stderr, "test_mat_pixel_bgr failed w=%d h=%d pixel_type=%d\n", w, h, i);
115 return -1;
116 }
117 }
118
119 return 0;
120}
121
122static int test_mat_pixel_rgba(int w, int h)
123{
124 int pixel_type_from[2] = {ncnn::Mat::PIXEL_RGBA, ncnn::Mat::PIXEL_RGBA2BGRA};
125 int pixel_type_to[2] = {ncnn::Mat::PIXEL_RGBA, ncnn::Mat::PIXEL_BGRA2RGBA};
126
127 ncnn::Mat a = RandomMat(w, h, 4);
128
129 for (int i = 0; i < 2; i++)
130 {
131 ncnn::Mat m = ncnn::Mat::from_pixels(a, pixel_type_from[i], w, h);
132 ncnn::Mat b(w, h, (size_t)4u, 4);
133 m.to_pixels(b, pixel_type_to[i]);
134
135 if (memcmp(a, b, w * h * 4) != 0)
136 {
137 fprintf(stderr, "test_mat_pixel_rgba failed w=%d h=%d pixel_type=%d\n", w, h, i);
138 return -1;
139 }
140 }
141
142 return 0;
143}
144
145static int test_mat_pixel_bgra(int w, int h)
146{
147 int pixel_type_from[2] = {ncnn::Mat::PIXEL_BGRA, ncnn::Mat::PIXEL_BGRA2RGBA};
148 int pixel_type_to[2] = {ncnn::Mat::PIXEL_BGRA, ncnn::Mat::PIXEL_RGBA2BGRA};
149
150 ncnn::Mat a = RandomMat(w, h, 4);
151
152 for (int i = 0; i < 2; i++)
153 {
154 ncnn::Mat m = ncnn::Mat::from_pixels(a, pixel_type_from[i], w, h);
155 ncnn::Mat b(w, h, (size_t)4u, 4);
156 m.to_pixels(b, pixel_type_to[i]);
157
158 if (memcmp(a, b, w * h * 4) != 0)
159 {
160 fprintf(stderr, "test_mat_pixel_bgra failed w=%d h=%d pixel_type=%d\n", w, h, i);
161 return -1;
162 }
163 }
164
165 return 0;
166}
167
168static int test_mat_pixel_roi_gray(int w, int h, int roix, int roiy, int roiw, int roih)
169{
170 ncnn::Option opt;
171 opt.num_threads = 1;
172
173 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};
174 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};
175
176 ncnn::Mat a = RandomMat(w, h, 1);
177
178 ncnn::Mat a2;
179 ncnn::convert_packing(a.reshape(w, h, 1), a2, 1, opt);
180
181 // FIXME enable more convert types
182 for (int i = 0; i < 1; i++)
183 {
184 ncnn::Mat m = ncnn::Mat::from_pixels_roi(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih);
185 ncnn::Mat b(roiw, roih, (size_t)1u, 1);
186 m.to_pixels(b, pixel_type_to[i]);
187
188 ncnn::Mat b2;
189 ncnn::Mat c2;
190 ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw), opt);
191 ncnn::convert_packing(b2, c2, 1, opt);
192
193 if (memcmp(b, c2, roiw * roih * 1) != 0)
194 {
195 fprintf(stderr, "test_mat_pixel_roi_gray failed w=%d h=%d roi=[%d %d %d %d] pixel_type=%d\n", w, h, roix, roiy, roiw, roih, i);
196 return -1;
197 }
198 }
199
200 return 0;
201}
202
203static int test_mat_pixel_roi_rgb(int w, int h, int roix, int roiy, int roiw, int roih)
204{
205 ncnn::Option opt;
206 opt.num_threads = 1;
207
208 int pixel_type_from[4] = {ncnn::Mat::PIXEL_RGB, ncnn::Mat::PIXEL_RGB2BGR, ncnn::Mat::PIXEL_RGB2RGBA, ncnn::Mat::PIXEL_RGB2BGRA};
209 int pixel_type_to[4] = {ncnn::Mat::PIXEL_RGB, ncnn::Mat::PIXEL_BGR2RGB, ncnn::Mat::PIXEL_RGBA2RGB, ncnn::Mat::PIXEL_BGRA2RGB};
210
211 ncnn::Mat a = RandomMat(w, h, 3);
212
213 ncnn::Mat a2;
214 ncnn::convert_packing(a.reshape(w, h, 1), a2, 1, opt);
215
216 // FIXME enable more convert types
217 for (int i = 0; i < 2; i++)
218 {
219 ncnn::Mat m = ncnn::Mat::from_pixels_roi(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih);
220 ncnn::Mat b(roiw, roih, (size_t)3u, 3);
221 m.to_pixels(b, pixel_type_to[i]);
222
223 ncnn::Mat b2;
224 ncnn::Mat c2;
225 ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw), opt);
226 ncnn::convert_packing(b2, c2, 3, opt);
227
228 if (memcmp(b, c2, roiw * roih * 3) != 0)
229 {
230 fprintf(stderr, "test_mat_pixel_roi_rgb failed w=%d h=%d roi=[%d %d %d %d] pixel_type=%d\n", w, h, roix, roiy, roiw, roih, i);
231 return -1;
232 }
233 }
234
235 return 0;
236}
237
238static int test_mat_pixel_roi_bgr(int w, int h, int roix, int roiy, int roiw, int roih)
239{
240 ncnn::Option opt;
241 opt.num_threads = 1;
242
243 int pixel_type_from[4] = {ncnn::Mat::PIXEL_BGR, ncnn::Mat::PIXEL_BGR2RGB, ncnn::Mat::PIXEL_BGR2RGBA, ncnn::Mat::PIXEL_BGR2BGRA};
244 int pixel_type_to[4] = {ncnn::Mat::PIXEL_BGR, ncnn::Mat::PIXEL_RGB2BGR, ncnn::Mat::PIXEL_RGBA2BGR, ncnn::Mat::PIXEL_BGRA2BGR};
245
246 ncnn::Mat a = RandomMat(w, h, 3);
247
248 ncnn::Mat a2;
249 ncnn::convert_packing(a.reshape(w, h, 1), a2, 1, opt);
250
251 // FIXME enable more convert types
252 for (int i = 0; i < 2; i++)
253 {
254 ncnn::Mat m = ncnn::Mat::from_pixels_roi(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih);
255 ncnn::Mat b(roiw, roih, (size_t)3u, 3);
256 m.to_pixels(b, pixel_type_to[i]);
257
258 ncnn::Mat b2;
259 ncnn::Mat c2;
260 ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw), opt);
261 ncnn::convert_packing(b2, c2, 3, opt);
262
263 if (memcmp(b, c2, roiw * roih * 3) != 0)
264 {
265 fprintf(stderr, "test_mat_pixel_roi_bgr failed w=%d h=%d roi=[%d %d %d %d] pixel_type=%d\n", w, h, roix, roiy, roiw, roih, i);
266 return -1;
267 }
268 }
269
270 return 0;
271}
272
273static int test_mat_pixel_roi_rgba(int w, int h, int roix, int roiy, int roiw, int roih)
274{
275 ncnn::Option opt;
276 opt.num_threads = 1;
277
278 int pixel_type_from[2] = {ncnn::Mat::PIXEL_RGBA, ncnn::Mat::PIXEL_RGBA2BGRA};
279 int pixel_type_to[2] = {ncnn::Mat::PIXEL_RGBA, ncnn::Mat::PIXEL_BGRA2RGBA};
280
281 ncnn::Mat a = RandomMat(w, h, 4);
282
283 ncnn::Mat a2;
284 ncnn::convert_packing(a.reshape(w, h, 1), a2, 1, opt);
285
286 for (int i = 0; i < 2; i++)
287 {
288 ncnn::Mat m = ncnn::Mat::from_pixels_roi(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih);
289 ncnn::Mat b(roiw, roih, (size_t)4u, 4);
290 m.to_pixels(b, pixel_type_to[i]);
291
292 ncnn::Mat b2;
293 ncnn::Mat c2;
294 ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw), opt);
295 ncnn::convert_packing(b2, c2, 4, opt);
296
297 if (memcmp(b, c2, roiw * roih * 4) != 0)
298 {
299 fprintf(stderr, "test_mat_pixel_roi_rgba failed w=%d h=%d roi=[%d %d %d %d] pixel_type=%d\n", w, h, roix, roiy, roiw, roih, i);
300 return -1;
301 }
302 }
303
304 return 0;
305}
306
307static int test_mat_pixel_roi_bgra(int w, int h, int roix, int roiy, int roiw, int roih)
308{
309 ncnn::Option opt;
310 opt.num_threads = 1;
311
312 int pixel_type_from[2] = {ncnn::Mat::PIXEL_BGRA, ncnn::Mat::PIXEL_BGRA2RGBA};
313 int pixel_type_to[2] = {ncnn::Mat::PIXEL_BGRA, ncnn::Mat::PIXEL_RGBA2BGRA};
314
315 ncnn::Mat a = RandomMat(w, h, 4);
316
317 ncnn::Mat a2;
318 ncnn::convert_packing(a.reshape(w, h, 1), a2, 1, opt);
319
320 for (int i = 0; i < 2; i++)
321 {
322 ncnn::Mat m = ncnn::Mat::from_pixels_roi(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih);
323 ncnn::Mat b(roiw, roih, (size_t)4u, 4);
324 m.to_pixels(b, pixel_type_to[i]);
325
326 ncnn::Mat b2;
327 ncnn::Mat c2;
328 ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw), opt);
329 ncnn::convert_packing(b2, c2, 4, opt);
330
331 if (memcmp(b, c2, roiw * roih * 4) != 0)
332 {
333 fprintf(stderr, "test_mat_pixel_roi_bgra failed w=%d h=%d roi=[%d %d %d %d] pixel_type=%d\n", w, h, roix, roiy, roiw, roih, i);
334 return -1;
335 }
336 }
337
338 return 0;
339}
340
341static int test_mat_pixel_yuv420sp2rgb(int w, int h)
342{
343 ncnn::Mat nv21 = RandomMat(w, h / 2 * 3, 1);
344
345 ncnn::Mat nv12 = nv21.clone();
346
347 // swap VU to UV
348 unsigned char* p = (unsigned char*)nv12 + w * h;
349 for (int i = 0; i < w * h / 4; i++)
350 {
351 unsigned char v = p[0];
352 unsigned char u = p[1];
353 p[0] = u;
354 p[1] = v;
355 p += 2;
356 }
357
358 ncnn::Mat rgb(w, h, (size_t)3u, 3);
359 yuv420sp2rgb(nv21, w, h, rgb);
360
361 ncnn::Mat rgb2(w, h, (size_t)3u, 3);
362 yuv420sp2rgb_nv12(nv12, w, h, rgb2);
363
364 if (memcmp(rgb, rgb2, w * h * 3) != 0)
365 {
366 fprintf(stderr, "test_mat_pixel_yuv420sp2rgb failed w=%d h=%d\n", w, h);
367 return -1;
368 }
369
370 return 0;
371}
372
373static int test_mat_pixel_0()
374{
375 return 0
376 || test_mat_pixel_gray(16, 16)
377 || test_mat_pixel_rgb(16, 16)
378 || test_mat_pixel_bgr(16, 16)
379 || test_mat_pixel_rgba(16, 16)
380 || test_mat_pixel_bgra(16, 16);
381}
382
383static int test_mat_pixel_1()
384{
385 return 0
386 || test_mat_pixel_gray(15, 15)
387 || test_mat_pixel_rgb(15, 15)
388 || test_mat_pixel_bgr(15, 15)
389 || test_mat_pixel_rgba(15, 15)
390 || test_mat_pixel_bgra(15, 15);
391}
392
393static int test_mat_pixel_2()
394{
395 return 0
396 || test_mat_pixel_gray(1, 1)
397 || test_mat_pixel_rgb(1, 1)
398 || test_mat_pixel_bgr(1, 1)
399 || test_mat_pixel_rgba(1, 1)
400 || test_mat_pixel_bgra(1, 1);
401}
402
403static int test_mat_pixel_3()
404{
405 return 0
406 || test_mat_pixel_gray(3, 3)
407 || test_mat_pixel_rgb(3, 3)
408 || test_mat_pixel_bgr(3, 3)
409 || test_mat_pixel_rgba(3, 3)
410 || test_mat_pixel_bgra(3, 3);
411}
412
413static int test_mat_pixel_4()
414{
415 return 0
416 || test_mat_pixel_roi_gray(16, 16, 1, 1, 13, 13)
417 || test_mat_pixel_roi_rgb(16, 16, 2, 1, 11, 11)
418 || test_mat_pixel_roi_bgr(16, 16, 1, 2, 11, 9)
419 || test_mat_pixel_roi_rgba(16, 16, 3, 2, 9, 11)
420 || test_mat_pixel_roi_bgra(16, 16, 2, 3, 9, 7);
421}
422
423static int test_mat_pixel_5()
424{
425 return 0
426 || test_mat_pixel_roi_gray(15, 15, 2, 3, 2, 3)
427 || test_mat_pixel_roi_rgb(15, 15, 3, 4, 5, 4)
428 || test_mat_pixel_roi_bgr(15, 15, 4, 5, 6, 7)
429 || test_mat_pixel_roi_rgba(15, 15, 6, 6, 3, 1)
430 || test_mat_pixel_roi_bgra(15, 15, 7, 3, 1, 1);
431}
432
433static int test_mat_pixel_6()
434{
435 return 0
436 || test_mat_pixel_yuv420sp2rgb(16, 16)
437 || test_mat_pixel_yuv420sp2rgb(12, 12)
438 || test_mat_pixel_yuv420sp2rgb(2, 2)
439 || test_mat_pixel_yuv420sp2rgb(6, 6);
440}
441
442int main()
443{
444 SRAND(7767517);
445
446 return 0
447 || test_mat_pixel_0()
448 || test_mat_pixel_1()
449 || test_mat_pixel_2()
450 || test_mat_pixel_3()
451 || test_mat_pixel_4()
452 || test_mat_pixel_5()
453 || test_mat_pixel_6();
454}