-
-
Notifications
You must be signed in to change notification settings - Fork 887
Add support for encoding lossy webp images with alpha channels #1971
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
96c1c72
Write ALPH chunk (only uncompressed for now)
brianpopow d192941
Add lossless alpha compression
brianpopow 8b8993d
Add encode lossy webp with alpha tests
brianpopow d398ae7
Default alpha compression to true
brianpopow cf672b9
Use memory allocator for alpha data
brianpopow b12ad75
Leave alpha data uncompressed, if compression does not yield in small…
brianpopow 484bd77
Merge branch 'master' into bp/webpalpha
brianpopow 192cfb0
Move disposing the alpha data to the AlphaEncoder
brianpopow 2491b6a
Change AverageBytesPerMb to ReadOnlySpan<byte>
brianpopow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| // Copyright (c) Six Labors. | ||
| // Licensed under the Apache License, Version 2.0. | ||
|
|
||
| using System; | ||
| using System.Buffers; | ||
| using SixLabors.ImageSharp.Advanced; | ||
| using SixLabors.ImageSharp.Formats.Webp.Lossless; | ||
| using SixLabors.ImageSharp.Memory; | ||
| using SixLabors.ImageSharp.PixelFormats; | ||
|
|
||
| namespace SixLabors.ImageSharp.Formats.Webp | ||
| { | ||
| /// <summary> | ||
| /// Methods for encoding the alpha data of a VP8 image. | ||
| /// </summary> | ||
| internal static class AlphaEncoder | ||
| { | ||
| /// <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 static 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; | ||
| IMemoryOwner<byte> 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, alphaData.GetSpan()); | ||
|
|
||
| size = lossLessEncoder.EncodeAlphaImageData(alphaAsImage, alphaData); | ||
|
|
||
| return alphaData; | ||
| } | ||
|
|
||
| size = width * height; | ||
| return alphaData; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Store the transparency in the green channel. | ||
| /// </summary> | ||
| /// <typeparam name="TPixel">The pixel format.</typeparam> | ||
| /// <param name="image">The <see cref="ImageFrame{TPixel}"/> to encode from.</param> | ||
| /// <param name="alphaData">A byte sequence of length width * height, containing all the 8-bit transparency values in scan order.</param> | ||
| /// <returns>The transparency image.</returns> | ||
| private static Image<Rgba32> DispatchAlphaToGreen<TPixel>(Image<TPixel> image, Span<byte> alphaData) | ||
| where TPixel : unmanaged, IPixel<TPixel> | ||
| { | ||
| int width = image.Width; | ||
| int height = image.Height; | ||
| var alphaAsImage = new Image<Rgba32>(width, height); | ||
|
|
||
| for (int y = 0; y < height; y++) | ||
| { | ||
| Memory<Rgba32> rowBuffer = alphaAsImage.DangerousGetPixelRowMemory(y); | ||
| Span<Rgba32> pixelRow = rowBuffer.Span; | ||
| Span<byte> alphaRow = alphaData.Slice(y * width, width); | ||
| for (int x = 0; x < width; x++) | ||
| { | ||
| // Leave A/R/B channels zero'd. | ||
| pixelRow[x] = new Rgba32(0, alphaRow[x], 0, 0); | ||
| } | ||
| } | ||
|
|
||
| return alphaAsImage; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Extract the alpha data of the image. | ||
| /// </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> | ||
| /// <returns>A byte sequence of length width * height, containing all the 8-bit transparency values in scan order.</returns> | ||
| private static IMemoryOwner<byte> ExtractAlphaChannel<TPixel>(Image<TPixel> image, Configuration configuration, MemoryAllocator memoryAllocator) | ||
| where TPixel : unmanaged, IPixel<TPixel> | ||
| { | ||
| Buffer2D<TPixel> imageBuffer = image.Frames.RootFrame.PixelBuffer; | ||
| int height = image.Height; | ||
| int width = image.Width; | ||
| IMemoryOwner<byte> alphaDataBuffer = memoryAllocator.Allocate<byte>(width * height); | ||
| Span<byte> alphaData = alphaDataBuffer.GetSpan(); | ||
|
|
||
| using IMemoryOwner<Rgba32> rowBuffer = memoryAllocator.Allocate<Rgba32>(width); | ||
| Span<Rgba32> rgbaRow = rowBuffer.GetSpan(); | ||
|
|
||
| for (int y = 0; y < height; y++) | ||
| { | ||
| Span<TPixel> rowSpan = imageBuffer.DangerousGetRowSpan(y); | ||
| PixelOperations<TPixel>.Instance.ToRgba32(configuration, rowSpan, rgbaRow); | ||
| int offset = y * width; | ||
| for (int x = 0; x < width; x++) | ||
| { | ||
| alphaData[offset + x] = rgbaRow[x].A; | ||
| } | ||
| } | ||
|
|
||
| return alphaDataBuffer; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't see where this is getting disposed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its done in the
Disposeof theVp8EncoderThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it would be better to keep the responsibility of disposing to the
AlphaEncoderinstead?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think that's wise. Difficult to keep track of otherwise.