/// <summary>
        /// Gets a list of the thumbnail image ids.
        /// </summary>
        /// <returns>A list of the thumbnail image ids.</returns>
        /// <exception cref="HeifException">Could not get all of the thumbnail image ids.</exception>
        /// <exception cref="ObjectDisposedException">The object has been disposed.</exception>
        public IReadOnlyList <HeifItemId> GetThumbnailImageIds()
        {
            VerifyNotDisposed();

            int count = LibHeifNative.heif_image_handle_get_number_of_thumbnails(this.imageHandle);

            if (count == 0)
            {
                return(Array.Empty <HeifItemId>());
            }

            var ids = new HeifItemId[count];

            unsafe
            {
                fixed(HeifItemId *ptr = ids)
                {
                    int filledCount = LibHeifNative.heif_image_handle_get_list_of_thumbnail_IDs(this.imageHandle,
                                                                                                ptr,
                                                                                                count);

                    if (filledCount != count)
                    {
                        ExceptionUtil.ThrowHeifException(Resources.CannotGetAllThumbnailIds);
                    }
                }
            }

            return(ids);
        }
        /// <summary>
        /// Gets the meta-data bytes.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <returns>The meta-data bytes.</returns>
        /// <exception cref="HeifException">
        /// A LibHeif error occurred.
        ///
        /// -or-
        ///
        /// The meta-data block is larger than 2 GB.
        /// </exception>
        /// <exception cref="ObjectDisposedException">The object has been disposed.</exception>
        public byte[] GetMetadata(HeifItemId id)
        {
            VerifyNotDisposed();

            ulong size = LibHeifNative.heif_image_handle_get_metadata_size(this.imageHandle, id).ToUInt64();

            if (size == 0)
            {
                return(null);
            }

            if (size > int.MaxValue)
            {
                ExceptionUtil.ThrowHeifException(Resources.MetadataBlockLargerThan2Gb);
            }

            byte[] metadata = new byte[size];

            unsafe
            {
                fixed(byte *ptr = metadata)
                {
                    var error = LibHeifNative.heif_image_handle_get_metadata(this.imageHandle, id, ptr);

                    error.ThrowIfError();
                }
            }

            return(metadata);
        }
        /// <summary>
        /// Gets the depth representation information.
        /// </summary>
        /// <param name="id">The depth image id.</param>
        /// <returns>The depth representation information.</returns>
        /// <exception cref="HeifException">A LibHeif error occurred.</exception>
        /// <exception cref="ObjectDisposedException">The object has been disposed.</exception>
        public HeifDepthRepresentationInfo GetDepthRepresentationInfo(HeifItemId id)
        {
            VerifyNotDisposed();

            HeifDepthRepresentationInfo depthRepresentationInfo     = null;
            SafeDepthRepresentationInfo safeDepthRepresentationInfo = null;

            try
            {
                if (LibHeifNative.heif_image_handle_get_depth_image_representation_info(this.imageHandle,
                                                                                        id,
                                                                                        out safeDepthRepresentationInfo))
                {
                    unsafe
                    {
                        var info = (heif_depth_representation_info *)safeDepthRepresentationInfo.DangerousGetHandle();

                        depthRepresentationInfo = new HeifDepthRepresentationInfo(info);
                    }
                }
            }
            finally
            {
                safeDepthRepresentationInfo?.Dispose();
            }

            return(depthRepresentationInfo);
        }
        /// <summary>
        /// Gets the auxiliary image handle.
        /// </summary>
        /// <param name="id">The auxiliary image id.</param>
        /// <returns>The auxiliary image handle.</returns>
        /// <exception cref="HeifException">A LibHeif error occurred.</exception>
        /// <exception cref="ObjectDisposedException">The object has been disposed.</exception>
        /// <seealso cref="GetAuxiliaryImageIds"/>
        public HeifImageHandle GetAuxiliaryImage(HeifItemId id)
        {
            VerifyNotDisposed();

            if (LibHeifVersion.Is1Point11OrLater)
            {
                HeifImageHandle     aux           = null;
                SafeHeifImageHandle auxSafeHandle = null;

                try
                {
                    var error = LibHeifNative.heif_image_handle_get_auxiliary_image_handle(this.imageHandle, id, out auxSafeHandle);
                    error.ThrowIfError();

                    aux           = new HeifImageHandle(auxSafeHandle, this.decodeErrorHandler, AuxiliaryImageType.VendorSpecific);
                    auxSafeHandle = null;
                }
                finally
                {
                    auxSafeHandle?.Dispose();
                }

                return(aux);
            }
            else
            {
                throw new HeifException(Resources.AuxiliaryImageAPINotSupported);
            }
        }
        /// <summary>
        /// Gets a list of the meta-data block ids.
        /// </summary>
        /// <param name="type">The meta-data block type.</param>
        /// <param name="contentType">The meta-data block content type.</param>
        /// <returns>A list of the meta-data block ids.</returns>
        /// <exception cref="HeifException">Could not get all of the meta-data block ids.</exception>
        /// <exception cref="ObjectDisposedException">The object has been disposed.</exception>
        public IReadOnlyList <HeifItemId> GetMetadataBlockIds(string type = null, string contentType = null)
        {
            VerifyNotDisposed();

            int count = LibHeifNative.heif_image_handle_get_number_of_metadata_blocks(this.imageHandle, type);

            if (count == 0)
            {
                return(Array.Empty <HeifItemId>());
            }

            var ids = new HeifItemId[count];

            unsafe
            {
                fixed(HeifItemId *ptr = ids)
                {
                    int filledCount = LibHeifNative.heif_image_handle_get_list_of_metadata_block_IDs(this.imageHandle,
                                                                                                     type,
                                                                                                     ptr,
                                                                                                     count);

                    if (filledCount != count)
                    {
                        ExceptionUtil.ThrowHeifException(Resources.CannotGetAllMetadataBlockIds);
                    }
                }
            }

            // The type must be defined in order to filter by content type.
            if (type != null && contentType != null)
            {
                var matchingItems = new List <HeifItemId>();

                for (int i = 0; i < ids.Length; i++)
                {
                    var id = ids[i];
                    var metadataContentType = LibHeifNative.heif_image_handle_get_metadata_content_type(this.imageHandle, id);

                    if (contentType.Equals(metadataContentType.GetStringValue(), StringComparison.Ordinal))
                    {
                        matchingItems.Add(id);
                    }
                }

                return(matchingItems);
            }
            else
            {
                return(ids);
            }
        }
        /// <summary>
        /// Gets a list of the auxiliary image ids.
        /// </summary>
        /// <returns>A list of the auxiliary image ids.</returns>
        /// <exception cref="HeifException">A LibHeif error occurred.</exception>
        /// <exception cref="ObjectDisposedException">The object has been disposed.</exception>
        /// <remarks>
        /// <para>The alpha and/or depth images are omitted from this list.</para>
        /// <para>
        /// LibHeif will include the alpha image (if present) when you call <see cref="Decode(HeifColorspace, HeifChroma, HeifDecodingOptions)"/>
        /// with the <see cref="HeifChroma"/> parameter set to one of the <c>InterleavedRgba</c> values.
        /// </para>
        /// <para>
        /// To read the depth images use <see cref="GetDepthImageIds"/> to get a list of the depth image item ids and
        /// <see cref="GetDepthImage(HeifItemId)"/> to convert the item id to a <see cref="HeifImageHandle"/>.
        /// </para>
        /// </remarks>
        /// <seealso cref="GetAuxiliaryImage(HeifItemId)"/>
        /// <seealso cref="GetAuxiliaryType"/>
        public IReadOnlyList <HeifItemId> GetAuxiliaryImageIds()
        {
            VerifyNotDisposed();

            if (LibHeifVersion.Is1Point11OrLater)
            {
                const heif_auxiliary_image_filter filter = heif_auxiliary_image_filter.OmitAlpha | heif_auxiliary_image_filter.OmitDepth;

                int count = LibHeifNative.heif_image_handle_get_number_of_auxiliary_images(this.imageHandle, filter);

                if (count == 0)
                {
                    return(Array.Empty <HeifItemId>());
                }

                var ids = new HeifItemId[count];

                unsafe
                {
                    fixed(HeifItemId *ptr = ids)
                    {
                        int filledCount = LibHeifNative.heif_image_handle_get_list_of_auxiliary_image_IDs(this.imageHandle,
                                                                                                          filter,
                                                                                                          ptr,
                                                                                                          count);

                        if (filledCount != count)
                        {
                            ExceptionUtil.ThrowHeifException(Resources.CannotGetAllAuxillaryImages);
                        }
                    }
                }

                return(ids);
            }
            else
            {
                return(Array.Empty <HeifItemId>());
            }
        }
        /// <summary>
        /// Gets the thumbnail image handle.
        /// </summary>
        /// <param name="id">The thumbnail image id.</param>
        /// <returns>The thumbnail image handle.</returns>
        /// <exception cref="HeifException">A LibHeif error occurred.</exception>
        /// <exception cref="ObjectDisposedException">The object has been disposed.</exception>
        public HeifImageHandle GetThumbnailImage(HeifItemId id)
        {
            VerifyNotDisposed();

            HeifImageHandle     thumbnail           = null;
            SafeHeifImageHandle thumbnailSafeHandle = null;

            try
            {
                var error = LibHeifNative.heif_image_handle_get_thumbnail(this.imageHandle, id, out thumbnailSafeHandle);
                error.ThrowIfError();

                thumbnail           = new HeifImageHandle(thumbnailSafeHandle, this.decodeErrorHandler);
                thumbnailSafeHandle = null;
            }
            finally
            {
                thumbnailSafeHandle?.Dispose();
            }

            return(thumbnail);
        }
        /// <summary>
        /// Gets the depth images.
        /// </summary>
        /// <param name="id">The depth image id.</param>
        /// <returns>The meta-data bytes.</returns>
        /// <exception cref="HeifException">A LibHeif error occurred.</exception>
        /// <exception cref="ObjectDisposedException">The object has been disposed.</exception>
        public HeifImageHandle GetDepthImage(HeifItemId id)
        {
            VerifyNotDisposed();

            HeifImageHandle     depth           = null;
            SafeHeifImageHandle depthSafeHandle = null;

            try
            {
                var error = LibHeifNative.heif_image_handle_get_depth_image_handle(this.imageHandle, id, out depthSafeHandle);
                error.ThrowIfError();

                depth           = new HeifImageHandle(depthSafeHandle, this.decodeErrorHandler);
                depthSafeHandle = null;
            }
            finally
            {
                depthSafeHandle?.Dispose();
            }

            return(depth);
        }