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

🖨️ Image processing: format detection, decoding, scaling, and pixel format conversion More...

Go to the source code of this file.

Functions

image_timage_new (size_t width, size_t height)
 Create a new image with standard allocation.
 
void image_destroy (image_t *p)
 Destroy an image allocated with image_new()
 
image_timage_new_from_pool (size_t width, size_t height)
 Create a new image from buffer pool.
 
void image_destroy_to_pool (image_t *image)
 Destroy an image allocated from buffer pool.
 
void image_clear (image_t *p)
 Clear image (set all pixels to black)
 
rgb_pixel_t * image_pixel (image_t *p, const int x, const int y)
 
void image_resize (const image_t *s, image_t *d)
 Resize image using nearest-neighbor interpolation.
 
void image_resize_interpolation (const image_t *source, image_t *dest)
 Resize image using bilinear interpolation.
 
void precalc_rgb_palettes (const float red, const float green, const float blue)
 Precalculate RGB palettes with color adjustment.
 
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 * 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_with_capabilities (const image_t *image, const terminal_capabilities_t *caps, const char *palette, const char luminance_palette[256] __attribute__((unused)))
 
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.
 

Detailed Description

🖨️ Image processing: format detection, decoding, scaling, and pixel format conversion

Definition in file video/image.c.

Function Documentation

◆ image_pixel()

rgb_pixel_t * image_pixel ( image_t p,
const int  x,
const int  y 
)
inline

Definition at line 224 of file video/image.c.

224 {
225 // Add bounds checking to prevent buffer overflow on invalid coordinates
226 if (!p || !p->pixels || x < 0 || x >= p->w || y < 0 || y >= p->h) {
227 return NULL;
228 }
229 return &p->pixels[x + y * p->w];
230}
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)

References image_t::h, image_t::pixels, and image_t::w.

◆ image_print_with_capabilities()

char * image_print_with_capabilities ( const image_t image,
const terminal_capabilities_t caps,
const char *  palette,
const char luminance_palette   __attribute__((unused))[256] 
)

Definition at line 655 of file video/image.c.

656 {
657
658 if (!image || !image->pixels || !caps || !palette) {
659 SET_ERRNO(ERROR_INVALID_PARAM, "image=%p or image->pixels=%p or caps=%p or palette=%p is NULL", image,
660 image->pixels, caps, palette);
661 return NULL;
662 }
663
664 // Handle half-block mode first (requires NEON)
665 if (caps->render_mode == RENDER_MODE_HALF_BLOCK) {
666#if SIMD_SUPPORT_NEON
667 // Use NEON half-block renderer
668 const uint8_t *rgb_data = (const uint8_t *)image->pixels;
669 return rgb_to_truecolor_halfblocks_neon(rgb_data, image->w, image->h, 0);
670#else
671 SET_ERRNO(ERROR_INVALID_STATE, "Half-block mode requires NEON support (ARM architecture)");
672 return NULL;
673#endif
674 }
675
676 // Standard color modes
677 bool use_background_mode = (caps->render_mode == RENDER_MODE_BACKGROUND);
678
679 char *result = NULL;
680
681 // Choose the appropriate printing method based on terminal capabilities
682 switch (caps->color_level) {
684 // Use existing truecolor printing function with client's palette
685#ifdef SIMD_SUPPORT
686 result = image_print_color_simd((image_t *)image, use_background_mode, false, palette);
687#else
688 result = image_print_color(image, palette);
689#endif
690 break;
691
692 case TERM_COLOR_256:
693#ifdef SIMD_SUPPORT
694 result = image_print_color_simd((image_t *)image, use_background_mode, true, palette);
695#else
696 // Use 256-color conversion
697 result = image_print_256color(image, palette);
698#endif
699 break;
700
701 case TERM_COLOR_16:
702 // Use 16-color conversion with Floyd-Steinberg dithering for better quality
703 result = image_print_16color_dithered_with_background(image, use_background_mode, palette);
704 break;
705
706 case TERM_COLOR_NONE:
707 default:
708 // Use grayscale/monochrome conversion with client's custom palette
709#ifdef SIMD_SUPPORT
710 result = image_print_simd((image_t *)image, palette);
711#else
712 result = image_print(image, palette);
713#endif
714 break;
715 }
716
717 // Note: Background mode is now handled via capabilities rather than global state
718 return result;
719}
unsigned char uint8_t
Definition common.h:56
#define SET_ERRNO(code, context_msg,...)
Set error code with custom context message and log it.
@ ERROR_INVALID_STATE
@ ERROR_INVALID_PARAM
@ RENDER_MODE_BACKGROUND
Background colors (block colors)
Definition terminal.h:471
@ RENDER_MODE_HALF_BLOCK
Unicode half-block characters (mixed foreground/background)
Definition terminal.h:473
@ TERM_COLOR_NONE
No color support (monochrome terminal)
Definition terminal.h:428
@ TERM_COLOR_16
16-color support (standard ANSI colors)
Definition terminal.h:430
@ TERM_COLOR_256
256-color support (extended ANSI palette)
Definition terminal.h:432
@ TERM_COLOR_TRUECOLOR
24-bit truecolor support (RGB colors)
Definition terminal.h:434
char * image_print(const image_t *p, const char *palette)
Print image as ASCII art (monochrome)
char * image_print_color(const image_t *p, const char *palette)
Print image as ASCII art with color.
char * image_print_simd(image_t *image, const char *ascii_chars)
Print image as ASCII using SIMD (monochrome)
Definition ascii_simd.c:252
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.
char * image_print_256color(const image_t *image, const char *palette)
Print image using 256-color ANSI mode.
char * image_print_color_simd(image_t *image, bool use_background_mode, bool use_256color, const char *ascii_chars)
Print image as ASCII with color using SIMD.
Image structure.
terminal_color_mode_t color_level
Detected color support level (terminal_color_mode_t)
Definition terminal.h:487
render_mode_t render_mode
Preferred rendering mode (render_mode_t)
Definition terminal.h:497

References terminal_capabilities_t::color_level, ERROR_INVALID_PARAM, ERROR_INVALID_STATE, image_t::h, image_print(), image_print_16color_dithered_with_background(), image_print_256color(), image_print_color(), image_print_color_simd(), image_print_simd(), image_t::pixels, terminal_capabilities_t::render_mode, RENDER_MODE_BACKGROUND, RENDER_MODE_HALF_BLOCK, SET_ERRNO, TERM_COLOR_16, TERM_COLOR_256, TERM_COLOR_NONE, TERM_COLOR_TRUECOLOR, and image_t::w.

Referenced by ascii_convert_with_capabilities().