FastLED 3.10.10
Loading...
Searching...
No Matches
primitives.h
Go to the documentation of this file.
1#pragma once
2
4#include "fl/stl/noexcept.h"
6
7#include "fl/gfx/canvas.h" // IWYU pragma: keep
8#include "fl/gfx/draw_mode.h"
9#include "fl/gfx/poly_abuffer.h" // IWYU pragma: keep
10#include "fl/math/distance_lut.h" // IWYU pragma: keep
11#include "fl/math/integer_math.h" // IWYU pragma: keep
13#include "fl/math/math.h"
14#include "fl/math/scale8.h"
15
16namespace fl {
17namespace gfx {
18
24
28template<typename T>
29inline fl::u8 coordToU8(T alpha) FL_NO_EXCEPT {
30 return (fl::u8)fl::clamp((alpha * T(255.0f) + T(0.5f)).to_int(), 0, 255);
31}
32
33// Specialization for float
34template<>
36 return (fl::u8)fl::round(alpha * 255.0f);
37}
38
39// Specialization for double
40template<>
42 return (fl::u8)fl::round(alpha * 255.0);
43}
44
45// Specialization for int
46template<>
48 return (fl::u8)fl::clamp(alpha, 0, 255);
49}
50
51// Specialization for s16x16: uses from_raw, no float at all
52template<>
55 // 1L: the shift must be at least as wide as the result (32768 overflows
56 // a 16-bit int; `1 << 15` is UB on AVR, where int is 16-bit).
57 constexpr fl::s16x16 half = fl::s16x16::from_raw(1L << 15);
58 return (fl::u8)fl::clamp((alpha * c255 + half).to_int(), (fl::i32)0, (fl::i32)255);
59}
60
63template<typename T>
64inline T fromInt(int n) FL_NO_EXCEPT { return T(static_cast<float>(n)); }
65
66template<>
70
73template<typename T>
74inline T fromFrac(int p, int q) FL_NO_EXCEPT { return T(static_cast<float>(p) / static_cast<float>(q)); }
75
76template<>
80
83template<typename T>
84inline T halfOf(T val) FL_NO_EXCEPT { return val / fromInt<T>(2); }
85
86template<>
88 return fl::s16x16::from_raw(val.raw() >> 1);
89}
90
91template<>
92inline float halfOf<float>(float val) { return val * 0.5f; }
93
94template<>
95inline double halfOf<double>(double val) { return val * 0.5; }
96
97template<>
98inline int halfOf<int>(int val) FL_NO_EXCEPT { return val >> 1; }
99
102template<typename T>
103inline int toInt(const T& val) FL_NO_EXCEPT {
104 return val.to_int(); // For s16x16 and similar
105}
106
107// Specialization for float
108template<>
109inline int toInt<float>(const float& val) FL_NO_EXCEPT {
110 return static_cast<int>(val);
111}
112
113// Specialization for int (passthrough)
114template<>
115inline int toInt<int>(const int& val) FL_NO_EXCEPT {
116 return val;
117}
118
119// Specialization for double
120template<>
121inline int toInt<double>(const double& val) FL_NO_EXCEPT {
122 return static_cast<int>(val);
123}
124
128template<typename PixelT, bool Overwrite>
129inline void addPixelToBuffer(PixelT* pixels, int width, int height,
130 int x, int y, const PixelT& color) FL_NO_EXCEPT {
131 if (x >= 0 && x < width && y >= 0 && y < height) {
132 if (Overwrite)
133 pixels[y * width + x] = color;
134 else
135 pixels[y * width + x] += color;
136 }
137}
138
139namespace detail {
140
141// void_t for C++11 SFINAE (detects member existence)
142template<typename...> struct voider { typedef void type; };
143
144// Trait: does this Coord type need division instead of reciprocal multiply?
145// Default (float, double, int — no FRAC_BITS member): use multiply
146template<typename Coord, typename = void>
148
149// Fixed-point types with FRAC_BITS < 16: reciprocal too imprecise, use division
150template<typename Coord>
151struct needs_division<Coord,
152 typename voider<fl::bool_constant<(Coord::FRAC_BITS < 16)>>::type>
153 : fl::bool_constant<(Coord::FRAC_BITS < 16)> {};
154
155// Tag-dispatch: high precision → multiply by precomputed reciprocal
156template<typename Coord>
157inline Coord aaRatioDispatch(Coord num, Coord, Coord inv_denom, fl::false_type) FL_NO_EXCEPT {
158 return num * inv_denom;
159}
160
161// Tag-dispatch: low precision → direct division (more accurate)
162template<typename Coord>
163inline Coord aaRatioDispatch(Coord num, Coord denom, Coord, fl::true_type) FL_NO_EXCEPT {
164 return num / denom;
165}
166
167} // namespace detail
168
172template<typename Coord>
173inline Coord aaRatio(Coord num, Coord denom, Coord inv_denom) FL_NO_EXCEPT {
174 return detail::aaRatioDispatch(num, denom, inv_denom,
176}
177
178// ============================================================================
179// Integer-optimized helpers for disc/ring/line drawing
180// ============================================================================
181
182namespace detail {
183
186template<typename Coord>
187inline fl::i32 toFixed8(Coord val) FL_NO_EXCEPT {
188 return static_cast<fl::i32>(val.to_float() * 256.0f);
189}
190
191template<>
192inline fl::i32 toFixed8<float>(float val) FL_NO_EXCEPT {
193 return static_cast<fl::i32>(val * 256.0f);
194}
195
196template<>
197inline fl::i32 toFixed8<double>(double val) FL_NO_EXCEPT {
198 return static_cast<fl::i32>(val * 256.0);
199}
200
201template<>
202inline fl::i32 toFixed8<int>(int val) FL_NO_EXCEPT {
203 return static_cast<fl::i32>(static_cast<fl::u32>(val) << 8);
204}
205
206template<>
208 // Q16.16 → Q8.8: shift right by 8
209 return static_cast<fl::i32>(val.raw() >> 8);
210}
211
216inline void computeBandShift(fl::i32 band, fl::u8 &shift_out, fl::u16 &inv_out) FL_NO_EXCEPT {
217 fl::u8 sh = 0;
218 fl::u32 tmp = static_cast<fl::u32>(band);
219 while (tmp > 255u) { tmp >>= 1; ++sh; }
220 shift_out = sh;
221 // Reciprocal: (255 * 256 + scaled/2) / scaled — rounds to nearest
222 fl::u8 scaled = static_cast<fl::u8>(tmp);
223 inv_out = static_cast<fl::u16>((65280u + (scaled >> 1)) / scaled);
224}
225
229template<typename Coord>
230inline fl::i32 toQ16(Coord val) FL_NO_EXCEPT {
231 return static_cast<fl::i32>(val.to_float() * 65536.0f);
232}
233
234template<>
235inline fl::i32 toQ16<float>(float val) FL_NO_EXCEPT {
236 return static_cast<fl::i32>(val * 65536.0f);
237}
238
239template<>
240inline fl::i32 toQ16<double>(double val) FL_NO_EXCEPT {
241 return static_cast<fl::i32>(val * 65536.0);
242}
243
244template<>
245inline fl::i32 toQ16<int>(int val) FL_NO_EXCEPT {
246 return static_cast<fl::i32>(val) << 16;
247}
248
249template<>
251 return val.raw();
252}
253
256// 8191L: shifting a 16-bit int by its own width is UB (AVR int is 16-bit),
257// where the constant silently collapses to 0 and every vertex clamps to the
258// origin.
259static const fl::i32 kPolyQ16Max = 8191L << 16;
260
264template<typename Coord>
265inline fl::i32 toQ16Clamped(Coord val) FL_NO_EXCEPT {
266 return polyABToQ16<Coord>(val);
267}
268
271 fl::i64 q = a / b;
272 if ((a % b) < 0) --q;
273 return q;
274}
275
280inline fl::i32 floorDiv16I32(fl::i32 v) FL_NO_EXCEPT { return v >> 16; }
281
283inline fl::i64 floorDiv16I64(fl::i64 v) FL_NO_EXCEPT { return v >> 16; }
284
292 // |x| < 2^45 + kPolyQ16Max and hw < 2^45 (see PolyEdge), so |v| < 2^47
293 // and v >> 16 fits i32 under the vertex clamp.
294 const fl::i32 c = static_cast<fl::i32>(floorDiv16I64(v));
295 if (c < -1) return -1;
296 if (c > width) return width;
297 return static_cast<int>(c); // fl-lint: int-template-ok clamped to [-1, width] first
298}
299
323struct PolyEdge {
324 fl::i64 x; // crossing x at current row center, Q16.16
325 fl::i64 step; // per-row increment, Q16.16 (0 for single-row edges)
326 fl::i32 rem; // 65536*B - step*dy, in [0, dy) (0 for single-row)
327 fl::i32 t; // running remainder accumulator
328 fl::i32 dy; // span height, Q16.16 (> 0)
329 fl::i32 B; // dx in the top-to-bottom direction, Q16.16 (signed)
330 fl::i32 ytop; // top endpoint y, Q16.16
331 fl::i32 ybot; // bottom endpoint y, Q16.16
332 fl::i64 hw; // per-row x half-span, ceil(32768*|B|/dy) (see above)
333 int py0; // first active canvas row (row containing ytop)
334 int py1; // last active canvas row (row containing ybot - 1)
335};
336
338// 1L: `1 << 28` is UB with a 16-bit int (AVR); GCC 7.3 evaluates it to 0,
339// which silently collapses all polygon coverage to zero (nothing renders).
340static const fl::i32 kPolyOne = 1L << 28;
341
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);
347 const fl::u64 p0 = (fl::u64)a0 * b0;
348 const fl::u64 p1 = (fl::u64)a0 * b1 + (fl::u64)a1 * b0;
349 const fl::u64 low = p0 + (p1 << 32);
350 hi = (fl::u64)a1 * b1 + (p1 >> 32) + (low < p0 ? 1u : 0u);
351 lo = low;
352}
353
355inline void polyAdd128(fl::u64 &hi, fl::u64 &lo, fl::u64 ahi, fl::u64 alo) FL_NO_EXCEPT {
356 const fl::u64 low = lo + alo;
357 hi += ahi + (low < lo ? 1u : 0u);
358 lo = low;
359}
360
375inline void polySortBand(fl::u8 *idx, fl::i64 *keys,
376 const PolyEdge *edges, int nc, fl::i32 mid) FL_NO_EXCEPT {
377 for (int e = 0; e < nc; ++e) {
378 const PolyEdge &ed = edges[idx[e]];
379 // (2*dy) fits in i32; (2*dy)*x and the sum wrap mod 2^64 as in the
380 // committed i64 expression. B*mid < 2^47 never wraps.
381 const fl::u64 num = (fl::u64)(2 * (fl::i64)ed.dy) * (fl::u64)ed.x
382 + (fl::u64)(static_cast<fl::i64>(ed.B) * (fl::i64)mid);
383 keys[e] = detail::floorDivI64(static_cast<fl::i64>(num),
384 (fl::i64)2 * ed.dy);
385 }
386 for (int e = 1; e < nc; ++e) {
387 const fl::i64 kv = keys[e];
388 const fl::u8 iv = idx[e];
389 int m = e - 1;
390 while (m >= 0 && keys[m] > kv) {
391 keys[m + 1] = keys[m];
392 idx[m + 1] = idx[m];
393 --m;
394 }
395 keys[m + 1] = kv;
396 idx[m + 1] = iv;
397 }
398}
399
402inline int polyClz32(fl::u32 v) FL_NO_EXCEPT {
403 int n = 0;
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; }
409 return n;
410}
411
414inline int polyTopBit128(fl::u32 m3, fl::u32 m2, fl::u32 m1,
415 fl::u32 m0) FL_NO_EXCEPT {
416 if (m3) return 127 - polyClz32(m3);
417 if (m2) return 95 - polyClz32(m2);
418 if (m1) return 63 - polyClz32(m1);
419 if (m0) return 31 - polyClz32(m0);
420 return -1;
421}
422
437 fl::u64 mhi, fl::u64 mlo, fl::u64 dv) FL_NO_EXCEPT {
438 fl::u32 m3 = (fl::u32)(mhi >> 32), m2 = (fl::u32)mhi;
439 fl::u32 m1 = (fl::u32)(mlo >> 32), m0 = (fl::u32)mlo;
440 const int top = polyTopBit128(m3, m2, m1, m0);
441 // The committed loop feeds bits 93..0 only; M bits above 93 are
442 // never read, so cap the window top at 93. In-range inputs never
443 // reach it (quotient < 2^29 and dv < 2^63 give M < 2^92).
444 const int t = (top < 94) ? top : 93;
445 // Left-align M by (127 - t) so its highest relevant bit reaches bit
446 // 127 of the window: the loop then feeds numerator bits with the
447 // fixed (w1 >> 63) extraction (constant-width shifts only — no
448 // libgcc __*di3 helpers on AVR).
449 const int k = 127 - t;
450 const int kl = k >> 5;
451 const int kb = k & 31;
452 {
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; }
458 if (kb) {
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;
465 n0 = n0 << kb;
466 }
467 m3 = n3; m2 = n2; m1 = n1; m0 = n0;
468 }
469 // The shift-subtract, expressed in 32-bit lanes: each lane op is
470 // the corresponding half of the u64 op it replaces (same carries,
471 // same mod-2^64 wraps of the remainder and quotient states), but
472 // avr-gcc inlines 32-bit shifts, compares and subtracts instead
473 // of emitting __lshrdi3/__ashldi3/__cmpdi2/__subdi3 helper calls
474 // per iteration — the dominant cost of this loop on AVR.
475 const fl::u32 d1 = (fl::u32)(dv >> 32), d0 = (fl::u32)dv;
476 fl::u32 s1 = 0, s0 = 0; // remainder (high, low), always < dv
477 fl::u32 q1 = 0, q0 = 0; // (q<<1)|ge each step; keeps the <64 bits
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);
483 m0 = m0 << 1;
484 const fl::u32 sh = s1 << 1, sl = s0 << 1;
485 s1 = sh | (s0 >> 31);
486 s0 = sl | bit;
487 const fl::u32 ge = (s1 > d1) || (s1 == d1 && s0 >= d0);
488 if (ge) {
489 const fl::u32 sb = s0 - d0;
490 s1 -= d1 + (fl::u32)(sb > s0);
491 s0 = sb;
492 }
493 const fl::u32 qh = q1 << 1, ql = q0 << 1;
494 q1 = qh | (q0 >> 31);
495 q0 = ql | ge;
496 }
497 // Round half-up: if 2r >= dv, q++ (r = (s1, s0)). The carry into
498 // r1 comes out of s0's MSB; s1's MSB is the 2r overflow bit.
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); // carry: q0 wrapped past 0xFFFFFFFF
503 q0 = qb;
504 }
505 fl::u64 q = ((fl::u64)q1 << 32) | q0;
506 if (q > (fl::u64)kPolyOne) q = (fl::u64)kPolyOne; // physical bound
507 return (fl::i32)q;
508}
509
541inline fl::i32 polyLineT28(fl::i32 B, fl::i32 D, fl::i64 u,
542 fl::i32 ta, fl::i32 tb) FL_NO_EXCEPT {
543 if (B == 0) {
544 // Vertical line: x is constant, cov = clamp(u0, 0, 1) * (tb-ta)/65536
545 // with u0 = u/D exact (u = D * u0).
546 fl::i64 w = u / D;
547 if (w < 0) w = 0;
548 else if (w > 65536) w = 65536;
549 return (fl::i32)(((w * (tb - ta)) + 8) >> 4);
550 }
551 const fl::i64 S = (B < 0) ? -(fl::i64)B : (fl::i64)B;
552 const fl::i64 sa = (fl::i64)B * ta;
553 const fl::i64 sb = (fl::i64)B * tb;
554 // s = B*tau runs over [sa, sb] as tau runs [ta, tb]; for B < 0 the
555 // interval is reversed — use min/max (the area is orientation-free).
556 const fl::i64 s_lo = (sa < sb) ? sa : sb;
557 const fl::i64 s_hi = (sa < sb) ? sb : sa;
558 const fl::i64 c0 = -u;
559 const fl::i64 c1 = (fl::i64)2 * D * 32768 - u;
560 fl::u64 mhi = 0, mlo = 0;
561 const fl::i64 lo = (s_lo > c0) ? s_lo : c0;
562 const fl::i64 hi = (s_hi < c1) ? s_hi : c1;
563 if (hi > lo) {
564 // (u+lo) and (u+hi) each lie in [0, 2*D*32768] (lo >= c0, hi <= c1),
565 // so both factors are non-negative and < 2^48.
566 polyMul128((fl::u64)(hi - lo),
567 (fl::u64)((u + lo) + (u + hi)), mhi, mlo);
568 }
569 const fl::i64 lo2 = (s_lo > c1) ? s_lo : c1;
570 if (s_hi > lo2) {
571 fl::u64 phi = 0, plo = 0;
572 polyMul128((fl::u64)(s_hi - lo2),
573 (fl::u64)((fl::i64)4 * D * 32768), phi, plo);
574 polyAdd128(mhi, mlo, phi, plo);
575 }
576 if (mhi == 0 && mlo == 0) return 0; // line misses the pixel in [ta, tb]
577 // den = 32 * D * S as a u64 pair (D*S < 2^62, so den_hi = DS >> 59 and
578 // den < 2^67).
579 const fl::u64 DS = (fl::u64)S * (fl::u64)D;
580 const fl::u64 dhi = DS >> 59;
581 const fl::u64 dlo = DS << 5;
582 // The loop below is a 64-bit remainder state machine
583 // s <- 2s + bit, s -= dv when s >= dv. It is an exact long division
584 // only while dv < 2^63 (s < dv keeps 2s + bit below 2^64). The
585 // committed implementation always fed den itself, so for den >= 2^63
586 // the carry out of the remainder's MSB was dropped (den in
587 // [2^63, 2^64)) or the quotient collapsed to 0/1 (den >= 2^64), giving
588 // wrong coverage for edges of dx*dy >= 2^26 px^2 (#5286). Those huge
589 // edges instead divide M' = M >> 4 by dv = den >> 4 = 2*DS < 2^63:
590 // floor(M' / dv) == floor(M / den) (nested floors collapse), and
591 // with R = 16*r' + (M mod 16), 2R >= den <=> 2r' + bit3(M) >= dv
592 // <=> 2r' >= dv, because 2r' and dv are both even.
593 // So quotient AND round-half-up are exact with the unchanged loop and
594 // rounding in polyDivRoundT28; only the huge-edge setup is new.
595 if (dhi != 0 || (dlo >> 63) != 0) {
596 const fl::u64 nlo = (mlo >> 4) | (mhi << 60); // M < 2^98, so
597 return polyDivRoundT28(mhi >> 4, nlo, DS << 1); // M >> 4 < 2^94
598 }
599 // Fast path: if mhi == 0 the numerator is < 2^64, so the loop's q
600 // accumulation (at most 64 steps) cannot wrap its u64 and the whole
601 // loop reduces to one exact 64/64 division plus the same round-half-up
602 // and physical clamp. On AVR the division lowers to __udivdi3 (already
603 // linked for the sort keys). dlo != 0 keeps this total for degenerate
604 // direct calls (the committed loop never divided; drawPolygon always
605 // feeds den > 0).
606 if (dlo != 0 && mhi == 0) {
607 fl::u64 q = mlo / dlo;
608 const fl::u64 r = mlo - q * dlo; // mlo % dlo, one division
609 if ((r >> 63) != 0 || (r << 1) >= dlo) ++q;
610 if (q > (fl::u64)kPolyOne) q = (fl::u64)kPolyOne;
611 return (fl::i32)q;
612 }
613 return polyDivRoundT28(mhi, mlo, dlo);
614}
615
619template<typename PixelT>
620struct DiscCtx {
621 fl::i32 xdelta0;
622 int xmin, xmax;
623 fl::i32 rin2, rout2;
625 fl::u16 band_inv; // reciprocal: (255 * 256 + scaled/2) / scaled
626 PixelT color;
627};
628
632template<typename PixelT>
633struct RingCtx {
634 fl::i32 xdelta0;
635 int xmin, xmax;
636 fl::i32 ii2, io2, oi2, oo2;
638 fl::u16 inner_inv, outer_inv; // reciprocals: (255 * 256 + scaled/2) / scaled
639 PixelT color;
640};
641
645template<typename PixelT, bool Overwrite>
646inline void renderDiscRow(PixelT* buf, int w, int py,
647 fl::i32 d2_row,
648 const DiscCtx<PixelT>& f) FL_NO_EXCEPT {
649 PixelT* ptr = &buf[py * w + f.xmin];
650 fl::i32 d2 = d2_row;
651 fl::i32 xd = f.xdelta0;
652 int px = f.xmin;
653 // Phase 1: Skip outside pixels (left, d2 decreasing)
654 while (px <= f.xmax && d2 >= f.rout2) {
655 d2 += xd; xd += 131072; ++ptr; ++px;
656 }
657 // Phase 2: Outer AA fringe (left)
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;
664 }
665 // Phase 3: Full brightness interior (d2 hits minimum, then increases)
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;
669 }
670 // Phase 4: Outer AA fringe (right, d2 increasing)
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;
677 }
678}
679
684template<typename PixelT, bool Overwrite>
685inline void renderRingRow(PixelT* buf, int w, int py,
686 fl::i32 d2_row,
688 PixelT* ptr = &buf[py * w + g.xmin];
689 fl::i32 d2 = d2_row;
690 fl::i32 xd = g.xdelta0;
691 int px = g.xmin;
692 // Preamble: left exterior (d2 >= oo2)
693 while (px <= g.xmax && d2 >= g.oo2) {
694 d2 += xd; xd += 131072; ++ptr; ++px;
695 }
696 // Phase 1: outer-left AA fringe (oi2 <= d2 < oo2)
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);
699 fl::u8 br = static_cast<fl::u8>((diff * g.outer_inv) >> 8);
700 PixelT c = g.color; c.nscale8(br);
701 if (Overwrite) *ptr = c; else *ptr += c;
702 d2 += xd; xd += 131072; ++ptr; ++px;
703 }
704 // Phase 2: full-brightness band left (io2 <= d2 < oi2)
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;
708 }
709 // Phase 3: inner-left AA fringe (ii2 <= d2 < io2)
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);
712 fl::u8 br = static_cast<fl::u8>((diff * g.inner_inv) >> 8);
713 PixelT c = g.color; c.nscale8(br);
714 if (Overwrite) *ptr = c; else *ptr += c;
715 d2 += xd; xd += 131072; ++ptr; ++px;
716 }
717 // Phase 4: transparent hole (d2 < ii2)
718 while (px <= g.xmax && d2 < g.ii2) {
719 d2 += xd; xd += 131072; ++ptr; ++px;
720 }
721 // Phase 5: inner-right AA fringe (ii2 <= d2 < io2)
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);
724 fl::u8 br = static_cast<fl::u8>((diff * g.inner_inv) >> 8);
725 PixelT c = g.color; c.nscale8(br);
726 if (Overwrite) *ptr = c; else *ptr += c;
727 d2 += xd; xd += 131072; ++ptr; ++px;
728 }
729 // Phase 6: full-brightness band right (io2 <= d2 < oi2)
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;
733 }
734 // Phase 7: outer-right AA fringe (oi2 <= d2 < oo2)
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);
737 fl::u8 br = static_cast<fl::u8>((diff * g.outer_inv) >> 8);
738 PixelT c = g.color; c.nscale8(br);
739 if (Overwrite) *ptr = c; else *ptr += c;
740 d2 += xd; xd += 131072; ++ptr; ++px;
741 }
742}
743
748template<typename PixelT>
749struct StrokeCtx {
750 fl::i32 threshold_q;
751 fl::i32 len2_q;
752 fl::i32 dx_q, dy_q;
754 fl::u16 aa_inv;
755 fl::i32 x0_8, y0_8, x1_8, y1_8;
756 fl::i32 r_max2_8;
758 fl::u16 cap_inv;
759 fl::i32 dot_ext_q;
761 PixelT color;
762 int xmin, xmax;
763};
764
768template<typename PixelT, bool Overwrite>
769inline void renderStrokeRow(PixelT* buf, int w, int py,
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;
775 int px = sc.xmin;
776 const fl::i32 thr = sc.threshold_q;
777 const fl::i32 neg_thr = -thr;
778
779 // Phase 1: Skip pixels outside the band
780 while (px <= sc.xmax && (cross >= thr || cross < neg_thr)) {
781 cross += sc.dy_q;
782 dot += sc.dx_q;
783 ++ptr; ++px;
784 }
785
786 // Hoist per-row endcap ey values (py is constant within a row)
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;
790
791 // Phase 2: Process visible pixels (|cross| < threshold)
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);
795
796 if (on_segment) {
797 // Integer AA: shift+multiply (no Coord multiply)
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);
801 // Fast approximate x/255 ≈ (x + 1 + (x >> 8)) >> 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);
804 PixelT c = sc.color;
805 c.nscale8(FL_PGM_READ_BYTE_NEAR(&distanceAA_LUT[sq_idx]));
806 if (Overwrite) *ptr = c; else *ptr += c;
807 } else if (sc.cap == LineCap::ROUND) {
808 // Endpoint distance in 8.8 fixed-point
809 fl::i32 ex8;
810 fl::i16 ey_s;
811 if (dot < 0) {
812 ex8 = (static_cast<fl::i32>(px) << 8) - sc.x0_8;
813 ey_s = static_cast<fl::i16>(ey8_y0);
814 } else {
815 ex8 = (static_cast<fl::i32>(px) << 8) - sc.x1_8;
816 ey_s = static_cast<fl::i16>(ey8_y1);
817 }
818 fl::i16 ex_s = static_cast<fl::i16>(ex8);
819 // 16×16→32 multiply (4 MUL on AVR vs ~70 for 32×32)
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);
826 PixelT c = sc.color;
827 c.nscale8(FL_PGM_READ_BYTE_NEAR(&distanceAA_LUT[idx]));
828 if (Overwrite) *ptr = c; else *ptr += c;
829 }
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);
837 PixelT c = sc.color;
838 c.nscale8(FL_PGM_READ_BYTE_NEAR(&distanceAA_LUT[sq_idx]));
839 if (Overwrite) *ptr = c; else *ptr += c;
840 }
841 }
842 // FLAT: pixels outside [0, len2] are silently dropped
843
844 cross += sc.dy_q;
845 dot += sc.dx_q;
846 ++ptr; ++px;
847 }
848 // Phase 3: remaining pixels are outside the band — nothing to do
849}
850
851} // namespace detail
852
858
863template<typename PixelT, typename Coord>
864void drawLine(Canvas<PixelT>& canvas, const PixelT& color,
865 Coord x0, Coord y0, Coord x1, Coord y1,
867
868template<typename PixelT, typename Coord>
869void drawDisc(Canvas<PixelT>& canvas, const PixelT& color,
870 Coord cx, Coord cy, Coord r,
872
873template<typename PixelT, typename Coord>
874void drawRing(Canvas<PixelT>& canvas, const PixelT& color,
875 Coord cx, Coord cy, Coord r, Coord thickness,
877
878template<typename PixelT, typename Coord>
879void drawStrokeLine(Canvas<PixelT>& canvas, const PixelT& color,
880 Coord x0, Coord y0, Coord x1, Coord y1, Coord thickness,
882
883template<typename PixelT, typename Coord>
885 fl::span<const PolyPoint<Coord>> pts,
887
888template<typename PixelT, typename Coord, int N>
890 const PolyPoint<Coord> (&pts)[N],
892
896
900
901// ---------------------------------------------------------------------------
902// drawLine: Integer Wu antialiased line (8.8 fixed-point internally)
903// Uses int loop counter and bit-shift for AA — no Coord ops in inner loop.
904// ---------------------------------------------------------------------------
905namespace detail {
906
907template<typename PixelT, typename Coord, bool Overwrite>
908inline void drawLineCore(Canvas<PixelT>& canvas, const PixelT& color,
909 Coord x0, Coord y0, Coord x1, Coord y1) FL_NO_EXCEPT {
910 PixelT* pixels = canvas.pixels;
911 int width = canvas.width;
912 int height = canvas.height;
913
914 // Convert to 8.8 fixed-point
915 fl::i32 fx0 = detail::toFixed8(x0);
916 fl::i32 fy0 = detail::toFixed8(y0);
917 fl::i32 fx1 = detail::toFixed8(x1);
918 fl::i32 fy1 = detail::toFixed8(y1);
919
920 bool steep = fl::abs(fy1 - fy0) > fl::abs(fx1 - fx0);
921 if (steep) { fl::swap(fx0, fy0); fl::swap(fx1, fy1); }
922 if (fx0 > fx1) { fl::swap(fx0, fx1); fl::swap(fy0, fy1); }
923
924 fl::i32 dx8 = fx1 - fx0;
925 fl::i32 dy8 = fy1 - fy0;
926
927 // Gradient per pixel step in 8.8: (dy * 256) / dx
928 fl::i32 gradient = (dx8 == 0) ? 0 : ((dy8 << 8) / dx8);
929
930 // First endpoint: round x to nearest pixel boundary
931 fl::i32 xend = (fx0 + 128) & ~0xFF;
932 fl::i32 yend = fy0 + ((gradient * (xend - fx0)) >> 8);
933 fl::i32 xgap = 256 - ((fx0 + 128) & 0xFF); // rfpart(x0 + 0.5)
934 int xpxl1 = xend >> 8;
935 int ypxl1 = yend >> 8;
936 fl::u8 yfrac1 = static_cast<fl::u8>(yend & 0xFF);
937
938 // Second endpoint
939 fl::i32 xend2 = (fx1 + 128) & ~0xFF;
940 fl::i32 yend2 = fy1 + ((gradient * (xend2 - fx1)) >> 8);
941 fl::i32 xgap2 = (fx1 + 128) & 0xFF; // fpart(x1 + 0.5)
942 int xpxl2 = xend2 >> 8;
943 int ypxl2 = yend2 >> 8;
944 fl::u8 yfrac2 = static_cast<fl::u8>(yend2 & 0xFF);
945
946 // Draw first endpoint
947 {
948 fl::u8 b1 = static_cast<fl::u8>(((255 - yfrac1) * xgap) >> 8);
949 fl::u8 b2 = static_cast<fl::u8>((yfrac1 * xgap) >> 8);
950 if (steep) {
951 PixelT c = color; c.nscale8(b1);
952 addPixelToBuffer<PixelT, Overwrite>(pixels, width, height, ypxl1, xpxl1, c);
953 c = color; c.nscale8(b2);
954 addPixelToBuffer<PixelT, Overwrite>(pixels, width, height, ypxl1 + 1, xpxl1, c);
955 } else {
956 PixelT c = color; c.nscale8(b1);
957 addPixelToBuffer<PixelT, Overwrite>(pixels, width, height, xpxl1, ypxl1, c);
958 c = color; c.nscale8(b2);
959 addPixelToBuffer<PixelT, Overwrite>(pixels, width, height, xpxl1, ypxl1 + 1, c);
960 }
961 }
962
963 // Draw second endpoint
964 {
965 fl::u8 b1 = static_cast<fl::u8>(((255 - yfrac2) * xgap2) >> 8);
966 fl::u8 b2 = static_cast<fl::u8>((yfrac2 * xgap2) >> 8);
967 if (steep) {
968 PixelT c = color; c.nscale8(b1);
969 addPixelToBuffer<PixelT, Overwrite>(pixels, width, height, ypxl2, xpxl2, c);
970 c = color; c.nscale8(b2);
971 addPixelToBuffer<PixelT, Overwrite>(pixels, width, height, ypxl2 + 1, xpxl2, c);
972 } else {
973 PixelT c = color; c.nscale8(b1);
974 addPixelToBuffer<PixelT, Overwrite>(pixels, width, height, xpxl2, ypxl2, c);
975 c = color; c.nscale8(b2);
976 addPixelToBuffer<PixelT, Overwrite>(pixels, width, height, xpxl2, ypxl2 + 1, c);
977 }
978 }
979
980 // Main loop: integer counter, 8.8 intery accumulation
981 fl::i32 intery = yend + gradient;
982 if (steep) {
983 for (int x = xpxl1 + 1; x < xpxl2; ++x) {
984 int y = intery >> 8;
985 fl::u8 frac = static_cast<fl::u8>(intery & 0xFF);
986 PixelT c = color; c.nscale8(255 - frac);
988 c = color; c.nscale8(frac);
990 intery += gradient;
991 }
992 } else {
993 for (int x = xpxl1 + 1; x < xpxl2; ++x) {
994 int y = intery >> 8;
995 fl::u8 frac = static_cast<fl::u8>(intery & 0xFF);
996 PixelT c = color; c.nscale8(255 - frac);
998 c = color; c.nscale8(frac);
1000 intery += gradient;
1001 }
1002 }
1003}
1004
1005template<typename PixelT, typename Coord, bool Overwrite>
1006inline void drawDiscCore(Canvas<PixelT>& canvas, const PixelT& color,
1007 Coord cx, Coord cy, Coord r) FL_NO_EXCEPT;
1008
1009template<typename PixelT, typename Coord, bool Overwrite>
1010inline void drawRingCore(Canvas<PixelT>& canvas, const PixelT& color,
1011 Coord cx, Coord cy, Coord r, Coord thickness) FL_NO_EXCEPT;
1012
1013template<typename PixelT, typename Coord, bool Overwrite>
1014inline void drawStrokeLineCore(Canvas<PixelT>& canvas, const PixelT& color,
1015 Coord x0, Coord y0, Coord x1, Coord y1,
1016 Coord thickness, LineCap cap) FL_NO_EXCEPT;
1017
1018template<typename PixelT, typename Coord, bool Overwrite, int MAXN, int CHUNK>
1019inline void drawPolygonCore(Canvas<PixelT>& canvas, const PixelT& color,
1021
1022} // namespace detail
1023
1024template<typename PixelT, typename Coord>
1025inline void drawLine(Canvas<PixelT>& canvas, const PixelT& color,
1026 Coord x0, Coord y0, Coord x1, Coord y1,
1030 else
1032}
1033
1034// ---------------------------------------------------------------------------
1035// drawDiscCore: Templated on Overwrite for compile-time dispatch.
1036// ---------------------------------------------------------------------------
1037template<typename PixelT, typename Coord, bool Overwrite>
1039 Coord cx, Coord cy, Coord r) FL_NO_EXCEPT {
1040 PixelT* pixels = canvas.pixels;
1041 int width = canvas.width;
1042 int height = canvas.height;
1043
1044 fl::i32 cx8 = detail::toFixed8(cx);
1045 fl::i32 cy8 = detail::toFixed8(cy);
1046 fl::i32 r8 = detail::toFixed8(r);
1047
1048 fl::i32 rin8 = r8 - 128; // r - 0.5 in 8.8
1049 fl::i32 rout8 = r8 + 128; // r + 0.5 in 8.8
1050
1051 fl::i32 rin2 = rin8 * rin8;
1052 fl::i32 rout2 = rout8 * rout8;
1053 fl::i32 band = rout2 - rin2;
1054 if (band <= 0) return;
1055
1056 int ri = (rout8 >> 8) + 1;
1057 int cxi = cx8 >> 8;
1058 int cyi = cy8 >> 8;
1059
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;
1064
1065 fl::i32 dx8 = (static_cast<fl::i32>(xmin) << 8) - cx8;
1066 fl::i32 dx2 = dx8 * dx8;
1067
1069 fc.xdelta0 = 512 * dx8 + 65536;
1070 fc.xmin = xmin; fc.xmax = xmax;
1071 fc.rin2 = rin2; fc.rout2 = rout2;
1073 fc.color = color;
1074
1075 fl::i32 cyfrac = (static_cast<fl::i32>(cyi) << 8) - cy8;
1076 fl::i32 d2c = dx2 + cyfrac * cyfrac;
1077
1078 if (cyi >= 0 && cyi < height)
1079 detail::renderDiscRow<PixelT, Overwrite>(pixels, width, cyi, d2c, fc);
1080
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;
1085
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)
1092 detail::renderDiscRow<PixelT, Overwrite>(pixels, width, pyb, botd2, fc);
1093 if (ctop && pyt >= 0 && pyt < height)
1094 detail::renderDiscRow<PixelT, Overwrite>(pixels, width, pyt, topd2, fc);
1095 botd2 += botdelta; botdelta += 131072;
1096 topd2 += topdelta; topdelta += 131072;
1097 }
1098}
1099
1100template<typename PixelT, typename Coord>
1101inline void drawDisc(Canvas<PixelT>& canvas, const PixelT& color,
1102 Coord cx, Coord cy, Coord r,
1106 else
1108}
1109
1110// ---------------------------------------------------------------------------
1111// drawRingCore: Templated on Overwrite for compile-time dispatch.
1112// ---------------------------------------------------------------------------
1113template<typename PixelT, typename Coord, bool Overwrite>
1115 Coord cx, Coord cy, Coord r, Coord thickness) FL_NO_EXCEPT {
1116 PixelT* pixels = canvas.pixels;
1117 int width = canvas.width;
1118 int height = canvas.height;
1119
1120 fl::i32 cx8 = detail::toFixed8(cx);
1121 fl::i32 cy8 = detail::toFixed8(cy);
1122 fl::i32 r8 = detail::toFixed8(r);
1123 fl::i32 t8 = detail::toFixed8(thickness);
1124
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;
1129
1130 if (r_ii8 < 0) r_ii8 = 0;
1131 if (r_io8 < 0) r_io8 = 0;
1132
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;
1137
1138 fl::i32 inner_band = io2 - ii2;
1139 fl::i32 outer_band = oo2 - oi2;
1140
1141 int ri = (r_oo8 >> 8) + 1;
1142 int cxi = cx8 >> 8;
1143 int cyi = cy8 >> 8;
1144
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;
1149
1150 fl::i32 dx8 = (static_cast<fl::i32>(xmin) << 8) - cx8;
1151 fl::i32 dx2 = dx8 * dx8;
1152
1154 gc.xdelta0 = 512 * dx8 + 65536;
1155 gc.xmin = xmin; gc.xmax = xmax;
1156 gc.ii2 = ii2; gc.io2 = io2; gc.oi2 = oi2; gc.oo2 = oo2;
1157 if (inner_band > 0) detail::computeBandShift(inner_band, gc.inner_shift, gc.inner_inv);
1158 else { gc.inner_shift = 0; gc.inner_inv = 65280u; }
1159 if (outer_band > 0) detail::computeBandShift(outer_band, gc.outer_shift, gc.outer_inv);
1160 else { gc.outer_shift = 0; gc.outer_inv = 65280u; }
1161 gc.color = color;
1162
1163 fl::i32 cyfrac = (static_cast<fl::i32>(cyi) << 8) - cy8;
1164 fl::i32 d2c = dx2 + cyfrac * cyfrac;
1165
1166 if (cyi >= 0 && cyi < height)
1167 detail::renderRingRow<PixelT, Overwrite>(pixels, width, cyi, d2c, gc);
1168
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;
1173
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)
1180 detail::renderRingRow<PixelT, Overwrite>(pixels, width, pyb, botd2, gc);
1181 if (ctop && pyt >= 0 && pyt < height)
1182 detail::renderRingRow<PixelT, Overwrite>(pixels, width, pyt, topd2, gc);
1183 botd2 += botdelta; botdelta += 131072;
1184 topd2 += topdelta; topdelta += 131072;
1185 }
1186}
1187
1188template<typename PixelT, typename Coord>
1189inline void drawRing(Canvas<PixelT>& canvas, const PixelT& color,
1190 Coord cx, Coord cy, Coord r, Coord thickness,
1194 else
1196}
1197
1198// ---------------------------------------------------------------------------
1199// drawStrokeLineCore: Templated on Overwrite for compile-time dispatch.
1200// ---------------------------------------------------------------------------
1201template<typename PixelT, typename Coord, bool Overwrite>
1203 Coord x0, Coord y0, Coord x1, Coord y1,
1204 Coord thickness, LineCap cap) FL_NO_EXCEPT {
1205 PixelT* pixels = canvas.pixels;
1206 int width = canvas.width;
1207 int height = canvas.height;
1208
1209 fl::i32 x0_8 = detail::toFixed8(x0), y0_8 = detail::toFixed8(y0);
1210 fl::i32 x1_8 = detail::toFixed8(x1), y1_8 = detail::toFixed8(y1);
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;
1214
1215 fl::i32 len2_16 = dx_8 * dx_8 + dy_8 * dy_8;
1216 fl::i32 len_8 = static_cast<fl::i32>(
1217 fl::isqrt32(static_cast<fl::u32>(len2_16)));
1218 fl::i32 thickness_8 = detail::toFixed8(thickness);
1219 fl::i32 r_max_8 = thickness_8 >> 1;
1220 fl::i32 threshold_q = r_max_8 * len_8;
1221
1222 if (threshold_q <= 0) return;
1223
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;
1231
1232 if (xmin < 0) xmin = 0;
1233 if (xmax >= width) xmax = width - 1;
1234 if (ymin < 0) ymin = 0;
1235 if (ymax >= height) ymax = height - 1;
1236 if (xmin > xmax || ymin > ymax) return;
1237
1238 fl::i32 dx_q = dx_8 << 8;
1239 fl::i32 dy_q = dy_8 << 8;
1240
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;
1245
1246 fl::i32 len2_q = len_8 * len_8;
1247 fl::i32 dot_ext_q = (cap == LineCap::SQUARE) ? threshold_q : 0;
1248
1250 sc.threshold_q = threshold_q;
1251 sc.len2_q = len2_q;
1252 sc.dx_q = dx_q;
1253 sc.dy_q = dy_q;
1254 sc.dot_ext_q = dot_ext_q;
1255 detail::computeBandShift(threshold_q, sc.aa_shift, sc.aa_inv);
1256 sc.x0_8 = x0_8; sc.y0_8 = y0_8;
1257 sc.x1_8 = x1_8; sc.y1_8 = y1_8;
1258 sc.r_max2_8 = r_max_8 * r_max_8;
1259 sc.cap_shift = 0; sc.cap_inv = 0;
1260 if (cap == LineCap::ROUND && sc.r_max2_8 > 0) {
1262 }
1263 sc.cap = cap;
1264 sc.color = color;
1265 sc.xmin = xmin; sc.xmax = xmax;
1266
1267 for (int py = ymin; py <= ymax; ++py) {
1268 if (py >= 0 && py < height) {
1270 cross_row_q, dot_row_q, sc);
1271 }
1272 cross_row_q -= sc.dx_q;
1273 dot_row_q += sc.dy_q;
1274 }
1275}
1276
1277template<typename PixelT, typename Coord>
1278inline void drawStrokeLine(Canvas<PixelT>& canvas, const PixelT& color,
1279 Coord x0, Coord y0, Coord x1, Coord y1, Coord thickness,
1280 LineCap cap, fl::DrawMode mode) FL_NO_EXCEPT {
1282 detail::drawStrokeLineCore<PixelT, Coord, true>(canvas, color, x0, y0, x1, y1, thickness, cap);
1283 else
1284 detail::drawStrokeLineCore<PixelT, Coord, false>(canvas, color, x0, y0, x1, y1, thickness, cap);
1285}
1286
1287// ---------------------------------------------------------------------------
1288// drawPolygonCore: even-odd fill with an exact per-pixel area model,
1289// organized as row bands.
1290//
1291// Each non-horizontal edge is active in every canvas row it geometrically
1292// occupies (py0 = the row containing ytop, py1 = the row containing ybot),
1293// and each row is split into sub-bands at vertex events (vertices whose y
1294// falls strictly inside the row). Within a band the crossing set is
1295// constant: an edge participates iff ytop < band_top && ybot > band_bottom
1296// (strict — at each vertex one edge ends and the other begins, so the
1297// crossing count is the same in every band of a closed polygon). Edges in a
1298// band are ordered by their floored Q16.16 x at the band midpoint (the
1299// committed implementation's midpoint key, polySortBand — stable on equal
1300// keys) and paired (0,1), (2,3),... For simple polygons this is exactly
1301// the even-odd rule; self-intersecting polygons follow the same
1302// deterministic span-pairing convention.
1303//
1304// The per-row scan is division-free: the crossing x (anchored at the row
1305// center, which polyLineT28 integrates from) advances by the Bresenham
1306// step/rem recurrence, and the per-row column extent is (x +/- hw) >> 16 —
1307// a shift — where hw = ceil(32768*|B|/dy) was computed once at setup.
1308// (MAXN, CHUNK) bound the
1309// stack: MAXN caps the vertex count, CHUNK the per-row accumulator width
1310// (CHUNK must be a power of two).
1311//
1312// Pixel coverage is accumulated additively over the row's bands into a
1313// CHUNK-px accumulator in T units (kPolyOne = 2^28 = full pixel), then
1314// quantized once per pixel: br = (acc*255 + ONE/2) >> 28. Coverage is
1315// exact for linear edges (see polyLineT28): for a simple polygon the
1316// per-pixel sum equals the true sub-pixel area, so total energy equals the
1317// shoelace area up to u8 rounding.
1318//
1319// Vertices beyond MAXN are ignored (the polygon closes from #MAXN-1 back to
1320// #0). Stack scales with the tier: vertices (8*MAXN B) + edges (56*MAXN B)
1321// + accb (4*CHUNK B) + per-row scratch + band prepass bidx/bnc/bfull
1322// ((MAXN+1)*(MAXN+5) B). Measured frames and the platform-dependent default
1323// tier are documented at FL_GFX_POLYGON_DEFAULT_MAXN in fl/gfx/canvas.h.
1324// ---------------------------------------------------------------------------
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());
1331 if (n < 3) return;
1332 PixelT* pixels = canvas.pixels;
1333 int width = canvas.width;
1334 int height = canvas.height;
1335 if (width <= 0 || height <= 0) return;
1336
1337 const int nv = (n < MAXN) ? n : MAXN;
1338
1339 // Convert vertices to Q16.16 (clamped to ±8191 px — i32-safe)
1340 fl::i32 vx[MAXN];
1341 fl::i32 vy[MAXN];
1342 for (int i = 0; i < nv; ++i) {
1343 vx[i] = detail::toQ16Clamped(pts[i].x);
1344 vy[i] = detail::toQ16Clamped(pts[i].y);
1345 }
1346
1347 // Build per-edge scanline state. Two i64 floor divisions per edge
1348 // (anchor crossing + step) are the only divisions in this function.
1349 PolyEdge edges[MAXN];
1350 int nedges = 0;
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; // horizontal edge: ignored (even-odd convention)
1356 // Orient edge top-to-bottom: B measured from the top vertex
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; // span height, > 0
1361 const fl::i32 ybot = ytop + dy2;
1362
1363 // Active pixel rows: the row containing ytop through the row
1364 // containing ybot (floor/ceil rule; rows are half-open so a vertex
1365 // on a row boundary belongs to exactly one edge's row range).
1366 int py0 = ytop >> 16;
1367 int py1 = (ybot - 1) >> 16;
1368 if (py0 < 0) py0 = 0;
1369 if (py1 >= height) py1 = height - 1;
1370 if (py0 > py1) continue; // edge off-canvas vertically
1371
1372 PolyEdge& ed = edges[nedges++];
1373 ed.py0 = py0;
1374 ed.py1 = py1;
1375 ed.dy = dy2;
1376 ed.B = B;
1377 ed.ytop = ytop;
1378 ed.ybot = ybot;
1379
1380 // Exact crossing at py0's row center (one i64 floor division):
1381 // x = xtop + floor(w0 * B / dy2), w0 = (py0<<16) + 32768 - ytop
1382 const fl::i64 w0 = (static_cast<fl::i64>(py0) << 16) + 32768 - ytop;
1383 const fl::i64 prod = w0 * B;
1384 const fl::i64 q = detail::floorDivI64(prod, dy2);
1385 ed.x = xtop + q;
1386 // 65536*B = s*dy2 + rem, rem in [0, dy2). s is the per-row x
1387 // advance (used by the carry on multi-row edges only) and, with
1388 // rem, yields the row's x half-span hw = ceil(32768*|B|/dy2)
1389 // without a second division:
1390 // B >= 0: 2*hw*dy2 = s*dy2 + rem ->
1391 // hw = (s>>1) + (s&1) + (s even && rem > 0)
1392 // B < 0: -s = ceil(65536*|B|/dy2) -> hw = ceil(-s/2)
1393 const fl::i64 p2 = static_cast<fl::i64>(65536) * B;
1394 const fl::i64 s = detail::floorDivI64(p2, dy2);
1395 const fl::i32 rem = static_cast<fl::i32>(p2 - s * dy2);
1396 ed.hw = (B >= 0)
1397 ? ((s >> 1) + (s & 1) + ((s & 1) == 0 && rem > 0 ? 1 : 0))
1398 : (((-s) >> 1) + ((-s) & 1));
1399 if (dy2 >= 65536) {
1400 ed.step = s;
1401 ed.rem = rem;
1402 ed.t = static_cast<fl::i32>(prod - q * dy2); // in [0, dy2)
1403 } else {
1404 // Span < 1 px: may still span two canvas rows (a row-boundary
1405 // crossing), but the carry stays a no-op (step = rem = t = 0):
1406 // x keeps its py0-row value and the per-row extent (x +/- hw)
1407 // boxes the line the kernel integrates — see PolyEdge.
1408 ed.step = 0;
1409 ed.rem = 0;
1410 ed.t = 0;
1411 }
1412 if (ed.py0 < ymin) ymin = ed.py0;
1413 if (ed.py1 > ymax) ymax = ed.py1;
1414 }
1415 if (nedges == 0) return;
1416
1417 // Scan rows
1418 fl::i32 ev[MAXN + 2]; // band boundaries: row ends + vertex y's in row
1419 fl::u8 idx[MAXN]; // a band's active edges, sorted by midpoint key
1420 fl::i64 keys[MAXN]; // per-edge midpoint sort keys (see polySortBand)
1421 int lx0a[MAXN]; // per-row per-edge column extents (left, right),
1422 int lx1a[MAXN]; // clamped to [-1, width] (see polyColClamped)
1423 fl::i32 accb[CHUNK]; // per-row CHUNK-px coverage accumulator (T units)
1424
1425 for (int py = ymin; py <= ymax; ++py) {
1426 const fl::i32 yc = (static_cast<fl::i32>(py) << 16) + 32768;
1427
1428 // Band boundaries: the row ends plus every vertex strictly inside
1429 // the row (a vertex exactly on a row boundary coincides with a band
1430 // edge — the floor/ceil row rule already splits the edges there).
1431 int nev = 0;
1432 ev[nev++] = -32768;
1433 ev[nev++] = 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;
1437 }
1438 // Insertion sort (nev <= MAXN + 2)
1439 for (int a = 1; a < nev; ++a) {
1440 const fl::i32 v = ev[a];
1441 int b = a - 1;
1442 while (b >= 0 && ev[b] > v) { ev[b + 1] = ev[b]; --b; }
1443 ev[b + 1] = v;
1444 }
1445
1446 // Per-row column extents of each active edge (x is at the row
1447 // center): the line stays within x +/- hw over the row, so px
1448 // outside [lx0, lx1] is a full/zero shortcut for polyLineT28.
1449 // floor(v / 65536) == v >> 16 — shifts, not divisions. The loop
1450 // also yields the row extent actually touched (xmin/xmax): a span
1451 // pair's range is [min(L.lx0, R.lx0), max(L.lx1, R.lx1)] — it
1452 // can reach BETWEEN two off-canvas edges (e.g. a polygon covering
1453 // the whole canvas), so the bounds are the min lx0 / max lx1 over
1454 // ALL row-active edges, clamped once. Pairs partition the edges
1455 // (odd count: the unpaired last edge only widens the range), so
1456 // this superset can only add no-op chunks that the fill loop
1457 // skips; the exact-x ordering of polySortBand only affects span
1458 // pairing.
1459 int minLx0 = 0, maxLx1 = 0;
1460 int nactive = 0;
1461 for (int e = 0; e < nedges; ++e) {
1462 const PolyEdge& ed = edges[e];
1463 if (py < ed.py0 || py > ed.py1) continue;
1464 lx0a[e] = detail::polyColClamped(ed.x - ed.hw, width);
1465 lx1a[e] = detail::polyColClamped(ed.x + ed.hw, width);
1466 if (nactive++ == 0) { minLx0 = lx0a[e]; maxLx1 = lx1a[e]; }
1467 else {
1468 if (lx0a[e] < minLx0) minLx0 = lx0a[e];
1469 if (lx1a[e] > maxLx1) maxLx1 = lx1a[e];
1470 }
1471 }
1472 int xmin = minLx0, xmax = maxLx1;
1473 if (nactive == 0) { xmin = width; xmax = -1; }
1474 else {
1475 if (xmin < 0) xmin = 0;
1476 if (xmax >= width) xmax = width - 1;
1477 }
1478
1479 // Per-band active-edge list and its midpoint order (polySortBand)
1480 // depend only on the row and the band bounds a, b — never on the
1481 // column chunk. Compute them once per row (bidx/bnc/bfull) and
1482 // reuse for every chunk: the committed code rebuilt and re-sorted
1483 // each band for every chunk (one i64 sort key per active edge per
1484 // chunk — pure redundancy for CHUNK == 1, where there is one chunk
1485 // per pixel column). Rows whose extent is empty (off-canvas
1486 // polygon) skip the prepass entirely: nothing would be filled.
1487 fl::u8 bidx[MAXN + 1][MAXN];
1488 fl::u8 bnc[MAXN + 1];
1489 fl::i32 bfull[MAXN + 1];
1490 if (xmin <= xmax) {
1491 for (int k = 0; k + 1 < nev; ++k) {
1492 const fl::i32 a = ev[k], b = ev[k + 1];
1493 int nc = 0;
1494 if (a < b) {
1495 for (int e = 0; e < nedges; ++e) {
1496 const PolyEdge& ed = edges[e];
1497 if (py >= ed.py0 && py <= ed.py1 &&
1498 ed.ytop < yc + b && ed.ybot > yc + a)
1499 idx[nc++] = (fl::u8)e;
1500 }
1501 if (nc >= 2) polySortBand(idx, keys, edges, nc, a + b);
1502 // Full-pixel coverage over this band: (b - a) *
1503 // kPolyOne >> 16 == (b - a) << 12 exactly (0 < b - a
1504 // < 2^16: both bounds lie inside the row).
1505 bfull[k] = (b - a) << 12;
1506 }
1507 bnc[k] = (fl::u8)nc;
1508 for (int e = 0; e < nc; ++e) bidx[k][e] = idx[e];
1509 }
1510 }
1511
1512 // Fill CHUNK-px chunks: accumulate coverage over the bands, then
1513 // quantize once per pixel.
1514 // An empty row extent (xmin > xmax, e.g. every active edge right of
1515 // the canvas) skipped the prepass, so bnc/bidx are uninitialized:
1516 // start past xmax so the chunk loop never reads them.
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];
1525 // Full-pixel coverage over this band (row prepass).
1526 const fl::i64 full = (fl::i64)bfull[k];
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;
1537 fl::i64 uL = (fl::i64)L.dy * (L.x - ((fl::i64)pl << 16));
1538 fl::i64 uR = (fl::i64)R.dy * (R.x - ((fl::i64)pl << 16));
1539 const fl::i64 suL = -((fl::i64)L.dy << 16);
1540 const fl::i64 suR = -((fl::i64)R.dy << 16);
1541 for (int px = pl; px <= ph; ++px) {
1542 fl::i32 TL = (px > Ll1) ? 0
1543 : (px < Ll0) ? (fl::i32)full
1544 : polyLineT28(L.B, L.dy, uL, a, b);
1545 fl::i32 TR = (px > Rl1) ? 0
1546 : (px < Rl0) ? (fl::i32)full
1547 : polyLineT28(R.B, R.dy, uR, a, b);
1548 const fl::i32 d = TR - TL;
1549 if (d > 0) accb[px - cs] += d;
1550 uL += suL;
1551 uR += suR;
1552 }
1553 }
1554 }
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;
1559 const fl::u8 br = (v >= kPolyOne)
1560 ? 255
1561 : static_cast<fl::u8>(((fl::u64)v * 255u
1562 + (fl::u64)(kPolyOne >> 1)) >> 28);
1563 if (br == 0) continue;
1564 PixelT c = color;
1565 c.nscale8(br);
1566 if (Overwrite) row[i] = c;
1567 else row[i] += c;
1568 }
1569 }
1570
1571 // Carry edge crossings to the next row's center (exact; t + rem
1572 // < 2*dy < 2^31, so the signed compare is safe)
1573 for (int e = 0; e < nedges; ++e) {
1574 PolyEdge& ed = edges[e];
1575 if (py >= ed.py0 && py <= ed.py1) {
1576 ed.x += ed.step;
1577 ed.t += ed.rem;
1578 if (ed.t >= ed.dy) {
1579 ed.t -= ed.dy;
1580 ++ed.x;
1581 }
1582 }
1583 }
1584 }
1585}
1586
1598template<typename PixelT, typename Coord, int MAXN, int CHUNK, PolyFill Fill>
1612
1613template<typename PixelT, typename Coord, int N, int MAXN, int CHUNK, PolyFill Fill>
1614inline void drawPolygon(Canvas<PixelT>& canvas, const PixelT& color,
1615 const PolyPoint<Coord> (&pts)[N],
1618 canvas, color, fl::span<const PolyPoint<Coord>>(pts, N), mode);
1619}
1620
1621} // namespace gfx
1622} // namespace fl
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.
Definition gfx.h:62
static constexpr i32 SCALE
Definition s16x16.h:25
static constexpr FASTLED_FORCE_INLINE s16x16 from_raw(i32 raw) FL_NO_EXCEPT
Definition s16x16.h:56
#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.
Definition bus_info.h:57
integral_constant< bool, B > bool_constant
Definition s16x16x4.h:33
integral_constant< bool, true > true_type
Definition s16x16x4.h:28
unsigned char u8
Definition s16x16x4.h:132
integral_constant< bool, false > false_type
Definition s16x16x4.h:29
enable_if_t< is_same< T, U >::value > swap(T &a, U &b) FL_NO_EXCEPT
Definition s16x16x4.h:905
Coord aaRatioDispatch(Coord num, Coord, Coord inv_denom, fl::false_type) FL_NO_EXCEPT
Definition primitives.h:157
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...
Definition primitives.h:414
fl::i32 floorDiv16I32(fl::i32 v) FL_NO_EXCEPT
Floor(v / 65536) for i32.
Definition primitives.h:280
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].
Definition primitives.h:291
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...
Definition primitives.h:436
fl::i32 toQ16Clamped(Coord val) FL_NO_EXCEPT
Convert any Coord type to Q16.16, clamped to ±8191 px (NaN -> 0).
Definition primitives.h:265
void drawLineCore(Canvas< PixelT > &canvas, const PixelT &color, Coord x0, Coord y0, Coord x1, Coord y1) FL_NO_EXCEPT
Definition primitives.h:908
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 ...
Definition primitives.h:402
static const fl::i32 kPolyQ16Max
Maximum |vertex coordinate| accepted by drawPolygon, in Q16.16 units.
Definition primitives.h:259
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.
Definition primitives.h:344
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.
Definition primitives.h:541
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².
Definition primitives.h:646
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.
Definition primitives.h:685
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).
Definition primitives.h:283
fl::i32 toQ16(Coord val) FL_NO_EXCEPT
Convert any Coord type to Q16.16 raw i32 for integer inner loops.
Definition primitives.h:230
fl::i32 toFixed8(Coord val) FL_NO_EXCEPT
Convert any Coord type to 8.8 fixed-point (fl::i32).
Definition primitives.h:187
void polyAdd128(fl::u64 &hi, fl::u64 &lo, fl::u64 ahi, fl::u64 alo) FL_NO_EXCEPT
128-bit add: (hi, lo) += (ahi, alo).
Definition primitives.h:355
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.
Definition primitives.h:216
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...
Definition primitives.h:375
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.
Definition primitives.h:769
fl::i64 floorDivI64(fl::i64 a, fl::i64 b) FL_NO_EXCEPT
Floor-divide a by positive b (rounds toward -inf).
Definition primitives.h:270
static const fl::i32 kPolyOne
Full-pixel coverage in T units (see polyLineT28).
Definition primitives.h:340
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.
Definition primitives.h:749
Ring context: bundles all per-circle constants into a struct passed by reference.
Definition primitives.h:633
Per-edge scanline state for drawPolygon (even-odd fill, band model).
Definition primitives.h:323
Disc context: bundles per-circle constants into a struct passed by reference.
Definition primitives.h:620
T fromInt(int n) FL_NO_EXCEPT
Convert an integer to Coord without float intermediate.
Definition primitives.h:64
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
Definition primitives.h:35
int toInt< double >(const double &val) FL_NO_EXCEPT
Definition primitives.h:121
fl::u8 coordToU8< double >(double alpha) FL_NO_EXCEPT
Definition primitives.h:41
LineCap
Line cap styles for stroke operations.
Definition canvas.h:20
fl::s16x16 fromFrac< fl::s16x16 >(int p, int q) FL_NO_EXCEPT
Definition primitives.h:77
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
Definition primitives.h:115
int toInt(const T &val) FL_NO_EXCEPT
Helper to convert any coordinate type to int Supports: s16x16 (via to_int()), float,...
Definition primitives.h:103
fl::u8 coordToU8< fl::s16x16 >(fl::s16x16 alpha) FL_NO_EXCEPT
Definition primitives.h:53
T fromFrac(int p, int q) FL_NO_EXCEPT
Convert rational p/q to Coord without float.
Definition primitives.h:74
float halfOf< float >(float val)
Definition primitives.h:92
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...
Definition primitives.h:129
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
Definition primitives.h:47
Coord aaRatio(Coord num, Coord denom, Coord inv_denom) FL_NO_EXCEPT
Compute num/denom using the best strategy for the Coord type:
Definition primitives.h:173
int halfOf< int >(int val) FL_NO_EXCEPT
Definition primitives.h:98
T halfOf(T val) FL_NO_EXCEPT
Divide Coord by 2 using shift (avoids expensive division on embedded).
Definition primitives.h:84
fl::s16x16 fromInt< fl::s16x16 >(int n) FL_NO_EXCEPT
Definition primitives.h:67
fl::u8 coordToU8(T alpha) FL_NO_EXCEPT
sqrt is provided by fl::sqrt overloads in math.h for:
Definition primitives.h:29
int toInt< float >(const float &val) FL_NO_EXCEPT
Definition primitives.h:109
fl::s16x16 halfOf< fl::s16x16 >(fl::s16x16 val) FL_NO_EXCEPT
Definition primitives.h:87
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)
Definition primitives.h:95
Polygon vertex (x, y coordinate pair) for drawPolygon C++11 aggregate: PolyPoint<float> p{3....
Definition canvas.h:25
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
u8 u8 height
Definition blur.h:186
FL_OPTIMIZE_FUNCTION constexpr u16 isqrt32(u32 x) FL_NO_EXCEPT
Definition isqrt.h:53
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
Definition math.h:327
fl::i64 i64
Definition s16x16x4.h:222
u8 width
Definition blur.h:186
gfx::LineCap LineCap
Line cap style.
Definition gfx.h:53
DrawMode
Definition draw_mode.h:5
@ DRAW_MODE_OVERWRITE
Definition draw_mode.h:5
InputGamut g
Definition rgbw.h:125
fl::u64 u64
Definition s16x16x4.h:221
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(...)
#define FL_NO_INLINE
Portable compile-time assertion wrapper.
Simple rectangular canvas for graphics operations Combines a pixel buffer with dimensions for cache-o...
Definition canvas.h:138