webcam-loopback-manipulation-screensharing-and-stuff git · main
vcam: v4l2 loopback with overlays, filters, OpenCV and RVM matting
C 51.3% C++ 40.7% Markdown 6.9% Makefile 1%git clone https://git.christianimmanuel.de/linux-system/webcam-loopback-manipulation-screensharing-and-stuff.gitwget https://git.christianimmanuel.de/linux-system/webcam-loopback-manipulation-screensharing-and-stuff/archive/webcam-loopback-manipulation-screensharing-and-stuff.tar.gz..
.gitignore | 38 +++++++++ README.md | 33 ++++++-- include/vcam.h | 13 ++- scripts/vcam-ctl | 9 +- scripts/vcam-tune | 133 +++++++++++++++++++++++++---- src/filter.c | 244 +++++++++++++++++++++++++++++++++++++++++++++++++++++- src/ipc.c | 8 +- src/main.c | 4 + src/overlay.c | 31 +++++++ 9 files changed, 479 insertions(+), 34 deletions(-) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..efa7302 --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +# Build artifacts +*.o +*.d +*.a +*.so +*.so.* +vcam +test_screen + +# Distribution archives +*.tar.gz +*.tar.bz2 +*.zip + +# Editor / IDE +.vscode/ +.idea/ +*.swp +*.swo +*~ +.DS_Store + +# Local notes / scratch +TODO.local +NOTES.local +scratch/ + +# Coverage / profiling +*.gcov +*.gcda +*.gcno +gmon.out +perf.data* + +# Core dumps +core +core.* +vgcore.* diff --git a/README.md b/README.md index 997dbc9..8e0a892 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,5 @@ # vcam — virtual webcam pipeline -100% vibe coded by ClaudeCode - -Loopback your webcam so you can add filters, screensharing, images, super laggy videos nad such things - Real-time virtual webcam written in C. Reads from a physical webcam, runs your frames through a chain of pixel effects, composites images / looping videos / live screen regions on top, and writes the result to a @@ -91,8 +87,6 @@ mandatory for Firefox. ## vcam — main process -Just run `./vcam` if you did everything above to run the video loopback modification software - ``` vcam [OPTIONS] @@ -205,6 +199,17 @@ Multiple captures are first-class: calling `vcam-ctl screen` repeatedly without `-i` creates `screen`, `screen2`, `screen3`, … so they don't collide. +If the captured region is larger than the vcam output (very common with +`--full` on a 4K monitor while vcam runs at 720p), the overlay is +automatically scaled down to fit while preserving aspect — no more +top-left-quadrant clipping. + +For low capture latency, vcam sets the kernel pipe size to exactly one +frame's worth of bytes (`F_SETPIPE_SZ`). This tightly couples +wf-recorder's output rate to vcam's consumption rate so frames can't +build up a backlog. On most systems on-screen lag drops to roughly one +frame after the change. + Examples: ```bash @@ -305,7 +310,7 @@ shown above. --- -## Filters (43 total, 6 categories) +## Filters (49 total, 6 categories) Filters run in chain order on every frame from the webcam, *before* overlays are composited. @@ -325,7 +330,8 @@ overlays are composited. | `brightness` | mult (1.2) | clamped multiply | | `contrast` | mult (1.4) | contrast around mid-grey | | `posterize` | levels (4) | reduce colour levels per channel | -| `edges` | — | Sobel edge detection | +| `edges` | 0–2 (1.0) | Sobel edge detection, strength multiplier | +| `saturate` | 0–3 (1.5) | chroma boost (0=gray, 1=normal, 2=vivid) | ### Fancy @@ -341,6 +347,7 @@ overlays are composited. | `cartoon` | edge thresh (50) | posterize + dark edges | | `solarize` | threshold (128) | invert pixels above value | | `rainbow` | speed (1.0) | animated additive hue rotation | +| `crt` | 0–1 (0.7) | CRT scanlines + barrel + R/B chromatic aberration | ### Soft & dreamy @@ -352,6 +359,7 @@ overlays are composited. | `pinch` | 0–1 (0.5) | radial inward pull | | `bulge` | 0–1 (0.5) | fisheye-like outward push | | `painterly` | 1–5 (3) | oil-painting effect | +| `velvet` | 0–1 (0.6) | soft desat + warm tint + blur — flattering portrait look | ### Themed looks @@ -362,6 +370,7 @@ overlays are composited. | `romantic` | 0–1 (0.7) | soft warm haze + blur + vignette | | `vangogh` | radius 1–5 (3) | heavy painterly + saturation | | `picasso` | segments 4–16 (8) | cubist: kaleidoscope + posterize + edges | +| `noir` | 0–1 (0.8) | high-contrast B&W with split-tone shadows/highlights | ### Mathematical patterns @@ -372,6 +381,7 @@ overlays are composited. | `modulo` | divisor 2–128 (64)| colour mod N | | `polar` | 0–1 (0.7) | polar-coordinate warp | | `voronoi` | cells 4–64 (20) | voronoi-cell mosaic | +| `julia` | 0–1 (0.5) | animated Julia fractal coloured overlay | ### Pixel-crazy / animated @@ -382,6 +392,7 @@ overlays are composited. | `popart` | 0–1 (1.0) | Warhol 2×2 quadrants with rotating colour tints | | `displace` | 0–1 (0.5) | bright pixels shift right, dark pixels left | | `ripple` | 0–1 (0.5) | animated water ripple radiating from the center | +| `scanlines`| gap 2–16 (3) | hard black scanline every N rows — CRT/terminal look | ### Combinations to try @@ -417,11 +428,15 @@ vcam-tune --help | `↑` / `k` | increase parameter | | `↓` / `j` | decrease parameter | | `r` | reset to default | -| `x` | remove and quit | +| `x` | remove current filter (stays in walk mode) | +| `a` | open a categorised picker to add a new filter | | `q` / Esc | quit | | `→` / `l` / Tab | next filter (walk mode) | | `←` / `h` | previous filter (walk mode) | +If you launch `vcam-tune` with no filters active, it opens the picker +automatically so you can start composing a chain from scratch. + Examples: ```bash diff --git a/include/vcam.h b/include/vcam.h index 30817c2..6956eec 100644 --- a/include/vcam.h +++ b/include/vcam.h @@ -84,7 +84,8 @@ typedef enum { FX_BRIGHTNESS, /* param = multiplier, default 1.2 */ FX_CONTRAST, /* param = multiplier, default 1.4 */ FX_POSTERIZE, /* param = levels per channel, def 4 */ - FX_EDGES, /* sobel edge detect */ + FX_EDGES, /* sobel edge detect, strength 0..2, default 1.0 */ + FX_SATURATE, /* saturation multiplier 0..3, default 1.5 */ /* ── fancier effects ── */ FX_CHROMASHIFT, /* param = offset px, default 5 (VHS-style RGB split) */ @@ -97,6 +98,7 @@ typedef enum { FX_CARTOON, /* posterize + edge enhancement */ FX_SOLARIZE, /* param = threshold 0..255, default 128 */ FX_RAINBOW, /* animated hue rotation, param = speed (default 1.0) */ + FX_CRT, /* CRT scanlines + slight curvature, 0..1, def 0.7 */ /* ── soft / dreamy / painterly ── */ FX_DREAM, /* blur + brightness boost (soft hazy glow) */ @@ -105,6 +107,7 @@ typedef enum { FX_PINCH, /* radial inward pull toward center */ FX_BULGE, /* radial outward push from center (fisheye-ish) */ FX_PAINTERLY, /* oil-painting: dominant color in neighborhood */ + FX_VELVET, /* desat + warm tint + soft blur, 0..1, def 0.6 */ /* ── themed / styled looks ── */ FX_MODERN, /* clean teal-orange grade, default 0.7 strength */ @@ -112,6 +115,7 @@ typedef enum { FX_ROMANTIC, /* soft warm haze, default 0.7 strength */ FX_VANGOGH, /* heavy painterly + saturation boost */ FX_PICASSO, /* kaleidoscope + posterize + cartoon edges */ + FX_NOIR, /* high-contrast B&W with split-tone shadows, def 0.8 */ /* ── mathematical patterns ── */ FX_SINE, /* sinusoidal x/y distortion, default 15px */ @@ -119,6 +123,7 @@ typedef enum { FX_MODULO, /* color modulo, default 64 */ FX_POLAR, /* polar-coordinate warp, 0..1 */ FX_VORONOI, /* voronoi cell mosaic, default 20 cells */ + FX_JULIA, /* Julia fractal color overlay, 0..1, def 0.5 */ /* ── pixel-crazy / animated ── */ FX_BITCRUSH, /* quantize bits per channel, 1..8, default 3 */ @@ -126,6 +131,7 @@ typedef enum { FX_POPART, /* warhol-style 2x2 quadrants, strength 0..1 */ FX_DISPLACE, /* luma-based horizontal displacement, default 0.5 */ FX_RIPPLE, /* animated water ripple from center, default 0.5 */ + FX_SCANLINES, /* black scanlines every N rows, gap 2..8, def 3 */ FX_UNKNOWN, } FilterType; @@ -222,5 +228,10 @@ void capture_close(V4L2Capture *cap); typedef struct V4L2Output V4L2Output; V4L2Output *output_open (const char *dev, int w, int h, int fps); + +/* Output frame dimensions — set by main.c after output_open succeeds. + * Screen / webcam overlays clamp against these to avoid being clipped. */ +extern int g_output_w; +extern int g_output_h; bool output_write(V4L2Output *out, const Frame *frame); void output_close(V4L2Output *out); diff --git a/scripts/vcam-ctl b/scripts/vcam-ctl index cf87330..b958bda 100755 --- a/scripts/vcam-ctl +++ b/scripts/vcam-ctl @@ -390,12 +390,11 @@ if [[ "$1" == "webcam" ]]; then # Webcam is 16:9 (height computed from width) HEIGHT=$(( WIDTH * 9 / 16 )) - PAD=10 case "$POS" in - tl) X=$PAD; Y=$PAD ;; - tr) X=$(( OUT_W - WIDTH - PAD )); Y=$PAD ;; - bl) X=$PAD; Y=$(( OUT_H - HEIGHT - PAD )) ;; - br) X=$(( OUT_W - WIDTH - PAD )); Y=$(( OUT_H - HEIGHT - PAD )) ;; + tl) X=0; Y=0 ;; + tr) X=$(( OUT_W - WIDTH )); Y=0 ;; + bl) X=0; Y=$(( OUT_H - HEIGHT )) ;; + br) X=$(( OUT_W - WIDTH )); Y=$(( OUT_H - HEIGHT )) ;; center) X=$(( (OUT_W - WIDTH) / 2 )); Y=$(( (OUT_H - HEIGHT) / 2 )) ;; full) X=0; Y=0; WIDTH=$OUT_W; HEIGHT=$OUT_H ;; *) echo "Unknown position '$POS' (use tl|tr|bl|br|center|full|off)" >&2; exit 1 ;; diff --git a/scripts/vcam-tune b/scripts/vcam-tune index 54a6967..49336c4 100755 --- a/scripts/vcam-tune +++ b/scripts/vcam-tune @@ -17,7 +17,8 @@ # ↑ / k increase current filter's parameter # ↓ / j decrease current filter's parameter # r reset current filter to its default value -# x remove current filter and exit +# x remove current filter +# a add a new filter (opens picker menu) # q / Esc quit (filter stays at its current value) # # Walk-mode-only keys: @@ -67,12 +68,12 @@ read_filters() { default_step() { case "$1" in blur|pixelate|posterize|chromashift|wave|painterly|vangogh) echo 1 ;; - kaleidoscope|picasso|voronoi|sine|modulo) echo 1 ;; + kaleidoscope|picasso|voronoi|sine|modulo|scanlines) echo 1 ;; bitcrush) echo 1 ;; cartoon|solarize) echo 5 ;; - brightness|contrast|swirl|rainbow) echo 0.1 ;; + brightness|contrast|swirl|rainbow|saturate) echo 0.1 ;; vignette|glitch|dream|bloom|glow|pinch|bulge|modern|cyberpunk|romantic|xor|polar) echo 0.05 ;; - melt|popart|displace|ripple) echo 0.05 ;; + melt|popart|displace|ripple|crt|velvet|noir|julia|edges) echo 0.05 ;; *) echo 0.1 ;; esac } @@ -103,8 +104,14 @@ default_param_for() { modulo) echo 64 ;; voronoi) echo 20 ;; bitcrush) echo 3 ;; - melt|displace|ripple) echo 0.5 ;; + melt|displace|ripple|julia) echo 0.5 ;; popart) echo 1 ;; + edges) echo 1.0 ;; + saturate) echo 1.5 ;; + crt) echo 0.7 ;; + velvet) echo 0.6 ;; + noir) echo 0.8 ;; + scanlines) echo 3 ;; *) echo 1 ;; esac } @@ -124,6 +131,65 @@ set_param() { return 1 } +# All available filters, grouped by category. +# Updated when src/filter.c changes — keep in sync with the help table. +FILTER_CATEGORIES=( + "Basic:grayscale sepia invert mirror flip pixelate blur vignette brightness contrast posterize edges saturate" + "Fancy:chromashift glitch kaleidoscope swirl thermal oldfilm wave cartoon solarize rainbow crt" + "Soft/Dreamy:dream bloom glow pinch bulge painterly velvet" + "Themed:modern cyberpunk romantic vangogh picasso noir" + "Math:sine xor modulo polar voronoi julia" + "Pixel-crazy:bitcrush melt popart displace ripple scanlines" +) + +# Show an interactive picker, return chosen filter name on stdout +# (returns nothing if cancelled). The caller is expected to have +# restored cooked-mode TTY before invoking; we handle our own. +pick_filter() { + local saved=$(stty -g) + stty echo icanon + echo "" >&2 + echo "─── add a filter ─────────────────────────" >&2 + local idx=0 + local -a all_names + for entry in "${FILTER_CATEGORIES[@]}"; do + local cat="${entry%%:*}" + local list="${entry#*:}" + echo "" >&2 + echo " ${cat}:" >&2 + local row="" + for name in $list; do + idx=$((idx + 1)) + all_names+=("$name") + row+=$(printf "%3d) %-12s" "$idx" "$name") + # Wrap every 3 entries + if (( idx % 3 == 0 )); then + echo " $row" >&2 + row="" + fi + done + [[ -n "$row" ]] && echo " $row" >&2 + done + echo "" >&2 + printf "Pick a filter (1-%d, or name, blank to cancel): " "$idx" >&2 + local choice + read -r choice + stty "$saved" + [[ -z "$choice" ]] && return 1 + if [[ "$choice" =~ ^[0-9]+$ ]]; then + local sel="${all_names[$((choice - 1))]:-}" + [[ -z "$sel" ]] && return 1 + echo "$sel" + else + # Verify it's actually a known filter + for name in "${all_names[@]}"; do + if [[ "$name" == "$choice" ]]; then echo "$choice"; return 0; fi + done + return 1 + fi + return 0 +} + # ── single-filter mode ─────────────────────────────────────── if [[ $# -ge 1 ]]; then ARG="$1" @@ -203,9 +269,14 @@ fi # ── walk mode ──────────────────────────────────────────────── read_filters if [[ ${#NAMES[@]} -eq 0 ]]; then - echo "No filters active. Add some with 'vcam-ctl filter add <name>'." >&2 - echo "Or pass a filter name to this tool: 'vcam-tune <name>'." >&2 - exit 1 + echo "No filters active — opening picker to add one." + SEL=$(pick_filter) || { echo "Cancelled."; exit 0; } + DEF=$(default_param_for "$SEL") + ipc "filter add $SEL $DEF" >/dev/null + read_filters + if [[ ${#NAMES[@]} -eq 0 ]]; then + echo "Failed to add filter '$SEL'." >&2; exit 1 + fi fi CURRENT_IDX=0 @@ -216,12 +287,16 @@ trap 'stty "$stty_orig"; echo' EXIT stty -echo -icanon time 0 min 1 echo "vcam-tune walk mode — $NUM_FILTERS active filter(s)" -echo " ↑/k=up ↓/j=down →/l/Tab=next ←/h=prev r=reset x=remove q=quit" +echo " ↑/k=up ↓/j=down →/l/Tab=next ←/h=prev r=reset x=remove a=add q=quit" echo "" draw() { local idx=$CURRENT_IDX printf '\r\033[K' + if [[ $NUM_FILTERS -eq 0 ]]; then + printf '[empty] press \033[1;33ma\033[0m to add a filter, \033[1;33mq\033[0m to quit' + return + fi printf '[%d/%d] ' $((idx + 1)) "$NUM_FILTERS" for i in "${!NAMES[@]}"; do if [[ $i -eq $idx ]]; then @@ -240,6 +315,8 @@ while :; do [[ -z "$c2" ]] && break # plain Esc if [[ "$c2" == "[" ]]; then IFS= read -rsn1 c3 + # All directional keys need at least one filter + if [[ $NUM_FILTERS -eq 0 ]]; then draw; continue; fi case "$c3" in A) # up name="${NAMES[CURRENT_IDX]}" @@ -258,6 +335,17 @@ while :; do esac fi else + case "$c" in + a|A) + # 'a' must work even when the chain is empty — handled later + ;; + q|Q) + break ;; + *) + # Everything else needs a current filter + if [[ $NUM_FILTERS -eq 0 ]]; then draw; continue; fi + ;; + esac case "$c" in k|K) name="${NAMES[CURRENT_IDX]}" @@ -284,14 +372,31 @@ while :; do NUM_FILTERS=${#NAMES[@]} if [[ $NUM_FILTERS -eq 0 ]]; then echo "" - echo "All filters removed." - exit 0 + echo "All filters removed. Press 'a' to add one, or 'q' to quit." + NUM_FILTERS=0 + CURRENT_IDX=0 + else + if [[ $CURRENT_IDX -ge $NUM_FILTERS ]]; then + CURRENT_IDX=$((NUM_FILTERS - 1)) + fi fi - if [[ $CURRENT_IDX -ge $NUM_FILTERS ]]; then - CURRENT_IDX=$((NUM_FILTERS - 1)) + ;; + a|A) + # Restore cooked TTY so the picker can read normally + stty "$stty_orig" + echo "" + SEL=$(pick_filter) || SEL="" + if [[ -n "$SEL" ]]; then + DEF=$(default_param_for "$SEL") + ipc "filter add $SEL $DEF" >/dev/null + read_filters + NUM_FILTERS=${#NAMES[@]} + CURRENT_IDX=$((NUM_FILTERS - 1)) # focus the new one fi + # Back to raw mode for the walk + stty -echo -icanon time 0 min 1 + echo "" ;; - q|Q) break ;; esac fi draw diff --git a/src/filter.c b/src/filter.c index 10b2ed9..f9f9165 100644 --- a/src/filter.c +++ b/src/filter.c @@ -61,12 +61,19 @@ static const struct { const char *name; FilterType t; } NAMES[] = { {"modulo", FX_MODULO}, {"polar", FX_POLAR}, {"voronoi", FX_VORONOI}, + {"julia", FX_JULIA}, /* pixel-crazy */ {"bitcrush", FX_BITCRUSH}, {"melt", FX_MELT}, {"popart", FX_POPART}, {"displace", FX_DISPLACE}, {"ripple", FX_RIPPLE}, + {"scanlines", FX_SCANLINES}, + /* new effect additions */ + {"saturate", FX_SATURATE}, + {"crt", FX_CRT}, + {"velvet", FX_VELVET}, + {"noir", FX_NOIR}, }; FilterType filter_type_from_name(const char *name) @@ -123,6 +130,13 @@ float filter_default_param(FilterType t) case FX_POPART: return 1.0f; case FX_DISPLACE: return 0.5f; case FX_RIPPLE: return 0.5f; + case FX_EDGES: return 1.0f; + case FX_SATURATE: return 1.5f; + case FX_CRT: return 0.7f; + case FX_VELVET: return 0.6f; + case FX_NOIR: return 0.8f; + case FX_JULIA: return 0.5f; + case FX_SCANLINES: return 3.0f; default: return 0.0f; } } @@ -370,9 +384,12 @@ static void fx_posterize(Frame *f, int levels) p[i] = (uint8_t)((p[i] / step) * step); } -static void fx_edges(Frame *f) +static void fx_edges(Frame *f, float strength) { - /* Simple Sobel on luma */ + /* Simple Sobel on luma. strength multiplies the gradient magnitude + * before clamping: 0..2 → 0=black, 1=normal, 2=more punchy. */ + if (strength < 0) strength = 0; + if (strength > 4) strength = 4; int w = f->width, h = f->height; uint8_t *luma = malloc((size_t)w * h); if (!luma) return; @@ -389,7 +406,7 @@ static void fx_edges(Frame *f) +luma[(y-1)*w + (x+1)] + 2*luma[y*w + (x+1)] + luma[(y+1)*w + (x+1)]; int gy = -luma[(y-1)*w + (x-1)] - 2*luma[(y-1)*w + x] - luma[(y-1)*w + (x+1)] +luma[(y+1)*w + (x-1)] + 2*luma[(y+1)*w + x] + luma[(y+1)*w + (x+1)]; - int v = abs(gx) + abs(gy); + int v = (int)((abs(gx) + abs(gy)) * strength); uint8_t e = clamp_u8(v); dst[x*3+0] = dst[x*3+1] = dst[x*3+2] = e; } @@ -1326,6 +1343,219 @@ static void fx_ripple(Frame *f, float strength) free(tmp); } +/* ══════════════════════════════════════════════════════════════ + * NEW: one filter per category — saturate, crt, velvet, noir, + * julia, scanlines. + * ══════════════════════════════════════════════════════════════ */ + +/* ── saturate: chroma-distance multiplier around luma (Basic) ── */ +static void fx_saturate(Frame *f, float factor) +{ + if (factor < 0) factor = 0; + if (factor > 4) factor = 4; + int n = f->width * f->height; + for (int i = 0; i < n; i++) { + uint8_t *p = f->data + i * 3; + int luma = (p[2]*299 + p[1]*587 + p[0]*114) / 1000; + p[0] = clamp_u8((int)(luma + (p[0] - luma) * factor)); + p[1] = clamp_u8((int)(luma + (p[1] - luma) * factor)); + p[2] = clamp_u8((int)(luma + (p[2] - luma) * factor)); + } +} + +/* ── crt: TV CRT scanlines + slight barrel + R/B fringing (Fancy) ── */ +static void fx_crt(Frame *f, float strength) +{ + if (strength < 0) strength = 0; + if (strength > 1) strength = 1; + int w = f->width, h = f->height; + uint8_t *tmp = malloc((size_t)w * h * 3); + if (!tmp) return; + memcpy(tmp, f->data, (size_t)w * h * 3); + + float cx = w / 2.0f, cy = h / 2.0f; + float barrel = strength * 0.15f; + int shift = (int)(strength * 3); + + for (int y = 0; y < h; y++) { + uint8_t *dst = f->data + y * f->stride; + float ny = (y - cy) / cy; + for (int x = 0; x < w; x++) { + float nx = (x - cx) / cx; + float r2 = nx*nx + ny*ny; + /* Slight barrel distortion toward edges */ + float dx = nx * (1.0f + barrel * r2); + float dy = ny * (1.0f + barrel * r2); + int sx = (int)(dx * cx + cx); + int sy = (int)(dy * cy + cy); + if (sx < 0) sx = 0; + if (sx >= w) sx = w-1; + if (sy < 0) sy = 0; + if (sy >= h) sy = h-1; + + /* R/B channel shift for chromatic aberration */ + int rxr = sx + shift; if (rxr >= w) rxr = w-1; + int rxb = sx - shift; if (rxb < 0) rxb = 0; + + const uint8_t *sR = tmp + sy * w * 3 + rxr * 3; + const uint8_t *sG = tmp + sy * w * 3 + sx * 3; + const uint8_t *sB = tmp + sy * w * 3 + rxb * 3; + uint8_t r = sR[2], g = sG[1], b = sB[0]; + + /* Scanline darken every other row */ + int dark = ((y & 1) == 0) ? (int)(strength * 60) : 0; + dst[x*3+0] = clamp_u8(b - dark); + dst[x*3+1] = clamp_u8(g - dark); + dst[x*3+2] = clamp_u8(r - dark); + } + } + free(tmp); +} + +/* ── velvet: desat + warm tint + soft blur (Soft) ─────────────── */ +static void fx_velvet(Frame *f, float strength) +{ + if (strength < 0) strength = 0; + if (strength > 1) strength = 1; + int w = f->width, h = f->height; + + /* 3x3 box blur in place using a one-row scratch buffer to avoid + * reading freshly-written pixels (cheap separable approximation). */ + uint8_t *tmp = malloc((size_t)w * h * 3); + if (!tmp) return; + memcpy(tmp, f->data, (size_t)w * h * 3); + + for (int y = 0; y < h; y++) { + int y0 = y > 0 ? y - 1 : 0; + int y1 = y < h - 1 ? y + 1 : h - 1; + uint8_t *dst = f->data + y * f->stride; + for (int x = 0; x < w; x++) { + int x0 = x > 0 ? x - 1 : 0; + int x1 = x < w - 1 ? x + 1 : w - 1; + int sb = 0, sg = 0, sr = 0; + for (int yy = y0; yy <= y1; yy++) { + for (int xx = x0; xx <= x1; xx++) { + const uint8_t *p = tmp + yy * w * 3 + xx * 3; + sb += p[0]; sg += p[1]; sr += p[2]; + } + } + int n = (y1 - y0 + 1) * (x1 - x0 + 1); + int bb = sb / n, gg = sg / n, rr = sr / n; + + /* Desat toward luma */ + int luma = (rr*299 + gg*587 + bb*114) / 1000; + float desat = 0.5f * strength; + bb = (int)(bb * (1 - desat) + luma * desat); + gg = (int)(gg * (1 - desat) + luma * desat); + rr = (int)(rr * (1 - desat) + luma * desat); + + /* Warm tint */ + rr = clamp_u8(rr + (int)(20 * strength)); + gg = clamp_u8(gg + (int)( 8 * strength)); + bb = clamp_u8(bb - (int)(15 * strength)); + + /* Blend with original by strength so '0' = no-op */ + const uint8_t *orig = tmp + y * w * 3 + x * 3; + dst[x*3+0] = (uint8_t)(orig[0] * (1 - strength) + bb * strength); + dst[x*3+1] = (uint8_t)(orig[1] * (1 - strength) + gg * strength); + dst[x*3+2] = (uint8_t)(orig[2] * (1 - strength) + rr * strength); + } + } + free(tmp); +} + +/* ── noir: high-contrast B&W with split-tone shadows (Themed) ── */ +static void fx_noir(Frame *f, float strength) +{ + if (strength < 0) strength = 0; + if (strength > 1) strength = 1; + int n = f->width * f->height; + for (int i = 0; i < n; i++) { + uint8_t *p = f->data + i * 3; + int luma = (p[2]*299 + p[1]*587 + p[0]*114) / 1000; + + /* Sigmoid-ish contrast boost around 128 */ + float l = luma / 255.0f; + l = (l - 0.5f) * (1.0f + strength * 1.2f) + 0.5f; + if (l < 0) l = 0; + if (l > 1) l = 1; + int v = (int)(l * 255); + + /* Split-tone: cool blue in shadows, warm sepia in highlights */ + int rb, gg, bb; + if (v < 128) { + float t = v / 128.0f; + rb = (int)(v * (0.85f + 0.15f * t)); + gg = (int)(v * (0.95f + 0.05f * t)); + bb = (int)(v * (1.15f - 0.05f * t)); + } else { + float t = (v - 128) / 127.0f; + rb = (int)(v * (1.10f + 0.05f * t)); + gg = (int)(v * (1.00f)); + bb = (int)(v * (0.85f - 0.05f * t)); + } + + /* Mix with original by strength */ + p[0] = clamp_u8((int)(p[0] * (1 - strength) + bb * strength)); + p[1] = clamp_u8((int)(p[1] * (1 - strength) + gg * strength)); + p[2] = clamp_u8((int)(p[2] * (1 - strength) + rb * strength)); + } +} + +/* ── julia: Julia-fractal coloured overlay blended with image (Math) ── */ +static void fx_julia(Frame *f, float strength) +{ + if (strength < 0) strength = 0; + if (strength > 1) strength = 1; + int w = f->width, h = f->height; + + /* Animate the constant c slowly over time */ + unsigned long t = anim_tick(); + float ang = t * 0.01f; + float cr = 0.7885f * cosf(ang); + float ci = 0.7885f * sinf(ang); + + const int max_iter = 32; + for (int y = 0; y < h; y++) { + uint8_t *dst = f->data + y * f->stride; + float zy0 = (y - h / 2.0f) * 3.0f / h; + for (int x = 0; x < w; x++) { + float zx = (x - w / 2.0f) * 3.0f / w; + float zy = zy0; + int iter = 0; + while (iter < max_iter && zx*zx + zy*zy < 4.0f) { + float nzx = zx*zx - zy*zy + cr; + zy = 2 * zx * zy + ci; + zx = nzx; + iter++; + } + /* Map iter to a colour gradient (HSV-ish) */ + float fi = iter / (float)max_iter; + int rr = (int)(255 * fi); + int gg = (int)(255 * sinf(fi * 3.14159f)); + int bb = (int)(255 * (1 - fi)); + + /* Blend with the image by strength */ + dst[x*3+0] = clamp_u8((int)(dst[x*3+0] * (1 - strength) + bb * strength)); + dst[x*3+1] = clamp_u8((int)(dst[x*3+1] * (1 - strength) + gg * strength)); + dst[x*3+2] = clamp_u8((int)(dst[x*3+2] * (1 - strength) + rr * strength)); + } + } +} + +/* ── scanlines: hard black lines every N rows (Pixel-crazy) ─── */ +static void fx_scanlines(Frame *f, float gap) +{ + int g = (int)gap; + if (g < 2) g = 2; + if (g > 16) g = 16; + for (int y = 0; y < f->height; y++) { + if ((y % g) == 0) { + memset(f->data + y * f->stride, 0, f->width * 3); + } + } +} + void filter_chain_apply(FilterChain *fc, Frame *frame) { pthread_mutex_lock(&fc->lock); @@ -1349,7 +1579,8 @@ void filter_chain_apply(FilterChain *fc, Frame *frame) case FX_BRIGHTNESS: fx_brightness(frame, f->param); break; case FX_CONTRAST: fx_contrast(frame, f->param); break; case FX_POSTERIZE: fx_posterize(frame, (int)f->param); break; - case FX_EDGES: fx_edges(frame); break; + case FX_EDGES: fx_edges(frame, f->param); break; + case FX_SATURATE: fx_saturate(frame, f->param); break; case FX_CHROMASHIFT: fx_chromashift(frame, (int)f->param); break; case FX_GLITCH: fx_glitch(frame, f->param); break; case FX_KALEIDOSCOPE: fx_kaleidoscope(frame, (int)f->param); break; @@ -1360,27 +1591,32 @@ void filter_chain_apply(FilterChain *fc, Frame *frame) case FX_CARTOON: fx_cartoon(frame, f->param); break; case FX_SOLARIZE: fx_solarize(frame, (int)f->param); break; case FX_RAINBOW: fx_rainbow(frame, f->param); break; + case FX_CRT: fx_crt(frame, f->param); break; case FX_DREAM: fx_dream(frame, f->param); break; case FX_BLOOM: fx_bloom(frame, f->param); break; case FX_GLOW: fx_glow(frame, f->param); break; case FX_PINCH: fx_pinch(frame, f->param); break; case FX_BULGE: fx_bulge(frame, f->param); break; case FX_PAINTERLY: fx_painterly(frame, (int)f->param); break; + case FX_VELVET: fx_velvet(frame, f->param); break; case FX_MODERN: fx_modern(frame, f->param); break; case FX_CYBERPUNK: fx_cyberpunk(frame, f->param); break; case FX_ROMANTIC: fx_romantic(frame, f->param); break; case FX_VANGOGH: fx_vangogh(frame, f->param); break; case FX_PICASSO: fx_picasso(frame, f->param); break; + case FX_NOIR: fx_noir(frame, f->param); break; case FX_SINE: fx_sine(frame, f->param); break; case FX_XOR: fx_xor(frame, f->param); break; case FX_MODULO: fx_modulo(frame, f->param); break; case FX_POLAR: fx_polar(frame, f->param); break; case FX_VORONOI: fx_voronoi(frame, f->param); break; + case FX_JULIA: fx_julia(frame, f->param); break; case FX_BITCRUSH: fx_bitcrush(frame, f->param); break; case FX_MELT: fx_melt(frame, f->param); break; case FX_POPART: fx_popart(frame, f->param); break; case FX_DISPLACE: fx_displace(frame, f->param); break; case FX_RIPPLE: fx_ripple(frame, f->param); break; + case FX_SCANLINES: fx_scanlines(frame, f->param); break; default: break; } } diff --git a/src/ipc.c b/src/ipc.c index d883875..7382be1 100644 --- a/src/ipc.c +++ b/src/ipc.c @@ -156,7 +156,8 @@ static void handle_command(int client_fd, char *line, OverlayRegistry *reg) send_line(client_fd, "OK brightness <mult> multiply brightness, default 1.2"); send_line(client_fd, "OK contrast <mult> contrast around 128, default 1.4"); send_line(client_fd, "OK posterize <levels> reduce color levels, default 4"); - send_line(client_fd, "OK edges Sobel edge detection"); + send_line(client_fd, "OK edges <strength> Sobel edge detection, 0..2, default 1.0"); + send_line(client_fd, "OK saturate <factor> chroma boost, 0=gray, 1=normal, 0..3, default 1.5"); send_line(client_fd, "OK"); send_line(client_fd, "OK fancy filters:"); send_line(client_fd, "OK chromashift <offset> VHS-style RGB channel split, default 5"); @@ -169,6 +170,7 @@ static void handle_command(int client_fd, char *line, OverlayRegistry *reg) send_line(client_fd, "OK cartoon <edge_thresh> posterize + dark edges, default 50"); send_line(client_fd, "OK solarize <threshold> invert pixels above value, default 128"); send_line(client_fd, "OK rainbow <speed> animated hue rotation, default 1.0"); + send_line(client_fd, "OK crt <strength> CRT scanlines + barrel + RGB fringe, 0..1, default 0.7"); send_line(client_fd, "OK"); send_line(client_fd, "OK soft & dreamy filters:"); send_line(client_fd, "OK dream <strength> hazy soft glow, 0..1, default 0.5"); @@ -177,6 +179,7 @@ static void handle_command(int client_fd, char *line, OverlayRegistry *reg) send_line(client_fd, "OK pinch <strength> radial inward pull, 0..1, default 0.5"); send_line(client_fd, "OK bulge <strength> fisheye-like outward push, 0..1, default 0.5"); send_line(client_fd, "OK painterly <radius> oil-painting effect, 1..5, default 3"); + send_line(client_fd, "OK velvet <strength> soft desat + warm tint + blur, 0..1, default 0.6"); send_line(client_fd, "OK"); send_line(client_fd, "OK themed looks:"); send_line(client_fd, "OK modern <strength> teal-orange cinematic grade, default 0.7"); @@ -184,6 +187,7 @@ static void handle_command(int client_fd, char *line, OverlayRegistry *reg) send_line(client_fd, "OK romantic <strength> soft warm haze, default 0.7"); send_line(client_fd, "OK vangogh <radius> heavy painterly + saturation, 1..5, default 3"); send_line(client_fd, "OK picasso <segments> cubist: kaleidoscope+poster+edges, 4..16, default 8"); + send_line(client_fd, "OK noir <strength> high-contrast B&W + split-tone, 0..1, default 0.8"); send_line(client_fd, "OK"); send_line(client_fd, "OK mathematical patterns:"); send_line(client_fd, "OK sine <amplitude> sin-wave x/y distortion, default 15"); @@ -191,6 +195,7 @@ static void handle_command(int client_fd, char *line, OverlayRegistry *reg) send_line(client_fd, "OK modulo <divisor> color mod N, 2..128, default 64"); send_line(client_fd, "OK polar <strength> polar-coordinate warp, 0..1, default 0.7"); send_line(client_fd, "OK voronoi <cells> voronoi cell mosaic, 4..64, default 20"); + send_line(client_fd, "OK julia <strength> animated Julia fractal overlay, 0..1, default 0.5"); send_line(client_fd, "OK"); send_line(client_fd, "OK pixel-crazy / animated:"); send_line(client_fd, "OK bitcrush <bits> quantize bits per channel, 1..8, default 3"); @@ -198,6 +203,7 @@ static void handle_command(int client_fd, char *line, OverlayRegistry *reg) send_line(client_fd, "OK popart <strength> Warhol 2x2 tinted quadrants, 0..1, default 1"); send_line(client_fd, "OK displace <strength> luma-driven horizontal shift, 0..1, default 0.5"); send_line(client_fd, "OK ripple <strength> animated water ripple from center, 0..1, default 0.5"); + send_line(client_fd, "OK scanlines <gap> black scanline every N rows, 2..16, default 3"); return; } diff --git a/src/main.c b/src/main.c index 475f454..932d3e7 100644 --- a/src/main.c +++ b/src/main.c @@ -42,6 +42,8 @@ static volatile bool g_running = true; static OverlayRegistry g_registry; static IPCServer g_ipc; FilterChain g_filters; /* extern'd by ipc.c */ +int g_output_w = 0; /* extern'd by overlay.c */ +int g_output_h = 0; static void on_signal(int sig) { @@ -237,6 +239,8 @@ int main(int argc, char *argv[]) registry_destroy(&g_registry); filter_chain_destroy(&g_filters); return 1; } + g_output_w = width; + g_output_h = height; /* ── start IPC server ── */ if (!ipc_start(&g_ipc, &g_registry)) { diff --git a/src/overlay.c b/src/overlay.c index 3d7f38c..224ba01 100644 --- a/src/overlay.c +++ b/src/overlay.c @@ -1,3 +1,4 @@ +#define _GNU_SOURCE /* for F_SETPIPE_SZ */ #include "vcam.h" #include <stdio.h> @@ -423,6 +424,23 @@ Overlay *overlay_create_screen(const char *geometry, o->src_w = gw; o->src_h = gh; resolve_dims(gw, gh, dst_w, dst_h, &o->dst_w, &o->dst_h); + /* Clamp to output dimensions if known — otherwise overlay_blit would + * just clip and the user would only see a corner of their screen. */ + if (g_output_w > 0 && g_output_h > 0 && + (o->dst_w > g_output_w || o->dst_h > g_output_h)) { + /* Scale down preserving aspect so it fits the output */ + double sx = (double)g_output_w / o->dst_w; + double sy = (double)g_output_h / o->dst_h; + double s = sx < sy ? sx : sy; + int new_w = (int)(o->dst_w * s); + int new_h = (int)(o->dst_h * s); + /* Snap to multiples of 2 for stride safety */ + new_w &= ~1; new_h &= ~1; + fprintf(stderr, "[screen] clamping %dx%d → %dx%d to fit %dx%d output\n", + o->dst_w, o->dst_h, new_w, new_h, g_output_w, g_output_h); + o->dst_w = new_w; o->dst_h = new_h; + } + bool wayland = getenv("WAYLAND_DISPLAY") != NULL; if (wayland) { @@ -446,6 +464,19 @@ Overlay *overlay_create_screen(const char *geometry, unlink(o->fifo_path); free(o); return NULL; } + /* Tune the kernel pipe buffer to ~one frame. + * Default is 64KB, way smaller than a raw frame (~3-8MB), which + * forces wf-recorder to issue many partial writes per frame. + * Larger than 1 frame would let wf-recorder build up a backlog + * → visible lag. Sizing to exactly one frame means wf-recorder + * can write a single frame in one shot, blocks on the next + * until we read — tight coupling, minimal latency. */ + size_t one_frame = (size_t)gw * gh * 4; /* worst case: BGR0 */ + if (fcntl(o->pipe_fd, F_SETPIPE_SZ, (int)one_frame) < 0) { + /* F_SETPIPE_SZ may fail past /proc/sys/fs/pipe-max-size; + * non-fatal, just means we use the kernel's default. */ + } + char wfr_geo[64]; snprintf(wfr_geo, sizeof wfr_geo, "%d,%d %dx%d", gx, gy, gw, gh);