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

WriteMask appears to be inverted in 7.5.0 #242

Closed
Brubning opened this issue Jun 28, 2018 · 5 comments
Closed

WriteMask appears to be inverted in 7.5.0 #242

Brubning opened this issue Jun 28, 2018 · 5 comments

Comments

@Brubning
Copy link

I have an application which uses Magick.NET to apply colour corrections to images via .Evaluate. We noticed that white areas of the images were taking the colour, so photos with white backgrounds look weird after correction. To fix this I apply a WriteMask to the image, anything which is very close to white is masked so that the colour correction is not applied.

With v7.4.4 this works brilliantly, upgrading to v7.5.0 this ask is inverted so that now the background is the only part of the image which takes the colour correction. See screen shot below of comparable images. The only difference is I change the installed version of Magick .NET (Q16, Any CPU) via NuGet.

The code to apply the write mask is:

    private void ApplyWriteMask(IMagickImage image)
    {
        var newImage = image.Clone();
        var whiteThreshold = new Percentage(99);
        var blackThreshold = new Percentage(1);

        newImage.ColorFuzz = new Percentage(3);
        newImage.WhiteThreshold(whiteThreshold);
        newImage.BlackThreshold(blackThreshold);

        newImage.Opaque(MagickColors.Black, MagickColors.Green);
        newImage.Opaque(MagickColors.White, MagickColors.Black);
        newImage.InverseOpaque(MagickColors.Black, MagickColors.White);

        image.WriteMask = newImage;
    }

I've attached the VS code project as well as a compiled version.

image

Noodle.MagickMemory.zip

Release.zip

@Brubning
Copy link
Author

I should also say, the test application basically processes all image files in one folder and saves the processed image to another folder. It was written to try and fix the problem described in issue #241 .

You'll need a source folder with a lot of images in, and an empty destination folder (it does overwrite, though). I've been running it with large numbers of files (100-1000) but this colour issue is obvious immediately.

Thanks

@Brubning
Copy link
Author

The WriteMask has changed in 7.5.0 so that transparency is used instead of white.

In v7.4.4, my WriteMask was defined by:

        newImage.Opaque(MagickColors.Black, MagickColors.Green);
        newImage.Opaque(MagickColors.White, MagickColors.Black);
        newImage.InverseOpaque(MagickColors.Black, MagickColors.White);

In 7.5.0 this inverts the effect that .Evalute has on the image. Instead I am now defining my WriteMask with:

        newImage.Opaque(MagickColors.White, MagickColors.Transparent);
        newImage.InverseOpaque(MagickColors.Transparent, MagickColors.Black);

I'm not sure if that's a bug or not, but it's different.

@dlemstra
Copy link
Owner

I just added a new unit test 7d00207 to check if there was an issue with the WriteMask and it looks like it works correctly in 7.5.0.0. Not sure why it did not work for you though. Maybe my unit test can help you further?

@Brubning
Copy link
Author

Thanks. It's weird because in my implementation using white as the mask colour doesn't work, but using transparency does. I'm on site today, but will make time to have a look at that test to see what's different in what I'm doing.

@ghost
Copy link

ghost commented Aug 22, 2018

I've experienced the same inversion of behavior, in which pre-7.5 IM releases appeared to modify pixels where the mask's pixel is white, whereas post-7.5 IM releases appear to protect pixels where the mask pixel is white. The new (post-7.5) behavior appears to be consistent with the older v6 ImageMagick write mask documentation. However, in IM v7, masks are documented to have changed behavior:

Version 7 supports masks for most image operators. Black pixels in a read mask ignores corresponding pixel in an image whereas black pixels in a write mask protects the corresponding pixel in the image. . . . This polarity is the reverse of masks in version 6 of ImageMagick

The current Magick.NET behavior does NOT appear to be consistent with the intended v7 mask behavior.

For reference, the mask images I'm using are single-channel / no-alpha grayscale, e.g.:

img.Alpha(AlphaOption.Remove);
img.HasAlpha = false;
img.ColorSpace = ColorSpace.Gray;
img.ColorType = ColorType.Grayscale;
img.Depth = 8;

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