hey-red / ImageSharp.Heif

HEIF/AVIF decoder for ImageSharp
MIT License
7 stars 3 forks source link
avif heic heif imagesharp

ImageSharp.Heif

HEIF/AVIF decoder for ImageSharp

🚧 WIP. Some features(like encoder) is not implemented.

Use libheif-sharp for all features provided by libheif.

Install

via NuGet:

PM> Install-Package HeyRed.ImageSharp.Heif

Native libs for x64 Linux and Windows:

PM> Install-Package LibHeif.Native

Separate native packages:

PM> Install-Package LibHeif.Native.win-x64
PM> Install-Package LibHeif.Native.linux-x64

Usage

using System.IO;

using SixLabors.ImageSharp;

using HeyRed.ImageSharp.Heif.Formats.Heif;
using HeyRed.ImageSharp.Heif.Formats.Avif;

var decoderOptions = new DecoderOptions()
{
    Configuration = new Configuration(
        new AvifConfigurationModule(), 
        new HeifConfigurationModule())
};

using var image = Image.Load(decoderOptions, "/path/to/image.avif"); // or image.heic

// Do something with image
...

Top level images

⚠️ Due to the ImageSharp limitations, decoding top level images with different sizes are not supported. ⚠️

Note: libheif have some limitations

By default DecodingMode set to PrimaryImage, but if you want decode all top level images see example listed below:

var decoderOptions = new HeifDecoderOptions
{
    DecodingMode = DecodingMode.TopLevelImages,
    // MaxFrames option is also supported
    GeneralOptions = new DecoderOptions
    {
        MaxFrames = 10,
    }
};

using var inputStream = File.OpenRead("/path/to/image.avif"); // or image.heic

using var image = HeifDecoder.Instance.Decode(decoderOptions, inputStream);

// Saves all frames
for (int i = 0; i < image.Frames.Count; i++)
{
    image.Frames
        .CloneFrame(i)
        .SaveAsJpeg($"frame{i}.jpg");
}

License

MIT