Exemplo n.º 1
0
        /// <summary>
        /// Encodes the image as webp to the specified stream.
        /// </summary>
        /// <typeparam name="TPixel">The pixel format.</typeparam>
        /// <param name="image">The <see cref="ImageFrame{TPixel}"/> to encode from.</param>
        /// <param name="stream">The <see cref="Stream"/> to encode the image data to.</param>
        /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
        public void Encode <TPixel>(Image <TPixel> image, Stream stream, CancellationToken cancellationToken)
            where TPixel : unmanaged, IPixel <TPixel>
        {
            Guard.NotNull(image, nameof(image));
            Guard.NotNull(stream, nameof(stream));

            this.configuration = image.GetConfiguration();
            bool lossless;

            if (this.fileFormat is not null)
            {
                lossless = this.fileFormat == WebpFileFormatType.Lossless;
            }
            else
            {
                WebpMetadata webpMetadata = image.Metadata.GetWebpMetadata();
                lossless = webpMetadata.FileFormat == WebpFileFormatType.Lossless;
            }

            if (lossless)
            {
                using var enc = new Vp8LEncoder(
                          this.memoryAllocator,
                          this.configuration,
                          image.Width,
                          image.Height,
                          this.quality,
                          this.method,
                          this.transparentColorMode,
                          this.nearLossless,
                          this.nearLosslessQuality);
                enc.Encode(image, stream);
            }
            else
            {
                using var enc = new Vp8Encoder(
                          this.memoryAllocator,
                          this.configuration,
                          image.Width,
                          image.Height,
                          this.quality,
                          this.method,
                          this.entropyPasses,
                          this.filterStrength,
                          this.spatialNoiseShaping,
                          this.alphaCompression);
                enc.Encode(image, stream);
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Vp8BitWriter"/> class.
 /// </summary>
 /// <param name="expectedSize">The expected size in bytes.</param>
 /// <param name="enc">The Vp8Encoder.</param>
 public Vp8BitWriter(int expectedSize, Vp8Encoder enc)
     : this(expectedSize) => this.enc = enc;