Skip to content

Rasterizing artifacts when rendering line with DrawLines #15

Description

@FrancoFun

Description

When plotting a graph using DrawLines, if the curve crosses the same y coordinate multiple times, some unwanted horizontal lines are being rendered.

Steps to Reproduce

Here's code producing the issue displayed in the image below. Artifacts are particularly visible on the bottom right corner.

public static void DrawLinesTest()
{
    int width = 1000;
    int height = 500;
    using (var image = new Image<Rgba32>(width, height))
    {
        image.Mutate(imageContext =>
        {
            imageContext.BackgroundColor(Rgba32.White);
            int pointCount = 1234;
            var line = Enumerable.Range(0, pointCount).Select(x => x * width / (float)pointCount).Select(x => new PointF(x, (0.5f + (float)Math.Sin(x / 3) / 2) * height)).ToArray();
            imageContext.DrawLines(new Rgba32(255, 0, 0), 1, line);
        });

        using (var file = File.Create("DrawLinesTest.png"))
        {
            image.SaveAsPng(file);
        }
    }
}

drawlinetest

System Configuration

  • ImageSharp version: 1.0.0-beta0002
  • Other ImageSharp packages and versions: Drawing version 1.0.0-beta0002
  • Environment (Operating system, version and so on): VS2017, Windows 10
  • .NET Framework version: .net Core 2.0 console application
  • Additional information: Saving the image as bmp didn't change anything

Activity

  1. zati- commented on May 15, 2018

    @zati-

    Same problem here, workaround is to disable antialiasing

  2. antonfirsov commented on May 15, 2018

    @antonfirsov
    Member

    Might be related to (or actually the same issue as) SixLabors/ImageSharp#572 and SixLabors/Shapes/#42.

    @tocsoft is my supposition right?

  3. tocsoft commented on May 15, 2018

    @tocsoft
    Member

    yeah, looks like its going to be the exact same root issues.

  4. woutware commented on May 22, 2018

    @woutware
    Contributor

    This is indeed a duplicate of SixLabors/ImageSharp#572.

    I've verified it now works ok after the fix, see attached image.

    drawlinestest

  5. JimBobSquarePants commented on May 23, 2018

    @JimBobSquarePants
    Member

    Closing now as in nightlies

  6. antonfirsov commented on May 23, 2018

    @antonfirsov
    Member

    Shouldn't be fixed yet. SL.ImageSharp.Drawing is still referencing shapes 1.0.0-ci0018.

    We still need a PR updating shapes.

  7. JimBobSquarePants commented on May 23, 2018

    @JimBobSquarePants
    Member

    I thought the PR was in no?

  8. antonfirsov commented on May 23, 2018

    @antonfirsov
    Member

    @JimBobSquarePants only in SL.Shapes: SixLabors/Shapes#43

    We need to update the reference to have these issues closed.

  9. antonfirsov commented on May 26, 2018

    @antonfirsov
    Member

    Fixed with SixLabors/ImageSharp#596 in beta4.

  10. jjxtra commented on Aug 14, 2019

    @jjxtra

    Any plans to put fix in a nuget package?

  11. JimBobSquarePants commented on Aug 14, 2019

    @JimBobSquarePants
    Member

    @jjxtra It was back in May 2018.

    https://www.nuget.org/packages/SixLabors.ImageSharp/1.0.0-beta0004

    Are you still seeing issues with either beta6 or the latest nightly builds from MyGet?

  12. jjxtra commented on Aug 14, 2019

    @jjxtra

    Yes with beta 6. Rendering us state lines onto an image, I am seeing horizontal gray lines if anti alias is on.

  13. jjxtra commented on Aug 14, 2019

    @jjxtra

    Beta 6 / latest nuget:

    Antialias On:
    image

    Antialias Off:
    image

  14. jjxtra commented on Aug 14, 2019

    @jjxtra

    Code is fairly straightforward, geo-json file...

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.IO;
    
    using Newtonsoft.Json.Linq;
    
    using SixLabors.Primitives;
    using SixLabors.ImageSharp.PixelFormats;
    using SixLabors.ImageSharp.Processing;
    using SixLabors.ImageSharp;
    using SixLabors.Shapes;
    
    namespace ImageCreator
    {
        class ImageCreatorApp
        {
            private static IEnumerable<IReadOnlyList<PointF>> EnumerateLines(JToken geometry)
            {
                // map the longitude (horizontal) to 0 - 360
                // map the latitude (vertical) to 0 - 180
                string type = geometry["type"].ToString();
                List<PointF> line = new List<PointF>();
                if (type.Equals("multipolygon", StringComparison.OrdinalIgnoreCase))
                {
                    foreach (JArray polygonGroup in geometry["coordinates"])
                    {
                        foreach (JArray polygon in polygonGroup)
                        {
                            foreach (JToken coord in polygon)
                            {
                                float lon = coord[0].Value<float>() + 180.0f;
                                if (lon > 180.0)
                                {
                                    lon = (360.0f - lon);
                                }
                                float lat = coord[1].Value<float>() + 90.0f;
                                if (lat > 90.0f)
                                {
                                    lat = (180.0f - lat);
                                }
                                line.Add(new PointF(lon, lat));
                            }
                            yield return line;
                            line.Clear();
                        }
                    }
                }
                else if (type.Equals("polygon", StringComparison.OrdinalIgnoreCase))
                {
                    foreach (JArray polygon in geometry["coordinates"])
                    {
                        foreach (JToken coord in polygon)
                        {
                            float lon = coord[0].Value<float>() + 180.0f;
                            if (lon > 180.0)
                            {
                                lon = (360.0f - lon);
                            }
                            float lat = coord[1].Value<float>() + 90.0f;
                            if (lat > 90.0f)
                            {
                                lat = (180.0f - lat);
                            }
                            line.Add(new PointF(lon, lat));
                        }
                        yield return line;
                        line.Clear();
                    }
                }
            }
    
            public static int Main(string[] args)
            {
                if (args.Length == 0)
                {
                    Console.WriteLine("Pass geo-json files as arguments");
                    return -1;
                }
    
                // have to acquire the ranges first...
                List<JToken> fileTokens = new List<JToken>();
                float minLat = float.MaxValue;
                float minLon = float.MaxValue;
                float maxLat = float.MinValue;
                float maxLon = float.MinValue;
                foreach (string file in args)
                {
                    JObject token = JObject.Parse(File.ReadAllText(file));
                    fileTokens.Add(token);
                    var results = token.Descendants().OfType<JProperty>().Where(t => t.Name == "geometry").Select(t => t.Value);
                    foreach (JToken geometry in results)
                    {
                        foreach (PointF coord in EnumerateLines(geometry).SelectMany(l => l))
                        {
                            minLon = Math.Min(minLon, coord.X);
                            minLat = Math.Min(minLat, coord.Y);
                            maxLon = Math.Max(maxLon, coord.X);
                            maxLat = Math.Max(maxLat, coord.Y);
                        }
                    }
                }
                float rangeLon = 1.0f / MathF.Abs(maxLon - minLon);
                float rangeLat = 1.0f / MathF.Abs(maxLat - minLat);
                float width = 4096.0f;
                float height = 2048.0f;
                float maxWidth = (width - 1.0f);
                float maxHeight = (height - 1.0f);
                SixLabors.ImageSharp.Image<Rgba32> img = new SixLabors.ImageSharp.Image<Rgba32>((int)width, (int)height);
                img.Mutate(m => m.Fill(Rgba32.Black));
                GraphicsOptions options = new GraphicsOptions { Antialias = false };
                foreach (JObject fileToken in fileTokens)
                {
                    var results = fileToken.Descendants().OfType<JProperty>().Where(t => t.Name == "geometry").Select(t => t.Value);
                    foreach (JToken geometry in results)
                    {
                        foreach (List<PointF> line in EnumerateLines(geometry))
                        {
                            for (int i = 0; i < line.Count; i++)
                            {
                                line[i] = new PointF
                                (
                                    Math.Min(maxWidth, (int)Math.Round(width * (1.0f - (rangeLon * MathF.Abs(maxLon - line[i].X))))),
                                    Math.Min(maxHeight, (int)Math.Round(height * (1.0f - (rangeLat * MathF.Abs(maxLat - line[i].Y)))))
                                );
                            }
                            if (line.Count > 3)
                            {
                                img.Mutate(m => m.DrawLines(options, Rgba32.White, 1.0f, line.ToArray()));
                            }
                        }
                    }
                }
                img.Save(File.OpenWrite(@"image.png"), new SixLabors.ImageSharp.Formats.Png.PngEncoder());
                return 0;
            }
        }
    }
  15. jjxtra commented on Aug 14, 2019

    @jjxtra
  16. JimBobSquarePants commented on Aug 14, 2019

    @JimBobSquarePants
    Member

    @jjxtra Thanks for providing so much detail, much appreciated!

    That looks like an issue to me so will reopen. In the interim could you please check again with the nightly code from MyGet and see if the issue persists there.

  17. JimBobSquarePants commented on Aug 14, 2019

    @JimBobSquarePants
    Member

    @jjxtra No need to do the additional testing for now. Cause is here. https://github.com/SixLabors/Shapes/issues/44

    We can use your information to formulate a fix.

  18. jjxtra commented on Aug 14, 2019

    @jjxtra

    Awesome, let me know if you need more info.

  19. jjxtra commented on Aug 21, 2019

    @jjxtra

    Sorry, wrong dll. The latest master branch as of 2019-08-21 still has the anti-alias line issue.

  20. antonfirsov commented on Jan 17, 2020

    @antonfirsov
    Member

    Closing as Duplicate of #5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions