Skip to content

Commit

Permalink
ChafaPalette: Improve dynamic palette generator
Browse files Browse the repository at this point in the history
This replaces the old median cut quantizer with a pairwise
nearest-neighbor clustering approach. It also makes the sixel
quantization parameters tunable with -w.

The new approach is faster and yields much higher quality. On
the highest setting, it outperforms ImageMagick on peak absolute
error and comes close in other quality measurements, using
less than 1/8 of the energy and 1/24 wall clock time (4 cores).

The prepared image in the below test is a 3873x2582 QIF image.
Other images show a similar trend. Dithering was turned off in
all tests.

specimen ncolors    MSE  SSIM   PSNR  PAE utime elapsed peakrss
original   37568      -     -      -    -     -       -       -
prepared   34683      -     -      -    -     -       -       -
magick       251  2.873 0.967 43.582 5654  3.89    3.96  283296
img2sixel    250  6.857 0.912 39.803 5397  7.90    7.93  114412
ncplayer     294 21.944 0.858 34.752 8995  0.67    0.54  204612
chafa-old    162  6.002 0.964 40.382 8481  0.50    0.15   95104
chafa-w1      24 27.572 0.838 33.760 6939  0.25    0.10   95104
chafa-w2      25 26.801 0.838 33.883 6682  0.26    0.10   94976
chafa-w3     135  7.390 0.929 39.478 4112  0.33    0.11   94984
chafa-w4     143  7.191 0.929 39.597 4112  0.34    0.11   95080
chafa-w5     154  7.221 0.929 39.578 3598  0.34    0.11   95000
chafa-w6     255  3.960 0.949 42.188 4369  0.43    0.13   94984
chafa-w7     255  3.991 0.949 42.154 3598  0.40    0.13   94860
chafa-w8     255  3.976 0.949 42.171 3855  0.42    0.16   94984
chafa-w9     255  3.965 0.949 42.182 3341  0.45    0.16   99008

Fixes #174 (GitHub).
  • Loading branch information
hpjansson committed Nov 20, 2024
1 parent 70d1868 commit 9ed690f
Show file tree
Hide file tree
Showing 10 changed files with 392 additions and 313 deletions.
3 changes: 2 additions & 1 deletion chafa/chafa-canvas.c
Original file line number Diff line number Diff line change
Expand Up @@ -1352,7 +1352,8 @@ draw_all_pixels (ChafaCanvas *canvas, ChafaPixelType src_pixel_type,
src_width, src_height,
src_rowstride,
halign, valign,
tuck);
tuck,
canvas->config.work_factor);
}
else if (canvas->config.pixel_mode == CHAFA_PIXEL_MODE_KITTY)
{
Expand Down
7 changes: 5 additions & 2 deletions chafa/internal/chafa-indexed-image.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ typedef struct
gconstpointer src_pixels;
gint src_width, src_height, src_rowstride;
gint dest_width, dest_height;
gfloat quality;

SmolScaleCtx *scale_ctx;
guint32 *scaled_data;
Expand Down Expand Up @@ -380,7 +381,7 @@ draw_pixels (DrawPixelsCtx *ctx)

chafa_palette_generate (&ctx->indexed_image->palette,
ctx->scaled_data, ctx->dest_width * ctx->dest_height,
ctx->color_space);
ctx->color_space, ctx->quality);

/* Single thread only for diffusion; it's a fully serial operation */
chafa_process_batches (ctx,
Expand Down Expand Up @@ -439,7 +440,8 @@ chafa_indexed_image_draw_pixels (ChafaIndexedImage *indexed_image,
gint src_width, gint src_height, gint src_rowstride,
gint dest_width, gint dest_height,
ChafaAlign halign, ChafaAlign valign,
ChafaTuck tuck)
ChafaTuck tuck,
gfloat quality)
{
DrawPixelsCtx ctx;
ChafaColor bg;
Expand All @@ -461,6 +463,7 @@ chafa_indexed_image_draw_pixels (ChafaIndexedImage *indexed_image,
ctx.src_rowstride = src_rowstride;
ctx.dest_width = dest_width;
ctx.dest_height = dest_height;
ctx.quality = quality;

#if 0
/* FIXME: Need a new smolscale compositing mode that preserves src
Expand Down
3 changes: 2 additions & 1 deletion chafa/internal/chafa-indexed-image.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ void chafa_indexed_image_draw_pixels (ChafaIndexedImage *indexed_image,
gint src_width, gint src_height, gint src_rowstride,
gint dest_width, gint dest_height,
ChafaAlign halign, ChafaAlign valign,
ChafaTuck tuck);
ChafaTuck tuck,
gfloat quality);

G_END_DECLS

Expand Down
2 changes: 2 additions & 0 deletions chafa/internal/chafa-math-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

G_BEGIN_DECLS

#define CHAFA_SQUARE(n) ((n) * (n))

void chafa_tuck_and_align (gint src_width, gint src_height,
gint dest_width, gint dest_height,
ChafaAlign halign, ChafaAlign valign,
Expand Down
Loading

0 comments on commit 9ed690f

Please sign in to comment.