Monorepo for Aesthetic.Computer
aesthetic.computer
1#ifndef AC_FRAMEBUFFER_H
2#define AC_FRAMEBUFFER_H
3
4#include <stdint.h>
5
6typedef struct {
7 uint32_t *pixels; // ARGB32 pixel buffer
8 int width;
9 int height;
10 int stride; // Pixels per row (may differ from width for alignment)
11} ACFramebuffer;
12
13ACFramebuffer *fb_create(int width, int height);
14void fb_destroy(ACFramebuffer *fb);
15void fb_clear(ACFramebuffer *fb, uint32_t color);
16void fb_copy_to(ACFramebuffer *src, uint32_t *dst, int dst_stride);
17void fb_copy_scaled(ACFramebuffer *src, uint32_t *dst, int dst_w, int dst_h, int dst_stride, int scale);
18
19// Direct pixel access with bounds checking
20static inline void fb_put_pixel(ACFramebuffer *fb, int x, int y, uint32_t color) {
21 if (x >= 0 && x < fb->width && y >= 0 && y < fb->height)
22 fb->pixels[y * fb->stride + x] = color;
23}
24
25static inline void fb_blend_pixel(ACFramebuffer *fb, int x, int y, uint32_t color) {
26 if (x >= 0 && x < fb->width && y >= 0 && y < fb->height) {
27 int idx = y * fb->stride + x;
28 uint8_t sa = (color >> 24) & 0xFF;
29 if (sa == 255) {
30 fb->pixels[idx] = color;
31 } else if (sa > 0) {
32 uint32_t dst = fb->pixels[idx];
33 uint8_t sr = (color >> 16) & 0xFF, sg = (color >> 8) & 0xFF, sb = color & 0xFF;
34 uint8_t dr = (dst >> 16) & 0xFF, dg = (dst >> 8) & 0xFF, db = dst & 0xFF;
35 uint8_t r = (sr * sa + dr * (255 - sa)) / 255;
36 uint8_t g = (sg * sa + dg * (255 - sa)) / 255;
37 uint8_t b = (sb * sa + db * (255 - sa)) / 255;
38 fb->pixels[idx] = (255u << 24) | ((uint32_t)r << 16) | ((uint32_t)g << 8) | b;
39 }
40 }
41}
42
43static inline uint32_t fb_get_pixel(ACFramebuffer *fb, int x, int y) {
44 if (x >= 0 && x < fb->width && y >= 0 && y < fb->height)
45 return fb->pixels[y * fb->stride + x];
46 return 0;
47}
48
49#endif