fileformat-heic / FileFormat.HEIC-for-.NET

FileFormat.HEIC is an open source implementation of the ISO/IEC 23008-12:2017 HEIF file format decoder.
Other
7 stars 1 forks source link
heic-to-jpeg heic-to-jpg heic-to-png

FileFormat.HEIC

FileFormat.HEIC is an open source SDK implementing the ISO/IEC 23008-12:2017 HEIF file format decoder.

It is written from scratch and has a plain C# API to enable a simple integration into other software.

Supported features

FileFormat.HEIC has support for:

FileFormat.HEIC doesn't support:

Usage examples

Read .heic file to int array with Argb32 data

using (var fs = new FileStream("filename.heic", FileMode.Open))
{
    HeicImage image = HeicImage.Load(fs);
    int[] pixels = image.GetInt32Array(Heic.Decoder.PixelFormat.Argb32);
}

Convert .heic file to .jpg using System.Windows.Media.Imaging.WriteableBitmap

using (var fs = new FileStream("filename.heic", FileMode.Open))
{
    HeicImage image = HeicImage.Load(fs);

    var pixels = image.GetByteArray(Heic.Decoder.PixelFormat.Bgra32);
    var width = (int)image.Width;
    var height = (int)image.Height;

    var wbitmap = new WriteableBitmap(width, height, 72, 72, PixelFormats.Bgra32, null);
    var rect = new Int32Rect(0, 0, width, height);
    wbitmap.WritePixels(rect, pixels, 4 * width, 0);

    using FileStream saveStream = new FileStream("output.jpg", FileMode.OpenOrCreate);
    JpegBitmapEncoder encoder = new JpegBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create(wbitmap));
    encoder.Save(saveStream);
}

Convert .heic file to .png using System.Windows.Media.Imaging.WriteableBitmap

using (var fs = new FileStream("filename.heic", FileMode.Open))
{
    HeicImage image = HeicImage.Load(fs);

    var pixels = image.GetByteArray(Heic.Decoder.PixelFormat.Bgra32);
    var width = (int)image.Width;
    var height = (int)image.Height;

    var wbitmap = new WriteableBitmap(width, height, 72, 72, PixelFormats.Bgra32, null);
    var rect = new Int32Rect(0, 0, width, height);
    wbitmap.WritePixels(rect, pixels, 4 * width, 0);

    using FileStream saveStream = new FileStream("output.png", FileMode.OpenOrCreate);
    PngBitmapEncoder encoder = new PngBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create(wbitmap));
    encoder.Save(saveStream);
}

Convert .heic file to .png using System.Drawing.Common.Bitmap

using (var fs = new FileStream("filename.heic", FileMode.Open))
{
    HeicImage image = HeicImage.Load(fs);

    var pixels = image.GetInt32Array(Heic.Decoder.PixelFormat.Argb32);
    var width = (int)image.Width;
    var height = (int)image.Height;
    var i = 0;

    Bitmap myBitmap = new Bitmap(width, height);
    for (int y = 0; y < height; y++)
        for (int x = 0; x < width; x++)
            myBitmap.SetPixel(x, y, Color.FromArgb(pixels[i++]));

    myBitmap.Save("output.png");
}

Convert .heic collection to a set of .png files

using (var fs = new FileStream("filename.heic", FileMode.Open))
{
    HeicImage image = HeicImage.Load(fs);

    foreach (var key in image.Frames.Keys)
    {
        var width = (int)image.Frames[key].Width;
        var height = (int)image.Frames[key].Height;
        var pixels = image.Frames[key].GetByteArray(FileFormat.Heic.Decoder.PixelFormat.Bgra32);

        var wbitmap = new WriteableBitmap(width, height, 72, 72, PixelFormats.Bgra32, null);
        var rect = new Int32Rect(0, 0, width, height);
        wbitmap.WritePixels(rect, pixels, 4 * width, 0);

        using FileStream saveStream = new FileStream("output"+key+".png", FileMode.OpenOrCreate);
        PngBitmapEncoder encoder = new PngBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(wbitmap));
        encoder.Save(saveStream);
    }
}

Documentation

All public classes, methods and properties are documented in corresponding API_README:

HeicImage

Methods

Name Type Description Parameters Notes
Load HeicImage Reads the file meta data and creates a class object for further decoding of the file contents. Stream stream - File stream. This operation does not decode pixels.
Use the default frame methods GetByteArray or GetInt32Array afterwards in order to decode pixels.
CanLoad bool Checks if the stream can be read as a heic image.
Returns true if file header contains heic signarure, false otherwise
Stream stream - File stream.
GetByteArray byte[] Get pixel data of the default image frame in the format of byte array.
Each three or four bytes (the count depends on the pixel format) refer to one pixel left to right top to bottom line by line.
Returns null if frame does not contain image data.
PixelFormat pixelFormat - Pixel format that defines the order of colors and the presence of alpha byte.
Rectangle boundsRectangle - Bounds of the requested area.
GetInt32Array int[] Get pixel data of the default image frame in the format of integer array.
Each int value refers to one pixel left to right top to bottom line by line.
Returns null if frame does not contain image data.
PixelFormat pixelFormat - Pixel format that defines the order of colors.
Rectangle boundsRectangle - Bounds of the requested area.

Properties

Name Type Description
Frames Dictionary<uint, HeicImageFrame> Dictionary of public Heic image frames with access by identifier.
AllFrames Dictionary<uint, HeicImageFrame> Dictionary of all Heic image frames with access by identifier.
DefaultFrame HeicImageFrame Returns the default image frame, which is specified in meta data.

HeicImageFrame

Methods

Name Type Description Parameters
GetByteArray byte[] Get pixel data in the format of byte array.
Each three or four bytes (the count depends on the pixel format) refer to one pixel left to right top to bottom line by line.
Returns null if frame does not contain image data.
PixelFormat pixelFormat - Pixel format that defines the order of colors and the presence of alpha byte.
Rectangle boundsRectangle - Bounds of the requested area.
GetInt32Array int[] Get pixel data in the format of integer array.
Each int value refers to one pixel left to right top to bottom line by line.
Returns null if frame does not contain image data.
PixelFormat pixelFormat - Pixel format that defines the order of colors.
Rectangle boundsRectangle - Bounds of the requested area.
GetTextData string Get frame text data.
Exists only for mime frame types.

Properties

Name Type Description
ImageType ImageFrameType Type of an image frame content.
Width uint Width of the image frame in pixels.
Height uint Height of the image frame in pixels.
HasAlpha bool Indicates the presence of transparency of transparency layer.
True if frame is linked with alpha data frame, false otherwise.
IsHidden bool Indicates the fact that frame is marked as hidden.
True if frame is hidden, false otherwise.
IsImage bool Indicates the fact that frame contains image data.
True if frame is image, false otherwise.
IsDerived bool Indicates the fact that frame contains image transform data and is inherited from another frame(-s).
True if frame is derived, false otherwise.
DerivativeType BoxType? Indicates the type of derivative content if the frame is derived.
AuxiliaryReferenceType AuxiliaryReferenceType Indicates the type of auxiliary reference layer if the frame type is auxiliary.
NumberOfChannels byte Number of channels with color data.
BitsPerChannel byte[] Bits per channel with color data.

License

FileFormat.HEIC is available under Openize License.

[!CAUTION] Openize does not and cannot grant You a patent license for the utilization of HEVC/H.265 image compression/decompression technologies.

FileFormat.HEIC uses FileFormat.IsoBmff that is distributed under MIT License.

OSS Notice

Sample files used for tests and located in the "./FileFormat.Heic.Tests/TestsData/samples/nokia" folder belong to Nokia Technologies and are used according to Nokia High-Efficiency Image File Format (HEIF) License

Licensed Field means the non-commercial purposes of evaluation, testing and academic research in each non-commercial case to use, run, modify (in a way that still complies with the Specification) and copy the Software to (a) generate, using one or more encoded pictures as inputs, a file complying with the Specification and including the one or more encoded pictures that were given as inputs; and/or (b) read a file complying with the Specification, resulting into one or more encoded pictures included in the file as outputs.