From aef727848c7753631b461b964368b98df309da6b Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Fri, 27 Nov 2026 13:55:10 +1000 Subject: [PATCH 1/2] Add gradient alpha interpolation option and Fill(Pen) - Update SixLabors.ImageSharp to 5.0.0-alpha.0.38 and fix the API breaks. - Add AlphaRepresentation to GradientBrush and PathGradientBrush. Associated (the default) interpolates premultiplied colors, as CSS Color 4 requires. All other values interpolate unassociated colors, as SVG 2 requires. Both the CPU and the WebGPU backends obey it. - Use the ImageSharp 5 pixel alpha APIs in the gradient encoders, the WebGPU ramp and solid color specialization, and the acrylic tint. - Add DrawingCanvas.Fill(Pen, ...). It fills the stroke of the path with the brush of the pen and the non-zero rule. The drawing transform changes the stroke as it changes all filled shapes. - Update the tests for ImageSharp 5: 15 default formats, and HalfSingle values in the HDR range. --- .../Shaders/WgslSource/fine.wgsl | 23 ++- .../WebGPUBackdropAcrylicLayerEffect.cs | 8 +- .../WebGPUSceneEncoder.cs | 122 ++++++++-------- .../ImageSharp.Drawing.csproj | 2 +- .../Processing/BackdropLayerEffect.cs | 8 +- .../Backends/DefaultDrawingBackend.cs | 4 +- .../Processing/BrushRenderer.cs | 2 +- .../Processing/DrawingCanvas.Shapes.cs | 35 +++++ .../Processing/DrawingCanvas.cs | 13 ++ .../Processing/DrawingCanvas{TPixel}.cs | 22 +++ .../Processing/EllipticGradientBrush.cs | 42 +++++- .../Processing/GradientBrush.cs | 46 ++++-- .../Processing/IGradientPixelEncoder.cs | 31 +--- .../Processing/LinearGradientBrush.cs | 56 +++++++- .../Processing/PathGradientBrush.cs | 81 ++++++++--- .../Processing/RadialGradientBrush.cs | 67 ++++++++- .../Processing/SweepGradientBrush.cs | 38 ++++- .../ConfigurationTests.cs | 2 +- .../Backends/WebGPUDrawingBackendTests.cs | 105 ++++++++++++++ .../DrawingCanvasTests.PathRules.cs | 55 +++++++ ...sWithDrawingCanvasTests.AssociatedAlpha.cs | 134 +++++++++++++++++- ...ssWithDrawingCanvasTests.FillSolidBrush.cs | 2 +- ...hDrawingCanvasTests.PathGradientBrushes.cs | 13 +- tests/ImageSharp.Drawing.Tests/TestFile.cs | 2 +- .../ImageProviders/TestImageProvider.cs | 4 +- .../TestUtilities/TestImageExtensions.cs | 4 +- .../Tests/TestImageProviderTests.cs | 4 +- ...eroUnderEvenOdd_Rgba32_actual-fill-pen.png | 3 + ...ZeroUnderEvenOdd_Rgba32_evenodd-stroke.png | 3 + ...ngleRedChannel_HalfSingle_Blank200x200.png | 3 + ...SingleRedChannel_HalfSingle_Blank20x20.png | 3 - ...aultOutput_Elliptic_Associated_Default.png | 3 + ...liptic_Associated_WebGPU_NativeSurface.png | 3 + ...ltOutput_Elliptic_Unassociated_Default.png | 3 + ...ptic_Unassociated_WebGPU_NativeSurface.png | 3 + ...efaultOutput_Linear_Associated_Default.png | 3 + ...Linear_Associated_WebGPU_NativeSurface.png | 3 + ...aultOutput_Linear_Unassociated_Default.png | 3 + ...near_Unassociated_WebGPU_NativeSurface.png | 3 + ...sDefaultOutput_Path_Associated_Default.png | 3 + ...t_Path_Associated_WebGPU_NativeSurface.png | 3 + ...efaultOutput_Path_Unassociated_Default.png | 3 + ...Path_Unassociated_WebGPU_NativeSurface.png | 3 + ...efaultOutput_Radial_Associated_Default.png | 3 + ...Radial_Associated_WebGPU_NativeSurface.png | 3 + ...aultOutput_Radial_Unassociated_Default.png | 3 + ...dial_Unassociated_WebGPU_NativeSurface.png | 3 + ...DefaultOutput_Sweep_Associated_Default.png | 3 + ..._Sweep_Associated_WebGPU_NativeSurface.png | 3 + ...faultOutput_Sweep_Unassociated_Default.png | 3 + ...weep_Unassociated_WebGPU_NativeSurface.png | 3 + ...formScale_MatchesDefaultOutput_Default.png | 3 + ...chesDefaultOutput_WebGPU_NativeSurface.png | 3 + 53 files changed, 829 insertions(+), 174 deletions(-) create mode 100644 tests/Images/ReferenceOutput/Drawing/DrawingCanvasTests/FillPen_SelfIntersectingStroke_UsesNonZeroUnderEvenOdd_Rgba32_actual-fill-pen.png create mode 100644 tests/Images/ReferenceOutput/Drawing/DrawingCanvasTests/FillPen_SelfIntersectingStroke_UsesNonZeroUnderEvenOdd_Rgba32_evenodd-stroke.png create mode 100644 tests/Images/ReferenceOutput/Drawing/ProcessWithDrawingCanvasTests/FillPathGradientBrushFillTriangleWithHalfSingleRedChannel_HalfSingle_Blank200x200.png delete mode 100644 tests/Images/ReferenceOutput/Drawing/ProcessWithDrawingCanvasTests/FillPathGradientBrushFillTriangleWithHalfSingleRedChannel_HalfSingle_Blank20x20.png create mode 100644 tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Elliptic_Associated_Default.png create mode 100644 tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Elliptic_Associated_WebGPU_NativeSurface.png create mode 100644 tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Elliptic_Unassociated_Default.png create mode 100644 tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Elliptic_Unassociated_WebGPU_NativeSurface.png create mode 100644 tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Linear_Associated_Default.png create mode 100644 tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Linear_Associated_WebGPU_NativeSurface.png create mode 100644 tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Linear_Unassociated_Default.png create mode 100644 tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Linear_Unassociated_WebGPU_NativeSurface.png create mode 100644 tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Path_Associated_Default.png create mode 100644 tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Path_Associated_WebGPU_NativeSurface.png create mode 100644 tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Path_Unassociated_Default.png create mode 100644 tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Path_Unassociated_WebGPU_NativeSurface.png create mode 100644 tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Radial_Associated_Default.png create mode 100644 tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Radial_Associated_WebGPU_NativeSurface.png create mode 100644 tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Radial_Unassociated_Default.png create mode 100644 tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Radial_Unassociated_WebGPU_NativeSurface.png create mode 100644 tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Sweep_Associated_Default.png create mode 100644 tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Sweep_Associated_WebGPU_NativeSurface.png create mode 100644 tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Sweep_Unassociated_Default.png create mode 100644 tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Sweep_Unassociated_WebGPU_NativeSurface.png create mode 100644 tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPen_NonUniformScale_MatchesDefaultOutput_Default.png create mode 100644 tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPen_NonUniformScale_MatchesDefaultOutput_WebGPU_NativeSurface.png diff --git a/src/ImageSharp.Drawing.WebGPU/Shaders/WgslSource/fine.wgsl b/src/ImageSharp.Drawing.WebGPU/Shaders/WgslSource/fine.wgsl index 7b61f06c0..a676d4085 100644 --- a/src/ImageSharp.Drawing.WebGPU/Shaders/WgslSource/fine.wgsl +++ b/src/ImageSharp.Drawing.WebGPU/Shaders/WgslSource/fine.wgsl @@ -430,6 +430,9 @@ fn image_extend_mode_i32(t: f32, mode: u32, max: i32) -> i32 { // Flag bit indicating the gradient carries an explicit center color, which // disables the single-triangle barycentric shortcut. const PATH_GRAD_HAS_EXPLICIT_CENTER_COLOR = 1u; +// This flag bit shows that the payload colors are unassociated. The shader interpolates +// these colors, and then it premultiplies the result. +const PATH_GRAD_UNASSOCIATED_INTERPOLATION = 2u; // Info-stream layout: a 7-word header (center point, max ray distance, and four // center-color components) followed by 12-word edge records (start point, end // point, and four components for each endpoint color). @@ -447,7 +450,8 @@ fn path_grad_load_point(offset: u32) -> vec2 { return vec2(bitcast(info[offset]), bitcast(info[offset + 1u])); } -// Loads an associated color stored as four f32 words in the info stream. +// Loads a color stored as four f32 words in the info stream. The flags of the +// gradient give the alpha representation of the color. fn path_grad_load_color(offset: u32) -> vec4 { return vec4( bitcast(info[offset]), @@ -509,15 +513,28 @@ fn path_grad_point_on_triangle(v1: vec2, v2: vec2, v3: vec2, poin } // Evaluates the path gradient brush at a point, returning an associated color. -// Payload colors are associated before interpolation as required by CSS Color 4: +// CSS Color 4 interpolates associated colors. SVG 2 interpolates unassociated colors, +// so the shader premultiplies the result of an unassociated gradient. // https://www.w3.org/TR/css-color-4/#interpolation-alpha +// https://www.w3.org/TR/SVG2/pservers.html#StopColorProperties +fn evaluate_path_gradient(path_grad: CmdPathGrad, point: vec2) -> vec4 { + let color = interpolate_path_gradient(path_grad, point); + if (path_grad.flags & PATH_GRAD_UNASSOCIATED_INTERPOLATION) != 0u { + return premul_alpha(color); + } + + return color; +} + +// Interpolates the path gradient brush at a point. The result has the alpha +// representation of the payload colors. // A three-edge gradient without an explicit center color interpolates // its vertex colors barycentrically. Otherwise a ray is cast from the point // away from the gradient center to find the nearest edge intersection; the // interpolated edge color is then mixed toward the center color by the ratio // of the point's distance from the edge to the center's distance from the // edge. Returns transparent when the point is not covered by any edge. -fn evaluate_path_gradient(path_grad: CmdPathGrad, point: vec2) -> vec4 { +fn interpolate_path_gradient(path_grad: CmdPathGrad, point: vec2) -> vec4 { let center = path_grad_load_point(path_grad.data_offset); let center_color = path_grad_load_color(path_grad.data_offset + 3u); diff --git a/src/ImageSharp.Drawing.WebGPU/WebGPUBackdropAcrylicLayerEffect.cs b/src/ImageSharp.Drawing.WebGPU/WebGPUBackdropAcrylicLayerEffect.cs index ebaf47eb7..4eec56ede 100644 --- a/src/ImageSharp.Drawing.WebGPU/WebGPUBackdropAcrylicLayerEffect.cs +++ b/src/ImageSharp.Drawing.WebGPU/WebGPUBackdropAcrylicLayerEffect.cs @@ -48,7 +48,7 @@ public WebGPUBackdropAcrylicLayerEffect(float sigma, Color tint) /// The acrylic tint. private void AddPasses(float sigma, Color color) { - Vector4 vector = color.ToScaledVector4(PixelAlphaRepresentation.Unassociated); + Vector4 vector = color.ToScaledVector4(PixelAlphaRepresentation.Associated); // Source RGB is retained by (1 - tint alpha), while the constant row contributes the // premultiplied tint. Alpha is preserved so the backdrop keeps its existing coverage. @@ -58,9 +58,9 @@ private void AddPasses(float sigma, Color color) tint.M22 = keep; tint.M33 = keep; tint.M44 = 1F; - tint.M51 = vector.X * vector.W; - tint.M52 = vector.Y * vector.W; - tint.M53 = vector.Z * vector.W; + tint.M51 = vector.X; + tint.M52 = vector.Y; + tint.M53 = vector.Z; WebGPUColorMatrixLayerEffect tintEffect = new(tint); ReadOnlySpan tintPasses = tintEffect.GetShaderPasses(); diff --git a/src/ImageSharp.Drawing.WebGPU/WebGPUSceneEncoder.cs b/src/ImageSharp.Drawing.WebGPU/WebGPUSceneEncoder.cs index 742c7832f..a26c2f14d 100644 --- a/src/ImageSharp.Drawing.WebGPU/WebGPUSceneEncoder.cs +++ b/src/ImageSharp.Drawing.WebGPU/WebGPUSceneEncoder.cs @@ -6398,6 +6398,7 @@ public static WebGPUEncodedScene Resolve( gradientRamps.Add(new GpuGradientRampDescriptor( gradientRamp.ColorStops, + gradientRamp.AlphaRepresentation, gradientRowOffset + gradientRamp.RowIndex)); } @@ -9229,7 +9230,7 @@ private static void AppendLinearGradientData( { uint indexMode = ((uint)gradientRowCount << 2) | MapExtendMode(brush.RepetitionMode); AppendGradientRamp(ref gradientPixels); - gradientRamps.Add(new GpuGradientRampDescriptor(brush.ColorStops.ToArray(), gradientRowCount)); + gradientRamps.Add(new GpuGradientRampDescriptor(brush.ColorStops.ToArray(), brush.AlphaRepresentation, gradientRowCount)); gradientRowCount++; drawData.Add(indexMode); @@ -9280,7 +9281,7 @@ private static void AppendRadialGradientData( { uint indexMode = ((uint)gradientRowCount << 2) | MapExtendMode(brush.RepetitionMode); AppendGradientRamp(ref gradientPixels); - gradientRamps.Add(new GpuGradientRampDescriptor(brush.ColorStops.ToArray(), gradientRowCount)); + gradientRamps.Add(new GpuGradientRampDescriptor(brush.ColorStops.ToArray(), brush.AlphaRepresentation, gradientRowCount)); gradientRowCount++; PointF center0; @@ -9330,7 +9331,7 @@ private static void AppendEllipticGradientData( { uint indexMode = ((uint)gradientRowCount << 2) | MapExtendMode(brush.RepetitionMode); AppendGradientRamp(ref gradientPixels); - gradientRamps.Add(new GpuGradientRampDescriptor(brush.ColorStops.ToArray(), gradientRowCount)); + gradientRamps.Add(new GpuGradientRampDescriptor(brush.ColorStops.ToArray(), brush.AlphaRepresentation, gradientRowCount)); gradientRowCount++; // Brushes reach the encoder after batcher normalization, so gradient payload points are @@ -9369,7 +9370,7 @@ private static void AppendSweepGradientData( { uint indexMode = ((uint)gradientRowCount << 2) | MapExtendMode(brush.RepetitionMode); AppendGradientRamp(ref gradientPixels); - gradientRamps.Add(new GpuGradientRampDescriptor(brush.ColorStops.ToArray(), gradientRowCount)); + gradientRamps.Add(new GpuGradientRampDescriptor(brush.ColorStops.ToArray(), brush.AlphaRepresentation, gradientRowCount)); gradientRowCount++; float sweepDegrees = brush.EndAngleDegrees - brush.StartAngleDegrees; @@ -9407,6 +9408,7 @@ private static void AppendPathGradientData( { ReadOnlySpan points = brush.Points; ReadOnlySpan colors = brush.Colors; + PixelAlphaRepresentation alphaRepresentation = brush.AlphaRepresentation; int edgeCount = points.Length; PointF center = default; @@ -9432,7 +9434,7 @@ private static void AppendPathGradientData( payload[0] = BitcastSingle(center.X); payload[1] = BitcastSingle(center.Y); payload[2] = BitcastSingle(maxDistance); - WriteAssociatedColor(payload, 3, brush.CenterColor); + WritePathGradientColor(payload, 3, brush.CenterColor, alphaRepresentation); for (int i = 0; i < edgeCount; i++) { @@ -9446,14 +9448,22 @@ private static void AppendPathGradientData( payload[payloadOffset + 1] = BitcastSingle(start.Y); payload[payloadOffset + 2] = BitcastSingle(end.X); payload[payloadOffset + 3] = BitcastSingle(end.Y); - WriteAssociatedColor(payload, payloadOffset + 4, colors[i % colors.Length]); - WriteAssociatedColor(payload, payloadOffset + 8, colors[(i + 1) % colors.Length]); + WritePathGradientColor(payload, payloadOffset + 4, colors[i % colors.Length], alphaRepresentation); + WritePathGradientColor(payload, payloadOffset + 8, colors[(i + 1) % colors.Length], alphaRepresentation); + } + + // These bits match PATH_GRAD_HAS_EXPLICIT_CENTER_COLOR and PATH_GRAD_UNASSOCIATED_INTERPOLATION + // in fine.wgsl. + uint flags = brush.HasExplicitCenterColor ? 1U : 0U; + if (alphaRepresentation != PixelAlphaRepresentation.Associated) + { + flags |= 2U; } pathGradientData.Advance(payload.Length); drawData.Add((uint)dataOffset); drawData.Add((uint)edgeCount); - drawData.Add(brush.HasExplicitCenterColor ? 1U : 0U); + drawData.Add(flags); drawData.Add(0U); } @@ -9475,12 +9485,13 @@ private static void AppendGradientRamp(ref OwnedStream gradientPixels) } /// - /// Evaluates the premultiplied gradient color at the normalized parameter. + /// Evaluates the gradient color at the normalized parameter. /// /// The gradient color stops, ordered by ratio. + /// The alpha representation of the stop colors during interpolation. /// The normalized sample position in the range [0, 1]. - /// The associated floating-point sample. - public static Vector4 EvaluateGradientColor(ReadOnlySpan colorStops, float t) + /// The floating-point sample in . + public static Vector4 EvaluateGradientColor(ReadOnlySpan colorStops, PixelAlphaRepresentation alphaRepresentation, float t) { // Walk the stop list once to find the enclosing interval, then interpolate within it. ColorStop from = colorStops[0]; @@ -9498,16 +9509,16 @@ public static Vector4 EvaluateGradientColor(ReadOnlySpan colorStops, if (from.Color.Equals(to.Color) || to.Ratio == from.Ratio) { - return from.Color.ToScaledVector4(PixelAlphaRepresentation.Associated); + return from.Color.ToScaledVector4(alphaRepresentation); } float localT = (t - from.Ratio) / (to.Ratio - from.Ratio); - // CSS Color 4 requires alpha premultiplication before interpolation. The ramp stores - // associated RGBA16Float directly, matching the fine shader's internal composition space. + // CSS Color 4 interpolates associated colors. SVG 2 interpolates unassociated colors. + // The stops use the alpha representation of the brush. return Vector4.Lerp( - from.Color.ToScaledVector4(PixelAlphaRepresentation.Associated), - to.Color.ToScaledVector4(PixelAlphaRepresentation.Associated), + from.Color.ToScaledVector4(alphaRepresentation), + to.Color.ToScaledVector4(alphaRepresentation), localT); } @@ -9533,16 +9544,17 @@ public static uint PackHalf2(float x, float y) => BitConverter.HalfToUInt16Bits((Half)x) | ((uint)BitConverter.HalfToUInt16Bits((Half)y) << 16); /// - /// Writes one associated color to the path-gradient payload without reducing it to RGBA8 first. + /// Writes one color to the path-gradient payload without reducing it to RGBA8 first. /// /// The payload receiving four consecutive floating-point words. /// The destination word offset. - /// The color to convert to associated components. - private static void WriteAssociatedColor(Span destination, int offset, in Color color) + /// The color to write. + /// The alpha representation of the colors during interpolation. + private static void WritePathGradientColor(Span destination, int offset, in Color color, PixelAlphaRepresentation alphaRepresentation) { // Path gradients interpolate endpoint colors directly in the shader. Keeping the source // precision here matches the CPU renderer and defers quantization until target encoding. - Vector4 vector = color.ToScaledVector4(PixelAlphaRepresentation.Associated); + Vector4 vector = color.ToScaledVector4(alphaRepresentation); destination[offset] = BitcastSingle(vector.X); destination[offset + 1] = BitcastSingle(vector.Y); destination[offset + 2] = BitcastSingle(vector.Z); @@ -10430,9 +10442,8 @@ public void SpecializeBrushColors() return; } - PixelAlphaRepresentation alphaRepresentation = TPixel.GetPixelTypeInfo().AlphaRepresentation; - this.SpecializeSolidColors(alphaRepresentation); - this.SpecializeGradientRamps(alphaRepresentation); + this.SpecializeSolidColors(); + this.SpecializeGradientRamps(); this.specializedPixelType = typeof(TPixel); } @@ -10442,22 +10453,16 @@ public void SpecializeBrushColors() /// it creates its CPU renderer. /// /// The pixel format of the target. - /// The alpha representation of . - private void SpecializeSolidColors(PixelAlphaRepresentation alphaRepresentation) + private void SpecializeSolidColors() where TPixel : unmanaged, IPixel { for (int i = 0; i < this.solidColors.Count; i++) { GpuSolidColorDescriptor descriptor = this.solidColors[i]; - Vector4 color = descriptor.Color.ToPixel().ToScaledVector4(); - if (alphaRepresentation != PixelAlphaRepresentation.Associated) - { - // Associating rounds the product into binary16; see the precision contract on - // SpecializeBrushColors. - float alpha = color.W; - color *= alpha; - color.W = alpha; - } + + // For a TPixel with unassociated alpha, the associated product rounds to binary16. + // The precision contract on SpecializeBrushColors gives the effect. + Vector4 color = descriptor.Color.ToPixel().ToAssociatedScaledVector4(); int wordIndex = checked((int)this.Layout.DrawDataBase + descriptor.DrawDataWordOffset); this.SetSceneWord(wordIndex, WebGPUSceneEncoder.PackHalf2(color.X, color.Y)); @@ -10471,8 +10476,7 @@ private void SpecializeSolidColors(PixelAlphaRepresentation alphaReprese /// encoders perform before blending. /// /// The pixel format of the target. - /// The alpha representation of . - private void SpecializeGradientRamps(PixelAlphaRepresentation alphaRepresentation) + private void SpecializeGradientRamps() where TPixel : unmanaged, IPixel { if (this.gradientPixelsOwner is null) @@ -10484,37 +10488,22 @@ private void SpecializeGradientRamps(PixelAlphaRepresentation alphaRepre for (int i = 0; i < this.gradientRamps.Count; i++) { GpuGradientRampDescriptor descriptor = this.gradientRamps[i]; + bool associated = descriptor.AlphaRepresentation == PixelAlphaRepresentation.Associated; int rowBase = descriptor.RowIndex * WebGPUSceneEncoder.GradientRowWordCount; for (int x = 0; x < WebGPUSceneEncoder.GradientWidth; x++) { float t = x / (float)(WebGPUSceneEncoder.GradientWidth - 1); - Vector4 color = WebGPUSceneEncoder.EvaluateGradientColor(descriptor.ColorStops, t); - if (alphaRepresentation == PixelAlphaRepresentation.Associated) - { - color = TPixel.FromScaledVector4(color).ToScaledVector4(); - } - else - { - // The CPU's straight-alpha encoder undoes the interpolation-time - // premultiplication before converting to TPixel, so the same division - // precedes the snap. Re-associating rounds the product into binary16; - // see the precision contract on SpecializeBrushColors. - float alpha = color.W; - if (alpha > 0) - { - color /= alpha; - color.W = alpha; - } - else - { - color = Vector4.Zero; - } + Vector4 color = WebGPUSceneEncoder.EvaluateGradientColor(descriptor.ColorStops, descriptor.AlphaRepresentation, t); - color = TPixel.FromScaledVector4(color).ToScaledVector4(); - float snappedAlpha = color.W; - color *= snappedAlpha; - color.W = snappedAlpha; - } + // The texel snaps through TPixel, as in the CPU gradient encoders. The ramp stores + // associated RGBA16Float, which is the composition space of the fine shader. + // For a TPixel with unassociated alpha, the associated product rounds to binary16. + // The precision contract on SpecializeBrushColors gives the effect. + TPixel pixel = associated + ? TPixel.FromAssociatedScaledVector4(color) + : TPixel.FromUnassociatedScaledVector4(color); + + color = pixel.ToAssociatedScaledVector4(); gradientPixels[rowBase + (x * 2)] = WebGPUSceneEncoder.PackHalf2(color.X, color.Y); gradientPixels[rowBase + (x * 2) + 1] = WebGPUSceneEncoder.PackHalf2(color.Z, color.W); @@ -10749,10 +10738,12 @@ internal readonly struct GpuGradientRampDescriptor /// Initializes a new instance of the struct. /// /// The gradient color stops, ordered by ratio. + /// The alpha representation of the stop colors during interpolation. /// The ramp texture row the gradient occupies. - public GpuGradientRampDescriptor(ColorStop[] colorStops, int rowIndex) + public GpuGradientRampDescriptor(ColorStop[] colorStops, PixelAlphaRepresentation alphaRepresentation, int rowIndex) { this.ColorStops = colorStops; + this.AlphaRepresentation = alphaRepresentation; this.RowIndex = rowIndex; } @@ -10761,6 +10752,11 @@ public GpuGradientRampDescriptor(ColorStop[] colorStops, int rowIndex) /// public ColorStop[] ColorStops { get; } + /// + /// Gets the alpha representation of the stop colors during interpolation. + /// + public PixelAlphaRepresentation AlphaRepresentation { get; } + /// /// Gets the ramp texture row the gradient occupies. /// diff --git a/src/ImageSharp.Drawing/ImageSharp.Drawing.csproj b/src/ImageSharp.Drawing/ImageSharp.Drawing.csproj index 389195369..5cec1e86d 100644 --- a/src/ImageSharp.Drawing/ImageSharp.Drawing.csproj +++ b/src/ImageSharp.Drawing/ImageSharp.Drawing.csproj @@ -45,7 +45,7 @@ - + diff --git a/src/ImageSharp.Drawing/Processing/BackdropLayerEffect.cs b/src/ImageSharp.Drawing/Processing/BackdropLayerEffect.cs index ae258ff9c..9eb628848 100644 --- a/src/ImageSharp.Drawing/Processing/BackdropLayerEffect.cs +++ b/src/ImageSharp.Drawing/Processing/BackdropLayerEffect.cs @@ -109,16 +109,16 @@ private static Action CreateOperation(float sigma, Colo // Blending a constant colour over the backdrop is linear, so it is expressed as a colour // matrix: the backdrop's channels scale by one minus the tint's alpha and the constant row // carries the pre-scaled tint. The backdrop's own alpha is preserved. - Vector4 vector = color.ToPixel().ToScaledVector4(); + Vector4 vector = color.ToPixel().ToAssociatedScaledVector4(); float keep = 1F - vector.W; ColorMatrix tint = default; tint.M11 = keep; tint.M22 = keep; tint.M33 = keep; tint.M44 = 1F; - tint.M51 = vector.X * vector.W; - tint.M52 = vector.Y * vector.W; - tint.M53 = vector.Z * vector.W; + tint.M51 = vector.X; + tint.M52 = vector.Y; + tint.M53 = vector.Z; return context => { diff --git a/src/ImageSharp.Drawing/Processing/Backends/DefaultDrawingBackend.cs b/src/ImageSharp.Drawing/Processing/Backends/DefaultDrawingBackend.cs index ddadd3422..2b9f32424 100644 --- a/src/ImageSharp.Drawing/Processing/Backends/DefaultDrawingBackend.cs +++ b/src/ImageSharp.Drawing/Processing/Backends/DefaultDrawingBackend.cs @@ -757,7 +757,7 @@ private static void CompositeLayerBand( return; } - PixelBlender blender = PixelOperations.Instance.GetPixelBlender(graphicsOptions); + PixelBlender blender = PixelBlenders.GetPixelBlender(graphicsOptions); Span amounts = brushWorkspace.GetAmounts(overlap.Width); amounts[..overlap.Width].Fill(graphicsOptions.BlendPercentage); @@ -811,7 +811,7 @@ public static void ComposeLayer( throw new NotSupportedException($"{nameof(DefaultDrawingBackend)} requires CPU-accessible destination frames."); } - PixelBlender blender = PixelOperations.Instance.GetPixelBlender(options); + PixelBlender blender = PixelBlenders.GetPixelBlender(options); float blendPercentage = options.BlendPercentage; int srcWidth = sourceRegion.Width; diff --git a/src/ImageSharp.Drawing/Processing/BrushRenderer.cs b/src/ImageSharp.Drawing/Processing/BrushRenderer.cs index e3e84777d..27f2864c1 100644 --- a/src/ImageSharp.Drawing/Processing/BrushRenderer.cs +++ b/src/ImageSharp.Drawing/Processing/BrushRenderer.cs @@ -24,7 +24,7 @@ protected BrushRenderer( this.Configuration = configuration; this.Options = options; this.CanvasWidth = canvasWidth; - this.Blender = PixelOperations.Instance.GetPixelBlender(options); + this.Blender = PixelBlenders.GetPixelBlender(options); } /// diff --git a/src/ImageSharp.Drawing/Processing/DrawingCanvas.Shapes.cs b/src/ImageSharp.Drawing/Processing/DrawingCanvas.Shapes.cs index 39f0f6862..7e7ac4b53 100644 --- a/src/ImageSharp.Drawing/Processing/DrawingCanvas.Shapes.cs +++ b/src/ImageSharp.Drawing/Processing/DrawingCanvas.Shapes.cs @@ -92,6 +92,41 @@ public void Fill(Brush brush, PathBuilder pathBuilder) this.Fill(brush, pathBuilder.Build()); } + /// + /// Fills the stroke of the rectangle with the brush of the pen. + /// + /// The pen that generates the stroke and supplies the brush. + /// The rectangle in local coordinates. + public void Fill(Pen pen, Rectangle region) + => this.Fill(pen, new RectanglePolygon(region)); + + /// + /// Fills the stroke of each path in the collection with the brush of the pen. + /// + /// The pen that generates the strokes and supplies the brush. + /// The paths in local coordinates. + public void Fill(Pen pen, IPathCollection paths) + { + Guard.NotNull(paths, nameof(paths)); + + foreach (IPath path in paths) + { + this.Fill(pen, path); + } + } + + /// + /// Fills the stroke of the path from the path builder with the brush of the pen. + /// + /// The pen that generates the stroke and supplies the brush. + /// The path builder that defines the path in local coordinates. + public void Fill(Pen pen, PathBuilder pathBuilder) + { + Guard.NotNull(pathBuilder, nameof(pathBuilder)); + + this.Fill(pen, pathBuilder.Build()); + } + /// /// Fills an ellipse using the provided brush. /// diff --git a/src/ImageSharp.Drawing/Processing/DrawingCanvas.cs b/src/ImageSharp.Drawing/Processing/DrawingCanvas.cs index 4856d4874..eca7ac0c4 100644 --- a/src/ImageSharp.Drawing/Processing/DrawingCanvas.cs +++ b/src/ImageSharp.Drawing/Processing/DrawingCanvas.cs @@ -192,6 +192,19 @@ public abstract partial class DrawingCanvas : IDisposable /// The path to fill. public abstract void Fill(Brush brush, IPath path); + /// + /// Fills the stroke of the path with the brush of the pen. + /// + /// + /// The stroke is the path that returns. For a circle, the stroke is a ring. + /// The stroke always uses the non-zero fill rule. The intersection rule of the drawing options does not apply. + /// The drawing transform changes the stroke in the same way as all filled shapes. A non-uniform scale or a skew + /// changes the stroke width. keeps an even stroke width under such a transform. + /// + /// The pen that generates the stroke and supplies the brush. + /// The path in local coordinates. + public abstract void Fill(Pen pen, IPath path); + /// /// Narrows the current clip region by intersecting it with the supplied clip paths. /// diff --git a/src/ImageSharp.Drawing/Processing/DrawingCanvas{TPixel}.cs b/src/ImageSharp.Drawing/Processing/DrawingCanvas{TPixel}.cs index 6ab3dc724..e21cbaa5f 100644 --- a/src/ImageSharp.Drawing/Processing/DrawingCanvas{TPixel}.cs +++ b/src/ImageSharp.Drawing/Processing/DrawingCanvas{TPixel}.cs @@ -576,6 +576,28 @@ public override void Fill(Brush brush, IPath path) this.EnqueueFillPath(brush, path); } + /// + public override void Fill(Pen pen, IPath path) + { + this.EnsureNotDisposed(); + Guard.NotNull(pen, nameof(pen)); + Guard.NotNull(path, nameof(path)); + + DrawingCanvasState state = this.ResolveState(); + + // The stroke is the raw output of the stroker, and its area obeys the non-zero rule. + // The even-odd rule makes holes at the joins and at the self-intersections of the stroke. + DrawingOptions options = state.Options.CloneOrReturnForRules( + IntersectionRule.NonZero, + state.Options.GraphicsOptions.AlphaCompositionMode, + state.Options.GraphicsOptions.ColorBlendingMode); + + this.PrepareCompositionCore( + pen.GeneratePath(path).AsClosedPath(), + pen.StrokeFill, + options); + } + /// public override void Clip(params IPath[] clipPaths) => this.Clip(ClipOperation.Intersection, clipPaths); diff --git a/src/ImageSharp.Drawing/Processing/EllipticGradientBrush.cs b/src/ImageSharp.Drawing/Processing/EllipticGradientBrush.cs index d7649f452..0a527b753 100644 --- a/src/ImageSharp.Drawing/Processing/EllipticGradientBrush.cs +++ b/src/ImageSharp.Drawing/Processing/EllipticGradientBrush.cs @@ -57,7 +57,34 @@ public EllipticGradientBrush( GradientRepetitionMode repetitionMode, Matrix4x4 gradientTransform, params ColorStop[] colorStops) - : base(repetitionMode, gradientTransform, colorStops) + : this(center, referenceAxisEnd, axisRatio, repetitionMode, gradientTransform, PixelAlphaRepresentation.Associated, colorStops) + { + } + + /// + /// Initializes a new instance of the class with the ellipse + /// defined in the gradient's own coordinate space. + /// + /// The center of the elliptical gradient and 0 for the color stops. + /// The end point of the reference axis of the ellipse. + /// + /// The ratio of the axis widths. + /// The second axis is perpendicular to the reference axis and its length is the reference axis length + /// multiplied by this factor. + /// + /// Defines how the colors of the gradients are repeated. + /// The transform from the gradient's coordinate space to the drawing. + /// The alpha representation of the stop colors during interpolation. + /// The color stops. + public EllipticGradientBrush( + PointF center, + PointF referenceAxisEnd, + float axisRatio, + GradientRepetitionMode repetitionMode, + Matrix4x4 gradientTransform, + PixelAlphaRepresentation alphaRepresentation, + params ColorStop[] colorStops) + : base(repetitionMode, gradientTransform, alphaRepresentation, colorStops) { this.Center = center; this.ReferenceAxisEnd = referenceAxisEnd; @@ -104,10 +131,17 @@ public override Brush Transform(Matrix4x4 matrix, Rectangle sourceInterest, Rect float newRefLen = MathF.Sqrt(((tRef.X - tc.X) * (tRef.X - tc.X)) + ((tRef.Y - tc.Y) * (tRef.Y - tc.Y))); float newSecLen = MathF.Sqrt(((tSec.X - tc.X) * (tSec.X - tc.X)) + ((tSec.Y - tc.Y) * (tSec.Y - tc.Y))); float newRatio = newRefLen > 0f ? newSecLen / newRefLen : this.AxisRatio; - return new EllipticGradientBrush(tc, tRef, newRatio, this.RepetitionMode, this.ColorStopsArray); + return new EllipticGradientBrush(tc, tRef, newRatio, this.RepetitionMode, Matrix4x4.Identity, this.AlphaRepresentation, this.ColorStopsArray); } - return new EllipticGradientBrush(this.Center, this.ReferenceAxisEnd, this.AxisRatio, this.RepetitionMode, gradientTransform, this.ColorStopsArray); + return new EllipticGradientBrush( + this.Center, + this.ReferenceAxisEnd, + this.AxisRatio, + this.RepetitionMode, + gradientTransform, + this.AlphaRepresentation, + this.ColorStopsArray); } /// @@ -117,7 +151,7 @@ public override BrushRenderer CreateRenderer( int canvasWidth, RectangleF region) { - if (TPixel.GetPixelTypeInfo().AlphaRepresentation == PixelAlphaRepresentation.Associated) + if (this.AlphaRepresentation == PixelAlphaRepresentation.Associated) { return new EllipticGradientBrushRenderer>(configuration, options, canvasWidth, this); } diff --git a/src/ImageSharp.Drawing/Processing/GradientBrush.cs b/src/ImageSharp.Drawing/Processing/GradientBrush.cs index c2c4795ab..e1c5488bb 100644 --- a/src/ImageSharp.Drawing/Processing/GradientBrush.cs +++ b/src/ImageSharp.Drawing/Processing/GradientBrush.cs @@ -30,9 +30,26 @@ protected GradientBrush(GradientRepetitionMode repetitionMode, params ColorStop[ /// The transform from the gradient's coordinate space to the drawing. /// The gradient colors. protected GradientBrush(GradientRepetitionMode repetitionMode, Matrix4x4 gradientTransform, params ColorStop[] colorStops) + : this(repetitionMode, gradientTransform, PixelAlphaRepresentation.Associated, colorStops) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// Defines how the colors are repeated beyond the interval [0..1]. + /// The transform from the gradient's coordinate space to the drawing. + /// The alpha representation of the stop colors during interpolation. + /// The gradient colors. + protected GradientBrush( + GradientRepetitionMode repetitionMode, + Matrix4x4 gradientTransform, + PixelAlphaRepresentation alphaRepresentation, + params ColorStop[] colorStops) { this.RepetitionMode = repetitionMode; this.GradientTransform = gradientTransform; + this.AlphaRepresentation = alphaRepresentation; InsertionSort(colorStops, (a, b) => a.Ratio.CompareTo(b.Ratio)); this.ColorStopsArray = colorStops; @@ -53,6 +70,13 @@ protected GradientBrush(GradientRepetitionMode repetitionMode, Matrix4x4 gradien /// public Matrix4x4 GradientTransform { get; } + /// + /// Gets the alpha representation of the stop colors during interpolation. + /// is the default. + /// All other values interpolate unassociated colors. + /// + public PixelAlphaRepresentation AlphaRepresentation { get; } + /// /// Gets the color stops for this gradient. /// @@ -70,6 +94,7 @@ public override bool Equals(Brush? other) { return this.RepetitionMode == brush.RepetitionMode && this.GradientTransform.Equals(brush.GradientTransform) + && this.AlphaRepresentation == brush.AlphaRepresentation && this.ColorStopsArray?.SequenceEqual(brush.ColorStopsArray) == true; } @@ -78,7 +103,7 @@ public override bool Equals(Brush? other) /// public override int GetHashCode() - => HashCode.Combine(this.RepetitionMode, this.GradientTransform, this.ColorStopsArray); + => HashCode.Combine(this.RepetitionMode, this.GradientTransform, this.AlphaRepresentation, this.ColorStopsArray); /// /// Inverts the affine part of , which maps drawing @@ -124,7 +149,7 @@ private static void InsertionSort(T[] collection, Comparison comparison) /// Base class for gradient brush applicators. /// /// The pixel format. - /// The destination representation encoder. + /// The encoder for the alpha representation of the brush. internal abstract class GradientBrushRenderer : BrushRenderer where TPixel : unmanaged, IPixel where TEncoder : struct, IGradientPixelEncoder @@ -152,14 +177,17 @@ protected GradientBrushRenderer( ColorStop[] colorStops = brush.ColorStopsArray; this.colorStops = new GradientColorStop[colorStops.Length]; - // CSS Color 4 requires alpha to be premultiplied before color interpolation. - // Cache associated stop vectors once so the result is independent of TPixel storage - // and the per-pixel interpolation loop does not repeat the conversion. + // CSS Color 4 interpolates associated colors. SVG 2 interpolates unassociated colors. + // The renderer stores the stops in the representation of the brush, without a TPixel + // conversion. The TPixel format cannot change the gradient, and the per-pixel loop + // does not convert the stops. // https://www.w3.org/TR/css-color-4/#interpolation-alpha + // https://www.w3.org/TR/SVG2/pservers.html#StopColorProperties + PixelAlphaRepresentation alphaRepresentation = brush.AlphaRepresentation; for (int i = 0; i < colorStops.Length; i++) { ColorStop stop = colorStops[i]; - this.colorStops[i] = new GradientColorStop(stop.Ratio, stop.Color.ToScaledVector4(PixelAlphaRepresentation.Associated)); + this.colorStops[i] = new GradientColorStop(stop.Ratio, stop.Color.ToScaledVector4(alphaRepresentation)); } this.repetitionMode = brush.RepetitionMode; @@ -331,10 +359,10 @@ public override void Apply( } /// - /// Stores one gradient stop in the associated representation used for interpolation. + /// Stores one gradient stop in the representation used for interpolation. /// /// The stop position. - /// The associated stop color. + /// The stop color in the alpha representation of the brush. private readonly struct GradientColorStop(float ratio, Vector4 color) { /// @@ -343,7 +371,7 @@ private readonly struct GradientColorStop(float ratio, Vector4 color) public float Ratio { get; } = ratio; /// - /// Gets the associated stop color. + /// Gets the stop color in the alpha representation of the brush. /// public Vector4 Color { get; } = color; } diff --git a/src/ImageSharp.Drawing/Processing/IGradientPixelEncoder.cs b/src/ImageSharp.Drawing/Processing/IGradientPixelEncoder.cs index 3c634f2c8..9b444d9ab 100644 --- a/src/ImageSharp.Drawing/Processing/IGradientPixelEncoder.cs +++ b/src/ImageSharp.Drawing/Processing/IGradientPixelEncoder.cs @@ -6,55 +6,38 @@ namespace SixLabors.ImageSharp.Drawing.Processing; /// -/// Encodes an associated gradient result in the destination pixel representation. +/// Encodes an interpolated gradient color as a destination pixel. /// /// The destination pixel format. internal interface IGradientPixelEncoder where TPixel : unmanaged, IPixel { /// - /// Converts an associated scaled vector to the destination pixel representation. + /// Converts a scaled vector to the destination pixel. /// - /// The associated scaled vector. + /// The scaled vector, in the alpha representation of the encoder. /// The destination pixel. public static abstract TPixel Encode(Vector4 color); } /// -/// Encodes associated gradient results for an associated-alpha destination. +/// Encodes a gradient color that has associated alpha. /// /// The destination pixel format. internal readonly struct AssociatedGradientPixelEncoder : IGradientPixelEncoder where TPixel : unmanaged, IPixel { /// - public static TPixel Encode(Vector4 color) => TPixel.FromScaledVector4(color); + public static TPixel Encode(Vector4 color) => TPixel.FromAssociatedScaledVector4(color); } /// -/// Encodes associated gradient results for an unassociated-alpha destination. +/// Encodes a gradient color that has unassociated alpha. /// /// The destination pixel format. internal readonly struct UnassociatedGradientPixelEncoder : IGradientPixelEncoder where TPixel : unmanaged, IPixel { /// - public static TPixel Encode(Vector4 color) - { - // CSS Color 4 undoes premultiplication only after interpolation. Straight targets require - // that conversion here, while associated targets store the interpolated vector directly. - // https://www.w3.org/TR/css-color-4/#interpolation-alpha - float alpha = color.W; - if (alpha > 0) - { - color /= alpha; - color.W = alpha; - } - else - { - color = Vector4.Zero; - } - - return TPixel.FromScaledVector4(color); - } + public static TPixel Encode(Vector4 color) => TPixel.FromUnassociatedScaledVector4(color); } diff --git a/src/ImageSharp.Drawing/Processing/LinearGradientBrush.cs b/src/ImageSharp.Drawing/Processing/LinearGradientBrush.cs index 2c7e3148b..2822ea21d 100644 --- a/src/ImageSharp.Drawing/Processing/LinearGradientBrush.cs +++ b/src/ImageSharp.Drawing/Processing/LinearGradientBrush.cs @@ -44,7 +44,28 @@ public LinearGradientBrush( GradientRepetitionMode repetitionMode, Matrix4x4 gradientTransform, params ColorStop[] colorStops) - : base(repetitionMode, gradientTransform, colorStops) + : this(p0, p1, repetitionMode, gradientTransform, PixelAlphaRepresentation.Associated, colorStops) + { + } + + /// + /// Initializes a new instance of the class using + /// a start and end point defined in the gradient's own coordinate space. + /// + /// The start point of the gradient. + /// The end point of the gradient. + /// Defines how the colors are repeated. + /// The transform from the gradient's coordinate space to the drawing. + /// The alpha representation of the stop colors during interpolation. + /// The ordered color stops of the gradient. + public LinearGradientBrush( + PointF p0, + PointF p1, + GradientRepetitionMode repetitionMode, + Matrix4x4 gradientTransform, + PixelAlphaRepresentation alphaRepresentation, + params ColorStop[] colorStops) + : base(repetitionMode, gradientTransform, alphaRepresentation, colorStops) { this.StartPoint = p0; this.EndPoint = p1; @@ -90,7 +111,32 @@ public LinearGradientBrush( GradientRepetitionMode repetitionMode, Matrix4x4 gradientTransform, params ColorStop[] colorStops) - : base(repetitionMode, gradientTransform, colorStops) + : this(p0, p1, rotationPoint, repetitionMode, gradientTransform, PixelAlphaRepresentation.Associated, colorStops) + { + } + + /// + /// Initializes a new instance of the class using + /// three points, defined in the gradient's own coordinate space, to define a rotated gradient axis. + /// + /// The first point (start of the gradient). + /// The second point (gradient vector endpoint). + /// + /// The rotation reference point. This defines the rotation of the gradient axis. + /// + /// Defines how the colors are repeated. + /// The transform from the gradient's coordinate space to the drawing. + /// The alpha representation of the stop colors during interpolation. + /// The ordered color stops of the gradient. + public LinearGradientBrush( + PointF p0, + PointF p1, + PointF rotationPoint, + GradientRepetitionMode repetitionMode, + Matrix4x4 gradientTransform, + PixelAlphaRepresentation alphaRepresentation, + params ColorStop[] colorStops) + : base(repetitionMode, gradientTransform, alphaRepresentation, colorStops) { ResolveAxis(p0, p1, rotationPoint, out PointF start, out PointF end); this.StartPoint = start; @@ -117,10 +163,12 @@ public override Brush Transform(Matrix4x4 matrix, Rectangle sourceInterest, Rect PointF.Transform(this.StartPoint, gradientTransform), PointF.Transform(this.EndPoint, gradientTransform), this.RepetitionMode, + Matrix4x4.Identity, + this.AlphaRepresentation, this.ColorStopsArray); } - return new LinearGradientBrush(this.StartPoint, this.EndPoint, this.RepetitionMode, gradientTransform, this.ColorStopsArray); + return new LinearGradientBrush(this.StartPoint, this.EndPoint, this.RepetitionMode, gradientTransform, this.AlphaRepresentation, this.ColorStopsArray); } /// @@ -188,7 +236,7 @@ public override BrushRenderer CreateRenderer( int canvasWidth, RectangleF region) { - if (TPixel.GetPixelTypeInfo().AlphaRepresentation == PixelAlphaRepresentation.Associated) + if (this.AlphaRepresentation == PixelAlphaRepresentation.Associated) { return new LinearGradientBrushRenderer>(configuration, options, canvasWidth, this); } diff --git a/src/ImageSharp.Drawing/Processing/PathGradientBrush.cs b/src/ImageSharp.Drawing/Processing/PathGradientBrush.cs index c84cf3bfd..ce4f56ce7 100644 --- a/src/ImageSharp.Drawing/Processing/PathGradientBrush.cs +++ b/src/ImageSharp.Drawing/Processing/PathGradientBrush.cs @@ -25,6 +25,17 @@ public sealed class PathGradientBrush : Brush /// Points that constitute a polygon that represents the gradient area. /// Array of colors that correspond to each point in the polygon. public PathGradientBrush(PointF[] points, Color[] colors) + : this(points, colors, PixelAlphaRepresentation.Associated) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// Points that constitute a polygon that represents the gradient area. + /// Array of colors that correspond to each point in the polygon. + /// The alpha representation of the colors during interpolation. + public PathGradientBrush(PointF[] points, Color[] colors, PixelAlphaRepresentation alphaRepresentation) { Guard.NotNull(points, nameof(points)); Guard.MustBeGreaterThanOrEqualTo(points.Length, 3, nameof(points)); @@ -42,7 +53,8 @@ public PathGradientBrush(PointF[] points, Color[] colors) this.edges[i] = new Edge(this.points[i % size], this.points[(i + 1) % size], ColorAt(i), ColorAt(i + 1)); } - this.CenterColor = CalculateCenterColor(this.colors); + this.AlphaRepresentation = alphaRepresentation; + this.CenterColor = CalculateCenterColor(this.colors, alphaRepresentation); Color ColorAt(int index) => this.colors[index % this.colors.Length]; } @@ -54,7 +66,19 @@ public PathGradientBrush(PointF[] points, Color[] colors) /// Array of colors that correspond to each point in the polygon. /// Color at the center of the gradient area to which the other colors converge. public PathGradientBrush(PointF[] points, Color[] colors, Color centerColor) - : this(points, colors) + : this(points, colors, centerColor, PixelAlphaRepresentation.Associated) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// Points that constitute a polygon that represents the gradient area. + /// Array of colors that correspond to each point in the polygon. + /// Color at the center of the gradient area to which the other colors converge. + /// The alpha representation of the colors during interpolation. + public PathGradientBrush(PointF[] points, Color[] colors, Color centerColor, PixelAlphaRepresentation alphaRepresentation) + : this(points, colors, alphaRepresentation) { this.CenterColor = centerColor; this.HasExplicitCenterColor = true; @@ -80,6 +104,13 @@ public PathGradientBrush(PointF[] points, Color[] colors, Color centerColor) /// public bool HasExplicitCenterColor { get; } + /// + /// Gets the alpha representation of the colors during interpolation. + /// is the default. + /// All other values interpolate unassociated colors. + /// + public PixelAlphaRepresentation AlphaRepresentation { get; } + /// public override Brush Transform(Matrix4x4 matrix, Rectangle sourceInterest, Rectangle preparedInterest) { @@ -95,8 +126,8 @@ public override Brush Transform(Matrix4x4 matrix, Rectangle sourceInterest, Rect } return this.HasExplicitCenterColor - ? new PathGradientBrush(transformedPoints, this.colors, this.CenterColor) - : new PathGradientBrush(transformedPoints, this.colors); + ? new PathGradientBrush(transformedPoints, this.colors, this.CenterColor, this.AlphaRepresentation) + : new PathGradientBrush(transformedPoints, this.colors, this.AlphaRepresentation); } /// @@ -106,6 +137,7 @@ public override bool Equals(Brush? other) { return this.CenterColor.Equals(brush.CenterColor) && this.HasExplicitCenterColor.Equals(brush.HasExplicitCenterColor) + && this.AlphaRepresentation == brush.AlphaRepresentation && this.edges?.SequenceEqual(brush.edges) == true; } @@ -114,7 +146,7 @@ public override bool Equals(Brush? other) /// public override int GetHashCode() - => HashCode.Combine(this.edges, this.CenterColor, this.HasExplicitCenterColor); + => HashCode.Combine(this.edges, this.CenterColor, this.HasExplicitCenterColor, this.AlphaRepresentation); /// public override BrushRenderer CreateRenderer( @@ -123,7 +155,7 @@ public override BrushRenderer CreateRenderer( int canvasWidth, RectangleF region) { - if (TPixel.GetPixelTypeInfo().AlphaRepresentation == PixelAlphaRepresentation.Associated) + if (this.AlphaRepresentation == PixelAlphaRepresentation.Associated) { return new PathGradientBrushRenderer>( configuration, @@ -131,7 +163,8 @@ public override BrushRenderer CreateRenderer( canvasWidth, this.edges, this.CenterColor, - this.HasExplicitCenterColor); + this.HasExplicitCenterColor, + this.AlphaRepresentation); } return new PathGradientBrushRenderer>( @@ -140,7 +173,8 @@ public override BrushRenderer CreateRenderer( canvasWidth, this.edges, this.CenterColor, - this.HasExplicitCenterColor); + this.HasExplicitCenterColor, + this.AlphaRepresentation); } /// @@ -148,8 +182,9 @@ public override BrushRenderer CreateRenderer( /// in scaled vector space. /// /// The point colors to average. + /// The alpha representation of the colors during interpolation. /// The averaged color. - private static Color CalculateCenterColor(Color[] colors) + private static Color CalculateCenterColor(Color[] colors, PixelAlphaRepresentation alphaRepresentation) { Guard.NotNull(colors, nameof(colors)); Guard.MustBeGreaterThan(colors.Length, 0, nameof(colors)); @@ -157,13 +192,11 @@ private static Color CalculateCenterColor(Color[] colors) Vector4 sum = Vector4.Zero; foreach (Color color in colors) { - // CSS Color 4 requires alpha to be premultiplied before color interpolation. The - // implicit center therefore averages associated values just like the rendered gradient. - // https://www.w3.org/TR/css-color-4/#interpolation-alpha - sum += color.ToScaledVector4(PixelAlphaRepresentation.Associated); + // The implicit center averages the colors in the alpha representation of the rendered gradient. + sum += color.ToScaledVector4(alphaRepresentation); } - return Color.FromScaledVector(sum / colors.Length, PixelAlphaRepresentation.Associated); + return Color.FromScaledVector(sum / colors.Length, alphaRepresentation); } /// @@ -262,7 +295,7 @@ public override int GetHashCode() /// The path gradient brush applicator. /// /// The pixel format. - /// The destination representation encoder. + /// The encoder for the alpha representation of the interpolated colors. private sealed class PathGradientBrushRenderer : BrushRenderer where TPixel : unmanaged, IPixel where TEncoder : struct, IGradientPixelEncoder @@ -292,33 +325,37 @@ private sealed class PathGradientBrushRenderer : BrushRenderer /// Edges of the polygon. /// Color at the center of the gradient area to which the other colors converge. /// Whether the center color is different from a smooth gradient between the edges. + /// The alpha representation of the colors during interpolation. public PathGradientBrushRenderer( Configuration configuration, GraphicsOptions options, int canvasWidth, IList edges, Color centerColor, - bool hasSpecialCenterColor) + bool hasSpecialCenterColor, + PixelAlphaRepresentation alphaRepresentation) : base(configuration, options, canvasWidth) { this.edges = edges; this.edgeColors = new (Vector4 StartColor, Vector4 EndColor)[edges.Count]; Vector2[] points = [.. edges.Select(s => s.Start)]; - // CSS Color 4 requires alpha to be premultiplied before color interpolation. Cache - // associated inputs without packing through TPixel so destination storage cannot - // change the gradient and conversion remains outside the hot loop. + // CSS Color 4 interpolates associated colors. SVG 2 interpolates unassociated colors. + // The renderer stores the colors in the representation of the brush, without a TPixel + // conversion. The TPixel format cannot change the gradient, and the per-pixel loop + // does not convert the colors. // https://www.w3.org/TR/css-color-4/#interpolation-alpha + // https://www.w3.org/TR/SVG2/pservers.html#StopColorProperties for (int i = 0; i < edges.Count; i++) { this.edgeColors[i] = ( - edges[i].StartColor.ToScaledVector4(PixelAlphaRepresentation.Associated), - edges[i].EndColor.ToScaledVector4(PixelAlphaRepresentation.Associated)); + edges[i].StartColor.ToScaledVector4(alphaRepresentation), + edges[i].EndColor.ToScaledVector4(alphaRepresentation)); } this.center = points.Aggregate((p1, p2) => p1 + p2) / edges.Count; this.hasSpecialCenterColor = hasSpecialCenterColor; - this.centerColor = centerColor.ToScaledVector4(PixelAlphaRepresentation.Associated); + this.centerColor = centerColor.ToScaledVector4(alphaRepresentation); this.centerPixel = TEncoder.Encode(this.centerColor); this.maxDistance = points.Select(p => p - this.center).Max(d => d.Length()); this.transparentPixel = Color.Transparent.ToPixel(); diff --git a/src/ImageSharp.Drawing/Processing/RadialGradientBrush.cs b/src/ImageSharp.Drawing/Processing/RadialGradientBrush.cs index 114406110..01b84c1e0 100644 --- a/src/ImageSharp.Drawing/Processing/RadialGradientBrush.cs +++ b/src/ImageSharp.Drawing/Processing/RadialGradientBrush.cs @@ -45,7 +45,28 @@ public RadialGradientBrush( GradientRepetitionMode repetitionMode, Matrix4x4 gradientTransform, params ColorStop[] colorStops) - : base(repetitionMode, gradientTransform, colorStops) + : this(center, radius, repetitionMode, gradientTransform, PixelAlphaRepresentation.Associated, colorStops) + { + } + + /// + /// Initializes a new instance of the class using a single circle + /// defined in the gradient's own coordinate space. + /// + /// The center of the circular gradient. + /// The radius of the circular gradient. + /// Defines how the colors in the gradient are repeated. + /// The transform from the gradient's coordinate space to the drawing. + /// The alpha representation of the stop colors during interpolation. + /// The ordered gradient stops. + public RadialGradientBrush( + PointF center, + float radius, + GradientRepetitionMode repetitionMode, + Matrix4x4 gradientTransform, + PixelAlphaRepresentation alphaRepresentation, + params ColorStop[] colorStops) + : base(repetitionMode, gradientTransform, alphaRepresentation, colorStops) { this.Center0 = center; this.Radius0 = radius; @@ -92,7 +113,32 @@ public RadialGradientBrush( GradientRepetitionMode repetitionMode, Matrix4x4 gradientTransform, params ColorStop[] colorStops) - : base(repetitionMode, gradientTransform, colorStops) + : this(startCenter, startRadius, endCenter, endRadius, repetitionMode, gradientTransform, PixelAlphaRepresentation.Associated, colorStops) + { + } + + /// + /// Initializes a new instance of the class using two circles + /// defined in the gradient's own coordinate space. + /// + /// The center of the starting circle. + /// The radius of the starting circle. + /// The center of the ending circle. + /// The radius of the ending circle. + /// Defines how the colors in the gradient are repeated. + /// The transform from the gradient's coordinate space to the drawing. + /// The alpha representation of the stop colors during interpolation. + /// The ordered gradient stops. + public RadialGradientBrush( + PointF startCenter, + float startRadius, + PointF endCenter, + float endRadius, + GradientRepetitionMode repetitionMode, + Matrix4x4 gradientTransform, + PixelAlphaRepresentation alphaRepresentation, + params ColorStop[] colorStops) + : base(repetitionMode, gradientTransform, alphaRepresentation, colorStops) { this.Center0 = startCenter; this.Radius0 = startRadius; @@ -138,10 +184,18 @@ public override Brush Transform(Matrix4x4 matrix, Rectangle sourceInterest, Rect if (this.IsTwoCircle) { PointF tc1 = PointF.Transform(this.Center1!.Value, gradientTransform); - return new RadialGradientBrush(tc0, this.Radius0 * scale, tc1, this.Radius1!.Value * scale, this.RepetitionMode, this.ColorStopsArray); + return new RadialGradientBrush( + tc0, + this.Radius0 * scale, + tc1, + this.Radius1!.Value * scale, + this.RepetitionMode, + Matrix4x4.Identity, + this.AlphaRepresentation, + this.ColorStopsArray); } - return new RadialGradientBrush(tc0, this.Radius0 * scale, this.RepetitionMode, this.ColorStopsArray); + return new RadialGradientBrush(tc0, this.Radius0 * scale, this.RepetitionMode, Matrix4x4.Identity, this.AlphaRepresentation, this.ColorStopsArray); } if (this.IsTwoCircle) @@ -153,10 +207,11 @@ public override Brush Transform(Matrix4x4 matrix, Rectangle sourceInterest, Rect this.Radius1!.Value, this.RepetitionMode, gradientTransform, + this.AlphaRepresentation, this.ColorStopsArray); } - return new RadialGradientBrush(this.Center0, this.Radius0, this.RepetitionMode, gradientTransform, this.ColorStopsArray); + return new RadialGradientBrush(this.Center0, this.Radius0, this.RepetitionMode, gradientTransform, this.AlphaRepresentation, this.ColorStopsArray); } /// @@ -185,7 +240,7 @@ public override BrushRenderer CreateRenderer( int canvasWidth, RectangleF region) { - if (TPixel.GetPixelTypeInfo().AlphaRepresentation == PixelAlphaRepresentation.Associated) + if (this.AlphaRepresentation == PixelAlphaRepresentation.Associated) { return new RadialGradientBrushRenderer>(configuration, options, canvasWidth, this); } diff --git a/src/ImageSharp.Drawing/Processing/SweepGradientBrush.cs b/src/ImageSharp.Drawing/Processing/SweepGradientBrush.cs index 96f45987a..50e9892ba 100644 --- a/src/ImageSharp.Drawing/Processing/SweepGradientBrush.cs +++ b/src/ImageSharp.Drawing/Processing/SweepGradientBrush.cs @@ -54,7 +54,30 @@ public SweepGradientBrush( GradientRepetitionMode repetitionMode, Matrix4x4 gradientTransform, params ColorStop[] colorStops) - : base(repetitionMode, gradientTransform, colorStops) + : this(center, startAngleDegrees, endAngleDegrees, repetitionMode, gradientTransform, PixelAlphaRepresentation.Associated, colorStops) + { + } + + /// + /// Initializes a new instance of the class with the center and + /// angles defined in the gradient's own coordinate space. + /// + /// The center point of the sweep gradient. + /// The starting angle, in degrees, measured counter-clockwise from +X on the design grid. + /// The ending angle, in degrees, measured counter-clockwise from +X on the design grid. + /// Defines how the gradient colors are repeated beyond the interval [0..1]. + /// The transform from the gradient's coordinate space to the drawing. + /// The alpha representation of the stop colors during interpolation. + /// The gradient color stops. Ratios must be in [0..1] and are interpreted along the angular sweep. + public SweepGradientBrush( + PointF center, + float startAngleDegrees, + float endAngleDegrees, + GradientRepetitionMode repetitionMode, + Matrix4x4 gradientTransform, + PixelAlphaRepresentation alphaRepresentation, + params ColorStop[] colorStops) + : base(repetitionMode, gradientTransform, alphaRepresentation, colorStops) { this.Center = center; this.StartAngleDegrees = startAngleDegrees; @@ -126,10 +149,19 @@ public override Brush Transform(Matrix4x4 matrix, Rectangle sourceInterest, Rect newStart, UnwrapSweepEndDegrees(newStart, newEnd, directionHint, MathF.Abs(sweepDegrees)), this.RepetitionMode, + Matrix4x4.Identity, + this.AlphaRepresentation, this.ColorStopsArray); } - return new SweepGradientBrush(this.Center, this.StartAngleDegrees, this.EndAngleDegrees, this.RepetitionMode, gradientTransform, this.ColorStopsArray); + return new SweepGradientBrush( + this.Center, + this.StartAngleDegrees, + this.EndAngleDegrees, + this.RepetitionMode, + gradientTransform, + this.AlphaRepresentation, + this.ColorStopsArray); } /// @@ -249,7 +281,7 @@ public override BrushRenderer CreateRenderer( int canvasWidth, RectangleF region) { - if (TPixel.GetPixelTypeInfo().AlphaRepresentation == PixelAlphaRepresentation.Associated) + if (this.AlphaRepresentation == PixelAlphaRepresentation.Associated) { return new SweepGradientBrushRenderer>(configuration, options, canvasWidth, this); } diff --git a/tests/ImageSharp.Drawing.Tests/ConfigurationTests.cs b/tests/ImageSharp.Drawing.Tests/ConfigurationTests.cs index 297656536..1320a7780 100644 --- a/tests/ImageSharp.Drawing.Tests/ConfigurationTests.cs +++ b/tests/ImageSharp.Drawing.Tests/ConfigurationTests.cs @@ -18,7 +18,7 @@ public class ConfigurationTests public Configuration DefaultConfiguration { get; } - private readonly int expectedDefaultConfigurationCount = 13; + private readonly int expectedDefaultConfigurationCount = 15; public ConfigurationTests() { diff --git a/tests/ImageSharp.Drawing.Tests/Processing/Backends/WebGPUDrawingBackendTests.cs b/tests/ImageSharp.Drawing.Tests/Processing/Backends/WebGPUDrawingBackendTests.cs index e3afeffe1..c9eaa0661 100644 --- a/tests/ImageSharp.Drawing.Tests/Processing/Backends/WebGPUDrawingBackendTests.cs +++ b/tests/ImageSharp.Drawing.Tests/Processing/Backends/WebGPUDrawingBackendTests.cs @@ -1811,6 +1811,42 @@ private static void AssertBackendPairSimilarityInRegion( AssertBackendPairSimilarity(defaultRegion, nativeRegion, defaultTolerancePercent); } + [WebGPUTheory] + [WithSolidFilledImages(360, 120, "White", PixelTypes.Rgba32)] + public void FillPen_NonUniformScale_MatchesDefaultOutput(TestImageProvider provider) + where TPixel : unmanaged, IPixel + { + // The transform scales x by 4, so the ring is 4 times wider at the sides than at the top. + DrawingOptions drawingOptions = new() + { + GraphicsOptions = new GraphicsOptions { Antialias = true }, + Transform = Matrix4x4.CreateScale(4F, 1F, 1F) * Matrix4x4.CreateTranslation(180F, 60F, 0F) + }; + + EllipsePolygon circle = new(PointF.Empty, 40F); + Pen pen = Pens.Solid(Color.CornflowerBlue, 8F); + void DrawAction(DrawingCanvas canvas) => canvas.Fill(pen, circle); + + using Image defaultImage = provider.GetImage(); + RenderWithDefaultBackend(defaultImage, drawingOptions, DrawAction); + + using WebGPUDrawingBackend nativeSurfaceBackend = new(); + using Image nativeSurfaceInitialImage = provider.GetImage(); + using Image nativeSurfaceImage = RenderWithNativeSurfaceWebGpuBackend( + defaultImage.Width, + defaultImage.Height, + nativeSurfaceBackend, + drawingOptions, + DrawAction, + nativeSurfaceInitialImage); + + DebugSaveBackendPair(provider, null, defaultImage, nativeSurfaceImage); + + // The measured difference between the two backends is 0.0009%. + AssertBackendPairSimilarity(defaultImage, nativeSurfaceImage, 0.001F); + AssertBackendPairReferenceOutputs(provider, null, defaultImage, nativeSurfaceImage); + } + [WebGPUTheory] [WithSolidFilledImages(400, 300, "White", PixelTypes.Rgba32)] public void DrawPath_Stroke_MatchesDefaultOutput(TestImageProvider provider) @@ -3884,6 +3920,75 @@ public void FillPath_WithZeroLengthLinearGradient_MatchesDefaultEndColor() Assert.Equal(defaultImage[0, 0], actual[0, 0]); } + [WebGPUTheory] + [WithSolidFilledImages(200, 200, "White", PixelTypes.Rgba32, ProcessWithDrawingCanvasTests.GradientKind.Linear, PixelAlphaRepresentation.Associated)] + [WithSolidFilledImages(200, 200, "White", PixelTypes.Rgba32, ProcessWithDrawingCanvasTests.GradientKind.Linear, PixelAlphaRepresentation.Unassociated)] + [WithSolidFilledImages(200, 200, "White", PixelTypes.Rgba32, ProcessWithDrawingCanvasTests.GradientKind.Radial, PixelAlphaRepresentation.Associated)] + [WithSolidFilledImages(200, 200, "White", PixelTypes.Rgba32, ProcessWithDrawingCanvasTests.GradientKind.Radial, PixelAlphaRepresentation.Unassociated)] + [WithSolidFilledImages(200, 200, "White", PixelTypes.Rgba32, ProcessWithDrawingCanvasTests.GradientKind.Elliptic, PixelAlphaRepresentation.Associated)] + [WithSolidFilledImages(200, 200, "White", PixelTypes.Rgba32, ProcessWithDrawingCanvasTests.GradientKind.Elliptic, PixelAlphaRepresentation.Unassociated)] + [WithSolidFilledImages(200, 200, "White", PixelTypes.Rgba32, ProcessWithDrawingCanvasTests.GradientKind.Sweep, PixelAlphaRepresentation.Associated)] + [WithSolidFilledImages(200, 200, "White", PixelTypes.Rgba32, ProcessWithDrawingCanvasTests.GradientKind.Sweep, PixelAlphaRepresentation.Unassociated)] + [WithSolidFilledImages(200, 200, "White", PixelTypes.Rgba32, ProcessWithDrawingCanvasTests.GradientKind.Path, PixelAlphaRepresentation.Associated)] + [WithSolidFilledImages(200, 200, "White", PixelTypes.Rgba32, ProcessWithDrawingCanvasTests.GradientKind.Path, PixelAlphaRepresentation.Unassociated)] + public void FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput( + TestImageProvider provider, + ProcessWithDrawingCanvasTests.GradientKind kind, + PixelAlphaRepresentation alphaRepresentation) + where TPixel : unmanaged, IPixel + { + DrawingOptions drawingOptions = new() + { + GraphicsOptions = new GraphicsOptions { Antialias = true } + }; + + // Opaque red to transparent blue. Associated interpolation fades the red to transparent. + // Unassociated interpolation shows purple between the two colors. + Color start = Color.Red; + Color end = Color.Blue.WithAlpha(0F); + ColorStop[] stops = [new(0, start), new(1, end)]; + GradientRepetitionMode none = GradientRepetitionMode.None; + Matrix4x4 identity = Matrix4x4.Identity; + PointF center = new(100, 100); + + Brush brush = kind switch + { + ProcessWithDrawingCanvasTests.GradientKind.Linear => + new LinearGradientBrush(new PointF(0, 100), new PointF(200, 100), none, identity, alphaRepresentation, stops), + ProcessWithDrawingCanvasTests.GradientKind.Radial => new RadialGradientBrush(center, 100F, none, identity, alphaRepresentation, stops), + ProcessWithDrawingCanvasTests.GradientKind.Elliptic => + new EllipticGradientBrush(center, new PointF(200, 100), 0.5F, none, identity, alphaRepresentation, stops), + ProcessWithDrawingCanvasTests.GradientKind.Sweep => new SweepGradientBrush(center, 0F, 360F, none, identity, alphaRepresentation, stops), + ProcessWithDrawingCanvasTests.GradientKind.Path => new PathGradientBrush( + [new(0, 0), new(200, 0), new(200, 200), new(0, 200)], + [start, start, end, end], + alphaRepresentation), + _ => throw new ArgumentOutOfRangeException(nameof(kind)) + }; + + void DrawAction(DrawingCanvas canvas) => canvas.Fill(brush, new RectanglePolygon(0, 0, 200, 200)); + + using Image defaultImage = provider.GetImage(); + RenderWithDefaultBackend(defaultImage, drawingOptions, DrawAction); + + using WebGPUDrawingBackend nativeSurfaceBackend = new(); + using Image nativeSurfaceInitialImage = provider.GetImage(); + using Image nativeSurfaceImage = RenderWithNativeSurfaceWebGpuBackend( + defaultImage.Width, + defaultImage.Height, + nativeSurfaceBackend, + drawingOptions, + DrawAction, + nativeSurfaceInitialImage); + + string testName = $"{kind}_{alphaRepresentation}"; + DebugSaveBackendPair(provider, testName, defaultImage, nativeSurfaceImage); + + // The largest measured difference between the two backends is 0.0382%, for the unassociated path gradient. + AssertBackendPairSimilarity(defaultImage, nativeSurfaceImage, 0.0383F); + AssertBackendPairReferenceOutputs(provider, testName, defaultImage, nativeSurfaceImage); + } + [WebGPUFact] public void FillPath_WithDontFillGradients_ComposesTransparentOutsideGradient() { diff --git a/tests/ImageSharp.Drawing.Tests/Processing/DrawingCanvasTests.PathRules.cs b/tests/ImageSharp.Drawing.Tests/Processing/DrawingCanvasTests.PathRules.cs index 3b9ee9e7a..b1eee100b 100644 --- a/tests/ImageSharp.Drawing.Tests/Processing/DrawingCanvasTests.PathRules.cs +++ b/tests/ImageSharp.Drawing.Tests/Processing/DrawingCanvasTests.PathRules.cs @@ -128,6 +128,61 @@ public void Clip_DifferenceWithMultiplePaths_MatchesSequentialDifferenceClips(TestImageProvider provider) + where TPixel : unmanaged, IPixel + { + using Image expected = provider.GetImage(); + using Image actual = provider.GetImage(); + using Image evenOdd = provider.GetImage(); + IPath path = CreateBowTiePath(new RectangleF(28, 34, 128, 152)); + + SolidPen pen = new(Color.CornflowerBlue, 24F); + pen.StrokeOptions.LineJoin = LineJoin.Round; + pen.StrokeOptions.LineCap = LineCap.Round; + IPath stroke = pen.GeneratePath(path); + + DrawingOptions evenOddOptions = new() + { + IntersectionRule = IntersectionRule.EvenOdd + }; + + DrawingOptions nonZeroOptions = new() + { + IntersectionRule = IntersectionRule.NonZero + }; + + using (DrawingCanvas canvas = CreateCanvas(provider, expected, nonZeroOptions)) + { + canvas.Fill(pen.StrokeFill, stroke); + } + + // The state uses the even-odd rule. Fill(Pen, IPath) must still fill the stroke with the non-zero rule. + using (DrawingCanvas canvas = CreateCanvas(provider, actual, evenOddOptions)) + { + canvas.Fill(pen, path); + } + + using (DrawingCanvas canvas = CreateCanvas(provider, evenOdd, evenOddOptions)) + { + canvas.Fill(pen.StrokeFill, stroke); + } + + expected.DebugSave(provider, "expected-nonzero-stroke", appendSourceFileOrDescription: false); + actual.DebugSave(provider, "actual-fill-pen", appendSourceFileOrDescription: false); + evenOdd.DebugSave(provider, "evenodd-stroke", appendSourceFileOrDescription: false); + + ImageComparer.Exact.VerifySimilarity(expected, actual); + + // The bow tie stroke overlaps itself, so the even-odd fill of the same stroke has holes. + // This proves that the scene can show a wrong fill rule. + Assert.Throws(() => ImageComparer.Exact.VerifySimilarity(evenOdd, actual)); + + actual.CompareToReferenceOutput(provider, "actual-fill-pen", appendSourceFileOrDescription: false); + evenOdd.CompareToReferenceOutput(provider, "evenodd-stroke", appendSourceFileOrDescription: false); + } + /// /// Creates a rectangle path that is equivalent to but intentionally /// has five vertices so the rectangle fast path does not recognize it. diff --git a/tests/ImageSharp.Drawing.Tests/Processing/ProcessWithDrawingCanvasTests.AssociatedAlpha.cs b/tests/ImageSharp.Drawing.Tests/Processing/ProcessWithDrawingCanvasTests.AssociatedAlpha.cs index 04e52554f..1092380e4 100644 --- a/tests/ImageSharp.Drawing.Tests/Processing/ProcessWithDrawingCanvasTests.AssociatedAlpha.cs +++ b/tests/ImageSharp.Drawing.Tests/Processing/ProcessWithDrawingCanvasTests.AssociatedAlpha.cs @@ -59,6 +59,126 @@ public void EllipticGradientUsesCssColor4AlphaInterpolation() public void SweepGradientUsesCssColor4AlphaInterpolation() => AssertGradientAssociationSimilarity(GradientKind.Sweep, GradientRepetitionMode.None, false); + /// + /// Verifies that each gradient brush interpolates its colors in the alpha representation of the brush. + /// + /// The gradient family. + /// The alpha representation of the brush. + [Theory] + [InlineData(GradientKind.Linear, PixelAlphaRepresentation.Associated)] + [InlineData(GradientKind.Linear, PixelAlphaRepresentation.Unassociated)] + [InlineData(GradientKind.Radial, PixelAlphaRepresentation.Associated)] + [InlineData(GradientKind.Radial, PixelAlphaRepresentation.Unassociated)] + [InlineData(GradientKind.Elliptic, PixelAlphaRepresentation.Associated)] + [InlineData(GradientKind.Elliptic, PixelAlphaRepresentation.Unassociated)] + [InlineData(GradientKind.Sweep, PixelAlphaRepresentation.Associated)] + [InlineData(GradientKind.Sweep, PixelAlphaRepresentation.Unassociated)] + [InlineData(GradientKind.Path, PixelAlphaRepresentation.Associated)] + [InlineData(GradientKind.Path, PixelAlphaRepresentation.Unassociated)] + public void GradientInterpolatesInBrushAlphaRepresentation(GradientKind kind, PixelAlphaRepresentation alphaRepresentation) + { + (Brush brush, Point sample) = CreateAlphaMidpointGradient(kind, alphaRepresentation); + DrawingOptions options = CreateBlendOptions(PixelColorBlendingMode.Normal, PixelAlphaCompositionMode.Src); + + // Associated: CSS Color 4 premultiplies (254, 0, 0, 254) and (0, 0, 254, 0) before it interpolates. + // The midpoint is the start red with half its alpha. Rgba32P stores 254 * 127 / 255 = 126.502, which rounds to 127. + // Unassociated: SVG 2 interpolates each channel. The midpoint is (127, 0, 127, 127). + // Rgba32P stores 127 * 127 / 255 = 63.25, which rounds to 63. + // https://www.w3.org/TR/css-color-4/#interpolation-alpha + // https://www.w3.org/TR/SVG2/pservers.html#StopColorProperties + bool associated = alphaRepresentation == PixelAlphaRepresentation.Associated; + Rgba32 expected = associated ? new Rgba32(254, 0, 0, 127) : new Rgba32(127, 0, 127, 127); + Rgba32P expectedAssociated = associated ? new Rgba32P(127, 0, 0, 127) : new Rgba32P(63, 0, 63, 127); + + using Image image = new(AlphaMidpointImageSize, AlphaMidpointImageSize); + using Image associatedImage = new(AlphaMidpointImageSize, AlphaMidpointImageSize); + image.Mutate(context => context.Paint(options, canvas => canvas.Fill(brush))); + associatedImage.Mutate(context => context.Paint(options, canvas => canvas.Fill(brush))); + + Assert.Equal(expected, image[sample.X, sample.Y]); + Assert.Equal(expectedAssociated, associatedImage[sample.X, sample.Y]); + } + + /// + /// Verifies that an affine transform and a projective transform keep the alpha representation of each gradient brush. + /// + /// The gradient family. + [Theory] + [InlineData(GradientKind.Linear)] + [InlineData(GradientKind.Radial)] + [InlineData(GradientKind.Elliptic)] + [InlineData(GradientKind.Sweep)] + [InlineData(GradientKind.Path)] + public void GradientTransformKeepsAlphaRepresentation(GradientKind kind) + { + (Brush brush, _) = CreateAlphaMidpointGradient(kind, PixelAlphaRepresentation.Unassociated); + Rectangle interest = new(0, 0, AlphaMidpointImageSize, AlphaMidpointImageSize); + Matrix4x4 affine = Matrix4x4.CreateScale(2F, 3F, 1F); + Matrix4x4 projective = Matrix4x4.Identity; + projective.M14 = 0.001F; + + Assert.Equal(PixelAlphaRepresentation.Unassociated, GetAlphaRepresentation(brush.Transform(affine, interest, interest))); + Assert.Equal(PixelAlphaRepresentation.Unassociated, GetAlphaRepresentation(brush.Transform(projective, interest, interest))); + } + + /// + /// Gets the alpha representation of a gradient brush or a path gradient brush. + /// + /// The brush. + /// The alpha representation of the brush. + private static PixelAlphaRepresentation GetAlphaRepresentation(Brush brush) + => brush is PathGradientBrush pathBrush ? pathBrush.AlphaRepresentation : ((GradientBrush)brush).AlphaRepresentation; + + /// + /// Gets the width and the height of the image that targets. + /// + private const int AlphaMidpointImageSize = 9; + + /// + /// Creates a gradient from (254, 0, 0, 254) to (0, 0, 254, 0) with its midpoint at the center of one pixel. + /// + /// + /// The stop bytes are even, so each unassociated channel of the midpoint is a whole byte. A small float + /// error in the gradient position cannot change the rounding of the midpoint. + /// + /// The gradient family. + /// The alpha representation of the brush. + /// The brush, and the pixel that has the midpoint color. + private static (Brush Brush, Point Sample) CreateAlphaMidpointGradient(GradientKind kind, PixelAlphaRepresentation alphaRepresentation) + { + Color start = Color.FromPixel(new Rgba32(254, 0, 0, 254)); + Color end = Color.FromPixel(new Rgba32(0, 0, 254, 0)); + ColorStop[] stops = [new(0, start), new(1, end)]; + + // Pixel (4, 4) has its center at (4.5, 4.5). The linear axis, the radius and the reference axis + // are 8 pixels long and start 4 pixels from that center. + PointF axisStart = new(0.5F, 4.5F); + PointF axisEnd = new(8.5F, 4.5F); + Point middle = new(4, 4); + + GradientRepetitionMode none = GradientRepetitionMode.None; + Matrix4x4 identity = Matrix4x4.Identity; + + return kind switch + { + GradientKind.Linear => (new LinearGradientBrush(axisStart, axisEnd, none, identity, alphaRepresentation, stops), middle), + GradientKind.Radial => (new RadialGradientBrush(axisStart, 8F, none, identity, alphaRepresentation, stops), middle), + GradientKind.Elliptic => (new EllipticGradientBrush(axisStart, axisEnd, 0.5F, none, identity, alphaRepresentation, stops), middle), + + // The sweep goes from 0 to 180 degrees. Pixel (4, 0) is straight above the center, at 90 degrees. + GradientKind.Sweep => (new SweepGradientBrush(new PointF(4.5F, 8.5F), 0F, 180F, none, identity, alphaRepresentation, stops), new Point(4, 0)), + + // The default center color of a path gradient is the mean of its point colors. Two start colors + // and two end colors give the midpoint at the center and at the middle of the right edge. + // Pixel (6, 4) lies between them, so the renderer interpolates along the edge and toward the center. + GradientKind.Path => (new PathGradientBrush( + [new(0.5F, 0.5F), new(8.5F, 0.5F), new(8.5F, 8.5F), new(0.5F, 8.5F)], + [start, start, end, end], + alphaRepresentation), new Point(6, 4)), + _ => throw new ArgumentOutOfRangeException(nameof(kind)) + }; + } + /// /// Verifies equal gradient stops use the same representation-independent result as the /// general interpolation path. @@ -111,7 +231,7 @@ public void RecolorBrushAssociatedDestinationUsesAssociatedComponents() float scaledThreshold = threshold * 4F; float distance = Vector4.DistanceSquared(backgroundPixel.ToScaledVector4(), source.ToScaledVector4(PixelAlphaRepresentation.Associated)); float amount = (scaledThreshold - distance) / scaledThreshold; - PixelBlender blender = PixelOperations.Instance.GetPixelBlender(options.GraphicsOptions); + PixelBlender blender = PixelBlenders.GetPixelBlender(options.GraphicsOptions); Rgba32P expected = blender.Blend(backgroundPixel, targetPixel, amount); // The straight RGB distance is two, but multiplying both colors by their low alpha @@ -423,7 +543,8 @@ private static Brush CreateGradient(GradientKind kind, GradientRepetitionMode re GradientKind.Linear => new LinearGradientBrush(new PointF(-4, 2), new PointF(12, 6), repetitionMode, colorStops), GradientKind.Radial => new RadialGradientBrush(new PointF(4, 4), 5F, repetitionMode, colorStops), GradientKind.Elliptic => new EllipticGradientBrush(new PointF(4, 4), new PointF(9, 4), 0.6F, repetitionMode, colorStops), - _ => new SweepGradientBrush(new PointF(4, 4), -45F, 315F, repetitionMode, colorStops) + GradientKind.Sweep => new SweepGradientBrush(new PointF(4, 4), -45F, 315F, repetitionMode, colorStops), + _ => throw new ArgumentOutOfRangeException(nameof(kind)) }; /// @@ -563,7 +684,7 @@ private static void AssertRecolorUsesConfiguredBlender(Rgba32 source, Rg using Image actual = RenderRecolor(source, brush, options); TPixel sourcePixel = Color.FromPixel(source).ToPixel(); TPixel targetPixel = Color.FromPixel(target).ToPixel(); - PixelBlender blender = PixelOperations.Instance.GetPixelBlender(options.GraphicsOptions); + PixelBlender blender = PixelBlenders.GetPixelBlender(options.GraphicsOptions); // An exact key match has replacement amount one. Full shape coverage then applies the // same configured blender a second time with the DrawingOptions blend percentage. @@ -790,13 +911,14 @@ private static void AssertAssociatedStorage(Image image) }); /// - /// Identifies the gradient family used by the shared representation test. + /// Identifies the gradient family used by the shared representation tests. /// - private enum GradientKind + public enum GradientKind { Linear, Radial, Elliptic, - Sweep + Sweep, + Path } } diff --git a/tests/ImageSharp.Drawing.Tests/Processing/ProcessWithDrawingCanvasTests.FillSolidBrush.cs b/tests/ImageSharp.Drawing.Tests/Processing/ProcessWithDrawingCanvasTests.FillSolidBrush.cs index 87191fc1f..955083fcd 100644 --- a/tests/ImageSharp.Drawing.Tests/Processing/ProcessWithDrawingCanvasTests.FillSolidBrush.cs +++ b/tests/ImageSharp.Drawing.Tests/Processing/ProcessWithDrawingCanvasTests.FillSolidBrush.cs @@ -183,7 +183,7 @@ public void FillSolidBrush_BlendFillColorOverBackground( appendPixelTypeToFileName: false, appendSourceFileOrDescription: false); - PixelBlender blender = PixelOperations.Instance.GetPixelBlender( + PixelBlender blender = PixelBlenders.GetPixelBlender( blenderMode, PixelAlphaCompositionMode.SrcOver); TPixel expectedPixel = blender.Blend(bgColor, fillColor.ToPixel(), blendPercentage); diff --git a/tests/ImageSharp.Drawing.Tests/Processing/ProcessWithDrawingCanvasTests.PathGradientBrushes.cs b/tests/ImageSharp.Drawing.Tests/Processing/ProcessWithDrawingCanvasTests.PathGradientBrushes.cs index f4471fe60..3c63f61ba 100644 --- a/tests/ImageSharp.Drawing.Tests/Processing/ProcessWithDrawingCanvasTests.PathGradientBrushes.cs +++ b/tests/ImageSharp.Drawing.Tests/Processing/ProcessWithDrawingCanvasTests.PathGradientBrushes.cs @@ -69,20 +69,21 @@ public void FillPathGradientBrushGeneralCasePreservesAlphaRepresentation } [Theory] - [WithBlankImage(20, 20, PixelTypes.HalfSingle)] + [WithBlankImage(200, 200, PixelTypes.HalfSingle)] public void FillPathGradientBrushFillTriangleWithHalfSingleRedChannel(TestImageProvider provider) where TPixel : unmanaged, IPixel => provider.VerifyOperation( ImageComparer.TolerantPercentage(0.02f), image => { - PointF[] points = [new(10, 0), new(20, 20), new(0, 20)]; + PointF[] points = [new(100, 0), new(200, 200), new(0, 200)]; // HalfSingle represents DXGI_FORMAT_R16_FLOAT, so its single component is red. - // Use the finite binary16 endpoints to cover the complete scaled [0, 1] range. - Color c1 = Color.FromPixel(new HalfSingle((float)Half.MinValue)); - Color c2 = Color.FromPixel(new HalfSingle(0)); - Color c3 = Color.FromPixel(new HalfSingle((float)Half.MaxValue)); + // HalfSingle stores HDR values, and the PNG reference clamps them to [0, 1]. + // The values 0, 0.5 and 1 keep the gradient visible in the reference image. + Color c1 = Color.FromPixel(new HalfSingle(0F)); + Color c2 = Color.FromPixel(new HalfSingle(0.5F)); + Color c3 = Color.FromPixel(new HalfSingle(1F)); Color[] colors = [c1, c2, c3]; diff --git a/tests/ImageSharp.Drawing.Tests/TestFile.cs b/tests/ImageSharp.Drawing.Tests/TestFile.cs index 345ad6791..3ba92513b 100644 --- a/tests/ImageSharp.Drawing.Tests/TestFile.cs +++ b/tests/ImageSharp.Drawing.Tests/TestFile.cs @@ -107,7 +107,7 @@ public string GetFileNameWithoutExtension(object value) /// The . /// public Image CreateRgba32Image(IImageDecoder decoder) - => this.CreateRgba32Image(decoder, new DecoderOptions()); + => this.CreateRgba32Image(decoder, new DecoderOptions(Configuration.Default)); /// /// Creates a new image. diff --git a/tests/ImageSharp.Drawing.Tests/TestUtilities/ImageProviders/TestImageProvider.cs b/tests/ImageSharp.Drawing.Tests/TestUtilities/ImageProviders/TestImageProvider.cs index 64c1bdaf2..492632bfc 100644 --- a/tests/ImageSharp.Drawing.Tests/TestUtilities/ImageProviders/TestImageProvider.cs +++ b/tests/ImageSharp.Drawing.Tests/TestUtilities/ImageProviders/TestImageProvider.cs @@ -88,10 +88,10 @@ public static TestImageProvider Solid( public abstract Image GetImage(); public Image GetImage(IImageDecoder decoder) - => this.GetImage(decoder, new DecoderOptions()); + => this.GetImage(decoder, new DecoderOptions(Configuration.Default)); public Task> GetImageAsync(IImageDecoder decoder) - => this.GetImageAsync(decoder, new DecoderOptions()); + => this.GetImageAsync(decoder, new DecoderOptions(Configuration.Default)); public virtual Image GetImage(IImageDecoder decoder, DecoderOptions options) => throw new NotSupportedException($"Decoder specific GetImage() is not supported with {this.GetType().Name}!"); diff --git a/tests/ImageSharp.Drawing.Tests/TestUtilities/TestImageExtensions.cs b/tests/ImageSharp.Drawing.Tests/TestUtilities/TestImageExtensions.cs index 24c39f696..e5cc12c33 100644 --- a/tests/ImageSharp.Drawing.Tests/TestUtilities/TestImageExtensions.cs +++ b/tests/ImageSharp.Drawing.Tests/TestUtilities/TestImageExtensions.cs @@ -756,14 +756,14 @@ public void Invoke(in RowInterval rows, Span span) for (int y = rows.Min; y < rows.Max; y++) { Span rowSpan = this.source.DangerousGetRowSpan(y).Slice(this.bounds.Left, this.bounds.Width); - PixelOperations.Instance.ToVector4(this.configuration, rowSpan, span, PixelConversionModifiers.Scale); + PixelOperations.Instance.ToUnassociatedScaledVector4(this.configuration, rowSpan, span); for (int i = 0; i < span.Length; i++) { ref Vector4 v = ref span[i]; v.W = 1F; } - PixelOperations.Instance.FromVector4Destructive(this.configuration, span, rowSpan, PixelConversionModifiers.Scale); + PixelOperations.Instance.FromUnassociatedScaledVector4Destructive(this.configuration, span, rowSpan); } } } diff --git a/tests/ImageSharp.Drawing.Tests/TestUtilities/Tests/TestImageProviderTests.cs b/tests/ImageSharp.Drawing.Tests/TestUtilities/Tests/TestImageProviderTests.cs index 648924fc3..a1697e602 100644 --- a/tests/ImageSharp.Drawing.Tests/TestUtilities/Tests/TestImageProviderTests.cs +++ b/tests/ImageSharp.Drawing.Tests/TestUtilities/Tests/TestImageProviderTests.cs @@ -445,7 +445,7 @@ internal void InitCaller(string name) private class TestDecoderOptions : ISpecializedDecoderOptions { - public DecoderOptions GeneralOptions { get; init; } = new(); + public DecoderOptions GeneralOptions { get; init; } = new(Configuration.Default); } private class TestDecoderWithParametersOptions : ISpecializedDecoderOptions @@ -454,6 +454,6 @@ private class TestDecoderWithParametersOptions : ISpecializedDecoderOptions public int Param2 { get; init; } - public DecoderOptions GeneralOptions { get; init; } = new(); + public DecoderOptions GeneralOptions { get; init; } = new(Configuration.Default); } } diff --git a/tests/Images/ReferenceOutput/Drawing/DrawingCanvasTests/FillPen_SelfIntersectingStroke_UsesNonZeroUnderEvenOdd_Rgba32_actual-fill-pen.png b/tests/Images/ReferenceOutput/Drawing/DrawingCanvasTests/FillPen_SelfIntersectingStroke_UsesNonZeroUnderEvenOdd_Rgba32_actual-fill-pen.png new file mode 100644 index 000000000..9a2503d45 --- /dev/null +++ b/tests/Images/ReferenceOutput/Drawing/DrawingCanvasTests/FillPen_SelfIntersectingStroke_UsesNonZeroUnderEvenOdd_Rgba32_actual-fill-pen.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:020cf7a86388e29b026d5c4a208fb41087fe36626b1268f21bd0aba883669afa +size 3745 diff --git a/tests/Images/ReferenceOutput/Drawing/DrawingCanvasTests/FillPen_SelfIntersectingStroke_UsesNonZeroUnderEvenOdd_Rgba32_evenodd-stroke.png b/tests/Images/ReferenceOutput/Drawing/DrawingCanvasTests/FillPen_SelfIntersectingStroke_UsesNonZeroUnderEvenOdd_Rgba32_evenodd-stroke.png new file mode 100644 index 000000000..fa77be275 --- /dev/null +++ b/tests/Images/ReferenceOutput/Drawing/DrawingCanvasTests/FillPen_SelfIntersectingStroke_UsesNonZeroUnderEvenOdd_Rgba32_evenodd-stroke.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5bb103317d05d52995f50ceb046c9907b794487954392444a94ea2a8b62e501f +size 4272 diff --git a/tests/Images/ReferenceOutput/Drawing/ProcessWithDrawingCanvasTests/FillPathGradientBrushFillTriangleWithHalfSingleRedChannel_HalfSingle_Blank200x200.png b/tests/Images/ReferenceOutput/Drawing/ProcessWithDrawingCanvasTests/FillPathGradientBrushFillTriangleWithHalfSingleRedChannel_HalfSingle_Blank200x200.png new file mode 100644 index 000000000..2bddcd341 --- /dev/null +++ b/tests/Images/ReferenceOutput/Drawing/ProcessWithDrawingCanvasTests/FillPathGradientBrushFillTriangleWithHalfSingleRedChannel_HalfSingle_Blank200x200.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f6c247df2a00c114426607cc7c96c4cca5dae638bf4200464c0cba1cee9fa2f6 +size 3031 diff --git a/tests/Images/ReferenceOutput/Drawing/ProcessWithDrawingCanvasTests/FillPathGradientBrushFillTriangleWithHalfSingleRedChannel_HalfSingle_Blank20x20.png b/tests/Images/ReferenceOutput/Drawing/ProcessWithDrawingCanvasTests/FillPathGradientBrushFillTriangleWithHalfSingleRedChannel_HalfSingle_Blank20x20.png deleted file mode 100644 index 8d4a81f75..000000000 --- a/tests/Images/ReferenceOutput/Drawing/ProcessWithDrawingCanvasTests/FillPathGradientBrushFillTriangleWithHalfSingleRedChannel_HalfSingle_Blank20x20.png +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:f38baa9ef4e4fa2484a2036f2b03f0aed4f2d82b5ffd42cba01643a12552621c -size 240 diff --git a/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Elliptic_Associated_Default.png b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Elliptic_Associated_Default.png new file mode 100644 index 000000000..514dcdda7 --- /dev/null +++ b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Elliptic_Associated_Default.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:688d2282f298c1a1de106525b9c0a56968df655006394ddd6765d512a96705fd +size 4341 diff --git a/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Elliptic_Associated_WebGPU_NativeSurface.png b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Elliptic_Associated_WebGPU_NativeSurface.png new file mode 100644 index 000000000..5b013e1b2 --- /dev/null +++ b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Elliptic_Associated_WebGPU_NativeSurface.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ed683e66ea2aa900a0c086bac9d7c2eab854c6b4c79fbf8c805a43529fa42ab5 +size 4457 diff --git a/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Elliptic_Unassociated_Default.png b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Elliptic_Unassociated_Default.png new file mode 100644 index 000000000..7fa0da212 --- /dev/null +++ b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Elliptic_Unassociated_Default.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5fe7b1c6169ff192e51f191463c46fe13aff30fab652bdf80bb10e571d4d4bcb +size 9334 diff --git a/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Elliptic_Unassociated_WebGPU_NativeSurface.png b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Elliptic_Unassociated_WebGPU_NativeSurface.png new file mode 100644 index 000000000..a67042b6f --- /dev/null +++ b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Elliptic_Unassociated_WebGPU_NativeSurface.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:75123ad7d548cdc940181e0949cd8d4f0ab86cfed1203dd16fda7806a4f23588 +size 9740 diff --git a/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Linear_Associated_Default.png b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Linear_Associated_Default.png new file mode 100644 index 000000000..75c6a0e91 --- /dev/null +++ b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Linear_Associated_Default.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:59fa88763693b215a770c527d33986f03c991c460aac3a6137083a97df45c507 +size 633 diff --git a/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Linear_Associated_WebGPU_NativeSurface.png b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Linear_Associated_WebGPU_NativeSurface.png new file mode 100644 index 000000000..7fad68f38 --- /dev/null +++ b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Linear_Associated_WebGPU_NativeSurface.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:578287d46ecdf8440d5a44ba77180ecd179b4ea0a814244cfcd3247192c87c5f +size 629 diff --git a/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Linear_Unassociated_Default.png b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Linear_Unassociated_Default.png new file mode 100644 index 000000000..911602127 --- /dev/null +++ b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Linear_Unassociated_Default.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f098e7b4d44cb180eba4c4be7735e8c3fb30b3d6e8c7387f131f0b929ab04cad +size 731 diff --git a/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Linear_Unassociated_WebGPU_NativeSurface.png b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Linear_Unassociated_WebGPU_NativeSurface.png new file mode 100644 index 000000000..0dcc2faac --- /dev/null +++ b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Linear_Unassociated_WebGPU_NativeSurface.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bf90b7a9b5c7dbc756c0880c20ebcdd07200a814632d087fefd6191891deca46 +size 751 diff --git a/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Path_Associated_Default.png b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Path_Associated_Default.png new file mode 100644 index 000000000..0030c83e8 --- /dev/null +++ b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Path_Associated_Default.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ab50d36a3d82f2710da920e488ce896c9a73185f8175f7dac9615aa6fb5d386a +size 661 diff --git a/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Path_Associated_WebGPU_NativeSurface.png b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Path_Associated_WebGPU_NativeSurface.png new file mode 100644 index 000000000..0030c83e8 --- /dev/null +++ b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Path_Associated_WebGPU_NativeSurface.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ab50d36a3d82f2710da920e488ce896c9a73185f8175f7dac9615aa6fb5d386a +size 661 diff --git a/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Path_Unassociated_Default.png b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Path_Unassociated_Default.png new file mode 100644 index 000000000..1db2b1041 --- /dev/null +++ b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Path_Unassociated_Default.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7069bb1455cc3b14d59a48a7e4980a0e43428287e94fe7e87b95b8a738631bce +size 714 diff --git a/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Path_Unassociated_WebGPU_NativeSurface.png b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Path_Unassociated_WebGPU_NativeSurface.png new file mode 100644 index 000000000..ddfb7cdef --- /dev/null +++ b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Path_Unassociated_WebGPU_NativeSurface.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ae9304142cafd967b439a052061aef150e9ec7b7335b84cf2272c83264948912 +size 755 diff --git a/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Radial_Associated_Default.png b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Radial_Associated_Default.png new file mode 100644 index 000000000..c9c9987d9 --- /dev/null +++ b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Radial_Associated_Default.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b444c4c42d6dc478f935d0041ef6c22509cef5f1d09a7d44861d6659e29189b8 +size 7285 diff --git a/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Radial_Associated_WebGPU_NativeSurface.png b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Radial_Associated_WebGPU_NativeSurface.png new file mode 100644 index 000000000..02cc0f909 --- /dev/null +++ b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Radial_Associated_WebGPU_NativeSurface.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e38b14024030e8af1fde13d96325c2cea134914bb5bf3aa537221fbff153b8d4 +size 7352 diff --git a/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Radial_Unassociated_Default.png b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Radial_Unassociated_Default.png new file mode 100644 index 000000000..82b0fe620 --- /dev/null +++ b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Radial_Unassociated_Default.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:62cc3282079405100da887ff6b31a8205f26ed6c80dca6716eee23f3b806dba1 +size 14631 diff --git a/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Radial_Unassociated_WebGPU_NativeSurface.png b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Radial_Unassociated_WebGPU_NativeSurface.png new file mode 100644 index 000000000..15cca9d2b --- /dev/null +++ b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Radial_Unassociated_WebGPU_NativeSurface.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:430381be56c73b8947384341277705e5d65d6398c3112d4fcc82344f40a08a87 +size 15599 diff --git a/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Sweep_Associated_Default.png b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Sweep_Associated_Default.png new file mode 100644 index 000000000..981ca28f8 --- /dev/null +++ b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Sweep_Associated_Default.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:023c6ba2b78d4a661b6294380ed838b8b01082677bb4c29b25c5ff6bb7a1ed6a +size 7346 diff --git a/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Sweep_Associated_WebGPU_NativeSurface.png b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Sweep_Associated_WebGPU_NativeSurface.png new file mode 100644 index 000000000..e438fe920 --- /dev/null +++ b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Sweep_Associated_WebGPU_NativeSurface.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fba9b208df4734b794d32eb05d5ea2e723db14f276370ae1fe9300ee36987d97 +size 7403 diff --git a/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Sweep_Unassociated_Default.png b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Sweep_Unassociated_Default.png new file mode 100644 index 000000000..1ae1a242e --- /dev/null +++ b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Sweep_Unassociated_Default.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2ee9f325ff3f7a5e96c8094ce34b57db0463263e6f8d01678d4ad8e4f85d955c +size 9970 diff --git a/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Sweep_Unassociated_WebGPU_NativeSurface.png b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Sweep_Unassociated_WebGPU_NativeSurface.png new file mode 100644 index 000000000..02503bff7 --- /dev/null +++ b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPath_WithGradientAlphaRepresentation_MatchesDefaultOutput_Sweep_Unassociated_WebGPU_NativeSurface.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2d8ab6d95a66463119d36902c0e5fb55d80c292c8141b7e0e1e815d840b2f441 +size 10370 diff --git a/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPen_NonUniformScale_MatchesDefaultOutput_Default.png b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPen_NonUniformScale_MatchesDefaultOutput_Default.png new file mode 100644 index 000000000..943e17480 --- /dev/null +++ b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPen_NonUniformScale_MatchesDefaultOutput_Default.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:313722361da4b600f060cc467f1c2bfd056296909b58dbfbb4c63c82ed25ef64 +size 6251 diff --git a/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPen_NonUniformScale_MatchesDefaultOutput_WebGPU_NativeSurface.png b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPen_NonUniformScale_MatchesDefaultOutput_WebGPU_NativeSurface.png new file mode 100644 index 000000000..8f7a935cd --- /dev/null +++ b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/FillPen_NonUniformScale_MatchesDefaultOutput_WebGPU_NativeSurface.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7aa9b3851fa00c1bbbbc2b3682c102aaec94aa27c6cb8f87ff769dca353bd913 +size 6136 From 2ccdbe23fcd99b7edc6d08811f1d2c07cd09d30e Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Fri, 27 Nov 2026 14:31:14 +1000 Subject: [PATCH 2/2] Use the full-precision tint color in the CPU layer effects The CPU acrylic, drop shadow, backdrop drop shadow, glow and inner shadow effects converted the tint color to Rgba32 before they built the color matrix. That cut the tint to 8 bits for each channel, while the WebGPU effects use the full-precision color. The CPU effects now use Color.ToScaledVector4 with the same alpha representation as their WebGPU counterparts. --- src/ImageSharp.Drawing/Processing/BackdropLayerEffect.cs | 4 ++-- src/ImageSharp.Drawing/Processing/LayerEffect.cs | 6 +++--- ...terLayerEffects_MatchesDefaultOutput_acrylic_Default.png | 4 ++-- ...ts_MatchesDefaultOutput_acrylic_WebGPU_NativeSurface.png | 4 ++-- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/ImageSharp.Drawing/Processing/BackdropLayerEffect.cs b/src/ImageSharp.Drawing/Processing/BackdropLayerEffect.cs index 9eb628848..976bbab69 100644 --- a/src/ImageSharp.Drawing/Processing/BackdropLayerEffect.cs +++ b/src/ImageSharp.Drawing/Processing/BackdropLayerEffect.cs @@ -109,7 +109,7 @@ private static Action CreateOperation(float sigma, Colo // Blending a constant colour over the backdrop is linear, so it is expressed as a colour // matrix: the backdrop's channels scale by one minus the tint's alpha and the constant row // carries the pre-scaled tint. The backdrop's own alpha is preserved. - Vector4 vector = color.ToPixel().ToAssociatedScaledVector4(); + Vector4 vector = color.ToScaledVector4(PixelAlphaRepresentation.Associated); float keep = 1F - vector.W; ColorMatrix tint = default; tint.M11 = keep; @@ -186,7 +186,7 @@ private static Action CreateOperation(float sigma, Colo // The tint replaces the backdrop's colour with the constant shadow colour and scales its // alpha; the DestOver write-back slots the blurred silhouette beneath the untouched // backdrop at the offset. - Vector4 vector = color.ToPixel().ToScaledVector4(); + Vector4 vector = color.ToScaledVector4(PixelAlphaRepresentation.Unassociated); ColorMatrix tint = default; tint.M44 = vector.W; tint.M51 = vector.X; diff --git a/src/ImageSharp.Drawing/Processing/LayerEffect.cs b/src/ImageSharp.Drawing/Processing/LayerEffect.cs index 2c54fd4e1..c1b90dca6 100644 --- a/src/ImageSharp.Drawing/Processing/LayerEffect.cs +++ b/src/ImageSharp.Drawing/Processing/LayerEffect.cs @@ -172,7 +172,7 @@ private static Action CreateOperation(float sigma, Colo // alpha. The colour filter operates on straight alpha, so with the RGB rows zeroed the // constant row sets the colour outright; because the colour is then constant across the // snapshot, the blur only spreads alpha and cannot bleed foreign colours into the shadow. - Vector4 vector = color.ToPixel().ToScaledVector4(); + Vector4 vector = color.ToScaledVector4(PixelAlphaRepresentation.Unassociated); ColorMatrix tint = default; tint.M44 = vector.W; tint.M51 = vector.X; @@ -244,7 +244,7 @@ private static Action CreateOperation(float sigma, Colo // alpha. The colour filter operates on straight alpha, so with the RGB rows zeroed the // constant row sets the colour outright; because the colour is then constant across the // snapshot, the blur only spreads alpha and cannot bleed foreign colours into the glow. - Vector4 vector = color.ToPixel().ToScaledVector4(); + Vector4 vector = color.ToScaledVector4(PixelAlphaRepresentation.Unassociated); ColorMatrix tint = default; tint.M44 = vector.W; tint.M51 = vector.X; @@ -326,7 +326,7 @@ private static Action CreateOperation(float sigma, Colo // with the constant RGB rows carrying the shadow colour, the blur feathers the boundary, // and the SrcAtop write-back clips the result onto the content so only the parts that // reach inside its edges remain. - Vector4 vector = color.ToPixel().ToScaledVector4(); + Vector4 vector = color.ToScaledVector4(PixelAlphaRepresentation.Unassociated); ColorMatrix invertedTint = default; invertedTint.M44 = -vector.W; invertedTint.M54 = vector.W; diff --git a/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/DrawText_WithBackdropFilterLayerEffects_MatchesDefaultOutput_acrylic_Default.png b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/DrawText_WithBackdropFilterLayerEffects_MatchesDefaultOutput_acrylic_Default.png index d151a9b57..c40060a7d 100644 --- a/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/DrawText_WithBackdropFilterLayerEffects_MatchesDefaultOutput_acrylic_Default.png +++ b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/DrawText_WithBackdropFilterLayerEffects_MatchesDefaultOutput_acrylic_Default.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:b500298b467004907a1ad28829754f07becdae12cd3efbf5329b78bebbc527a6 -size 66512 +oid sha256:ccdd5080840dca614abdfd793c8bb03141f37366808ccc78704a0fc07df4ae88 +size 80765 diff --git a/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/DrawText_WithBackdropFilterLayerEffects_MatchesDefaultOutput_acrylic_WebGPU_NativeSurface.png b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/DrawText_WithBackdropFilterLayerEffects_MatchesDefaultOutput_acrylic_WebGPU_NativeSurface.png index 4d3bab323..c2b97e232 100644 --- a/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/DrawText_WithBackdropFilterLayerEffects_MatchesDefaultOutput_acrylic_WebGPU_NativeSurface.png +++ b/tests/Images/ReferenceOutput/Drawing/WebGPUDrawingBackendTests/DrawText_WithBackdropFilterLayerEffects_MatchesDefaultOutput_acrylic_WebGPU_NativeSurface.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:5d846917eb6c45c177dc13bc37f5744db329f3041941a37f0ba1e9a1141781ac -size 65215 +oid sha256:0957d6d5ef01145b24b9cdd3e916b388cc869c4b8c9027db4c4b478ecb4b6dcb +size 79300