30 return (
fl::u8)
fl::clamp((alpha * T(255.0f) + T(0.5f)).to_int(), 0, 255);
58 return (
fl::u8)
fl::clamp((alpha * c255 + half).to_int(), (fl::i32)0, (fl::i32)255);
110 return static_cast<int>(val);
122 return static_cast<int>(val);
128template<
typename PixelT,
bool Overwrite>
146template<
typename Coord,
typename =
void>
150template<
typename Coord>
156template<
typename Coord>
158 return num * inv_denom;
162template<
typename Coord>
172template<
typename Coord>
186template<
typename Coord>
188 return static_cast<fl::i32
>(val.to_float() * 256.0f);
193 return static_cast<fl::i32
>(val * 256.0f);
198 return static_cast<fl::i32
>(val * 256.0);
203 return static_cast<fl::i32
>(
static_cast<fl::u32
>(val) << 8);
209 return static_cast<fl::i32
>(val.raw() >> 8);
218 fl::u32 tmp =
static_cast<fl::u32
>(band);
219 while (tmp > 255u) { tmp >>= 1; ++sh; }
223 inv_out =
static_cast<fl::u16
>((65280u + (scaled >> 1)) / scaled);
229template<
typename Coord>
231 return static_cast<fl::i32
>(val.to_float() * 65536.0f);
236 return static_cast<fl::i32
>(val * 65536.0f);
241 return static_cast<fl::i32
>(val * 65536.0);
246 return static_cast<fl::i32
>(val) << 16;
264template<
typename Coord>
272 if ((a % b) < 0) --q;
295 if (c < -1)
return -1;
297 return static_cast<int>(c);
345 const fl::u32 a0 = (fl::u32)a, a1 = (fl::u32)(a >> 32);
346 const fl::u32 b0 = (fl::u32)b, b1 = (fl::u32)(b >> 32);
349 const fl::u64 low = p0 + (p1 << 32);
350 hi = (
fl::u64)a1 * b1 + (p1 >> 32) + (low < p0 ? 1u : 0u);
357 hi += ahi + (low < lo ? 1u : 0u);
377 for (
int e = 0; e < nc; ++e) {
386 for (
int e = 1; e < nc; ++e) {
390 while (m >= 0 && keys[m] > kv) {
391 keys[m + 1] = keys[m];
404 if ((v & 0xFFFF0000u) == 0) { n = 16; v <<= 16; }
405 if ((v & 0xFF000000u) == 0) { n += 8; v <<= 8; }
406 if ((v & 0xF0000000u) == 0) { n += 4; v <<= 4; }
407 if ((v & 0xC0000000u) == 0) { n += 2; v <<= 2; }
408 if ((v & 0x80000000u) == 0) { n += 1; }
438 fl::u32 m3 = (fl::u32)(mhi >> 32), m2 = (fl::u32)mhi;
439 fl::u32 m1 = (fl::u32)(mlo >> 32), m0 = (fl::u32)mlo;
444 const int t = (top < 94) ? top : 93;
449 const int k = 127 -
t;
450 const int kl = k >> 5;
451 const int kb = k & 31;
453 fl::u32 n3, n2, n1, n0;
454 if (kl >= 3) { n3 = m0; n2 = 0; n1 = 0; n0 = 0; }
455 else if (kl == 2) { n3 = m1; n2 = m0; n1 = 0; n0 = 0; }
456 else if (kl == 1) { n3 = m2; n2 = m1; n1 = m0; n0 = 0; }
457 else { n3 = m3; n2 = m2; n1 = m1; n0 = m0; }
459 const fl::u32 h3 = n2 >> (32 - kb);
460 const fl::u32 h2 = n1 >> (32 - kb);
461 const fl::u32 h1 = n0 >> (32 - kb);
462 n3 = (n3 << kb) | h3;
463 n2 = (n2 << kb) | h2;
464 n1 = (n1 << kb) | h1;
467 m3 = n3; m2 = n2; m1 = n1; m0 = n0;
475 const fl::u32 d1 = (fl::u32)(dv >> 32), d0 = (fl::u32)dv;
476 fl::u32 s1 = 0, s0 = 0;
477 fl::u32 q1 = 0, q0 = 0;
478 for (
int i =
t; i >= 0; --i) {
479 const fl::u32 bit = m3 >> 31;
480 m3 = (m3 << 1) | (m2 >> 31);
481 m2 = (m2 << 1) | (m1 >> 31);
482 m1 = (m1 << 1) | (m0 >> 31);
484 const fl::u32 sh = s1 << 1, sl = s0 << 1;
485 s1 = sh | (s0 >> 31);
487 const fl::u32 ge = (s1 > d1) || (s1 == d1 && s0 >= d0);
489 const fl::u32 sb = s0 - d0;
490 s1 -= d1 + (fl::u32)(sb > s0);
493 const fl::u32 qh = q1 << 1, ql = q0 << 1;
494 q1 = qh | (q0 >> 31);
499 const fl::u32 r1 = (s1 << 1) | (s0 >> 31), r0 = s0 << 1;
500 if ((s1 >> 31) != 0 || r1 > d1 || (r1 == d1 && r0 >= d0)) {
501 const fl::u32 qb = q0 + 1;
502 q1 += (fl::u32)(qb < q0);
548 else if (w > 65536) w = 65536;
549 return (fl::i32)(((w * (tb - ta)) + 8) >> 4);
556 const fl::i64 s_lo = (sa < sb) ? sa : sb;
557 const fl::i64 s_hi = (sa < sb) ? sb : sa;
561 const fl::i64 lo = (s_lo > c0) ? s_lo : c0;
562 const fl::i64 hi = (s_hi < c1) ? s_hi : c1;
567 (
fl::u64)((u + lo) + (u + hi)), mhi, mlo);
569 const fl::i64 lo2 = (s_lo > c1) ? s_lo : c1;
576 if (mhi == 0 && mlo == 0)
return 0;
595 if (dhi != 0 || (dlo >> 63) != 0) {
596 const fl::u64 nlo = (mlo >> 4) | (mhi << 60);
606 if (dlo != 0 && mhi == 0) {
608 const fl::u64 r = mlo - q * dlo;
609 if ((r >> 63) != 0 || (r << 1) >= dlo) ++q;
619template<
typename PixelT>
632template<
typename PixelT>
645template<
typename PixelT,
bool Overwrite>
649 PixelT* ptr = &buf[py * w + f.xmin];
651 fl::i32 xd = f.xdelta0;
654 while (px <= f.xmax && d2 >= f.rout2) {
655 d2 += xd; xd += 131072; ++ptr; ++px;
658 while (px <= f.xmax && d2 >= f.rin2 && d2 < f.rout2) {
659 fl::u16 diff =
static_cast<fl::u16
>(
static_cast<fl::u32
>(f.rout2 - d2) >> f.band_shift);
660 fl::u8 br =
static_cast<fl::u8>((diff * f.band_inv) >> 8);
661 PixelT c = f.color; c.nscale8(br);
662 if (Overwrite) *ptr = c;
else *ptr += c;
663 d2 += xd; xd += 131072; ++ptr; ++px;
666 while (px <= f.xmax && d2 < f.rin2) {
667 if (Overwrite) *ptr = f.color;
else *ptr += f.color;
668 d2 += xd; xd += 131072; ++ptr; ++px;
671 while (px <= f.xmax && d2 < f.rout2) {
672 fl::u16 diff =
static_cast<fl::u16
>(
static_cast<fl::u32
>(f.rout2 - d2) >> f.band_shift);
673 fl::u8 br =
static_cast<fl::u8>((diff * f.band_inv) >> 8);
674 PixelT c = f.color; c.nscale8(br);
675 if (Overwrite) *ptr = c;
else *ptr += c;
676 d2 += xd; xd += 131072; ++ptr; ++px;
684template<
typename PixelT,
bool Overwrite>
688 PixelT* ptr = &buf[py * w +
g.xmin];
690 fl::i32 xd =
g.xdelta0;
693 while (px <= g.xmax && d2 >=
g.oo2) {
694 d2 += xd; xd += 131072; ++ptr; ++px;
697 while (px <= g.xmax && d2 >=
g.oi2 && d2 <
g.oo2) {
698 fl::u16 diff =
static_cast<fl::u16
>(
static_cast<fl::u32
>(
g.oo2 - d2) >>
g.outer_shift);
700 PixelT c =
g.color; c.nscale8(br);
701 if (Overwrite) *ptr = c;
else *ptr += c;
702 d2 += xd; xd += 131072; ++ptr; ++px;
705 while (px <= g.xmax && d2 >=
g.io2 && d2 <
g.oi2) {
706 if (Overwrite) *ptr =
g.color;
else *ptr +=
g.color;
707 d2 += xd; xd += 131072; ++ptr; ++px;
710 while (px <= g.xmax && d2 >=
g.ii2 && d2 <
g.io2) {
711 fl::u16 diff =
static_cast<fl::u16
>(
static_cast<fl::u32
>(d2 -
g.ii2) >>
g.inner_shift);
713 PixelT c =
g.color; c.nscale8(br);
714 if (Overwrite) *ptr = c;
else *ptr += c;
715 d2 += xd; xd += 131072; ++ptr; ++px;
718 while (px <=
g.xmax && d2 <
g.ii2) {
719 d2 += xd; xd += 131072; ++ptr; ++px;
722 while (px <= g.xmax && d2 >=
g.ii2 && d2 <
g.io2) {
723 fl::u16 diff =
static_cast<fl::u16
>(
static_cast<fl::u32
>(d2 -
g.ii2) >>
g.inner_shift);
725 PixelT c =
g.color; c.nscale8(br);
726 if (Overwrite) *ptr = c;
else *ptr += c;
727 d2 += xd; xd += 131072; ++ptr; ++px;
730 while (px <= g.xmax && d2 >=
g.io2 && d2 <
g.oi2) {
731 if (Overwrite) *ptr =
g.color;
else *ptr +=
g.color;
732 d2 += xd; xd += 131072; ++ptr; ++px;
735 while (px <= g.xmax && d2 >=
g.oi2 && d2 <
g.oo2) {
736 fl::u16 diff =
static_cast<fl::u16
>(
static_cast<fl::u32
>(
g.oo2 - d2) >>
g.outer_shift);
738 PixelT c =
g.color; c.nscale8(br);
739 if (Overwrite) *ptr = c;
else *ptr += c;
740 d2 += xd; xd += 131072; ++ptr; ++px;
748template<
typename PixelT>
768template<
typename PixelT,
bool Overwrite>
770 fl::i32 cross_start, fl::i32 dot_start,
772 PixelT* ptr = &buf[py * w + sc.xmin];
773 fl::i32 cross = cross_start;
774 fl::i32 dot = dot_start;
776 const fl::i32 thr = sc.threshold_q;
777 const fl::i32 neg_thr = -thr;
780 while (px <= sc.xmax && (cross >= thr || cross < neg_thr)) {
787 fl::i32 py8 =
static_cast<fl::i32
>(py) << 8;
788 fl::i32 ey8_y0 = py8 - sc.y0_8;
789 fl::i32 ey8_y1 = py8 - sc.y1_8;
792 while (px <= sc.xmax && cross < thr && cross >= neg_thr) {
793 fl::i32 abs_cross = (cross < 0) ? -cross : cross;
794 bool on_segment = (dot >= 0 && dot <= sc.len2_q);
798 fl::u16 shifted =
static_cast<fl::u16
>(
799 static_cast<fl::u32
>(abs_cross) >> sc.aa_shift);
800 fl::u8 linear_idx =
static_cast<fl::u8>((shifted * sc.aa_inv) >> 8);
802 fl::u16 sq =
static_cast<fl::u16
>(linear_idx) * linear_idx;
803 fl::u8 sq_idx =
static_cast<fl::u8>((sq + 1 + (sq >> 8)) >> 8);
806 if (Overwrite) *ptr = c;
else *ptr += c;
807 }
else if (sc.cap == LineCap::ROUND) {
812 ex8 = (
static_cast<fl::i32
>(px) << 8) - sc.x0_8;
813 ey_s =
static_cast<fl::i16
>(ey8_y0);
815 ex8 = (
static_cast<fl::i32
>(px) << 8) - sc.x1_8;
816 ey_s =
static_cast<fl::i16
>(ey8_y1);
818 fl::i16 ex_s =
static_cast<fl::i16
>(ex8);
820 fl::i32 ed2 =
static_cast<fl::i32
>(ex_s) * ex_s +
821 static_cast<fl::i32
>(ey_s) * ey_s;
822 if (ed2 < sc.r_max2_8) {
823 fl::u16 shifted =
static_cast<fl::u16
>(
824 static_cast<fl::u32
>(ed2) >> sc.cap_shift);
825 fl::u8 idx =
static_cast<fl::u8>((shifted * sc.cap_inv) >> 8);
828 if (Overwrite) *ptr = c;
else *ptr += c;
830 }
else if (sc.cap == LineCap::SQUARE) {
831 if (dot >= -sc.dot_ext_q && dot <= sc.len2_q + sc.dot_ext_q) {
832 fl::u16 shifted =
static_cast<fl::u16
>(
833 static_cast<fl::u32
>(abs_cross) >> sc.aa_shift);
834 fl::u8 linear_idx =
static_cast<fl::u8>((shifted * sc.aa_inv) >> 8);
835 fl::u16 sq =
static_cast<fl::u16
>(linear_idx) * linear_idx;
836 fl::u8 sq_idx =
static_cast<fl::u8>((sq + 1 + (sq >> 8)) >> 8);
839 if (Overwrite) *ptr = c;
else *ptr += c;
863template<
typename PixelT,
typename Coord>
865 Coord x0, Coord y0, Coord x1, Coord y1,
868template<
typename PixelT,
typename Coord>
870 Coord cx, Coord cy, Coord r,
873template<
typename PixelT,
typename Coord>
875 Coord cx, Coord cy, Coord r, Coord thickness,
878template<
typename PixelT,
typename Coord>
880 Coord x0, Coord y0, Coord x1, Coord y1, Coord thickness,
883template<
typename PixelT,
typename Coord>
888template<
typename PixelT,
typename Coord,
int N>
907template<
typename PixelT,
typename Coord,
bool Overwrite>
910 PixelT* pixels =
canvas.pixels;
924 fl::i32 dx8 = fx1 - fx0;
925 fl::i32 dy8 = fy1 - fy0;
928 fl::i32 gradient = (dx8 == 0) ? 0 : ((dy8 << 8) / dx8);
931 fl::i32 xend = (fx0 + 128) & ~0xFF;
932 fl::i32 yend = fy0 + ((gradient * (xend - fx0)) >> 8);
933 fl::i32 xgap = 256 - ((fx0 + 128) & 0xFF);
934 int xpxl1 = xend >> 8;
935 int ypxl1 = yend >> 8;
939 fl::i32 xend2 = (fx1 + 128) & ~0xFF;
940 fl::i32 yend2 = fy1 + ((gradient * (xend2 - fx1)) >> 8);
941 fl::i32 xgap2 = (fx1 + 128) & 0xFF;
942 int xpxl2 = xend2 >> 8;
943 int ypxl2 = yend2 >> 8;
948 fl::u8 b1 =
static_cast<fl::u8>(((255 - yfrac1) * xgap) >> 8);
951 PixelT c =
color; c.nscale8(b1);
953 c =
color; c.nscale8(b2);
956 PixelT c =
color; c.nscale8(b1);
958 c =
color; c.nscale8(b2);
965 fl::u8 b1 =
static_cast<fl::u8>(((255 - yfrac2) * xgap2) >> 8);
968 PixelT c =
color; c.nscale8(b1);
970 c =
color; c.nscale8(b2);
973 PixelT c =
color; c.nscale8(b1);
975 c =
color; c.nscale8(b2);
981 fl::i32 intery = yend + gradient;
983 for (
int x = xpxl1 + 1;
x < xpxl2; ++
x) {
986 PixelT c =
color; c.nscale8(255 - frac);
988 c =
color; c.nscale8(frac);
993 for (
int x = xpxl1 + 1;
x < xpxl2; ++
x) {
996 PixelT c =
color; c.nscale8(255 - frac);
998 c =
color; c.nscale8(frac);
1005template<
typename PixelT,
typename Coord,
bool Overwrite>
1009template<
typename PixelT,
typename Coord,
bool Overwrite>
1011 Coord cx, Coord cy, Coord r, Coord thickness)
FL_NO_EXCEPT;
1013template<
typename PixelT,
typename Coord,
bool Overwrite>
1015 Coord x0, Coord y0, Coord x1, Coord y1,
1018template<
typename PixelT,
typename Coord,
bool Overwrite,
int MAXN,
int CHUNK>
1024template<
typename PixelT,
typename Coord>
1026 Coord x0, Coord y0, Coord x1, Coord y1,
1037template<
typename PixelT,
typename Coord,
bool Overwrite>
1040 PixelT* pixels =
canvas.pixels;
1048 fl::i32 rin8 = r8 - 128;
1049 fl::i32 rout8 = r8 + 128;
1051 fl::i32 rin2 = rin8 * rin8;
1052 fl::i32 rout2 = rout8 * rout8;
1053 fl::i32 band = rout2 - rin2;
1054 if (band <= 0)
return;
1056 int ri = (rout8 >> 8) + 1;
1060 int xmin = cxi - ri;
if (xmin < 0) xmin = 0;
1061 int xmax = cxi + ri;
if (xmax >=
width) xmax =
width - 1;
1062 if (xmin > xmax)
return;
1063 if (cyi + ri < 0 || cyi - ri >=
height)
return;
1065 fl::i32 dx8 = (
static_cast<fl::i32
>(xmin) << 8) - cx8;
1066 fl::i32 dx2 = dx8 * dx8;
1069 fc.
xdelta0 = 512 * dx8 + 65536;
1075 fl::i32 cyfrac = (
static_cast<fl::i32
>(cyi) << 8) - cy8;
1076 fl::i32 d2c = dx2 + cyfrac * cyfrac;
1078 if (cyi >= 0 && cyi <
height)
1081 fl::i32 botd2 = d2c, botdelta = 512 * cyfrac + 65536;
1082 fl::i32 topd2 = d2c, topdelta = -512 * cyfrac + 65536;
1083 botd2 += botdelta; botdelta += 131072;
1084 topd2 += topdelta; topdelta += 131072;
1086 for (
int dy = 1; dy <= ri; ++dy) {
1087 bool cbot = (botd2 - dx2 <= rout2);
1088 bool ctop = (topd2 - dx2 <= rout2);
1089 if (!cbot && !ctop)
break;
1090 int pyb = cyi + dy, pyt = cyi - dy;
1091 if (cbot && pyb >= 0 && pyb <
height)
1093 if (ctop && pyt >= 0 && pyt <
height)
1095 botd2 += botdelta; botdelta += 131072;
1096 topd2 += topdelta; topdelta += 131072;
1100template<
typename PixelT,
typename Coord>
1102 Coord cx, Coord cy, Coord r,
1113template<
typename PixelT,
typename Coord,
bool Overwrite>
1115 Coord cx, Coord cy, Coord r, Coord thickness)
FL_NO_EXCEPT {
1116 PixelT* pixels =
canvas.pixels;
1125 fl::i32 r_ii8 = r8 - 128;
1126 fl::i32 r_io8 = r8 + 128;
1127 fl::i32 r_oi8 = r8 + t8 - 128;
1128 fl::i32 r_oo8 = r8 + t8 + 128;
1130 if (r_ii8 < 0) r_ii8 = 0;
1131 if (r_io8 < 0) r_io8 = 0;
1133 fl::i32 ii2 = r_ii8 * r_ii8;
1134 fl::i32 io2 = r_io8 * r_io8;
1135 fl::i32 oi2 = r_oi8 * r_oi8;
1136 fl::i32 oo2 = r_oo8 * r_oo8;
1138 fl::i32 inner_band = io2 - ii2;
1139 fl::i32 outer_band = oo2 - oi2;
1141 int ri = (r_oo8 >> 8) + 1;
1145 int xmin = cxi - ri;
if (xmin < 0) xmin = 0;
1146 int xmax = cxi + ri;
if (xmax >=
width) xmax =
width - 1;
1147 if (xmin > xmax)
return;
1148 if (cyi + ri < 0 || cyi - ri >=
height)
return;
1150 fl::i32 dx8 = (
static_cast<fl::i32
>(xmin) << 8) - cx8;
1151 fl::i32 dx2 = dx8 * dx8;
1154 gc.
xdelta0 = 512 * dx8 + 65536;
1156 gc.
ii2 = ii2; gc.
io2 = io2; gc.
oi2 = oi2; gc.
oo2 = oo2;
1163 fl::i32 cyfrac = (
static_cast<fl::i32
>(cyi) << 8) - cy8;
1164 fl::i32 d2c = dx2 + cyfrac * cyfrac;
1166 if (cyi >= 0 && cyi <
height)
1169 fl::i32 botd2 = d2c, botdelta = 512 * cyfrac + 65536;
1170 fl::i32 topd2 = d2c, topdelta = -512 * cyfrac + 65536;
1171 botd2 += botdelta; botdelta += 131072;
1172 topd2 += topdelta; topdelta += 131072;
1174 for (
int dy = 1; dy <= ri; ++dy) {
1175 bool cbot = (botd2 - dx2 <= oo2);
1176 bool ctop = (topd2 - dx2 <= oo2);
1177 if (!cbot && !ctop)
break;
1178 int pyb = cyi + dy, pyt = cyi - dy;
1179 if (cbot && pyb >= 0 && pyb <
height)
1181 if (ctop && pyt >= 0 && pyt <
height)
1183 botd2 += botdelta; botdelta += 131072;
1184 topd2 += topdelta; topdelta += 131072;
1188template<
typename PixelT,
typename Coord>
1190 Coord cx, Coord cy, Coord r, Coord thickness,
1201template<
typename PixelT,
typename Coord,
bool Overwrite>
1203 Coord x0, Coord y0, Coord x1, Coord y1,
1205 PixelT* pixels =
canvas.pixels;
1211 fl::i32 dx_8 = x1_8 - x0_8;
1212 fl::i32 dy_8 = y1_8 - y0_8;
1213 if (dx_8 == 0 && dy_8 == 0)
return;
1215 fl::i32 len2_16 = dx_8 * dx_8 + dy_8 * dy_8;
1216 fl::i32 len_8 =
static_cast<fl::i32
>(
1219 fl::i32 r_max_8 = thickness_8 >> 1;
1220 fl::i32 threshold_q = r_max_8 * len_8;
1222 if (threshold_q <= 0)
return;
1224 int x0i = x0_8 >> 8, y0i = y0_8 >> 8;
1225 int x1i = x1_8 >> 8, y1i = y1_8 >> 8;
1226 int margin = ((r_max_8 + 255) >> 8) + 2;
1227 int xmin = (x0i < x1i ? x0i : x1i) - margin;
1228 int xmax = (x0i > x1i ? x0i : x1i) + margin;
1229 int ymin = (y0i < y1i ? y0i : y1i) - margin;
1230 int ymax = (y0i > y1i ? y0i : y1i) + margin;
1232 if (xmin < 0) xmin = 0;
1234 if (ymin < 0) ymin = 0;
1236 if (xmin > xmax || ymin > ymax)
return;
1238 fl::i32 dx_q = dx_8 << 8;
1239 fl::i32 dy_q = dy_8 << 8;
1241 fl::i32 rx_base_8 = (
static_cast<fl::i32
>(xmin) << 8) - x0_8;
1242 fl::i32 ry_row_8 = (
static_cast<fl::i32
>(ymin) << 8) - y0_8;
1243 fl::i32 cross_row_q = rx_base_8 * dy_8 - ry_row_8 * dx_8;
1244 fl::i32 dot_row_q = rx_base_8 * dx_8 + ry_row_8 * dy_8;
1246 fl::i32 len2_q = len_8 * len_8;
1247 fl::i32 dot_ext_q = (cap == LineCap::SQUARE) ? threshold_q : 0;
1260 if (cap == LineCap::ROUND && sc.
r_max2_8 > 0) {
1267 for (
int py = ymin; py <= ymax; ++py) {
1268 if (py >= 0 && py <
height) {
1270 cross_row_q, dot_row_q, sc);
1272 cross_row_q -= sc.
dx_q;
1273 dot_row_q += sc.
dy_q;
1277template<
typename PixelT,
typename Coord>
1279 Coord x0, Coord y0, Coord x1, Coord y1, Coord thickness,
1325template<
typename PixelT,
typename Coord,
bool Overwrite,
int MAXN,
int CHUNK>
1328 FL_STATIC_ASSERT(MAXN >= 3 && MAXN <= 256,
"drawPolygon: MAXN must be in [3, 256] (edge indices are u8)");
1329 FL_STATIC_ASSERT(CHUNK > 0 && (CHUNK & (CHUNK - 1)) == 0,
"drawPolygon: CHUNK must be a power of two");
1330 const fl::i32 n =
static_cast<fl::i32
>(pts.size());
1332 PixelT* pixels =
canvas.pixels;
1337 const int nv = (n < MAXN) ? n : MAXN;
1342 for (
int i = 0; i < nv; ++i) {
1351 int ymin =
height, ymax = -1;
1352 for (
int i = 0; i < nv; ++i) {
1353 const int j = (i + 1 == nv) ? 0 : (i + 1);
1354 fl::i32 dy = vy[j] - vy[i];
1355 if (dy == 0)
continue;
1357 fl::i32 ytop, xtop,
B;
1358 if (dy > 0) { ytop = vy[i]; xtop = vx[i];
B = vx[j] - vx[i]; }
1359 else { ytop = vy[j]; xtop = vx[j];
B = vx[i] - vx[j]; }
1360 const fl::i32 dy2 = (dy > 0) ? dy : -dy;
1361 const fl::i32 ybot = ytop + dy2;
1366 int py0 = ytop >> 16;
1367 int py1 = (ybot - 1) >> 16;
1368 if (py0 < 0) py0 = 0;
1370 if (py0 > py1)
continue;
1382 const fl::i64 w0 = (
static_cast<fl::i64>(py0) << 16) + 32768 - ytop;
1395 const fl::i32 rem =
static_cast<fl::i32
>(p2 - s * dy2);
1397 ? ((s >> 1) + (s & 1) + ((s & 1) == 0 && rem > 0 ? 1 : 0))
1398 : (((-s) >> 1) + ((-s) & 1));
1402 ed.
t =
static_cast<fl::i32
>(prod - q * dy2);
1412 if (ed.
py0 < ymin) ymin = ed.
py0;
1413 if (ed.
py1 > ymax) ymax = ed.
py1;
1415 if (nedges == 0)
return;
1418 fl::i32 ev[MAXN + 2];
1423 fl::i32 accb[CHUNK];
1425 for (
int py = ymin; py <= ymax; ++py) {
1426 const fl::i32 yc = (
static_cast<fl::i32
>(py) << 16) + 32768;
1434 for (
int i = 0; i < nv; ++i) {
1435 if ((vy[i] >> 16) == py && (vy[i] & 0xFFFF) != 0)
1436 ev[nev++] = vy[i] - yc;
1439 for (
int a = 1; a < nev; ++a) {
1440 const fl::i32 v = ev[a];
1442 while (b >= 0 && ev[b] > v) { ev[b + 1] = ev[b]; --b; }
1459 int minLx0 = 0, maxLx1 = 0;
1461 for (
int e = 0; e < nedges; ++e) {
1463 if (py < ed.py0 || py > ed.
py1)
continue;
1466 if (nactive++ == 0) { minLx0 = lx0a[e]; maxLx1 = lx1a[e]; }
1468 if (lx0a[e] < minLx0) minLx0 = lx0a[e];
1469 if (lx1a[e] > maxLx1) maxLx1 = lx1a[e];
1472 int xmin = minLx0, xmax = maxLx1;
1473 if (nactive == 0) { xmin =
width; xmax = -1; }
1475 if (xmin < 0) xmin = 0;
1487 fl::u8 bidx[MAXN + 1][MAXN];
1489 fl::i32 bfull[MAXN + 1];
1491 for (
int k = 0; k + 1 < nev; ++k) {
1492 const fl::i32 a = ev[k], b = ev[k + 1];
1495 for (
int e = 0; e < nedges; ++e) {
1497 if (py >= ed.
py0 && py <= ed.
py1 &&
1498 ed.
ytop < yc + b && ed.
ybot > yc + a)
1501 if (nc >= 2)
polySortBand(idx, keys, edges, nc, a + b);
1505 bfull[k] = (b - a) << 12;
1508 for (
int e = 0; e < nc; ++e) bidx[k][e] = idx[e];
1517 const int csFirst = (xmin <= xmax) ? (xmin & ~(CHUNK - 1)) : xmax + 1;
1518 for (
int cs = csFirst; cs <= xmax; cs += CHUNK) {
1519 for (
int i = 0; i < CHUNK; ++i) accb[i] = 0;
1520 const int ce = (cs + CHUNK <= xmax + 1) ? cs + CHUNK : xmax + 1;
1521 for (
int k = 0; k + 1 < nev; ++k) {
1522 const int nc = bnc[k];
1523 if (nc < 2)
continue;
1524 const fl::i32 a = ev[k], b = ev[k + 1];
1527 for (
int p = 0; p + 1 < nc; p += 2) {
1528 const PolyEdge& L = edges[bidx[k][p]];
1529 const PolyEdge& R = edges[bidx[k][p + 1]];
1530 const int Ll0 = lx0a[bidx[k][p]], Ll1 = lx1a[bidx[k][p]];
1531 const int Rl0 = lx0a[bidx[k][p + 1]], Rl1 = lx1a[bidx[k][p + 1]];
1532 int pl = (Ll0 < Rl0) ? Ll0 : Rl0;
1533 if (pl < cs) pl = cs;
1534 int ph = (Ll1 > Rl1) ? Ll1 : Rl1;
1535 if (ph >= ce) ph = ce - 1;
1536 if (pl > ph)
continue;
1541 for (
int px = pl; px <= ph; ++px) {
1542 fl::i32 TL = (px > Ll1) ? 0
1543 : (px < Ll0) ? (fl::i32)full
1545 fl::i32 TR = (px > Rl1) ? 0
1546 : (px < Rl0) ? (fl::i32)full
1548 const fl::i32 d = TR - TL;
1549 if (d > 0) accb[px - cs] += d;
1555 PixelT* row = pixels + py *
width + cs;
1556 for (
int i = 0; i < ce - cs; ++i) {
1557 const fl::i32 v = accb[i];
1558 if (v <= 0)
continue;
1563 if (br == 0)
continue;
1566 if (Overwrite) row[i] = c;
1573 for (
int e = 0; e < nedges; ++e) {
1575 if (py >= ed.
py0 && py <= ed.
py1) {
1578 if (ed.
t >= ed.
dy) {
1598template<
typename PixelT,
typename Coord,
int MAXN,
int CHUNK, PolyFill Fill>
1613template<
typename PixelT,
typename Coord,
int N,
int MAXN,
int CHUNK, PolyFill Fill>
fl::CanvasRGB canvas(fl::span< CRGB >(leds, NUM_LEDS), WIDTH, HEIGHT)
Canvas types for gfx primitives (implementation)
Generic canvas for any pixel type (e.g.
static constexpr i32 SCALE
static constexpr FASTLED_FORCE_INLINE s16x16 from_raw(i32 raw) FL_NO_EXCEPT
#define FL_PGM_READ_BYTE_NEAR(x)
Read a byte (8-bit) from PROGMEM memory.
Legacy compatibility header for 8-bit scaling functions.
Compile-time linker keep-alive hook for a single fl::Bus.
integral_constant< bool, B > bool_constant
integral_constant< bool, true > true_type
integral_constant< bool, false > false_type
enable_if_t< is_same< T, U >::value > swap(T &a, U &b) FL_NO_EXCEPT
Coord aaRatioDispatch(Coord num, Coord, Coord inv_denom, fl::false_type) FL_NO_EXCEPT
int polyTopBit128(fl::u32 m3, fl::u32 m2, fl::u32 m1, fl::u32 m0) FL_NO_EXCEPT
Index (0..127) of the highest set bit of a 128-bit value held in four 32-bit lanes (m3 most significa...
fl::i32 floorDiv16I32(fl::i32 v) FL_NO_EXCEPT
Floor(v / 65536) for i32.
void drawPolygonABufferCore(Canvas< PixelT > &canvas, const PixelT &color, fl::span< const PolyPoint< Coord > > pts) FL_NO_EXCEPT
A-buffer 8x4 antialiased polygon fill (see file header).
int polyColClamped(fl::i64 v, int width) FL_NO_EXCEPT
Pixel column floor(v / 65536) of a row-extent bound v = x +/- hw, clamped to [-1, width].
void drawRingCore(Canvas< PixelT > &canvas, const PixelT &color, Coord cx, Coord cy, Coord r, Coord thickness) FL_NO_EXCEPT
FL_NO_INLINE fl::i32 polyDivRoundT28(fl::u64 mhi, fl::u64 mlo, fl::u64 dv) FL_NO_EXCEPT
T = round_half_up(M / dv) clamped to kPolyOne, for M = (mhi, mlo) and 0 < dv < 2^63 (polyLineT28's qu...
fl::i32 toQ16Clamped(Coord val) FL_NO_EXCEPT
Convert any Coord type to Q16.16, clamped to ±8191 px (NaN -> 0).
void drawLineCore(Canvas< PixelT > &canvas, const PixelT &color, Coord x0, Coord y0, Coord x1, Coord y1) FL_NO_EXCEPT
int polyClz32(fl::u32 v) FL_NO_EXCEPT
Count leading zeros of a non-zero 32-bit value (branch form: portable to MSVC where __builtin_clz is ...
static const fl::i32 kPolyQ16Max
Maximum |vertex coordinate| accepted by drawPolygon, in Q16.16 units.
void polyMul128(fl::u64 a, fl::u64 b, fl::u64 &hi, fl::u64 &lo) FL_NO_EXCEPT
Multiply two unsigned values < 2^48, returning the 128-bit product as a (hi, lo) u64 pair.
void drawDiscCore(Canvas< PixelT > &canvas, const PixelT &color, Coord cx, Coord cy, Coord r) FL_NO_EXCEPT
fl::i32 polyLineT28(fl::i32 B, fl::i32 D, fl::i64 u, fl::i32 ta, fl::i32 tb) FL_NO_EXCEPT
Exact left-of-line coverage of one pixel for one polygon edge.
void renderDiscRow(PixelT *buf, int w, int py, fl::i32 d2_row, const DiscCtx< PixelT > &f) FL_NO_EXCEPT
Render one scanline of a disc using incremental d².
void renderRingRow(PixelT *buf, int w, int py, fl::i32 d2_row, const RingCtx< PixelT > &g) FL_NO_EXCEPT
Render one scanline of a ring using incremental d² with phase-based scanning.
void drawPolygonCore(Canvas< PixelT > &canvas, const PixelT &color, fl::span< const PolyPoint< Coord > > pts) FL_NO_EXCEPT
void drawStrokeLineCore(Canvas< PixelT > &canvas, const PixelT &color, Coord x0, Coord y0, Coord x1, Coord y1, Coord thickness, LineCap cap) FL_NO_EXCEPT
fl::i64 floorDiv16I64(fl::i64 v) FL_NO_EXCEPT
Same for i64 (line crossings can exceed i32 for steep short edges).
fl::i32 toQ16(Coord val) FL_NO_EXCEPT
Convert any Coord type to Q16.16 raw i32 for integer inner loops.
fl::i32 toFixed8(Coord val) FL_NO_EXCEPT
Convert any Coord type to 8.8 fixed-point (fl::i32).
void polyAdd128(fl::u64 &hi, fl::u64 &lo, fl::u64 ahi, fl::u64 alo) FL_NO_EXCEPT
128-bit add: (hi, lo) += (ahi, alo).
void computeBandShift(fl::i32 band, fl::u8 &shift_out, fl::u16 &inv_out) FL_NO_EXCEPT
Precompute right-shift and reciprocal multiplier for AA division.
fl::i32 polyABToQ16(Coord val) FL_NO_EXCEPT
Convert any Coord type to Q16.16, clamped to ±8191 px.
void polySortBand(fl::u8 *idx, fl::i64 *keys, const PolyEdge *edges, int nc, fl::i32 mid) FL_NO_EXCEPT
Sort a band's active edges (idx[0..nc)) by the committed implementation's midpoint key: the FLOORED Q...
void renderStrokeRow(PixelT *buf, int w, int py, fl::i32 cross_start, fl::i32 dot_start, const StrokeCtx< PixelT > &sc) FL_NO_EXCEPT
Render one scanline of a stroke line using phase-based scanning.
fl::i64 floorDivI64(fl::i64 a, fl::i64 b) FL_NO_EXCEPT
Floor-divide a by positive b (rounds toward -inf).
static const fl::i32 kPolyOne
Full-pixel coverage in T units (see polyLineT28).
const fl::u8 distanceAA_LUT[256]
Distance antialiasing lookup table Maps normalized squared distance d²/r_max² (scaled 0....
============================================================================
Stroke line context: bundles per-line constants into a struct passed by reference.
Ring context: bundles all per-circle constants into a struct passed by reference.
Per-edge scanline state for drawPolygon (even-odd fill, band model).
Disc context: bundles per-circle constants into a struct passed by reference.
T fromInt(int n) FL_NO_EXCEPT
Convert an integer to Coord without float intermediate.
void drawStrokeLine(Canvas< PixelT > &canvas, const PixelT &color, Coord x0, Coord y0, Coord x1, Coord y1, Coord thickness, LineCap cap, fl::DrawMode mode=fl::DrawMode::DRAW_MODE_BLEND) FL_NO_EXCEPT
fl::u8 coordToU8< float >(float alpha) FL_NO_EXCEPT
int toInt< double >(const double &val) FL_NO_EXCEPT
fl::u8 coordToU8< double >(double alpha) FL_NO_EXCEPT
LineCap
Line cap styles for stroke operations.
fl::s16x16 fromFrac< fl::s16x16 >(int p, int q) FL_NO_EXCEPT
void drawLine(Canvas< PixelT > &canvas, const PixelT &color, Coord x0, Coord y0, Coord x1, Coord y1, fl::DrawMode mode=fl::DrawMode::DRAW_MODE_BLEND) FL_NO_EXCEPT
============================================================================
int toInt< int >(const int &val) FL_NO_EXCEPT
int toInt(const T &val) FL_NO_EXCEPT
Helper to convert any coordinate type to int Supports: s16x16 (via to_int()), float,...
fl::u8 coordToU8< fl::s16x16 >(fl::s16x16 alpha) FL_NO_EXCEPT
T fromFrac(int p, int q) FL_NO_EXCEPT
Convert rational p/q to Coord without float.
float halfOf< float >(float val)
void drawRing(Canvas< PixelT > &canvas, const PixelT &color, Coord cx, Coord cy, Coord r, Coord thickness, fl::DrawMode mode=fl::DrawMode::DRAW_MODE_BLEND) FL_NO_EXCEPT
void addPixelToBuffer(PixelT *pixels, int width, int height, int x, int y, const PixelT &color) FL_NO_EXCEPT
Internal helper: add or set pixel to rectangular buffer with bounds checking Direct row-major indexin...
void drawPolygon(Canvas< PixelT > &canvas, const PixelT &color, fl::span< const PolyPoint< Coord > > pts, fl::DrawMode mode=fl::DrawMode::DRAW_MODE_BLEND) FL_NO_EXCEPT
Fill selects the polygon backend (PolyFill::Exact by default; PolyFill::ABuffer for the faster 8x4 A-...
fl::u8 coordToU8< int >(int alpha) FL_NO_EXCEPT
Coord aaRatio(Coord num, Coord denom, Coord inv_denom) FL_NO_EXCEPT
Compute num/denom using the best strategy for the Coord type:
int halfOf< int >(int val) FL_NO_EXCEPT
T halfOf(T val) FL_NO_EXCEPT
Divide Coord by 2 using shift (avoids expensive division on embedded).
fl::s16x16 fromInt< fl::s16x16 >(int n) FL_NO_EXCEPT
fl::u8 coordToU8(T alpha) FL_NO_EXCEPT
sqrt is provided by fl::sqrt overloads in math.h for:
int toInt< float >(const float &val) FL_NO_EXCEPT
fl::s16x16 halfOf< fl::s16x16 >(fl::s16x16 val) FL_NO_EXCEPT
void drawDisc(Canvas< PixelT > &canvas, const PixelT &color, Coord cx, Coord cy, Coord r, fl::DrawMode mode=fl::DrawMode::DRAW_MODE_BLEND) FL_NO_EXCEPT
double halfOf< double >(double val)
Polygon vertex (x, y coordinate pair) for drawPolygon C++11 aggregate: PolyPoint<float> p{3....
u8 u8 fract8 blur_amount FL_NO_EXCEPT
FL_OPTIMIZE_FUNCTION constexpr u16 isqrt32(u32 x) FL_NO_EXCEPT
constexpr enable_if< is_fixed_point< T >::value, T >::type clamp(T x, T lo, T hi) FL_NO_EXCEPT
enable_if<!is_integral< T >::value, T >::type round(T value) FL_NO_EXCEPT
gfx::LineCap LineCap
Line cap style.
constexpr enable_if< is_fixed_point< T >::value, T >::type abs(T x) FL_NO_EXCEPT
Base definition for an LED controller.
A-buffer 8x4 antialiased polygon fill (fast, low-stack backend)
#define FL_STATIC_ASSERT(...)
Portable compile-time assertion wrapper.
Simple rectangular canvas for graphics operations Combines a pixel buffer with dimensions for cache-o...