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 25 kB

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