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

Failing to detect alpha channel in JPEG2000 files #1449

Closed
AdamFrisby opened this issue Oct 4, 2023 · 11 comments
Closed

Failing to detect alpha channel in JPEG2000 files #1449

AdamFrisby opened this issue Oct 4, 2023 · 11 comments
Milestone

Comments

@AdamFrisby
Copy link

Magick.NET version

13.3.0 Q16-AnyCPU

Environment (Operating system, version and so on)

Amazon Linux 2

Description

JPEG2000 files containing alpha channels no longer correctly set the image.hasAlpha attribute correctly.

The current version of MagickViewer correctly saves a PNG containing the alpha channel of the same test file.

Steps to Reproduce

In the following code (it needs to handle multiple container types, hence the layering) hasAlpha no longer returns true, where previously it would.

Additionally, on further inspection, it also appears ChannelCount is now 3, not 4.

Environment: Amazon Linux 2, .NET Core 7 using NativeAOT.

                try
                {
                    using (MagickImage image = new MagickImage(tempFilename, MagickFormat.J2c))
                    {
                        hasAlpha = image.HasAlpha;
                        await WriteTemporaryImage(image, tempPng);
                    }
                }
                catch (ImageMagick.MagickImageErrorException ex1)
                {
                    try
                    {
                        using (MagickImage image = new MagickImage(tempFilename, MagickFormat.J2k))
                        {
                            hasAlpha = image.HasAlpha;
                            await WriteTemporaryImage(image, tempPng);
                        }
                    }
                    catch (MagickImageErrorException ec2)
                    {
                        using (MagickImage image = new MagickImage(tempFilename, MagickFormat.Jp2))
                        {
                            hasAlpha = image.HasAlpha;
                            await WriteTemporaryImage(image, tempPng);
                        }
                    }
                }
@dlemstra
Copy link
Owner

dlemstra commented Oct 4, 2023

Can you share an image that can be used to reproduce this?

@AdamFrisby
Copy link
Author

@dlemstra of course, my sample files I can't share on the internet, but I can send one another way if that is possible.

@dlemstra
Copy link
Owner

dlemstra commented Oct 5, 2023

I didn't see anything in your post that would say that your image was private? You could share it by email? It can be found on my profile page.

p.s. MagickViewer hasn't been updated in ages and I am considering to archive it and tell people to move to ImageGlass.

@AdamFrisby
Copy link
Author

Sent - thankyou!

And yes, I was just using it as a test to prove the file itself wasn't broken since it was a convenient viewing application. :)

@dlemstra
Copy link
Owner

dlemstra commented Oct 5, 2023

We made some changes to ImageMagick recently when reading jpeg2000 images. There is a marker in the channel that specifies if it is an alpha channel or not. And it turns out that for your image the 4th channel is a meta channel instead of an alpha channel. And the current version of ImageMagick no longer reads that channel. I just pushed a patch to add support for reading that extra channel again. In the next version of Magick.NET you will be able to read that channel again but it won't be marked as an alpha channel.

What do you see in other viewers that makes you think that this image has an alpha channel?

@AdamFrisby
Copy link
Author

Interesting; the file definitely is intended to have a alpha channel (it's a 3D texture), but it appears that whatever tool is writing the file is doing so improperly (I'm actually not surprised, most J2K libraries are not ... great). I've opened it in a few other JPEG2000 libraries which do see it as a alpha channel (OpenJpeg, CSJ2K) but that may be them misinterpreting the spec, or just naively assuming 4 channels = RGBA.

Thank you so much for investigating and adding that patch.

@dlemstra
Copy link
Owner

dlemstra commented Oct 5, 2023

We could introduce an option that would always force the 4th channel to be an alpha channel? And then add this to the Jp2ReadDefines? But not sure what that define should be called. Maybe jp2:assume-alpha? Would such an option help you?

@AdamFrisby
Copy link
Author

Yeah, that would be fantastic.

@dlemstra
Copy link
Owner

dlemstra commented Oct 6, 2023

The next release will have an AssumeAlpha property in the Jp2ReadDefines that you can use to automatically enable the alpha channel when the image has 2 or 4 channels.

@dlemstra dlemstra added this to the 13.4.0 milestone Oct 6, 2023
@AdamFrisby
Copy link
Author

Very much appreciated - one last question from me, is there a beta version I can help test with?

@dlemstra
Copy link
Owner

dlemstra commented Oct 9, 2023

I was able to add some unit tests for this locally that use an that I created with the same alpha flag set to zero as in the image that your provided. But you can probably test this yourself with one of the build artifacts after I have updated ImageMagick in this repository in a couple days.

dlemstra added a commit that referenced this issue Oct 12, 2023
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