ascii-chat 0.8.38
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
ascii_wasm.c
Go to the documentation of this file.
1// lib/video/ascii_wasm.c
2// WASM-specific ASCII rendering wrapper for browser
3#include <emscripten.h>
4#include <stdint.h>
5#include <stdlib.h>
6#include <string.h>
7
8// Include the actual headers
9#include <ascii-chat/video/image.h>
10#include <ascii-chat/video/palette.h>
11
12// External function from ascii.c
13extern char *ascii_convert(image_t *original, const ssize_t width, const ssize_t height, const bool color,
14 const bool _aspect_ratio, const bool stretch, const char *palette_chars,
15 const char luminance_palette[256]);
16
17// External function from palette.c
18extern int build_client_luminance_palette(const char *palette_chars, size_t palette_len, char luminance_mapping[256]);
19
20// Default palette (from palette.h)
21static const char *WASM_PALETTE = " ...',;:clodxkO0KXNWM";
22static const size_t WASM_PALETTE_LEN = 23;
23
24// Global luminance palette (initialized once)
25static char g_luminance_palette[256];
26static bool g_palette_initialized = false;
27
28// Initialize palette on first use
29static void ensure_palette_initialized(void) {
30 if (!g_palette_initialized) {
31 build_client_luminance_palette(WASM_PALETTE, WASM_PALETTE_LEN, g_luminance_palette);
32 g_palette_initialized = true;
33 }
34}
35
36// Exported function: Convert RGBA frame to ASCII
37// Takes RGBA data (as from Canvas getImageData), converts to RGB, renders to ASCII
38EMSCRIPTEN_KEEPALIVE
39char *convert_frame_to_ascii(uint8_t *rgba_data, // RGBA format from canvas (4 bytes per pixel)
40 int width, int height, int ascii_width, int ascii_height) {
41 ensure_palette_initialized();
42
43 // Create image_t structure
44 image_t image;
45 image.w = width;
46 image.h = height;
47
48 // Allocate RGB pixel array (3 bytes per pixel)
49 image.pixels = (rgb_pixel_t *)malloc(width * height * sizeof(rgb_pixel_t));
50 if (!image.pixels) {
51 return NULL;
52 }
53
54 // Convert RGBA to RGB (skip alpha channel)
55 for (int i = 0; i < width * height; i++) {
56 image.pixels[i].r = rgba_data[i * 4 + 0];
57 image.pixels[i].g = rgba_data[i * 4 + 1];
58 image.pixels[i].b = rgba_data[i * 4 + 2];
59 // Skip alpha channel (rgba_data[i * 4 + 3])
60 }
61
62 // Call ascii_convert
63 // Parameters: image, target_width, target_height, color, aspect_ratio, stretch, palette, luminance
64 char *result = ascii_convert(&image, ascii_width, ascii_height,
65 false, // color: false for monochrome (simpler for MVP)
66 true, // aspect_ratio: true to maintain aspect
67 false, // stretch: false to preserve aspect ratio
68 WASM_PALETTE, g_luminance_palette);
69
70 // Free the temporary RGB buffer
71 free(image.pixels);
72
73 return result;
74}
75
76// Free memory allocated by convert_frame_to_ascii
77EMSCRIPTEN_KEEPALIVE
78void free_ascii_buffer(char *buffer) {
79 if (buffer) {
80 free(buffer);
81 }
82}
char * ascii_convert(image_t *original, const ssize_t width, const ssize_t height, const bool color, const bool _aspect_ratio, const bool stretch, const char *palette_chars, const char luminance_palette[256])
Definition ascii.c:69
EMSCRIPTEN_KEEPALIVE void free_ascii_buffer(char *buffer)
Definition ascii_wasm.c:78
int build_client_luminance_palette(const char *palette_chars, size_t palette_len, char luminance_mapping[256])
Definition palette.c:279
EMSCRIPTEN_KEEPALIVE char * convert_frame_to_ascii(uint8_t *rgba_data, int width, int height, int ascii_width, int ascii_height)
Definition ascii_wasm.c:39