How to open a HEIF (.heic) image?

12,583

Solution 1

An example viewer specific for HEIF from Nokia is available here. I also tested directly with FFmpeg and it's able to open (play/decompress) the provided conformance files.

Solution 2

libheif seems to be a pretty active LGPL library for HEIF, with a C API. From the README:

The library has a C API for easy integration and wide language support. Note that the API is still work in progress and may still change.

Loading the primary image in an HEIF file is as easy as this:

heif_context* ctx = heif_context_alloc();
heif_context_read_from_file(ctx, input_filename, nullptr);

// get a handle to the primary image
heif_image_handle* handle;
heif_context_get_primary_image_handle(ctx, &handle);

// decode the image and convert colorspace to RGB, saved as 24bit interleaved
heif_image* img;
heif_decode_image(handle, &img, heif_colorspace_RGB, heif_chroma_interleaved_24bit, nullptr);

int stride;
const uint8_t* data = heif_pixel_image_get_plane_readonly(img, heif_channel_interleaved, &stride);

A nice, Emscripten-powered demo is available which actually allows you to load and view HEIF files directly inside a browser.

Share:
12,583
Jean-Milost Reymond
Author by

Jean-Milost Reymond

Updated on June 08, 2022

Comments

  • Jean-Milost Reymond
    Jean-Milost Reymond about 2 years

    For a c++ project, I need to open and show a HEIF (.heic) image. What I know (if I'm right) is that the HEIF images are based on the ffmpeg standard, and requires a H265 codec to be read.

    I found several open-source H265 codecs:

    I can open and show H265 encoded video files with each of them, but I'm unable to simply open, show or convert a .heic image. All of them return an error, or just do nothing.

    To be honest I'm a little puzzled, because the HEIF standard seem to be a well kept secret. I'm unable to find a relevant info that could allow me to walk to a solution. Those I found are just tricks and workarounds, like e.g. forcing the device (I'm speaking here about the Apple iPhone using the new iOS11) to generate a jpg image instead of a heic, or using a third party application like dr.fone. Of course these solutions are irrelevant for me.

    So, somebody can tell me which codec I should use with a .heif image, and how I can use it to open it? Or are there open source libraries or examples that allow to manipulate this type of image file? Somebody can point me to the good direction?

  • Jean-Milost Reymond
    Jean-Milost Reymond almost 7 years
    Thanks for the links, I will do new tests with the conformance files. For the Nokia SDK, I already tried to use it, but without success, and the very restrictive license of the product discouraged me to push further my research. Anyway it's just a parser that allows to open a .heic file and transmit its stream to a codec, or I'm wrong?
  • Jean-Milost Reymond
    Jean-Milost Reymond almost 7 years
    So, the question may be now: "how to transmit a such stream to a codec". First of all, which codec can I use for that? (of course, a part of the answer should be in the conformance files, that I will test as soon as possible)
  • Ronald S. Bultje
    Ronald S. Bultje almost 7 years
    You mean when using Nokia's SDK? I would assume they'd want you to use hm for that, but it's obviously up to you, any decoder (even FFmpeg or de265) would work for that.
  • Jean-Milost Reymond
    Jean-Milost Reymond over 5 years
    Thank you for sharing this info. If a such a library had existed when I had to solve this question, I would probably have used it, but unfortunately in my case I already implemented my own library. But it's a good point to know that an opensource alternative exists now.