[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<uint32_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 <noreply@openai.com>

---------

Co-authored-by: Arthur031221 <levi74108520963@gmail.com>
This commit is contained in:
Behdad EsfahbodandArthur031221 authored and GitHub committed 2026-10-01 20:11:49 -06:00
1 parent b58d201455
commit f20f4c20bc
2 files changed
+103 -11

No files matched your search

+9 -11
View File
@@ -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<uint32_t> *row =
(hb_packed_t<uint32_t> *) (buf + (size_t) y * ext.stride);
for (unsigned x = 0; x < ext.width; x++)
row[x] = hb_packed_t<uint32_t> (bg);
}
}
if (unlikely (!c->surface_stack.push_or_fail (root)))