I'm using Freeimage 3.17.0 .NET to export some images with alpha in Jpeg2000 (JP2) format. When I load the exported images in GIMP, I can see they have an alpha channel, but they don't seem to be compliant with Safari, because Safari in both iOS and macOS shows the alpha channel of those images as black.
If instead, I export the image to PNG and and save it to Jpeg2000 using the Preview tool in macOS, the alpha is displayed properly.
Am I missing any flags for JP2? Or is this a bug in Freeimage? The project hasn't been updated since 2015...
Edit(1): I'm looking at the output file with an hex editor and it begins with something like this,
....jP ..‡.....ftypjp2 ....jp2 ...³jp2h....ihdr
Edit(2): According to https://www.iana.org/assignments/media-types/image/jp2 the image should start indeed with these bytes,
{ 0x00, 0x00, 0x00, 0x0C, 0x6A, 0x50, 0x20, 0x20, 0x0D, 0x0A, 0x87, 0x0A }
which it's what both images, the one I saved with Preview, and the one I saved with Freeimage, start with.
Edit(3): Here's some unit test I wrote,
using(var bmp = new Bitmap("myimage.png"))
using(var fibm = new FreeImageBitmap(bmp))
{
fibm.Save("myimage-0.jp2", FREE_IMAGE_FORMAT.FIF_JP2, FREE_IMAGE_SAVE_FLAGS.DEFAULT);
fibm.Save("myimage-64.jp2", FREE_IMAGE_FORMAT.FIF_JP2, (FREE_IMAGE_SAVE_FLAGS)64);
}
The images I created with that seem to have the correct magic numbers at the beginning of the buffer. But still macOS & Safari think that those images have no alpha... Also, I created a very small 2x2 PNG image with alpha in GIMP, 139 bytes, and tried to save it with the above code, but FreeImage throws an exception, "Unable to save bitmap". The problem is inside the Dll so I haven't managed to step throw from C#. I tried exporting it to JP2 with macOS Preview and it works, albeit generating a big image of 3.9KB... Although it looks like most of it is the embedded color profile...
Edit(4): I've imported the Freeimage SVN repository into Github, fixed a build issue, and attempted to update OpenJpeg to version 2.3.0 (I believe Freeimage uses 2.1.0 atm). For the time being, I just build the OpenJpeg library manually and linked to it statically in Freeimage. Here's a link to the appropriate branch in my repository, with a README,
https://github.com/endavid/freeimage/tree/openjpeg2.3.0/FreeImage/Source/openjpeg
Although OpenJpeg 2.3.0 by itself (it comes with a couple of binary apps for conversion) does produce the right output, i.e. an image that Safari understands it has alpha, the output from FreeImage, even with the updated OpenJpeg, is still not good. So it looks like it's a bug in FreeImage...