The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
gfx_internal.h
Go to the documentation of this file.
1#pragma once
2
3/**
4 * @file gfx_internal.h
5 * @brief pixel arithmetic shared by the libgfx sources.
6 *
7 * the helpers work on two channels at once: red and blue in one 32-bit
8 * word (mask 0x00FF00FF), alpha and green in another. each channel has
9 * 16 bits of room, so a product of two bytes does not spill into the next.
10 */
11
12#include "gfx.h"
13
14/**
15 * every channel of p times a / 255, rounded exactly.
16 * @param a 0 to 255.
17 */
18static inline uint32_t gfx_scale_pixel(uint32_t p, uint32_t a)
19{
20 uint32_t rb = (p & 0x00FF00FFu) * a + 0x00800080u;
21 rb = ((rb + ((rb >> 8) & 0x00FF00FFu)) >> 8) & 0x00FF00FFu;
22 uint32_t ag = ((p >> 8) & 0x00FF00FFu) * a + 0x00800080u;
23 ag = (ag + ((ag >> 8) & 0x00FF00FFu)) & 0xFF00FF00u;
24 return rb | ag;
25}
26
27/**
28 * src over dst, both premultiplied.
29 */
30static inline uint32_t gfx_over_pixel(uint32_t dst, uint32_t src)
31{
32 uint32_t a = src >> 24;
33 if (a == 255)
34 return src;
35 if (a == 0)
36 return dst;
37 return src + gfx_scale_pixel(dst, 255 - a);
38}
39
40/**
41 * a and b mixed: f / 256 of b and the rest of a.
42 * @param f 0 to 256.
43 */
44static inline uint32_t gfx_lerp_pixel(uint32_t a, uint32_t b, uint32_t f)
45{
46 uint32_t inv = 256 - f;
47 uint32_t rb = ((((a & 0x00FF00FFu) * inv) + ((b & 0x00FF00FFu) * f)) >> 8) & 0x00FF00FFu;
48 uint32_t ag = ((((a >> 8) & 0x00FF00FFu) * inv) + (((b >> 8) & 0x00FF00FFu) * f)) &
49 0xFF00FF00u;
50 return rb | ag;
51}
52
53/**
54 * the buffer's clip rectangle, also clipped to the buffer.
55 */
56static inline gfx_rect_t gfx_clip_of(const gfx_buffer_t *buf)
57{
58 return gfx_rect_intersect(buf->clip, gfx_rect(0, 0, buf->width, buf->height));
59}
gfx_rect_t gfx_rect_intersect(gfx_rect_t a, gfx_rect_t b)
the part two rectangles share (empty if none).
Definition gfx.c:31
libgfx: drawing into pixel buffers in memory.
unsigned int uint32_t
Definition fb.h:17
Definition gfx.h:41
int32_t width
Definition gfx.h:43
int32_t height
Definition gfx.h:44
gfx_rect_t clip
Definition gfx.h:46
Definition gfx.h:33