RMGui 0.15.0
C11 retained-mode UI library: layout kernel, style schema, widget painters, fonts, software framebuffer
Loading...
Searching...
No Matches
fb.h File Reference

fb.h — software RGBA8 framebuffer: allocation, opaque fills, opaque surface-to-surface copies, a filtered resampling copy and masked, filtered composites (clipped to a rounded rect or to a coverage mask). More...

#include <stddef.h>
#include <stdint.h>
#include "geom.h"

Data Structures

struct  rmgui_fb_t
 Software RGBA8 framebuffer. More...

Macros

#define RMGUI_FB_BYTES_PER_PIXEL   4
 Bytes per pixel: R, G, B, A.

Typedefs

typedef struct rmgui_mask rmgui_mask_t
 A coverage mask (mask.h).

Functions

int rmgui_fb_alloc (rmgui_fb_t *out, int width, int height)
 Allocate a width x height framebuffer into *out, every pixel transparent black (all bytes 0).
int rmgui_fb_resize (rmgui_fb_t *fb, int width, int height)
 Give fb the dimensions width x height, every pixel transparent black.
void rmgui_fb_free (rmgui_fb_t *fb)
 Release fb's pixel block and leave fb empty.
void rmgui_fb_clear (rmgui_fb_t *fb, rmgui_color_t color)
 Set every pixel of fb to color, written verbatim (no blending).
void rmgui_fb_fill_rect (rmgui_fb_t *fb, rmgui_rect_t rect, rmgui_color_t color)
 Set every pixel of rect, clipped to fb, to color, written verbatim (no blending; draw.h's rmgui_draw_fill_rect blends).
void rmgui_fb_blit (rmgui_fb_t *dst, const rmgui_fb_t *src, int x, int y)
 Copy all of src into dst with src's top-left pixel at (x, y), clipped to dst.
void rmgui_fb_blit_scaled (rmgui_fb_t *dst, const rmgui_fb_t *src, int x, int y, int k)
 Copy all of src into dst magnified k times with src's top-left pixel at (x, y), clipped to dst: dst pixel (x + i, y + j) for 0 <= i < k * src->width, 0 <= j < k * src->height receives src pixel (i / k, j / k), all four channels verbatim.
void rmgui_fb_blit_stretch (rmgui_fb_t *dst, const rmgui_fb_t *src, rmgui_rect_t rect)
 Resample all of src into rect of dst, clipped to dst, by nearest-neighbour point sampling with corners anchored: rect column i samples src column round(i * (src->width - 1) / (rect.w - 1)) (column 0 when rect.w == 1), rows likewise, rounding halves up.
void rmgui_fb_blit_filtered (rmgui_fb_t *dst, const rmgui_fb_t *src, rmgui_rect_t src_rect, rmgui_rect_t rect, rmgui_rect_t clip)
 Resample the src_rect part of src into rect of dst, clipped to dst and to clip, with a filter chosen per axis; all four channels are written (a copy, not a blend).
void rmgui_fb_blit_masked (rmgui_fb_t *dst, const rmgui_fb_t *src, rmgui_rect_t src_rect, rmgui_rect_t rect, rmgui_rect_t clip, rmgui_rect_t mask, int radius, rmgui_color_t tint)
 Composite the src_rect part of src, resampled into rect exactly as rmgui_fb_blit_filtered samples it, over dst inside the rounded rect mask, clipped to dst, to clip and to rect.
void rmgui_fb_blit_shaped (rmgui_fb_t *dst, const rmgui_fb_t *src, rmgui_rect_t src_rect, rmgui_rect_t rect, rmgui_rect_t clip, const rmgui_mask_t *shape, rmgui_color_t tint)
 Composite the src_rect part of src, resampled into rect exactly as rmgui_fb_blit_filtered samples it, over dst through the coverage mask shape, clipped to dst, to clip and to rect.

Detailed Description

fb.h — software RGBA8 framebuffer: allocation, opaque fills, opaque surface-to-surface copies, a filtered resampling copy and masked, filtered composites (clipped to a rounded rect or to a coverage mask).

Every function here is integer-only and writes bytes in R, G, B, A order, so the same inputs produce the same bytes on every platform. Coordinates and extents are framebuffer pixels and may be arbitrary ints: whatever falls outside the destination is dropped, never asserted. Rects and colors pass by value (geom.h). A NULL framebuffer argument, or a blit whose dst and src are the same framebuffer, is refused: nothing is written.

Macro Definition Documentation

◆ RMGUI_FB_BYTES_PER_PIXEL

#define RMGUI_FB_BYTES_PER_PIXEL   4

Bytes per pixel: R, G, B, A.

Framebuffer states and ownership are documented on rmgui_fb_t.

Typedef Documentation

◆ rmgui_mask_t

typedef struct rmgui_mask rmgui_mask_t

A coverage mask (mask.h).

Function Documentation

◆ rmgui_fb_alloc()

int rmgui_fb_alloc ( rmgui_fb_t * out,
int width,
int height )

Allocate a width x height framebuffer into *out, every pixel transparent black (all bytes 0).

*out must not hold an allocated framebuffer: it is overwritten, not released (use rmgui_fb_resize to change the size of an allocated one). Returns 0 on success. Returns -1, leaving *out unchanged, when out is NULL, width or height is not positive, the byte size does not fit in size_t, or the allocation fails.

◆ rmgui_fb_resize()

int rmgui_fb_resize ( rmgui_fb_t * fb,
int width,
int height )

Give fb the dimensions width x height, every pixel transparent black.

fb may be empty or allocated.

Returns 0 on success. Returns -1, leaving fb unchanged (dimensions and pixel contents intact), when fb is NULL, width or height is not positive, the byte size does not fit in size_t, or the allocation fails.

◆ rmgui_fb_free()

void rmgui_fb_free ( rmgui_fb_t * fb)

Release fb's pixel block and leave fb empty.

fb may be NULL or empty (no-op).

◆ rmgui_fb_clear()

void rmgui_fb_clear ( rmgui_fb_t * fb,
rmgui_color_t color )

Set every pixel of fb to color, written verbatim (no blending).

◆ rmgui_fb_fill_rect()

void rmgui_fb_fill_rect ( rmgui_fb_t * fb,
rmgui_rect_t rect,
rmgui_color_t color )

Set every pixel of rect, clipped to fb, to color, written verbatim (no blending; draw.h's rmgui_draw_fill_rect blends).

Non-positive w or h paints nothing.

◆ rmgui_fb_blit()

void rmgui_fb_blit ( rmgui_fb_t * dst,
const rmgui_fb_t * src,
int x,
int y )

Copy all of src into dst with src's top-left pixel at (x, y), clipped to dst.

All four channels are copied verbatim (an opaque copy, not a blend).

◆ rmgui_fb_blit_scaled()

void rmgui_fb_blit_scaled ( rmgui_fb_t * dst,
const rmgui_fb_t * src,
int x,
int y,
int k )

Copy all of src into dst magnified k times with src's top-left pixel at (x, y), clipped to dst: dst pixel (x + i, y + j) for 0 <= i < k * src->width, 0 <= j < k * src->height receives src pixel (i / k, j / k), all four channels verbatim.

Clipping never shifts the sampling. k <= 0 paints nothing.

◆ rmgui_fb_blit_stretch()

void rmgui_fb_blit_stretch ( rmgui_fb_t * dst,
const rmgui_fb_t * src,
rmgui_rect_t rect )

Resample all of src into rect of dst, clipped to dst, by nearest-neighbour point sampling with corners anchored: rect column i samples src column round(i * (src->width - 1) / (rect.w - 1)) (column 0 when rect.w == 1), rows likewise, rounding halves up.

The rect's corner pixels therefore reproduce src's corner pixels exactly, at any factor up or down. All four channels are copied verbatim. Clipping never shifts the sampling. Non-positive w or h, or an empty src, paints nothing.

◆ rmgui_fb_blit_filtered()

void rmgui_fb_blit_filtered ( rmgui_fb_t * dst,
const rmgui_fb_t * src,
rmgui_rect_t src_rect,
rmgui_rect_t rect,
rmgui_rect_t clip )

Resample the src_rect part of src into rect of dst, clipped to dst and to clip, with a filter chosen per axis; all four channels are written (a copy, not a blend).

Pixels are straight (not premultiplied) alpha.

Along an axis, S is the src_rect extent and D the rect extent, and destination position i (0 <= i < D) takes integer weights w over source positions k (relative to src_rect) that sum to the axis total T:

  • bilinear when S <= 2*D (magnification and reduction up to 2x): t = (2i+1)*S - D, clamped to [0, 2*D*(S-1)], is the sample centre in 1/(2D) source px; k = floor(t / 2D), f = t mod 2D, the weight of k+1 is w1 = floor((65536*f + D) / 2D), that of k is 65536 - w1, and T = 65536 (a weight of 0 is no tap);
  • box (area average) when S > 2*D: the destination position covers the source interval [i*S, (i+1)*S) in 1/D source px, the weight of k is the length of that interval within [k*D, (k+1)*D), and T = S. A pixel's weight is wx * wy and W = Tx * Ty. With sums over its taps of A = sum(w * alpha) and, per colour channel, C = sum(w * alpha * c), the pixel is alpha = floor((2A + W) / 2W) and c = floor((2C + A) / 2A) (alpha-weighted, so a transparent pixel's colour never bleeds), or (0, 0, 0, 0) when A == 0. A pixel with a single tap on both axes is that source pixel verbatim, so equal extents copy src_rect exactly. Each pixel depends only on its own rect position: clipping never shifts the sampling. The work is bounded by the visible pixels times their taps.

Paints nothing when src_rect is empty or not inside src, when src_rect holds more than 2^47 pixels, when rect or clip is empty, or on the refusals above.

◆ rmgui_fb_blit_masked()

void rmgui_fb_blit_masked ( rmgui_fb_t * dst,
const rmgui_fb_t * src,
rmgui_rect_t src_rect,
rmgui_rect_t rect,
rmgui_rect_t clip,
rmgui_rect_t mask,
int radius,
rmgui_color_t tint )

Composite the src_rect part of src, resampled into rect exactly as rmgui_fb_blit_filtered samples it, over dst inside the rounded rect mask, clipped to dst, to clip and to rect.

The mask's corner radius is radius px (negative counts as 0), clamped to half the smaller side of mask, so a square mask with a large radius is its inscribed circle, and to 65536 px. Its coverage c (0..256) is that of draw.h's anti-aliased shapes: the count of each pixel's 16 x 16 samples strictly inside the rect with its corners rounded, at the 1/256 px geometry of those shapes, so a circular mask covers exactly the samples rmgui_draw_aa_disc of the same centre and radius covers.

tint modulates each sample (r, g, b, a): the colour becomes (round(r*tint.r/255), round(g*tint.g/255), round(b*tint.b/255)) and the alpha round(a*tint.a/255), halves up, so tint.a is the opacity and a transparent pixel stays transparent; {255, 255, 255, 255} is no tint. The pixel is then blended once with that colour at alpha (alpha * c + 128) / 256 by draw.h's straight-over rule; alpha 0 leaves it untouched.

Paints nothing on rmgui_fb_blit_filtered's refusals, when mask is empty or tint.a is 0.

◆ rmgui_fb_blit_shaped()

void rmgui_fb_blit_shaped ( rmgui_fb_t * dst,
const rmgui_fb_t * src,
rmgui_rect_t src_rect,
rmgui_rect_t rect,
rmgui_rect_t clip,
const rmgui_mask_t * shape,
rmgui_color_t tint )

Composite the src_rect part of src, resampled into rect exactly as rmgui_fb_blit_filtered samples it, over dst through the coverage mask shape, clipped to dst, to clip and to rect.

shape (mask.h) is stretched over rect by mask.h's nearest-pixel rule, so the clipped part never shifts it. Its coverage c (0..255) at a pixel scales that pixel's alpha, which makes an anti-aliased mask give an anti-aliased edge.

tint modulates each sample (r, g, b, a): the colour becomes (round(r*tint.r/255), round(g*tint.g/255), round(b*tint.b/255)) and the alpha round(a*tint.a/255), halves up, so tint.a is the opacity and a transparent pixel stays transparent; {255, 255, 255, 255} is no tint. The pixel is then blended once with that colour at alpha round(alpha * c / 255), halves up, by draw.h's straight-over rule; alpha 0 leaves it untouched. With c == 255 everywhere the result is that of rmgui_fb_blit_masked with a radius of 0 and a mask covering rect.

Paints nothing on rmgui_fb_blit_filtered's refusals, when shape is NULL or empty, or when tint.a is 0.