Skip to content

Commit

Permalink
libchafa: Import newest version of Smolscale with sRGB handling etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
hpjansson committed Dec 22, 2023
1 parent bcd1396 commit 27a31f9
Show file tree
Hide file tree
Showing 12 changed files with 7,259 additions and 4,908 deletions.
21 changes: 12 additions & 9 deletions chafa/chafa-symbol-map.c
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,12 @@ glyph_to_bitmap (gint width, gint height,

/* Scale to cell dimensions */

smol_scale_simple ((SmolPixelType) pixel_format, pixels, width, height, rowstride,
SMOL_PIXEL_RGBA8_PREMULTIPLIED,
smol_scale_simple (pixels, (SmolPixelType) pixel_format, width, height, rowstride,
(gpointer) scaled_pixels,
SMOL_PIXEL_RGBA8_UNASSOCIATED, /* FIXME: Premul */
CHAFA_SYMBOL_WIDTH_PIXELS, CHAFA_SYMBOL_HEIGHT_PIXELS,
CHAFA_SYMBOL_WIDTH_PIXELS * 4);
CHAFA_SYMBOL_WIDTH_PIXELS * 4,
SMOL_NO_FLAGS);

/* Generate coverage map */

Expand All @@ -308,11 +309,12 @@ glyph_to_bitmap_wide (gint width, gint height,

/* Scale to cell dimensions */

smol_scale_simple ((SmolPixelType) pixel_format, pixels, width, height, rowstride,
SMOL_PIXEL_RGBA8_PREMULTIPLIED,
smol_scale_simple (pixels, (SmolPixelType) pixel_format, width, height, rowstride,
(gpointer) scaled_pixels,
SMOL_PIXEL_RGBA8_UNASSOCIATED, /* FIXME: Premul */
CHAFA_SYMBOL_WIDTH_PIXELS * 2, CHAFA_SYMBOL_HEIGHT_PIXELS,
CHAFA_SYMBOL_WIDTH_PIXELS * 4 * 2);
CHAFA_SYMBOL_WIDTH_PIXELS * 4 * 2,
SMOL_NO_FLAGS);

/* Generate coverage map */

Expand Down Expand Up @@ -1872,10 +1874,11 @@ chafa_symbol_map_get_glyph (ChafaSymbolMap *symbol_map,
gpointer temp_pixels = g_malloc (width * CHAFA_SYMBOL_HEIGHT_PIXELS * 4);

/* Convert to desired pixel format */
smol_scale_simple (SMOL_PIXEL_ARGB8_PREMULTIPLIED, *pixels_out,
smol_scale_simple (*pixels_out, SMOL_PIXEL_ARGB8_UNASSOCIATED, /* FIXME: Premul */
width, height, rowstride,
(SmolPixelType) pixel_format, temp_pixels,
width, height, rowstride);
temp_pixels, (SmolPixelType) pixel_format,
width, height, rowstride,
SMOL_NO_FLAGS);
g_free (*pixels_out);
*pixels_out = temp_pixels;
}
Expand Down
36 changes: 34 additions & 2 deletions chafa/internal/chafa-indexed-image.c
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ chafa_indexed_image_draw_pixels (ChafaIndexedImage *indexed_image,
gint dest_width, gint dest_height)
{
DrawPixelsCtx ctx;
ChafaColor bg;

g_return_if_fail (dest_width == indexed_image->width);
g_return_if_fail (dest_height <= indexed_image->height);
Expand All @@ -444,23 +445,54 @@ chafa_indexed_image_draw_pixels (ChafaIndexedImage *indexed_image,
ctx.dest_width = dest_width;
ctx.dest_height = dest_height;

#if 0
/* FIXME: Need a new smolscale compositing mode that preserves src
* alpha before this can be implemented */
bg = *chafa_palette_get_color (&indexed_image->palette,
CHAFA_COLOR_SPACE_RGB,
CHAFA_PALETTE_INDEX_BG);
bg.ch [3] = 0xff;
#else
gen_color_lut_rgba8 (ctx.bg_color_lut,
*chafa_palette_get_color (&indexed_image->palette,
CHAFA_COLOR_SPACE_RGB,
CHAFA_PALETTE_INDEX_BG));
#endif

ctx.scaled_data = g_new (guint32, dest_width * dest_height);
ctx.scale_ctx = smol_scale_new_full ((SmolPixelType) src_pixel_type,
ctx.scale_ctx = smol_scale_new_full (/* Source */
(const guint32 *) src_pixels,
(SmolPixelType) src_pixel_type,
src_width,
src_height,
src_rowstride,
SMOL_PIXEL_RGBA8_PREMULTIPLIED,
/* Fill */
#if 0
bg.ch,
#else
NULL,
#endif
SMOL_PIXEL_RGBA8_UNASSOCIATED,
/* Destination */
NULL,
SMOL_PIXEL_RGBA8_PREMULTIPLIED,
dest_width,
dest_height,
dest_width * sizeof (guint32),
/* Placement */
0,
0,
dest_width * SMOL_SUBPIXEL_MUL,
dest_height * SMOL_SUBPIXEL_MUL,
/* Extra args */
SMOL_COMPOSITE_SRC_CLEAR_DEST,
#if 0
SMOL_NO_FLAGS,
NULL,
#else
SMOL_DISABLE_SRGB_LINEARIZATION,
post_scale_row,
#endif
&ctx);

draw_pixels (&ctx);
Expand Down
28 changes: 24 additions & 4 deletions chafa/internal/chafa-iterm2-canvas.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,17 @@ typedef enum
}
TiffTagId;

typedef enum
{
TIFF_EXTRA_SAMPLE_UNSPECIFIED = 0,
TIFF_EXTRA_SAMPLE_ASSOC_ALPHA = 1,
TIFF_EXTRA_SAMPLE_UNASSOC_ALPHA = 2
}
TiffExtraSampleType;

#define TIFF_PHOTOMETRIC_INTERPRETATION_RGB 2
#define TIFF_ORIENTATION_TOPLEFT 1
#define TIFF_PLANAR_CONFIGURATION_CONTIGUOUS 1
#define TIFF_EXTRA_SAMPLE_ASSOC_ALPHA 1

typedef struct
{
Expand Down Expand Up @@ -150,16 +157,29 @@ chafa_iterm2_canvas_draw_all_pixels (ChafaIterm2Canvas *iterm2_canvas, ChafaPixe
return;

ctx.iterm2_canvas = iterm2_canvas;
ctx.scale_ctx = smol_scale_new_full ((SmolPixelType) src_pixel_type,
ctx.scale_ctx = smol_scale_new_full (/* Source */
(const guint32 *) src_pixels,
(SmolPixelType) src_pixel_type,
src_width,
src_height,
src_rowstride,
SMOL_PIXEL_RGBA8_PREMULTIPLIED,
/* Fill */
NULL,
SMOL_PIXEL_RGBA8_UNASSOCIATED,
/* Destination */
NULL,
SMOL_PIXEL_RGBA8_UNASSOCIATED, /* FIXME: Premul? */
iterm2_canvas->width,
iterm2_canvas->height,
iterm2_canvas->width * sizeof (guint32),
/* Placement */
0,
0,
iterm2_canvas->width * SMOL_SUBPIXEL_MUL,
iterm2_canvas->height * SMOL_SUBPIXEL_MUL,
/* Extra args */
SMOL_COMPOSITE_SRC,
SMOL_DISABLE_SRGB_LINEARIZATION,
NULL,
&ctx);

Expand Down Expand Up @@ -246,7 +266,7 @@ chafa_iterm2_canvas_build_ansi (ChafaIterm2Canvas *iterm2_canvas, ChafaTermInfo
generate_tag (&base64, out_str, TIFF_TAG_STRIP_BYTE_COUNTS, TIFF_TYPE_LONG, 1,
iterm2_canvas->width * iterm2_canvas->height * 4);
generate_tag (&base64, out_str, TIFF_TAG_PLANAR_CONFIGURATION, TIFF_TYPE_SHORT, 1, TIFF_PLANAR_CONFIGURATION_CONTIGUOUS);
generate_tag (&base64, out_str, TIFF_TAG_EXTRA_SAMPLES, TIFF_TYPE_SHORT, 1, TIFF_EXTRA_SAMPLE_ASSOC_ALPHA);
generate_tag (&base64, out_str, TIFF_TAG_EXTRA_SAMPLES, TIFF_TYPE_SHORT, 1, TIFF_EXTRA_SAMPLE_UNASSOC_ALPHA);

/* Next IFD offset (terminator) */

Expand Down
36 changes: 21 additions & 15 deletions chafa/internal/chafa-kitty-canvas.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ typedef struct
{
ChafaKittyCanvas *kitty_canvas;
SmolScaleCtx *scale_ctx;
ChafaColor bg_color;
gboolean flatten_alpha;
}
DrawCtx;

Expand Down Expand Up @@ -130,14 +128,6 @@ draw_pixels_worker (ChafaBatchInfo *batch, const DrawCtx *ctx)
((guint32 *) ctx->kitty_canvas->rgba_image) + (ctx->kitty_canvas->width * batch->first_row),
batch->first_row,
batch->n_rows);

/* FIXME: Smolscale should be able to do this */
if (ctx->flatten_alpha)
chafa_composite_rgba_on_solid_color (ctx->bg_color,
ctx->kitty_canvas->rgba_image,
ctx->kitty_canvas->width,
batch->first_row,
batch->n_rows);
}

void
Expand All @@ -146,7 +136,9 @@ chafa_kitty_canvas_draw_all_pixels (ChafaKittyCanvas *kitty_canvas, ChafaPixelTy
gint src_width, gint src_height, gint src_rowstride,
ChafaColor bg_color)
{
uint8_t bg_color_rgba [4];
DrawCtx ctx;
gboolean flatten_alpha;

g_return_if_fail (kitty_canvas != NULL);
g_return_if_fail (src_pixel_type < CHAFA_PIXEL_MAX);
Expand All @@ -157,22 +149,36 @@ chafa_kitty_canvas_draw_all_pixels (ChafaKittyCanvas *kitty_canvas, ChafaPixelTy
if (src_width == 0 || src_height == 0)
return;

flatten_alpha = bg_color.ch [3] == 0;
bg_color.ch [3] = 0xff;
chafa_color8_store_to_rgba8 (bg_color, bg_color_rgba);

ctx.kitty_canvas = kitty_canvas;
ctx.scale_ctx = smol_scale_new_full ((SmolPixelType) src_pixel_type,
ctx.scale_ctx = smol_scale_new_full (/* Source */
(const guint32 *) src_pixels,
(SmolPixelType) src_pixel_type,
src_width,
src_height,
src_rowstride,
SMOL_PIXEL_RGBA8_PREMULTIPLIED,
/* Fill */
flatten_alpha ? bg_color_rgba : NULL,
SMOL_PIXEL_RGBA8_UNASSOCIATED,
/* Destination */
NULL,
SMOL_PIXEL_RGBA8_UNASSOCIATED, /* FIXME: Opaque? */
kitty_canvas->width,
kitty_canvas->height,
kitty_canvas->width * sizeof (guint32),
/* Placement */
0,
0,
kitty_canvas->width * SMOL_SUBPIXEL_MUL,
kitty_canvas->height * SMOL_SUBPIXEL_MUL,
/* Extra args */
SMOL_COMPOSITE_SRC,
SMOL_NO_FLAGS,
NULL,
&ctx);
ctx.bg_color = bg_color;
ctx.flatten_alpha = bg_color.ch [3] == 0;

chafa_process_batches (&ctx,
(GFunc) draw_pixels_worker,
NULL,
Expand Down
48 changes: 11 additions & 37 deletions chafa/internal/chafa-pixops.c
Original file line number Diff line number Diff line change
Expand Up @@ -620,33 +620,6 @@ composite_alpha_on_bg (ChafaColor bg_color,
}
}

/* FIXME: Could we always destroy the alpha channel and eliminate the other
* variant? */
static void
composite_alpha_on_solid (ChafaColor bg_color,
ChafaPixel *pixels, gint width, gint first_row, gint n_rows)
{
ChafaPixel *p0, *p1;

p0 = pixels + first_row * width;
p1 = p0 + n_rows * width;

for ( ; p0 < p1; p0++)
{
p0->col.ch [0] += (bg_color.ch [0] * (255 - (guint32) p0->col.ch [3])) / 255;
p0->col.ch [1] += (bg_color.ch [1] * (255 - (guint32) p0->col.ch [3])) / 255;
p0->col.ch [2] += (bg_color.ch [2] * (255 - (guint32) p0->col.ch [3])) / 255;
p0->col.ch [3] = 0xff;
}
}

void
chafa_composite_rgba_on_solid_color (ChafaColor color,
ChafaPixel *pixels, gint width, gint first_row, gint n_rows)
{
composite_alpha_on_solid (color, pixels, width, first_row, n_rows);
}

static void
prepare_pixels_2_worker (ChafaBatchInfo *batch, PrepareContext *prep_ctx)
{
Expand Down Expand Up @@ -798,16 +771,17 @@ chafa_prepare_pixel_data_for_symbols (const ChafaPalette *palette,
prep_ctx.dest_width = dest_width;
prep_ctx.dest_height = dest_height;

prep_ctx.scale_ctx = smol_scale_new ((SmolPixelType) prep_ctx.src_pixel_type,
(const guint32 *) prep_ctx.src_pixels,
prep_ctx.src_width,
prep_ctx.src_height,
prep_ctx.src_rowstride,
SMOL_PIXEL_RGBA8_PREMULTIPLIED,
NULL,
prep_ctx.dest_width,
prep_ctx.dest_height,
prep_ctx.dest_width * sizeof (guint32));
prep_ctx.scale_ctx = smol_scale_new_simple (prep_ctx.src_pixels,
(SmolPixelType) prep_ctx.src_pixel_type,
prep_ctx.src_width,
prep_ctx.src_height,
prep_ctx.src_rowstride,
NULL,
SMOL_PIXEL_RGBA8_UNASSOCIATED, /* FIXME: Premul */
prep_ctx.dest_width,
prep_ctx.dest_height,
prep_ctx.dest_width * sizeof (guint32),
SMOL_NO_FLAGS);

prepare_pixels_pass_1 (&prep_ctx);
prepare_pixels_pass_2 (&prep_ctx);
Expand Down
3 changes: 0 additions & 3 deletions chafa/internal/chafa-pixops.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ void chafa_sort_pixel_index_by_channel (guint8 *index,
const ChafaPixel *pixels, gint n_pixels,
gint ch);

void chafa_composite_rgba_on_solid_color (ChafaColor color,
ChafaPixel *pixels, gint width, gint first_row, gint n_rows);

G_END_DECLS

#endif /* __CHAFA_PIXOPS_H__ */
1 change: 1 addition & 0 deletions chafa/internal/smolscale/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ libsmolscale_la_LIBADD =
libsmolscale_la_SOURCES = \
smolscale.c \
smolscale.h \
smolscale-generic.c \
smolscale-private.h

if HAVE_AVX2_INTRINSICS
Expand Down
Loading

0 comments on commit 27a31f9

Please sign in to comment.