michael@0: /* -*- Mode: c; c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t; -*- */ michael@0: /* michael@0: * Copyright © 2010, 2012 Soren Sandmann Pedersen michael@0: * Copyright © 2010, 2012 Red Hat, Inc. michael@0: * michael@0: * Permission is hereby granted, free of charge, to any person obtaining a michael@0: * copy of this software and associated documentation files (the "Software"), michael@0: * to deal in the Software without restriction, including without limitation michael@0: * the rights to use, copy, modify, merge, publish, distribute, sublicense, michael@0: * and/or sell copies of the Software, and to permit persons to whom the michael@0: * Software is furnished to do so, subject to the following conditions: michael@0: * michael@0: * The above copyright notice and this permission notice (including the next michael@0: * paragraph) shall be included in all copies or substantial portions of the michael@0: * Software. michael@0: * michael@0: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR michael@0: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, michael@0: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL michael@0: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER michael@0: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING michael@0: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER michael@0: * DEALINGS IN THE SOFTWARE. michael@0: * michael@0: * Author: Soren Sandmann Pedersen (sandmann@cs.au.dk) michael@0: */ michael@0: michael@0: #ifdef HAVE_CONFIG_H michael@0: #include michael@0: #endif michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "pixman-private.h" michael@0: michael@0: /* Workaround for http://gcc.gnu.org/PR54965 */ michael@0: /* GCC 4.6 has problems with force_inline, so just use normal inline instead */ michael@0: #if defined(__GNUC__) && (__GNUC__ == 4) && (__GNUC_MINOR__ == 6) michael@0: #undef force_inline michael@0: #define force_inline __inline__ michael@0: #endif michael@0: michael@0: #define IS_ZERO(f) (-FLT_MIN < (f) && (f) < FLT_MIN) michael@0: michael@0: typedef float (* combine_channel_t) (float sa, float s, float da, float d); michael@0: michael@0: static force_inline void michael@0: combine_inner (pixman_bool_t component, michael@0: float *dest, const float *src, const float *mask, int n_pixels, michael@0: combine_channel_t combine_a, combine_channel_t combine_c) michael@0: { michael@0: int i; michael@0: michael@0: if (!mask) michael@0: { michael@0: for (i = 0; i < 4 * n_pixels; i += 4) michael@0: { michael@0: float sa = src[i + 0]; michael@0: float sr = src[i + 1]; michael@0: float sg = src[i + 2]; michael@0: float sb = src[i + 3]; michael@0: michael@0: float da = dest[i + 0]; michael@0: float dr = dest[i + 1]; michael@0: float dg = dest[i + 2]; michael@0: float db = dest[i + 3]; michael@0: michael@0: dest[i + 0] = combine_a (sa, sa, da, da); michael@0: dest[i + 1] = combine_c (sa, sr, da, dr); michael@0: dest[i + 2] = combine_c (sa, sg, da, dg); michael@0: dest[i + 3] = combine_c (sa, sb, da, db); michael@0: } michael@0: } michael@0: else michael@0: { michael@0: for (i = 0; i < 4 * n_pixels; i += 4) michael@0: { michael@0: float sa, sr, sg, sb; michael@0: float ma, mr, mg, mb; michael@0: float da, dr, dg, db; michael@0: michael@0: sa = src[i + 0]; michael@0: sr = src[i + 1]; michael@0: sg = src[i + 2]; michael@0: sb = src[i + 3]; michael@0: michael@0: if (component) michael@0: { michael@0: ma = mask[i + 0]; michael@0: mr = mask[i + 1]; michael@0: mg = mask[i + 2]; michael@0: mb = mask[i + 3]; michael@0: michael@0: sr *= mr; michael@0: sg *= mg; michael@0: sb *= mb; michael@0: michael@0: ma *= sa; michael@0: mr *= sa; michael@0: mg *= sa; michael@0: mb *= sa; michael@0: michael@0: sa = ma; michael@0: } michael@0: else michael@0: { michael@0: ma = mask[i + 0]; michael@0: michael@0: sa *= ma; michael@0: sr *= ma; michael@0: sg *= ma; michael@0: sb *= ma; michael@0: michael@0: ma = mr = mg = mb = sa; michael@0: } michael@0: michael@0: da = dest[i + 0]; michael@0: dr = dest[i + 1]; michael@0: dg = dest[i + 2]; michael@0: db = dest[i + 3]; michael@0: michael@0: dest[i + 0] = combine_a (ma, sa, da, da); michael@0: dest[i + 1] = combine_c (mr, sr, da, dr); michael@0: dest[i + 2] = combine_c (mg, sg, da, dg); michael@0: dest[i + 3] = combine_c (mb, sb, da, db); michael@0: } michael@0: } michael@0: } michael@0: michael@0: #define MAKE_COMBINER(name, component, combine_a, combine_c) \ michael@0: static void \ michael@0: combine_ ## name ## _float (pixman_implementation_t *imp, \ michael@0: pixman_op_t op, \ michael@0: float *dest, \ michael@0: const float *src, \ michael@0: const float *mask, \ michael@0: int n_pixels) \ michael@0: { \ michael@0: combine_inner (component, dest, src, mask, n_pixels, \ michael@0: combine_a, combine_c); \ michael@0: } michael@0: michael@0: #define MAKE_COMBINERS(name, combine_a, combine_c) \ michael@0: MAKE_COMBINER(name ## _ca, TRUE, combine_a, combine_c) \ michael@0: MAKE_COMBINER(name ## _u, FALSE, combine_a, combine_c) michael@0: michael@0: michael@0: /* michael@0: * Porter/Duff operators michael@0: */ michael@0: typedef enum michael@0: { michael@0: ZERO, michael@0: ONE, michael@0: SRC_ALPHA, michael@0: DEST_ALPHA, michael@0: INV_SA, michael@0: INV_DA, michael@0: SA_OVER_DA, michael@0: DA_OVER_SA, michael@0: INV_SA_OVER_DA, michael@0: INV_DA_OVER_SA, michael@0: ONE_MINUS_SA_OVER_DA, michael@0: ONE_MINUS_DA_OVER_SA, michael@0: ONE_MINUS_INV_DA_OVER_SA, michael@0: ONE_MINUS_INV_SA_OVER_DA michael@0: } combine_factor_t; michael@0: michael@0: #define CLAMP(f) \ michael@0: (((f) < 0)? 0 : (((f) > 1.0) ? 1.0 : (f))) michael@0: michael@0: static force_inline float michael@0: get_factor (combine_factor_t factor, float sa, float da) michael@0: { michael@0: float f = -1; michael@0: michael@0: switch (factor) michael@0: { michael@0: case ZERO: michael@0: f = 0.0f; michael@0: break; michael@0: michael@0: case ONE: michael@0: f = 1.0f; michael@0: break; michael@0: michael@0: case SRC_ALPHA: michael@0: f = sa; michael@0: break; michael@0: michael@0: case DEST_ALPHA: michael@0: f = da; michael@0: break; michael@0: michael@0: case INV_SA: michael@0: f = 1 - sa; michael@0: break; michael@0: michael@0: case INV_DA: michael@0: f = 1 - da; michael@0: break; michael@0: michael@0: case SA_OVER_DA: michael@0: if (IS_ZERO (da)) michael@0: f = 1.0f; michael@0: else michael@0: f = CLAMP (sa / da); michael@0: break; michael@0: michael@0: case DA_OVER_SA: michael@0: if (IS_ZERO (sa)) michael@0: f = 1.0f; michael@0: else michael@0: f = CLAMP (da / sa); michael@0: break; michael@0: michael@0: case INV_SA_OVER_DA: michael@0: if (IS_ZERO (da)) michael@0: f = 1.0f; michael@0: else michael@0: f = CLAMP ((1.0f - sa) / da); michael@0: break; michael@0: michael@0: case INV_DA_OVER_SA: michael@0: if (IS_ZERO (sa)) michael@0: f = 1.0f; michael@0: else michael@0: f = CLAMP ((1.0f - da) / sa); michael@0: break; michael@0: michael@0: case ONE_MINUS_SA_OVER_DA: michael@0: if (IS_ZERO (da)) michael@0: f = 0.0f; michael@0: else michael@0: f = CLAMP (1.0f - sa / da); michael@0: break; michael@0: michael@0: case ONE_MINUS_DA_OVER_SA: michael@0: if (IS_ZERO (sa)) michael@0: f = 0.0f; michael@0: else michael@0: f = CLAMP (1.0f - da / sa); michael@0: break; michael@0: michael@0: case ONE_MINUS_INV_DA_OVER_SA: michael@0: if (IS_ZERO (sa)) michael@0: f = 0.0f; michael@0: else michael@0: f = CLAMP (1.0f - (1.0f - da) / sa); michael@0: break; michael@0: michael@0: case ONE_MINUS_INV_SA_OVER_DA: michael@0: if (IS_ZERO (da)) michael@0: f = 0.0f; michael@0: else michael@0: f = CLAMP (1.0f - (1.0f - sa) / da); michael@0: break; michael@0: } michael@0: michael@0: return f; michael@0: } michael@0: michael@0: #define MAKE_PD_COMBINERS(name, a, b) \ michael@0: static float force_inline \ michael@0: pd_combine_ ## name (float sa, float s, float da, float d) \ michael@0: { \ michael@0: const float fa = get_factor (a, sa, da); \ michael@0: const float fb = get_factor (b, sa, da); \ michael@0: \ michael@0: return MIN (1.0f, s * fa + d * fb); \ michael@0: } \ michael@0: \ michael@0: MAKE_COMBINERS(name, pd_combine_ ## name, pd_combine_ ## name) michael@0: michael@0: MAKE_PD_COMBINERS (clear, ZERO, ZERO) michael@0: MAKE_PD_COMBINERS (src, ONE, ZERO) michael@0: MAKE_PD_COMBINERS (dst, ZERO, ONE) michael@0: MAKE_PD_COMBINERS (over, ONE, INV_SA) michael@0: MAKE_PD_COMBINERS (over_reverse, INV_DA, ONE) michael@0: MAKE_PD_COMBINERS (in, DEST_ALPHA, ZERO) michael@0: MAKE_PD_COMBINERS (in_reverse, ZERO, SRC_ALPHA) michael@0: MAKE_PD_COMBINERS (out, INV_DA, ZERO) michael@0: MAKE_PD_COMBINERS (out_reverse, ZERO, INV_SA) michael@0: MAKE_PD_COMBINERS (atop, DEST_ALPHA, INV_SA) michael@0: MAKE_PD_COMBINERS (atop_reverse, INV_DA, SRC_ALPHA) michael@0: MAKE_PD_COMBINERS (xor, INV_DA, INV_SA) michael@0: MAKE_PD_COMBINERS (add, ONE, ONE) michael@0: michael@0: MAKE_PD_COMBINERS (saturate, INV_DA_OVER_SA, ONE) michael@0: michael@0: MAKE_PD_COMBINERS (disjoint_clear, ZERO, ZERO) michael@0: MAKE_PD_COMBINERS (disjoint_src, ONE, ZERO) michael@0: MAKE_PD_COMBINERS (disjoint_dst, ZERO, ONE) michael@0: MAKE_PD_COMBINERS (disjoint_over, ONE, INV_SA_OVER_DA) michael@0: MAKE_PD_COMBINERS (disjoint_over_reverse, INV_DA_OVER_SA, ONE) michael@0: MAKE_PD_COMBINERS (disjoint_in, ONE_MINUS_INV_DA_OVER_SA, ZERO) michael@0: MAKE_PD_COMBINERS (disjoint_in_reverse, ZERO, ONE_MINUS_INV_SA_OVER_DA) michael@0: MAKE_PD_COMBINERS (disjoint_out, INV_DA_OVER_SA, ZERO) michael@0: MAKE_PD_COMBINERS (disjoint_out_reverse, ZERO, INV_SA_OVER_DA) michael@0: MAKE_PD_COMBINERS (disjoint_atop, ONE_MINUS_INV_DA_OVER_SA, INV_SA_OVER_DA) michael@0: MAKE_PD_COMBINERS (disjoint_atop_reverse, INV_DA_OVER_SA, ONE_MINUS_INV_SA_OVER_DA) michael@0: MAKE_PD_COMBINERS (disjoint_xor, INV_DA_OVER_SA, INV_SA_OVER_DA) michael@0: michael@0: MAKE_PD_COMBINERS (conjoint_clear, ZERO, ZERO) michael@0: MAKE_PD_COMBINERS (conjoint_src, ONE, ZERO) michael@0: MAKE_PD_COMBINERS (conjoint_dst, ZERO, ONE) michael@0: MAKE_PD_COMBINERS (conjoint_over, ONE, ONE_MINUS_SA_OVER_DA) michael@0: MAKE_PD_COMBINERS (conjoint_over_reverse, ONE_MINUS_DA_OVER_SA, ONE) michael@0: MAKE_PD_COMBINERS (conjoint_in, DA_OVER_SA, ZERO) michael@0: MAKE_PD_COMBINERS (conjoint_in_reverse, ZERO, SA_OVER_DA) michael@0: MAKE_PD_COMBINERS (conjoint_out, ONE_MINUS_DA_OVER_SA, ZERO) michael@0: MAKE_PD_COMBINERS (conjoint_out_reverse, ZERO, ONE_MINUS_SA_OVER_DA) michael@0: MAKE_PD_COMBINERS (conjoint_atop, DA_OVER_SA, ONE_MINUS_SA_OVER_DA) michael@0: MAKE_PD_COMBINERS (conjoint_atop_reverse, ONE_MINUS_DA_OVER_SA, SA_OVER_DA) michael@0: MAKE_PD_COMBINERS (conjoint_xor, ONE_MINUS_DA_OVER_SA, ONE_MINUS_SA_OVER_DA) michael@0: michael@0: /* michael@0: * PDF blend modes: michael@0: * michael@0: * The following blend modes have been taken from the PDF ISO 32000 michael@0: * specification, which at this point in time is available from michael@0: * http://www.adobe.com/devnet/acrobat/pdfs/PDF32000_2008.pdf michael@0: * The relevant chapters are 11.3.5 and 11.3.6. michael@0: * The formula for computing the final pixel color given in 11.3.6 is: michael@0: * αr × Cr = (1 – αs) × αb × Cb + (1 – αb) × αs × Cs + αb × αs × B(Cb, Cs) michael@0: * with B() being the blend function. michael@0: * Note that OVER is a special case of this operation, using B(Cb, Cs) = Cs michael@0: * michael@0: * These blend modes should match the SVG filter draft specification, as michael@0: * it has been designed to mirror ISO 32000. Note that at the current point michael@0: * no released draft exists that shows this, as the formulas have not been michael@0: * updated yet after the release of ISO 32000. michael@0: * michael@0: * The default implementation here uses the PDF_SEPARABLE_BLEND_MODE and michael@0: * PDF_NON_SEPARABLE_BLEND_MODE macros, which take the blend function as an michael@0: * argument. Note that this implementation operates on premultiplied colors, michael@0: * while the PDF specification does not. Therefore the code uses the formula michael@0: * ar.Cra = (1 – as) . Dca + (1 – ad) . Sca + B(Dca, ad, Sca, as) michael@0: */ michael@0: michael@0: #define MAKE_SEPARABLE_PDF_COMBINERS(name) \ michael@0: static force_inline float \ michael@0: combine_ ## name ## _a (float sa, float s, float da, float d) \ michael@0: { \ michael@0: return da + sa - da * sa; \ michael@0: } \ michael@0: \ michael@0: static force_inline float \ michael@0: combine_ ## name ## _c (float sa, float s, float da, float d) \ michael@0: { \ michael@0: float f = (1 - sa) * d + (1 - da) * s; \ michael@0: \ michael@0: return f + blend_ ## name (sa, s, da, d); \ michael@0: } \ michael@0: \ michael@0: MAKE_COMBINERS (name, combine_ ## name ## _a, combine_ ## name ## _c) michael@0: michael@0: static force_inline float michael@0: blend_multiply (float sa, float s, float da, float d) michael@0: { michael@0: return d * s; michael@0: } michael@0: michael@0: static force_inline float michael@0: blend_screen (float sa, float s, float da, float d) michael@0: { michael@0: return d * sa + s * da - s * d; michael@0: } michael@0: michael@0: static force_inline float michael@0: blend_overlay (float sa, float s, float da, float d) michael@0: { michael@0: if (2 * d < da) michael@0: return 2 * s * d; michael@0: else michael@0: return sa * da - 2 * (da - d) * (sa - s); michael@0: } michael@0: michael@0: static force_inline float michael@0: blend_darken (float sa, float s, float da, float d) michael@0: { michael@0: s = s * da; michael@0: d = d * sa; michael@0: michael@0: if (s > d) michael@0: return d; michael@0: else michael@0: return s; michael@0: } michael@0: michael@0: static force_inline float michael@0: blend_lighten (float sa, float s, float da, float d) michael@0: { michael@0: s = s * da; michael@0: d = d * sa; michael@0: michael@0: if (s > d) michael@0: return s; michael@0: else michael@0: return d; michael@0: } michael@0: michael@0: static force_inline float michael@0: blend_color_dodge (float sa, float s, float da, float d) michael@0: { michael@0: if (IS_ZERO (d)) michael@0: return 0.0f; michael@0: else if (d * sa >= sa * da - s * da) michael@0: return sa * da; michael@0: else if (IS_ZERO (sa - s)) michael@0: return sa * da; michael@0: else michael@0: return sa * sa * d / (sa - s); michael@0: } michael@0: michael@0: static force_inline float michael@0: blend_color_burn (float sa, float s, float da, float d) michael@0: { michael@0: if (d >= da) michael@0: return sa * da; michael@0: else if (sa * (da - d) >= s * da) michael@0: return 0.0f; michael@0: else if (IS_ZERO (s)) michael@0: return 0.0f; michael@0: else michael@0: return sa * (da - sa * (da - d) / s); michael@0: } michael@0: michael@0: static force_inline float michael@0: blend_hard_light (float sa, float s, float da, float d) michael@0: { michael@0: if (2 * s < sa) michael@0: return 2 * s * d; michael@0: else michael@0: return sa * da - 2 * (da - d) * (sa - s); michael@0: } michael@0: michael@0: static force_inline float michael@0: blend_soft_light (float sa, float s, float da, float d) michael@0: { michael@0: if (2 * s < sa) michael@0: { michael@0: if (IS_ZERO (da)) michael@0: return d * sa; michael@0: else michael@0: return d * sa - d * (da - d) * (sa - 2 * s) / da; michael@0: } michael@0: else michael@0: { michael@0: if (IS_ZERO (da)) michael@0: { michael@0: return 0.0f; michael@0: } michael@0: else michael@0: { michael@0: if (4 * d <= da) michael@0: return d * sa + (2 * s - sa) * d * ((16 * d / da - 12) * d / da + 3); michael@0: else michael@0: return d * sa + (sqrtf (d * da) - d) * (2 * s - sa); michael@0: } michael@0: } michael@0: } michael@0: michael@0: static force_inline float michael@0: blend_difference (float sa, float s, float da, float d) michael@0: { michael@0: float dsa = d * sa; michael@0: float sda = s * da; michael@0: michael@0: if (sda < dsa) michael@0: return dsa - sda; michael@0: else michael@0: return sda - dsa; michael@0: } michael@0: michael@0: static force_inline float michael@0: blend_exclusion (float sa, float s, float da, float d) michael@0: { michael@0: return s * da + d * sa - 2 * d * s; michael@0: } michael@0: michael@0: MAKE_SEPARABLE_PDF_COMBINERS (multiply) michael@0: MAKE_SEPARABLE_PDF_COMBINERS (screen) michael@0: MAKE_SEPARABLE_PDF_COMBINERS (overlay) michael@0: MAKE_SEPARABLE_PDF_COMBINERS (darken) michael@0: MAKE_SEPARABLE_PDF_COMBINERS (lighten) michael@0: MAKE_SEPARABLE_PDF_COMBINERS (color_dodge) michael@0: MAKE_SEPARABLE_PDF_COMBINERS (color_burn) michael@0: MAKE_SEPARABLE_PDF_COMBINERS (hard_light) michael@0: MAKE_SEPARABLE_PDF_COMBINERS (soft_light) michael@0: MAKE_SEPARABLE_PDF_COMBINERS (difference) michael@0: MAKE_SEPARABLE_PDF_COMBINERS (exclusion) michael@0: michael@0: /* michael@0: * PDF nonseperable blend modes. michael@0: * michael@0: * These are implemented using the following functions to operate in Hsl michael@0: * space, with Cmax, Cmid, Cmin referring to the max, mid and min value michael@0: * of the red, green and blue components. michael@0: * michael@0: * LUM (C) = 0.3 × Cred + 0.59 × Cgreen + 0.11 × Cblue michael@0: * michael@0: * clip_color (C): michael@0: * l = LUM (C) michael@0: * min = Cmin michael@0: * max = Cmax michael@0: * if n < 0.0 michael@0: * C = l + (((C – l) × l) ⁄ (l – min)) michael@0: * if x > 1.0 michael@0: * C = l + (((C – l) × (1 – l)) (max – l)) michael@0: * return C michael@0: * michael@0: * set_lum (C, l): michael@0: * d = l – LUM (C) michael@0: * C += d michael@0: * return clip_color (C) michael@0: * michael@0: * SAT (C) = CH_MAX (C) - CH_MIN (C) michael@0: * michael@0: * set_sat (C, s): michael@0: * if Cmax > Cmin michael@0: * Cmid = ( ( ( Cmid – Cmin ) × s ) ⁄ ( Cmax – Cmin ) ) michael@0: * Cmax = s michael@0: * else michael@0: * Cmid = Cmax = 0.0 michael@0: * Cmin = 0.0 michael@0: * return C michael@0: */ michael@0: michael@0: /* For premultiplied colors, we need to know what happens when C is michael@0: * multiplied by a real number. LUM and SAT are linear: michael@0: * michael@0: * LUM (r × C) = r × LUM (C) SAT (r × C) = r × SAT (C) michael@0: * michael@0: * If we extend clip_color with an extra argument a and change michael@0: * michael@0: * if x >= 1.0 michael@0: * michael@0: * into michael@0: * michael@0: * if x >= a michael@0: * michael@0: * then clip_color is also linear: michael@0: * michael@0: * r * clip_color (C, a) = clip_color (r_c, ra); michael@0: * michael@0: * for positive r. michael@0: * michael@0: * Similarly, we can extend set_lum with an extra argument that is just passed michael@0: * on to clip_color: michael@0: * michael@0: * r × set_lum ( C, l, a) michael@0: * michael@0: * = r × clip_color ( C + l - LUM (C), a) michael@0: * michael@0: * = clip_color ( r * C + r × l - LUM (r × C), r * a) michael@0: * michael@0: * = set_lum ( r * C, r * l, r * a) michael@0: * michael@0: * Finally, set_sat: michael@0: * michael@0: * r * set_sat (C, s) = set_sat (x * C, r * s) michael@0: * michael@0: * The above holds for all non-zero x because they x'es in the fraction for michael@0: * C_mid cancel out. Specifically, it holds for x = r: michael@0: * michael@0: * r * set_sat (C, s) = set_sat (r_c, rs) michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * So, for the non-separable PDF blend modes, we have (using s, d for michael@0: * non-premultiplied colors, and S, D for premultiplied: michael@0: * michael@0: * Color: michael@0: * michael@0: * a_s * a_d * B(s, d) michael@0: * = a_s * a_d * set_lum (S/a_s, LUM (D/a_d), 1) michael@0: * = set_lum (S * a_d, a_s * LUM (D), a_s * a_d) michael@0: * michael@0: * michael@0: * Luminosity: michael@0: * michael@0: * a_s * a_d * B(s, d) michael@0: * = a_s * a_d * set_lum (D/a_d, LUM(S/a_s), 1) michael@0: * = set_lum (a_s * D, a_d * LUM(S), a_s * a_d) michael@0: * michael@0: * michael@0: * Saturation: michael@0: * michael@0: * a_s * a_d * B(s, d) michael@0: * = a_s * a_d * set_lum (set_sat (D/a_d, SAT (S/a_s)), LUM (D/a_d), 1) michael@0: * = set_lum (a_s * a_d * set_sat (D/a_d, SAT (S/a_s)), michael@0: * a_s * LUM (D), a_s * a_d) michael@0: * = set_lum (set_sat (a_s * D, a_d * SAT (S), a_s * LUM (D), a_s * a_d)) michael@0: * michael@0: * Hue: michael@0: * michael@0: * a_s * a_d * B(s, d) michael@0: * = a_s * a_d * set_lum (set_sat (S/a_s, SAT (D/a_d)), LUM (D/a_d), 1) michael@0: * = set_lum (set_sat (a_d * S, a_s * SAT (D)), a_s * LUM (D), a_s * a_d) michael@0: * michael@0: */ michael@0: michael@0: typedef struct michael@0: { michael@0: float r; michael@0: float g; michael@0: float b; michael@0: } rgb_t; michael@0: michael@0: static force_inline float michael@0: minf (float a, float b) michael@0: { michael@0: return a < b? a : b; michael@0: } michael@0: michael@0: static force_inline float michael@0: maxf (float a, float b) michael@0: { michael@0: return a > b? a : b; michael@0: } michael@0: michael@0: static force_inline float michael@0: channel_min (const rgb_t *c) michael@0: { michael@0: return minf (minf (c->r, c->g), c->b); michael@0: } michael@0: michael@0: static force_inline float michael@0: channel_max (const rgb_t *c) michael@0: { michael@0: return maxf (maxf (c->r, c->g), c->b); michael@0: } michael@0: michael@0: static force_inline float michael@0: get_lum (const rgb_t *c) michael@0: { michael@0: return c->r * 0.3f + c->g * 0.59f + c->b * 0.11f; michael@0: } michael@0: michael@0: static force_inline float michael@0: get_sat (const rgb_t *c) michael@0: { michael@0: return channel_max (c) - channel_min (c); michael@0: } michael@0: michael@0: static void michael@0: clip_color (rgb_t *color, float a) michael@0: { michael@0: float l = get_lum (color); michael@0: float n = channel_min (color); michael@0: float x = channel_max (color); michael@0: float t; michael@0: michael@0: if (n < 0.0f) michael@0: { michael@0: t = l - n; michael@0: if (IS_ZERO (t)) michael@0: { michael@0: color->r = 0.0f; michael@0: color->g = 0.0f; michael@0: color->b = 0.0f; michael@0: } michael@0: else michael@0: { michael@0: color->r = l + (((color->r - l) * l) / t); michael@0: color->g = l + (((color->g - l) * l) / t); michael@0: color->b = l + (((color->b - l) * l) / t); michael@0: } michael@0: } michael@0: if (x > a) michael@0: { michael@0: t = x - l; michael@0: if (IS_ZERO (t)) michael@0: { michael@0: color->r = a; michael@0: color->g = a; michael@0: color->b = a; michael@0: } michael@0: else michael@0: { michael@0: color->r = l + (((color->r - l) * (a - l) / t)); michael@0: color->g = l + (((color->g - l) * (a - l) / t)); michael@0: color->b = l + (((color->b - l) * (a - l) / t)); michael@0: } michael@0: } michael@0: } michael@0: michael@0: static void michael@0: set_lum (rgb_t *color, float sa, float l) michael@0: { michael@0: float d = l - get_lum (color); michael@0: michael@0: color->r = color->r + d; michael@0: color->g = color->g + d; michael@0: color->b = color->b + d; michael@0: michael@0: clip_color (color, sa); michael@0: } michael@0: michael@0: static void michael@0: set_sat (rgb_t *src, float sat) michael@0: { michael@0: float *max, *mid, *min; michael@0: float t; michael@0: michael@0: if (src->r > src->g) michael@0: { michael@0: if (src->r > src->b) michael@0: { michael@0: max = &(src->r); michael@0: michael@0: if (src->g > src->b) michael@0: { michael@0: mid = &(src->g); michael@0: min = &(src->b); michael@0: } michael@0: else michael@0: { michael@0: mid = &(src->b); michael@0: min = &(src->g); michael@0: } michael@0: } michael@0: else michael@0: { michael@0: max = &(src->b); michael@0: mid = &(src->r); michael@0: min = &(src->g); michael@0: } michael@0: } michael@0: else michael@0: { michael@0: if (src->r > src->b) michael@0: { michael@0: max = &(src->g); michael@0: mid = &(src->r); michael@0: min = &(src->b); michael@0: } michael@0: else michael@0: { michael@0: min = &(src->r); michael@0: michael@0: if (src->g > src->b) michael@0: { michael@0: max = &(src->g); michael@0: mid = &(src->b); michael@0: } michael@0: else michael@0: { michael@0: max = &(src->b); michael@0: mid = &(src->g); michael@0: } michael@0: } michael@0: } michael@0: michael@0: t = *max - *min; michael@0: michael@0: if (IS_ZERO (t)) michael@0: { michael@0: *mid = *max = 0.0f; michael@0: } michael@0: else michael@0: { michael@0: *mid = ((*mid - *min) * sat) / t; michael@0: *max = sat; michael@0: } michael@0: michael@0: *min = 0.0f; michael@0: } michael@0: michael@0: /* michael@0: * Hue: michael@0: * B(Cb, Cs) = set_lum (set_sat (Cs, SAT (Cb)), LUM (Cb)) michael@0: */ michael@0: static force_inline void michael@0: blend_hsl_hue (rgb_t *res, michael@0: const rgb_t *dest, float da, michael@0: const rgb_t *src, float sa) michael@0: { michael@0: res->r = src->r * da; michael@0: res->g = src->g * da; michael@0: res->b = src->b * da; michael@0: michael@0: set_sat (res, get_sat (dest) * sa); michael@0: set_lum (res, sa * da, get_lum (dest) * sa); michael@0: } michael@0: michael@0: /* michael@0: * Saturation: michael@0: * B(Cb, Cs) = set_lum (set_sat (Cb, SAT (Cs)), LUM (Cb)) michael@0: */ michael@0: static force_inline void michael@0: blend_hsl_saturation (rgb_t *res, michael@0: const rgb_t *dest, float da, michael@0: const rgb_t *src, float sa) michael@0: { michael@0: res->r = dest->r * sa; michael@0: res->g = dest->g * sa; michael@0: res->b = dest->b * sa; michael@0: michael@0: set_sat (res, get_sat (src) * da); michael@0: set_lum (res, sa * da, get_lum (dest) * sa); michael@0: } michael@0: michael@0: /* michael@0: * Color: michael@0: * B(Cb, Cs) = set_lum (Cs, LUM (Cb)) michael@0: */ michael@0: static force_inline void michael@0: blend_hsl_color (rgb_t *res, michael@0: const rgb_t *dest, float da, michael@0: const rgb_t *src, float sa) michael@0: { michael@0: res->r = src->r * da; michael@0: res->g = src->g * da; michael@0: res->b = src->b * da; michael@0: michael@0: set_lum (res, sa * da, get_lum (dest) * sa); michael@0: } michael@0: michael@0: /* michael@0: * Luminosity: michael@0: * B(Cb, Cs) = set_lum (Cb, LUM (Cs)) michael@0: */ michael@0: static force_inline void michael@0: blend_hsl_luminosity (rgb_t *res, michael@0: const rgb_t *dest, float da, michael@0: const rgb_t *src, float sa) michael@0: { michael@0: res->r = dest->r * sa; michael@0: res->g = dest->g * sa; michael@0: res->b = dest->b * sa; michael@0: michael@0: set_lum (res, sa * da, get_lum (src) * da); michael@0: } michael@0: michael@0: #define MAKE_NON_SEPARABLE_PDF_COMBINERS(name) \ michael@0: static void \ michael@0: combine_ ## name ## _u_float (pixman_implementation_t *imp, \ michael@0: pixman_op_t op, \ michael@0: float *dest, \ michael@0: const float *src, \ michael@0: const float *mask, \ michael@0: int n_pixels) \ michael@0: { \ michael@0: int i; \ michael@0: \ michael@0: for (i = 0; i < 4 * n_pixels; i += 4) \ michael@0: { \ michael@0: float sa, da; \ michael@0: rgb_t sc, dc, rc; \ michael@0: \ michael@0: sa = src[i + 0]; \ michael@0: sc.r = src[i + 1]; \ michael@0: sc.g = src[i + 2]; \ michael@0: sc.b = src[i + 3]; \ michael@0: \ michael@0: da = dest[i + 0]; \ michael@0: dc.r = dest[i + 1]; \ michael@0: dc.g = dest[i + 2]; \ michael@0: dc.b = dest[i + 3]; \ michael@0: \ michael@0: if (mask) \ michael@0: { \ michael@0: float ma = mask[i + 0]; \ michael@0: \ michael@0: /* Component alpha is not supported for HSL modes */ \ michael@0: sa *= ma; \ michael@0: sc.r *= ma; \ michael@0: sc.g *= ma; \ michael@0: sc.g *= ma; \ michael@0: } \ michael@0: \ michael@0: blend_ ## name (&rc, &dc, da, &sc, sa); \ michael@0: \ michael@0: dest[i + 0] = sa + da - sa * da; \ michael@0: dest[i + 1] = (1 - sa) * dc.r + (1 - da) * sc.r + rc.r; \ michael@0: dest[i + 2] = (1 - sa) * dc.g + (1 - da) * sc.g + rc.g; \ michael@0: dest[i + 3] = (1 - sa) * dc.b + (1 - da) * sc.b + rc.b; \ michael@0: } \ michael@0: } michael@0: michael@0: MAKE_NON_SEPARABLE_PDF_COMBINERS(hsl_hue) michael@0: MAKE_NON_SEPARABLE_PDF_COMBINERS(hsl_saturation) michael@0: MAKE_NON_SEPARABLE_PDF_COMBINERS(hsl_color) michael@0: MAKE_NON_SEPARABLE_PDF_COMBINERS(hsl_luminosity) michael@0: michael@0: void michael@0: _pixman_setup_combiner_functions_float (pixman_implementation_t *imp) michael@0: { michael@0: /* Unified alpha */ michael@0: imp->combine_float[PIXMAN_OP_CLEAR] = combine_clear_u_float; michael@0: imp->combine_float[PIXMAN_OP_SRC] = combine_src_u_float; michael@0: imp->combine_float[PIXMAN_OP_DST] = combine_dst_u_float; michael@0: imp->combine_float[PIXMAN_OP_OVER] = combine_over_u_float; michael@0: imp->combine_float[PIXMAN_OP_OVER_REVERSE] = combine_over_reverse_u_float; michael@0: imp->combine_float[PIXMAN_OP_IN] = combine_in_u_float; michael@0: imp->combine_float[PIXMAN_OP_IN_REVERSE] = combine_in_reverse_u_float; michael@0: imp->combine_float[PIXMAN_OP_OUT] = combine_out_u_float; michael@0: imp->combine_float[PIXMAN_OP_OUT_REVERSE] = combine_out_reverse_u_float; michael@0: imp->combine_float[PIXMAN_OP_ATOP] = combine_atop_u_float; michael@0: imp->combine_float[PIXMAN_OP_ATOP_REVERSE] = combine_atop_reverse_u_float; michael@0: imp->combine_float[PIXMAN_OP_XOR] = combine_xor_u_float; michael@0: imp->combine_float[PIXMAN_OP_ADD] = combine_add_u_float; michael@0: imp->combine_float[PIXMAN_OP_SATURATE] = combine_saturate_u_float; michael@0: michael@0: /* Disjoint, unified */ michael@0: imp->combine_float[PIXMAN_OP_DISJOINT_CLEAR] = combine_disjoint_clear_u_float; michael@0: imp->combine_float[PIXMAN_OP_DISJOINT_SRC] = combine_disjoint_src_u_float; michael@0: imp->combine_float[PIXMAN_OP_DISJOINT_DST] = combine_disjoint_dst_u_float; michael@0: imp->combine_float[PIXMAN_OP_DISJOINT_OVER] = combine_disjoint_over_u_float; michael@0: imp->combine_float[PIXMAN_OP_DISJOINT_OVER_REVERSE] = combine_disjoint_over_reverse_u_float; michael@0: imp->combine_float[PIXMAN_OP_DISJOINT_IN] = combine_disjoint_in_u_float; michael@0: imp->combine_float[PIXMAN_OP_DISJOINT_IN_REVERSE] = combine_disjoint_in_reverse_u_float; michael@0: imp->combine_float[PIXMAN_OP_DISJOINT_OUT] = combine_disjoint_out_u_float; michael@0: imp->combine_float[PIXMAN_OP_DISJOINT_OUT_REVERSE] = combine_disjoint_out_reverse_u_float; michael@0: imp->combine_float[PIXMAN_OP_DISJOINT_ATOP] = combine_disjoint_atop_u_float; michael@0: imp->combine_float[PIXMAN_OP_DISJOINT_ATOP_REVERSE] = combine_disjoint_atop_reverse_u_float; michael@0: imp->combine_float[PIXMAN_OP_DISJOINT_XOR] = combine_disjoint_xor_u_float; michael@0: michael@0: /* Conjoint, unified */ michael@0: imp->combine_float[PIXMAN_OP_CONJOINT_CLEAR] = combine_conjoint_clear_u_float; michael@0: imp->combine_float[PIXMAN_OP_CONJOINT_SRC] = combine_conjoint_src_u_float; michael@0: imp->combine_float[PIXMAN_OP_CONJOINT_DST] = combine_conjoint_dst_u_float; michael@0: imp->combine_float[PIXMAN_OP_CONJOINT_OVER] = combine_conjoint_over_u_float; michael@0: imp->combine_float[PIXMAN_OP_CONJOINT_OVER_REVERSE] = combine_conjoint_over_reverse_u_float; michael@0: imp->combine_float[PIXMAN_OP_CONJOINT_IN] = combine_conjoint_in_u_float; michael@0: imp->combine_float[PIXMAN_OP_CONJOINT_IN_REVERSE] = combine_conjoint_in_reverse_u_float; michael@0: imp->combine_float[PIXMAN_OP_CONJOINT_OUT] = combine_conjoint_out_u_float; michael@0: imp->combine_float[PIXMAN_OP_CONJOINT_OUT_REVERSE] = combine_conjoint_out_reverse_u_float; michael@0: imp->combine_float[PIXMAN_OP_CONJOINT_ATOP] = combine_conjoint_atop_u_float; michael@0: imp->combine_float[PIXMAN_OP_CONJOINT_ATOP_REVERSE] = combine_conjoint_atop_reverse_u_float; michael@0: imp->combine_float[PIXMAN_OP_CONJOINT_XOR] = combine_conjoint_xor_u_float; michael@0: michael@0: /* PDF operators, unified */ michael@0: imp->combine_float[PIXMAN_OP_MULTIPLY] = combine_multiply_u_float; michael@0: imp->combine_float[PIXMAN_OP_SCREEN] = combine_screen_u_float; michael@0: imp->combine_float[PIXMAN_OP_OVERLAY] = combine_overlay_u_float; michael@0: imp->combine_float[PIXMAN_OP_DARKEN] = combine_darken_u_float; michael@0: imp->combine_float[PIXMAN_OP_LIGHTEN] = combine_lighten_u_float; michael@0: imp->combine_float[PIXMAN_OP_COLOR_DODGE] = combine_color_dodge_u_float; michael@0: imp->combine_float[PIXMAN_OP_COLOR_BURN] = combine_color_burn_u_float; michael@0: imp->combine_float[PIXMAN_OP_HARD_LIGHT] = combine_hard_light_u_float; michael@0: imp->combine_float[PIXMAN_OP_SOFT_LIGHT] = combine_soft_light_u_float; michael@0: imp->combine_float[PIXMAN_OP_DIFFERENCE] = combine_difference_u_float; michael@0: imp->combine_float[PIXMAN_OP_EXCLUSION] = combine_exclusion_u_float; michael@0: michael@0: imp->combine_float[PIXMAN_OP_HSL_HUE] = combine_hsl_hue_u_float; michael@0: imp->combine_float[PIXMAN_OP_HSL_SATURATION] = combine_hsl_saturation_u_float; michael@0: imp->combine_float[PIXMAN_OP_HSL_COLOR] = combine_hsl_color_u_float; michael@0: imp->combine_float[PIXMAN_OP_HSL_LUMINOSITY] = combine_hsl_luminosity_u_float; michael@0: michael@0: /* Component alpha combiners */ michael@0: imp->combine_float_ca[PIXMAN_OP_CLEAR] = combine_clear_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_SRC] = combine_src_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_DST] = combine_dst_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_OVER] = combine_over_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_OVER_REVERSE] = combine_over_reverse_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_IN] = combine_in_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_IN_REVERSE] = combine_in_reverse_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_OUT] = combine_out_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_OUT_REVERSE] = combine_out_reverse_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_ATOP] = combine_atop_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_ATOP_REVERSE] = combine_atop_reverse_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_XOR] = combine_xor_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_ADD] = combine_add_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_SATURATE] = combine_saturate_ca_float; michael@0: michael@0: /* Disjoint CA */ michael@0: imp->combine_float_ca[PIXMAN_OP_DISJOINT_CLEAR] = combine_disjoint_clear_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_DISJOINT_SRC] = combine_disjoint_src_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_DISJOINT_DST] = combine_disjoint_dst_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_DISJOINT_OVER] = combine_disjoint_over_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_DISJOINT_OVER_REVERSE] = combine_disjoint_over_reverse_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_DISJOINT_IN] = combine_disjoint_in_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_DISJOINT_IN_REVERSE] = combine_disjoint_in_reverse_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_DISJOINT_OUT] = combine_disjoint_out_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_DISJOINT_OUT_REVERSE] = combine_disjoint_out_reverse_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_DISJOINT_ATOP] = combine_disjoint_atop_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_DISJOINT_ATOP_REVERSE] = combine_disjoint_atop_reverse_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_DISJOINT_XOR] = combine_disjoint_xor_ca_float; michael@0: michael@0: /* Conjoint CA */ michael@0: imp->combine_float_ca[PIXMAN_OP_CONJOINT_CLEAR] = combine_conjoint_clear_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_CONJOINT_SRC] = combine_conjoint_src_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_CONJOINT_DST] = combine_conjoint_dst_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_CONJOINT_OVER] = combine_conjoint_over_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_CONJOINT_OVER_REVERSE] = combine_conjoint_over_reverse_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_CONJOINT_IN] = combine_conjoint_in_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_CONJOINT_IN_REVERSE] = combine_conjoint_in_reverse_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_CONJOINT_OUT] = combine_conjoint_out_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_CONJOINT_OUT_REVERSE] = combine_conjoint_out_reverse_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_CONJOINT_ATOP] = combine_conjoint_atop_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_CONJOINT_ATOP_REVERSE] = combine_conjoint_atop_reverse_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_CONJOINT_XOR] = combine_conjoint_xor_ca_float; michael@0: michael@0: /* PDF operators CA */ michael@0: imp->combine_float_ca[PIXMAN_OP_MULTIPLY] = combine_multiply_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_SCREEN] = combine_screen_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_OVERLAY] = combine_overlay_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_DARKEN] = combine_darken_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_LIGHTEN] = combine_lighten_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_COLOR_DODGE] = combine_color_dodge_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_COLOR_BURN] = combine_color_burn_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_HARD_LIGHT] = combine_hard_light_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_SOFT_LIGHT] = combine_soft_light_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_DIFFERENCE] = combine_difference_ca_float; michael@0: imp->combine_float_ca[PIXMAN_OP_EXCLUSION] = combine_exclusion_ca_float; michael@0: michael@0: /* It is not clear that these make sense, so make them noops for now */ michael@0: imp->combine_float_ca[PIXMAN_OP_HSL_HUE] = combine_dst_u_float; michael@0: imp->combine_float_ca[PIXMAN_OP_HSL_SATURATION] = combine_dst_u_float; michael@0: imp->combine_float_ca[PIXMAN_OP_HSL_COLOR] = combine_dst_u_float; michael@0: imp->combine_float_ca[PIXMAN_OP_HSL_LUMINOSITY] = combine_dst_u_float; michael@0: }