A region clip under a scale draws a row twice when a band boundary falls on a pixel centre
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 2/5
- Geschätzter Aufwand
- 1-3 Stunden
- Anfängerfreundlichkeit
- 78/100
- Issue-Typ
- Bug
- Klarheit
- Klar beschrieben
- Aktivitätsstatus
- Aktiv
- Tech-Stack
- csharp
- Bereich
- computer-graphics
Rechercherichtung
Beginne mit Processing/Backends/DefaultDrawingBackend.Helpers.cs, insbesondere GetAxisCoverage und ApplyRegionClip, und vergleiche die Hartkanten-Konvention mit der Abdeckungsregel des Rasterizers. Reproduziere die doppelt gemischte Zeile mit dem bereitgestellten .NET-Programm und füge dann einen fokussierten Test an der Grenze des transformierten Bereichs hinzu oder aktualisiere ihn. Das Ergebnis ist erreicht, wenn benachbarte transformierte Bänder niemals dasselbe Pixelzentrum akzeptieren und die reproduzierte Zeile nur einmal gemischt wird.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
Prerequisites
- I have written a descriptive issue title
- I have verified that I am running the latest version of ImageSharp.Drawing
- I have verified if the problem exist in both
DEBUGandRELEASEmode - I have searched open and closed issues to ensure it has not already been reported
ImageSharp.Drawing version
main at commit a4e94b8 (the same hard-edge test is in v3.1.3)
Other ImageSharp packages and versions
SixLabors.ImageSharp 5.0.0-alpha.0.38, SixLabors.Fonts 3.2.1, SixLabors.PolygonClipper 2.0.0-alpha.0.2
Environment (Operating system, version and so on)
macOS 15.8.3 (arm64)
.NET Framework version
.NET 10 (net10.0)
Description
When a Region clip is drawn under a transform that keeps rectangles axis aligned (for example a scale), its rectangles become floating-point rectangles with hard edges. If the transformed boundary between two bands passes exactly through the centre of a pixel row, both rectangles accept that row. A translucent fill is then blended twice in the columns that both rectangles cover, and the row is darker than the rest of the fill.
Explanation
DrawingClipDescriptor.Transform (Processing/DrawingClipDescriptor.cs) maps each rectangle of an integer region through the matrix and creates a Region descriptor with DrawingClipEdgeMode.Hard.
DefaultDrawingBackend.ApplyRegionClip (Processing/Backends/DefaultDrawingBackend.Helpers.cs, line 413) passes the whole span to ApplyRectangleIntersection once for each rectangle (line 432). Each call sends the columns it accepts on to the next clip and finally to the brush. This is correct only if no pixel is accepted by two rectangles.
The row test is GetAxisCoverage (line 718). For a hard edge it is:
float pixelCentre = pixel + 0.5F;
return pixelCentre >= clipStart && pixelCentre <= clipEnd ? 1F : 0F;
Both ends of the interval are inclusive. The region rectangles (0, 0, 20, 7) and (10, 7, 20, 7) share the boundary y = 7. Under a scale of 1.5 the boundary is at y = 10.5, which is the centre of row 10. The first rectangle accepts row 10 because 10.5 <= 10.5, and the second rectangle accepts it because 10.5 >= 10.5. In the columns 15 to 29, where both rectangles exist, the pixel is drawn two times.
The same inclusive test also makes row 10 part of the first rectangle at x = 5 and part of the second rectangle at x = 40, so each rectangle is one row taller than its transformed height.
Suggested fix
Make the hard-edge test of GetAxisCoverage half open, so that adjacent intervals never share a pixel centre:
return pixelCentre >= clipStart && pixelCentre < clipEnd ? 1F : 0F;
This must keep the existing results of rectangle clips whose edges are not on a pixel centre (they do not change). It changes a hard-edged rectangle whose bottom or right edge is exactly on a pixel centre: that row or column is no longer inside. Check that the hard-edge rule of the rasterizer uses the same convention, so that a hard clip and a hard fill of the same rectangle select the same pixels.
If the inclusive test must stay for single rectangles, ApplyRegionClip can instead give each row to one rectangle only: keep the last accepted band and skip a rectangle whose band starts where the previous accepted band ends, or emit each column at most once as ApplyFloatingRegionDifference does with its cursor.
The SVG work does not change this code.
Notes
- Found while checking the coverage restore of
ApplyCoverageRunfor the SVG work. The double draw happens only for an integer region drawn under a transform that keeps rectangles axis aligned and puts a band boundary on a row centre (scale factors such as 1.5 or 2.5 do that for many boundaries). - A second, smaller effect follows from the double pass:
ApplyCoverageRunscales the coverage by the clip multiplier and divides it back afterwards. In single precision(c * m) / mis not alwaysc, so the second pass through a deeper antialiased clip can read a coverage that is one unit in the last place off. It can change a pixel only inside the double-drawn row. It was measured with a script, not with the program below. - Not run: the difference operation. From reading
ApplyFloatingRegionDifference, it keeps a cursor and does not emit a column twice, so it should not draw twice; its row test has the same inclusive end. - Not run on v3.1.3: its
GetAxisCoveragehas the same inclusive hard-edge test (line 709 of the same file).
Steps to Reproduce
Project file (Repro.csproj). It references a Release build of main; the path is a placeholder.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<Reference Include="SixLabors.ImageSharp.Drawing" HintPath="path/to/SixLabors.ImageSharp.Drawing.dll" />
<PackageReference Include="SixLabors.ImageSharp" Version="5.0.0-alpha.0.38" />
<PackageReference Include="SixLabors.Fonts" Version="3.2.1" />
<PackageReference Include="SixLabors.PolygonClipper" Version="2.0.0-alpha.0.2" />
</ItemGroup>
</Project>
Program.cs:
using System.Numerics;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Drawing;
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
// Two rectangles of an integer region. Under a scale of 1.5 their shared boundary y = 7 maps to y = 10.5,
// which is the centre of pixel row 10.
Region region = new();
region.Add(new Rectangle(0, 0, 20, 7));
region.Add(new Rectangle(10, 7, 20, 7));
using Image<Rgba32> image = new(64, 32, Color.White.ToPixel<Rgba32>());
image.Mutate(c => c.Paint(canvas =>
{
_ = canvas.Save(new DrawingOptions { Transform = Matrix4x4.CreateScale(1.5F) });
canvas.Clip(region.ToPath());
canvas.Fill(Brushes.Solid(Color.ParseHex("#0000FF80")), new RectanglePolygon(0, 0, 40, 20));
canvas.Restore();
}));
for (int y = 8; y <= 12; y++)
{
Console.WriteLine($"row {y,2}: x=5 {image[5, y]} x=20 {image[20, y]} x=40 {image[40, y]}");
}
int[] dark = Enumerable.Range(0, image.Width).Where(x => image[x, 10].R == 63).ToArray();
Console.WriteLine($"row 10 columns blended twice: {dark.Length}, from x={dark.FirstOrDefault()} to x={dark.LastOrDefault()}");
image.SaveAsPng("region-clip.png");
Run with dotnet run -c Release. A Debug build of main gives the same output and a byte-identical PNG.
Observed output:
row 8: x=5 Rgba32(127, 127, 255, 255) x=20 Rgba32(127, 127, 255, 255) x=40 Rgba32(255, 255, 255, 255)
row 9: x=5 Rgba32(127, 127, 255, 255) x=20 Rgba32(127, 127, 255, 255) x=40 Rgba32(255, 255, 255, 255)
row 10: x=5 Rgba32(127, 127, 255, 255) x=20 Rgba32(63, 63, 255, 255) x=40 Rgba32(127, 127, 255, 255)
row 11: x=5 Rgba32(255, 255, 255, 255) x=20 Rgba32(127, 127, 255, 255) x=40 Rgba32(127, 127, 255, 255)
row 12: x=5 Rgba32(255, 255, 255, 255) x=20 Rgba32(127, 127, 255, 255) x=40 Rgba32(127, 127, 255, 255)
row 10 columns blended twice: 15, from x=15 to x=29
Expected: every covered pixel is blended once, so row 10 at x = 20 is (127, 127, 255), like the rows around it. Row 10 belongs to one rectangle only: with a half-open interval it belongs to the second rectangle, so x = 5 is white and x = 40 is (127, 127, 255).
The program writes region-clip.png; the dark line is row 10 from x = 15 to x = 29, the columns where the two transformed rectangles overlap (15 to 30).
Images
Output enlarged 8 times with nearest-neighbour scaling. The darker line where the two rectangles meet is row 10, blended twice.
Original 64 × 32 output (region-clip.png).
- Vorherrschende Sprache
- C#
- Sterne
- 322
- Forks
- 49
- Ø Merge
- 3 Std. 27 Min.
- Gemergte PRs (30 T.)
- 6
Entwicklungsumgebung
- Kein Dockerfile und keine Docker-Compose-Datei
- Hat eine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus SixLabors/ImageSharp.Drawing
-
PatternPen with a four-value pattern and LineJoin.Miter draws an EllipsePolygon almost solidEvtl. vergeben Ein verknüpfter Pull Request ist offen oder bereits gemergt. Offenbug
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 55/100
SixLabors/ImageSharp.Drawing#430 · 1 Kommentar · 1 Reaktion ·
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 45/100
SixLabors/ImageSharp.Drawing#319 · 2 Kommentare ·
Alle Issues in SixLabors/ImageSharp.Drawing
Ähnliche Issues
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 92/100
NetSparkleUpdater/NetSparkle#762 ·
-
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 88/100
-
bug effort:S P3
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 62/100
nightscout/nocturne#4563 ·
Maintainer antworten meist innerhalb von 1 Tag
-
enhancement
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
Tyrrrz/YoutubeDownloader#925 ·
-
Bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
Maintainer antworten meist innerhalb von 1 Tag