20 stable releases

2.1.0+1.17.4 Nov 28, 2023
2.0.0+1.16.2 Sep 12, 2023
1.16.2 Sep 8, 2023
1.16.1 Jun 21, 2023
1.4.2 Jul 16, 2019

#336 in Images

Download history 837/week @ 2023-10-30 728/week @ 2023-11-06 993/week @ 2023-11-13 1195/week @ 2023-11-20 1267/week @ 2023-11-27 1055/week @ 2023-12-04 591/week @ 2023-12-11 650/week @ 2023-12-18 712/week @ 2023-12-25 759/week @ 2024-01-01 732/week @ 2024-01-08 634/week @ 2024-01-15 924/week @ 2024-01-22 964/week @ 2024-01-29 1030/week @ 2024-02-05 943/week @ 2024-02-12

3,922 downloads per month
Used in 11 crates (2 directly)

MIT license

285KB
3K SLoC

libheif-sys is bindings to libheif

CHANGELOG

System dependencies

  • libheif-dev >= 1.17.0 (any version if use-bindgen feature is enabled)
  • clang - to generate rust bindings for libheif. See bindgen requirements.

clang wouldn't be needed if you disable use-bindgen feature. In this case the pre-generated file bindings.rs will be used instead of generating it on the fly with help of bindgen crate.

Warning: bindings.rs file was generated under x64 linux and may not work as expected under x32 architectures or other operating systems.

Linux

The crate uses pkg-confing to find installed libheif.

Windows

The crate uses vcpkg crate to find libheif installed with help of vcpkg.

You can use cargo-vcpkg to install libheif with help of cargo command:

cargo vcpkg -v build

cargo-vcpkg can fetch and build a vcpkg installation of required packages from scratch. It merges package requirements specified in the Cargo.toml of crates in the dependency tree.

Example of reading and decoding of HEIF-image

use std::ffi;
use std::ptr;

use libheif_sys as lh;

#[test]
fn read_and_decode_heic_file() {
    unsafe {
        lh::heif_init(ptr::null_mut());

        let ctx = lh::heif_context_alloc();
        assert_ne!(ctx, ptr::null_mut());

        let c_name = ffi::CString::new("data/test.heif").unwrap();
        let err = lh::heif_context_read_from_file(ctx, c_name.as_ptr(), ptr::null());
        assert_eq!(err.code, 0);

        let mut handle = ptr::null_mut();
        let err = lh::heif_context_get_primary_image_handle(ctx, &mut handle);
        assert_eq!(err.code, 0);
        assert!(!handle.is_null());

        let width = lh::heif_image_handle_get_width(handle);
        assert_eq!(width, 4032);
        let height = lh::heif_image_handle_get_height(handle);
        assert_eq!(height, 3024);

        let options = lh::heif_decoding_options_alloc();

        let mut image = ptr::null_mut();
        let err = lh::heif_decode_image(
            handle,
            &mut image,
            lh::heif_colorspace_heif_colorspace_RGB,
            lh::heif_chroma_heif_chroma_444,
            options,
        );
        lh::heif_decoding_options_free(options);
        assert_eq!(err.code, 0);
        assert!(!image.is_null());

        let colorspace = lh::heif_image_get_colorspace(image);
        assert_eq!(colorspace, lh::heif_colorspace_heif_colorspace_RGB);
        let chroma_format = lh::heif_image_get_chroma_format(image);
        assert_eq!(chroma_format, lh::heif_chroma_heif_chroma_444);
        let width = lh::heif_image_get_width(image, lh::heif_channel_heif_channel_R);
        assert_eq!(width, 4032);
        let height = lh::heif_image_get_height(image, lh::heif_channel_heif_channel_R);
        assert_eq!(height, 3024);

        lh::heif_context_free(ctx);

        lh::heif_deinit();
    };
}

Dependencies