18#include "../output_buffer.h"
26char *render_ascii_image_monochrome_sse2(
const image_t *image,
const char *ascii_chars) {
27 if (!image || !image->
pixels || !ascii_chars) {
31 const int h = image->
h;
32 const int w = image->
w;
34 if (h <= 0 || w <= 0) {
41 log_error(
"Failed to get UTF-8 palette cache");
46 const size_t max_char_bytes = 4;
47 const size_t len = (size_t)h * ((
size_t)w * max_char_bytes + 1);
53 const rgb_pixel_t *pixels = (
const rgb_pixel_t *)image->
pixels;
56 for (
int y = 0; y < h; y++) {
57 const rgb_pixel_t *row = &pixels[y * w];
61 for (; x + 15 < w; x += 16) {
63 uint8_t r_array[16], g_array[16], b_array[16];
64 for (
int j = 0; j < 16; j++) {
65 r_array[j] = row[x + j].r;
66 g_array[j] = row[x + j].g;
67 b_array[j] = row[x + j].b;
71 __m128i r_vec_lo = _mm_loadl_epi64((__m128i *)(r_array + 0));
72 __m128i r_vec_hi = _mm_loadl_epi64((__m128i *)(r_array + 8));
73 __m128i g_vec_lo = _mm_loadl_epi64((__m128i *)(g_array + 0));
74 __m128i g_vec_hi = _mm_loadl_epi64((__m128i *)(g_array + 8));
75 __m128i b_vec_lo = _mm_loadl_epi64((__m128i *)(b_array + 0));
76 __m128i b_vec_hi = _mm_loadl_epi64((__m128i *)(b_array + 8));
79 __m128i r_16_lo = _mm_unpacklo_epi8(r_vec_lo, _mm_setzero_si128());
80 __m128i g_16_lo = _mm_unpacklo_epi8(g_vec_lo, _mm_setzero_si128());
81 __m128i b_16_lo = _mm_unpacklo_epi8(b_vec_lo, _mm_setzero_si128());
83 __m128i luma_r_lo = _mm_mullo_epi16(r_16_lo, _mm_set1_epi16(77));
84 __m128i luma_g_lo = _mm_mullo_epi16(g_16_lo, _mm_set1_epi16(150));
85 __m128i luma_b_lo = _mm_mullo_epi16(b_16_lo, _mm_set1_epi16(29));
87 __m128i luma_sum_lo = _mm_add_epi16(luma_r_lo, luma_g_lo);
88 luma_sum_lo = _mm_add_epi16(luma_sum_lo, luma_b_lo);
89 luma_sum_lo = _mm_add_epi16(luma_sum_lo, _mm_set1_epi16(128));
90 luma_sum_lo = _mm_srli_epi16(luma_sum_lo, 8);
93 __m128i r_16_hi = _mm_unpacklo_epi8(r_vec_hi, _mm_setzero_si128());
94 __m128i g_16_hi = _mm_unpacklo_epi8(g_vec_hi, _mm_setzero_si128());
95 __m128i b_16_hi = _mm_unpacklo_epi8(b_vec_hi, _mm_setzero_si128());
97 __m128i luma_r_hi = _mm_mullo_epi16(r_16_hi, _mm_set1_epi16(77));
98 __m128i luma_g_hi = _mm_mullo_epi16(g_16_hi, _mm_set1_epi16(150));
99 __m128i luma_b_hi = _mm_mullo_epi16(b_16_hi, _mm_set1_epi16(29));
101 __m128i luma_sum_hi = _mm_add_epi16(luma_r_hi, luma_g_hi);
102 luma_sum_hi = _mm_add_epi16(luma_sum_hi, luma_b_hi);
103 luma_sum_hi = _mm_add_epi16(luma_sum_hi, _mm_set1_epi16(128));
104 luma_sum_hi = _mm_srli_epi16(luma_sum_hi, 8);
107 __m128i luminance_lo = _mm_packus_epi16(luma_sum_lo, _mm_setzero_si128());
108 __m128i luminance_hi = _mm_packus_epi16(luma_sum_hi, _mm_setzero_si128());
112 _mm_storel_epi64((__m128i *)(luma_array + 0), luminance_lo);
113 _mm_storel_epi64((__m128i *)(luma_array + 8), luminance_hi);
116 for (
int j = 0; j < 16; j++) {
131 const rgb_pixel_t pixel = row[x];
158 return (
uint8_t)(16 + 36 * (r / 51) + 6 * (g / 51) + (b / 51));
162char *render_ascii_sse2_unified_optimized(
const image_t *image,
bool use_background,
bool use_256color,
163 const char *ascii_chars) {
164 if (!image || !image->
pixels) {
168 const int width = image->
w;
169 const int height = image->
h;
171 if (width <= 0 || height <= 0) {
180 size_t bytes_per_pixel = use_256color ? 6u : 8u;
183 size_t height_times_width;
184 if (checked_size_mul((
size_t)height, (
size_t)width, &height_times_width) !=
ASCIICHAT_OK) {
185 log_error(
"Buffer size overflow: height * width overflow");
189 size_t pixel_data_size;
190 if (checked_size_mul(height_times_width, bytes_per_pixel, &pixel_data_size) !=
ASCIICHAT_OK) {
191 log_error(
"Buffer size overflow: (height * width) * bytes_per_pixel overflow");
195 size_t height_times_16;
196 if (checked_size_mul((
size_t)height, 16u, &height_times_16) !=
ASCIICHAT_OK) {
197 log_error(
"Buffer size overflow: height * 16 overflow");
202 if (checked_size_add(pixel_data_size, height_times_16, &temp) !=
ASCIICHAT_OK) {
203 log_error(
"Buffer size overflow: pixel_data + height*16 overflow");
208 log_error(
"Buffer size overflow: total capacity overflow");
219 log_error(
"Failed to get UTF-8 palette cache for SSE2 color");
228 int curR = -1, curG = -1, curB = -1;
229 int cur_color_idx = -1;
231 for (
int y = 0; y < height; y++) {
232 const rgb_pixel_t *row = &((
const rgb_pixel_t *)image->
pixels)[y * width];
236 while (x + 16 <= width) {
238 uint8_t r_array[16], g_array[16], b_array[16];
239 for (
int j = 0; j < 16; j++) {
240 r_array[j] = row[x + j].r;
241 g_array[j] = row[x + j].g;
242 b_array[j] = row[x + j].b;
246 __m128i r_vec = _mm_loadl_epi64((__m128i *)r_array);
247 __m128i g_vec = _mm_loadl_epi64((__m128i *)g_array);
248 __m128i b_vec = _mm_loadl_epi64((__m128i *)b_array);
251 __m128i r_16 = _mm_unpacklo_epi8(r_vec, _mm_setzero_si128());
252 __m128i g_16 = _mm_unpacklo_epi8(g_vec, _mm_setzero_si128());
253 __m128i b_16 = _mm_unpacklo_epi8(b_vec, _mm_setzero_si128());
256 __m128i luma_r = _mm_mullo_epi16(r_16, _mm_set1_epi16(
LUMA_RED));
257 __m128i luma_g = _mm_mullo_epi16(g_16, _mm_set1_epi16(
LUMA_GREEN));
258 __m128i luma_b = _mm_mullo_epi16(b_16, _mm_set1_epi16(
LUMA_BLUE));
260 __m128i luma_sum = _mm_add_epi16(luma_r, luma_g);
261 luma_sum = _mm_add_epi16(luma_sum, luma_b);
262 luma_sum = _mm_add_epi16(luma_sum, _mm_set1_epi16(
LUMA_THRESHOLD));
263 luma_sum = _mm_srli_epi16(luma_sum, 8);
266 __m128i luminance = _mm_packus_epi16(luma_sum, _mm_setzero_si128());
268 _mm_storel_epi64((__m128i *)luma_array, luminance);
272 for (
int i = 0; i < 8; i++) {
273 const uint8_t luma_idx = luma_array[i] >> 2;
274 char_indices[i] = luma_idx;
280 for (
int i = 0; i < 8; i++) {
281 color_indices[i] = rgb_to_256color_sse2(r_array[i], g_array[i], b_array[i]);
285 for (
int i = 0; i < 8;) {
286 const uint8_t char_idx = char_indices[i];
288 const uint8_t color_idx = color_indices[i];
291 while (j < 8 && char_indices[j] == char_idx && color_indices[j] == color_idx) {
296 if (color_idx != cur_color_idx) {
297 if (use_background) {
302 cur_color_idx = color_idx;
310 for (
uint32_t k = 1; k < run; k++) {
318 for (
int i = 0; i < 8;) {
319 const uint8_t char_idx = char_indices[i];
326 while (j < 8 && char_indices[j] == char_idx && r_array[j] == r && g_array[j] == g && b_array[j] == b) {
331 if (r != curR || g != curG || b != curB) {
332 if (use_background) {
347 for (
uint32_t k = 1; k < run; k++) {
359 const rgb_pixel_t *p = &row[x];
360 uint32_t R = p->r, G = p->g, B = p->b;
371 const rgb_pixel_t *q = &row[j];
372 uint32_t R2 = q->r, G2 = q->g, B2 = q->b;
376 if (luma_idx2 != luma_idx || color_idx2 != color_idx)
382 if (color_idx != cur_color_idx) {
383 if (use_background) {
388 cur_color_idx = color_idx;
396 for (
uint32_t k = 1; k < run; k++) {
405 const rgb_pixel_t *q = &row[j];
406 uint32_t R2 = q->r, G2 = q->g, B2 = q->b;
409 if (luma_idx2 != luma_idx || R2 != R || G2 != G || B2 != B)
415 if ((
int)R != curR || (int)G != curG || (
int)B != curB) {
416 if (use_background) {
431 for (
uint32_t k = 1; k < run; k++) {
441 if (y < height - 1) {
444 curR = curG = curB = -1;
453void sse2_caches_destroy(
void) {
455 log_debug(
"SSE2_CACHE: SSE2 caches cleaned up");
SIMD-optimized ASCII conversion interface.
#define SAFE_MALLOC(size, cast)
#define log_error(...)
Log an ERROR message.
#define log_debug(...)
Log a DEBUG message.
#define LUMA_BLUE
Luminance blue coefficient (0.114 * 256 = 29)
void emit_set_256_color_bg(outbuf_t *ob, uint8_t color_idx)
Emit 256-color background ANSI sequence.
#define LUMA_GREEN
Luminance green coefficient (0.587 * 256 = 150)
utf8_palette_cache_t * get_utf8_palette_cache(const char *ascii_chars)
Get or create UTF-8 palette cache.
void emit_set_256_color_fg(outbuf_t *ob, uint8_t color_idx)
Emit 256-color foreground ANSI sequence.
void ob_term(outbuf_t *ob)
Append null terminator to buffer.
#define LUMA_THRESHOLD
Luminance threshold for rounding.
void ob_putc(outbuf_t *ob, char c)
Append a character to buffer.
bool rep_is_profitable(uint32_t runlen)
Check if run-length encoding is profitable.
void emit_set_truecolor_fg(outbuf_t *ob, uint8_t r, uint8_t g, uint8_t b)
Emit truecolor foreground ANSI sequence.
void emit_rep(outbuf_t *ob, uint32_t extra)
Emit run-length encoded sequence.
void ob_write(outbuf_t *ob, const char *s, size_t n)
Append a string to buffer.
void emit_reset(outbuf_t *ob)
Emit ANSI reset sequence.
void emit_set_truecolor_bg(outbuf_t *ob, uint8_t r, uint8_t g, uint8_t b)
Emit truecolor background ANSI sequence.
#define LUMA_RED
Luminance red coefficient (0.299 * 256 = 77)
✅ Safe Integer Arithmetic and Overflow Detection
SSE2-optimized ASCII rendering functions.
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)
Dynamic output buffer (auto-expanding)
size_t cap
Buffer capacity in bytes (maximum length before reallocation)
char * buf
Buffer pointer (allocated, owned by caller, must be freed)
UTF-8 character structure.
UTF-8 palette cache structure.