From 400a80282aca8d6c8045bfb72889a062419e44ef Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sun, 22 Feb 2026 01:31:28 -0300 Subject: [PATCH] raster: implement all 28 composite blend modes Implement the full set of hb_paint_composite_mode_t blend modes: - Porter-Duff (13): CLEAR, SRC, DEST, SRC_OVER, DEST_OVER, SRC_IN, DEST_IN, SRC_OUT, DEST_OUT, SRC_ATOP, DEST_ATOP, XOR, PLUS - Separable (11): MULTIPLY, SCREEN, OVERLAY, DARKEN, LIGHTEN, COLOR_DODGE, COLOR_BURN, HARD_LIGHT, SOFT_LIGHT, DIFFERENCE, EXCLUSION - HSL (4): HUE, SATURATION, COLOR, LUMINOSITY Co-Authored-By: Claude Opus 4.6 --- src/hb-raster-paint.cc | 355 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 312 insertions(+), 43 deletions(-) diff --git a/src/hb-raster-paint.cc b/src/hb-raster-paint.cc index 20643fcac..97acc809e 100644 --- a/src/hb-raster-paint.cc +++ b/src/hb-raster-paint.cc @@ -86,11 +86,319 @@ color_to_premul_pixel (hb_color_t color) * Image compositing (operates on hb_raster_image_t) */ -/* Composite src image onto dst image using SRC_OVER. +/* Pack BGRA components into a premultiplied uint32_t. */ +static inline uint32_t +pack_pixel (uint8_t b, uint8_t g, uint8_t r, uint8_t a) +{ + return (uint32_t) b | ((uint32_t) g << 8) | ((uint32_t) r << 16) | ((uint32_t) a << 24); +} + +/* Unpack premultiplied pixel to float RGBA [0,1]. */ +static inline void +unpack_to_float (uint32_t px, float &r, float &g, float &b, float &a) +{ + b = (px & 0xFF) / 255.f; + g = ((px >> 8) & 0xFF) / 255.f; + r = ((px >> 16) & 0xFF) / 255.f; + a = (px >> 24) / 255.f; +} + +/* Pack float RGBA [0,1] premultiplied back to uint32_t. */ +static inline uint32_t +pack_from_float (float r, float g, float b, float a) +{ + return pack_pixel ((uint8_t) (hb_clamp (b, 0.f, 1.f) * 255.f + 0.5f), + (uint8_t) (hb_clamp (g, 0.f, 1.f) * 255.f + 0.5f), + (uint8_t) (hb_clamp (r, 0.f, 1.f) * 255.f + 0.5f), + (uint8_t) (hb_clamp (a, 0.f, 1.f) * 255.f + 0.5f)); +} + +/* Separable blend mode function: operates on unpremultiplied [0,1] channels. */ +static inline float +blend_multiply (float sc, float dc) { return sc * dc; } +static inline float +blend_screen (float sc, float dc) { return sc + dc - sc * dc; } +static inline float +blend_overlay (float sc, float dc) +{ return dc <= 0.5f ? 2.f * sc * dc : 1.f - 2.f * (1.f - sc) * (1.f - dc); } +static inline float +blend_darken (float sc, float dc) { return hb_min (sc, dc); } +static inline float +blend_lighten (float sc, float dc) { return hb_max (sc, dc); } +static inline float +blend_color_dodge (float sc, float dc) +{ + if (dc <= 0.f) return 0.f; + if (sc >= 1.f) return 1.f; + return hb_min (1.f, dc / (1.f - sc)); +} +static inline float +blend_color_burn (float sc, float dc) +{ + if (dc >= 1.f) return 1.f; + if (sc <= 0.f) return 0.f; + return 1.f - hb_min (1.f, (1.f - dc) / sc); +} +static inline float +blend_hard_light (float sc, float dc) +{ return sc <= 0.5f ? 2.f * sc * dc : 1.f - 2.f * (1.f - sc) * (1.f - dc); } +static inline float +blend_soft_light (float sc, float dc) +{ + if (sc <= 0.5f) + return dc - (1.f - 2.f * sc) * dc * (1.f - dc); + float d = (dc <= 0.25f) ? ((16.f * dc - 12.f) * dc + 4.f) * dc + : sqrtf (dc); + return dc + (2.f * sc - 1.f) * (d - dc); +} +static inline float +blend_difference (float sc, float dc) { return fabsf (sc - dc); } +static inline float +blend_exclusion (float sc, float dc) { return sc + dc - 2.f * sc * dc; } + +/* Apply a separable blend mode per-pixel. + * Both src and dst are premultiplied BGRA32. */ +static inline uint32_t +apply_separable_blend (uint32_t src, uint32_t dst, + float (*blend_fn)(float, float)) +{ + float sr, sg, sb, sa; + float dr, dg, db, da; + unpack_to_float (src, sr, sg, sb, sa); + unpack_to_float (dst, dr, dg, db, da); + + /* Unpremultiply */ + float usr = sa > 0.f ? sr / sa : 0.f; + float usg = sa > 0.f ? sg / sa : 0.f; + float usb = sa > 0.f ? sb / sa : 0.f; + float udr = da > 0.f ? dr / da : 0.f; + float udg = da > 0.f ? dg / da : 0.f; + float udb = da > 0.f ? db / da : 0.f; + + /* Blend (unpremultiplied channels) */ + float br = blend_fn (usr, udr); + float bg = blend_fn (usg, udg); + float bb = blend_fn (usb, udb); + + /* Composite formula: result = Sa*Da*B + Sa*(1-Da)*Sc + (1-Sa)*Da*Dc + * where B is the blended result */ + float ra = sa + da - sa * da; + float rr = sa * da * br + sa * (1.f - da) * usr + (1.f - sa) * da * udr; + float rg = sa * da * bg + sa * (1.f - da) * usg + (1.f - sa) * da * udg; + float rb = sa * da * bb + sa * (1.f - da) * usb + (1.f - sa) * da * udb; + + /* Re-premultiply */ + return pack_from_float (rr, rg, rb, ra); +} + +/* HSL helpers */ +static inline float +hsl_luminosity (float r, float g, float b) +{ return 0.299f * r + 0.587f * g + 0.114f * b; } + +static inline float +hsl_saturation (float r, float g, float b) +{ return hb_max (hb_max (r, g), b) - hb_min (hb_min (r, g), b); } + +static inline void +hsl_clip_color (float &r, float &g, float &b) +{ + float l = hsl_luminosity (r, g, b); + float mn = hb_min (hb_min (r, g), b); + float mx = hb_max (hb_max (r, g), b); + if (mn < 0.f) + { + float d = l - mn; + if (d > 0.f) { r = l + (r - l) * l / d; g = l + (g - l) * l / d; b = l + (b - l) * l / d; } + } + if (mx > 1.f) + { + float d = mx - l; + if (d > 0.f) { r = l + (r - l) * (1.f - l) / d; g = l + (g - l) * (1.f - l) / d; b = l + (b - l) * (1.f - l) / d; } + } +} + +static inline void +hsl_set_luminosity (float &r, float &g, float &b, float l) +{ + float d = l - hsl_luminosity (r, g, b); + r += d; g += d; b += d; + hsl_clip_color (r, g, b); +} + +static inline void +hsl_set_saturation_inner (float &mn, float &mid, float &mx, float s) +{ + if (mx > mn) + { + mid = (mid - mn) * s / (mx - mn); + mx = s; + } + else + mid = mx = 0.f; + mn = 0.f; +} + +static inline void +hsl_set_saturation (float &r, float &g, float &b, float s) +{ + /* Sort and apply set_saturation to the sorted triple */ + if (r <= g) + { + if (g <= b) hsl_set_saturation_inner (r, g, b, s); + else if (r <= b) hsl_set_saturation_inner (r, b, g, s); + else hsl_set_saturation_inner (b, r, g, s); + } + else + { + if (r <= b) hsl_set_saturation_inner (g, r, b, s); + else if (g <= b) hsl_set_saturation_inner (g, b, r, s); + else hsl_set_saturation_inner (b, g, r, s); + } +} + +/* Apply an HSL blend mode per-pixel. */ +static inline uint32_t +apply_hsl_blend (uint32_t src, uint32_t dst, + hb_paint_composite_mode_t mode) +{ + float sr, sg, sb, sa; + float dr, dg, db, da; + unpack_to_float (src, sr, sg, sb, sa); + unpack_to_float (dst, dr, dg, db, da); + + /* Unpremultiply */ + float usr = sa > 0.f ? sr / sa : 0.f; + float usg = sa > 0.f ? sg / sa : 0.f; + float usb = sa > 0.f ? sb / sa : 0.f; + float udr = da > 0.f ? dr / da : 0.f; + float udg = da > 0.f ? dg / da : 0.f; + float udb = da > 0.f ? db / da : 0.f; + + float br = udr, bg = udg, bb = udb; + + if (mode == HB_PAINT_COMPOSITE_MODE_HSL_HUE) + { + br = usr; bg = usg; bb = usb; + hsl_set_saturation (br, bg, bb, hsl_saturation (udr, udg, udb)); + hsl_set_luminosity (br, bg, bb, hsl_luminosity (udr, udg, udb)); + } + else if (mode == HB_PAINT_COMPOSITE_MODE_HSL_SATURATION) + { + br = udr; bg = udg; bb = udb; + hsl_set_saturation (br, bg, bb, hsl_saturation (usr, usg, usb)); + hsl_set_luminosity (br, bg, bb, hsl_luminosity (udr, udg, udb)); + } + else if (mode == HB_PAINT_COMPOSITE_MODE_HSL_COLOR) + { + br = usr; bg = usg; bb = usb; + hsl_set_luminosity (br, bg, bb, hsl_luminosity (udr, udg, udb)); + } + else /* HSL_LUMINOSITY */ + { + br = udr; bg = udg; bb = udb; + hsl_set_luminosity (br, bg, bb, hsl_luminosity (usr, usg, usb)); + } + + float ra = sa + da - sa * da; + float rr = sa * da * br + sa * (1.f - da) * usr + (1.f - sa) * da * udr; + float rg = sa * da * bg + sa * (1.f - da) * usg + (1.f - sa) * da * udg; + float rb = sa * da * bb + sa * (1.f - da) * usb + (1.f - sa) * da * udb; + + return pack_from_float (rr, rg, rb, ra); +} + +/* Porter-Duff composite: result = Fa * src + Fb * dst. + * Operates on premultiplied BGRA32 pixels. */ +static inline uint32_t +porter_duff (uint32_t src, uint32_t dst, unsigned fa_num, unsigned fa_den, + unsigned fb_num, unsigned fb_den) +{ + auto channel = [&] (unsigned sc, unsigned dc) -> uint8_t + { + return (uint8_t) hb_clamp ((int) (sc * fa_num / fa_den + dc * fb_num / fb_den), 0, 255); + }; + uint8_t rb = channel (src & 0xFF, dst & 0xFF); + uint8_t rg = channel ((src >> 8) & 0xFF, (dst >> 8) & 0xFF); + uint8_t rr = channel ((src >> 16) & 0xFF, (dst >> 16) & 0xFF); + uint8_t ra = channel (src >> 24, dst >> 24); + return pack_pixel (rb, rg, rr, ra); +} + +/* Composite per-pixel with full blend mode support. */ +static inline uint32_t +composite_pixel (uint32_t src, uint32_t dst, + hb_paint_composite_mode_t mode) +{ + uint8_t sa = (uint8_t) (src >> 24); + uint8_t da = (uint8_t) (dst >> 24); + + switch (mode) + { + case HB_PAINT_COMPOSITE_MODE_CLEAR: + return 0; + case HB_PAINT_COMPOSITE_MODE_SRC: + return src; + case HB_PAINT_COMPOSITE_MODE_DEST: + return dst; + case HB_PAINT_COMPOSITE_MODE_SRC_OVER: + return src_over (src, dst); + case HB_PAINT_COMPOSITE_MODE_DEST_OVER: + return src_over (dst, src); + case HB_PAINT_COMPOSITE_MODE_SRC_IN: + return alpha_mul (src, da); + case HB_PAINT_COMPOSITE_MODE_DEST_IN: + return alpha_mul (dst, sa); + case HB_PAINT_COMPOSITE_MODE_SRC_OUT: + return alpha_mul (src, 255 - da); + case HB_PAINT_COMPOSITE_MODE_DEST_OUT: + return alpha_mul (dst, 255 - sa); + case HB_PAINT_COMPOSITE_MODE_SRC_ATOP: + return porter_duff (src, dst, da, 255, 255 - sa, 255); + case HB_PAINT_COMPOSITE_MODE_DEST_ATOP: + return porter_duff (dst, src, sa, 255, 255 - da, 255); + case HB_PAINT_COMPOSITE_MODE_XOR: + return porter_duff (src, dst, 255 - da, 255, 255 - sa, 255); + case HB_PAINT_COMPOSITE_MODE_PLUS: + { + uint8_t rb = (uint8_t) hb_min (255u, (unsigned) (src & 0xFF) + (dst & 0xFF)); + uint8_t rg = (uint8_t) hb_min (255u, (unsigned) ((src >> 8) & 0xFF) + ((dst >> 8) & 0xFF)); + uint8_t rr = (uint8_t) hb_min (255u, (unsigned) ((src >> 16) & 0xFF) + ((dst >> 16) & 0xFF)); + uint8_t ra = (uint8_t) hb_min (255u, (unsigned) (src >> 24) + (dst >> 24)); + return pack_pixel (rb, rg, rr, ra); + } + + /* Separable blend modes */ + case HB_PAINT_COMPOSITE_MODE_MULTIPLY: return apply_separable_blend (src, dst, blend_multiply); + case HB_PAINT_COMPOSITE_MODE_SCREEN: return apply_separable_blend (src, dst, blend_screen); + case HB_PAINT_COMPOSITE_MODE_OVERLAY: return apply_separable_blend (src, dst, blend_overlay); + case HB_PAINT_COMPOSITE_MODE_DARKEN: return apply_separable_blend (src, dst, blend_darken); + case HB_PAINT_COMPOSITE_MODE_LIGHTEN: return apply_separable_blend (src, dst, blend_lighten); + case HB_PAINT_COMPOSITE_MODE_COLOR_DODGE: return apply_separable_blend (src, dst, blend_color_dodge); + case HB_PAINT_COMPOSITE_MODE_COLOR_BURN: return apply_separable_blend (src, dst, blend_color_burn); + case HB_PAINT_COMPOSITE_MODE_HARD_LIGHT: return apply_separable_blend (src, dst, blend_hard_light); + case HB_PAINT_COMPOSITE_MODE_SOFT_LIGHT: return apply_separable_blend (src, dst, blend_soft_light); + case HB_PAINT_COMPOSITE_MODE_DIFFERENCE: return apply_separable_blend (src, dst, blend_difference); + case HB_PAINT_COMPOSITE_MODE_EXCLUSION: return apply_separable_blend (src, dst, blend_exclusion); + + /* HSL blend modes */ + case HB_PAINT_COMPOSITE_MODE_HSL_HUE: + case HB_PAINT_COMPOSITE_MODE_HSL_SATURATION: + case HB_PAINT_COMPOSITE_MODE_HSL_COLOR: + case HB_PAINT_COMPOSITE_MODE_HSL_LUMINOSITY: + return apply_hsl_blend (src, dst, mode); + + default: + return src_over (src, dst); + } +} + +/* Composite src image onto dst image. * Both images must have the same extents and BGRA32 format. */ static void -composite_src_over (hb_raster_image_t *dst, - const hb_raster_image_t *src) +composite_images (hb_raster_image_t *dst, + const hb_raster_image_t *src, + hb_paint_composite_mode_t mode) { unsigned w = dst->extents.width; unsigned h = dst->extents.height; @@ -101,46 +409,7 @@ composite_src_over (hb_raster_image_t *dst, uint32_t *dp = reinterpret_cast (dst->buffer.arrayZ + y * stride); const uint32_t *sp = reinterpret_cast (src->buffer.arrayZ + y * stride); for (unsigned x = 0; x < w; x++) - dp[x] = src_over (sp[x], dp[x]); - } -} - -/* TODO: implement all 28 blend modes from hb_paint_composite_mode_t. - * For now, fall back to SRC_OVER for everything. */ -static void -composite_images (hb_raster_image_t *dst, - const hb_raster_image_t *src, - hb_paint_composite_mode_t mode) -{ - /* TODO: implement all 28 blend modes. For now only a few key ones. */ - if (mode == HB_PAINT_COMPOSITE_MODE_CLEAR) - { - unsigned stride = dst->extents.stride; - unsigned h = dst->extents.height; - /* Clear dst where src has alpha */ - for (unsigned y = 0; y < h; y++) - { - uint32_t *dp = reinterpret_cast (dst->buffer.arrayZ + y * stride); - const uint32_t *sp = reinterpret_cast (src->buffer.arrayZ + y * stride); - for (unsigned x = 0; x < dst->extents.width; x++) - if (sp[x] >> 24) - dp[x] = 0; - } - } - else if (mode == HB_PAINT_COMPOSITE_MODE_SRC) - { - unsigned stride = dst->extents.stride; - unsigned h = dst->extents.height; - memcpy (dst->buffer.arrayZ, src->buffer.arrayZ, stride * h); - } - else if (mode == HB_PAINT_COMPOSITE_MODE_DEST) - { - /* No-op: destination unchanged */ - } - else - { - /* Default: SRC_OVER (covers SRC_OVER and all unimplemented modes) */ - composite_src_over (dst, src); + dp[x] = composite_pixel (sp[x], dp[x], mode); } }