コード例 #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HeifContext"/> class.
        /// </summary>
        /// <exception cref="HeifException">
        /// Unable to create the native HeifContext.
        ///
        /// -or-
        ///
        /// The LibHeif version is not supported.
        /// </exception>
        public HeifContext()
        {
            LibHeifVersion.ThrowIfNotSupported();

            this.context = CreateNativeContext();
            this.reader  = null;
        }
コード例 #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HeifContext"/> class with the specified stream to read from, and optionally leaves the stream open.
        /// </summary>
        /// <param name="stream">The stream to read from.</param>
        /// <param name="leaveOpen">
        /// <see langword="true"/> to leave the stream open after the <see cref="HeifContext"/> object is disposed; otherwise, <see langword="false"/>.
        /// </param>
        /// <exception cref="ArgumentNullException"><paramref name="stream"/> is null.</exception>
        /// <exception cref="ArgumentException"><paramref name="stream"/> must support reading and seeking.</exception>
        /// <exception cref="HeifException">
        /// Unable to create the native HeifContext.
        ///
        /// -or-
        ///
        /// The LibHeif version is not supported.
        ///
        /// -or-
        ///
        /// A LibHeif error occurred.
        /// </exception>
        public HeifContext(Stream stream, bool leaveOpen = false)
        {
            Validate.IsNotNull(stream, nameof(stream));
            LibHeifVersion.ThrowIfNotSupported();

            if (!stream.CanRead || !stream.CanSeek)
            {
                ExceptionUtil.ThrowArgumentException(Resources.StreamCannotReadAndSeek);
            }

            this.context = CreateNativeContext();
            try
            {
                this.reader = HeifReaderFactory.CreateFromStream(stream, !leaveOpen);
                InitializeContextFromReader();
            }
            catch (Exception ex)
            {
                this.context.Dispose();
                this.reader?.Dispose();

                if (ex is ArgumentException || ex is HeifException)
                {
                    throw;
                }
                else
                {
                    throw new HeifException(ex.Message, ex);
                }
            }
        }
コード例 #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HeifContext"/> class, with the specified file to read from.
        /// </summary>
        /// <param name="path">The file to read from.</param>
        /// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
        /// <exception cref="ArgumentException">
        /// <paramref name="path"/> is empty, contains only whitespace or contains invalid characters.
        /// </exception>
        /// <exception cref="FileNotFoundException">The file specified by <paramref name="path"/> does not exist.</exception>
        /// <exception cref="HeifException">
        /// Unable to create the native HeifContext.
        ///
        /// -or-
        ///
        /// The LibHeif version is not supported.
        ///
        /// -or-
        ///
        /// A LibHeif error occurred.
        /// </exception>
        /// <exception cref="IOException">An I/O error occurred.</exception>
        /// <exception cref="System.Security.SecurityException">The caller does not have the required permission.</exception>
        /// <exception cref="UnauthorizedAccessException">
        /// The access requested is not permitted by the operating system for the specified path.
        /// </exception>
        public HeifContext(string path)
        {
            Validate.IsNotNull(path, nameof(path));
            LibHeifVersion.ThrowIfNotSupported();

            this.context = CreateNativeContext();
            try
            {
                this.reader = HeifReaderFactory.CreateFromFile(path);
                InitializeContextFromReader();
            }
            catch
            {
                this.context.Dispose();
                this.reader?.Dispose();
                throw;
            }
        }
コード例 #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HeifContext"/> class, with the specified byte array to read from.
        /// </summary>
        /// <param name="bytes">A byte array that contains the HEIF image.</param>
        /// <exception cref="ArgumentNullException"><paramref name="bytes"/> is null.</exception>
        /// <exception cref="ArgumentException"><paramref name="bytes"/> is an empty array.</exception>
        /// <exception cref="HeifException">
        /// Unable to create the native HeifContext.
        ///
        /// -or-
        ///
        /// The LibHeif version is not supported.
        ///
        /// -or-
        ///
        /// A LibHeif error occurred.
        /// </exception>
        public HeifContext(byte[] bytes)
        {
            Validate.IsNotNullOrEmptyArray(bytes, nameof(bytes));
            LibHeifVersion.ThrowIfNotSupported();

            this.context = CreateNativeContext();
            try
            {
                this.reader = HeifReaderFactory.CreateFromMemory(bytes);
                InitializeContextFromReader();
            }
            catch
            {
                this.context.Dispose();
                this.reader?.Dispose();
                throw;
            }
        }
コード例 #5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HeifContext"/> class, with the specified file to read from.
        /// </summary>
        /// <param name="path">The file to read from.</param>
        /// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
        /// <exception cref="ArgumentException">
        /// <paramref name="path"/> is empty, contains only whitespace or contains invalid characters.
        /// </exception>
        /// <exception cref="FileNotFoundException">The file specified by <paramref name="path"/> does not exist.</exception>
        /// <exception cref="HeifException">
        /// Unable to create the native HeifContext.
        ///
        /// -or-
        ///
        /// The LibHeif version is not supported.
        ///
        /// -or-
        ///
        /// A LibHeif error occurred.
        /// </exception>
        /// <exception cref="IOException">An I/O error occurred.</exception>
        /// <exception cref="System.Security.SecurityException">The caller does not have the required permission.</exception>
        /// <exception cref="UnauthorizedAccessException">
        /// The access requested is not permitted by the operating system for the specified path.
        /// </exception>
        public HeifContext(string path)
        {
            Validate.IsNotNull(path, nameof(path));
            LibHeifVersion.ThrowIfNotSupported();

            this.context = CreateNativeContext();
            try
            {
                this.readerStreamIO = HeifStreamFactory.CreateFromFile(path, FileMode.Open, FileAccess.Read, FileShare.Read);
                InitializeContextFromReader();
            }
            catch
            {
                this.context.Dispose();
                this.readerStreamIO?.Dispose();
                throw;
            }
        }
コード例 #6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HeifImage"/> class.
        /// </summary>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="colorspace">The color space.</param>
        /// <param name="chroma">The chroma.</param>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="width"/> is less than or equal to zero.
        ///
        /// -or-
        ///
        /// <paramref name="height"/> is less than or equal to zero.
        /// </exception>
        /// <exception cref="HeifException">
        /// A LibHeif error occurred.
        ///
        /// -or-
        ///
        /// The LibHeif version is not supported.
        /// </exception>
        public HeifImage(int width, int height, HeifColorspace colorspace, HeifChroma chroma)
        {
            Validate.IsPositive(width, nameof(width));
            Validate.IsPositive(height, nameof(height));

            LibHeifVersion.ThrowIfNotSupported();

            var error = LibHeifNative.heif_image_create(width,
                                                        height,
                                                        colorspace,
                                                        chroma,
                                                        out this.image);

            error.ThrowIfError();
            // The caller can set a color profile after the image has been created.
            this.cachedImageColorProfile      = null;
            this.fetchedColorProfileFromImage = true;
            this.sync       = new object();
            this.Width      = width;
            this.Height     = height;
            this.Colorspace = colorspace;
            this.Chroma     = chroma;
        }
コード例 #7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HeifContext"/> class with the specified stream to read from, and optionally leaves the stream open.
        /// </summary>
        /// <param name="stream">The stream to read from.</param>
        /// <param name="leaveOpen">
        /// <see langword="true"/> to leave the stream open after the <see cref="HeifContext"/> object is disposed; otherwise, <see langword="false"/>.
        /// </param>
        /// <exception cref="ArgumentNullException"><paramref name="stream"/> is null.</exception>
        /// <exception cref="ArgumentException"><paramref name="stream"/> must support reading and seeking.</exception>
        /// <exception cref="HeifException">
        /// Unable to create the native HeifContext.
        ///
        /// -or-
        ///
        /// The LibHeif version is not supported.
        ///
        /// -or-
        ///
        /// A LibHeif error occurred.
        /// </exception>
        public HeifContext(Stream stream, bool leaveOpen = false)
        {
            Validate.IsNotNull(stream, nameof(stream));
            LibHeifVersion.ThrowIfNotSupported();

            if (!stream.CanRead || !stream.CanSeek)
            {
                ExceptionUtil.ThrowArgumentException(Resources.StreamCannotReadAndSeek);
            }

            this.context = CreateNativeContext();
            try
            {
                this.readerStreamIO = new HeifStreamIO(stream, !leaveOpen);
                InitializeContextFromReader();
            }
            catch
            {
                this.context.Dispose();
                this.readerStreamIO?.Dispose();
                throw;
            }
        }