117 lines
2.6 KiB
C
117 lines
2.6 KiB
C
#include "fb.h"
|
|
#include <stddef.h>
|
|
#include "mem.h"
|
|
|
|
static struct {
|
|
uint32_t * addr;
|
|
uint32_t width;
|
|
uint32_t height;
|
|
uint32_t pitch;
|
|
} fb;
|
|
|
|
static inline uint32_t build_pixel(uint8_t r, uint8_t g, uint8_t b)
|
|
{
|
|
return (r << 16u) | (g << 8u) | b;
|
|
}
|
|
|
|
static inline void fb_set_pixel(int x, int y, uint8_t r, uint8_t g, uint8_t b)
|
|
{
|
|
fb.addr[fb.pitch * y + x] = build_pixel(r, g, b);
|
|
}
|
|
|
|
void fb_clear(void)
|
|
{
|
|
memset32(fb.addr, 0u, fb.pitch * fb.height);
|
|
}
|
|
|
|
void fb_init(uint32_t * addr, uint32_t width, uint32_t height, uint32_t pitch)
|
|
{
|
|
fb.addr = addr;
|
|
fb.width = width;
|
|
fb.height = height;
|
|
fb.pitch = pitch / 4u;
|
|
fb_clear();
|
|
}
|
|
|
|
uint32_t * fb_addr(void)
|
|
{
|
|
return fb.addr;
|
|
}
|
|
|
|
bool fb_ready(void)
|
|
{
|
|
return fb.addr != NULL;
|
|
}
|
|
|
|
uint32_t fb_width(void)
|
|
{
|
|
return fb.width;
|
|
}
|
|
|
|
uint32_t fb_height(void)
|
|
{
|
|
return fb.height;
|
|
}
|
|
|
|
void fb_blend_alpha8(const uint8_t * bitmap, int width, int height, int pitch, int x, int y, uint8_t r, uint8_t g, uint8_t b)
|
|
{
|
|
if (((x + width) <= 0) || (x >= (int)fb.width) || ((y + height) <= 0) || (y >= (int)fb.height))
|
|
{
|
|
return;
|
|
}
|
|
if (x < 0)
|
|
{
|
|
width += x;
|
|
bitmap += (-x);
|
|
x = 0;
|
|
}
|
|
if (y < 0)
|
|
{
|
|
height += y;
|
|
bitmap += ((-y) * pitch);
|
|
y = 0;
|
|
}
|
|
if ((x + width) > (int)fb.width)
|
|
{
|
|
width = (int)fb.width - x;
|
|
}
|
|
if ((y + height) > (int)fb.height)
|
|
{
|
|
height = (int)fb.height - y;
|
|
}
|
|
uint32_t * target = &fb.addr[fb.pitch * y + x];
|
|
for (int row = 0; row < height; row++)
|
|
{
|
|
for (int col = 0; col < width; col++)
|
|
{
|
|
uint32_t alpha = bitmap[col];
|
|
uint32_t current_pixel = target[col];
|
|
uint8_t cr = (current_pixel >> 16u) & 0xFFu;
|
|
uint8_t cg = (current_pixel >> 8u) & 0xFFu;
|
|
uint8_t cb = current_pixel & 0xFFu;
|
|
uint8_t pr = alpha * r / 255u;
|
|
uint8_t pg = alpha * g / 255u;
|
|
uint8_t pb = alpha * b / 255u;
|
|
uint32_t current_alpha = 255u - alpha;
|
|
uint32_t pixel = build_pixel(
|
|
pr + current_alpha * cr / 255u,
|
|
pg + current_alpha * cg / 255u,
|
|
pb + current_alpha * cb / 255u);
|
|
target[col] = pixel;
|
|
}
|
|
bitmap += pitch;
|
|
target += fb.pitch;
|
|
}
|
|
}
|
|
|
|
void fb_fill(int x, int y, int width, int height, uint8_t r, uint8_t g, uint8_t b)
|
|
{
|
|
uint32_t * target = &fb.addr[fb.pitch * y + x];
|
|
uint32_t pixel = build_pixel(r, g, b);
|
|
for (int row = 0; row < height; row++)
|
|
{
|
|
memset32(target, pixel, width);
|
|
target += fb.pitch;
|
|
}
|
|
}
|