ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
render/luminance.c
Go to the documentation of this file.
1#include <ascii-chat/otel.h>
2#include <ascii-chat/common.h>
8#include <string.h>
9#include <limits.h>
10
11#include "luminance.h"
12
13char *ascii_luminance_render(const image_t *image, const char *palette, ascii_color_t mode, bool background, bool dither, const simd_cells_t *ops) {
14 if (!ops || mode < ASCII_COLOR_NONE || mode > ASCII_COLOR_TRUE) {
15 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid rendering backend or color mode");
16 return NULL;
17 }
18 if (!dither) {
19#if SIMD_SUPPORT_SSE2
20 if (ops == &simd_cells_sse2)
21 return render_ascii_cells_sse2(image, palette, mode, background);
22#endif
23#if SIMD_SUPPORT_SSSE3
24 if (ops == &simd_cells_ssse3)
25 return render_ascii_cells_ssse3(image, palette, mode, background);
26#endif
27#if SIMD_SUPPORT_AVX2
28 if (ops == &simd_cells_avx2)
29 return render_ascii_cells_avx2(image, palette, mode, background);
30#endif
31#if SIMD_SUPPORT_NEON
32 if (ops == &simd_cells_neon)
33 return render_ascii_cells_neon(image, palette, mode, background);
34#endif
35#if SIMD_SUPPORT_SVE
36 if (ops == &simd_cells_sve)
37 return render_ascii_cells_sve(image, palette, mode, background);
38#endif
39 }
40 ascii_frame_t frame;
41 if (ascii_frame_init(&frame, image, palette) != ASCIICHAT_OK)
42 return NULL;
43 rgb_error_t *errors = NULL;
44 if (mode == ASCII_COLOR_16 && dither)
45 errors = SAFE_CALLOC((size_t)image->w * (size_t)image->h, sizeof(rgb_error_t), rgb_error_t *);
46 frame.dithered = errors != NULL;
47 for (int y = 0; y < image->h; ++y) {
48 const uint8_t *source = (const uint8_t *)(image->pixels + (size_t)y * frame.width);
49 ops->luminance(source, frame.luma, frame.width);
50 ops->pack(source, frame.packed, frame.width);
51 ops->lookup(frame.luma, frame.table, frame.glyph, frame.width);
52 if (mode == ASCII_COLOR_16 || mode == ASCII_COLOR_256) {
53 if (errors) {
54 // Floyd-Steinberg error propagation is ordered; the other stages remain SIMD.
55 for (int x = 0; x < image->w; ++x) {
56 uint32_t pixel = frame.packed[x];
57 frame.index[x] = rgb_to_16color_dithered((int)(pixel >> 16), (int)((pixel >> 8) & 255), (int)(pixel & 255), x, y, image->w, image->h, errors);
58 }
59 } else
60 ops->quantize(frame.packed, frame.index, frame.width, mode == ASCII_COLOR_16 ? 16 : 256);
61 }
62 ascii_frame_emit_row(&frame, mode, background, y + 1 < image->h, ops);
63 }
64 SAFE_FREE(errors);
65 return ascii_frame_finish(&frame);
66}
67
68char *ascii_luminance_halfblocks(const uint8_t *rgb, int width, int height, int stride, ascii_color_t mode, const simd_cells_t *ops) {
69#if SIMD_SUPPORT_SSE2
70 if (ops == &simd_cells_sse2)
71 return render_halfblocks_sse2(rgb, width, height, stride, mode);
72#endif
73#if SIMD_SUPPORT_SSSE3
74 if (ops == &simd_cells_ssse3)
75 return render_halfblocks_ssse3(rgb, width, height, stride, mode);
76#endif
77#if SIMD_SUPPORT_AVX2
78 if (ops == &simd_cells_avx2)
79 return render_halfblocks_avx2(rgb, width, height, stride, mode);
80#endif
81#if SIMD_SUPPORT_NEON
82 if (ops == &simd_cells_neon)
83 return render_halfblocks_neon(rgb, width, height, stride, mode);
84#endif
85#if SIMD_SUPPORT_SVE
86 if (ops == &simd_cells_sve)
87 return render_halfblocks_sve(rgb, width, height, stride, mode);
88#endif
89
90 if (width <= 0 || height <= 0)
91 return platform_strdup("");
92 if (!ops) {
93 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid half-block backend");
94 return NULL;
95 }
96 ascii_frame_t frame;
97 if (ascii_half_frame_init(&frame, rgb, width, height, &stride, mode) != ASCIICHAT_OK)
98 return NULL;
99 for (size_t y = 0; y < (size_t)height; y += 2) {
100 const uint8_t *top = rgb + y * (size_t)stride;
101 const uint8_t *bottom = y + 1 < (size_t)height ? top + stride : top;
102 ops->pack(top, frame.packed, frame.width);
103 ops->pack(bottom, frame.index, frame.width);
104 if (mode == ASCII_COLOR_TRUE) {
105 memcpy(frame.fg, frame.packed, frame.width * sizeof(uint32_t));
106 memcpy(frame.bg, frame.index, frame.width * sizeof(uint32_t));
107 } else if (mode != ASCII_COLOR_NONE) {
108 ops->quantize(frame.packed, frame.fg, frame.width, mode == ASCII_COLOR_16 ? 16 : 256);
109 ops->quantize(frame.index, frame.bg, frame.width, mode == ASCII_COLOR_16 ? 16 : 256);
110 }
111 ops->halfblock(frame.packed, frame.index, frame.glyph, frame.fg, frame.bg, frame.width, (unsigned)mode, frame.table);
112 ascii_frame_emit_halfrow(&frame, mode, y + 2 < (size_t)height, ops);
113 }
114 return ascii_frame_finish(&frame);
115}
ANSI escape sequence utilities and fast color code generation.
#define SET_ERRNO(code,...)
const simd_cells_t simd_cells_sve
const simd_cells_t simd_cells_sse2
const simd_cells_t simd_cells_avx2
const simd_cells_t simd_cells_ssse3
const simd_cells_t simd_cells_neon
audio_visualization_source_t source
Definition fft.c:25
unsigned int height
Definition fft.c:24
unsigned int width
Definition fft.c:24
uint8_t rgb_to_16color_dithered(int r, int g, int b, int x, int y, int width, int height, rgb_error_t *error_buffer)
Definition ansi.c:427
unsigned int uint32_t
#define SAFE_FREE(ptr)
#define SAFE_CALLOC(count, size, cast)
unsigned char uint8_t
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
⚙️ Common definitions, error codes, macros, and types shared throughout the application
SIMD-optimized ASCII conversion interface.
char * render_halfblocks_sse2(const uint8_t *, int, int, int, ascii_color_t)
char * render_halfblocks_ssse3(const uint8_t *, int, int, int, ascii_color_t)
char * render_halfblocks_avx2(const uint8_t *, int, int, int, ascii_color_t)
char * render_ascii_cells_sve(const image_t *, const char *, ascii_color_t, bool)
char * render_ascii_cells_sse2(const image_t *, const char *, ascii_color_t, bool)
char * render_halfblocks_sve(const uint8_t *, int, int, int, ascii_color_t)
char * render_halfblocks_neon(const uint8_t *, int, int, int, ascii_color_t)
char * render_ascii_cells_ssse3(const image_t *, const char *, ascii_color_t, bool)
char * render_ascii_cells_neon(const image_t *, const char *, ascii_color_t, bool)
char * render_ascii_cells_avx2(const image_t *, const char *, ascii_color_t, bool)
#define ascii_frame_emit_halfrow
#define ascii_frame_init
#define ascii_half_frame_init
#define ascii_frame_emit_row
#define ascii_frame_finish
✅ Safe Integer Arithmetic and Overflow Detection
char * ascii_luminance_render(const image_t *image, const char *palette, ascii_color_t mode, bool background, bool dither, const simd_cells_t *ops)
char * ascii_luminance_halfblocks(const uint8_t *rgb, int width, int height, int stride, ascii_color_t mode, const simd_cells_t *ops)
char * platform_strdup(const char *s)
🔤 String Manipulation and Shell Escaping Utilities
When code needs to work with ascii frame, it keeps output, output_target, width, source_height here i...
SIMD-friendly storage for one RGB pixel. r, g, and b hold the color channels and padding keeps the la...
int h
Image height in pixels (must be > 0).
rgb_pixel_t * pixels
Pixel data array (width * height RGB pixels, row-major order).
int w
Image width in pixels (must be > 0).
The useful part of simd cells is in luminance: together they describe what is pending,...
Definition cells.h:14
void(* pack)(const uint8_t *rgb, uint32_t *out, size_t count)
Definition cells.h:16
void(* halfblock)(const uint32_t *top, const uint32_t *bottom, uint32_t *glyph, uint32_t *fg, uint32_t *bg, size_t count, unsigned mode, const uint32_t keys[5])
Definition cells.h:19
void(* quantize)(const uint32_t *rgb, uint32_t *out, size_t count, unsigned colors)
Definition cells.h:18
simd_luminance_fn luminance
Definition cells.h:15
void(* lookup)(const uint8_t *luma, const uint32_t table[64], uint32_t *out, size_t count)
Definition cells.h:17