Top Related Projects
Quick Overview
libheif is a free and open-source library for encoding and decoding HEIF (High-Efficiency Image Format) files. HEIF is a modern image format that offers better compression and quality compared to traditional image formats like JPEG. libheif provides a simple API for developers to work with HEIF images in their applications.
Pros
- Efficient Image Compression: HEIF offers significantly better compression than JPEG, resulting in smaller file sizes without sacrificing image quality.
- Widespread Adoption: HEIF is supported by major platforms and devices, including iOS, macOS, and Android, making it a viable alternative to JPEG.
- Flexible Image Handling: libheif supports various HEIF features, such as alpha channels, HDR, and multi-frame images.
- Cross-Platform Compatibility: libheif is a cross-platform library, allowing developers to integrate HEIF support into their applications on different operating systems.
Cons
- Limited Browser Support: While HEIF is gaining traction, browser support is still limited, which may hinder its widespread adoption on the web.
- Licensing Concerns: The HEIF format is patented, and there are potential licensing fees associated with its use, which may be a concern for some developers.
- Dependency on External Libraries: libheif relies on other libraries, such as libde265 and libx265, which may add complexity to the integration process.
- Lack of Widespread Tooling: Compared to more established image formats like JPEG, the ecosystem of tools and utilities for working with HEIF is still relatively limited.
Code Examples
Here are a few examples of how to use the libheif library:
- Decoding a HEIF Image:
#include <libheif/heif.h>
heif_context* ctx = heif_context_create();
heif_image_handle* handle;
heif_decode_status status = heif_context_get_primary_image_handle(ctx, &handle);
heif_image* image;
heif_decode_image(handle, &image, heif_colorspace_RGB, heif_chroma_interleaved_RGB, NULL);
// Access and process the image data
uint8_t* data = heif_image_get_data_pointer(image);
int width = heif_image_get_width(image, heif_channel_interleaved);
int height = heif_image_get_height(image, heif_channel_interleaved);
// Clean up
heif_image_release(image);
heif_image_handle_release(handle);
heif_context_free(ctx);
- Encoding a HEIF Image:
#include <libheif/heif.h>
heif_context* ctx = heif_context_create();
heif_image* image = heif_image_create(width, height, heif_colorspace_RGB, heif_chroma_interleaved_RGB);
// Set image data
uint8_t* data = heif_image_get_data_pointer(image);
// Fill the data with image pixels
heif_writer* writer = heif_context_get_writer(ctx);
heif_image_handle* handle;
heif_encode_status status = heif_context_encode_image(ctx, image, writer, &handle, NULL);
// Write the HEIF file
FILE* fp = fopen("output.heif", "wb");
heif_writer_write_to_file(writer, fp);
fclose(fp);
// Clean up
heif_image_handle_release(handle);
heif_image_release(image);
heif_context_free(ctx);
- Extracting Metadata from a HEIF Image:
#include <libheif/heif.h>
heif_context* ctx = heif_context_create();
heif_image_handle* handle;
heif_decode_status status = heif_context_get_primary_image_handle(ctx, &handle);
// Get image metadata
int width = heif_image_handle_get_width(handle);
int height = heif_
Competitor Comparisons
High Efficiency Image File Format
Pros of HEIF
- HEIF is a more comprehensive and actively maintained library, with support for a wider range of HEIF features and capabilities.
- HEIF has better documentation and a more active community, making it easier to get started and find support.
- HEIF is licensed under the MPL 2.0 license, which is more permissive than the GPL 3.0 license used by libheif.
Cons of HEIF
- HEIF is a larger and more complex library, which may make it more difficult to integrate into some projects.
- HEIF has a more restrictive license than libheif, which may be a concern for some users.
- HEIF may have a higher learning curve for developers who are new to the HEIF format.
Code Comparison
libheif:
heif_context_t* ctx = heif_context_alloc();
heif_image_handle_t* handle;
heif_decode_image(ctx, data, size, HEIF_COLORSPACE_RGB, HEIF_CHROMA_420, &handle, HEIF_DECODE_FLAGS_IGNORE_TRANSFORMATIONS);
heif_image_t* image;
heif_image_handle_get_image_data(handle, &image);
HEIF:
heif::Context ctx;
heif::ImageHandle handle = ctx.decode_image(data, size);
heif::Image image = handle.get_image();
Open h.265 video codec implementation.
Pros of libde265
- libde265 is a free and open-source implementation of the H.265/HEVC video codec, which provides improved compression efficiency compared to older codecs like H.264.
- The library is actively maintained and has a larger community of contributors compared to libheif.
- libde265 supports a wider range of hardware acceleration options, including NVIDIA CUDA and Intel Quick Sync Video.
Cons of libde265
- libde265 is primarily focused on the H.265/HEVC codec, while libheif supports a broader range of image formats, including HEIF, AVIF, and WebP.
- The API and usage of libde265 may be more complex compared to libheif, as it deals with the intricacies of video decoding.
- libde265 has a larger codebase and dependency footprint, which may be a concern for some use cases.
Code Comparison
libheif (HEIF image decoding):
heif_context* ctx = heif_context_alloc();
heif_image_handle* handle;
heif_decode_image(ctx, &handle, 0, HEIF_COLORSPACE_RGB, HEIF_CHROMA_444, NULL, NULL);
libde265 (H.265/HEVC video decoding):
de265_decoder* decoder = de265_new_decoder();
de265_start_worker_threads(decoder, 4);
de265_push_data(decoder, data, size, 0, NULL);
de265_decode_data(decoder);
Mirror only. Please do not send pull requests. See https://chromium.googlesource.com/webm/libwebp/+/HEAD/CONTRIBUTING.md.
Pros of libwebp
- Widely adopted and supported by major web browsers and image editing software
- Provides a range of compression options and features, including lossless and lossy compression
- Actively maintained with regular updates and bug fixes
Cons of libwebp
- Limited support for alpha transparency compared to HEIF
- Older image format that may not offer the same level of compression efficiency as newer formats like HEIF
- Lacks some advanced features found in HEIF, such as support for HDR and 10-bit color depth
Code Comparison
Here's a brief comparison of the code structure for the two libraries:
libheif:
static void heif_error_cb(void *userdata, enum heif_error_code code, enum heif_suberror_code subcode, const char *message) {
struct heif_error *error = (struct heif_error*)userdata;
error->code = code;
error->subcode = subcode;
error->message = message;
}
heif_context_t* heif_context_alloc() {
heif_context_t* ctx = (heif_context_t*)calloc(1, sizeof(heif_context_t));
ctx->error_cb = heif_error_cb;
ctx->error_cb_userdata = &ctx->error;
return ctx;
}
libwebp:
int WebPGetInfo(const uint8_t* data, size_t data_size,
int* width, int* height) {
if (data == NULL || data_size < WEBP_HEADER_SIZE) return 0;
if (width != NULL) *width = (data[WEBP_HEADER_SIZE + 0] << 8) |
data[WEBP_HEADER_SIZE + 1];
if (height != NULL) *height = (data[WEBP_HEADER_SIZE + 2] << 8) |
data[WEBP_HEADER_SIZE + 3];
return 1;
}
int WebPGetFeatures(const uint8_t* data, size_t data_size,
WebPBitstreamFeatures* features) {
if (data == NULL || data_size < WEBP_HEADER_SIZE) return VP8_STATUS_INVALID_PARAM;
return VP8GetInfo(data, data_size, features);
}
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
libheif
libheif is an ISO/IEC 23008-12:2017 HEIF and AVIF (AV1 Image File Format) file format decoder and encoder. There is partial support for ISO/IEC 23008-12:2022 (2nd Edition) capabilities.
HEIF and AVIF are new image file formats employing HEVC (H.265) or AV1 image coding, respectively, for the best compression ratios currently possible.
libheif makes use of libde265 for HEIF image decoding and x265 for encoding. For AVIF, libaom, dav1d, svt-av1, or rav1e are used as codecs.
Supported features
libheif has support for:
- HEIC, AVIF, VVC, JPEG-in-HEIF, JPEG2000, uncompressed (ISO/IEC 23001-17:2024)
- alpha channels, depth maps, thumbnails, auxiliary images
- multiple images in a file
- HDR images, correct color transform according to embedded color profiles
- image transformations (crop, mirror, rotate), overlay images
- plugin interface to add alternative codecs
- reading EXIF and XMP metadata
- region annotations and mask images
- decoding of files while downloading (e.g. extract image size before file has been completely downloaded)
Supported codecs:
Format | Decoders | Encoders |
---|---|---|
HEIC | libde265, ffmpeg | x265, kvazaar |
AVIF | AOM, dav1d | AOM, rav1e, svt-av1 |
VVC | vvdec | vvenc, uvg266 |
JPEG | libjpeg(-turbo) | libjpeg(-turbo) |
JPEG2000 | OpenJPEG | OpenJPEG |
uncompressed | built-in | built-in |
API
The library has a C API for easy integration and wide language support.
The decoder automatically supports both HEIF and AVIF through the same API. No changes are required to existing code to support AVIF.
The encoder can be switched between HEIF and AVIF simply by setting heif_compression_HEVC
or heif_compression_AV1
to heif_context_get_encoder_for_format()
.
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_RGB, nullptr);
int stride;
const uint8_t* data = heif_image_get_plane_readonly(img, heif_channel_interleaved, &stride);
// ... process data as needed ...
// clean up resources
heif_image_release(img);
heif_image_handle_release(handle);
heif_context_free(ctx);
Writing an HEIF file can be done like this:
heif_context* ctx = heif_context_alloc();
// get the default encoder
heif_encoder* encoder;
heif_context_get_encoder_for_format(ctx, heif_compression_HEVC, &encoder);
// set the encoder parameters
heif_encoder_set_lossy_quality(encoder, 50);
// encode the image
heif_image* image; // code to fill in the image omitted in this example
heif_context_encode_image(ctx, image, encoder, nullptr, nullptr);
heif_encoder_release(encoder);
heif_context_write_to_file(ctx, "output.heic");
heif_context_free(ctx);
Get the EXIF data from an HEIF file:
heif_item_id exif_id;
int n = heif_image_handle_get_list_of_metadata_block_IDs(image_handle, "Exif", &exif_id, 1);
if (n==1) {
size_t exifSize = heif_image_handle_get_metadata_size(image_handle, exif_id);
uint8_t* exifData = malloc(exifSize);
struct heif_error error = heif_image_handle_get_metadata(image_handle, exif_id, exifData);
}
See the header file heif.h
for the complete C API.
There is also a C++ API which is a header-only wrapper to the C API. Hence, you can use the C++ API and still be binary compatible. Code using the C++ API is much less verbose than using the C API directly.
There is also an experimental Go API, but this is not stable yet.
Compiling
This library uses the CMake build system (the earlier autotools build files have been removed in v1.16.0).
For a minimal configuration, we recommend to use the codecs libde265 and x265 for HEIC and AOM for AVIF. Make sure that you compile and install libde265 first, so that the configuration script will find this. Also install x265 and its development files if you want to use HEIF encoding, but note that x265 is GPL. An alternative to x265 is kvazaar (BSD).
The basic build steps are as follows (--preset argument needs CMake >= 3.21):
mkdir build
cd build
cmake --preset=release ..
make
There are CMake presets to cover the most frequent use cases.
release
: the preferred preset which compiles all codecs as separate plugins. If you do not want to distribute some of these plugins (e.g. HEIC), you can omit packaging these.release-noplugins
: this is a smaller, self-contained build of libheif without using the plugin system. A single library is built with support for HEIC and AVIF.testing
: for building and executing the unit tests. Also the internal library symbols are exposed. Do not use for distribution.fuzzing
: all codecs like in release build, but configured into a self-contained library with enabled fuzzers. The library should not distributed.
You can optionally adapt these standard configurations to your needs.
This can be done, for example, by calling ccmake .
from within the build
directory.
CMake configuration variables
Libheif supports many different codecs. In order to reduce the number of dependencies and the library size, you can choose which of these codecs to include. Each codec can be compiled either as built-in to the library with a hard dependency, or as a separate plugin file that is loaded dynamically.
For each codec, there are two configuration variables:
WITH_{codec}
: enables the codecWITH_{codec}_PLUGIN
: when enabled, the codec is compiled as a separate plugin.
In order to use dynamic plugins, also make sure that ENABLE_PLUGIN_LOADING
is enabled.
The placeholder {codec}
can have these values: LIBDE265
, X265
, AOM_DECODER
, AOM_ENCODER
, SvtEnc
, DAV1D
, FFMPEG_DECODER
, JPEG_DECODER
, JPEG_ENCODER
, KVAZAAR
, OpenJPEG_DECODER
, OpenJPEG_ENCODER
, OPENJPH_ENCODER
, VVDEC
, VVENC
, UVG266
.
Further options are:
WITH_UNCOMPRESSED_CODEC
: enable support for uncompressed images according to ISO/IEC 23001-17:2024. This is experimental and not available as a dynamic plugin. When enabled, it adds a dependency tozlib
, and optionally will usebrotli
.WITH_HEADER_COMPRESSION
: enables support for compressed metadata. When enabled, it adds a dependency tozlib
. Note that header compression is not widely supported yet.WITH_LIBSHARPYUV
: enables high-quality YCbCr/RGB color space conversion algorithms (requireslibsharpyuv
, e.g. from thethird-party
directory).ENABLE_MULTITHREADING_SUPPORT
: can be used to disable any multithreading support, e.g. for embedded platforms.ENABLE_PARALLEL_TILE_DECODING
: when enabled, libheif will decode tiled images in parallel to speed up compilation.PLUGIN_DIRECTORY
: the directory where libheif will search for dynamic plugins when the environment variableLIBHEIF_PLUGIN_PATH
is not set.WITH_REDUCED_VISIBILITY
: only export those symbols into the library that are public API. Has to be turned off for running some tests.
macOS
-
Install dependencies with Homebrew
brew install cmake make pkg-config x265 libde265 libjpeg libtool
-
Configure and build project (--preset argument needs CMake >= 3.21):
mkdir build cd build cmake --preset=release .. ./configure make
Windows
You can build and install libheif using the vcpkg dependency manager:
git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.bat
./vcpkg integrate install
./vcpkg install libheif
The libheif port in vcpkg is kept up to date by Microsoft team members and community contributors. If the version is out of date, please create an issue or pull request on the vcpkg repository.
Adding libaom encoder/decoder for AVIF
- Run the
aom.cmd
script in thethird-party
directory to download libaom and compile it.
When running cmake
or configure
, make sure that the environment variable
PKG_CONFIG_PATH
includes the absolute path to third-party/aom/dist/lib/pkgconfig
.
Adding rav1e encoder for AVIF
- Install
cargo
. - Install
cargo-c
by executing
cargo install --force cargo-c
- Run the
rav1e.cmd
script in thethird-party
directory to download rav1e and compile it.
When running cmake
, make sure that the environment variable
PKG_CONFIG_PATH
includes the absolute path to third-party/rav1e/dist/lib/pkgconfig
.
Adding dav1d decoder for AVIF
- Install
meson
. - Run the
dav1d.cmd
script in thethird-party
directory to download dav1d and compile it.
When running cmake
, make sure that the environment variable
PKG_CONFIG_PATH
includes the absolute path to third-party/dav1d/dist/lib/x86_64-linux-gnu/pkgconfig
.
Adding SVT-AV1 encoder for AVIF
You can either use the SVT-AV1 encoder libraries installed in the system or use a self-compiled current version. If you want to compile SVT-AV1 yourself,
- Run the
svt.cmd
script in thethird-party
directory to download SVT-AV1 and compile it.
When running cmake
or configure
, make sure that the environment variable
PKG_CONFIG_PATH
includes the absolute path to third-party/SVT-AV1/Build/linux/install/lib/pkgconfig
.
You may have to replace linux
in this path with your system's identifier.
You have to enable SVT-AV1 with CMake.
Codec plugins
Starting with v1.14.0, each codec backend can be compiled statically into libheif or as a dynamically loaded plugin (currently Linux only). You can choose this individually for each codec backend in the CMake settings. Compiling a codec backend as dynamic plugin will generate a shared library that is installed in the system together with libheif. The advantage is that only the required plugins have to be installed and libheif has fewer dependencies.
The plugins are loaded from the colon-separated (semicolon-separated on Windows) list of directories stored in the environment variable LIBHEIF_PLUGIN_PATH
.
If this variable is empty, they are loaded from a directory specified in the CMake configuration.
You can also add plugin directories programmatically.
Codec specific notes
- the FFMPEG decoding plugin can make use of h265 hardware decoders. However, it currently (v1.17.0, ffmpeg v4.4.2) does not work correctly with all streams. Thus, libheif still prefers the libde265 decoder if it is available.
Encoder benchmark
A current benchmark of the AVIF encoders (as of 14 Oct 2022) can be found on the Wiki page AVIF encoding benchmark.
Language bindings
- .NET Platform (C#, F#, and other languages): libheif-sharp
- C++: part of libheif
- Go: part of libheif
- JavaScript: by compilation with emscripten (see below)
- NodeJS module: libheif-js
- Python: pyheif, pillow_heif
- Rust: libheif-sys
- Swift: libheif-Xcode
Languages that can directly interface with C libraries (e.g., Swift, C#) should work out of the box.
Compiling to JavaScript / WASM
libheif can also be compiled to JavaScript using emscripten. It can be built like this (in the libheif directory):
mkdir buildjs
cd buildjs
USE_WASM=0 ../build-emscripten.sh ..
Set USE_WASM=1
to build with WASM output.
See the build-emscripten.sh
script for further options.
Online demo
Check out this online demo.
This is libheif
running in JavaScript in your browser.
Example programs
Some example programs are provided in the examples
directory.
The program heif-dec
converts all images stored in an HEIF/AVIF file to JPEG or PNG.
heif-enc
lets you convert JPEG files to HEIF/AVIF.
The program heif-info
is a simple, minimal decoder that dumps the file structure to the console.
For example convert example.heic
to JPEGs and one of the JPEGs back to HEIF:
cd examples/
./heif-dec example.heic example.jpeg
./heif-enc example-1.jpeg -o example.heif
In order to convert example-1.jpeg
to AVIF use:
./heif-enc example-1.jpeg -A -o example.avif
There is also a GIMP plugin using libheif here.
HEIF/AVIF thumbnails for the Gnome desktop
The program heif-thumbnailer
can be used as an HEIF/AVIF thumbnailer for the Gnome desktop.
The matching Gnome configuration files are in the gnome
directory.
Place the files heif.xml
and avif.xml
into /usr/share/mime/packages
and heif.thumbnailer
into /usr/share/thumbnailers
.
You may have to run update-mime-database /usr/share/mime
to update the list of known MIME types.
gdk-pixbuf loader
libheif also includes a gdk-pixbuf loader for HEIF/AVIF images. 'make install' will copy the plugin
into the system directories. However, you will still have to run gdk-pixbuf-query-loaders --update-cache
to update the gdk-pixbuf loader database.
Software using libheif
- GIMP
- Krita
- ImageMagick
- GraphicsMagick
- darktable
- digiKam 7.0.0
- libvips
- libGD
- Kodi HEIF image decoder plugin
- bimg
- GDAL
- OpenImageIO
- XnView
Packaging status
Sponsors
Since I work as an independent developer, I need your support to be able to allocate time for libheif. You can sponsor the development using the link in the right hand column.
A big thank you goes to these major sponsors for supporting the development of libheif:
- Shopify
- StrukturAG
License
The libheif is distributed under the terms of the GNU Lesser General Public License. The sample applications are distributed under the terms of the MIT License.
See COPYING for more details.
Copyright (c) 2017-2020 Struktur AG
Copyright (c) 2017-2024 Dirk Farin
Contact: Dirk Farin dirk.farin@gmail.com
Top Related Projects
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot