Fix allocates the max-sized buffer once and reuses it, no per-frame alloc/free

This commit is contained in:
Harshit-Dhanwalkar 2026-06-18 13:33:21 +05:30
parent e4051f2a71
commit dc843f3e59
2 changed files with 5 additions and 13 deletions

View file

@ -388,8 +388,11 @@ static int emit_glyph(char *out, size_t out_size, int out_idx,
int grayscale_to_ascii(const uint8_t *gray, const uint8_t *rgb, int src_w,
int src_h, int dst_w, int dst_h, char *out,
size_t out_size, const ascii_opts_t *opts) {
render_mode_t render_mode = opts ? opts->render_mode : RENDER_BRAILLE;
int safe_dst_w = dst_w - (dst_w % 2);
int safe_dst_h = dst_h - (dst_h % 4);
int safe_dst_h = (render_mode == RENDER_HALF_BLOCK) ? dst_h - (dst_h % 2)
: dst_h - (dst_h % 4);
int brightness = opts ? opts->brightness : 0;
int contrast = opts ? opts->contrast : 100;
@ -398,7 +401,6 @@ int grayscale_to_ascii(const uint8_t *gray, const uint8_t *rgb, int src_w,
edge_mode_t edge_mode = opts ? opts->edges : EDGE_OFF;
int do_dither = opts ? opts->dither : 0;
int thresh_limit = opts ? opts->threshold_val : 35;
render_mode_t render_mode = opts ? opts->render_mode : RENDER_BRAILLE;
const char *ramp = (opts && opts->charset && opts->charset[0])
? opts->charset
: ASCII_CHARS_DEFAULT;

View file

@ -238,7 +238,7 @@ int main(int argc, char *argv[]) {
.dither = 0,
.threshold_val = 35,
.charset = NULL,
.render_mode = RENDER_BRAILLE,
.render_mode = RENDER_DOTS, // RENDER_BRAILLE,
.depth_pop = 0,
.depth_invert = 0,
};
@ -579,14 +579,6 @@ int main(int argc, char *argv[]) {
break;
}
size_t out_size = ascii_out_size_for_mode(subpixel_w, subpixel_h,
opts.color, opts.render_mode);
char *out_buf = malloc(out_size);
if (!out_buf) {
perror("Failed to allocate text output buffer");
break;
}
// Process frame mapping using dynamically calculated bounds
int len = grayscale_to_ascii(gray, rgb, cam.width, cam.height, subpixel_w,
subpixel_h, out_buf, out_size, &opts);
@ -600,8 +592,6 @@ int main(int argc, char *argv[]) {
selected, opts.color, &opts, &charsets);
}
free(out_buf);
if (webcam_requeue_buffer(&cam) < 0) {
perror("requeue_buffer");
break;