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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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, 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, 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, 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, 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, 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. 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};
  136. 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};
  137. ncnn::Mat a = RandomMat(w, h, 1);
  138. ncnn::Mat a2;
  139. ncnn::convert_packing(a.reshape(w, h, 1), a2, 1);
  140. // FIXME enable more convert types
  141. for (int i = 0; i < 1; i++)
  142. {
  143. ncnn::Mat m = ncnn::Mat::from_pixels_roi(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih);
  144. ncnn::Mat b(roiw, roih, 1u, 1);
  145. m.to_pixels(b, pixel_type_to[i]);
  146. ncnn::Mat b2;
  147. ncnn::Mat c2;
  148. ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw));
  149. ncnn::convert_packing(b2, c2, 1);
  150. if (memcmp(b, c2, roiw * roih * 1) != 0)
  151. {
  152. 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);
  153. return -1;
  154. }
  155. }
  156. return 0;
  157. }
  158. static int test_mat_pixel_roi_rgb(int w, int h, int roix, int roiy, int roiw, int roih)
  159. {
  160. int pixel_type_from[4] = {ncnn::Mat::PIXEL_RGB, ncnn::Mat::PIXEL_RGB2BGR, ncnn::Mat::PIXEL_RGB2RGBA, ncnn::Mat::PIXEL_RGB2BGRA};
  161. int pixel_type_to[4] = {ncnn::Mat::PIXEL_RGB, ncnn::Mat::PIXEL_BGR2RGB, ncnn::Mat::PIXEL_RGBA2RGB, ncnn::Mat::PIXEL_BGRA2RGB};
  162. ncnn::Mat a = RandomMat(w, h, 3);
  163. ncnn::Mat a2;
  164. ncnn::convert_packing(a.reshape(w, h, 1), a2, 1);
  165. // FIXME enable more convert types
  166. for (int i = 0; i < 2; i++)
  167. {
  168. ncnn::Mat m = ncnn::Mat::from_pixels_roi(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih);
  169. ncnn::Mat b(roiw, roih, 3u, 3);
  170. m.to_pixels(b, pixel_type_to[i]);
  171. ncnn::Mat b2;
  172. ncnn::Mat c2;
  173. ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw));
  174. ncnn::convert_packing(b2, c2, 3);
  175. if (memcmp(b, c2, roiw * roih * 3) != 0)
  176. {
  177. 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);
  178. return -1;
  179. }
  180. }
  181. return 0;
  182. }
  183. static int test_mat_pixel_roi_bgr(int w, int h, int roix, int roiy, int roiw, int roih)
  184. {
  185. int pixel_type_from[4] = {ncnn::Mat::PIXEL_BGR, ncnn::Mat::PIXEL_BGR2RGB, ncnn::Mat::PIXEL_BGR2RGBA, ncnn::Mat::PIXEL_BGR2BGRA};
  186. int pixel_type_to[4] = {ncnn::Mat::PIXEL_BGR, ncnn::Mat::PIXEL_RGB2BGR, ncnn::Mat::PIXEL_RGBA2BGR, ncnn::Mat::PIXEL_BGRA2BGR};
  187. ncnn::Mat a = RandomMat(w, h, 3);
  188. ncnn::Mat a2;
  189. ncnn::convert_packing(a.reshape(w, h, 1), a2, 1);
  190. // FIXME enable more convert types
  191. for (int i = 0; i < 2; i++)
  192. {
  193. ncnn::Mat m = ncnn::Mat::from_pixels_roi(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih);
  194. ncnn::Mat b(roiw, roih, 3u, 3);
  195. m.to_pixels(b, pixel_type_to[i]);
  196. ncnn::Mat b2;
  197. ncnn::Mat c2;
  198. ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw));
  199. ncnn::convert_packing(b2, c2, 3);
  200. if (memcmp(b, c2, roiw * roih * 3) != 0)
  201. {
  202. 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);
  203. return -1;
  204. }
  205. }
  206. return 0;
  207. }
  208. static int test_mat_pixel_roi_rgba(int w, int h, int roix, int roiy, int roiw, int roih)
  209. {
  210. int pixel_type_from[2] = {ncnn::Mat::PIXEL_RGBA, ncnn::Mat::PIXEL_RGBA2BGRA};
  211. int pixel_type_to[2] = {ncnn::Mat::PIXEL_RGBA, ncnn::Mat::PIXEL_BGRA2RGBA};
  212. ncnn::Mat a = RandomMat(w, h, 4);
  213. ncnn::Mat a2;
  214. ncnn::convert_packing(a.reshape(w, h, 1), a2, 1);
  215. for (int i = 0; i < 2; i++)
  216. {
  217. ncnn::Mat m = ncnn::Mat::from_pixels_roi(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih);
  218. ncnn::Mat b(roiw, roih, 4u, 4);
  219. m.to_pixels(b, pixel_type_to[i]);
  220. ncnn::Mat b2;
  221. ncnn::Mat c2;
  222. ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw));
  223. ncnn::convert_packing(b2, c2, 4);
  224. if (memcmp(b, c2, roiw * roih * 4) != 0)
  225. {
  226. 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);
  227. return -1;
  228. }
  229. }
  230. return 0;
  231. }
  232. static int test_mat_pixel_roi_bgra(int w, int h, int roix, int roiy, int roiw, int roih)
  233. {
  234. int pixel_type_from[2] = {ncnn::Mat::PIXEL_BGRA, ncnn::Mat::PIXEL_BGRA2RGBA};
  235. int pixel_type_to[2] = {ncnn::Mat::PIXEL_BGRA, ncnn::Mat::PIXEL_RGBA2BGRA};
  236. ncnn::Mat a = RandomMat(w, h, 4);
  237. ncnn::Mat a2;
  238. ncnn::convert_packing(a.reshape(w, h, 1), a2, 1);
  239. for (int i = 0; i < 2; i++)
  240. {
  241. ncnn::Mat m = ncnn::Mat::from_pixels_roi(a, pixel_type_from[i], w, h, roix, roiy, roiw, roih);
  242. ncnn::Mat b(roiw, roih, 4u, 4);
  243. m.to_pixels(b, pixel_type_to[i]);
  244. ncnn::Mat b2;
  245. ncnn::Mat c2;
  246. ncnn::copy_cut_border(a2, b2, roiy, h - (roiy + roih), roix, w - (roix + roiw));
  247. ncnn::convert_packing(b2, c2, 4);
  248. if (memcmp(b, c2, roiw * roih * 4) != 0)
  249. {
  250. 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);
  251. return -1;
  252. }
  253. }
  254. return 0;
  255. }
  256. static int test_mat_pixel_yuv420sp2rgb(int w, int h)
  257. {
  258. ncnn::Mat nv21 = RandomMat(w, h / 2 * 3, 1);
  259. ncnn::Mat nv12 = nv21.clone();
  260. // swap VU to UV
  261. unsigned char* p = (unsigned char*)nv12 + w * h;
  262. for (int i = 0; i < w * h / 4; i++)
  263. {
  264. unsigned char v = p[0];
  265. unsigned char u = p[1];
  266. p[0] = u;
  267. p[1] = v;
  268. p += 2;
  269. }
  270. ncnn::Mat rgb(w, h, 3u, 3);
  271. yuv420sp2rgb(nv21, w, h, rgb);
  272. ncnn::Mat rgb2(w, h, 3u, 3);
  273. yuv420sp2rgb_nv12(nv12, w, h, rgb2);
  274. if (memcmp(rgb, rgb2, w * h * 3) != 0)
  275. {
  276. fprintf(stderr, "test_mat_pixel_yuv420sp2rgb failed w=%d h=%d\n", w, h);
  277. return -1;
  278. }
  279. return 0;
  280. }
  281. static int test_mat_pixel_0()
  282. {
  283. return 0
  284. || test_mat_pixel_gray(16, 16)
  285. || test_mat_pixel_rgb(16, 16)
  286. || test_mat_pixel_bgr(16, 16)
  287. || test_mat_pixel_rgba(16, 16)
  288. || test_mat_pixel_bgra(16, 16);
  289. }
  290. static int test_mat_pixel_1()
  291. {
  292. return 0
  293. || test_mat_pixel_gray(15, 15)
  294. || test_mat_pixel_rgb(15, 15)
  295. || test_mat_pixel_bgr(15, 15)
  296. || test_mat_pixel_rgba(15, 15)
  297. || test_mat_pixel_bgra(15, 15);
  298. }
  299. static int test_mat_pixel_2()
  300. {
  301. return 0
  302. || test_mat_pixel_gray(1, 1)
  303. || test_mat_pixel_rgb(1, 1)
  304. || test_mat_pixel_bgr(1, 1)
  305. || test_mat_pixel_rgba(1, 1)
  306. || test_mat_pixel_bgra(1, 1);
  307. }
  308. static int test_mat_pixel_3()
  309. {
  310. return 0
  311. || test_mat_pixel_gray(3, 3)
  312. || test_mat_pixel_rgb(3, 3)
  313. || test_mat_pixel_bgr(3, 3)
  314. || test_mat_pixel_rgba(3, 3)
  315. || test_mat_pixel_bgra(3, 3);
  316. }
  317. static int test_mat_pixel_4()
  318. {
  319. return 0
  320. || test_mat_pixel_roi_gray(16, 16, 1, 1, 13, 13)
  321. || test_mat_pixel_roi_rgb(16, 16, 2, 1, 11, 11)
  322. || test_mat_pixel_roi_bgr(16, 16, 1, 2, 11, 9)
  323. || test_mat_pixel_roi_rgba(16, 16, 3, 2, 9, 11)
  324. || test_mat_pixel_roi_bgra(16, 16, 2, 3, 9, 7);
  325. }
  326. static int test_mat_pixel_5()
  327. {
  328. return 0
  329. || test_mat_pixel_roi_gray(15, 15, 2, 3, 2, 3)
  330. || test_mat_pixel_roi_rgb(15, 15, 3, 4, 5, 4)
  331. || test_mat_pixel_roi_bgr(15, 15, 4, 5, 6, 7)
  332. || test_mat_pixel_roi_rgba(15, 15, 6, 6, 3, 1)
  333. || test_mat_pixel_roi_bgra(15, 15, 7, 3, 1, 1);
  334. }
  335. static int test_mat_pixel_6()
  336. {
  337. return 0
  338. || test_mat_pixel_yuv420sp2rgb(16, 16)
  339. || test_mat_pixel_yuv420sp2rgb(12, 12)
  340. || test_mat_pixel_yuv420sp2rgb(2, 2)
  341. || test_mat_pixel_yuv420sp2rgb(6, 6);
  342. }
  343. int main()
  344. {
  345. SRAND(7767517);
  346. return 0
  347. || test_mat_pixel_0()
  348. || test_mat_pixel_1()
  349. || test_mat_pixel_2()
  350. || test_mat_pixel_3()
  351. || test_mat_pixel_4()
  352. || test_mat_pixel_5()
  353. || test_mat_pixel_6();
  354. }