dlemstra / Magick.NET

The .NET library for ImageMagick
Apache License 2.0
3.44k stars 416 forks source link

HasAlpha property set incorrectly on TIFF sRGB file #1429

Open pablojmp opened 1 year ago

pablojmp commented 1 year ago

Magick.NET version

Magick.NET-Q8-x64 13.1.2

Environment (Operating system, version and so on)

Windows 10 22H2 Build 19045.3324

Description

TIFF file with sRGB colorspace fails to detect the alpha channel. It was correctly detected on version 12.3.0

Also, MagickImage.ChannelCount and MagickImage.Channels.Count are not equal for this sample

The file: 2001022-fullcolor.zip

Steps to Reproduce

    using ImageMagick; 
    using System.Linq;

    static void Main()
    {
      using(var lIM = new MagickImage(testFile))
      {
        var lHasAlpha = lIM.HasAlpha;
        var lChannels_Count = lIM.Channels.Count();
        var lChannelsCount = lIM.ChannelCount;
      }
    }
pablojmp commented 1 year ago

ColorType is detected as TrueColor on 13.1.2 and as TrueColorAlpha on 12.3.0

dlemstra commented 1 year ago

The recent version of ImageMagick has made some improvements to read additional channels. The tiff library that is being used reports the following warning:

Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.

In earlier versions of ImageMagick we would not check this and assume the image has an alpha channel. But looking at the output image we probably fixed reading it? It now shows a colored image and it looks like the extra sample/channel contains a mask for certain areas of the image?

pablojmp commented 1 year ago

how should i access the extra samples channel in the new version? i was doing image.Separate(Channels.Alpha).First();. how should i detect the non-color channels?

dlemstra commented 1 year ago

There is no proper api to do that at this time. I will be going to add this but that will be going to take me a while because I am working on some big internal changes related to those flags. For now you can probably do this: image.Separate((Channels) 1024).First();

pablojmp commented 1 year ago

i think it's better in my use case to revert to 12.3.0 for the moment. is there an issue i can subscribe to so i know when you're done with the changes?

pablojmp commented 8 months ago

@dlemstra hi! i've looked into version 13.5.0 and channels.count and channelscount now match (4 for both), but hasalpha is still false on 13.5. i'd like to update as 12.3 is marked as vulnerable. is there any way to detect the channel as an alpha channel? new updated test code:


public static class Program
{
  public static void Main()
  {
    var testFile = @"../../../../2001022-fullcolor.tif";

    using (var lIM = new MagickImage(testFile))
    {
      var lHasAlpha = lIM.HasAlpha;
      var lChannels = lIM.Channels.ToList();
      var lChannelCount = lIM.ChannelCount;

      Console.WriteLine($"Channels: {string.Join(", ", lChannels.Cast<int>())}");
      Console.WriteLine($"HasAlpha: {lHasAlpha}. Channels.Count {lChannels.Count}. ChannelsCount {lChannelCount}");
    }
  }
}

Output with 12.3:

Channels: 0, 1, 2, 4
HasAlpha: True. Channels.Count 4. ChannelsCount 4

Output with 13.5:

Channels: 0, 1, 2, 10
HasAlpha: False. Channels.Count 4. ChannelsCount 4
dlemstra commented 8 months ago

There is no option yet to directly assume that the 4th channel is an alpha channel so you will need to do this really expensive "trick":

using (var lIM = new MagickImage(testFile))
{
    using (var images = new MagickImageCollection(lIM.Separate()))
    {
        using (var image = images.Combine())
        {
        }
    }
}

I haven't made time yet to figure out how I can add an option for this.