SixLabors / ImageSharp.Drawing

:pen: Extensions to ImageSharp containing a cross-platform 2D polygon manipulation API and drawing operations.
https://sixlabors.com/products/imagesharp-drawing/
Other
282 stars 38 forks source link

Rasterizing artifacts when rendering line with DrawLines #15

Closed FrancoFun closed 4 years ago

FrancoFun commented 6 years ago

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

zati- commented 6 years ago

Same problem here, workaround is to disable antialiasing

antonfirsov commented 6 years ago

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

@tocsoft is my supposition right?

tocsoft commented 6 years ago

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

woutware commented 6 years ago

This is indeed a duplicate of https://github.com/SixLabors/ImageSharp/issues/572.

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

drawlinestest

JimBobSquarePants commented 6 years ago

Closing now as in nightlies

antonfirsov commented 6 years ago

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

We still need a PR updating shapes.

JimBobSquarePants commented 6 years ago

I thought the PR was in no?

antonfirsov commented 6 years ago

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

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

antonfirsov commented 6 years ago

Fixed with SixLabors/ImageSharp#596 in beta4.

jjxtra commented 5 years ago

Any plans to put fix in a nuget package?

JimBobSquarePants commented 5 years ago

@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?

jjxtra commented 5 years ago

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

jjxtra commented 5 years ago

Beta 6 / latest nuget:

Antialias On: image

Antialias Off: image

jjxtra commented 5 years ago

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;
        }
    }
}
jjxtra commented 5 years ago

States Geo-Json File Download

JimBobSquarePants commented 5 years ago

@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.

JimBobSquarePants commented 5 years ago

@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.

jjxtra commented 5 years ago

Awesome, let me know if you need more info.

jjxtra commented 5 years ago

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

antonfirsov commented 4 years ago

Closing as Duplicate of #5.