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_drawing.cpp 24 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. // Copyright 2021 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 int RandomInt(int a, int b)
  10. {
  11. float random = ((float)RAND()) / (float)uint64_t(-1); //RAND_MAX;
  12. int diff = b - a;
  13. float r = random * diff;
  14. return a + (int)r;
  15. }
  16. static int RandomInt2(int a, int b)
  17. {
  18. float random = ((float)RAND()) / (float)uint64_t(-1); //RAND_MAX;
  19. int diff = b - a;
  20. float r = random * diff;
  21. return (a + (int)r + 1) / 2 * 2;
  22. }
  23. static int test_mat_pixel_drawing_c1(int w, int h)
  24. {
  25. ncnn::Mat a(w, h, (size_t)1u, 1);
  26. ncnn::Mat b(h, w, (size_t)1u, 1);
  27. int _color = 0;
  28. unsigned char* color = (unsigned char*)&_color;
  29. // fill with color
  30. color[0] = 255;
  31. ncnn::draw_rectangle_c1(a, w, h, 0, 0, w, h, _color, -1);
  32. ncnn::draw_rectangle_c1(b, h, w, 0, 0, h, w, _color, -1);
  33. // draw rectangle
  34. int rx = RandomInt(0, w);
  35. int ry = RandomInt(0, h);
  36. int rw = RandomInt(0, w - rx);
  37. int rh = RandomInt(0, h - ry);
  38. color[0] = 100;
  39. ncnn::draw_rectangle_c1(a, w, h, rx, ry, rw, rh, _color, 3);
  40. ncnn::draw_rectangle_c1(b, h, w, ry, rx, rh, rw, _color, 3);
  41. // draw filled rectangle out of image
  42. color[0] = 144;
  43. ncnn::draw_rectangle_c1(a, w, h, w - 10, -10, 20, 30, _color, -1);
  44. ncnn::draw_rectangle_c1(b, h, w, -10, w - 10, 30, 20, _color, -1);
  45. color[0] = 166;
  46. ncnn::draw_rectangle_c1(a, w, h, -rw / 2, -rh / 3, rw, rh, _color, 7);
  47. ncnn::draw_rectangle_c1(b, h, w, -rh / 3, -rw / 2, rh, rw, _color, 7);
  48. // draw rectangle out of image
  49. color[0] = 44;
  50. ncnn::draw_rectangle_c1(a, w, h, rx + w / 2, ry + h / 2, rw, rh, _color, 1);
  51. ncnn::draw_rectangle_c1(b, h, w, ry + h / 2, rx + w / 2, rh, rw, _color, 1);
  52. color[0] = 66;
  53. ncnn::draw_rectangle_c1(a, w, h, -rw / 2, -rh / 3, rw, rh, _color, 7);
  54. ncnn::draw_rectangle_c1(b, h, w, -rh / 3, -rw / 2, rh, rw, _color, 7);
  55. // draw filled circle
  56. int cx = RandomInt(0, w);
  57. int cy = RandomInt(0, h);
  58. int radius = RandomInt(0, std::min(w, h));
  59. color[0] = 20;
  60. ncnn::draw_circle_c1(a, w, h, cx, cy, radius, _color, -1);
  61. ncnn::draw_circle_c1(b, h, w, cy, cx, radius, _color, -1);
  62. // draw filled circle out of image
  63. color[0] = 230;
  64. ncnn::draw_circle_c1(a, w, h, 10, -4, 6, _color, -1);
  65. ncnn::draw_circle_c1(b, h, w, -4, 10, 6, _color, -1);
  66. // draw circle out of image
  67. color[0] = 130;
  68. ncnn::draw_circle_c1(a, w, h, cx, cy, radius + std::min(w, h) / 2, _color, 5);
  69. ncnn::draw_circle_c1(b, h, w, cy, cx, radius + std::min(w, h) / 2, _color, 5);
  70. // draw line
  71. int x0 = RandomInt(0, w);
  72. int y0 = RandomInt(0, h);
  73. int x1 = RandomInt(0, w);
  74. int y1 = RandomInt(0, h);
  75. color[0] = 233;
  76. ncnn::draw_line_c1(a, w, h, x0, y0, x1, y1, _color, 7);
  77. ncnn::draw_line_c1(b, h, w, y0, x0, y1, x1, _color, 7);
  78. // draw line out of image
  79. color[0] = 192;
  80. ncnn::draw_line_c1(a, w, h, x0 - w, y0 - h, x1 + w, y1 + h, _color, 1);
  81. ncnn::draw_line_c1(b, h, w, y0 - h, x0 - w, y1 + h, x1 + w, _color, 1);
  82. // transpose b
  83. ncnn::Mat c(w, h, (size_t)1u, 1);
  84. ncnn::kanna_rotate_c1(b, h, w, c, w, h, 5);
  85. // draw text
  86. const char text[] = "saJIEWdl\nj43@o";
  87. int tx = RandomInt(0, w / 2);
  88. int ty = RandomInt(0, h / 2);
  89. int fontpixelsize = 10;
  90. color[0] = 128;
  91. ncnn::draw_text_c1(a, w, h, text, tx, ty, fontpixelsize, _color);
  92. int tw;
  93. int th;
  94. ncnn::get_text_drawing_size(text, fontpixelsize, &tw, &th);
  95. const int len = strlen(text);
  96. for (int i = 0; i < 8; i++)
  97. {
  98. const char ch[2] = {text[i], '\0'};
  99. ncnn::draw_text_c1(c, w, h, ch, tx + tw / 8 * i, ty, fontpixelsize, _color);
  100. }
  101. for (int i = 9; i < len; i++)
  102. {
  103. const char ch[2] = {text[i], '\0'};
  104. ncnn::draw_text_c1(c, w, h, ch, tx + tw / 8 * (i - 9), ty + th / 2, fontpixelsize, _color);
  105. }
  106. // draw text out of image
  107. fontpixelsize = std::max(w, h) / 2;
  108. color[0] = 228;
  109. ncnn::draw_text_c1(a, w, h, "QAQ", -3, -5, fontpixelsize, _color);
  110. ncnn::get_text_drawing_size("QAQ", fontpixelsize, &tw, &th);
  111. ncnn::draw_text_c1(c, w, h, "Q", -3, -5, fontpixelsize, _color);
  112. ncnn::draw_text_c1(c, w, h, "A", -3 + tw / 3, -5, fontpixelsize, _color);
  113. ncnn::draw_text_c1(c, w, h, "Q", -3 + tw / 3 * 2, -5, fontpixelsize, _color);
  114. if (memcmp(a, c, w * h) != 0)
  115. {
  116. fprintf(stderr, "test_mat_pixel_drawing_c1 failed w=%d h=%d\n", w, h);
  117. return -1;
  118. }
  119. return 0;
  120. }
  121. static int test_mat_pixel_drawing_c2(int w, int h)
  122. {
  123. ncnn::Mat a(w, h, (size_t)2u, 2);
  124. ncnn::Mat b(h, w, (size_t)2u, 2);
  125. int _color = 0;
  126. unsigned char* color = (unsigned char*)&_color;
  127. // fill with color
  128. color[0] = 255;
  129. color[1] = 251;
  130. ncnn::draw_rectangle_c2(a, w, h, 0, 0, w, h, _color, -1);
  131. ncnn::draw_rectangle_c2(b, h, w, 0, 0, h, w, _color, -1);
  132. // draw rectangle
  133. int rx = RandomInt(0, w);
  134. int ry = RandomInt(0, h);
  135. int rw = RandomInt(0, w - rx);
  136. int rh = RandomInt(0, h - ry);
  137. color[0] = 100;
  138. color[1] = 130;
  139. ncnn::draw_rectangle_c2(a, w, h, rx, ry, rw, rh, _color, 3);
  140. ncnn::draw_rectangle_c2(b, h, w, ry, rx, rh, rw, _color, 3);
  141. // draw filled rectangle out of image
  142. color[0] = 144;
  143. color[1] = 133;
  144. ncnn::draw_rectangle_c2(a, w, h, w - 10, -10, 20, 30, _color, -1);
  145. ncnn::draw_rectangle_c2(b, h, w, -10, w - 10, 30, 20, _color, -1);
  146. color[0] = 166;
  147. color[1] = 133;
  148. ncnn::draw_rectangle_c2(a, w, h, -rw / 2, -rh / 3, rw, rh, _color, 7);
  149. ncnn::draw_rectangle_c2(b, h, w, -rh / 3, -rw / 2, rh, rw, _color, 7);
  150. // draw rectangle out of image
  151. color[0] = 44;
  152. color[1] = 33;
  153. ncnn::draw_rectangle_c2(a, w, h, rx + w / 2, ry + h / 2, rw, rh, _color, 1);
  154. ncnn::draw_rectangle_c2(b, h, w, ry + h / 2, rx + w / 2, rh, rw, _color, 1);
  155. color[0] = 66;
  156. color[1] = 44;
  157. ncnn::draw_rectangle_c2(a, w, h, -rw / 2, -rh / 3, rw, rh, _color, 7);
  158. ncnn::draw_rectangle_c2(b, h, w, -rh / 3, -rw / 2, rh, rw, _color, 7);
  159. // draw filled circle
  160. int cx = RandomInt(0, w);
  161. int cy = RandomInt(0, h);
  162. int radius = RandomInt(0, std::min(w, h));
  163. color[0] = 20;
  164. color[1] = 120;
  165. ncnn::draw_circle_c2(a, w, h, cx, cy, radius, _color, -1);
  166. ncnn::draw_circle_c2(b, h, w, cy, cx, radius, _color, -1);
  167. // draw filled circle out of image
  168. color[0] = 230;
  169. color[1] = 130;
  170. ncnn::draw_circle_c2(a, w, h, 10, -4, 6, _color, -1);
  171. ncnn::draw_circle_c2(b, h, w, -4, 10, 6, _color, -1);
  172. // draw circle out of image
  173. color[0] = 130;
  174. color[1] = 30;
  175. ncnn::draw_circle_c2(a, w, h, cx, cy, radius + std::min(w, h) / 2, _color, 5);
  176. ncnn::draw_circle_c2(b, h, w, cy, cx, radius + std::min(w, h) / 2, _color, 5);
  177. // draw line
  178. int x0 = RandomInt(0, w);
  179. int y0 = RandomInt(0, h);
  180. int x1 = RandomInt(0, w);
  181. int y1 = RandomInt(0, h);
  182. color[0] = 233;
  183. color[1] = 233;
  184. ncnn::draw_line_c2(a, w, h, x0, y0, x1, y1, _color, 7);
  185. ncnn::draw_line_c2(b, h, w, y0, x0, y1, x1, _color, 7);
  186. // draw line out of image
  187. color[0] = 192;
  188. color[1] = 192;
  189. ncnn::draw_line_c2(a, w, h, x0 - w, y0 - h, x1 + w, y1 + h, _color, 1);
  190. ncnn::draw_line_c2(b, h, w, y0 - h, x0 - w, y1 + h, x1 + w, _color, 1);
  191. // transpose b
  192. ncnn::Mat c(w, h, (size_t)2u, 2);
  193. ncnn::kanna_rotate_c2(b, h, w, c, w, h, 5);
  194. // draw text
  195. const char text[] = "Q`~\\=f\nPN\'/<DSA";
  196. int tx = RandomInt(0, w / 2);
  197. int ty = RandomInt(0, h / 2);
  198. int fontpixelsize = 12;
  199. color[0] = 0;
  200. color[1] = 128;
  201. ncnn::draw_text_c2(a, w, h, text, tx, ty, fontpixelsize, _color);
  202. int tw;
  203. int th;
  204. ncnn::get_text_drawing_size(text, fontpixelsize, &tw, &th);
  205. const int len = strlen(text);
  206. for (int i = 0; i < 6; i++)
  207. {
  208. const char ch[2] = {text[i], '\0'};
  209. ncnn::draw_text_c2(c, w, h, ch, tx + tw / 8 * i, ty, fontpixelsize, _color);
  210. }
  211. for (int i = 7; i < len; i++)
  212. {
  213. const char ch[2] = {text[i], '\0'};
  214. ncnn::draw_text_c2(c, w, h, ch, tx + tw / 8 * (i - 7), ty + th / 2, fontpixelsize, _color);
  215. }
  216. // draw text out of image
  217. fontpixelsize = std::max(w, h) / 3;
  218. color[0] = 228;
  219. color[1] = 0;
  220. ncnn::draw_text_c2(a, w, h, "!@#$%^&", -1, -2, fontpixelsize, _color);
  221. ncnn::get_text_drawing_size("!@#$%^&", fontpixelsize, &tw, &th);
  222. ncnn::draw_text_c2(c, w, h, "!@#", -1, -2, fontpixelsize, _color);
  223. ncnn::draw_text_c2(c, w, h, "$", -1 + tw / 7 * 3, -2, fontpixelsize, _color);
  224. ncnn::draw_text_c2(c, w, h, "%^&", -1 + tw / 7 * 4, -2, fontpixelsize, _color);
  225. if (memcmp(a, c, w * h * 2) != 0)
  226. {
  227. fprintf(stderr, "test_mat_pixel_drawing_c2 failed w=%d h=%d\n", w, h);
  228. return -1;
  229. }
  230. return 0;
  231. }
  232. static int test_mat_pixel_drawing_c3(int w, int h)
  233. {
  234. ncnn::Mat a(w, h, (size_t)3u, 3);
  235. ncnn::Mat b(h, w, (size_t)3u, 3);
  236. int _color = 0;
  237. unsigned char* color = (unsigned char*)&_color;
  238. // fill with color
  239. color[0] = 255;
  240. color[1] = 251;
  241. color[2] = 244;
  242. ncnn::draw_rectangle_c3(a, w, h, 0, 0, w, h, _color, -1);
  243. ncnn::draw_rectangle_c3(b, h, w, 0, 0, h, w, _color, -1);
  244. // draw rectangle
  245. int rx = RandomInt(0, w);
  246. int ry = RandomInt(0, h);
  247. int rw = RandomInt(0, w - rx);
  248. int rh = RandomInt(0, h - ry);
  249. color[0] = 100;
  250. color[1] = 130;
  251. color[2] = 150;
  252. ncnn::draw_rectangle_c3(a, w, h, rx, ry, rw, rh, _color, 3);
  253. ncnn::draw_rectangle_c3(b, h, w, ry, rx, rh, rw, _color, 3);
  254. // draw filled rectangle out of image
  255. color[0] = 144;
  256. color[1] = 133;
  257. color[2] = 122;
  258. ncnn::draw_rectangle_c3(a, w, h, w - 10, -10, 20, 30, _color, -1);
  259. ncnn::draw_rectangle_c3(b, h, w, -10, w - 10, 30, 20, _color, -1);
  260. color[0] = 166;
  261. color[1] = 133;
  262. color[2] = 122;
  263. ncnn::draw_rectangle_c3(a, w, h, -rw / 2, -rh / 3, rw, rh, _color, 7);
  264. ncnn::draw_rectangle_c3(b, h, w, -rh / 3, -rw / 2, rh, rw, _color, 7);
  265. // draw rectangle out of image
  266. color[0] = 44;
  267. color[1] = 33;
  268. color[2] = 22;
  269. ncnn::draw_rectangle_c3(a, w, h, rx + w / 2, ry + h / 2, rw, rh, _color, 1);
  270. ncnn::draw_rectangle_c3(b, h, w, ry + h / 2, rx + w / 2, rh, rw, _color, 1);
  271. color[0] = 66;
  272. color[1] = 44;
  273. color[2] = 33;
  274. ncnn::draw_rectangle_c3(a, w, h, -rw / 2, -rh / 3, rw, rh, _color, 7);
  275. ncnn::draw_rectangle_c3(b, h, w, -rh / 3, -rw / 2, rh, rw, _color, 7);
  276. // draw filled circle
  277. int cx = RandomInt(0, w);
  278. int cy = RandomInt(0, h);
  279. int radius = RandomInt(0, std::min(w, h));
  280. color[0] = 20;
  281. color[1] = 120;
  282. color[2] = 220;
  283. ncnn::draw_circle_c3(a, w, h, cx, cy, radius, _color, -1);
  284. ncnn::draw_circle_c3(b, h, w, cy, cx, radius, _color, -1);
  285. // draw filled circle out of image
  286. color[0] = 230;
  287. color[1] = 130;
  288. color[2] = 110;
  289. ncnn::draw_circle_c3(a, w, h, 10, -4, 6, _color, -1);
  290. ncnn::draw_circle_c3(b, h, w, -4, 10, 6, _color, -1);
  291. // draw circle out of image
  292. color[0] = 130;
  293. color[1] = 30;
  294. color[2] = 230;
  295. ncnn::draw_circle_c3(a, w, h, cx, cy, radius + std::min(w, h) / 2, _color, 5);
  296. ncnn::draw_circle_c3(b, h, w, cy, cx, radius + std::min(w, h) / 2, _color, 5);
  297. // draw line
  298. int x0 = RandomInt(0, w);
  299. int y0 = RandomInt(0, h);
  300. int x1 = RandomInt(0, w);
  301. int y1 = RandomInt(0, h);
  302. color[0] = 233;
  303. color[1] = 233;
  304. color[2] = 233;
  305. ncnn::draw_line_c3(a, w, h, x0, y0, x1, y1, _color, 7);
  306. ncnn::draw_line_c3(b, h, w, y0, x0, y1, x1, _color, 7);
  307. // draw line out of image
  308. color[0] = 192;
  309. color[1] = 192;
  310. color[2] = 0;
  311. ncnn::draw_line_c3(a, w, h, x0 - w, y0 - h, x1 + w, y1 + h, _color, 1);
  312. ncnn::draw_line_c3(b, h, w, y0 - h, x0 - w, y1 + h, x1 + w, _color, 1);
  313. // transpose b
  314. ncnn::Mat c(w, h, (size_t)3u, 3);
  315. ncnn::kanna_rotate_c3(b, h, w, c, w, h, 5);
  316. // draw text
  317. const char text[] = "Q`~\\=f\nPN\'/<DSA";
  318. int tx = RandomInt(0, w / 2);
  319. int ty = RandomInt(0, h / 2);
  320. int fontpixelsize = 12;
  321. color[0] = 0;
  322. color[1] = 128;
  323. color[2] = 128;
  324. ncnn::draw_text_c3(a, w, h, text, tx, ty, fontpixelsize, _color);
  325. int tw;
  326. int th;
  327. ncnn::get_text_drawing_size(text, fontpixelsize, &tw, &th);
  328. const int len = strlen(text);
  329. for (int i = 0; i < 6; i++)
  330. {
  331. const char ch[2] = {text[i], '\0'};
  332. ncnn::draw_text_c3(c, w, h, ch, tx + tw / 8 * i, ty, fontpixelsize, _color);
  333. }
  334. for (int i = 7; i < len; i++)
  335. {
  336. const char ch[2] = {text[i], '\0'};
  337. ncnn::draw_text_c3(c, w, h, ch, tx + tw / 8 * (i - 7), ty + th / 2, fontpixelsize, _color);
  338. }
  339. // draw text out of image
  340. fontpixelsize = std::max(w, h) / 2;
  341. color[0] = 228;
  342. color[1] = 0;
  343. color[2] = 128;
  344. ncnn::draw_text_c3(a, w, h, "qwqwqwq", -13, -15, fontpixelsize, _color);
  345. ncnn::get_text_drawing_size("qwqwqwq", fontpixelsize, &tw, &th);
  346. ncnn::draw_text_c3(c, w, h, "qwq", -13, -15, fontpixelsize, _color);
  347. ncnn::draw_text_c3(c, w, h, "w", -13 + tw / 7 * 3, -15, fontpixelsize, _color);
  348. ncnn::draw_text_c3(c, w, h, "qwq", -13 + tw / 7 * 4, -15, fontpixelsize, _color);
  349. if (memcmp(a, c, w * h * 3) != 0)
  350. {
  351. fprintf(stderr, "test_mat_pixel_drawing_c3 failed w=%d h=%d\n", w, h);
  352. return -1;
  353. }
  354. return 0;
  355. }
  356. static int test_mat_pixel_drawing_c4(int w, int h)
  357. {
  358. ncnn::Mat a(w, h, (size_t)4u, 4);
  359. ncnn::Mat b(h, w, (size_t)4u, 4);
  360. int _color = 0;
  361. unsigned char* color = (unsigned char*)&_color;
  362. // fill with color
  363. color[0] = 255;
  364. color[1] = 255;
  365. color[2] = 255;
  366. color[3] = 0;
  367. ncnn::draw_rectangle_c4(a, w, h, 0, 0, w, h, _color, -1);
  368. ncnn::draw_rectangle_c4(b, h, w, 0, 0, h, w, _color, -1);
  369. // draw rectangle
  370. int rx = RandomInt(0, w);
  371. int ry = RandomInt(0, h);
  372. int rw = RandomInt(0, w - rx);
  373. int rh = RandomInt(0, h - ry);
  374. color[0] = 100;
  375. color[1] = 20;
  376. color[2] = 200;
  377. color[3] = 100;
  378. ncnn::draw_rectangle_c4(a, w, h, rx, ry, rw, rh, _color, 3);
  379. ncnn::draw_rectangle_c4(b, h, w, ry, rx, rh, rw, _color, 3);
  380. // draw filled rectangle out of image
  381. color[0] = 144;
  382. color[1] = 133;
  383. color[2] = 122;
  384. color[3] = 30;
  385. ncnn::draw_rectangle_c4(a, w, h, w - 10, -10, 20, 30, _color, -1);
  386. ncnn::draw_rectangle_c4(b, h, w, -10, w - 10, 30, 20, _color, -1);
  387. color[0] = 166;
  388. color[1] = 133;
  389. color[2] = 122;
  390. color[3] = 20;
  391. ncnn::draw_rectangle_c4(a, w, h, -rw / 2, -rh / 3, rw, rh, _color, 7);
  392. ncnn::draw_rectangle_c4(b, h, w, -rh / 3, -rw / 2, rh, rw, _color, 7);
  393. // draw rectangle out of image
  394. color[0] = 44;
  395. color[1] = 144;
  396. color[2] = 44;
  397. color[3] = 144;
  398. ncnn::draw_rectangle_c4(a, w, h, rx + w / 2, ry + h / 2, rw, rh, _color, 1);
  399. ncnn::draw_rectangle_c4(b, h, w, ry + h / 2, rx + w / 2, rh, rw, _color, 1);
  400. color[0] = 66;
  401. color[1] = 44;
  402. color[2] = 33;
  403. color[3] = 112;
  404. ncnn::draw_rectangle_c4(a, w, h, -rw / 2, -rh / 3, rw, rh, _color, 7);
  405. ncnn::draw_rectangle_c4(b, h, w, -rh / 3, -rw / 2, rh, rw, _color, 7);
  406. // draw filled circle
  407. int cx = RandomInt(0, w);
  408. int cy = RandomInt(0, h);
  409. int radius = RandomInt(0, std::min(w, h));
  410. color[0] = 10;
  411. color[1] = 2;
  412. color[2] = 200;
  413. color[3] = 20;
  414. ncnn::draw_circle_c4(a, w, h, cx, cy, radius, _color, -1);
  415. ncnn::draw_circle_c4(b, h, w, cy, cx, radius, _color, -1);
  416. // draw filled circle out of image
  417. color[0] = 230;
  418. color[1] = 130;
  419. color[2] = 110;
  420. color[3] = 5;
  421. ncnn::draw_circle_c4(a, w, h, 10, -4, 6, _color, -1);
  422. ncnn::draw_circle_c4(b, h, w, -4, 10, 6, _color, -1);
  423. // draw circle out of image
  424. color[0] = 130;
  425. color[1] = 255;
  426. color[2] = 130;
  427. color[3] = 255;
  428. ncnn::draw_circle_c4(a, w, h, cx, cy, radius + std::min(w, h) / 2, _color, 5);
  429. ncnn::draw_circle_c4(b, h, w, cy, cx, radius + std::min(w, h) / 2, _color, 5);
  430. // draw line
  431. int x0 = RandomInt(0, w);
  432. int y0 = RandomInt(0, h);
  433. int x1 = RandomInt(0, w);
  434. int y1 = RandomInt(0, h);
  435. color[0] = 233;
  436. color[1] = 233;
  437. color[2] = 233;
  438. color[3] = 233;
  439. ncnn::draw_line_c4(a, w, h, x0, y0, x1, y1, _color, 7);
  440. ncnn::draw_line_c4(b, h, w, y0, x0, y1, x1, _color, 7);
  441. // draw line out of image
  442. color[0] = 192;
  443. color[1] = 22;
  444. color[2] = 1;
  445. color[3] = 0;
  446. ncnn::draw_line_c4(a, w, h, x0 - w, y0 - h, x1 + w, y1 + h, _color, 1);
  447. ncnn::draw_line_c4(b, h, w, y0 - h, x0 - w, y1 + h, x1 + w, _color, 1);
  448. // transpose b
  449. ncnn::Mat c(w, h, (size_t)4u, 4);
  450. ncnn::kanna_rotate_c4(b, h, w, c, w, h, 5);
  451. // draw text
  452. const char text[] = "!@)\n($ 34\n2]\"M,";
  453. int tx = RandomInt(0, w / 2);
  454. int ty = RandomInt(0, h / 2);
  455. int fontpixelsize = 23;
  456. color[0] = 11;
  457. color[1] = 128;
  458. color[2] = 12;
  459. color[3] = 128;
  460. ncnn::draw_text_c4(a, w, h, text, tx, ty, fontpixelsize, _color);
  461. int tw;
  462. int th;
  463. ncnn::get_text_drawing_size(text, fontpixelsize, &tw, &th);
  464. const int len = strlen(text);
  465. for (int i = 0; i < 3; i++)
  466. {
  467. const char ch[2] = {text[i], '\0'};
  468. ncnn::draw_text_c4(c, w, h, ch, tx + tw / 5 * i, ty, fontpixelsize, _color);
  469. }
  470. for (int i = 4; i < 9; i++)
  471. {
  472. const char ch[2] = {text[i], '\0'};
  473. ncnn::draw_text_c4(c, w, h, ch, tx + tw / 5 * (i - 4), ty + th / 3, fontpixelsize, _color);
  474. }
  475. for (int i = 10; i < len; i++)
  476. {
  477. const char ch[2] = {text[i], '\0'};
  478. ncnn::draw_text_c4(c, w, h, ch, tx + tw / 5 * (i - 10), ty + th / 3 * 2, fontpixelsize, _color);
  479. }
  480. // draw text out of image
  481. fontpixelsize = std::max(w, h) / 3;
  482. color[0] = 228;
  483. color[1] = 0;
  484. color[2] = 128;
  485. color[3] = 200;
  486. ncnn::draw_text_c4(a, w, h, "=_+!//zzzz", -13, -15, fontpixelsize, _color);
  487. ncnn::get_text_drawing_size("=_+!//zzzz", fontpixelsize, &tw, &th);
  488. ncnn::draw_text_c4(c, w, h, "=_+", -13, -15, fontpixelsize, _color);
  489. ncnn::draw_text_c4(c, w, h, "!", -13 + tw / 10 * 3, -15, fontpixelsize, _color);
  490. ncnn::draw_text_c4(c, w, h, "//zzzz", -13 + tw / 10 * 4, -15, fontpixelsize, _color);
  491. if (memcmp(a, c, w * h * 4) != 0)
  492. {
  493. fprintf(stderr, "test_mat_pixel_drawing_c4 failed w=%d h=%d\n", w, h);
  494. return -1;
  495. }
  496. return 0;
  497. }
  498. static int test_mat_pixel_drawing_0()
  499. {
  500. return 0
  501. || test_mat_pixel_drawing_c1(22, 33)
  502. || test_mat_pixel_drawing_c2(22, 23)
  503. || test_mat_pixel_drawing_c3(32, 23)
  504. || test_mat_pixel_drawing_c4(42, 13)
  505. || test_mat_pixel_drawing_c1(202, 303)
  506. || test_mat_pixel_drawing_c2(202, 203)
  507. || test_mat_pixel_drawing_c3(302, 203)
  508. || test_mat_pixel_drawing_c4(402, 103);
  509. }
  510. static int test_mat_pixel_drawing_yuv420sp(int w, int h)
  511. {
  512. ncnn::Mat a(w, h * 3 / 2, (size_t)1u, 1);
  513. ncnn::Mat b(h, w * 3 / 2, (size_t)1u, 1);
  514. int _color = 0;
  515. unsigned char* color = (unsigned char*)&_color;
  516. // fill with color
  517. color[0] = 255;
  518. color[1] = 255;
  519. color[2] = 255;
  520. ncnn::draw_rectangle_yuv420sp(a, w, h, 0, 0, w, h, _color, -1);
  521. ncnn::draw_rectangle_yuv420sp(b, h, w, 0, 0, h, w, _color, -1);
  522. // draw rectangle
  523. int rx = RandomInt2(0, w);
  524. int ry = RandomInt2(0, h);
  525. int rw = RandomInt2(0, w - rx);
  526. int rh = RandomInt2(0, h - ry);
  527. color[0] = 100;
  528. color[1] = 20;
  529. color[2] = 200;
  530. ncnn::draw_rectangle_yuv420sp(a, w, h, rx, ry, rw, rh, _color, 4);
  531. ncnn::draw_rectangle_yuv420sp(b, h, w, ry, rx, rh, rw, _color, 4);
  532. // draw filled rectangle out of image
  533. color[0] = 144;
  534. color[1] = 133;
  535. color[2] = 122;
  536. ncnn::draw_rectangle_yuv420sp(a, w, h, w - 10, -10, 20, 30, _color, -1);
  537. ncnn::draw_rectangle_yuv420sp(b, h, w, -10, w - 10, 30, 20, _color, -1);
  538. color[0] = 166;
  539. color[1] = 133;
  540. color[2] = 122;
  541. ncnn::draw_rectangle_yuv420sp(a, w, h, -rw / 2, -rh / 3, rw, rh, _color, 8);
  542. ncnn::draw_rectangle_yuv420sp(b, h, w, -rh / 3, -rw / 2, rh, rw, _color, 8);
  543. // draw rectangle out of image
  544. color[0] = 44;
  545. color[1] = 144;
  546. color[2] = 44;
  547. ncnn::draw_rectangle_yuv420sp(a, w, h, rx + w / 2, ry + h / 2, rw, rh, _color, 2);
  548. ncnn::draw_rectangle_yuv420sp(b, h, w, ry + h / 2, rx + w / 2, rh, rw, _color, 2);
  549. color[0] = 66;
  550. color[1] = 44;
  551. color[2] = 33;
  552. ncnn::draw_rectangle_yuv420sp(a, w, h, -rw / 2, -rh / 3, rw, rh, _color, 8);
  553. ncnn::draw_rectangle_yuv420sp(b, h, w, -rh / 3, -rw / 2, rh, rw, _color, 8);
  554. // draw filled circle
  555. int cx = RandomInt2(0, w);
  556. int cy = RandomInt2(0, h);
  557. int radius = RandomInt2(0, std::min(w, h));
  558. color[0] = 10;
  559. color[1] = 2;
  560. color[2] = 200;
  561. ncnn::draw_circle_yuv420sp(a, w, h, cx, cy, radius, _color, -1);
  562. ncnn::draw_circle_yuv420sp(b, h, w, cy, cx, radius, _color, -1);
  563. // draw filled circle out of image
  564. color[0] = 230;
  565. color[1] = 130;
  566. color[2] = 110;
  567. ncnn::draw_circle_yuv420sp(a, w, h, 10, -4, 6, _color, -1);
  568. ncnn::draw_circle_yuv420sp(b, h, w, -4, 10, 6, _color, -1);
  569. // draw circle out of image
  570. color[0] = 130;
  571. color[1] = 255;
  572. color[2] = 130;
  573. ncnn::draw_circle_yuv420sp(a, w, h, cx, cy, radius + std::min(w, h) / 2, _color, 6);
  574. ncnn::draw_circle_yuv420sp(b, h, w, cy, cx, radius + std::min(w, h) / 2, _color, 6);
  575. // draw line
  576. int x0 = RandomInt2(0, w);
  577. int y0 = RandomInt2(0, h);
  578. int x1 = RandomInt2(0, w);
  579. int y1 = RandomInt2(0, h);
  580. color[0] = 233;
  581. color[1] = 233;
  582. color[2] = 233;
  583. ncnn::draw_line_yuv420sp(a, w, h, x0, y0, x1, y1, _color, 8);
  584. ncnn::draw_line_yuv420sp(b, h, w, y0, x0, y1, x1, _color, 8);
  585. // draw line out of image
  586. color[0] = 192;
  587. color[1] = 22;
  588. color[2] = 1;
  589. ncnn::draw_line_yuv420sp(a, w, h, x0 - w, y0 - h, x1 + w, y1 + h, _color, 2);
  590. ncnn::draw_line_yuv420sp(b, h, w, y0 - h, x0 - w, y1 + h, x1 + w, _color, 2);
  591. // transpose b
  592. ncnn::Mat c(w, h * 3 / 2, (size_t)1u, 1);
  593. ncnn::kanna_rotate_yuv420sp(b, h, w, c, w, h, 5);
  594. // draw text
  595. const char text[] = "!@)\n($ 34\n2]\"M,";
  596. int tx = RandomInt2(0, w / 2);
  597. int ty = RandomInt2(0, h / 2);
  598. int fontpixelsize = 24;
  599. color[0] = 11;
  600. color[1] = 128;
  601. color[2] = 12;
  602. ncnn::draw_text_yuv420sp(a, w, h, text, tx, ty, fontpixelsize, _color);
  603. int tw;
  604. int th;
  605. ncnn::get_text_drawing_size(text, fontpixelsize, &tw, &th);
  606. const int len = strlen(text);
  607. for (int i = 0; i < 3; i++)
  608. {
  609. const char ch[2] = {text[i], '\0'};
  610. ncnn::draw_text_yuv420sp(c, w, h, ch, tx + tw / 5 * i, ty, fontpixelsize, _color);
  611. }
  612. for (int i = 4; i < 9; i++)
  613. {
  614. const char ch[2] = {text[i], '\0'};
  615. ncnn::draw_text_yuv420sp(c, w, h, ch, tx + tw / 5 * (i - 4), ty + th / 3, fontpixelsize, _color);
  616. }
  617. for (int i = 10; i < len; i++)
  618. {
  619. const char ch[2] = {text[i], '\0'};
  620. ncnn::draw_text_yuv420sp(c, w, h, ch, tx + tw / 5 * (i - 10), ty + th / 3 * 2, fontpixelsize, _color);
  621. }
  622. // draw text out of image
  623. fontpixelsize = (std::max(w, h) / 3 + 1) / 2 * 2;
  624. color[0] = 228;
  625. color[1] = 0;
  626. color[2] = 128;
  627. ncnn::draw_text_yuv420sp(a, w, h, "=_+!//zzzz", -14, -12, fontpixelsize, _color);
  628. ncnn::get_text_drawing_size("=_+!//zzzz", fontpixelsize, &tw, &th);
  629. ncnn::draw_text_yuv420sp(c, w, h, "=_+", -14, -12, fontpixelsize, _color);
  630. ncnn::draw_text_yuv420sp(c, w, h, "!", -14 + tw / 10 * 3, -12, fontpixelsize, _color);
  631. ncnn::draw_text_yuv420sp(c, w, h, "//zzzz", -14 + tw / 10 * 4, -12, fontpixelsize, _color);
  632. if (memcmp(a, c, w * h * 3 / 2) != 0)
  633. {
  634. fprintf(stderr, "test_mat_pixel_drawing_yuv420sp failed w=%d h=%d\n", w, h);
  635. return -1;
  636. }
  637. return 0;
  638. }
  639. static int test_mat_pixel_drawing_1()
  640. {
  641. return 0
  642. || test_mat_pixel_drawing_yuv420sp(10, 10)
  643. || test_mat_pixel_drawing_yuv420sp(120, 160)
  644. || test_mat_pixel_drawing_yuv420sp(220, 340);
  645. }
  646. int main()
  647. {
  648. SRAND(7767517);
  649. return 0
  650. || test_mat_pixel_drawing_0()
  651. || test_mat_pixel_drawing_1();
  652. }