Skip to content

Commit

Permalink
Added Chromaticity to MagickImage that will replace the separate prop…
Browse files Browse the repository at this point in the history
…erties.
  • Loading branch information
dlemstra committed Nov 12, 2023
1 parent 917d113 commit 36a7259
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 142 deletions.
9 changes: 9 additions & 0 deletions src/Magick.NET.Core/IMagickImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,24 +81,33 @@ public partial interface IMagickImage : IDisposable
/// </summary>
IEnumerable<PixelChannel> Channels { get; }

/// <summary>
/// Gets or sets the chromaticity of the image.
/// </summary>
IChromaticityInfo Chromaticity { get; set; }

/// <summary>
/// Gets or sets the chromaticity blue primary point.
/// </summary>
[Obsolete($"This property will be removed in the next major release, use {nameof(Chromaticity)} instead.")]
IPrimaryInfo ChromaBluePrimary { get; set; }

/// <summary>
/// Gets or sets the chromaticity green primary point.
/// </summary>
[Obsolete($"This property will be removed in the next major release, use {nameof(Chromaticity)} instead.")]
IPrimaryInfo ChromaGreenPrimary { get; set; }

/// <summary>
/// Gets or sets the chromaticity red primary point.
/// </summary>
[Obsolete($"This property will be removed in the next major release, use {nameof(Chromaticity)} instead.")]
IPrimaryInfo ChromaRedPrimary { get; set; }

/// <summary>
/// Gets or sets the chromaticity white primary point.
/// </summary>
[Obsolete($"This property will be removed in the next major release, use {nameof(Chromaticity)} instead.")]
IPrimaryInfo ChromaWhitePoint { get; set; }

/// <summary>
Expand Down
30 changes: 30 additions & 0 deletions src/Magick.NET.Core/Types/IChromaticityInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET.
// Licensed under the Apache License, Version 2.0.

namespace ImageMagick;

/// <summary>
/// Chromaticity information.
/// </summary>
public interface IChromaticityInfo
{
/// <summary>
/// Gets the chromaticity blue primary point.
/// </summary>
IPrimaryInfo Blue { get; }

/// <summary>
/// Gets the chromaticity green primary point.
/// </summary>
IPrimaryInfo Green { get; }

/// <summary>
/// Gets the chromaticity red primary point.
/// </summary>
IPrimaryInfo Red { get; }

/// <summary>
/// Gets the chromaticity white primary point.
/// </summary>
IPrimaryInfo White { get; }
}
33 changes: 33 additions & 0 deletions src/Magick.NET/MagickImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -455,9 +455,39 @@ public IEnumerable<PixelChannel> Channels
}
}

/// <summary>
/// Gets or sets the chromaticity of the image.
/// </summary>
public IChromaticityInfo Chromaticity
{
get
{
if (_nativeInstance.ChromaRedPrimary is null ||
_nativeInstance.ChromaGreenPrimary is null ||
_nativeInstance.ChromaBluePrimary is null ||
_nativeInstance.ChromaWhitePoint is null)
throw new MagickErrorException("Unable to allocate primary info");

return new ChromaticityInfo(
_nativeInstance.ChromaRedPrimary,
_nativeInstance.ChromaGreenPrimary,
_nativeInstance.ChromaBluePrimary,
_nativeInstance.ChromaWhitePoint);
}

set
{
_nativeInstance.ChromaRedPrimary = value.Red;
_nativeInstance.ChromaGreenPrimary = value.Green;
_nativeInstance.ChromaBluePrimary = value.Blue;
_nativeInstance.ChromaWhitePoint = value.White;
}
}

/// <summary>
/// Gets or sets the chromaticity blue primary point.
/// </summary>
[Obsolete($"This property will be removed in the next major release, use {nameof(Chromaticity)} instead.")]
public IPrimaryInfo ChromaBluePrimary
{
get
Expand All @@ -475,6 +505,7 @@ public IPrimaryInfo ChromaBluePrimary
/// <summary>
/// Gets or sets the chromaticity green primary point.
/// </summary>
[Obsolete($"This property will be removed in the next major release, use {nameof(Chromaticity)} instead.")]
public IPrimaryInfo ChromaGreenPrimary
{
get
Expand All @@ -492,6 +523,7 @@ public IPrimaryInfo ChromaGreenPrimary
/// <summary>
/// Gets or sets the chromaticity red primary point.
/// </summary>
[Obsolete($"This property will be removed in the next major release, use {nameof(Chromaticity)} instead.")]
public IPrimaryInfo ChromaRedPrimary
{
get
Expand All @@ -509,6 +541,7 @@ public IPrimaryInfo ChromaRedPrimary
/// <summary>
/// Gets or sets the chromaticity white primary point.
/// </summary>
[Obsolete($"This property will be removed in the next major release, use {nameof(Chromaticity)} instead.")]
public IPrimaryInfo ChromaWhitePoint
{
get
Expand Down
45 changes: 45 additions & 0 deletions src/Magick.NET/Types/ChromaticityInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET.
// Licensed under the Apache License, Version 2.0.

namespace ImageMagick;

/// <summary>
/// PrimaryInfo information.
/// </summary>
public partial class ChromaticityInfo : IChromaticityInfo
{
/// <summary>
/// Initializes a new instance of the <see cref="ChromaticityInfo"/> class.
/// </summary>
/// <param name="red">The chromaticity red primary point.</param>
/// <param name="green">The chromaticity green primary point.</param>
/// <param name="blue">The chromaticity blue primary point.</param>
/// <param name="white">The chromaticity white primary point.</param>
public ChromaticityInfo(IPrimaryInfo red, IPrimaryInfo green, IPrimaryInfo blue, IPrimaryInfo white)
{
Red = red;
Green = green;
Blue = blue;
White = white;
}

/// <summary>
/// Gets the chromaticity blue primary point.
/// </summary>
public IPrimaryInfo Blue { get; }

/// <summary>
/// Gets the chromaticity green primary point.
/// </summary>
public IPrimaryInfo Green { get; }

/// <summary>
/// Gets the chromaticity red primary point.
/// </summary>
public IPrimaryInfo Red { get; }

/// <summary>
/// Gets the chromaticity white primary point.
/// </summary>
public IPrimaryInfo White { get; }
}

This file was deleted.

This file was deleted.

65 changes: 65 additions & 0 deletions tests/Magick.NET.Tests/MagickImageTests/TheChromaProperty.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// 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 TheChromaProperty
{
[Fact]
public void ShouldHaveTheCorrectDefaultValues()
{
using var image = new MagickImage(Files.SnakewarePNG);

Assert.InRange(image.Chromaticity.Red.X, 0.64, 0.641);
Assert.InRange(image.Chromaticity.Red.Y, 0.33, 0.331);
Assert.InRange(image.Chromaticity.Red.Z, 0.00, 0.001);

Assert.InRange(image.Chromaticity.Blue.X, 0.15, 0.151);
Assert.InRange(image.Chromaticity.Blue.Y, 0.06, 0.061);
Assert.InRange(image.Chromaticity.Blue.Z, 0.00, .001);

Assert.InRange(image.Chromaticity.Green.X, 0.30, 0.301);
Assert.InRange(image.Chromaticity.Green.Y, 0.60, 0.601);
Assert.InRange(image.Chromaticity.Green.Z, 0.00, 0.001);

Assert.InRange(image.Chromaticity.White.X, 0.3127, 0.31271);
Assert.InRange(image.Chromaticity.White.Y, 0.329, 0.3291);
Assert.InRange(image.Chromaticity.White.Z, 0.00, 0.001);
}

[Fact]
public void ShouldHaveTheCorrectValuesWhenChanged()
{
using var image = new MagickImage(Files.SnakewarePNG);

var chromaticity = new ChromaticityInfo(
new PrimaryInfo(0.5, 1.0, 1.5),
new PrimaryInfo(0.6, 2.0, 2.5),
new PrimaryInfo(0.7, 3.0, 3.5),
new PrimaryInfo(0.8, 4.0, 4.5));

image.Chromaticity = chromaticity;

Assert.InRange(image.Chromaticity.Red.X, 0.50, 0.501);
Assert.InRange(image.Chromaticity.Red.Y, 1.00, 1.001);
Assert.InRange(image.Chromaticity.Red.Z, 1.50, 1.501);

Assert.InRange(image.Chromaticity.Green.X, 0.60, 0.601);
Assert.InRange(image.Chromaticity.Green.Y, 2.00, 2.001);
Assert.InRange(image.Chromaticity.Green.Z, 2.50, 2.501);

Assert.InRange(image.Chromaticity.Blue.X, 0.70, 0.701);
Assert.InRange(image.Chromaticity.Blue.Y, 3.00, 3.001);
Assert.InRange(image.Chromaticity.Blue.Z, 3.50, 3.501);

Assert.InRange(image.Chromaticity.White.X, 0.80, 0.801);
Assert.InRange(image.Chromaticity.White.Y, 4.00, 4.001);
Assert.InRange(image.Chromaticity.White.Z, 4.50, 4.501);
}
}
}

This file was deleted.

This file was deleted.

0 comments on commit 36a7259

Please sign in to comment.