aienos.com
Source of www.aienos.com, served from my own hardware
Brought by drakestapleton
aienos.com / om / allen-scene / native / paint.h
| 1 | /* paint.h -- the drawing kit shared by the night sky (ui/night.c), Allen's |
| 2 | * presence (ui/mind.c) and his home (ui/allen_ui.c): rectangles, colour |
| 3 | * arithmetic, soft round lights, anti-aliased threads, rounded shapes and |
| 4 | * text, all into a RAM canvas of 0x00RRGGBB pixels; plus the integer |
| 5 | * mathematics they share (sine, atan2, exp, log2, hashed noise). Integer |
| 6 | * only (the kernel builds -mgeneral-regs-only), freestanding, no memory of |
| 7 | * its own beyond fixed tables. Host tested through ui/tests/allen_ui_test.c. |
| 8 | * |
| 9 | * Angles are in 65536ths of a turn. Positions handed to the soft shapes are |
| 10 | * in 16ths of a pixel, so slow movement is smooth rather than stepping a |
| 11 | * whole pixel at a time. */ |
| 12 | #ifndef AIENOS_PAINT_H |
| 13 | #define AIENOS_PAINT_H |
| 14 | #include <stddef.h> |
| 15 | #include <stdint.h> |
| 16 | |
| 17 | #include "allen_font.h" |
| 18 | |
| 19 | typedef struct { |
| 20 | int32_t x, y, w, h; |
| 21 | } ck_rect; |
| 22 | |
| 23 | typedef struct { |
| 24 | uint32_t *px; /* w * h pixels, rows w apart */ |
| 25 | int32_t w, h; |
| 26 | } ck_canvas; |
| 27 | |
| 28 | #define CK_RGB(r, g, b) ((uint32_t)(r) << 16 | (uint32_t)(g) << 8 | (uint32_t)(b)) |
| 29 | |
| 30 | static inline int32_t ck_min(int32_t a, int32_t b) { return a < b ? a : b; } |
| 31 | static inline int32_t ck_max(int32_t a, int32_t b) { return a > b ? a : b; } |
| 32 | static inline int32_t ck_abs(int32_t a) { return a < 0 ? -a : a; } |
| 33 | static inline int32_t ck_clamp(int32_t v, int32_t lo, int32_t hi) { return v < lo ? lo : v > hi ? hi : v; } |
| 34 | |
| 35 | /* r becomes its overlap with c; 0 when they do not meet. */ |
| 36 | static inline int ck_clip(ck_rect *r, const ck_rect *c) |
| 37 | { |
| 38 | int32_t x0 = ck_max(r->x, c->x), y0 = ck_max(r->y, c->y); |
| 39 | int32_t x1 = ck_min(r->x + r->w, c->x + c->w), y1 = ck_min(r->y + r->h, c->y + c->h); |
| 40 | if (x1 <= x0 || y1 <= y0) |
| 41 | return 0; |
| 42 | *r = (ck_rect){x0, y0, x1 - x0, y1 - y0}; |
| 43 | return 1; |
| 44 | } |
| 45 | |
| 46 | static inline int ck_meets(ck_rect a, const ck_rect *b) { return ck_clip(&a, b); } |
| 47 | static inline ck_rect ck_grow(ck_rect r, int32_t m) { return (ck_rect){r.x - m, r.y - m, r.w + 2 * m, r.h + 2 * m}; } |
| 48 | |
| 49 | /* The smallest rectangle holding both (an empty one adds nothing). */ |
| 50 | static inline ck_rect ck_union(ck_rect a, ck_rect b) |
| 51 | { |
| 52 | if (a.w <= 0 || a.h <= 0) |
| 53 | return b; |
| 54 | if (b.w <= 0 || b.h <= 0) |
| 55 | return a; |
| 56 | int32_t x0 = ck_min(a.x, b.x), y0 = ck_min(a.y, b.y); |
| 57 | return (ck_rect){x0, y0, ck_max(a.x + a.w, b.x + b.w) - x0, ck_max(a.y + a.h, b.y + b.h) - y0}; |
| 58 | } |
| 59 | |
| 60 | /* ---- colour: channels 0..255, weights 0..256 ---- */ |
| 61 | |
| 62 | static inline uint32_t ck_ch(uint32_t c, int s) { return c >> s & 255u; } |
| 63 | |
| 64 | /* c scaled by f/256 (f <= 256), all three channels at once */ |
| 65 | static inline uint32_t ck_scale(uint32_t c, uint32_t f) |
| 66 | { |
| 67 | return ((c & 0xff00ffu) * f >> 8 & 0xff00ffu) | ((c & 0x00ff00u) * f >> 8 & 0x00ff00u); |
| 68 | } |
| 69 | |
| 70 | /* d + c * a / 256 per channel, held at 255 */ |
| 71 | static inline uint32_t ck_add(uint32_t d, uint32_t c, uint32_t a) |
| 72 | { |
| 73 | uint32_t r = ck_ch(d, 16) + (ck_ch(c, 16) * a >> 8), g = ck_ch(d, 8) + (ck_ch(c, 8) * a >> 8), |
| 74 | b = ck_ch(d, 0) + (ck_ch(c, 0) * a >> 8); |
| 75 | return (r > 255 ? 255u : r) << 16 | (g > 255 ? 255u : g) << 8 | (b > 255 ? 255u : b); |
| 76 | } |
| 77 | |
| 78 | /* d toward c by a/256 */ |
| 79 | static inline uint32_t ck_mix(uint32_t d, uint32_t c, uint32_t a) |
| 80 | { |
| 81 | if (a >= 256) |
| 82 | return c; |
| 83 | return ck_scale(d, 256 - a) + ck_scale(c, a); |
| 84 | } |
| 85 | |
| 86 | /* ---- easing: t and the result in 0..256 ---- */ |
| 87 | |
| 88 | static inline uint32_t ck_smooth(int32_t t) /* 3t^2 - 2t^3 */ |
| 89 | { |
| 90 | t = ck_clamp(t, 0, 256); |
| 91 | return (uint32_t)(t * t * (768 - 2 * t) >> 16); |
| 92 | } |
| 93 | |
| 94 | static inline uint32_t ck_ease_out(int32_t t) /* 1 - (1 - t)^3 */ |
| 95 | { |
| 96 | t = 256 - ck_clamp(t, 0, 256); |
| 97 | return 256u - (uint32_t)(t * t * t >> 16); |
| 98 | } |
| 99 | |
| 100 | /* 0..256 for how far `now` is from `from` to `to` (ms) */ |
| 101 | static inline int32_t ck_prog(uint32_t now, uint32_t from, uint32_t to) |
| 102 | { |
| 103 | if (now <= from) |
| 104 | return 0; |
| 105 | if (now >= to) |
| 106 | return 256; |
| 107 | return (int32_t)((uint64_t)(now - from) * 256u / (to - from)); |
| 108 | } |
| 109 | |
| 110 | /* ---- mathematics (paint.c) ---- */ |
| 111 | |
| 112 | int32_t ck_sin(uint32_t turn65536); /* scaled by 16384 */ |
| 113 | int32_t ck_cos(uint32_t turn65536); |
| 114 | uint32_t ck_atan2(int32_t y, int32_t x); /* the angle of (x, y), 0..65535 */ |
| 115 | uint32_t ck_exp_neg(uint32_t x256); /* exp(-x/256), scaled by 65536 */ |
| 116 | uint32_t ck_log2_q16(uint64_t v); /* log2(v) * 65536, v >= 1 */ |
| 117 | uint64_t ck_isqrt(uint64_t v); |
| 118 | uint32_t ck_hash(int32_t x, int32_t y, uint32_t seed); |
| 119 | uint32_t ck_noise(int32_t x256, int32_t y256, uint32_t seed); /* smooth value noise, 0..65535 */ |
| 120 | uint32_t ck_fbm(int32_t x256, int32_t y256, uint32_t seed, int octaves); /* 0..65535 */ |
| 121 | |
| 122 | /* ---- shapes (paint.c); each draws only inside the clip rectangle ---- */ |
| 123 | |
| 124 | /* A soft round light at (cx, cy) with radius r (16ths of a pixel): colour |
| 125 | * added with weight a * (1 - d^2/r^2)^3. */ |
| 126 | void ck_glow(ck_canvas *cv, const ck_rect *clip, int32_t cx, int32_t cy, int32_t r, uint32_t color, uint32_t a); |
| 127 | /* A thread one pixel wide from (x0, y0) to (x1, y1) (16ths of a pixel), |
| 128 | * anti-aliased, added as light whose weight runs from a0 to a1 along it. */ |
| 129 | void ck_thread(ck_canvas *cv, const ck_rect *clip, int32_t x0, int32_t y0, int32_t x1, int32_t y1, uint32_t color, |
| 130 | uint32_t a0, uint32_t a1); |
| 131 | /* Signed distance from a pixel's centre to a rounded rectangle, 16ths of a |
| 132 | * pixel, negative inside; and the coverage of a pixel at such a distance. */ |
| 133 | int32_t ck_sd_rrect(int32_t x, int32_t y, const ck_rect *r, int32_t radius); |
| 134 | static inline uint32_t ck_cover(int32_t d16) |
| 135 | { |
| 136 | int32_t v = 8 - d16; |
| 137 | return v <= 0 ? 0u : v >= 16 ? 256u : (uint32_t)(v * 16); |
| 138 | } |
| 139 | /* One letter at pen (1/256 px) on baseline y, mixed in with weight a |
| 140 | * (additive light when add is set); returns the pen after it. */ |
| 141 | uint64_t ck_letter(ck_canvas *cv, const ck_rect *clip, ck_af_face *f, uint64_t pen, int32_t y, char c, |
| 142 | uint32_t color, uint32_t a, int add); |
| 143 | #endif |