Crate libheif_sys
source ·Expand description
§libheif-sys is bindings to libheif
§System dependencies
libheif-dev
>= 1.17.0 (any version ifuse-bindgen
feature is enabled)clang
- to generate rust bindings forlibheif
. 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();
};
}
Structs§
- Note: a value of 0 for any of these values indicates that the value is undefined. The unit of these values is Candelas per square meter.
- The units for max_display_mastering_luminance and min_display_mastering_luminance is Candelas per square meter.
- — register plugins
- The encoder used for actually encoding an image.
- A description of the encoder’s capabilities and name.
- A configuration parameter of the encoder. Each encoder implementation may have a different set of parameters. For the most common settings (e.q. quality), special functions to set the parameters are provided.
- ========================= library initialization ======================
- Note: color coordinates are defined according to the CIE 1931 definition of x as specified in ISO 11664-1 (see also ISO 11664-3 and CIE 15).
Constants§
- HEIF image with AV1
- EVC image
- EVC sequence
- HEIF image with h265
- multiview
- scalable
- 10bit images, or anything that uses h265 with range extension
- brands for image sequences
- multiview sequence
- scalable sequence
- brands for image sequences
- JPEG2000 image sequence
- JPEG2000 image
- image, any coding algorithm
- sequence, any coding algorithm
- VVC image
- VVC sequence
- Combine with ‘heif_chroma_upsampling_bilinear’ for best quality. Makes edges look sharper when using YUV 420 with bilinear chroma upsampling.
- HDR, big endian.
- HDR, little endian.
- HDR, big endian.
- HDR, little endian.
- g=0.3;0.6, b=0.15;0.06, r=0.64;0.33, w=0.3127,0.3290
- heif_colorspace_RGB should be used with one of these heif_chroma values:
- heif_colorspace_YCbCr should be used with one of these heif_chroma values:
- heif_colorspace_monochrome should only be used with heif_chroma = heif_chroma_monochrome
- AV1 compression, used for AVIF images.
- AVC compression. (Currently unused in libheif.)
- EVC compression. (Currently unused in libheif.)
- HEVC compression, used for HEIC images.
- JPEG compression.
- JPEG 2000 compression.
- VVC compression. (Currently unused in libheif.)
- Mask image encoding.
- Uncompressed encoding.
- Unspecified / undefined compression format.
- Application has asked for a color profile type that does not exist
- The decoder plugin generated an error
- The encoder plugin generated an error
- Error during encoding or when writing to the output
- Input file does not exist.
- Error in input file. Corrupted or invalid content.
- Could not allocate enough memory.
- Everything ok, no error occurred.
- Error loading a dynamic plugin
- Image requires an unsupported decoder feature.
- Input file type is not supported.
- Library API has been used in an invalid way.
- not sure whether it is an heif, try detection with more input data
- it is heif and can be read by libheif
- it is heif, but cannot be read by libheif
- TODO: or 601-7 according to h.273
- TODO: or 709-6 according to h.273
- size has not been reached and never will. The file has grown to its full size
- requested size has been reached, we can read until this point
- size has not been reached yet, but it may still grow further
- Overlay image completely outside of visible canvas area
- error while scanning the directory for plugins
- — Encoding_error —
- — Encoding_error —
- — Encoding_error —
- — Encoding_error —
- End of data reached unexpectedly.
- Size of box (defined in header) is wrong
- Tile-images in a grid image are missing
- Overlay image completely outside of visible canvas area
- Invalid specification of image grid (tiled image)
- Overlay image completely outside of visible canvas area
- Invalid specification of overlay image
- The value for the given parameter is not in the valid range.
- Overlay image completely outside of visible canvas area
- Error in property specification
- Invalid specification of region item
- An item property referenced in the ‘ipma’ box is not existing in the ‘ipco’ container.
- Image reference cycle found in iref
- Tile-images in a grid image are missing
- Overlay image completely outside of visible canvas area
- Mandatory ‘ftyp’ box is missing
- Mandatory ‘ftyp’ box is missing
- Mandatory ‘ftyp’ box is missing
- Mandatory ‘ftyp’ box is missing
- Mandatory ‘ftyp’ box is missing
- Mandatory ‘ftyp’ box is missing
- Overlay image completely outside of visible canvas area
- Mandatory ‘ftyp’ box is missing
- Mandatory ‘ftyp’ box is missing
- Mandatory ‘ftyp’ box is missing
- Mandatory ‘ftyp’ box is missing
- Image has no (compressed) data
- Mandatory ‘ftyp’ box is missing
- Overlay image completely outside of visible canvas area
- Mandatory ‘ftyp’ box is missing
- Mandatory ‘ftyp’ box is missing
- No properties have been assigned to an item.
- Image channel referenced that does not exist in the image
- also used for Invalid_input
- An API argument was given a NULL pointer, which is not allowed for that function.
- Overlay image completely outside of visible canvas area
- trying to remove a plugin that is not loaded
- a specific plugin file cannot be loaded
- A security limit preventing unreasonable memory allocations was exceeded by the input file. Please check whether the file is valid. If it is, contact us so that we could increase the security limits further.
- — Encoding_error —
- Overlay image completely outside of visible canvas area
- Overlay image completely outside of visible canvas area
- Overlay image completely outside of visible canvas area
- Overlay image completely outside of visible canvas area
- no further information available
- — Encoder_plugin_error —
- Image was coded with an unsupported compression method.
- The conversion of the source image to the requested chroma / colorspace is not supported.
- Image is specified in an unknown way, e.g. as tiled grid image (which is supported)
- The conversion of the source image to the requested chroma / colorspace is not supported.
- Image is specified in an unknown way, e.g. as tiled grid image (which is supported)
- The conversion of the source image to the requested chroma / colorspace is not supported.
- The given (encoder) parameter name does not exist.
- The version of the passed plugin is not supported.
- The version of the passed writer is not supported.
- Overlay image completely outside of visible canvas area
- Overlay image completely outside of visible canvas area
Functions§
- the output buffer must be at least 4 bytes long
- input data should be at least 12 bytes
- Add XMP metadata to an image.
- New version of heif_context_add_XMP_metadata() with data compression (experimental).
- Add EXIF metadata to an image.
- Add generic, proprietary metadata to an image. You have to specify an ‘item_type’ that will identify your metadata. ‘content_type’ can be an additional type, or it can be NULL. For example, this function can be used to add IPTC metadata (IIM stream, not XMP) to an image. Although not standard, we propose to store IPTC data with item type=“iptc”, content_type=NULL.
- Allocate a new context for reading HEIF files. Has to be freed again with heif_context_free().
- Assign ‘thumbnail_image’ as the thumbnail image of ‘master_image’.
- Print information about the boxes of a HEIF file to file descriptor. This is for debugging and informational purposes only. You should not rely on the output having a specific format. At best, you should not use this at all.
- Compress the input image. Returns a handle to the coded image in ‘out_image_handle’ unless out_image_handle = NULL. ‘options’ should be NULL for now. The first image added to the context is also automatically set the primary image, but you can change the primary image later with heif_context_set_primary_image().
- Encode the ‘image’ as a scaled down thumbnail image. The image is scaled down to fit into a square area of width ‘bbox_size’. If the input image is already so small that it fits into this bounding box, no thumbnail image is encoded and NULL is returned in ‘out_thumb_image_handle’. No error is returned in this case. The encoded thumbnail is automatically assigned to the ‘master_image_handle’. Hence, you do not have to call heif_context_assign_thumbnail().
- Free a previously allocated HEIF context. You should not free a context twice.
- Get an encoder instance that can be used to actually encode images from a descriptor.
- DEPRECATED: use heif_get_encoder_descriptors() instead. Get a list of available encoders. You can filter the encoders by compression format and name. Use format_filter==heif_compression_undefined and name_filter==NULL as wildcards. The returned list of encoders is sorted by their priority (which is a plugin property). The number of encoders is returned, which are not more than ‘count’ if (out_encoders != nullptr). By setting out_encoders==nullptr, you can query the number of encoders, ‘count’ is ignored. Note: to get the actual encoder from the descriptors returned here, use heif_context_get_encoder().
- Get an encoder for the given compression format. If there are several encoder plugins for this format, the encoder with the highest plugin priority will be returned.
- Get the image handle for a known image ID.
- Fills in image IDs into the user-supplied int-array ‘ID_array’, preallocated with ‘count’ entries. Function returns the total number of IDs filled into the array.
- Number of top-level images in the HEIF file. This does not include the thumbnails or the tile images that are composed to an image grid. You can get access to the thumbnails via the main image handle.
- Get a handle to the primary image of the HEIF file. This is the image that should be displayed primarily when there are several images in the file.
- Read a HEIF file from a named disk file. The heif_reading_options should currently be set to NULL.
- Read a HEIF file stored completely in memory. The heif_reading_options should currently be set to NULL. DEPRECATED: use heif_context_read_from_memory_without_copy() instead.
- Same as heif_context_read_from_memory() except that the provided memory is not copied. That means, you will have to keep the memory area alive as long as you use the heif_context.
- If the maximum threads number is set to 0, the image tiles are decoded in the main thread. This is different from setting it to 1, which will generate a single background thread to decode the tiles. Note that this setting only affects libheif itself. The codecs itself may still use multi-threaded decoding. You can use it, for example, in cases where you are decoding several images in parallel anyway you thus want to minimize parallelism in each decoder.
- ==================================================================================================== Encoding API
- Decode an heif_image_handle into the actual pixel image and also carry out all geometric transformations specified in the HEIF file (rotation, cropping, mirroring).
- Return a short, symbolic name for identifying the decoder. This name should stay constant over different decoder versions. Note: the returned ID may be NULL for old plugins that don’t support this yet.
- Return a long, descriptive name of the decoder (including version information).
- Allocate decoding options and fill with default values. Note: you should always get the decoding options through this function since the option structure may grow in size in future versions.
- Deinitialise and clean up library.
- Return a short, symbolic name for identifying the encoder. This name should stay constant over different encoder versions.
- Return a long, descriptive name of the encoder (including version information).
- DEPRECATED, typo in function name
- DEPRECATED, typo in function name
- Get the encoder name from the encoder itself.
- Get the current value of a parameter of any type as a human readable string. The returned string is compatible with heif_encoder_set_parameter().
- Query whether a specific parameter has a default value.
- Get a generic list of encoder parameters. Each encoder may define its own, additional set of parameters. You do not have to free the returned list.
- Return the parameter name.
- Return the parameter type.
- DEPRECATED. Use heif_encoder_parameter_get_valid_integer_values() instead.
- If integer is limited by a range, have_minimum and/or have_maximum will be != 0 and *minimum, *maximum is set. If integer is limited by a fixed set of values, *num_valid_values will be >0 and *out_integer_array is set.
- returns a NULL-terminated list of valid strings or NULL if all values are allowed
- You have to release the encoder after use.
- level should be between 0 (= none) to 4 (= full)
- Set a ‘quality’ factor (0-100). How this is mapped to actual encoding parameters is encoder dependent.
- Set a parameter of any type to the string value. Integer values are parsed from the string. Boolean values can be “true”/“false”/“1”/“0”
- ‘brand_fourcc’ must be 4 character long, but need not be 0-terminated
- Get a list of available decoders. You can filter the encoders by compression format. Use format_filter==heif_compression_undefined to get all available decoders. The returned list of decoders is sorted by their priority (which is a plugin property). The number of decoders is returned, which are not more than ‘count’ if (out_decoders != nullptr). By setting out_decoders==nullptr, you can query the number of decoders, ‘count’ is ignored.
- Get a list of available encoders. You can filter the encoders by compression format and name. Use format_filter==heif_compression_undefined and name_filter==NULL as wildcards. The returned list of encoders is sorted by their priority (which is a plugin property). The number of encoders is returned, which are not more than ‘count’ if (out_encoders != nullptr). By setting out_encoders==nullptr, you can query the number of encoders, ‘count’ is ignored. Note: to get the actual encoder from the descriptors returned here, use heif_context_get_encoder().
- Returns one of these MIME types:
- Get a NULL terminated array of the plugin directories that are searched by libheif. This includes the paths specified in the environment variable LIBHEIF_PLUGIN_PATHS and the built-in path (if not overridden by the environment variable).
- Version string of linked libheif library.
- Numeric version of linked libheif library, encoded as 0xHHMMLL00 = hh.mm.ll, where hh, mm, ll is the decimal representation of HH, MM, LL. For example: 0x02150300 is version 2.21.3
- Numeric part “LL” from above. Returned as a decimal number.
- Numeric part “HH” from above. Returned as a decimal number.
- Numeric part “MM” from above. Returned as a decimal number.
- ‘brand_fourcc’ must be 4 character long, but need not be 0-terminated returns 1 if file includes the brand, and 0 if it does not returns -1 if the provided data is not sufficient (you should input at least as many bytes as indicated in the first 4 bytes of the file, usually ~50 bytes will do) returns -2 on other errors
- Quick check whether there is a decoder available for the given format. Note that the decoder still may not be able to decode all variants of that format. You will have to query that further (todo) or just try to decode and check the returned error.
- Quick check whether there is an enoder available for the given format. Note that the encoder may be limited to a certain subset of features (e.g. only 8 bit, only lossy). You will have to query the specific capabilities further.
- This function is only for decoder plugin implementors.
- The indicated bit_depth corresponds to the bit depth per channel. I.e. for interleaved formats like RRGGBB, the bit_depth would be, e.g., 10 bit instead of 30 bits or 3*16=48 bits. For backward compatibility, one can also specify 24bits for RGB and 32bits for RGBA, instead of the preferred 8 bits.
- Create a new image of the specified resolution and colorspace. Note: no memory for the actual image data is reserved yet. You have to use heif_image_add_plane() to add the image planes required by your colorspace/chroma.
- This function extends the padding of the image so that it has at least the given physical size. The padding border is filled with the pixels along the right/bottom border. This function may be useful if you want to process the image, but have some external padding requirements. The image size will not be modified if it is already larger/equal than the given physical size. I.e. you cannot assume that after calling this function, the stride will be equal to min_physical_width.
- Get the number of bits per pixel in the given image channel. Returns -1 if a non-existing channel was given. Note that the number of bits per pixel may be different for each color channel. This function returns the number of bits used for storage of each pixel. Especially for HDR images, this is probably not what you want. Have a look at heif_image_get_bits_per_pixel_range() instead.
- Get the number of bits per pixel in the given image channel. This function returns the number of bits used for representing the pixel value, which might be smaller than the number of bits used in memory. For example, in 12bit HDR images, this function returns ‘12’, while still 16 bits are reserved for storage. For interleaved RGBA with 12 bit, this function also returns ‘12’, not ‘48’ or ‘64’ (heif_image_get_bits_per_pixel returns 64 in this case).
- Get the chroma format of the image.
- Get the colorspace format of the image.
- Fills the image decoding warnings into the provided ‘out_warnings’ array. The size of the array has to be provided in max_output_buffer_entries. If max_output_buffer_entries==0, the number of decoder warnings is returned. The function fills the warnings into the provided buffer, starting with ‘first_warning_idx’. It returns the number of warnings filled into the buffer. Note: you can iterate through all warnings by using ‘max_output_buffer_entries=1’ and iterate ‘first_warning_idx’.
- Get the height of a specified image channel.
- Get a pointer to the actual pixel data. The ‘out_stride’ is returned as “bytes per line”. When out_stride is NULL, no value will be written. Returns NULL if a non-existing channel was given.
- Get the height of the main channel.
- Get the width of the main channel.
- Get the width of a specified image channel.
- DEPRECATED (because typo in function name). Use heif_image_handle_release_auxiliary_type() instead.
- Get the image handle of an auxiliary image.
- You are responsible to deallocate the returned buffer with heif_image_handle_release_auxiliary_type().
- Returns -1 on error, e.g. if this information is not present in the image.
- Returns ‘heif_color_profile_type_not_present’ if there is no color profile. If there is an ICC profile and an NCLX profile, the ICC profile is returned. TODO: we need a new API for this function as images can contain both NCLX and ICC at the same time. However, you can still use heif_image_handle_get_raw_color_profile() and heif_image_handle_get_nclx_color_profile() to access both profiles.
- This gets the context associated with the image handle. Note that you have to release the returned context with heif_context_free() in any case.
- Returns true when there is depth_representation_info available Note 1: depth_image_id is currently unused because we support only one depth channel per image, but you should still provide the correct ID for future compatibility. Note 2: Because of an API bug before v1.11.0, the function also works when ‘handle’ is the handle of the depth image. However, you should pass the handle of the main image. Please adapt your code if needed.
- Get the image width from the ‘ispe’ box. This is the original image size without any transformations applied to it. Do not use this unless you know exactly what you are doing.
- ‘type_filter’ can be used to get only metadata of specific types, like “Exif”. If ‘type_filter’ is NULL, it will return all types of metadata IDs.
- Returns -1 on error, e.g. if this information is not present in the image.
- ‘out_data’ must point to a memory area of the size reported by heif_image_handle_get_metadata_size(). The data is returned exactly as stored in the HEIF file. For Exif data, you probably have to skip the first four bytes of the data, since they indicate the offset to the start of the TIFF header of the Exif data.
- For EXIF, the content type is empty. For XMP, the content type is “application/rdf+xml”.
- Only valid for item type == “uri “, an absolute URI
- Get the size of the raw metadata, as stored in the HEIF file.
- Return a string indicating the type of the metadata, as specified in the HEIF file. Exif data will have the type string “Exif”. This string will be valid until the next call to a libheif function. You do not have to free this string.
- Returns ‘heif_error_Color_profile_does_not_exist’ when there is no NCLX profile. TODO: This function does currently not return an NCLX profile if it is stored in the image bitstream. Only NCLX profiles stored as colr boxes are returned. This may change in the future.
- List the number of auxiliary images assigned to this image handle.
- How many metadata blocks are attached to an image. If you only want to get EXIF data, set the type_filter to “Exif”. Otherwise, set the type_filter to NULL.
- List the number of thumbnails assigned to this image handle. Usually 0 or 1.
- Return the colorspace that libheif proposes to use for decoding. Usually, these will be either YCbCr or Monochrome, but it may also propose RGB for images encoded with matrix_coefficients=0. It may also return *_undefined if the file misses relevant information to determine this without decoding.
- Returns ‘heif_error_Color_profile_does_not_exist’ when there is no ICC profile.
- Get the image handle of a thumbnail image.
- Get the resolution of an image.
- ———————–– depth images ———————––
- Check whether the given image_handle is the primary image of the file.
- Release image handle.
- Release heif_image.
- Currently, heif_scaling_options is not defined yet. Pass a NULL pointer.
- Signal that the image is premultiplied by the alpha pixel values.
- The color profile is not attached to the image handle because we might need it for color space transform and encoding.
- Initialise library.
- Returns an array of compatible brands. The array is allocated by this function and has to be freed with ‘heif_free_list_of_compatible_brands()’. The number of entries is returned in out_size.
- input data should be at least 12 bytes DEPRECATED, use heif_read_main_brand() instead
- Converts the internal numeric representation of heif_mastering_display_colour_volume to the normalized values, collected in heif_decoded_mastering_display_colour_volume. Values that are out-of-range are decoded to 0, indicating an undefined value (as specified in ISO/IEC 23008-2).
- Returned color profile has ‘version’ field set to the maximum allowed. Do not fill values for higher versions as these might be outside the allocated structure size. May return NULL.
- input data should be at least 12 bytes
- DEPRECATED. Use heif_register_decoder_plugin(const struct heif_decoder_plugin*) instead.
Type Aliases§
- DEPRECATED, use heif_brand2 and the heif_brand2_* constants below instead
- ———————–– color profiles ———————––
- libheif known compression formats.
- ========================= file type check ======================
- The orientation values are defined equal to the EXIF Orientation tag.
- — Plugins are currently only supported on Unix platforms.
- Planar RGB images are specified as heif_colorspace_RGB / heif_chroma_444.