1
// Tencent is pleased to support the open source community by making ncnn available.
3
// Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
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
8
// https://opensource.org/licenses/BSD-3-Clause
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.
34
#include "mat_pixel_drawing_font.h"
36
void draw_rectangle_c1(unsigned char* pixels, int w, int h, int rx, int ry, int rw, int rh, unsigned int color, int thickness)
38
return draw_rectangle_c1(pixels, w, h, w, rx, ry, rw, rh, color, thickness);
41
void draw_rectangle_c2(unsigned char* pixels, int w, int h, int rx, int ry, int rw, int rh, unsigned int color, int thickness)
43
return draw_rectangle_c2(pixels, w, h, w * 2, rx, ry, rw, rh, color, thickness);
46
void draw_rectangle_c3(unsigned char* pixels, int w, int h, int rx, int ry, int rw, int rh, unsigned int color, int thickness)
48
return draw_rectangle_c3(pixels, w, h, w * 3, rx, ry, rw, rh, color, thickness);
51
void draw_rectangle_c4(unsigned char* pixels, int w, int h, int rx, int ry, int rw, int rh, unsigned int color, int thickness)
53
return draw_rectangle_c4(pixels, w, h, w * 4, rx, ry, rw, rh, color, thickness);
56
void draw_rectangle_c1(unsigned char* pixels, int w, int h, int stride, int rx, int ry, int rw, int rh, unsigned int color, int thickness)
58
const unsigned char* pen_color = (const unsigned char*)&color;
63
for (int y = ry; y < ry + rh; y++)
71
unsigned char* p = pixels + stride * y;
73
for (int x = rx; x < rx + rw; x++)
88
const int t0 = thickness / 2;
89
const int t1 = thickness - t0;
93
for (int y = ry - t0; y < ry + t1; y++)
101
unsigned char* p = pixels + stride * y;
103
for (int x = rx - t0; x < rx + rw + t1; x++)
118
for (int y = ry + rh - t0; y < ry + rh + t1; y++)
126
unsigned char* p = pixels + stride * y;
128
for (int x = rx - t0; x < rx + rw + t1; x++)
142
for (int x = rx - t0; x < rx + t1; x++)
150
for (int y = ry + t1; y < ry + rh - t0; y++)
158
unsigned char* p = pixels + stride * y;
165
for (int x = rx + rw - t0; x < rx + rw + t1; x++)
173
for (int y = ry + t1; y < ry + rh - t0; y++)
181
unsigned char* p = pixels + stride * y;
188
void draw_rectangle_c2(unsigned char* pixels, int w, int h, int stride, int rx, int ry, int rw, int rh, unsigned int color, int thickness)
190
const unsigned char* pen_color = (const unsigned char*)&color;
195
for (int y = ry; y < ry + rh; y++)
203
unsigned char* p = pixels + stride * y;
205
for (int x = rx; x < rx + rw; x++)
213
p[x * 2 + 0] = pen_color[0];
214
p[x * 2 + 1] = pen_color[1];
221
const int t0 = thickness / 2;
222
const int t1 = thickness - t0;
226
for (int y = ry - t0; y < ry + t1; y++)
234
unsigned char* p = pixels + stride * y;
236
for (int x = rx - t0; x < rx + rw + t1; x++)
244
p[x * 2 + 0] = pen_color[0];
245
p[x * 2 + 1] = pen_color[1];
252
for (int y = ry + rh - t0; y < ry + rh + t1; y++)
260
unsigned char* p = pixels + stride * y;
262
for (int x = rx - t0; x < rx + rw + t1; x++)
270
p[x * 2 + 0] = pen_color[0];
271
p[x * 2 + 1] = pen_color[1];
277
for (int x = rx - t0; x < rx + t1; x++)
285
for (int y = ry + t1; y < ry + rh - t0; y++)
293
unsigned char* p = pixels + stride * y;
295
p[x * 2 + 0] = pen_color[0];
296
p[x * 2 + 1] = pen_color[1];
301
for (int x = rx + rw - t0; x < rx + rw + t1; x++)
309
for (int y = ry + t1; y < ry + rh - t0; y++)
317
unsigned char* p = pixels + stride * y;
319
p[x * 2 + 0] = pen_color[0];
320
p[x * 2 + 1] = pen_color[1];
325
void draw_rectangle_c3(unsigned char* pixels, int w, int h, int stride, int rx, int ry, int rw, int rh, unsigned int color, int thickness)
327
const unsigned char* pen_color = (const unsigned char*)&color;
332
for (int y = ry; y < ry + rh; y++)
340
unsigned char* p = pixels + stride * y;
342
for (int x = rx; x < rx + rw; x++)
350
p[x * 3 + 0] = pen_color[0];
351
p[x * 3 + 1] = pen_color[1];
352
p[x * 3 + 2] = pen_color[2];
359
const int t0 = thickness / 2;
360
const int t1 = thickness - t0;
364
for (int y = ry - t0; y < ry + t1; y++)
372
unsigned char* p = pixels + stride * y;
374
for (int x = rx - t0; x < rx + rw + t1; x++)
382
p[x * 3 + 0] = pen_color[0];
383
p[x * 3 + 1] = pen_color[1];
384
p[x * 3 + 2] = pen_color[2];
391
for (int y = ry + rh - t0; y < ry + rh + t1; y++)
399
unsigned char* p = pixels + stride * y;
401
for (int x = rx - t0; x < rx + rw + t1; x++)
409
p[x * 3 + 0] = pen_color[0];
410
p[x * 3 + 1] = pen_color[1];
411
p[x * 3 + 2] = pen_color[2];
417
for (int x = rx - t0; x < rx + t1; x++)
425
for (int y = ry + t1; y < ry + rh - t0; y++)
433
unsigned char* p = pixels + stride * y;
435
p[x * 3 + 0] = pen_color[0];
436
p[x * 3 + 1] = pen_color[1];
437
p[x * 3 + 2] = pen_color[2];
442
for (int x = rx + rw - t0; x < rx + rw + t1; x++)
450
for (int y = ry + t1; y < ry + rh - t0; y++)
458
unsigned char* p = pixels + stride * y;
460
p[x * 3 + 0] = pen_color[0];
461
p[x * 3 + 1] = pen_color[1];
462
p[x * 3 + 2] = pen_color[2];
467
void draw_rectangle_c4(unsigned char* pixels, int w, int h, int stride, int rx, int ry, int rw, int rh, unsigned int color, int thickness)
469
const unsigned char* pen_color = (const unsigned char*)&color;
474
for (int y = ry; y < ry + rh; y++)
482
unsigned char* p = pixels + stride * y;
484
for (int x = rx; x < rx + rw; x++)
492
p[x * 4 + 0] = pen_color[0];
493
p[x * 4 + 1] = pen_color[1];
494
p[x * 4 + 2] = pen_color[2];
495
p[x * 4 + 3] = pen_color[3];
502
const int t0 = thickness / 2;
503
const int t1 = thickness - t0;
507
for (int y = ry - t0; y < ry + t1; y++)
515
unsigned char* p = pixels + stride * y;
517
for (int x = rx - t0; x < rx + rw + t1; x++)
525
p[x * 4 + 0] = pen_color[0];
526
p[x * 4 + 1] = pen_color[1];
527
p[x * 4 + 2] = pen_color[2];
528
p[x * 4 + 3] = pen_color[3];
535
for (int y = ry + rh - t0; y < ry + rh + t1; y++)
543
unsigned char* p = pixels + stride * y;
545
for (int x = rx - t0; x < rx + rw + t1; x++)
553
p[x * 4 + 0] = pen_color[0];
554
p[x * 4 + 1] = pen_color[1];
555
p[x * 4 + 2] = pen_color[2];
556
p[x * 4 + 3] = pen_color[3];
562
for (int x = rx - t0; x < rx + t1; x++)
570
for (int y = ry + t1; y < ry + rh - t0; y++)
578
unsigned char* p = pixels + stride * y;
580
p[x * 4 + 0] = pen_color[0];
581
p[x * 4 + 1] = pen_color[1];
582
p[x * 4 + 2] = pen_color[2];
583
p[x * 4 + 3] = pen_color[3];
588
for (int x = rx + rw - t0; x < rx + rw + t1; x++)
596
for (int y = ry + t1; y < ry + rh - t0; y++)
604
unsigned char* p = pixels + stride * y;
606
p[x * 4 + 0] = pen_color[0];
607
p[x * 4 + 1] = pen_color[1];
608
p[x * 4 + 2] = pen_color[2];
609
p[x * 4 + 3] = pen_color[3];
614
void draw_rectangle_yuv420sp(unsigned char* yuv420sp, int w, int h, int rx, int ry, int rw, int rh, unsigned int color, int thickness)
618
// assert rx % 2 == 0
619
// assert ry % 2 == 0
620
// assert rw % 2 == 0
621
// assert rh % 2 == 0
622
// assert thickness % 2 == 0
624
const unsigned char* pen_color = (const unsigned char*)&color;
628
unsigned char* pen_color_y = (unsigned char*)&v_y;
629
unsigned char* pen_color_uv = (unsigned char*)&v_uv;
630
pen_color_y[0] = pen_color[0];
631
pen_color_uv[0] = pen_color[1];
632
pen_color_uv[1] = pen_color[2];
634
unsigned char* Y = yuv420sp;
635
draw_rectangle_c1(Y, w, h, rx, ry, rw, rh, v_y, thickness);
637
unsigned char* UV = yuv420sp + w * h;
638
int thickness_uv = thickness == -1 ? thickness : std::max(thickness / 2, 1);
639
draw_rectangle_c2(UV, w / 2, h / 2, rx / 2, ry / 2, rw / 2, rh / 2, v_uv, thickness_uv);
642
static inline bool distance_lessequal(int x0, int y0, int x1, int y1, float r)
646
int q = dx * dx + dy * dy;
650
static inline bool distance_inrange(int x0, int y0, int x1, int y1, float r0, float r1)
654
int q = dx * dx + dy * dy;
655
return q >= r0 * r0 && q < r1 * r1;
658
void draw_circle_c1(unsigned char* pixels, int w, int h, int cx, int cy, int radius, unsigned int color, int thickness)
660
return draw_circle_c1(pixels, w, h, w, cx, cy, radius, color, thickness);
663
void draw_circle_c2(unsigned char* pixels, int w, int h, int cx, int cy, int radius, unsigned int color, int thickness)
665
return draw_circle_c2(pixels, w, h, w * 2, cx, cy, radius, color, thickness);
668
void draw_circle_c3(unsigned char* pixels, int w, int h, int cx, int cy, int radius, unsigned int color, int thickness)
670
return draw_circle_c3(pixels, w, h, w * 3, cx, cy, radius, color, thickness);
673
void draw_circle_c4(unsigned char* pixels, int w, int h, int cx, int cy, int radius, unsigned int color, int thickness)
675
return draw_circle_c4(pixels, w, h, w * 4, cx, cy, radius, color, thickness);
678
void draw_circle_c1(unsigned char* pixels, int w, int h, int stride, int cx, int cy, int radius, unsigned int color, int thickness)
680
const unsigned char* pen_color = (const unsigned char*)&color;
685
for (int y = cy - (radius - 1); y < cy + radius; y++)
693
unsigned char* p = pixels + stride * y;
695
for (int x = cx - (radius - 1); x < cx + radius; x++)
703
// distance from cx cy
704
if (distance_lessequal(x, y, cx, cy, radius))
714
const float t0 = thickness / 2.f;
715
const float t1 = thickness - t0;
717
for (int y = cy - (radius - 1) - t0; y < cy + radius + t1; y++)
725
unsigned char* p = pixels + stride * y;
727
for (int x = cx - (radius - 1) - t0; x < cx + radius + t1; x++)
735
// distance from cx cy
736
if (distance_inrange(x, y, cx, cy, radius - t0, radius + t1))
744
void draw_circle_c2(unsigned char* pixels, int w, int h, int stride, int cx, int cy, int radius, unsigned int color, int thickness)
746
const unsigned char* pen_color = (const unsigned char*)&color;
751
for (int y = cy - (radius - 1); y < cy + radius; y++)
759
unsigned char* p = pixels + stride * y;
761
for (int x = cx - (radius - 1); x < cx + radius; x++)
769
// distance from cx cy
770
if (distance_lessequal(x, y, cx, cy, radius))
772
p[x * 2 + 0] = pen_color[0];
773
p[x * 2 + 1] = pen_color[1];
781
const float t0 = thickness / 2.f;
782
const float t1 = thickness - t0;
784
for (int y = cy - radius - t0; y < cy + radius + t1; y++)
792
unsigned char* p = pixels + stride * y;
794
for (int x = cx - radius - t0; x < cx + radius + t1; x++)
802
// distance from cx cy
803
if (distance_inrange(x, y, cx, cy, radius - t0, radius + t1))
805
p[x * 2 + 0] = pen_color[0];
806
p[x * 2 + 1] = pen_color[1];
812
void draw_circle_c3(unsigned char* pixels, int w, int h, int stride, int cx, int cy, int radius, unsigned int color, int thickness)
814
const unsigned char* pen_color = (const unsigned char*)&color;
819
for (int y = cy - (radius - 1); y < cy + radius; y++)
827
unsigned char* p = pixels + stride * y;
829
for (int x = cx - (radius - 1); x < cx + radius; x++)
837
// distance from cx cy
838
if (distance_lessequal(x, y, cx, cy, radius))
840
p[x * 3 + 0] = pen_color[0];
841
p[x * 3 + 1] = pen_color[1];
842
p[x * 3 + 2] = pen_color[2];
850
const float t0 = thickness / 2.f;
851
const float t1 = thickness - t0;
853
for (int y = cy - radius - t0; y < cy + radius + t1; y++)
861
unsigned char* p = pixels + stride * y;
863
for (int x = cx - radius - t0; x < cx + radius + t1; x++)
871
// distance from cx cy
872
if (distance_inrange(x, y, cx, cy, radius - t0, radius + t1))
874
p[x * 3 + 0] = pen_color[0];
875
p[x * 3 + 1] = pen_color[1];
876
p[x * 3 + 2] = pen_color[2];
882
void draw_circle_c4(unsigned char* pixels, int w, int h, int stride, int cx, int cy, int radius, unsigned int color, int thickness)
884
const unsigned char* pen_color = (const unsigned char*)&color;
889
for (int y = cy - (radius - 1); y < cy + radius; y++)
897
unsigned char* p = pixels + stride * y;
899
for (int x = cx - (radius - 1); x < cx + radius; x++)
907
// distance from cx cy
908
if (distance_lessequal(x, y, cx, cy, radius))
910
p[x * 4 + 0] = pen_color[0];
911
p[x * 4 + 1] = pen_color[1];
912
p[x * 4 + 2] = pen_color[2];
913
p[x * 4 + 3] = pen_color[3];
921
const float t0 = thickness / 2.f;
922
const float t1 = thickness - t0;
924
for (int y = cy - (radius - 1) - t0; y < cy + radius + t1; y++)
932
unsigned char* p = pixels + stride * y;
934
for (int x = cx - (radius - 1) - t0; x < cx + radius + t1; x++)
942
// distance from cx cy
943
if (distance_inrange(x, y, cx, cy, radius - t0, radius + t1))
945
p[x * 4 + 0] = pen_color[0];
946
p[x * 4 + 1] = pen_color[1];
947
p[x * 4 + 2] = pen_color[2];
948
p[x * 4 + 3] = pen_color[3];
954
void draw_circle_yuv420sp(unsigned char* yuv420sp, int w, int h, int cx, int cy, int radius, unsigned int color, int thickness)
958
// assert cx % 2 == 0
959
// assert cy % 2 == 0
960
// assert radius % 2 == 0
961
// assert thickness % 2 == 0
963
const unsigned char* pen_color = (const unsigned char*)&color;
967
unsigned char* pen_color_y = (unsigned char*)&v_y;
968
unsigned char* pen_color_uv = (unsigned char*)&v_uv;
969
pen_color_y[0] = pen_color[0];
970
pen_color_uv[0] = pen_color[1];
971
pen_color_uv[1] = pen_color[2];
973
unsigned char* Y = yuv420sp;
974
draw_circle_c1(Y, w, h, cx, cy, radius, v_y, thickness);
976
unsigned char* UV = yuv420sp + w * h;
977
int thickness_uv = thickness == -1 ? thickness : std::max(thickness / 2, 1);
978
draw_circle_c2(UV, w / 2, h / 2, cx / 2, cy / 2, radius / 2, v_uv, thickness_uv);
981
static inline bool distance_lessthan(int x, int y, int x0, int y0, int x1, int y1, float t)
988
float r = (float)(dx0 * dx01 + dy0 * dy01) / (dx01 * dx01 + dy01 * dy01);
993
float px = x0 + dx01 * r;
994
float py = y0 + dy01 * r;
997
float p = dx * dx + dy * dy;
1001
void draw_line_c1(unsigned char* pixels, int w, int h, int x0, int y0, int x1, int y1, unsigned int color, int thickness)
1003
draw_line_c1(pixels, w, h, w, x0, y0, x1, y1, color, thickness);
1006
void draw_line_c2(unsigned char* pixels, int w, int h, int x0, int y0, int x1, int y1, unsigned int color, int thickness)
1008
draw_line_c2(pixels, w, h, w * 2, x0, y0, x1, y1, color, thickness);
1011
void draw_line_c3(unsigned char* pixels, int w, int h, int x0, int y0, int x1, int y1, unsigned int color, int thickness)
1013
draw_line_c3(pixels, w, h, w * 3, x0, y0, x1, y1, color, thickness);
1016
void draw_line_c4(unsigned char* pixels, int w, int h, int x0, int y0, int x1, int y1, unsigned int color, int thickness)
1018
draw_line_c4(pixels, w, h, w * 4, x0, y0, x1, y1, color, thickness);
1021
void draw_line_c1(unsigned char* pixels, int w, int h, int stride, int x0, int y0, int x1, int y1, unsigned int color, int thickness)
1023
const unsigned char* pen_color = (const unsigned char*)&color;
1025
const float t0 = thickness / 2.f;
1026
const float t1 = thickness - t0;
1028
int x_min = std::min(x0, x1);
1029
int x_max = std::max(x0, x1);
1030
int y_min = std::min(y0, y1);
1031
int y_max = std::max(y0, y1);
1033
for (int y = y_min - t0; y < y_max + t1; y++)
1041
unsigned char* p = pixels + stride * y;
1043
for (int x = x_min - t0; x < x_max + t1; x++)
1051
// distance from line
1052
if (distance_lessthan(x, y, x0, y0, x1, y1, t1))
1054
p[x] = pen_color[0];
1060
void draw_line_c2(unsigned char* pixels, int w, int h, int stride, int x0, int y0, int x1, int y1, unsigned int color, int thickness)
1062
const unsigned char* pen_color = (const unsigned char*)&color;
1064
const float t0 = thickness / 2.f;
1065
const float t1 = thickness - t0;
1067
int x_min = std::min(x0, x1);
1068
int x_max = std::max(x0, x1);
1069
int y_min = std::min(y0, y1);
1070
int y_max = std::max(y0, y1);
1072
for (int y = y_min - t0; y < y_max + t1; y++)
1080
unsigned char* p = pixels + stride * y;
1082
for (int x = x_min - t0; x < x_max + t1; x++)
1090
// distance from line
1091
if (distance_lessthan(x, y, x0, y0, x1, y1, t1))
1093
p[x * 2 + 0] = pen_color[0];
1094
p[x * 2 + 1] = pen_color[1];
1100
void draw_line_c3(unsigned char* pixels, int w, int h, int stride, int x0, int y0, int x1, int y1, unsigned int color, int thickness)
1102
const unsigned char* pen_color = (const unsigned char*)&color;
1104
const float t0 = thickness / 2.f;
1105
const float t1 = thickness - t0;
1107
int x_min = std::min(x0, x1);
1108
int x_max = std::max(x0, x1);
1109
int y_min = std::min(y0, y1);
1110
int y_max = std::max(y0, y1);
1112
for (int y = y_min - t0; y < y_max + t1; y++)
1120
unsigned char* p = pixels + stride * y;
1122
for (int x = x_min - t0; x < x_max + t1; x++)
1130
// distance from line
1131
if (distance_lessthan(x, y, x0, y0, x1, y1, t1))
1133
p[x * 3 + 0] = pen_color[0];
1134
p[x * 3 + 1] = pen_color[1];
1135
p[x * 3 + 2] = pen_color[2];
1141
void draw_line_c4(unsigned char* pixels, int w, int h, int stride, int x0, int y0, int x1, int y1, unsigned int color, int thickness)
1143
const unsigned char* pen_color = (const unsigned char*)&color;
1145
const float t0 = thickness / 2.f;
1146
const float t1 = thickness - t0;
1148
int x_min = std::min(x0, x1);
1149
int x_max = std::max(x0, x1);
1150
int y_min = std::min(y0, y1);
1151
int y_max = std::max(y0, y1);
1153
for (int y = y_min - t0; y < y_max + t1; y++)
1161
unsigned char* p = pixels + stride * y;
1163
for (int x = x_min - t0; x < x_max + t1; x++)
1171
// distance from line
1172
if (distance_lessthan(x, y, x0, y0, x1, y1, t1))
1174
p[x * 4 + 0] = pen_color[0];
1175
p[x * 4 + 1] = pen_color[1];
1176
p[x * 4 + 2] = pen_color[2];
1177
p[x * 4 + 3] = pen_color[3];
1183
void draw_line_yuv420sp(unsigned char* yuv420sp, int w, int h, int x0, int y0, int x1, int y1, unsigned int color, int thickness)
1185
// assert w % 2 == 0
1186
// assert h % 2 == 0
1187
// assert x0 % 2 == 0
1188
// assert y0 % 2 == 0
1189
// assert x1 % 2 == 0
1190
// assert y1 % 2 == 0
1191
// assert thickness % 2 == 0
1193
const unsigned char* pen_color = (const unsigned char*)&color;
1197
unsigned char* pen_color_y = (unsigned char*)&v_y;
1198
unsigned char* pen_color_uv = (unsigned char*)&v_uv;
1199
pen_color_y[0] = pen_color[0];
1200
pen_color_uv[0] = pen_color[1];
1201
pen_color_uv[1] = pen_color[2];
1203
unsigned char* Y = yuv420sp;
1204
draw_line_c1(Y, w, h, x0, y0, x1, y1, v_y, thickness);
1206
unsigned char* UV = yuv420sp + w * h;
1207
int thickness_uv = thickness == -1 ? thickness : std::max(thickness / 2, 1);
1208
draw_line_c2(UV, w / 2, h / 2, x0 / 2, y0 / 2, x1 / 2, y1 / 2, v_uv, thickness_uv);
1211
void get_text_drawing_size(const char* text, int fontpixelsize, int* w, int* h)
1216
const int n = strlen(text);
1219
for (int i = 0; i < n; i++)
1226
*w = std::max(*w, line_w);
1227
*h += fontpixelsize * 2;
1231
if (isprint(ch) != 0)
1233
line_w += fontpixelsize;
1237
*w = std::max(*w, line_w);
1238
*h += fontpixelsize * 2;
1241
void draw_text_c1(unsigned char* pixels, int w, int h, const char* text, int x, int y, int fontpixelsize, unsigned int color)
1243
return draw_text_c1(pixels, w, h, w, text, x, y, fontpixelsize, color);
1246
void draw_text_c2(unsigned char* pixels, int w, int h, const char* text, int x, int y, int fontpixelsize, unsigned int color)
1248
return draw_text_c2(pixels, w, h, w * 2, text, x, y, fontpixelsize, color);
1251
void draw_text_c3(unsigned char* pixels, int w, int h, const char* text, int x, int y, int fontpixelsize, unsigned int color)
1253
return draw_text_c3(pixels, w, h, w * 3, text, x, y, fontpixelsize, color);
1256
void draw_text_c4(unsigned char* pixels, int w, int h, const char* text, int x, int y, int fontpixelsize, unsigned int color)
1258
return draw_text_c4(pixels, w, h, w * 4, text, x, y, fontpixelsize, color);
1261
void resize_bilinear_font(const unsigned char* font_bitmap, unsigned char* resized_font_bitmap, int fontpixelsize)
1263
const int INTER_RESIZE_COEF_BITS = 11;
1264
const int INTER_RESIZE_COEF_SCALE = 1 << INTER_RESIZE_COEF_BITS;
1266
const int srcw = 20;
1267
const int srch = 40;
1268
const int w = fontpixelsize;
1269
const int h = fontpixelsize * 2;
1271
double scale = (double)srcw / w;
1273
int* buf = new int[w + h + w + h];
1275
int* xofs = buf; //new int[w];
1276
int* yofs = buf + w; //new int[h];
1278
short* ialpha = (short*)(buf + w + h); //new short[w * 2];
1279
short* ibeta = (short*)(buf + w + h + w); //new short[h * 2];
1286
#define SATURATE_CAST_SHORT(X) (short)::std::min(::std::max((int)(X + (X >= 0.f ? 0.5f : -0.5f)), SHRT_MIN), SHRT_MAX);
1288
for (int dx = 0; dx < w; dx++)
1290
fx = (float)((dx + 0.5) * scale - 0.5);
1291
sx = static_cast<int>(floor(fx));
1296
float a0 = (1.f - fx) * INTER_RESIZE_COEF_SCALE;
1297
float a1 = fx * INTER_RESIZE_COEF_SCALE;
1299
ialpha[dx * 2] = SATURATE_CAST_SHORT(a0);
1300
ialpha[dx * 2 + 1] = SATURATE_CAST_SHORT(a1);
1303
for (int dy = 0; dy < h; dy++)
1305
fy = (float)((dy + 0.5) * scale - 0.5);
1306
sy = static_cast<int>(floor(fy));
1311
float b0 = (1.f - fy) * INTER_RESIZE_COEF_SCALE;
1312
float b1 = fy * INTER_RESIZE_COEF_SCALE;
1314
ibeta[dy * 2] = SATURATE_CAST_SHORT(b0);
1315
ibeta[dy * 2 + 1] = SATURATE_CAST_SHORT(b1);
1318
#undef SATURATE_CAST_SHORT
1321
Mat rowsbuf0(w, (size_t)2u);
1322
Mat rowsbuf1(w, (size_t)2u);
1323
short* rows0 = (short*)rowsbuf0;
1324
short* rows1 = (short*)rowsbuf1;
1327
short* rows1p = rows1;
1328
for (int dx = 0; dx < w; dx++)
1336
for (int dy = 0; dy < h; dy++)
1344
else if (sy == prev_sy1 + 1)
1347
short* rows0_old = rows0;
1350
const unsigned char* S1 = font_bitmap + 10 * (sy + 1);
1354
short* rows1p = rows1;
1355
for (int dx = 0; dx < w; dx++)
1362
const short* ialphap = ialpha;
1363
short* rows1p = rows1;
1364
for (int dx = 0; dx < w; dx++)
1367
short a0 = ialphap[0];
1368
short a1 = ialphap[1];
1376
S1p1 = S1[0] & 0x0f;
1378
else if (sx >= srcw - 1)
1380
S1p0 = (S1[9] & 0xf0) >> 4;
1385
S1p0 = sx % 2 == 0 ? S1[sx / 2] & 0x0f : (S1[sx / 2] & 0xf0) >> 4;
1386
S1p1 = sx % 2 == 0 ? (S1[sx / 2] & 0xf0) >> 4 : S1[sx / 2 + 1] & 0x0f;
1388
rows1p[dx] = (S1p0 * a0 + S1p1 * a1) * 17 >> 4;
1397
const unsigned char* S0 = font_bitmap + 10 * (sy);
1398
const unsigned char* S1 = font_bitmap + 10 * (sy + 1);
1402
const short* ialphap = ialpha;
1403
short* rows0p = rows0;
1404
short* rows1p = rows1;
1405
for (int dx = 0; dx < w; dx++)
1408
short a0 = ialphap[0];
1409
short a1 = ialphap[1];
1417
S0p1 = S0[0] & 0x0f;
1419
else if (sx >= srcw - 1)
1421
S0p0 = (S0[9] & 0xf0) >> 4;
1426
S0p0 = sx % 2 == 0 ? S0[sx / 2] & 0x0f : (S0[sx / 2] & 0xf0) >> 4;
1427
S0p1 = sx % 2 == 0 ? (S0[sx / 2] & 0xf0) >> 4 : S0[sx / 2 + 1] & 0x0f;
1429
rows0p[dx] = (S0p0 * a0 + S0p1 * a1) * 17 >> 4;
1437
const short* ialphap = ialpha;
1438
short* rows0p = rows0;
1439
short* rows1p = rows1;
1440
for (int dx = 0; dx < w; dx++)
1443
short a0 = ialphap[0];
1444
short a1 = ialphap[1];
1454
S0p1 = S0[0] & 0x0f;
1456
S1p1 = S1[0] & 0x0f;
1458
else if (sx >= srcw - 1)
1460
S0p0 = (S0[9] & 0xf0) >> 4;
1462
S1p0 = (S1[9] & 0xf0) >> 4;
1467
S0p0 = sx % 2 == 0 ? S0[sx / 2] & 0x0f : (S0[sx / 2] & 0xf0) >> 4;
1468
S0p1 = sx % 2 == 0 ? (S0[sx / 2] & 0xf0) >> 4 : S0[sx / 2 + 1] & 0x0f;
1469
S1p0 = sx % 2 == 0 ? S1[sx / 2] & 0x0f : (S1[sx / 2] & 0xf0) >> 4;
1470
S1p1 = sx % 2 == 0 ? (S1[sx / 2] & 0xf0) >> 4 : S1[sx / 2 + 1] & 0x0f;
1472
rows0p[dx] = (S0p0 * a0 + S0p1 * a1) * 17 >> 4;
1473
rows1p[dx] = (S1p0 * a0 + S1p1 * a1) * 17 >> 4;
1482
if (dy + 1 < h && yofs[dy + 1] == sy)
1484
// vresize for two rows
1485
short b0 = ibeta[0];
1486
short b1 = ibeta[1];
1487
short b2 = ibeta[2];
1488
short b3 = ibeta[3];
1490
short* rows0p = rows0;
1491
short* rows1p = rows1;
1492
unsigned char* Dp0 = resized_font_bitmap + w * (dy);
1493
unsigned char* Dp1 = resized_font_bitmap + w * (dy + 1);
1497
int16x8_t _b0 = vdupq_n_s16(b0);
1498
int16x8_t _b1 = vdupq_n_s16(b1);
1499
int16x8_t _b2 = vdupq_n_s16(b2);
1500
int16x8_t _b3 = vdupq_n_s16(b3);
1501
for (; dx + 15 < w; dx += 16)
1503
int16x8_t _r00 = vld1q_s16(rows0p);
1504
int16x8_t _r01 = vld1q_s16(rows0p + 8);
1505
int16x8_t _r10 = vld1q_s16(rows1p);
1506
int16x8_t _r11 = vld1q_s16(rows1p + 8);
1507
int16x8_t _acc00 = vaddq_s16(vqdmulhq_s16(_r00, _b0), vqdmulhq_s16(_r10, _b1));
1508
int16x8_t _acc01 = vaddq_s16(vqdmulhq_s16(_r01, _b0), vqdmulhq_s16(_r11, _b1));
1509
int16x8_t _acc10 = vaddq_s16(vqdmulhq_s16(_r00, _b2), vqdmulhq_s16(_r10, _b3));
1510
int16x8_t _acc11 = vaddq_s16(vqdmulhq_s16(_r01, _b2), vqdmulhq_s16(_r11, _b3));
1511
uint8x16_t _Dp0 = vcombine_u8(vqrshrun_n_s16(_acc00, 3), vqrshrun_n_s16(_acc01, 3));
1512
uint8x16_t _Dp1 = vcombine_u8(vqrshrun_n_s16(_acc10, 3), vqrshrun_n_s16(_acc11, 3));
1513
vst1q_u8(Dp0, _Dp0);
1514
vst1q_u8(Dp1, _Dp1);
1520
for (; dx + 7 < w; dx += 8)
1522
int16x8_t _r0 = vld1q_s16(rows0p);
1523
int16x8_t _r1 = vld1q_s16(rows1p);
1524
int16x8_t _acc0 = vaddq_s16(vqdmulhq_s16(_r0, _b0), vqdmulhq_s16(_r1, _b1));
1525
int16x8_t _acc1 = vaddq_s16(vqdmulhq_s16(_r0, _b2), vqdmulhq_s16(_r1, _b3));
1526
uint8x8_t _Dp0 = vqrshrun_n_s16(_acc0, 3);
1527
uint8x8_t _Dp1 = vqrshrun_n_s16(_acc1, 3);
1537
__m128i _b0 = _mm_set1_epi16(b0);
1538
__m128i _b1 = _mm_set1_epi16(b1);
1539
__m128i _b2 = _mm_set1_epi16(b2);
1540
__m128i _b3 = _mm_set1_epi16(b3);
1541
__m128i _v2 = _mm_set1_epi16(2);
1542
for (; dx + 15 < w; dx += 16)
1544
__m128i _r00 = _mm_loadu_si128((const __m128i*)rows0p);
1545
__m128i _r01 = _mm_loadu_si128((const __m128i*)(rows0p + 8));
1546
__m128i _r10 = _mm_loadu_si128((const __m128i*)rows1p);
1547
__m128i _r11 = _mm_loadu_si128((const __m128i*)(rows1p + 8));
1548
__m128i _acc00 = _mm_add_epi16(_mm_mulhi_epi16(_r00, _b0), _mm_mulhi_epi16(_r10, _b1));
1549
__m128i _acc01 = _mm_add_epi16(_mm_mulhi_epi16(_r01, _b0), _mm_mulhi_epi16(_r11, _b1));
1550
__m128i _acc10 = _mm_add_epi16(_mm_mulhi_epi16(_r00, _b2), _mm_mulhi_epi16(_r10, _b3));
1551
__m128i _acc11 = _mm_add_epi16(_mm_mulhi_epi16(_r01, _b2), _mm_mulhi_epi16(_r11, _b3));
1552
_acc00 = _mm_srai_epi16(_mm_add_epi16(_acc00, _v2), 2);
1553
_acc01 = _mm_srai_epi16(_mm_add_epi16(_acc01, _v2), 2);
1554
_acc10 = _mm_srai_epi16(_mm_add_epi16(_acc10, _v2), 2);
1555
_acc11 = _mm_srai_epi16(_mm_add_epi16(_acc11, _v2), 2);
1556
__m128i _Dp0 = _mm_packus_epi16(_acc00, _acc01);
1557
__m128i _Dp1 = _mm_packus_epi16(_acc10, _acc11);
1558
_mm_storeu_si128((__m128i*)Dp0, _Dp0);
1559
_mm_storeu_si128((__m128i*)Dp1, _Dp1);
1565
for (; dx + 7 < w; dx += 8)
1567
__m128i _r0 = _mm_loadu_si128((const __m128i*)rows0p);
1568
__m128i _r1 = _mm_loadu_si128((const __m128i*)rows1p);
1569
__m128i _acc0 = _mm_add_epi16(_mm_mulhi_epi16(_r0, _b0), _mm_mulhi_epi16(_r1, _b1));
1570
__m128i _acc1 = _mm_add_epi16(_mm_mulhi_epi16(_r0, _b2), _mm_mulhi_epi16(_r1, _b3));
1571
_acc0 = _mm_srai_epi16(_mm_add_epi16(_acc0, _v2), 2);
1572
_acc1 = _mm_srai_epi16(_mm_add_epi16(_acc1, _v2), 2);
1573
__m128i _Dp0 = _mm_packus_epi16(_acc0, _acc0);
1574
__m128i _Dp1 = _mm_packus_epi16(_acc1, _acc1);
1575
_mm_storel_epi64((__m128i*)Dp0, _Dp0);
1576
_mm_storel_epi64((__m128i*)Dp1, _Dp1);
1583
for (; dx < w; dx++)
1585
short s0 = *rows0p++;
1586
short s1 = *rows1p++;
1588
*Dp0++ = (unsigned char)(((short)((b0 * s0) >> 16) + (short)((b1 * s1) >> 16) + 2) >> 2);
1589
*Dp1++ = (unsigned char)(((short)((b2 * s0) >> 16) + (short)((b3 * s1) >> 16) + 2) >> 2);
1598
short b0 = ibeta[0];
1599
short b1 = ibeta[1];
1601
short* rows0p = rows0;
1602
short* rows1p = rows1;
1603
unsigned char* Dp = resized_font_bitmap + w * (dy);
1607
int16x8_t _b0 = vdupq_n_s16(b0);
1608
int16x8_t _b1 = vdupq_n_s16(b1);
1609
for (; dx + 15 < w; dx += 16)
1611
int16x8_t _r00 = vld1q_s16(rows0p);
1612
int16x8_t _r01 = vld1q_s16(rows0p + 8);
1613
int16x8_t _r10 = vld1q_s16(rows1p);
1614
int16x8_t _r11 = vld1q_s16(rows1p + 8);
1615
int16x8_t _acc0 = vaddq_s16(vqdmulhq_s16(_r00, _b0), vqdmulhq_s16(_r10, _b1));
1616
int16x8_t _acc1 = vaddq_s16(vqdmulhq_s16(_r01, _b0), vqdmulhq_s16(_r11, _b1));
1617
uint8x16_t _Dp = vcombine_u8(vqrshrun_n_s16(_acc0, 3), vqrshrun_n_s16(_acc1, 3));
1623
for (; dx + 7 < w; dx += 8)
1625
int16x8_t _r0 = vld1q_s16(rows0p);
1626
int16x8_t _r1 = vld1q_s16(rows1p);
1627
int16x8_t _acc = vaddq_s16(vqdmulhq_s16(_r0, _b0), vqdmulhq_s16(_r1, _b1));
1628
uint8x8_t _Dp = vqrshrun_n_s16(_acc, 3);
1636
__m128i _b0 = _mm_set1_epi16(b0);
1637
__m128i _b1 = _mm_set1_epi16(b1);
1638
__m128i _v2 = _mm_set1_epi16(2);
1639
for (; dx + 15 < w; dx += 16)
1641
__m128i _r00 = _mm_loadu_si128((const __m128i*)rows0p);
1642
__m128i _r01 = _mm_loadu_si128((const __m128i*)(rows0p + 8));
1643
__m128i _r10 = _mm_loadu_si128((const __m128i*)rows1p);
1644
__m128i _r11 = _mm_loadu_si128((const __m128i*)(rows1p + 8));
1645
__m128i _acc0 = _mm_add_epi16(_mm_mulhi_epi16(_r00, _b0), _mm_mulhi_epi16(_r10, _b1));
1646
__m128i _acc1 = _mm_add_epi16(_mm_mulhi_epi16(_r01, _b0), _mm_mulhi_epi16(_r11, _b1));
1647
_acc0 = _mm_srai_epi16(_mm_add_epi16(_acc0, _v2), 2);
1648
_acc1 = _mm_srai_epi16(_mm_add_epi16(_acc1, _v2), 2);
1649
__m128i _Dp = _mm_packus_epi16(_acc0, _acc1);
1650
_mm_storeu_si128((__m128i*)Dp, _Dp);
1655
for (; dx + 7 < w; dx += 8)
1657
__m128i _r0 = _mm_loadu_si128((const __m128i*)rows0p);
1658
__m128i _r1 = _mm_loadu_si128((const __m128i*)rows1p);
1659
__m128i _acc = _mm_add_epi16(_mm_mulhi_epi16(_r0, _b0), _mm_mulhi_epi16(_r1, _b1));
1660
_acc = _mm_srai_epi16(_mm_add_epi16(_acc, _v2), 2);
1661
__m128i _Dp = _mm_packus_epi16(_acc, _acc);
1662
_mm_storel_epi64((__m128i*)Dp, _Dp);
1668
for (; dx < w; dx++)
1670
short s0 = *rows0p++;
1671
short s1 = *rows1p++;
1673
*Dp++ = (unsigned char)(((short)((b0 * s0) >> 16) + (short)((b1 * s1) >> 16) + 2) >> 2);
1683
void draw_text_c1(unsigned char* pixels, int w, int h, int stride, const char* text, int x, int y, int fontpixelsize, unsigned int color)
1685
const unsigned char* pen_color = (const unsigned char*)&color;
1687
unsigned char* resized_font_bitmap = new unsigned char[fontpixelsize * fontpixelsize * 2];
1689
const int n = strlen(text);
1693
for (int i = 0; i < n; i++)
1701
cursor_y += fontpixelsize * 2;
1707
cursor_x += fontpixelsize;
1711
if (isprint(ch) != 0)
1713
const unsigned char* font_bitmap = mono_font_data[ch - '!'];
1715
// draw resized character
1716
resize_bilinear_font(font_bitmap, resized_font_bitmap, fontpixelsize);
1718
const int ystart = std::max(cursor_y, 0);
1719
const int yend = std::min(cursor_y + fontpixelsize * 2, h);
1720
const int xstart = std::max(cursor_x, 0);
1721
const int xend = std::min(cursor_x + fontpixelsize, w);
1723
for (int j = ystart; j < yend; j++)
1725
const unsigned char* palpha = resized_font_bitmap + (j - cursor_y) * fontpixelsize + xstart - cursor_x;
1726
unsigned char* p = pixels + stride * j + xstart;
1728
for (int k = xstart; k < xend; k++)
1730
unsigned char alpha = *palpha++;
1732
p[0] = (p[0] * (255 - alpha) + pen_color[0] * alpha) / 255;
1737
cursor_x += fontpixelsize;
1741
delete[] resized_font_bitmap;
1744
void draw_text_c2(unsigned char* pixels, int w, int h, int stride, const char* text, int x, int y, int fontpixelsize, unsigned int color)
1746
const unsigned char* pen_color = (const unsigned char*)&color;
1748
unsigned char* resized_font_bitmap = new unsigned char[fontpixelsize * fontpixelsize * 2];
1750
const int n = strlen(text);
1754
for (int i = 0; i < n; i++)
1762
cursor_y += fontpixelsize * 2;
1768
cursor_x += fontpixelsize;
1772
if (isprint(ch) != 0)
1774
const unsigned char* font_bitmap = mono_font_data[ch - '!'];
1776
// draw resized character
1777
resize_bilinear_font(font_bitmap, resized_font_bitmap, fontpixelsize);
1779
const int ystart = std::max(cursor_y, 0);
1780
const int yend = std::min(cursor_y + fontpixelsize * 2, h);
1781
const int xstart = std::max(cursor_x, 0);
1782
const int xend = std::min(cursor_x + fontpixelsize, w);
1784
for (int j = ystart; j < yend; j++)
1786
const unsigned char* palpha = resized_font_bitmap + (j - cursor_y) * fontpixelsize + xstart - cursor_x;
1787
unsigned char* p = pixels + stride * j + xstart * 2;
1789
for (int k = xstart; k < xend; k++)
1791
unsigned char alpha = *palpha++;
1793
p[0] = (p[0] * (255 - alpha) + pen_color[0] * alpha) / 255;
1794
p[1] = (p[1] * (255 - alpha) + pen_color[1] * alpha) / 255;
1799
cursor_x += fontpixelsize;
1803
delete[] resized_font_bitmap;
1806
void draw_text_c3(unsigned char* pixels, int w, int h, int stride, const char* text, int x, int y, int fontpixelsize, unsigned int color)
1808
const unsigned char* pen_color = (const unsigned char*)&color;
1810
unsigned char* resized_font_bitmap = new unsigned char[fontpixelsize * fontpixelsize * 2];
1812
const int n = strlen(text);
1816
for (int i = 0; i < n; i++)
1824
cursor_y += fontpixelsize * 2;
1830
cursor_x += fontpixelsize;
1834
if (isprint(ch) != 0)
1836
const unsigned char* font_bitmap = mono_font_data[ch - '!'];
1838
// draw resized character
1839
resize_bilinear_font(font_bitmap, resized_font_bitmap, fontpixelsize);
1841
const int ystart = std::max(cursor_y, 0);
1842
const int yend = std::min(cursor_y + fontpixelsize * 2, h);
1843
const int xstart = std::max(cursor_x, 0);
1844
const int xend = std::min(cursor_x + fontpixelsize, w);
1846
for (int j = ystart; j < yend; j++)
1848
const unsigned char* palpha = resized_font_bitmap + (j - cursor_y) * fontpixelsize + xstart - cursor_x;
1849
unsigned char* p = pixels + stride * j + xstart * 3;
1851
for (int k = xstart; k < xend; k++)
1853
unsigned char alpha = *palpha++;
1855
p[0] = (p[0] * (255 - alpha) + pen_color[0] * alpha) / 255;
1856
p[1] = (p[1] * (255 - alpha) + pen_color[1] * alpha) / 255;
1857
p[2] = (p[2] * (255 - alpha) + pen_color[2] * alpha) / 255;
1862
cursor_x += fontpixelsize;
1866
delete[] resized_font_bitmap;
1869
void draw_text_c4(unsigned char* pixels, int w, int h, int stride, const char* text, int x, int y, int fontpixelsize, unsigned int color)
1871
const unsigned char* pen_color = (const unsigned char*)&color;
1873
unsigned char* resized_font_bitmap = new unsigned char[fontpixelsize * fontpixelsize * 2];
1875
const int n = strlen(text);
1879
for (int i = 0; i < n; i++)
1887
cursor_y += fontpixelsize * 2;
1893
cursor_x += fontpixelsize;
1897
if (isprint(ch) != 0)
1899
const unsigned char* font_bitmap = mono_font_data[ch - '!'];
1901
// draw resized character
1902
resize_bilinear_font(font_bitmap, resized_font_bitmap, fontpixelsize);
1904
const int ystart = std::max(cursor_y, 0);
1905
const int yend = std::min(cursor_y + fontpixelsize * 2, h);
1906
const int xstart = std::max(cursor_x, 0);
1907
const int xend = std::min(cursor_x + fontpixelsize, w);
1909
for (int j = ystart; j < yend; j++)
1911
const unsigned char* palpha = resized_font_bitmap + (j - cursor_y) * fontpixelsize + xstart - cursor_x;
1912
unsigned char* p = pixels + stride * j + xstart * 4;
1914
for (int k = xstart; k < xend; k++)
1916
unsigned char alpha = *palpha++;
1918
p[0] = (p[0] * (255 - alpha) + pen_color[0] * alpha) / 255;
1919
p[1] = (p[1] * (255 - alpha) + pen_color[1] * alpha) / 255;
1920
p[2] = (p[2] * (255 - alpha) + pen_color[2] * alpha) / 255;
1921
p[3] = (p[3] * (255 - alpha) + pen_color[3] * alpha) / 255;
1926
cursor_x += fontpixelsize;
1930
delete[] resized_font_bitmap;
1933
void draw_text_yuv420sp(unsigned char* yuv420sp, int w, int h, const char* text, int x, int y, int fontpixelsize, unsigned int color)
1935
// assert w % 2 == 0
1936
// assert h % 2 == 0
1937
// assert x % 2 == 0
1938
// assert y % 2 == 0
1939
// assert fontpixelsize % 2 == 0
1941
const unsigned char* pen_color = (const unsigned char*)&color;
1945
unsigned char* pen_color_y = (unsigned char*)&v_y;
1946
unsigned char* pen_color_uv = (unsigned char*)&v_uv;
1947
pen_color_y[0] = pen_color[0];
1948
pen_color_uv[0] = pen_color[1];
1949
pen_color_uv[1] = pen_color[2];
1951
unsigned char* Y = yuv420sp;
1952
draw_text_c1(Y, w, h, text, x, y, fontpixelsize, v_y);
1954
unsigned char* UV = yuv420sp + w * h;
1955
draw_text_c2(UV, w / 2, h / 2, text, x / 2, y / 2, std::max(fontpixelsize / 2, 1), v_uv);
1958
#endif // NCNN_PIXEL_DRAWING