Skip to content

Commit

Permalink
Added NegateGrayscale to MagickImage.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlemstra committed Jun 11, 2022
1 parent e19665c commit 91a9bb6
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 15 deletions.
13 changes: 13 additions & 0 deletions src/Magick.NET.Core/IMagickImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2398,6 +2398,19 @@ public partial interface IMagickImage : IDisposable
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
void Negate(Channels channels);

/// <summary>
/// Negate the grayscale colors in image.
/// </summary>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
void NegateGrayscale();

/// <summary>
/// Negate the grayscale colors in image for the specified channel.
/// </summary>
/// <param name="channels">The channel(s) that should be negated.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
void NegateGrayscale(Channels channels);

/// <summary>
/// Normalize image (increase contrast by normalizing the pixel values to span the full range
/// of color values).
Expand Down
15 changes: 15 additions & 0 deletions src/Magick.NET/MagickImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4521,6 +4521,21 @@ public void Negate(bool onlyGrayscale, Channels channels)
public void Negate(Channels channels)
=> Negate(false, channels);

/// <summary>
/// Negate the grayscale colors in image.
/// </summary>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void NegateGrayscale()
=> NegateGrayscale(ImageMagick.Channels.Composite);

/// <summary>
/// Negate the grayscale colors in image for the specified channel.
/// </summary>
/// <param name="channels">The channel(s) that should be negated.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void NegateGrayscale(Channels channels)
=> _nativeInstance.Negate(true, channels);

/// <summary>
/// Normalize image (increase contrast by normalizing the pixel values to span the full range
/// of color values).
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET.
// Licensed under the Apache License, Version 2.0.

using ImageMagick;
using Xunit;

namespace Magick.NET.Tests
{
public partial class MagickImageTests
{
public class TheNegateGrayscaleMethod
{
[Fact]
public void ShouldOnlyNegateGrayscale()
{
using (var image = new MagickImage("xc:white", 2, 1))
{
using (var pixels = image.GetPixels())
{
var pixel = pixels.GetPixel(1, 0);
pixel.SetChannel(1, 0);
pixel.SetChannel(2, 0);
}

image.NegateGrayscale();

ColorAssert.Equal(MagickColors.Black, image, 0, 0);
ColorAssert.Equal(MagickColors.Red, image, 1, 0);
}
}

[Fact]
public void ShouldNegateTheSpecifedChannels()
{
using (var image = new MagickImage("xc:white", 2, 1))
{
using (var pixels = image.GetPixels())
{
var pixel = pixels.GetPixel(1, 0);
pixel.SetChannel(1, 0);
pixel.SetChannel(2, 0);
}

image.NegateGrayscale(Channels.Green);

ColorAssert.Equal(MagickColors.Fuchsia, image, 0, 0);
ColorAssert.Equal(MagickColors.Red, image, 1, 0);
}
}
}
}
}
30 changes: 15 additions & 15 deletions tests/Magick.NET.Tests/MagickImageTests/TheNegateMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@ public partial class MagickImageTests
{
public class TheNegateMethod
{
public class WithBoolean
[Fact]
public void ShouldNegateTheImage()
{
[Fact]
public void ShouldOnlyNegateGrayscaleWhenSetToTrue()
using (var image = new MagickImage("xc:white", 1, 1))
{
using (var image = new MagickImage("xc:white", 2, 1))
{
using (var pixels = image.GetPixels())
{
var pixel = pixels.GetPixel(1, 0);
pixel.SetChannel(1, 0);
pixel.SetChannel(2, 0);
}
image.Negate();

image.Negate(true);
ColorAssert.Equal(MagickColors.Black, image, 0, 0);
}
}

[Fact]
public void ShouldNegateTheSpecifedChannels()
{
using (var image = new MagickImage("xc:white", 1, 1))
{
image.Negate(Channels.Red);

ColorAssert.Equal(MagickColors.Black, image, 0, 0);
ColorAssert.Equal(MagickColors.Red, image, 1, 0);
}
ColorAssert.Equal(MagickColors.Aqua, image, 0, 0);
}
}
}
Expand Down

0 comments on commit 91a9bb6

Please sign in to comment.