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

Image format not supported exception with test heic file #89

Closed
cvdsoftware opened this issue Jul 8, 2022 · 3 comments
Closed

Image format not supported exception with test heic file #89

cvdsoftware opened this issue Jul 8, 2022 · 3 comments

Comments

@cvdsoftware
Copy link

I'm trying to convert sample1.heic from https://filesamples.com/formats/heic (image1.zip) using the latest version from nuget (PhotoSauce.MagicScaler 0.13.0 and PhotoSauce.NativeCodecs.Libheif 1.12.0-Preview2) on .NET 6.0:

CodecManager.Configure(codecs => {
    var wicheif = codecs.OfType<IImageDecoderInfo>().FirstOrDefault(c => c.MimeTypes.Any(m => m == ImageMimeTypes.Heic));
    if (wicheif != null)
        codecs.Remove(wicheif);

    codecs.UseLibheif();
});

MagicImageProcessor.ProcessImage(@"c:\temp\heic\image1.heic", @"c:\temp\heic\image1.jpg", new ProcessImageSettings { Width = 240, Height = 240, ResizeMode = CropScaleMode.Max, EncoderOptions = new JpegEncoderOptions() { Quality = 80 } });

But I receive a exception: System.IO.InvalidDataException: 'Image format not supported. Please ensure the input file is an image and that a codec capable of reading the image is registered.' Other test heic files works fine with the test code.

If I disable the Libheif native codec it works on Windows 10 (but Windows Server doesn't have the codec), also the Windows Photo app can show the file:
image

I don't now if this is just a coincidence or a bug in the native codec.

Btw, thanks for your great lib!

@saucecontrol
Copy link
Owner

Hey, thanks for the report.

That sample image has an unusual signature that's not recognized by the plugin. Normal HEIC BMFF box type is ftypheic but that image has ftypmif1. It appears that's a valid (if uncommon) structure, and libheif does support it, so we'll just have to update the patterns the plugin registers.

In the meantime, if you need to decode files with that signature, you can update the patterns matched by the codec just after you register it, like this:

CodecManager.Configure(codecs => {
    // remove WIC HEIF
    var wicheif = codecs.OfType<IImageDecoderInfo>().FirstOrDefault(c => c.MimeTypes.Any(m => m == ImageMimeTypes.Heic));
    if (wicheif != null)
        codecs.Remove(wicheif);

    // register libheif
    codecs.UseLibheif();

    // remove libheif and re-add with updated patterns
    var libheif = codecs.OfType<DecoderInfo>().Single(c => c.MimeTypes.Any(m => m == ImageMimeTypes.Heic));
    codecs.Remove(libheif);

    var mif1 = new ContainerPattern {
        Offset = 0,
        Pattern = new byte[] { 0, 0, 0, 0, (byte)'f', (byte)'t', (byte)'y', (byte)'p', (byte)'m', (byte)'i', (byte)'f', (byte)'1' },
        Mask = new byte[] { 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }
    };
    var patterns = libheif.Patterns.Append(mif1);

    codecs.Add(libheif with { Patterns = patterns });
});

@cvdsoftware
Copy link
Author

Sorry for my late response, was on vacation.

Your fix works perfectly, thanks for the quick response!

@saucecontrol
Copy link
Owner

Thanks for confirming. I've published a new package with the additional signature (and which no longer requires manually removing the Windows HEIF codec before registering the plugin) https://www.nuget.org/packages/PhotoSauce.NativeCodecs.Libheif

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