From 0e3bf6c9109da862adc65b76f15df22de1d063c4 Mon Sep 17 00:00:00 2001 From: Vftdan Date: Wed, 2 Oct 2024 19:43:30 +0200 Subject: [PATCH] Now only calculate color difference once for each palette entry Not clear whether it is more optimal --- img2cpi.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/img2cpi.c b/img2cpi.c index 87c4d86..4f66bc3 100644 --- a/img2cpi.c +++ b/img2cpi.c @@ -578,16 +578,23 @@ void convert_2x3(const struct image_pal *img, struct cc_char *characters) { void convert_8x11(const struct image_pal *img, struct cc_char *characters) { int w = img->w / 8, h = img->h / 11; + float palette_self_diffs[0x100][0x10] = {{(float) 0xffffff}}; + for (int input_color = 0x0; input_color < 0x100 && input_color < img->palette_size; input_color++) { + for (int output_color = 0x0; output_color < 0x10 && output_color < img->palette_size; output_color++) { + palette_self_diffs[input_color][output_color] = get_color_difference(img->palette[input_color], img->palette[output_color]); + } + } + for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { float chunk_palette_diffs[8][11][0x10] = {{{(float) 0xffffff}}}; for (int ox = 0; ox < 8; ox++) { for (int oy = 0; oy < 11; oy++) { - union color pixel = img->palette[img->pixels[ + uint8_t pixel_unresolved = img->pixels[ ox + (x + (y * 11 + oy) * w) * 8 - ]]; + ]; for (int color = 0x0; color < 0x10 && color < img->palette_size; color++) { - chunk_palette_diffs[ox][oy][color] = get_color_difference(pixel, img->palette[color]); + chunk_palette_diffs[ox][oy][color] = palette_self_diffs[pixel_unresolved][color]; } } }