Elffuss BraidHub

kikocis/ncnn public

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