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

Single-pixel wide rectangle borders draw with inaccurate corner colours #107

Closed
4 tasks done
atruskie opened this issue Nov 26, 2020 · 3 comments
Closed
4 tasks done
Milestone

Comments

@atruskie
Copy link
Contributor

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 DEBUG and RELEASE mode
  • I have searched open and closed issues to ensure it has not already been reported

Description

When drawing a single pixel wide rectangle, the corners appear to have slightly inaccurate colours.
In this example I expect every pixel of the rectangle border to be solid red (R: 255), but some the corners are R:241 (both top corners) and R:246 (both bottom corners).

Also, relevant: coordinates pixel aligned, non-fractional, anti-aliasing off. I tried a few different blend options, but they didn't seem to make a difference either.

Steps to Reproduce

var testImage = new Image<Rgb24>(Configuration.Default, 100, 100, Color.Black);

var rectangle = new RectangleF(10.0f, 10.0f, 79, 79);
var pen = new Pen(Color.Red, 1f);

var options = new ShapeGraphicsOptions()
{
    GraphicsOptions = new GraphicsOptions()
    {
        //BlendPercentage = 1,
        Antialias = false,
        //ColorBlendingMode = PixelColorBlendingMode.Normal,
        AntialiasSubpixelDepth = 0,
    },
};

testImage.Mutate(x => x.Draw(pen, rectangle));

testImage.Save("rectangle.png");

// expected (bottom right)
//      88 | 89 | 90
// 88 | B  | R  | B
// 89 | R  | R  | B
// 90 | B  | B  | B


// should be black
Debug.Assert(Color.Black.ToPixel<Rgb24>() ==
    testImage[88, 88]);

// BUG: these assertions SHOULD FAIL but do not
// should be red (bug is that it is blended)
Debug.Assert(new Rgb24(241, 0, 0) == testImage[10, 10]);
Debug.Assert(new Rgb24(246, 0, 0) == testImage[10, 89]);
Debug.Assert(new Rgb24(246, 0, 0) == testImage[89, 89]);
Debug.Assert(new Rgb24(241, 0, 0) == testImage[89, 10]);
// END BUG

// should be black
Debug.Assert(Color.Black.ToPixel<Rgb24>() == testImage[90, 90]);

// should be red
Debug.Assert(new Rgb24(255, 0, 0) == testImage[11, 10]);
Debug.Assert(new Rgb24(255, 0, 0) == testImage[11, 89]);
Debug.Assert(new Rgb24(255, 0, 0) == testImage[88, 89]);
Debug.Assert(new Rgb24(255, 0, 0) == testImage[88, 10]);

System Configuration

  • ImageSharp.Drawing version: 1.0.0-beta11
  • Other ImageSharp packages and versions: SixLabors.ImageSharp 1.0.2
  • Environment (Operating system, version and so on): Win 10, x64
  • .NET Framework version: netcoreapp3.1, and net5.0 (5.0.100)
  • Additional information: I had a smoke test set up and the behavior of this bug has changed in the amount of error from true red by like 3/255 (this was producing rgb(247, 0, 0) bottom right).

Might be related to #5 or #28.

Output image from above test:
rectangle

@antonfirsov antonfirsov added bug Something isn't working and removed needs triage labels Nov 26, 2020
@antonfirsov antonfirsov added this to the 1.0.0-rc.1 milestone Nov 26, 2020
@antonfirsov antonfirsov removed the bug Something isn't working label Nov 26, 2020
@antonfirsov
Copy link
Member

@atruskie you should pass (Shape)GraphicsOptions to the drawing call, otherwise anti-aliasing will kick in:

-testImage.Mutate(x => x.Draw(pen, rectangle));
+testImage.Mutate(x => x.Draw(options, pen, rectangle));

Or:

testImage.Mutate(x =>
    {
        x.SetGraphicsOptions(new GraphicsOptions {Antialias = false});
        x.Draw(pen, rectangle);
    });

The output seems to be correct when anti-aliasing is actually disabled:
image

@atruskie
Copy link
Contributor Author

Oh god - I'm so sorry. Clearly, I was more tired than I thought.

@antonfirsov
Copy link
Member

@atruskie no worries, happy to help!

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

2 participants