From f20f4c20bcb715eeb7974854b0689688975fdeee Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 1 Oct 2026 20:11:49 -0600 Subject: [PATCH] [raster] Fix background color and stride handling (#6310) * [raster] Fix background color premultiplication and stride handling ensure_initialized()'s background fill had two bugs: 1. The background pixel was built with HB_COLOR(blue, green, red, alpha), which packs channels through HB_TAG's bit layout (b<<24 | g<<16 | r<<8 | a). Every other pixel in this buffer is premultiplied BGRA32 (b | g<<8 | r<<16 | a<<24, see hb-raster.hh's SRC_OVER comment: "premultiplied src over premultiplied dst"), so the background was both unpremultiplied and in the wrong byte positions: a half-transparent red background (blue=0, green=0, red=255, alpha=128) filled the buffer with 0x0000ff80 instead of the correct premultiplied 0x80800000. 2. The fill then wrote that pixel into a contiguous run of width * height uint32_t slots starting at the buffer origin, assuming every row is packed at exactly width * 4 bytes. Once a caller sets a stride wider than that (row padding, alignment, embedding the glyph image inside a larger atlas), the fill drifts across row boundaries after the first row, leaving later rows partially uninitialized or wrongly colored. Build bg with color_to_premul_pixel(), the same helper solid fills already use, and fill row by row using the actual stride. Added two regression tests in test/api/test-raster.cc: test_background_premultiplied (default stride, half-alpha background, checks the stored pixel is premultiplied) and test_background_stride (stride much wider than width * 4, opaque background, checks every row). Assisted-by: Claude * [raster] Handle unaligned background strides A caller-supplied stride does not have to be word-aligned, so casting each row to uint32_t * can perform misaligned stores. Use hb_packed_t for the row writes, and exercise an odd stride in the regression test with byte-wise pixel checks. Tests: meson test -C build Assisted-by: Codex --------- Co-authored-by: Arthur031221 --- src/hb-raster-paint.cc | 20 ++++----- test/api/test-raster.cc | 94 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 11 deletions(-) diff --git a/src/hb-raster-paint.cc b/src/hb-raster-paint.cc index 4091d2ec2..5165afadf 100644 --- a/src/hb-raster-paint.cc +++ b/src/hb-raster-paint.cc @@ -128,19 +128,17 @@ ensure_initialized (hb_raster_paint_t *c) if (hb_color_get_alpha (c->background)) { - uint32_t bg = HB_COLOR (hb_color_get_blue (c->background), - hb_color_get_green (c->background), - hb_color_get_red (c->background), - hb_color_get_alpha (c->background)); -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wcast-align" - uint32_t *pixels = (uint32_t *) hb_raster_image_get_buffer (root); -#pragma GCC diagnostic pop + uint32_t bg = color_to_premul_pixel (c->background); hb_raster_extents_t ext; hb_raster_image_get_extents (root, &ext); - unsigned count = ext.width * ext.height; - for (unsigned i = 0; i < count; i++) - pixels[i] = bg; + const uint8_t *buf = hb_raster_image_get_buffer (root); + for (unsigned y = 0; y < ext.height; y++) + { + hb_packed_t *row = + (hb_packed_t *) (buf + (size_t) y * ext.stride); + for (unsigned x = 0; x < ext.width; x++) + row[x] = hb_packed_t (bg); + } } if (unlikely (!c->surface_stack.push_or_fail (root))) diff --git a/test/api/test-raster.cc b/test/api/test-raster.cc index 1db578c96..906e706ef 100644 --- a/test/api/test-raster.cc +++ b/test/api/test-raster.cc @@ -410,6 +410,98 @@ test_budget_colr_outline (void) hb_face_destroy (face); } +/* ── Test 11: background color is premultiplied before filling ─────── */ + +/* hb_raster_paint_set_background() must store the premultiplied BGRA32 + * pixel in the buffer, matching the format every other paint op in this + * file assumes (see hb-raster.hh's SRC_OVER: "premultiplied src over + * premultiplied dst"), not the raw unpremultiplied channel bytes. */ +static void +test_background_premultiplied (void) +{ + hb_raster_paint_t *paint = hb_raster_paint_create_or_fail (); + + const unsigned width = 2, height = 2; + hb_raster_extents_t ext = {0, 0, width, height, 0}; + hb_raster_paint_set_extents (paint, &ext); + + /* Half-alpha opaque red: blue=0, green=0, red=255, alpha=128. + * Premultiplied: r = div255(255*128) = 128, a stays 128. */ + hb_raster_paint_set_background (paint, HB_COLOR (0, 0, 255, 128)); + + hb_paint_funcs_t *funcs = hb_raster_paint_get_funcs (paint); + hb_paint_push_clip_rectangle (funcs, paint, 0.f, 0.f, (float) width, (float) height); + hb_paint_pop_clip (funcs, paint); + + hb_raster_image_t *img = hb_raster_paint_render (paint); + g_assert_nonnull (img); + + hb_raster_extents_t out_ext; + hb_raster_image_get_extents (img, &out_ext); + const uint8_t *buf = hb_raster_image_get_buffer (img); + + for (unsigned y = 0; y < out_ext.height; y++) + { + const uint32_t *row = (const uint32_t *) (buf + (size_t) y * out_ext.stride); + for (unsigned x = 0; x < out_ext.width; x++) + /* 0x80800000: alpha=0x80, premultiplied red=0x80, in BGRA memory + * order. The old HB_COLOR()-based reconstruction produces + * 0x0000ff80 instead (wrong byte layout, not premultiplied). */ + g_assert_cmphex (row[x], ==, 0x80800000); + } + + hb_raster_image_destroy (img); + hb_raster_paint_destroy (paint); +} + +/* ── Test 12: background pre-fill honors a stride wider than width*4 ── */ + +/* hb_raster_paint_set_background() must pre-fill every pixel of the + * output image with the premultiplied BGRA32 background color, honoring + * a caller-supplied stride wider than width * 4 bytes. */ +static void +test_background_stride (void) +{ + hb_raster_paint_t *paint = hb_raster_paint_create_or_fail (); + + const unsigned width = 2, height = 4; + const unsigned stride = 9; /* wider than width * 4, but not word-aligned */ + hb_raster_extents_t ext = {0, 0, width, height, stride}; + hb_raster_paint_set_extents (paint, &ext); + + /* Opaque red: blue=0, green=0, red=255, alpha=255. */ + hb_raster_paint_set_background (paint, HB_COLOR (0, 0, 255, 255)); + + /* Trigger initialization without painting any pixels directly, so the + * only thing that can have colored a pixel is the background fill. */ + hb_paint_funcs_t *funcs = hb_raster_paint_get_funcs (paint); + hb_paint_push_clip_rectangle (funcs, paint, 0.f, 0.f, (float) width, (float) height); + hb_paint_pop_clip (funcs, paint); + + hb_raster_image_t *img = hb_raster_paint_render (paint); + g_assert_nonnull (img); + + hb_raster_extents_t out_ext; + hb_raster_image_get_extents (img, &out_ext); + const uint8_t *buf = hb_raster_image_get_buffer (img); + + for (unsigned y = 0; y < out_ext.height; y++) + { + const uint8_t *row = buf + (size_t) y * out_ext.stride; + for (unsigned x = 0; x < out_ext.width; x++) + { + const uint8_t *pixel = row + x * 4; + g_assert_cmphex (pixel[0], ==, 0x00); /* blue */ + g_assert_cmphex (pixel[1], ==, 0x00); /* green */ + g_assert_cmphex (pixel[2], ==, 0xff); /* red */ + g_assert_cmphex (pixel[3], ==, 0xff); /* alpha */ + } + } + + hb_raster_image_destroy (img); + hb_raster_paint_destroy (paint); +} + /* ── main ────────────────────────────────────────────────────────── */ int @@ -427,6 +519,8 @@ main (int argc, char **argv) hb_test_add (test_set_glyph_extents_overflow); hb_test_add (test_budget); hb_test_add (test_budget_colr_outline); + hb_test_add (test_background_premultiplied); + hb_test_add (test_background_stride); return hb_test_run (); }