ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
video/ascii/common.h
Go to the documentation of this file.
1#pragma once
2
27#include <stdbool.h>
28#include <stddef.h>
29#include <ascii-chat/atomic.h>
31#include <ascii-chat/uthash.h>
32#include "../../common.h"
33
34// Check for SIMD support and include architecture-specific headers
35// CMake explicitly controls which SIMD levels to enable via ENABLE_SIMD_* options
36// and defines SIMD_SUPPORT_* to 1 (enabled) or 0 (disabled).
37//
38// Only auto-detect from compiler macros if CMake hasn't already defined them.
39// This ensures CMake's explicit choices (e.g., AVX2-only, no SSE2/SSSE3) are respected.
40//
41// Disable SIMD on Windows with Clang due to intrinsics compatibility issues
42#if defined(_WIN32) && defined(__clang__) && __clang_major__ >= 20
43// Windows with Clang 20+ has issues with MMX intrinsics
44// Keep SIMD disabled until fixed
45#else
46// Only auto-detect if CMake hasn't already defined the macro
47#if defined(__ARM_FEATURE_SVE) && !defined(SIMD_SUPPORT_SVE)
48#define SIMD_SUPPORT_SVE 1
49#endif
50
51#if defined(__AVX2__) && !defined(SIMD_SUPPORT_AVX2)
52#define SIMD_SUPPORT_AVX2 1
53#endif
54
55#if defined(__SSE2__) && !defined(SIMD_SUPPORT_SSE2)
56#define SIMD_SUPPORT_SSE2 1
57#endif
58
59#if defined(__SSSE3__) && !defined(SIMD_SUPPORT_SSSE3)
60#define SIMD_SUPPORT_SSSE3 1
61#endif
62
63#if defined(__ARM_NEON) && !defined(SIMD_SUPPORT_NEON)
64#define SIMD_SUPPORT_NEON 1
65#endif
66#endif
67
68// Define umbrella SIMD_SUPPORT macro if any specific SIMD is supported
69#if defined(SIMD_SUPPORT_SVE) || defined(SIMD_SUPPORT_AVX2) || defined(SIMD_SUPPORT_SSSE3) || \
70 defined(SIMD_SUPPORT_SSE2) || defined(SIMD_SUPPORT_NEON)
71#define SIMD_SUPPORT 1
72#endif
73
80#define LUMA_RED 77
82#define LUMA_GREEN 150
84#define LUMA_BLUE 29
86#define LUMA_THRESHOLD 128
87
95typedef struct {
97 char s[3];
98} dec3_t;
99
105typedef struct {
106 dec3_t dec3_table[256];
109
112
113// Pre-computed ANSI escape code templates
114// static const char ANSI_FG_PREFIX[] = "\033[38;2;"; // Unused, replaced by inline constants
115// static const char ANSI_BG_PREFIX[] = "\033[48;2;"; // Unused, replaced by inline constants
116// static const char ANSI_SUFFIX[] = "m";
117
125void init_dec3(void);
126
134void ascii_simd_init(void);
135
137extern char g_default_luminance_palette[256];
138
145
151typedef struct {
152 char *data;
153 size_t len;
154 size_t cap;
155} Str;
156
163void str_init(Str *s);
164
172
180void str_reserve(Str *s, size_t need);
181
190void str_append_bytes(Str *s, const void *src, size_t n);
191
199void str_append_c(Str *s, char c);
200
209void str_printf(Str *s, const char *fmt, ...);
210
218typedef struct {
219 int cFR, cFG, cFB;
220 int cBR, cBG, cBB;
221 int runLen;
222 int seeded;
223} RLEState;
224
232typedef struct {
233 int w;
234 int h;
236} ImageRGB;
237
248ImageRGB alloc_image(int w, int h);
249
259void convert_pixels_scalar(const rgb_pixel_t *pixels, char *ascii_chars, int count, const char luminance_palette[256]);
260
269char *convert_pixels_scalar_with_newlines(image_t *image, const char luminance_palette[256]);
270
271// Row-based functions removed - use image_print_color() instead
272
278typedef struct {
279 double scalar_time;
280 double sse2_time;
281 double ssse3_time;
282 double avx2_time;
283 double neon_time;
284 double sve_time;
286 const char *best_method;
288
298simd_benchmark_t benchmark_simd_conversion(int width, int height, int iterations);
299
310simd_benchmark_t benchmark_simd_color_conversion(int width, int height, int iterations, bool background_mode);
311
324simd_benchmark_t benchmark_simd_conversion_with_source(int width, int height, int iterations, bool background_mode,
325 const image_t *source_image, bool use_256color);
326
340 bool background_mode, const image_t *source_image,
341 bool use_256color);
342
351
362char *image_print_color_simd(image_t *image, bool use_background_mode, bool use_256color, const char *ascii_chars);
363
372void set_color_quality_mode(bool high_quality);
373
381
389void prewarm_sgr256_fg_cache(void);
390
398void prewarm_sgr256_cache(void);
399
408char *get_sgr256_fg_string(uint8_t fg, uint8_t *len_out);
409
418char *get_sgr256_bg_string(uint8_t bg, uint8_t *len_out);
419
429char *get_sgr256_fg_bg_string(uint8_t fg, uint8_t bg, uint8_t *len_out);
430
452size_t write_row_rep_from_arrays_enhanced(const uint8_t *fg_r, const uint8_t *fg_g, const uint8_t *fg_b,
453 const uint8_t *bg_r, const uint8_t *bg_g, const uint8_t *bg_b,
454 const uint8_t *fg_idx, const uint8_t *bg_idx, const char *ascii_chars,
455 int width, char *dst, size_t cap, bool is_truecolor);
456
462#define RAMP64_SIZE 64
463#define CACHE_FREQUENCY_DECAY_TIME 10000 // 10 seconds in milliseconds
464#define CACHE_RECENCY_SCALE 100
465#define CACHE_MAX_LIFETIME 3600 // 1 hour maximum cache lifetime in seconds
466
473
492
498void build_utf8_luminance_cache(const char *ascii_chars, utf8_char_t cache[256]);
499
505void build_utf8_ramp64_cache(const char *ascii_chars, utf8_char_t cache64[64], uint8_t char_index_ramp[256]);
506
512utf8_palette_cache_t *get_utf8_palette_cache(const char *ascii_chars);
513
521void simd_caches_destroy_all(void);
522
523// Include architecture-specific implementations
524#if SIMD_SUPPORT_SSE2
526#endif
527#if SIMD_SUPPORT_SSSE3
529#endif
530#if SIMD_SUPPORT_AVX2
532#endif
533#if SIMD_SUPPORT_SVE
535#endif
536#if SIMD_SUPPORT_NEON
538#endif
⚛️ Atomic operations abstraction layer with debug tracking
AVX2-optimized ASCII rendering functions.
unsigned int uint32_t
Definition common.h:58
unsigned long long uint64_t
Definition common.h:59
unsigned char uint8_t
Definition common.h:56
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)
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:150
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:129
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.
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)
bool get_256_color_fast_path(void)
Get 256-color fast path setting.
char g_default_luminance_palette[256]
Default luminance palette (256 characters)
void prewarm_sgr256_fg_cache(void)
Prewarm 256-color foreground cache for benchmarks.
Definition sgr.c:125
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:142
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:134
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:413
NEON-optimized ASCII rendering functions.
SSE2-optimized ASCII rendering functions.
SSSE3-optimized ASCII rendering functions.
ClangTool/LibTooling compatibility shim for stdbool.h.
ImageRGB structure for NEON renderers.
Run-length encoding state for ANSI color optimization.
Dynamic string buffer structure.
Atomic value wrapper for integral/boolean types.
Definition atomic.h:76
Decimal conversion cache structure (1-3 digits)
Global decimal cache for digit conversion.
Image structure.
RGB pixel structure.
SIMD benchmark results structure.
SVE-optimized ASCII rendering functions.