ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
Video chat in your terminal
Loading...
Searching...
No Matches
video/ascii/common.h
Go to the documentation of this file.
1#pragma once
2
26
27/* Existing buffer budgets in bytes, including NUL for text storage. */
28#define ASCII_LUMINANCE_LEVELS 256
29#define ASCII_PALETTE_BUFFER_SIZE 256
30
31#include <stdbool.h>
32#include <stddef.h>
33#include <libplatform/atomic.h>
35#include <ascii-chat/uthash.h>
36#include "../../common.h"
37
38// Check for SIMD support and include architecture-specific headers
39// CMake explicitly controls which SIMD levels to enable via ENABLE_SIMD_* options
40// and defines SIMD_SUPPORT_* to 1 (enabled) or 0 (disabled).
41//
42// Only auto-detect from compiler macros if CMake hasn't already defined them.
43// This ensures CMake's explicit choices (e.g., AVX2-only, no SSE2/SSSE3) are respected.
44//
45// Disable SIMD on Windows with Clang due to intrinsics compatibility issues
46#if defined(_WIN32) && defined(__clang__) && __clang_major__ >= 20
47// Windows with Clang 20+ has issues with MMX intrinsics
48// Keep SIMD disabled until fixed
49#else
50// Only auto-detect if CMake hasn't already defined the macro
51#if defined(__ARM_FEATURE_SVE) && !defined(SIMD_SUPPORT_SVE)
52#define SIMD_SUPPORT_SVE 1
53#endif
54
55#if defined(__AVX2__) && !defined(SIMD_SUPPORT_AVX2)
56#define SIMD_SUPPORT_AVX2 1
57#endif
58
59#if defined(__SSE2__) && !defined(SIMD_SUPPORT_SSE2)
60#define SIMD_SUPPORT_SSE2 1
61#endif
62
63#if defined(__SSSE3__) && !defined(SIMD_SUPPORT_SSSE3)
64#define SIMD_SUPPORT_SSSE3 1
65#endif
66
67#if defined(__ARM_NEON) && !defined(SIMD_SUPPORT_NEON)
68#define SIMD_SUPPORT_NEON 1
69#endif
70#endif
71
72// Availability means a backend was compiled, not that the running CPU supports it.
73#if SIMD_SUPPORT_SVE || SIMD_SUPPORT_AVX2 || SIMD_SUPPORT_SSSE3 || SIMD_SUPPORT_SSE2 || SIMD_SUPPORT_NEON
74#define SIMD_SUPPORT 1
75#endif
76
89
91#define LUMA_RED 77
93#define LUMA_GREEN 150
95#define LUMA_BLUE 29
97#define LUMA_THRESHOLD 128
98
100
106typedef struct {
108 char s[3];
109} dec3_t;
110
120
123
124// Pre-computed ANSI escape code templates
125// static const char ANSI_FG_PREFIX[] = "\033[38;2;"; // Unused, replaced by inline constants
126// static const char ANSI_BG_PREFIX[] = "\033[48;2;"; // Unused, replaced by inline constants
127// static const char ANSI_SUFFIX[] = "m";
128
136void init_dec3(void);
137
145void ascii_simd_init(void);
146
149
156
167typedef struct {
168 char *data;
169 size_t len;
170 size_t cap;
171} Str;
172
179void str_init(Str *s);
180
188
196void str_reserve(Str *s, size_t need);
197
206void str_append_bytes(Str *s, const void *src, size_t n);
207
215void str_append_c(Str *s, char c);
216
225void str_printf(Str *s, const char *fmt, ...);
226
234typedef struct {
235 int cFR, cFG, cFB;
236 int cBR, cBG, cBB;
237 int runLen;
238 int seeded;
239} RLEState;
240
248typedef struct {
249 int w;
250 int h;
252} ImageRGB;
253
264ImageRGB alloc_image(int w, int h);
265
275void convert_pixels_scalar(const rgb_pixel_t *pixels, char *ascii_chars, int count,
276 const char luminance_palette[ASCII_LUMINANCE_LEVELS]);
277
286char *convert_pixels_scalar_with_newlines(image_t *image, const char luminance_palette[ASCII_LUMINANCE_LEVELS]);
287
288// Row-based functions removed - use image_print_color() instead
289
295typedef struct {
296 double scalar_time;
297 double sse2_time;
298 double ssse3_time;
299 double avx2_time;
300 double neon_time;
301 double sve_time;
303 const char *best_method;
305
316
327simd_benchmark_t benchmark_simd_color_conversion(int width, int height, int iterations, bool background_mode);
328
341simd_benchmark_t benchmark_simd_conversion_with_source(int width, int height, int iterations, bool background_mode,
342 const image_t *source_image, bool use_256color);
343
357 bool background_mode, const image_t *source_image,
358 bool use_256color);
359
368
379char *image_print_color_simd(image_t *image, bool use_background_mode, bool use_256color, const char *ascii_chars);
380
389void set_color_quality_mode(bool high_quality);
390
398
406void prewarm_sgr256_fg_cache(void);
407
415void prewarm_sgr256_cache(void);
416
425char *get_sgr256_fg_string(uint8_t fg, uint8_t *len_out);
426
435char *get_sgr256_bg_string(uint8_t bg, uint8_t *len_out);
436
446char *get_sgr256_fg_bg_string(uint8_t fg, uint8_t bg, uint8_t *len_out);
447
469size_t write_row_rep_from_arrays_enhanced(const uint8_t *fg_r, const uint8_t *fg_g, const uint8_t *fg_b,
470 const uint8_t *bg_r, const uint8_t *bg_g, const uint8_t *bg_b,
471 const uint8_t *fg_idx, const uint8_t *bg_idx, const char *ascii_chars,
472 int width, char *dst, size_t cap, bool is_truecolor);
473
479#define RAMP64_SIZE 64
480#define CACHE_FREQUENCY_DECAY_TIME 10000 // 10 seconds in milliseconds
481#define CACHE_RECENCY_SCALE 100
482#define CACHE_MAX_LIFETIME 3600 // 1 hour maximum cache lifetime in seconds
483
490
509
515void build_utf8_luminance_cache(const char *ascii_chars, utf8_char_t cache[256]);
516
522void build_utf8_ramp64_cache(const char *ascii_chars, utf8_char_t cache64[64],
523 uint8_t char_index_ramp[ASCII_LUMINANCE_LEVELS]);
524
530utf8_palette_cache_t *get_utf8_palette_cache(const char *ascii_chars);
531
539void simd_caches_destroy_all(void);
540
541// Include architecture-specific implementations
542#if SIMD_SUPPORT_SSE2
544#endif
545#if SIMD_SUPPORT_SSSE3
547#endif
548#if SIMD_SUPPORT_AVX2
550#endif
551#if SIMD_SUPPORT_SVE
553#endif
554#if SIMD_SUPPORT_NEON
556#endif
AVX2-optimized ASCII rendering functions.
⚙️ Common definitions, error codes, macros, and types shared throughout the application
unsigned int height
Definition fft.c:23
unsigned int width
Definition fft.c:23
#define UTF8_MAX_BYTES
unsigned int uint32_t
Definition common.h:62
unsigned long long uint64_t
Definition common.h:63
unsigned char uint8_t
Definition common.h:60
simd_benchmark_t benchmark_simd_color_conversion(int width, int height, int iterations, bool background_mode)
Benchmark SIMD color conversion methods.
global_dec3_cache_t g_dec3_cache
Global decimal cache instance.
void build_utf8_luminance_cache(const char *ascii_chars, utf8_char_t cache[256])
Build UTF-8 luminance cache for all 256 levels.
void str_destroy(Str *s)
Free string buffer.
void simd_caches_destroy_all(void)
Destroy all SIMD caches (palette and other caches).
uint8_t utf8_bytes[4]
char * get_sgr256_fg_bg_string(uint8_t fg, uint8_t bg, uint8_t *len_out)
Get 256-color foreground/background ANSI sequence string.
Definition sgr.c:116
utf8_palette_cache_t * get_utf8_palette_cache(const char *ascii_chars)
Get UTF-8 palette cache for a character set.
void set_color_quality_mode(bool high_quality)
Set color quality mode.
simd_benchmark_t benchmark_simd_conversion(int width, int height, int iterations)
Benchmark SIMD conversion methods (monochrome).
char * convert_pixels_scalar_with_newlines(image_t *image, const char luminance_palette[256])
Convert image to ASCII with newlines (scalar fallback).
void init_dec3(void)
Initialize decimal lookup table.
void str_reserve(Str *s, size_t need)
Reserve space in string buffer.
void str_printf(Str *s, const char *fmt,...)
Append formatted string to buffer.
size_t write_row_rep_from_arrays_enhanced(const uint8_t *fg_r, const uint8_t *fg_g, const uint8_t *fg_b, const uint8_t *bg_r, const uint8_t *bg_g, const uint8_t *bg_b, const uint8_t *fg_idx, const uint8_t *bg_idx, const char *ascii_chars, int width, char *dst, size_t cap, bool is_truecolor)
Enhanced REP compression writer.
void str_append_c(Str *s, char c)
Append character to string buffer.
void str_init(Str *s)
Initialize string buffer.
simd_benchmark_t benchmark_simd_color_conversion_with_source(int width, int height, int iterations, bool background_mode, const image_t *source_image, bool use_256color)
Benchmark SIMD color conversion with source image.
void prewarm_sgr256_cache(void)
Prewarm 256-color foreground/background cache for benchmarks.
Definition sgr.c:95
void print_simd_capabilities(void)
Print detected SIMD capabilities.
simd_benchmark_t benchmark_simd_conversion_with_source(int width, int height, int iterations, bool background_mode, const image_t *source_image, bool use_256color)
Benchmark SIMD conversion with source image.
#define ASCII_LUMINANCE_LEVELS
Number of luminance levels supported (256).
Definition ascii.h:440
void convert_pixels_scalar(const rgb_pixel_t *pixels, char *ascii_chars, int count, const char luminance_palette[256])
Convert pixels to ASCII (scalar fallback).
#define ASCII_PALETTE_BUFFER_SIZE
bool get_256_color_fast_path(void)
Get 256-color fast path setting.
char g_default_luminance_palette[ASCII_LUMINANCE_LEVELS]
Default luminance palette (256 characters).
void prewarm_sgr256_fg_cache(void)
Prewarm 256-color foreground cache for benchmarks.
Definition sgr.c:91
ImageRGB alloc_image(int w, int h)
Allocate a new ImageRGB (RGB8 format).
struct utf8_palette_cache utf8_palette_cache_t
char * get_sgr256_bg_string(uint8_t bg, uint8_t *len_out)
Get 256-color background ANSI sequence string.
Definition sgr.c:108
utf8_char_t cache64[64]
char * get_sgr256_fg_string(uint8_t fg, uint8_t *len_out)
Get 256-color foreground ANSI sequence string.
Definition sgr.c:100
uint8_t * pixels
void build_utf8_ramp64_cache(const char *ascii_chars, utf8_char_t cache64[64], uint8_t char_index_ramp[256])
Build UTF-8 ramp64 cache and character index ramp.
void init_default_luminance_palette(void)
Initialize default luminance palette.
utf8_char_t cache[256]
char * data
void str_append_bytes(Str *s, const void *src, size_t n)
Append bytes to string buffer.
void ascii_simd_init(void)
Initialize SIMD subsystem.
char * image_print_color_simd(image_t *image, bool use_background_mode, bool use_256color, const char *ascii_chars)
Print image as ASCII with color using SIMD.
Definition sgr.c:159
NEON-optimized ASCII rendering functions.
SSE2-optimized ASCII rendering functions.
SSSE3-optimized ASCII rendering functions.
ImageRGB structure for NEON renderers.
Run-length encoding state for ANSI color optimization.
Growable NUL-terminated buffer used while assembling ASCII output.
Decimal conversion cache structure (1-3 digits).
Global decimal cache for digit conversion.
SIMD-friendly storage for one RGB pixel. r, g, and b hold the color channels and padding keeps the la...
SIMD benchmark results structure.
SVE-optimized ASCII rendering functions.