ascii-chat 0.8.38
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
output_buffer.c File Reference

📝 Output buffer helpers for efficient string building in ASCII rendering pipeline More...

Go to the source code of this file.

Functions

void ob_reserve (outbuf_t *ob, size_t need)
 
void ob_putc (outbuf_t *ob, char c)
 
void ob_write (outbuf_t *ob, const char *s, size_t n)
 
void ob_term (outbuf_t *ob)
 
void ob_u8 (outbuf_t *ob, uint8_t v)
 
void ob_u32 (outbuf_t *ob, uint32_t v)
 
void emit_set_truecolor_fg (outbuf_t *ob, uint8_t r, uint8_t g, uint8_t b)
 
void emit_set_truecolor_bg (outbuf_t *ob, uint8_t r, uint8_t g, uint8_t b)
 
void emit_reset (outbuf_t *ob)
 
bool rep_is_profitable (uint32_t runlen)
 
void emit_rep (outbuf_t *ob, uint32_t extra)
 
void emit_set_256_color_fg (outbuf_t *ob, uint8_t color_idx)
 
void emit_set_256_color_bg (outbuf_t *ob, uint8_t color_idx)
 
void emit_set_fg (outbuf_t *ob, uint8_t r, uint8_t g, uint8_t b)
 
void emit_set_bg (outbuf_t *ob, uint8_t r, uint8_t g, uint8_t b)
 

Detailed Description

📝 Output buffer helpers for efficient string building in ASCII rendering pipeline

Definition in file output_buffer.c.

Function Documentation

◆ emit_rep()

void emit_rep ( outbuf_t *  ob,
uint32_t  extra 
)

Definition at line 150 of file output_buffer.c.

150 {
151 if (!ob)
152 return;
153 // ESC [ extra b
154 ob_putc(ob, 0x1b);
155 ob_putc(ob, '[');
156 ob_u32(ob, extra);
157 ob_putc(ob, 'b');
158}
void ob_u32(outbuf_t *ob, uint32_t v)
void ob_putc(outbuf_t *ob, char c)

References ob_putc(), and ob_u32().

Referenced by ansi_compress_rle(), convert_pixels_scalar_with_newlines(), and image_print().

◆ emit_reset()

void emit_reset ( outbuf_t *  ob)

Definition at line 132 of file output_buffer.c.

132 {
133 if (!ob)
134 return;
135 ob_putc(ob, 0x1b);
136 ob_putc(ob, '[');
137 ob_putc(ob, '0');
138 ob_putc(ob, 'm');
139}

References ob_putc().

◆ emit_set_256_color_bg()

void emit_set_256_color_bg ( outbuf_t *  ob,
uint8_t  color_idx 
)

Definition at line 171 of file output_buffer.c.

171 {
172 if (!ob)
173 return;
174 // Use pre-cached color sequence instead of building on-demand
175 uint8_t seq_len;
176 const char *seq = get_sgr256_bg_string(color_idx, &seq_len);
177 ob_write(ob, seq, seq_len);
178}
char * get_sgr256_bg_string(uint8_t bg, uint8_t *len_out)
void ob_write(outbuf_t *ob, const char *s, size_t n)

References get_sgr256_bg_string(), and ob_write().

◆ emit_set_256_color_fg()

void emit_set_256_color_fg ( outbuf_t *  ob,
uint8_t  color_idx 
)

Definition at line 161 of file output_buffer.c.

161 {
162 if (!ob)
163 return;
164 // Use pre-cached color sequence instead of building on-demand
165 uint8_t seq_len;
166 const char *seq = get_sgr256_fg_string(color_idx, &seq_len);
167 ob_write(ob, seq, seq_len);
168}
char * get_sgr256_fg_string(uint8_t fg, uint8_t *len_out)

References get_sgr256_fg_string(), and ob_write().

◆ emit_set_bg()

void emit_set_bg ( outbuf_t *  ob,
uint8_t  r,
uint8_t  g,
uint8_t  b 
)

Definition at line 195 of file output_buffer.c.

195 {
196 if (!ob)
197 return;
198 // ESC[48;2;R;G;Bm
199 ob_putc(ob, 0x1b);
200 ob_putc(ob, '[');
201 ob_write(ob, "48;2;", 5);
202 ob_u8(ob, r);
203 ob_putc(ob, ';');
204 ob_u8(ob, g);
205 ob_putc(ob, ';');
206 ob_u8(ob, b);
207 ob_putc(ob, 'm');
208}
void ob_u8(outbuf_t *ob, uint8_t v)

References ob_putc(), ob_u8(), and ob_write().

◆ emit_set_fg()

void emit_set_fg ( outbuf_t *  ob,
uint8_t  r,
uint8_t  g,
uint8_t  b 
)

Definition at line 180 of file output_buffer.c.

180 {
181 if (!ob)
182 return;
183 // ESC[38;2;R;G;Bm
184 ob_putc(ob, 0x1b);
185 ob_putc(ob, '[');
186 ob_write(ob, "38;2;", 5);
187 ob_u8(ob, r);
188 ob_putc(ob, ';');
189 ob_u8(ob, g);
190 ob_putc(ob, ';');
191 ob_u8(ob, b);
192 ob_putc(ob, 'm');
193}

References ob_putc(), ob_u8(), and ob_write().

◆ emit_set_truecolor_bg()

void emit_set_truecolor_bg ( outbuf_t *  ob,
uint8_t  r,
uint8_t  g,
uint8_t  b 
)

Definition at line 117 of file output_buffer.c.

117 {
118 if (!ob)
119 return;
120 // ESC[48;2;R;G;Bm
121 ob_putc(ob, 0x1b);
122 ob_putc(ob, '[');
123 ob_write(ob, "48;2;", 5);
124 ob_u8(ob, r);
125 ob_putc(ob, ';');
126 ob_u8(ob, g);
127 ob_putc(ob, ';');
128 ob_u8(ob, b);
129 ob_putc(ob, 'm');
130}

References ob_putc(), ob_u8(), and ob_write().

◆ emit_set_truecolor_fg()

void emit_set_truecolor_fg ( outbuf_t *  ob,
uint8_t  r,
uint8_t  g,
uint8_t  b 
)

Definition at line 101 of file output_buffer.c.

101 {
102 if (!ob)
103 return;
104 // ESC[38;2;R;G;Bm
105 ob_putc(ob, 0x1b);
106 ob_putc(ob, '[');
107 ob_write(ob, "38;2;", 5);
108 ob_u8(ob, r);
109 ob_putc(ob, ';');
110 ob_u8(ob, g);
111 ob_putc(ob, ';');
112 ob_u8(ob, b);
113 ob_putc(ob, 'm');
114}

References ob_putc(), ob_u8(), and ob_write().

◆ ob_putc()

void ob_putc ( outbuf_t *  ob,
char  c 
)

Definition at line 43 of file output_buffer.c.

43 {
44 if (!ob)
45 return;
46 ob_reserve(ob, 1);
47 ob->buf[ob->len++] = c;
48}
void ob_reserve(outbuf_t *ob, size_t need)

References ob_reserve().

Referenced by ansi_compress_rle(), ansi_strip_escapes(), convert_pixels_scalar_with_newlines(), emit_rep(), emit_reset(), emit_set_bg(), emit_set_fg(), emit_set_truecolor_bg(), emit_set_truecolor_fg(), image_print(), ob_term(), and ob_u8().

◆ ob_reserve()

void ob_reserve ( outbuf_t *  ob,
size_t  need 
)

Definition at line 22 of file output_buffer.c.

22 {
23 if (!ob)
24 return;
25 if (ob->cap == 0) {
26 // Always allocate at least default capacity on first call
27 size_t ncap = 4096;
28 while (ncap < ob->len + need)
29 ncap = (ncap * 3) / 2;
30 // SAFE_REALLOC handles NULL ptr as malloc - this is safe
31 ob->buf = SAFE_REALLOC(ob->buf, ncap, char *);
32 ob->cap = ncap;
33 } else if (ob->len + need > ob->cap) {
34 // Expand existing buffer
35 size_t ncap = ob->cap;
36 while (ncap < ob->len + need)
37 ncap = (ncap * 3) / 2;
38 ob->buf = SAFE_REALLOC(ob->buf, ncap, char *);
39 ob->cap = ncap;
40 }
41}

Referenced by ansi_compress_rle(), ansi_expand_rle(), ansi_strip_escapes(), ob_putc(), ob_u32(), and ob_write().

◆ ob_term()

void ob_term ( outbuf_t *  ob)

Definition at line 58 of file output_buffer.c.

58 {
59 if (!ob)
60 return;
61 ob_putc(ob, '\0');
62}

References ob_putc().

Referenced by ansi_compress_rle(), ansi_expand_rle(), ansi_strip_escapes(), convert_pixels_scalar_with_newlines(), and image_print().

◆ ob_u32()

void ob_u32 ( outbuf_t *  ob,
uint32_t  v 
)

Definition at line 86 of file output_buffer.c.

86 {
87 if (!ob)
88 return;
89 char tmp[10];
90 int i = 0;
91 do {
92 tmp[i++] = '0' + (v % 10u);
93 v /= 10u;
94 } while (v);
95 ob_reserve(ob, (size_t)i);
96 while (i--)
97 ob->buf[ob->len++] = tmp[i];
98}

References ob_reserve().

Referenced by emit_rep().

◆ ob_u8()

void ob_u8 ( outbuf_t *  ob,
uint8_t  v 
)

Definition at line 65 of file output_buffer.c.

65 {
66 if (!ob)
67 return;
68 if (v >= 100) {
69 uint8_t d0 = v / 100;
70 uint8_t r = v % 100;
71 uint8_t d1 = r / 10;
72 uint8_t d2 = r % 10;
73 ob_putc(ob, '0' + d0);
74 ob_putc(ob, '0' + d1);
75 ob_putc(ob, '0' + d2);
76 } else if (v >= 10) {
77 uint8_t d1 = v / 10;
78 uint8_t d2 = v % 10;
79 ob_putc(ob, '0' + d1);
80 ob_putc(ob, '0' + d2);
81 } else {
82 ob_putc(ob, '0' + v);
83 }
84}

References ob_putc().

Referenced by emit_set_bg(), emit_set_fg(), emit_set_truecolor_bg(), and emit_set_truecolor_fg().

◆ ob_write()

void ob_write ( outbuf_t *  ob,
const char *  s,
size_t  n 
)

Definition at line 50 of file output_buffer.c.

50 {
51 if (!ob || n == 0)
52 return;
53 ob_reserve(ob, n);
54 SAFE_MEMCPY(ob->buf + ob->len, ob->cap - ob->len, s, n);
55 ob->len += n;
56}

References ob_reserve().

Referenced by ansi_compress_rle(), ansi_expand_rle(), emit_set_256_color_bg(), emit_set_256_color_fg(), emit_set_bg(), emit_set_fg(), emit_set_truecolor_bg(), emit_set_truecolor_fg(), and image_print().

◆ rep_is_profitable()

bool rep_is_profitable ( uint32_t  runlen)

Definition at line 142 of file output_buffer.c.

142 {
143 if (runlen <= 2)
144 return false;
145 uint32_t k = runlen - 1; // Extra repetitions beyond the first character
146 uint32_t rep_cost = (uint32_t)(digits_u32(k) + 3); // ESC [ digits b
147 return k > rep_cost; // Manual repetition cost vs REP cost
148}

Referenced by ansi_compress_rle(), convert_pixels_scalar_with_newlines(), and image_print().