Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rasterizing artifacts when rendering line with DrawLines #15

Closed
FrancoFun opened this issue Mar 22, 2018 · 20 comments
Closed

Rasterizing artifacts when rendering line with DrawLines #15

FrancoFun opened this issue Mar 22, 2018 · 20 comments
Assignees

Comments

@FrancoFun
Copy link

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
@zati-
Copy link

zati- commented May 15, 2018

Same problem here, workaround is to disable antialiasing

@antonfirsov
Copy link
Member

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

@tocsoft is my supposition right?

@tocsoft
Copy link
Member

tocsoft commented May 15, 2018

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

@woutware
Copy link
Contributor

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

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

drawlinestest

@JimBobSquarePants
Copy link
Member

Closing now as in nightlies

@antonfirsov
Copy link
Member

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

We still need a PR updating shapes.

@JimBobSquarePants
Copy link
Member

I thought the PR was in no?

@antonfirsov
Copy link
Member

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

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

@antonfirsov
Copy link
Member

Fixed with SixLabors/ImageSharp#596 in beta4.

@jjxtra
Copy link

jjxtra commented Aug 14, 2019

Any plans to put fix in a nuget package?

@JimBobSquarePants
Copy link
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?

@jjxtra
Copy link

jjxtra commented Aug 14, 2019

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

@jjxtra
Copy link

jjxtra commented Aug 14, 2019

Beta 6 / latest nuget:

Antialias On:
image

Antialias Off:
image

@jjxtra
Copy link

jjxtra commented Aug 14, 2019

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
Copy link

jjxtra commented Aug 14, 2019

@JimBobSquarePants
Copy link
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.

@JimBobSquarePants
Copy link
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.

@jjxtra
Copy link

jjxtra commented Aug 14, 2019

Awesome, let me know if you need more info.

@jjxtra
Copy link

jjxtra commented Aug 21, 2019

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

@antonfirsov antonfirsov transferred this issue from SixLabors/ImageSharp Jan 17, 2020
@antonfirsov
Copy link
Member

Closing as Duplicate of #5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants