Exemple #1
0
        /// <summary>
        /// Encodes a Graphics Interchange Format (GIF) image from multiple raw image buffers to a specified <see cref="Stream"/>.
        /// </summary>
        /// <param name="frames">The image frames to encode.</param>
        /// <param name="outStream">The stream that the image is encoded to.</param>
        /// <returns>A task that represents the asynchronous encoding operation.</returns>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="frames"/> is null.<br/>
        ///     -or-<br/>
        ///     <paramref name="outStream"/> is null.
        /// </exception>
        /// <exception cref="ArgumentException">
        ///     <paramref name="frames"/> has no element(empty).<br/>
        ///     -or-<br/>
        ///     <paramref name="outStream"/> is not writable.
        /// </exception>
        /// <exception cref="InvalidOperationException">The resolution is not set.</exception>
        /// <exception cref="ObjectDisposedException">The <see cref="ImageEncoder"/> has already been disposed of.</exception>
        /// <seealso cref="ImageEncoder.SetResolution"/>
        /// <since_tizen> 4 </since_tizen>
        public Task EncodeAsync(IEnumerable <GifFrame> frames, Stream outStream)
        {
            if (frames == null)
            {
                throw new ArgumentNullException(nameof(frames));
            }

            if (frames.Count() == 0)
            {
                throw new ArgumentException("frames is a empty collection", nameof(frames));
            }

            return(EncodeAsync(handle =>
            {
                foreach (GifFrame frame in frames)
                {
                    if (frame == null)
                    {
                        throw new ArgumentNullException(nameof(frames));
                    }
                    Unmanaged.SetInputBuffer(handle, frame.Buffer).
                    ThrowIfFailed("Failed to configure encoder; Buffer");

                    Unmanaged.SetGifFrameDelayTime(handle, (ulong)frame.Delay).
                    ThrowIfFailed("Failed to configure encoder; Delay");
                }
            }, outStream));
        }
Exemple #2
0
 internal override void Configure(ImageEncoderHandle handle)
 {
     if (_quality.HasValue)
     {
         Unmanaged.SetQuality(handle, _quality.Value).
         ThrowIfFailed("Failed to configure encoder; Quality");
     }
 }
Exemple #3
0
        internal ImageEncoder(ImageFormat format)
        {
            Unmanaged.Create(format, out _handle).ThrowIfFailed("Failed to create ImageEncoder");

            Debug.Assert(_handle != null);

            OutputFormat = format;
        }
Exemple #4
0
 internal override void Configure(ImageEncoderHandle handle)
 {
     if (_compression.HasValue)
     {
         Unmanaged.SetPngCompression(handle, _compression.Value).
         ThrowIfFailed("Failed to configure encoder; PngCompression");
     }
 }
Exemple #5
0
        /// <summary>
        /// Sets the color-space of the output image.
        /// </summary>
        /// <param name="colorSpace">The target color-space.</param>
        /// <exception cref="ArgumentException"><paramref name="colorSpace"/> is invalid.</exception>
        /// <exception cref="NotSupportedException"><paramref name="colorSpace"/> is not supported by the encoder.</exception>
        /// <seealso cref="ImageUtil.GetSupportedColorSpaces(ImageFormat)"/>
        /// <since_tizen> 4 </since_tizen>
        public void SetColorSpace(ColorSpace colorSpace)
        {
            ValidationUtil.ValidateEnum(typeof(ColorSpace), colorSpace, nameof(colorSpace));

            if (ImageUtil.GetSupportedColorSpaces(OutputFormat).Contains(colorSpace) == false)
            {
                throw new NotSupportedException($"{colorSpace.ToString()} is not supported for {OutputFormat}.");
            }

            Unmanaged.SetColorspace(Handle, colorSpace.ToImageColorSpace()).
            ThrowIfFailed("Failed to set the color space");
        }
Exemple #6
0
        /// <summary>
        /// Sets the resolution of the output image.
        /// </summary>
        /// <param name="resolution">The target resolution.</param>
        /// <exception cref="ArgumentOutOfRangeException">
        ///     The width of <paramref name="resolution"/> is less than or equal to zero.<br/>
        ///     -or-<br/>
        ///     The height of <paramref name="resolution"/> is less than or equal to zero.
        /// </exception>
        /// <since_tizen> 4 </since_tizen>
        public void SetResolution(Size resolution)
        {
            if (resolution.Width <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(resolution), resolution.Width,
                                                      "The width of resolution can't be less than or equal to zero.");
            }
            if (resolution.Height <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(resolution), resolution.Height,
                                                      "The height of resolution can't be less than or equal to zero.");
            }

            Unmanaged.SetResolution(Handle, (uint)resolution.Width, (uint)resolution.Height).
            ThrowIfFailed("Failed to set the resolution");

            _hasResolution = true;
        }
Exemple #7
0
        /// <summary>
        /// Encodes an image from a raw image buffer to a specified <see cref="Stream"/>.
        /// </summary>
        /// <param name="inputBuffer">The image buffer to encode.</param>
        /// <param name="outStream">The stream that the image is encoded to.</param>
        /// <returns>A task that represents the asynchronous encoding operation.</returns>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="inputBuffer"/> is null.<br/>
        ///     -or-<br/>
        ///     <paramref name="outStream"/> is null.
        /// </exception>
        /// <exception cref="ArgumentException">
        ///     <paramref name="inputBuffer"/> is an empty array.<br/>
        ///     -or-<br/>
        ///     <paramref name="outStream"/> is not writable.
        /// </exception>
        /// <exception cref="InvalidOperationException">The resolution is not set.</exception>
        /// <exception cref="ObjectDisposedException">The <see cref="ImageEncoder"/> has already been disposed of.</exception>
        /// <seealso cref="SetResolution"/>
        /// <since_tizen> 4 </since_tizen>
        public Task EncodeAsync(byte[] inputBuffer, Stream outStream)
        {
            if (inputBuffer == null)
            {
                throw new ArgumentNullException(nameof(inputBuffer));
            }

            if (inputBuffer.Length == 0)
            {
                throw new ArgumentException("buffer is empty.", nameof(inputBuffer));
            }

            return(EncodeAsync(handle =>
            {
                Unmanaged.SetInputBuffer(handle, inputBuffer).
                ThrowIfFailed("Failed to configure encoder; InputBuffer");
            }, outStream));
        }
Exemple #8
0
        private void RunEncoding(object outStream)
        {
            IntPtr outBuffer = IntPtr.Zero;

            try
            {
                Unmanaged.SetOutputBuffer(Handle, out outBuffer).ThrowIfFailed("Failed to initialize encoder");

                Unmanaged.Run(Handle, out var size).ThrowIfFailed("Failed to encode given image");

                byte[] buf = new byte[size];
                Marshal.Copy(outBuffer, buf, 0, (int)size);
                (outStream as Stream).Write(buf, 0, (int)size);
            }
            finally
            {
                Interop.Libc.Free(outBuffer);
            }
        }