ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
common.c File Reference

Go to the source code of this file.

Macros

#define VIDEO_ASCII_RENDER_SEQUENCE_BUFFER_SIZE   16

Functions

outbuf_t * ascii_render_set_output_target (outbuf_t *target)
void ascii_emit_prepared_row (outbuf_t *out, ascii_color_t mode, const simd_cells_t *ops, const uint32_t *glyph, const uint32_t *fg, const uint32_t *bg, size_t width)
asciichat_error_t ascii_frame_init (ascii_frame_t *frame, const image_t *image, const char *palette)
uint32_t ascii_background_contrast (ascii_color_t mode, uint32_t color, uint8_t luma)
void ascii_frame_emit_row (ascii_frame_t *frame, ascii_color_t mode, bool background, bool more_rows, const simd_cells_t *ops)
char * ascii_frame_finish (ascii_frame_t *frame)
asciichat_error_t ascii_half_frame_init (ascii_frame_t *frame, const uint8_t *rgb, int width, int height, int *stride, ascii_color_t mode)
void ascii_frame_emit_halfrow (ascii_frame_t *frame, ascii_color_t mode, bool more_rows, const simd_cells_t *ops)

Macro Definition Documentation

◆ VIDEO_ASCII_RENDER_SEQUENCE_BUFFER_SIZE

#define VIDEO_ASCII_RENDER_SEQUENCE_BUFFER_SIZE   16

Definition at line 11 of file video/ascii/render/common.c.

Function Documentation

◆ ascii_background_contrast()

uint32_t ascii_background_contrast ( ascii_color_t mode,
uint32_t color,
uint8_t luma )

Definition at line 122 of file video/ascii/render/common.c.

122 {
123 if (mode == ASCII_COLOR_16) {
124 uint8_t r, g, b;
125 get_16color_rgb((uint8_t)color, &r, &g, &b);
126 return (77 * r + 150 * g + 29 * b) / 256 < 127 ? 15u : 0;
127 }
128 return luma < 128 ? (mode == ASCII_COLOR_TRUE ? 0xffffffu : 15u) : 0;
129}
void get_16color_rgb(uint8_t color_index, uint8_t *r, uint8_t *g, uint8_t *b)
Definition ansi.c:396
unsigned char uint8_t

References ASCII_COLOR_16, ASCII_COLOR_TRUE, and get_16color_rgb().

Referenced by ascii_frame_emit_row(), and ascii_glyph_render().

◆ ascii_emit_prepared_row()

void ascii_emit_prepared_row ( outbuf_t * out,
ascii_color_t mode,
const simd_cells_t * ops,
const uint32_t * glyph,
const uint32_t * fg,
const uint32_t * bg,
size_t width )

Definition at line 60 of file video/ascii/render/common.c.

60 {
61 if (mode == ASCII_COLOR_NONE) {
62 if (ops->emit_mono) {
63 ops->emit_mono(out, glyph, width);
64 } else {
65 for (size_t x = 0; x < width;) {
66 size_t run = ops->run(glyph + x, fg + x, bg + x, width - x);
67 emit_glyph_run(out, &glyph[x], glyph_bytes(glyph[x]), (uint32_t)run);
68 x += run;
69 }
70 }
71 return;
72 }
73 uint32_t current_fg = UINT32_MAX, current_bg = UINT32_MAX;
74 for (size_t x = 0; x < width;) {
75 size_t run = ops->run(glyph + x, fg + x, bg + x, width - x);
76 if (fg[x] == UINT32_MAX && bg[x] == UINT32_MAX && (current_fg != UINT32_MAX || current_bg != UINT32_MAX)) {
78 current_fg = current_bg = UINT32_MAX;
79 }
80 if (fg[x] != current_fg) {
81 emit_color(out, mode, false, (int)fg[x]);
82 current_fg = fg[x];
83 }
84 if (bg[x] != current_bg) {
85 emit_color(out, mode, true, (int)bg[x]);
86 current_bg = bg[x];
87 }
88 emit_glyph_run(out, &glyph[x], glyph_bytes(glyph[x]), (uint32_t)run);
89 x += run;
90 }
91}
unsigned int width
Definition fft.c:24
unsigned int uint32_t
void emit_reset(outbuf_t *out)
Emit ANSI reset sequence.
void emit_glyph_run(outbuf_t *out, const void *glyph, size_t bytes, uint32_t count)
static bool size_t gotel_query_t * out
Definition queries.h:8
size_t(* run)(const uint32_t *glyph, const uint32_t *fg, const uint32_t *bg, size_t count)
Definition cells.h:20
void(* emit_mono)(outbuf_t *out, const uint32_t *glyph, size_t count)
Definition cells.h:21

References ASCII_COLOR_NONE, emit_glyph_run(), simd_cells_t::emit_mono, emit_reset(), out, simd_cells_t::run, and width.

Referenced by ascii_frame_emit_halfrow(), ascii_frame_emit_row(), ascii_glyph_render(), and ascii_render_glyph_match_mode().

◆ ascii_frame_emit_halfrow()

void ascii_frame_emit_halfrow ( ascii_frame_t * frame,
ascii_color_t mode,
bool more_rows,
const simd_cells_t * ops )

Definition at line 184 of file video/ascii/render/common.c.

184 {
185 ascii_stabilizer_halfrow(frame, mode, ops);
186 ascii_emit_prepared_row(&frame->output, mode, ops, frame->glyph, frame->fg, frame->bg, frame->width);
187 emit_row_end(&frame->output, mode != ASCII_COLOR_NONE, more_rows);
188 ++frame->row;
189}
void emit_row_end(outbuf_t *out, bool colored, bool newline)
void ascii_stabilizer_halfrow(ascii_frame_t *frame, ascii_color_t mode, const simd_cells_t *ops)
Definition stabilize.c:175
void ascii_emit_prepared_row(outbuf_t *out, ascii_color_t mode, const simd_cells_t *ops, const uint32_t *glyph, const uint32_t *fg, const uint32_t *bg, size_t width)

References ASCII_COLOR_NONE, ascii_emit_prepared_row(), ascii_stabilizer_halfrow(), ascii_frame_t::bg, emit_row_end(), ascii_frame_t::fg, ascii_frame_t::glyph, ascii_frame_t::output, ascii_frame_t::row, and ascii_frame_t::width.

◆ ascii_frame_emit_row()

void ascii_frame_emit_row ( ascii_frame_t * frame,
ascii_color_t mode,
bool background,
bool more_rows,
const simd_cells_t * ops )

Definition at line 131 of file video/ascii/render/common.c.

131 {
132 ascii_stabilizer_frame_row(frame, mode, background, ops);
133 for (size_t x = 0; x < frame->width; ++x) {
134 frame->fg[x] = frame->bg[x] = UINT32_MAX;
135 if (mode == ASCII_COLOR_NONE)
136 continue;
137 uint32_t value = mode == ASCII_COLOR_TRUE ? frame->packed[x] : frame->index[x];
138 uint32_t contrast = ascii_background_contrast(mode, value, frame->luma[x]);
139 frame->fg[x] = background ? contrast : value;
140 frame->bg[x] = background ? value : UINT32_MAX;
141 }
142 ascii_emit_prepared_row(&frame->output, mode, ops, frame->glyph, frame->fg, frame->bg, frame->width);
143 emit_row_end(&frame->output, mode != ASCII_COLOR_NONE, more_rows);
144 ++frame->row;
145}
void ascii_stabilizer_frame_row(ascii_frame_t *frame, ascii_color_t mode, bool background, const simd_cells_t *ops)
Definition stabilize.c:123
uint32_t ascii_background_contrast(ascii_color_t mode, uint32_t color, uint8_t luma)

References ascii_background_contrast(), ASCII_COLOR_NONE, ASCII_COLOR_TRUE, ascii_emit_prepared_row(), ascii_stabilizer_frame_row(), ascii_frame_t::bg, emit_row_end(), ascii_frame_t::fg, ascii_frame_t::glyph, ascii_frame_t::index, ascii_frame_t::luma, ascii_frame_t::output, ascii_frame_t::packed, ascii_frame_t::row, and ascii_frame_t::width.

Referenced by ascii_frame_t::_Alignas().

◆ ascii_frame_finish()

char * ascii_frame_finish ( ascii_frame_t * frame)

Definition at line 146 of file video/ascii/render/common.c.

146 {
147 if (frame->scratch != frame->inline_scratch)
148 SAFE_FREE(frame->scratch);
149 ob_term(&frame->output);
150 if (frame->output_target)
151 *frame->output_target = frame->output;
152 return frame->output.buf;
153}
#define SAFE_FREE(ptr)
void ob_term(outbuf_t *ob)
Append null terminator to buffer.
char * buf
Buffer pointer (allocated, owned by caller, must be freed).

References outbuf_t::buf, ob_term(), ascii_frame_t::output, ascii_frame_t::output_target, SAFE_FREE, and ascii_frame_t::scratch.

Referenced by ascii_frame_t::_Alignas().

◆ ascii_frame_init()

asciichat_error_t ascii_frame_init ( ascii_frame_t * frame,
const image_t * image,
const char * palette )

Definition at line 93 of file video/ascii/render/common.c.

93 {
94 ASCII_OTEL_SCOPE(ASCII_FRAME_INIT);
96 memset(frame, 0, sizeof(*frame));
97 if (!image || !image->pixels || image->w <= 0 || image->h <= 0 || !palette || !palette[0])
98 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid image or palette");
99 size_t bytes, pixels, capacity, rows;
100 if (checked_size_mul((size_t)image->w, (size_t)image->h, &pixels) != ASCIICHAT_OK || checked_size_mul(pixels, 48u, &capacity) != ASCIICHAT_OK ||
101 checked_size_mul((size_t)image->h, 5u, &rows) != ASCIICHAT_OK || checked_size_add(capacity, rows, &capacity) != ASCIICHAT_OK || checked_size_add(capacity, 1u, &capacity) != ASCIICHAT_OK ||
102 checked_size_mul((size_t)image->w, 6 * sizeof(uint32_t), &bytes) != ASCIICHAT_OK)
103 return ERROR_INVALID_PARAM;
105 if (!cache)
106 return ERROR_INVALID_PARAM;
107 prepare_output(frame, capacity, target);
108 frame->width = (size_t)image->w;
111 for (size_t i = 0; i < 64; ++i)
112 memcpy(frame->table + i, cache->cache64[i].utf8_bytes, (size_t)cache->cache64[i].byte_len);
113 frame->scratch = bytes <= sizeof(frame->inline_scratch) ? frame->inline_scratch : SAFE_MALLOC(bytes, uint32_t *);
114 frame->packed = frame->scratch;
115 frame->index = frame->packed + frame->width;
116 frame->glyph = frame->index + frame->width;
117 frame->fg = frame->glyph + frame->width;
118 frame->bg = frame->fg + frame->width;
119 frame->luma = (uint8_t *)(frame->bg + frame->width);
120 return ASCIICHAT_OK;
121}
#define SET_ERRNO(code,...)
char cells[(512 *256 *2)]
Definition fft.c:22
unsigned int height
Definition fft.c:24
#define SAFE_MALLOC(size, cast)
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
utf8_palette_cache_t * get_utf8_palette_cache(const char *ascii_chars)
Get UTF-8 palette cache for a character set.
struct utf8_palette_cache utf8_palette_cache_t
#define ascii_render_set_output_target
#define ASCII_OTEL_SCOPE(id)
Definition otel.h:77
ascii_stabilizer_t * ascii_stabilizer_target(void)
Definition stabilize.c:9
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).
Dynamic output buffer (auto-expanding).

References ASCII_OTEL_SCOPE, ascii_render_set_output_target, ascii_stabilizer_target(), ASCIICHAT_OK, ascii_frame_t::bg, utf8_char_t::byte_len, utf8_palette_cache::cache64, cells, ERROR_INVALID_PARAM, ascii_frame_t::fg, get_utf8_palette_cache(), ascii_frame_t::glyph, image::h, height, ascii_frame_t::index, ascii_frame_t::luma, ascii_frame_t::packed, image::pixels, SAFE_MALLOC, ascii_frame_t::scratch, SET_ERRNO, ascii_frame_t::stabilizer, ascii_frame_t::table, utf8_char_t::utf8_bytes, image::w, ascii_frame_t::width, and width.

Referenced by ascii_frame_t::_Alignas().

◆ ascii_half_frame_init()

asciichat_error_t ascii_half_frame_init ( ascii_frame_t * frame,
const uint8_t * rgb,
int width,
int height,
int * stride,
ascii_color_t mode )

Definition at line 155 of file video/ascii/render/common.c.

155 {
156 ASCII_OTEL_SCOPE(ASCII_HALF_FRAME_INIT);
158 memset(frame, 0, sizeof(*frame));
159 size_t row_bytes, extent, bytes, capacity;
160 if (!rgb || width <= 0 || height <= 0 || mode < ASCII_COLOR_NONE || mode > ASCII_COLOR_TRUE || checked_size_mul((size_t)width, 3, &row_bytes) != ASCIICHAT_OK || row_bytes > INT_MAX)
161 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid half-block input");
162 if (*stride <= 0)
163 *stride = (int)row_bytes;
164 if ((size_t)*stride < row_bytes || checked_size_mul((size_t)*stride, (size_t)height, &extent) != ASCIICHAT_OK || checked_size_mul((size_t)width, 6 * sizeof(uint32_t), &bytes) != ASCIICHAT_OK ||
165 checked_size_mul((size_t)width, ((size_t)height + 1) / 2, &capacity) != ASCIICHAT_OK || checked_size_mul(capacity, 48u, &capacity) != ASCIICHAT_OK ||
166 checked_size_add(capacity, 1u, &capacity) != ASCIICHAT_OK)
167 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid half-block stride or size");
168 prepare_output(frame, capacity, target);
169 frame->width = (size_t)width;
170 frame->source_height = (size_t)height;
173 frame->scratch = bytes <= sizeof(frame->inline_scratch) ? frame->inline_scratch : SAFE_MALLOC(bytes, uint32_t *);
174 frame->packed = frame->scratch;
175 frame->index = frame->packed + width;
176 frame->glyph = frame->index + width;
177 frame->fg = frame->glyph + width;
178 frame->bg = frame->fg + width;
179 const char blocks[5][4] = {"\xe2\x96\x91", "\xe2\x96\x92", "\xe2\x96\x93", "\xe2\x96\x88", "\xe2\x96\x80"};
180 for (size_t i = 0; i < 5; ++i)
181 memcpy(frame->table + i, blocks[i], sizeof(blocks[i]) - 1);
182 return ASCIICHAT_OK;
183}

References ASCII_COLOR_TRUE, ASCII_OTEL_SCOPE, ascii_render_set_output_target, ascii_stabilizer_target(), ASCIICHAT_OK, ascii_frame_t::bg, cells, ERROR_INVALID_PARAM, ascii_frame_t::fg, ascii_frame_t::glyph, height, ascii_frame_t::index, ascii_frame_t::packed, SAFE_MALLOC, ascii_frame_t::scratch, SET_ERRNO, ascii_frame_t::source_height, ascii_frame_t::stabilizer, ascii_frame_t::table, ascii_frame_t::width, and width.

◆ ascii_render_set_output_target()

outbuf_t * ascii_render_set_output_target ( outbuf_t * target)

Internal one-shot output target, consumed by the next frame initializer. Returns the prior target so capability conversion can restore its scope.

Definition at line 15 of file video/ascii/render/common.c.

15 {
16 outbuf_t *previous = g_output_target;
17 g_output_target = target;
18 return previous;
19}