/// <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); } }