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