You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_mat_pixel.cpp 13 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. // Copyright 2020 Tencent
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. #include "mat.h"
  4. #include "prng.h"
  5. #include <string.h>
  6. static struct prng_rand_t g_prng_rand_state;
  7. #define SRAND(seed) prng_srand(seed, &g_prng_rand_state)
  8. #define RAND() prng_rand(&g_prng_rand_state)
  9. static ncnn::Mat RandomMat(int w, int h, int elempack)
  10. {
  11. ncnn::Mat m(w, h, (size_t)elempack, elempack);
  12. unsigned char* p = m;
  13. for (int i = 0; i < w * h * elempack; i++)
  14. {
  15. p[i] = RAND() % 256;
  16. }
  17. return m;
  18. }
  19. static ncnn::Mat FilledMat(int w, int h, int elempack, unsigned char v)
  20. {
  21. ncnn::Mat m(w, h, (size_t)elempack, elempack);
  22. unsigned char* p = m;
  23. for (int i = 0; i < w * h * elempack; i++)
  24. {
  25. p[i] = v;
  26. }
  27. return m;
  28. }
  29. static int test_mat_pixel_gray(int w, int h)
  30. {
  31. 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};
  32. 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};
  33. ncnn::Mat a = RandomMat(w, h, 1);
  34. // FIXME enable more convert types
  35. for (int i = 0; i < 1; i++)
  36. {
  37. ncnn::Mat m = ncnn::Mat::from_pixels(a, pixel_type_from[i], w, h);
  38. ncnn::Mat b(w, h, (size_t)1u, 1);
  39. m.to_pixels(b, pixel_type_to[i]);
  40. if (memcmp(a, b, w * h * 1) != 0)
  41. {
  42. fprintf(stderr, "test_mat_pixel_gray failed w=%d h=%d pixel_type=%d\n", w, h, i);
  43. return -1;
  44. }
  45. }
  46. return 0;
  47. }
  48. static int test_mat_pixel_rgb(int w, int h)
  49. {
  50. int pixel_type_from[4] = {ncnn::Mat::PIXEL_RGB, ncnn::Mat::PIXEL_RGB2BGR, ncnn::Mat::PIXEL_RGB2RGBA, ncnn::Mat::PIXEL_RGB2BGRA};
  51. int pixel_type_to[4] = {ncnn::Mat::PIXEL_RGB, ncnn::Mat::PIXEL_BGR2RGB, ncnn::Mat::PIXEL_RGBA2RGB, ncnn::Mat::PIXEL_BGRA2RGB};
  52. ncnn::Mat a = RandomMat(w, h, 3);
  53. // FIXME enable more convert types
  54. for (int i = 0; i < 2; i++)
  55. {
  56. ncnn::Mat m = ncnn::Mat::from_pixels(a, pixel_type_from[i], w, h);
  57. ncnn::Mat b(w, h, (size_t)3u, 3);
  58. m.to_pixels(b, pixel_type_to[i]);
  59. if (memcmp(a, b, w * h * 3) != 0)
  60. {
  61. fprintf(stderr, "test_mat_pixel_rgb failed w=%d h=%d pixel_type=%d\n", w, h, i);
  62. return -1;
  63. }
  64. }
  65. return 0;
  66. }
  67. static int test_mat_pixel_bgr(int w, int h)
  68. {
  69. int pixel_type_from[4] = {ncnn::Mat::PIXEL_BGR, ncnn::Mat::PIXEL_BGR2RGB, ncnn::Mat::PIXEL_BGR2RGBA, ncnn::Mat::PIXEL_BGR2BGRA};
  70. int pixel_type_to[4] = {ncnn::Mat::PIXEL_BGR, ncnn::Mat::PIXEL_RGB2BGR, ncnn::Mat::PIXEL_RGBA2BGR, ncnn::Mat::PIXEL_BGRA2BGR};
  71. ncnn::Mat a = RandomMat(w, h, 3);
  72. // FIXME enable more convert types
  73. for (int i = 0; i < 2; i++)
  74. {
  75. ncnn::Mat m = ncnn::Mat::from_pixels(a, pixel_type_from[i], w, h);
  76. ncnn::Mat b(w, h, (size_t)3u, 3);
  77. m.to_pixels(b, pixel_type_to[i]);
  78. if (memcmp(a, b, w * h * 3) != 0)
  79. {
  80. fprintf(stderr, "test_mat_pixel_bgr failed w=%d h=%d pixel_type=%d\n", w, h, i);
  81. return -1;
  82. }
  83. }
  84. return 0;
  85. }
  86. static int test_mat_pixel_rgba(int w, int h)
  87. {
  88. int pixel_type_from[2] = {ncnn::Mat::PIXEL_RGBA, ncnn::Mat::PIXEL_RGBA2BGRA};
  89. int pixel_type_to[2] = {ncnn::Mat::PIXEL_RGBA, ncnn::Mat::PIXEL_BGRA2RGBA};
  90. ncnn::Mat a = RandomMat(w, h, 4);
  91. for (int i = 0; i < 2; i++)
  92. {
  93. ncnn::Mat m = ncnn::Mat::from_pixels(a, pixel_type_from[i], w, h);
  94. ncnn::Mat b(w, h, (size_t)4u, 4);
  95. m.to_pixels(b, pixel_type_to[i]);
  96. if (memcmp(a, b, w * h * 4) != 0)
  97. {
  98. fprintf(stderr, "test_mat_pixel_rgba failed w=%d h=%d pixel_type=%d\n", w, h, i);
  99. return -1;
  100. }
  101. }
  102. return 0;
  103. }
  104. static int test_mat_pixel_bgra(int w, int h)
  105. {
  106. int pixel_type_from[2] = {ncnn::Mat::PIXEL_BGRA, ncnn::Mat::PIXEL_BGRA2RGBA};
  107. int pixel_type_to[2] = {ncnn::Mat::PIXEL_BGRA, ncnn::Mat::PIXEL_RGBA2BGRA};
  108. ncnn::Mat a = RandomMat(w, h, 4);
  109. for (int i = 0; i < 2; i++)
  110. {
  111. ncnn::Mat m = ncnn::Mat::from_pixels(a, pixel_type_from[i], w, h);
  112. ncnn::Mat b(w, h, (size_t)4u, 4);
  113. m.to_pixels(b, pixel_type_to[i]);
  114. if (memcmp(a, b, w * h * 4) != 0)
  115. {
  116. fprintf(stderr, "test_mat_pixel_bgra failed w=%d h=%d pixel_type=%d\n", w, h, i);
  117. return -1;
  118. }
  119. }
  120. return 0;
  121. }
  122. static int test_mat_pixel_roi_gray(int w, int h, int roix, int roiy, int roiw, int roih)
  123. {
  124. ncnn::Option opt;
  125. opt.num_threads = 1;
  126. 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};
  127. 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};
  128. ncnn::Mat a = RandomMat(w, h, 1);
  129. ncnn::Mat a2;
  130. ncnn::convert_packing(a.reshape(w, h, 1), a2, 1, opt);
  131. // FIXME enable more convert types
  132. for (int i = 0; i < 1; i++)
  133. {
  134. ncnn::Mat m = ncnn::Mat::from_pixels_roi(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih);
  135. ncnn::Mat b(roiw, roih, (size_t)1u, 1);
  136. m.to_pixels(b, pixel_type_to[i]);
  137. ncnn::Mat b2;
  138. ncnn::Mat c2;
  139. ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw), opt);
  140. ncnn::convert_packing(b2, c2, 1, opt);
  141. if (memcmp(b, c2, roiw * roih * 1) != 0)
  142. {
  143. 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);
  144. return -1;
  145. }
  146. }
  147. return 0;
  148. }
  149. static int test_mat_pixel_roi_rgb(int w, int h, int roix, int roiy, int roiw, int roih)
  150. {
  151. ncnn::Option opt;
  152. opt.num_threads = 1;
  153. int pixel_type_from[4] = {ncnn::Mat::PIXEL_RGB, ncnn::Mat::PIXEL_RGB2BGR, ncnn::Mat::PIXEL_RGB2RGBA, ncnn::Mat::PIXEL_RGB2BGRA};
  154. int pixel_type_to[4] = {ncnn::Mat::PIXEL_RGB, ncnn::Mat::PIXEL_BGR2RGB, ncnn::Mat::PIXEL_RGBA2RGB, ncnn::Mat::PIXEL_BGRA2RGB};
  155. ncnn::Mat a = RandomMat(w, h, 3);
  156. ncnn::Mat a2;
  157. ncnn::convert_packing(a.reshape(w, h, 1), a2, 1, opt);
  158. // FIXME enable more convert types
  159. for (int i = 0; i < 2; i++)
  160. {
  161. ncnn::Mat m = ncnn::Mat::from_pixels_roi(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih);
  162. ncnn::Mat b(roiw, roih, (size_t)3u, 3);
  163. m.to_pixels(b, pixel_type_to[i]);
  164. ncnn::Mat b2;
  165. ncnn::Mat c2;
  166. ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw), opt);
  167. ncnn::convert_packing(b2, c2, 3, opt);
  168. if (memcmp(b, c2, roiw * roih * 3) != 0)
  169. {
  170. 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);
  171. return -1;
  172. }
  173. }
  174. return 0;
  175. }
  176. static int test_mat_pixel_roi_bgr(int w, int h, int roix, int roiy, int roiw, int roih)
  177. {
  178. ncnn::Option opt;
  179. opt.num_threads = 1;
  180. int pixel_type_from[4] = {ncnn::Mat::PIXEL_BGR, ncnn::Mat::PIXEL_BGR2RGB, ncnn::Mat::PIXEL_BGR2RGBA, ncnn::Mat::PIXEL_BGR2BGRA};
  181. int pixel_type_to[4] = {ncnn::Mat::PIXEL_BGR, ncnn::Mat::PIXEL_RGB2BGR, ncnn::Mat::PIXEL_RGBA2BGR, ncnn::Mat::PIXEL_BGRA2BGR};
  182. ncnn::Mat a = RandomMat(w, h, 3);
  183. ncnn::Mat a2;
  184. ncnn::convert_packing(a.reshape(w, h, 1), a2, 1, opt);
  185. // FIXME enable more convert types
  186. for (int i = 0; i < 2; i++)
  187. {
  188. ncnn::Mat m = ncnn::Mat::from_pixels_roi(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih);
  189. ncnn::Mat b(roiw, roih, (size_t)3u, 3);
  190. m.to_pixels(b, pixel_type_to[i]);
  191. ncnn::Mat b2;
  192. ncnn::Mat c2;
  193. ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw), opt);
  194. ncnn::convert_packing(b2, c2, 3, opt);
  195. if (memcmp(b, c2, roiw * roih * 3) != 0)
  196. {
  197. 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);
  198. return -1;
  199. }
  200. }
  201. return 0;
  202. }
  203. static int test_mat_pixel_roi_rgba(int w, int h, int roix, int roiy, int roiw, int roih)
  204. {
  205. ncnn::Option opt;
  206. opt.num_threads = 1;
  207. int pixel_type_from[2] = {ncnn::Mat::PIXEL_RGBA, ncnn::Mat::PIXEL_RGBA2BGRA};
  208. int pixel_type_to[2] = {ncnn::Mat::PIXEL_RGBA, ncnn::Mat::PIXEL_BGRA2RGBA};
  209. ncnn::Mat a = RandomMat(w, h, 4);
  210. ncnn::Mat a2;
  211. ncnn::convert_packing(a.reshape(w, h, 1), a2, 1, opt);
  212. for (int i = 0; i < 2; i++)
  213. {
  214. ncnn::Mat m = ncnn::Mat::from_pixels_roi(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih);
  215. ncnn::Mat b(roiw, roih, (size_t)4u, 4);
  216. m.to_pixels(b, pixel_type_to[i]);
  217. ncnn::Mat b2;
  218. ncnn::Mat c2;
  219. ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw), opt);
  220. ncnn::convert_packing(b2, c2, 4, opt);
  221. if (memcmp(b, c2, roiw * roih * 4) != 0)
  222. {
  223. 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);
  224. return -1;
  225. }
  226. }
  227. return 0;
  228. }
  229. static int test_mat_pixel_roi_bgra(int w, int h, int roix, int roiy, int roiw, int roih)
  230. {
  231. ncnn::Option opt;
  232. opt.num_threads = 1;
  233. int pixel_type_from[2] = {ncnn::Mat::PIXEL_BGRA, ncnn::Mat::PIXEL_BGRA2RGBA};
  234. int pixel_type_to[2] = {ncnn::Mat::PIXEL_BGRA, ncnn::Mat::PIXEL_RGBA2BGRA};
  235. ncnn::Mat a = RandomMat(w, h, 4);
  236. ncnn::Mat a2;
  237. ncnn::convert_packing(a.reshape(w, h, 1), a2, 1, opt);
  238. for (int i = 0; i < 2; i++)
  239. {
  240. ncnn::Mat m = ncnn::Mat::from_pixels_roi(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih);
  241. ncnn::Mat b(roiw, roih, (size_t)4u, 4);
  242. m.to_pixels(b, pixel_type_to[i]);
  243. ncnn::Mat b2;
  244. ncnn::Mat c2;
  245. ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw), opt);
  246. ncnn::convert_packing(b2, c2, 4, opt);
  247. if (memcmp(b, c2, roiw * roih * 4) != 0)
  248. {
  249. 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);
  250. return -1;
  251. }
  252. }
  253. return 0;
  254. }
  255. static int test_mat_pixel_yuv420sp2rgb(int w, int h)
  256. {
  257. ncnn::Mat nv21 = RandomMat(w, h / 2 * 3, 1);
  258. ncnn::Mat nv12 = nv21.clone();
  259. // swap VU to UV
  260. unsigned char* p = (unsigned char*)nv12 + w * h;
  261. for (int i = 0; i < w * h / 4; i++)
  262. {
  263. unsigned char v = p[0];
  264. unsigned char u = p[1];
  265. p[0] = u;
  266. p[1] = v;
  267. p += 2;
  268. }
  269. ncnn::Mat rgb(w, h, (size_t)3u, 3);
  270. yuv420sp2rgb(nv21, w, h, rgb);
  271. ncnn::Mat rgb2(w, h, (size_t)3u, 3);
  272. yuv420sp2rgb_nv12(nv12, w, h, rgb2);
  273. if (memcmp(rgb, rgb2, w * h * 3) != 0)
  274. {
  275. fprintf(stderr, "test_mat_pixel_yuv420sp2rgb failed w=%d h=%d\n", w, h);
  276. return -1;
  277. }
  278. return 0;
  279. }
  280. static int test_mat_pixel_0()
  281. {
  282. return 0
  283. || test_mat_pixel_gray(16, 16)
  284. || test_mat_pixel_rgb(16, 16)
  285. || test_mat_pixel_bgr(16, 16)
  286. || test_mat_pixel_rgba(16, 16)
  287. || test_mat_pixel_bgra(16, 16);
  288. }
  289. static int test_mat_pixel_1()
  290. {
  291. return 0
  292. || test_mat_pixel_gray(15, 15)
  293. || test_mat_pixel_rgb(15, 15)
  294. || test_mat_pixel_bgr(15, 15)
  295. || test_mat_pixel_rgba(15, 15)
  296. || test_mat_pixel_bgra(15, 15);
  297. }
  298. static int test_mat_pixel_2()
  299. {
  300. return 0
  301. || test_mat_pixel_gray(1, 1)
  302. || test_mat_pixel_rgb(1, 1)
  303. || test_mat_pixel_bgr(1, 1)
  304. || test_mat_pixel_rgba(1, 1)
  305. || test_mat_pixel_bgra(1, 1);
  306. }
  307. static int test_mat_pixel_3()
  308. {
  309. return 0
  310. || test_mat_pixel_gray(3, 3)
  311. || test_mat_pixel_rgb(3, 3)
  312. || test_mat_pixel_bgr(3, 3)
  313. || test_mat_pixel_rgba(3, 3)
  314. || test_mat_pixel_bgra(3, 3);
  315. }
  316. static int test_mat_pixel_4()
  317. {
  318. return 0
  319. || test_mat_pixel_roi_gray(16, 16, 1, 1, 13, 13)
  320. || test_mat_pixel_roi_rgb(16, 16, 2, 1, 11, 11)
  321. || test_mat_pixel_roi_bgr(16, 16, 1, 2, 11, 9)
  322. || test_mat_pixel_roi_rgba(16, 16, 3, 2, 9, 11)
  323. || test_mat_pixel_roi_bgra(16, 16, 2, 3, 9, 7);
  324. }
  325. static int test_mat_pixel_5()
  326. {
  327. return 0
  328. || test_mat_pixel_roi_gray(15, 15, 2, 3, 2, 3)
  329. || test_mat_pixel_roi_rgb(15, 15, 3, 4, 5, 4)
  330. || test_mat_pixel_roi_bgr(15, 15, 4, 5, 6, 7)
  331. || test_mat_pixel_roi_rgba(15, 15, 6, 6, 3, 1)
  332. || test_mat_pixel_roi_bgra(15, 15, 7, 3, 1, 1);
  333. }
  334. static int test_mat_pixel_6()
  335. {
  336. return 0
  337. || test_mat_pixel_yuv420sp2rgb(16, 16)
  338. || test_mat_pixel_yuv420sp2rgb(12, 12)
  339. || test_mat_pixel_yuv420sp2rgb(2, 2)
  340. || test_mat_pixel_yuv420sp2rgb(6, 6);
  341. }
  342. int main()
  343. {
  344. SRAND(7767517);
  345. return 0
  346. || test_mat_pixel_0()
  347. || test_mat_pixel_1()
  348. || test_mat_pixel_2()
  349. || test_mat_pixel_3()
  350. || test_mat_pixel_4()
  351. || test_mat_pixel_5()
  352. || test_mat_pixel_6();
  353. }