Skip to content

Commit

Permalink
Added AssumeAlpha to the Jp2ReadDefines that allows a user to assume …
Browse files Browse the repository at this point in the history
…a 2nd or 4th channel is an alpha channel (#1449).
  • Loading branch information
dlemstra committed Oct 12, 2023
1 parent 905bf7a commit d2f5540
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Magick.NET/Formats/Jp2/Jp2ReadDefines.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public sealed class Jp2ReadDefines : IReadDefines
public MagickFormat Format
=> MagickFormat.Jp2;

/// <summary>
/// Gets or sets the flag that assumes an alpha channel when the image has 2 or 4 channels (jp2:assume-alpha).
/// </summary>
public bool? AssumeAlpha { get; set; }

/// <summary>
/// Gets or sets the maximum number of quality layers to decode (jp2:quality-layers).
/// </summary>
Expand All @@ -33,6 +38,9 @@ public IEnumerable<IDefine> Defines
{
get
{
if (AssumeAlpha.HasValue)
yield return new MagickDefine(Format, "assume-alpha", AssumeAlpha.Value);

if (QualityLayers.HasValue)
yield return new MagickDefine(Format, "quality-layers", QualityLayers.Value);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET.
// Licensed under the Apache License, Version 2.0.

using ImageMagick;
using ImageMagick.Formats;
using Xunit;

namespace Magick.NET.Tests;

public partial class Jp2ReadDefinesTests
{
public class TheAssumeAlphaProperty
{
[Fact]
public void ShouldTreatMetaChannelAsAlpha()
{
var settings = new MagickReadSettings
{
Defines = new Jp2ReadDefines
{
AssumeAlpha = true,
},
};

using var image = new MagickImage();
image.Read(Files.Coders.TestJP2, settings);

Assert.Equal("true", image.Settings.GetDefine(MagickFormat.Jp2, "assume-alpha"));
Assert.True(image.HasAlpha);
Assert.Equal(4, image.ChannelCount);
Assert.Contains(PixelChannel.Alpha, image.Channels);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public void ShouldNotSetAnyDefines()
using var image = new MagickImage();
image.Settings.SetDefines(new Jp2ReadDefines());

Assert.Null(image.Settings.GetDefine(MagickFormat.Jp2, "assume-alpha"));
Assert.Null(image.Settings.GetDefine(MagickFormat.Jp2, "quality-layers"));
Assert.Null(image.Settings.GetDefine(MagickFormat.Jp2, "reduce-factor"));
}
Expand Down

0 comments on commit d2f5540

Please sign in to comment.