diff --git a/src/hb-raster-image.cc b/src/hb-raster-image.cc index 7b04b073c..8efc3ceb9 100644 --- a/src/hb-raster-image.cc +++ b/src/hb-raster-image.cc @@ -28,6 +28,331 @@ #include "hb-raster.hh" +#include + + +/* + * Image compositing + */ + +/* 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 hb_raster_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 functions: operate 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); + + 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 = blend_fn (usr, udr); + float bg = blend_fn (usg, udg); + float bb = blend_fn (usb, udb); + + 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); +} + +/* 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) +{ + 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); + } +} + +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); + + 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); +} + +/* 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 hb_raster_src_over (src, dst); + case HB_PAINT_COMPOSITE_MODE_DEST_OVER: + return hb_raster_src_over (dst, src); + case HB_PAINT_COMPOSITE_MODE_SRC_IN: + return hb_raster_alpha_mul (src, da); + case HB_PAINT_COMPOSITE_MODE_DEST_IN: + return hb_raster_alpha_mul (dst, sa); + case HB_PAINT_COMPOSITE_MODE_SRC_OUT: + return hb_raster_alpha_mul (src, 255 - da); + case HB_PAINT_COMPOSITE_MODE_DEST_OUT: + return hb_raster_alpha_mul (dst, 255 - sa); + case HB_PAINT_COMPOSITE_MODE_SRC_ATOP: + { + /* Fa=Da, Fb=1-Sa */ + uint32_t a = hb_raster_alpha_mul (src, da); + uint32_t b = hb_raster_alpha_mul (dst, 255 - sa); + uint8_t rb = (uint8_t) hb_min (255u, (unsigned) (a & 0xFF) + (b & 0xFF)); + uint8_t rg = (uint8_t) hb_min (255u, (unsigned) ((a >> 8) & 0xFF) + ((b >> 8) & 0xFF)); + uint8_t rr = (uint8_t) hb_min (255u, (unsigned) ((a >> 16) & 0xFF) + ((b >> 16) & 0xFF)); + uint8_t ra = (uint8_t) hb_min (255u, (unsigned) (a >> 24) + (b >> 24)); + return hb_raster_pack_pixel (rb, rg, rr, ra); + } + case HB_PAINT_COMPOSITE_MODE_DEST_ATOP: + { + uint32_t a = hb_raster_alpha_mul (dst, sa); + uint32_t b = hb_raster_alpha_mul (src, 255 - da); + uint8_t rb = (uint8_t) hb_min (255u, (unsigned) (a & 0xFF) + (b & 0xFF)); + uint8_t rg = (uint8_t) hb_min (255u, (unsigned) ((a >> 8) & 0xFF) + ((b >> 8) & 0xFF)); + uint8_t rr = (uint8_t) hb_min (255u, (unsigned) ((a >> 16) & 0xFF) + ((b >> 16) & 0xFF)); + uint8_t ra = (uint8_t) hb_min (255u, (unsigned) (a >> 24) + (b >> 24)); + return hb_raster_pack_pixel (rb, rg, rr, ra); + } + case HB_PAINT_COMPOSITE_MODE_XOR: + { + uint32_t a = hb_raster_alpha_mul (src, 255 - da); + uint32_t b = hb_raster_alpha_mul (dst, 255 - sa); + uint8_t rb = (uint8_t) hb_min (255u, (unsigned) (a & 0xFF) + (b & 0xFF)); + uint8_t rg = (uint8_t) hb_min (255u, (unsigned) ((a >> 8) & 0xFF) + ((b >> 8) & 0xFF)); + uint8_t rr = (uint8_t) hb_min (255u, (unsigned) ((a >> 16) & 0xFF) + ((b >> 16) & 0xFF)); + uint8_t ra = (uint8_t) hb_min (255u, (unsigned) (a >> 24) + (b >> 24)); + return hb_raster_pack_pixel (rb, rg, rr, ra); + } + 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 hb_raster_pack_pixel (rb, rg, rr, ra); + } + + 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); + + 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 hb_raster_src_over (src, dst); + } +} + +/* Composite src image onto dst image. + * Both images must have the same extents and BGRA32 format. */ +void +hb_raster_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; + unsigned stride = dst->extents.stride; + + 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 < w; x++) + dp[x] = composite_pixel (sp[x], dp[x], mode); + } +} + /* hb_raster_image_t */ diff --git a/src/hb-raster-paint.cc b/src/hb-raster-paint.cc index 97acc809e..1b4078e29 100644 --- a/src/hb-raster-paint.cc +++ b/src/hb-raster-paint.cc @@ -33,387 +33,21 @@ /* - * Pixel helpers + * Pixel helpers (paint-specific) */ -static inline uint8_t -div255 (unsigned a) -{ - return (uint8_t) ((a + 128 + ((a + 128) >> 8)) >> 8); -} - -/* SRC_OVER: premultiplied src over premultiplied dst. */ -static inline uint32_t -src_over (uint32_t src, uint32_t dst) -{ - uint8_t sa = (uint8_t) (src >> 24); - if (sa == 255) return src; - if (sa == 0) return dst; - unsigned inv_sa = 255 - sa; - uint8_t rb = div255 ((dst & 0xFF) * inv_sa) + (uint8_t) (src & 0xFF); - uint8_t rg = div255 (((dst >> 8) & 0xFF) * inv_sa) + (uint8_t) ((src >> 8) & 0xFF); - uint8_t rr = div255 (((dst >> 16) & 0xFF) * inv_sa) + (uint8_t) ((src >> 16) & 0xFF); - uint8_t ra = div255 (((dst >> 24) & 0xFF) * inv_sa) + sa; - return (uint32_t) rb | ((uint32_t) rg << 8) | ((uint32_t) rr << 16) | ((uint32_t) ra << 24); -} - -/* Scale a premultiplied pixel by an alpha [0,255]. */ -static inline uint32_t -alpha_mul (uint32_t px, unsigned a) -{ - if (a == 255) return px; - if (a == 0) return 0; - uint8_t rb = div255 ((px & 0xFF) * a); - uint8_t rg = div255 (((px >> 8) & 0xFF) * a); - uint8_t rr = div255 (((px >> 16) & 0xFF) * a); - uint8_t ra = div255 (((px >> 24) & 0xFF) * a); - return (uint32_t) rb | ((uint32_t) rg << 8) | ((uint32_t) rr << 16) | ((uint32_t) ra << 24); -} - /* Convert unpremultiplied hb_color_t (BGRA order) to premultiplied BGRA32 pixel. */ static inline uint32_t color_to_premul_pixel (hb_color_t color) { uint8_t a = hb_color_get_alpha (color); - uint8_t r = div255 (hb_color_get_red (color) * a); - uint8_t g = div255 (hb_color_get_green (color) * a); - uint8_t b = div255 (hb_color_get_blue (color) * a); + uint8_t r = hb_raster_div255 (hb_color_get_red (color) * a); + uint8_t g = hb_raster_div255 (hb_color_get_green (color) * a); + uint8_t b = hb_raster_div255 (hb_color_get_blue (color) * a); return (uint32_t) b | ((uint32_t) g << 8) | ((uint32_t) r << 16) | ((uint32_t) a << 24); } -/* - * Image compositing (operates on hb_raster_image_t) - */ - -/* 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_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; - unsigned stride = dst->extents.stride; - - 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 < w; x++) - dp[x] = composite_pixel (sp[x], dp[x], mode); - } -} - - /* * Paint callbacks */ @@ -547,7 +181,7 @@ hb_raster_paint_push_clip_glyph (hb_paint_funcs_t *pfuncs HB_UNUSED, uint8_t glyph_alpha = (x < mask_ext.width && y < mask_ext.height) ? mask_buf[y * mask_ext.stride + x] : 0; uint8_t old_alpha = old_clip.get_alpha (x, y); - new_clip.alpha[y * new_clip.stride + x] = div255 (glyph_alpha * old_alpha); + new_clip.alpha[y * new_clip.stride + x] = hb_raster_div255 (glyph_alpha * old_alpha); } hb_raster_draw_recycle_image (rdr, mask_img); @@ -664,7 +298,7 @@ hb_raster_paint_pop_group (hb_paint_funcs_t *pfuncs HB_UNUSED, hb_raster_image_t *dst = c->current_surface (); if (dst && src) - composite_images (dst, src, mode); + hb_raster_composite_images (dst, src, mode); c->release_surface (src); } @@ -689,7 +323,7 @@ hb_raster_paint_color (hb_paint_funcs_t *pfuncs HB_UNUSED, color = HB_COLOR (hb_color_get_blue (c->foreground), hb_color_get_green (c->foreground), hb_color_get_red (c->foreground), - div255 (hb_color_get_alpha (c->foreground) * + hb_raster_div255 (hb_color_get_alpha (c->foreground) * hb_color_get_alpha (color))); } @@ -707,8 +341,8 @@ hb_raster_paint_color (hb_paint_funcs_t *pfuncs HB_UNUSED, { uint8_t clip_alpha = clip.get_alpha (x, y); if (clip_alpha == 0) continue; - uint32_t src = alpha_mul (premul, clip_alpha); - row[x] = src_over (src, row[x]); + uint32_t src = hb_raster_alpha_mul (premul, clip_alpha); + row[x] = hb_raster_src_over (src, row[x]); } } } @@ -786,8 +420,8 @@ hb_raster_paint_image (hb_paint_funcs_t *pfuncs HB_UNUSED, continue; uint32_t src_px = reinterpret_cast (data)[iy * width + ix]; - src_px = alpha_mul (src_px, clip_alpha); - row[px] = src_over (src_px, row[px]); + src_px = hb_raster_alpha_mul (src_px, clip_alpha); + row[px] = hb_raster_src_over (src_px, row[px]); } } @@ -830,7 +464,7 @@ get_color_stops (hb_raster_paint_t *c, (*stops)[i].color = HB_COLOR (hb_color_get_blue (c->foreground), hb_color_get_green (c->foreground), hb_color_get_red (c->foreground), - div255 (hb_color_get_alpha (c->foreground) * + hb_raster_div255 (hb_color_get_alpha (c->foreground) * hb_color_get_alpha ((*stops)[i].color))); *count = len; @@ -1029,8 +663,8 @@ hb_raster_paint_linear_gradient (hb_paint_funcs_t *pfuncs HB_UNUSED, float proj_t = ((gx - gx0) * dx + (gy - gy0) * dy) * inv_denom; uint32_t src = evaluate_color_line (stops, len, proj_t, extend); - src = alpha_mul (src, clip_alpha); - row[px] = src_over (src, row[px]); + src = hb_raster_alpha_mul (src, clip_alpha); + row[px] = hb_raster_src_over (src, row[px]); } } } @@ -1146,8 +780,8 @@ hb_raster_paint_radial_gradient (hb_paint_funcs_t *pfuncs HB_UNUSED, } uint32_t src = evaluate_color_line (stops, len, grad_t, extend); - src = alpha_mul (src, clip_alpha); - row[px] = src_over (src, row[px]); + src = hb_raster_alpha_mul (src, clip_alpha); + row[px] = hb_raster_src_over (src, row[px]); } } } @@ -1229,8 +863,8 @@ hb_raster_paint_sweep_gradient (hb_paint_funcs_t *pfuncs HB_UNUSED, float grad_t = (angle - a0) * inv_angle_range; uint32_t src = evaluate_color_line (stops, len, grad_t, extend); - src = alpha_mul (src, clip_alpha); - row[px] = src_over (src, row[px]); + src = hb_raster_alpha_mul (src, clip_alpha); + row[px] = hb_raster_src_over (src, row[px]); } } } diff --git a/src/hb-raster.hh b/src/hb-raster.hh index b3f37b7bf..88ae27352 100644 --- a/src/hb-raster.hh +++ b/src/hb-raster.hh @@ -44,4 +44,56 @@ struct hb_raster_image_t }; +/* + * Shared pixel helpers (used by both paint and image compositing) + */ + +static inline uint8_t +hb_raster_div255 (unsigned a) +{ + return (uint8_t) ((a + 128 + ((a + 128) >> 8)) >> 8); +} + +static inline uint32_t +hb_raster_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); +} + +/* SRC_OVER: premultiplied src over premultiplied dst. */ +static inline uint32_t +hb_raster_src_over (uint32_t src, uint32_t dst) +{ + uint8_t sa = (uint8_t) (src >> 24); + if (sa == 255) return src; + if (sa == 0) return dst; + unsigned inv_sa = 255 - sa; + uint8_t rb = hb_raster_div255 ((dst & 0xFF) * inv_sa) + (uint8_t) (src & 0xFF); + uint8_t rg = hb_raster_div255 (((dst >> 8) & 0xFF) * inv_sa) + (uint8_t) ((src >> 8) & 0xFF); + uint8_t rr = hb_raster_div255 (((dst >> 16) & 0xFF) * inv_sa) + (uint8_t) ((src >> 16) & 0xFF); + uint8_t ra = hb_raster_div255 (((dst >> 24) & 0xFF) * inv_sa) + sa; + return (uint32_t) rb | ((uint32_t) rg << 8) | ((uint32_t) rr << 16) | ((uint32_t) ra << 24); +} + +/* Scale a premultiplied pixel by an alpha [0,255]. */ +static inline uint32_t +hb_raster_alpha_mul (uint32_t px, unsigned a) +{ + if (a == 255) return px; + if (a == 0) return 0; + uint8_t rb = hb_raster_div255 ((px & 0xFF) * a); + uint8_t rg = hb_raster_div255 (((px >> 8) & 0xFF) * a); + uint8_t rr = hb_raster_div255 (((px >> 16) & 0xFF) * a); + uint8_t ra = hb_raster_div255 (((px >> 24) & 0xFF) * a); + return (uint32_t) rb | ((uint32_t) rg << 8) | ((uint32_t) rr << 16) | ((uint32_t) ra << 24); +} + + +/* Composite src image onto dst. */ +HB_INTERNAL void +hb_raster_composite_images (hb_raster_image_t *dst, + const hb_raster_image_t *src, + hb_paint_composite_mode_t mode); + + #endif /* HB_RASTER_HH */