/// <summary>
        /// Decodes this instance to a <see cref="HeifImage"/>.
        /// </summary>
        /// <param name="colorspace">The destination image color space.</param>
        /// <param name="chroma">The chroma.</param>
        /// <param name="options">The decoding options.</param>
        /// <returns>The decoded image.</returns>
        /// <exception cref="HeifException">
        /// A LibHeif error occurred.
        ///
        /// -or-
        ///
        /// The color profile type is not supported.
        /// </exception>
        /// <exception cref="ObjectDisposedException">The object has been disposed.</exception>
        public HeifImage Decode(HeifColorspace colorspace, HeifChroma chroma, HeifDecodingOptions options = null)
        {
            VerifyNotDisposed();

            HeifImage     image         = null;
            SafeHeifImage safeHeifImage = null;

            var imageHandleColorProfile = GetImageHandleColorProfile();

            try
            {
                heif_error error;

                if (options != null)
                {
                    using (var safeHeifDecodingOptions = options.CreateDecodingOptions())
                    {
                        error = LibHeifNative.heif_decode_image(this.imageHandle,
                                                                out safeHeifImage,
                                                                colorspace,
                                                                chroma,
                                                                safeHeifDecodingOptions);
                    }
                }
                else
                {
                    error = LibHeifNative.heif_decode_image(this.imageHandle,
                                                            out safeHeifImage,
                                                            colorspace,
                                                            chroma,
                                                            IntPtr.Zero);
                }

                if (error.IsError)
                {
                    if (this.decodeErrorHandler != null)
                    {
                        this.decodeErrorHandler.Invoke(error);
                    }
                    else
                    {
                        error.ThrowIfError();
                    }
                }

                // Passing the image handle width and height works around a bug with the
                // heif_image_get_primary_height method in some versions of libheif.
                image = new HeifImage(safeHeifImage,
                                      this.Width,
                                      this.Height,
                                      imageHandleColorProfile);
                safeHeifImage = null;
            }
            finally
            {
                safeHeifImage?.Dispose();
            }

            return(image);
        }
Esempio n. 2
0
        /// <summary>
        /// Scales the image.
        /// </summary>
        /// <param name="newWidth">The new width.</param>
        /// <param name="newHeight">The new height.</param>
        /// <returns>The scaled image.</returns>
        /// <exception cref="HeifException">A LibHeif error occurred.</exception>
        /// <exception cref="ObjectDisposedException">The object has been disposed.</exception>
        public HeifImage ScaleImage(int newWidth, int newHeight)
        {
            VerifyNotDisposed();

            HeifImage     scaledImage   = null;
            SafeHeifImage safeHeifImage = null;

            try
            {
                var error = LibHeifNative.heif_image_scale_image(this.image, out safeHeifImage, newWidth, newHeight, IntPtr.Zero);
                error.ThrowIfError();

                scaledImage   = new HeifImage(safeHeifImage, newWidth, newHeight, this.cachedImageColorProfile);
                safeHeifImage = null;
            }
            finally
            {
                safeHeifImage?.Dispose();
            }

            return(scaledImage);
        }
Esempio n. 3
0
        /// <summary>
        /// Scales the image.
        /// </summary>
        /// <param name="newWidth">The new width.</param>
        /// <param name="newHeight">The new height.</param>
        /// <returns>The scaled image.</returns>
        /// <exception cref="HeifException">A LibHeif error occurred.</exception>
        /// <exception cref="ObjectDisposedException">The object has been disposed.</exception>
        public HeifImage ScaleImage(int newWidth, int newHeight)
        {
            VerifyNotDisposed();

            if (!this.fetchedColorProfilesFromImage)
            {
                lock (this.sync)
                {
                    if (!this.fetchedColorProfilesFromImage)
                    {
                        UpdateCachedColorProfilesWhileLocked();
                        this.fetchedColorProfilesFromImage = true;
                    }
                }
            }

            HeifImage     scaledImage   = null;
            SafeHeifImage safeHeifImage = null;

            try
            {
                var error = LibHeifNative.heif_image_scale_image(this.image, out safeHeifImage, newWidth, newHeight, IntPtr.Zero);
                error.ThrowIfError();

                scaledImage = new HeifImage(safeHeifImage,
                                            newWidth,
                                            newHeight,
                                            this.cachedIccColorProfile,
                                            this.cachedNclxColorProfile);
                safeHeifImage = null;
            }
            finally
            {
                safeHeifImage?.Dispose();
            }

            return(scaledImage);
        }