ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
backends.h File Reference

Go to the source code of this file.

Functions

char * digital_rain_render_at (digital_rain_t *rain, const char *frame, float seconds)
char * rainbow_render_ansi_at (const char *frame, float seconds)
asciichat_error_t test_pattern_render_at (struct test_pattern *pattern, int index, double time_ms, bool cadence)

Function Documentation

◆ digital_rain_render_at()

char * digital_rain_render_at ( digital_rain_t * rain,
const char * frame,
float seconds )

Definition at line 376 of file digital_rain.c.

376 {
377 if (!rain || !frame) {
378 log_error("NULL parameter");
379 return NULL;
380 }
381
382 if (!isfinite(seconds) || seconds < 0)
383 return NULL;
384 float elapsed = fmaxf(0.0f, seconds - rain->time);
385 rain->time = seconds;
386 float sim_time = seconds;
387 // brightness_decay retains its meaning as a blend factor at 60 Hz.
388 float decay = fminf(1.0f, fmaxf(0.0f, rain->brightness_decay));
389 float alpha = elapsed > 0 ? 1.0f - powf(1.0f - decay, elapsed * 60.0f) : 0.0f;
390
391 if (rain->rainbow_mode)
392 color_filter_calculate_rainbow(sim_time, &rain->color_r, &rain->color_g, &rain->color_b);
393
394 size_t input_len = strlen(frame);
395 if (input_len > (SIZE_MAX - 1024) / 48)
396 return NULL;
397 size_t output_capacity = input_len * 48 + 1024;
398 char *output = SAFE_MALLOC(output_capacity, char *);
399 if (!output)
400 return NULL;
401
402 const char *src = frame;
403 char *dst = output;
404 size_t remaining = output_capacity;
405 int col = 0, row = 0;
406 float foreground_luminance = 1.0f, background_luminance = 0.0f;
407 bool has_background = false;
408 bool colored = false;
409
410 while (*src && remaining > 100) {
411 if (*src == '\033') {
412 int r, g, b;
413 bool foreground;
414 const char *after = parse_ansi_color(src, &r, &g, &b, &foreground);
415 if (after) {
416 float luminance = fminf(1.0f, fmaxf(0.0f, (0.2126f * r + 0.7152f * g + 0.0722f * b) / 255.0f));
417 if (foreground)
418 foreground_luminance = luminance;
419 else {
420 background_luminance = luminance;
421 has_background = true;
422 }
423 src = after;
424 } else {
425 after = skip_ansi_sequence(src);
426 size_t count = (size_t)(after - src);
427 if ((count == 4 && memcmp(src, "\033[0m", 4) == 0) || (count == 3 && memcmp(src, "\033[m", 3) == 0)) {
428 foreground_luminance = 1.0f;
429 has_background = false;
430 }
431 memcpy(dst, src, count);
432 dst += count;
433 remaining -= count;
434 src = after;
435 }
436 continue;
437 }
438 if (*src == '\n' || *src == '\r') {
439 if (*src == '\n')
440 row++;
441 col = 0;
442 *dst++ = *src++;
443 remaining--;
444 continue;
445 }
446
447 float head;
448 float brightness = get_rain_brightness(rain, col, row, sim_time, &head);
449 // Update once per cell, independently of how many ANSI sequences precede it.
450 if (row < rain->num_rows && col < rain->num_columns) {
451 size_t idx = (size_t)row * (size_t)rain->num_columns + (size_t)col;
452 if (!rain->first_frame)
453 brightness = rain->previous_brightness[idx] + (brightness - rain->previous_brightness[idx]) * alpha;
454 rain->previous_brightness[idx] = brightness;
455 }
456 int count = generate_rain_color(dst, remaining, rain, brightness, head, foreground_luminance, true);
457 colored = true;
458 dst += count;
459 remaining -= (size_t)count;
460 if (has_background) {
461 count = generate_rain_color(dst, remaining, rain, brightness, head, background_luminance, false);
462 dst += count;
463 remaining -= (size_t)count;
464 }
465 uint32_t codepoint;
466 int bytes = utf8_decode((const uint8_t *)src, &codepoint);
467 if (bytes <= 0) {
468 bytes = 1;
469 codepoint = (uint8_t)*src;
470 }
471 if (bytes == 1 && codepoint > 32 && codepoint < 127) {
472 *dst++ = (char)mutate_glyph(codepoint, col, row, sim_time);
473 src++;
474 remaining--;
475 } else {
476 memcpy(dst, src, (size_t)bytes);
477 dst += bytes;
478 src += bytes;
479 remaining -= (size_t)bytes;
480 }
481 col++;
482 }
483 // Monochrome input has no trailing SGR reset to copy. The effect introduces
484 // colors itself, so every colored result must restore terminal attributes.
485 if (colored) {
486 const char reset[] = "\033[0m";
487 if (remaining < sizeof(reset)) {
488 SAFE_FREE(output);
489 return NULL;
490 }
491 memcpy(dst, reset, sizeof(reset));
492 } else {
493 *dst = '\0';
494 }
495 rain->first_frame = false;
496 return output;
497}
unsigned int uint32_t
#define SAFE_FREE(ptr)
#define SAFE_MALLOC(size, cast)
unsigned char uint8_t
#define log_error(...)
Log an ERROR message.
Definition log/log.h:604
int utf8_decode(const uint8_t *s, uint32_t *codepoint)
Decode a UTF-8 sequence to a Unicode codepoint.
Definition utf8.c:28
void color_filter_calculate_rainbow(float time, uint8_t *r, uint8_t *g, uint8_t *b)
Calculate rainbow color from time (3.5s cycle).
uint8_t color_g
Matrix green G component.
uint8_t color_b
Matrix green B component.
float brightness_decay
Brightness smoothing factor (0-1).
float time
Current simulation time (accumulated).
uint8_t color_r
Matrix green R component.
bool first_frame
True if this is the first frame.
bool rainbow_mode
True if rainbow color cycling is enabled.
float * previous_brightness
Previous frame brightness (for smoothing).
int num_columns
Number of columns in grid.

References digital_rain::brightness_decay, digital_rain::color_b, color_filter_calculate_rainbow(), digital_rain::color_g, digital_rain::color_r, digital_rain::first_frame, log_error, digital_rain::num_columns, digital_rain::previous_brightness, digital_rain::rainbow_mode, SAFE_FREE, SAFE_MALLOC, digital_rain::time, and utf8_decode().

Referenced by animation_apply().

◆ rainbow_render_ansi_at()

char * rainbow_render_ansi_at ( const char * frame,
float seconds )

Definition at line 364 of file color_filter.c.

364 {
365 if (!ansi_string) {
366 return NULL;
367 }
368
369 // Calculate rainbow color once for this frame
370 uint8_t rainbow_r = 0, rainbow_g = 0, rainbow_b = 0;
371 color_filter_calculate_rainbow(time_seconds, &rainbow_r, &rainbow_g, &rainbow_b);
372
373 // Build the replacement ANSI code string once
375 snprintf(rainbow_code, sizeof(rainbow_code), "\x1b[38;2;%d;%d;%dm", rainbow_r, rainbow_g, rainbow_b);
376 size_t rainbow_code_len = strlen(rainbow_code);
377
378 // Check if there are any ANSI codes to replace
379 if (!strstr(ansi_string, "\x1b[38;2;")) {
380 return NULL; // No ANSI codes found, no replacement needed
381 }
382
383 size_t input_len = strlen(ansi_string);
384 size_t replacements = 0;
385 const char *scan = ansi_string;
386 while ((scan = strstr(scan, "\x1b[38;2;")) != NULL) {
387 const char *end = strchr(scan + 7, 'm');
388 if (!end)
389 break;
390 replacements++;
391 scan = end + 1;
392 }
393 if (input_len == SIZE_MAX || replacements > (SIZE_MAX - input_len - 1) / rainbow_code_len)
394 return NULL;
395 char *new_result = SAFE_MALLOC(input_len + replacements * rainbow_code_len + 1, char *);
396 if (!new_result) {
397 return NULL;
398 }
399
400 // Replace all RGB color codes with rainbow color
401 const char *src = ansi_string;
402 char *dst = new_result;
403
404 while (*src) {
405 const char *ansi_start = strstr(src, "\x1b[38;2;");
406 if (ansi_start) {
407 // Copy everything before this ANSI code
408 size_t before_len = (size_t)(ansi_start - src);
409 memcpy(dst, src, before_len);
410 dst += before_len;
411
412 // Skip the old ANSI code (find the 'm' that ends it)
413 const char *ansi_end = strchr(ansi_start + 7, 'm');
414 if (ansi_end) {
415 // Copy the rainbow replacement code
416 // NOLINTNEXTLINE(bugprone-not-null-terminated-result) - binary data
417 memcpy(dst, rainbow_code, rainbow_code_len);
418 dst += rainbow_code_len;
419
420 // Move source past the old code
421 src = ansi_end + 1;
422 } else {
423 // Preserve a malformed trailing sequence without duplicating the prefix.
424 size_t remaining = strlen(ansi_start);
425 memcpy(dst, ansi_start, remaining);
426 dst += remaining;
427 break;
428 }
429 } else {
430 // No more ANSI codes, copy the rest
431 size_t remaining = strlen(src);
432 memcpy(dst, src, remaining);
433 dst += remaining;
434 break;
435 }
436 }
437 *dst = '\0';
438
439 return new_result;
440}
#define VIDEO_RGBA_COLOR_FILTER_RAINBOW_CODE_BUFFER_SIZE
Definition color_filter.c:8

References color_filter_calculate_rainbow(), SAFE_MALLOC, and VIDEO_RGBA_COLOR_FILTER_RAINBOW_CODE_BUFFER_SIZE.

Referenced by animation_apply().

◆ test_pattern_render_at()

asciichat_error_t test_pattern_render_at ( struct test_pattern * pattern,
int index,
double time_ms,
bool cadence )

Definition at line 159 of file video/anim/test_pattern.c.

159 {
160 if (!p || !p->image || index < 0 || index > 1 || !isfinite(time_ms) || time_ms < 0)
161 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid test pattern frame parameters");
162 int w = p->image->w, h = p->image->h;
163 double phase = fmod(time_ms, index ? 4000 : 10000) / (index ? 4000 : 10000);
164 rgb_pixel_t dark = {16, 24, 32}, white = {255, 255, 255};
165 rgb_pixel_t first = hue(phase * 360, 0.5), last = hue(fmod(phase + 0.5, 1) * 360, 0.5);
166 double box = fmin(fmax(32, fmin(w, h) / 4.0), fmin(w, h) / 2.0);
167 double bx = fmod(time_ms / 11, w + box) - box, by = fmod(time_ms / 17, h + box) - box;
168 double stripe = w / 12.0, offset = phase * w;
169 int first_stripe = (int)ceil(-offset / stripe);
170 double radius = fmin(fmax(12, fmin(w, h) / 8.0), fmin(w, h) / 4.0);
171 double cx = w + radius - phase * (w + radius * 2), cy = h / 2.0 + sin(phase * 12.566370614359172) * h / 4.0;
173 for (int i = 0; i < 12; i++)
174 colors[i] = hue(i * 30, 0.55);
175 uint32_t frame = (uint32_t)fmod(floor(time_ms / (1000.0 / 60)), 512);
176 uint32_t code = frame ^ (frame >> 1);
177 rgb_pixel_t *pixels = p->image->pixels;
178 if (index == 1) {
179 // The background is constant down each column. Rasterize one row, then copy.
180 for (int x = 0; x < w; x++) {
181 rgb_pixel_t pixel = dark;
182 int k = (int)floor((x - offset) / stripe) + 1;
183 for (int j = k; j <= k + 1; j++)
184 if (j >= first_stripe && j <= first_stripe + 12)
185 blend(&pixel, colors[(j % 12 + 12) % 12], overlap(x, j * stripe + offset - stripe, j * stripe + offset));
186 pixels[x] = pixel;
187 }
188 for (int y = 1; y < h; y++)
189 memcpy(pixels + (size_t)y * w, pixels, (size_t)w * sizeof(*pixels));
190 if (cadence) {
191 // Show all nine Gray-code bits in a compact 3x3 marker. Showing only
192 // bit zero repeats every other 60 Hz frame, hiding valid presentations
193 // from a visual cadence check. Adjacent codes differ in exactly one
194 // tile while the surrounding animated pattern remains visible.
195 int marker = (int)fmax(16, fmin(w, h) / 16.0);
196 int marker_left = (w - marker) / 2;
197 int marker_top = (h - marker) / 2;
198 for (int y = marker_top; y < marker_top + marker && y < h; y++)
199 for (int x = marker_left; x < marker_left + marker && x < w; x++) {
200 unsigned tile_x = (unsigned)(x - marker_left) * 3 / (unsigned)marker;
201 unsigned tile_y = (unsigned)(y - marker_top) * 3 / (unsigned)marker;
202 unsigned bit = tile_y * 3 + tile_x;
203 pixels[(size_t)y * w + x] = (code & (1u << bit)) ? white : (rgb_pixel_t){0, 0, 0};
204 }
205 return ASCIICHAT_OK;
206 }
207 int top = (int)fmax(0, floor(cy - radius - 1)), bottom = (int)fmin(h, ceil(cy + radius + 1));
208 int left = (int)fmax(0, floor(cx - radius - 1)), right = (int)fmin(w, ceil(cx + radius + 1));
209 for (int y = top; y < bottom; y++)
210 for (int x = left; x < right; x++) {
211 double dx0 = x + 0.5 - cx, dy0 = y + 0.5 - cy, d2 = dx0 * dx0 + dy0 * dy0;
212 if (d2 > (radius + 0.75) * (radius + 0.75))
213 continue;
214 double coverage = 1;
215 if (d2 > (radius - 0.75) * (radius - 0.75)) {
216 int inside = 0;
217 for (int sy = 0; sy < 4; sy++)
218 for (int sx = 0; sx < 4; sx++) {
219 double dx = x + (sx + 0.5) / 4 - cx, dy = y + (sy + 0.5) / 4 - cy;
220 if (dx * dx + dy * dy < radius * radius)
221 inside++;
222 }
223 coverage = inside / 16.0;
224 }
225 blend(&pixels[(size_t)y * w + x], white, coverage);
226 }
227 } else {
228 // A fixed-point ramp avoids three floating-point conversions per source
229 // pixel. 4096 intervals keep rounding within one RGB level of Canvas.
231 for (int i = 0; i <= TEST_PATTERN_GRADIENT_INTERVALS; i++) {
232 double t = i / (double)TEST_PATTERN_GRADIENT_INTERVALS;
233 gradient[i] = (rgb_pixel_t){(uint8_t)(first.r * (1 - t) + last.r * t + 0.5), (uint8_t)(first.g * (1 - t) + last.g * t + 0.5), (uint8_t)(first.b * (1 - t) + last.b * t + 0.5)};
234 }
235 double scale = (double)TEST_PATTERN_GRADIENT_INTERVALS * 65536 / ((double)w * w + (double)h * h);
236 uint32_t step = (uint32_t)(w * scale + 0.5);
237 for (int y = 0; y < h; y++) {
238 uint32_t position = (uint32_t)((0.5 * w + (y + 0.5) * h) * scale + 0.5);
239 for (int x = 0; x < w; x++, position += step)
240 pixels[(size_t)y * w + x] = gradient[position >> 16];
241 }
242 int top = (int)fmax(0, floor(by)), bottom = (int)fmin(h, ceil(by + box));
243 int left = (int)fmax(0, floor(bx)), right = (int)fmin(w, ceil(bx + box));
244 for (int y = top; y < bottom; y++)
245 for (int x = left; x < right; x++)
246 blend(&pixels[(size_t)y * w + x], white, 0.8 * overlap(x, bx, bx + box) * overlap(y, by, by + box));
247 }
248 render_test_label(p->image);
249 return ASCIICHAT_OK;
250}
#define SET_ERRNO(code,...)
unsigned char colors[(512 *256 *2) *3]
Definition fft.c:23
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
static bool size_t index
Definition queries.h:10
uint8_t b
Blue color component (0-255).
uint8_t g
Green color component (0-255).
uint8_t r
Red color component (0-255).
#define TEST_PATTERN_GRADIENT_INTERVALS

References ASCIICHAT_OK, rgb_pixel_t::b, colors, ERROR_INVALID_PARAM, rgb_pixel_t::g, image::h, test_pattern::image, index, image::pixels, rgb_pixel_t::r, SET_ERRNO, TEST_PATTERN_GRADIENT_INTERVALS, and image::w.

Referenced by animation_apply().