ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
foreground.c File Reference

Go to the source code of this file.

Functions

char * image_print (const image_t *p, const char *palette)
 Print image as ASCII art (monochrome)
 
void quantize_color (int *r, int *g, int *b, int levels)
 Quantize color to specified number of levels.
 
char * image_print_color (const image_t *p, const char *palette)
 Print image as ASCII art with color.
 
char * image_print_color_utf8 (const image_t *p, const char *palette)
 
char * rgb_to_ansi_fg (int r, int g, int b)
 Convert RGB to ANSI foreground color code.
 
char * rgb_to_ansi_bg (int r, int g, int b)
 Convert RGB to ANSI background color code.
 
void rgb_to_ansi_8bit (int r, int g, int b, int *fg_code, int *bg_code)
 Convert RGB to 8-bit ANSI color codes.
 
char * image_print_256color (const image_t *image, const char *palette)
 Print image using 256-color ANSI mode.
 
char * image_print_16color (const image_t *image, const char *palette)
 Print image using 16-color ANSI mode.
 
char * image_print_16color_dithered (const image_t *image, const char *palette)
 Print image using 16-color ANSI mode with dithering.
 
char * image_print_16color_dithered_with_background (const image_t *image, bool use_background, const char *palette)
 Print image using 16-color ANSI mode with dithering and background colors.
 

Function Documentation

◆ image_print_color_utf8()

char * image_print_color_utf8 ( const image_t *  p,
const char *  palette 
)

Definition at line 310 of file foreground.c.

310 {
311 if (!p || !palette || !p->pixels) {
312 return NULL;
313 }
314
315 utf8_palette_cache_t *utf8_cache = get_utf8_palette_cache(palette);
316 if (!utf8_cache) {
317 return NULL;
318 }
319
320 const int h = p->h;
321 const int w = p->w;
322 const size_t h_sz = (size_t)h;
323 const size_t w_sz = (size_t)w;
324
325 if (h_sz > 0 && w_sz > SIZE_MAX / h_sz) {
326 return NULL;
327 }
328
329 const size_t bytes_per_pixel = 30; // fg sequence (~19) + char (up to 4) + margin
330 const size_t total_pixels = h_sz * w_sz;
331 const size_t lines_size = total_pixels * bytes_per_pixel + h_sz + 16;
332
333 char *lines;
334 lines = SAFE_MALLOC(lines_size, char *);
335
336 char *ptr = lines;
337 const rgb_pixel_t *pix = p->pixels;
338
339 for (int y = 0; y < h; y++) {
340 const int row_offset = y * w;
341
342 for (int x = 0; x < w; x++) {
343 const rgb_pixel_t pixel = pix[row_offset + x];
344
345 ptr += SAFE_SNPRINTF(ptr, 20, "\033[38;2;%d;%d;%dm", pixel.r, pixel.g, pixel.b);
346
347 int luminance = (77 * pixel.r + 150 * pixel.g + 29 * pixel.b + 128) >> 8;
348 uint8_t safe_luminance = clamp_rgb(luminance);
349 const utf8_char_t *char_info = &utf8_cache->cache[safe_luminance];
350 for (int i = 0; i < char_info->byte_len; i++) {
351 *ptr++ = char_info->utf8_bytes[i];
352 }
353 }
354
355 *ptr++ = '\033';
356 *ptr++ = '[';
357 *ptr++ = '0';
358 *ptr++ = 'm';
359 if (y < h - 1) {
360 *ptr++ = '\n';
361 }
362 }
363
364 *ptr = '\0';
365 return lines;
366}
#define SAFE_MALLOC(size, cast)
Definition common.h:264
#define SAFE_SNPRINTF(buffer, buffer_size,...)
Definition common.h:492
unsigned char uint8_t
Definition common.h:56
uint8_t utf8_bytes[4]
utf8_palette_cache_t * get_utf8_palette_cache(const char *ascii_chars)
Get UTF-8 palette cache for a character set.
utf8_char_t cache[256]
int w
Image width in pixels (must be > 0)
int h
Image height in pixels (must be > 0)
rgb_pixel_t * pixels
Pixel data array (width * height RGB pixels, row-major order)
RGB pixel structure.
uint8_t b
Blue color component (0-255)
uint8_t g
Green color component (0-255)
uint8_t r
Red color component (0-255)

References rgb_pixel_t::b, utf8_char_t::byte_len, utf8_palette_cache::cache, rgb_pixel_t::g, get_utf8_palette_cache(), image_t::h, image_t::pixels, rgb_pixel_t::r, SAFE_MALLOC, SAFE_SNPRINTF, utf8_char_t::utf8_bytes, and image_t::w.

Referenced by ascii_convert(), and image_print_with_capabilities().