/// <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); } }
/// <summary> /// Encodes the alpha channel data. /// Data is either compressed as lossless webp image or uncompressed. /// </summary> /// <typeparam name="TPixel">The pixel format.</typeparam> /// <param name="image">The <see cref="ImageFrame{TPixel}"/> to encode from.</param> /// <param name="configuration">The global configuration.</param> /// <param name="memoryAllocator">The memory manager.</param> /// <param name="compress">Indicates, if the data should be compressed with the lossless webp compression.</param> /// <param name="size">The size in bytes of the alpha data.</param> /// <returns>The encoded alpha data.</returns> public IMemoryOwner <byte> EncodeAlpha <TPixel>(Image <TPixel> image, Configuration configuration, MemoryAllocator memoryAllocator, bool compress, out int size) where TPixel : unmanaged, IPixel <TPixel> { int width = image.Width; int height = image.Height; this.alphaData = ExtractAlphaChannel(image, configuration, memoryAllocator); if (compress) { WebpEncodingMethod effort = WebpEncodingMethod.Default; int quality = 8 * (int)effort; using var lossLessEncoder = new Vp8LEncoder( memoryAllocator, configuration, width, height, quality, effort, WebpTransparentColorMode.Preserve, false, 0); // The transparency information will be stored in the green channel of the ARGB quadruplet. // The green channel is allowed extra transformation steps in the specification -- unlike the other channels, // that can improve compression. using Image <Rgba32> alphaAsImage = DispatchAlphaToGreen(image, this.alphaData.GetSpan()); size = lossLessEncoder.EncodeAlphaImageData(alphaAsImage, this.alphaData); return(this.alphaData); } size = width * height; return(this.alphaData); }