|
|
|
@ -24,13 +24,15 @@ namespace SixLabors.ImageSharp.Formats.Webp |
|
|
|
/// <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>
|
|
|
|
/// <returns>The alpha data.</returns>
|
|
|
|
public static byte[] EncodeAlpha<TPixel>(Image<TPixel> image, Configuration configuration, MemoryAllocator memoryAllocator, bool compress) |
|
|
|
/// <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> |
|
|
|
{ |
|
|
|
byte[] alphaData = ExtractAlphaChannel(image, configuration, memoryAllocator); |
|
|
|
int width = image.Width; |
|
|
|
int height = image.Height; |
|
|
|
IMemoryOwner<byte> alphaData = ExtractAlphaChannel(image, configuration, memoryAllocator); |
|
|
|
|
|
|
|
if (compress) |
|
|
|
{ |
|
|
|
WebpEncodingMethod effort = WebpEncodingMethod.Default; |
|
|
|
@ -49,11 +51,14 @@ namespace SixLabors.ImageSharp.Formats.Webp |
|
|
|
// 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); |
|
|
|
using Image<Rgba32> alphaAsImage = DispatchAlphaToGreen(image, alphaData.GetSpan()); |
|
|
|
|
|
|
|
size = lossLessEncoder.EncodeAlphaImageData(alphaAsImage, alphaData); |
|
|
|
|
|
|
|
return lossLessEncoder.EncodeAlphaImageData(alphaAsImage); |
|
|
|
return alphaData; |
|
|
|
} |
|
|
|
|
|
|
|
size = width * height; |
|
|
|
return alphaData; |
|
|
|
} |
|
|
|
|
|
|
|
@ -64,7 +69,7 @@ namespace SixLabors.ImageSharp.Formats.Webp |
|
|
|
/// <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, byte[] alphaData) |
|
|
|
private static Image<Rgba32> DispatchAlphaToGreen<TPixel>(Image<TPixel> image, Span<byte> alphaData) |
|
|
|
where TPixel : unmanaged, IPixel<TPixel> |
|
|
|
{ |
|
|
|
int width = image.Width; |
|
|
|
@ -75,7 +80,7 @@ namespace SixLabors.ImageSharp.Formats.Webp |
|
|
|
{ |
|
|
|
Memory<Rgba32> rowBuffer = alphaAsImage.DangerousGetPixelRowMemory(y); |
|
|
|
Span<Rgba32> pixelRow = rowBuffer.Span; |
|
|
|
Span<byte> alphaRow = alphaData.AsSpan(y * width, width); |
|
|
|
Span<byte> alphaRow = alphaData.Slice(y * width, width); |
|
|
|
for (int x = 0; x < width; x++) |
|
|
|
{ |
|
|
|
// Leave A/R/B channels zero'd.
|
|
|
|
@ -94,13 +99,14 @@ namespace SixLabors.ImageSharp.Formats.Webp |
|
|
|
/// <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 byte[] ExtractAlphaChannel<TPixel>(Image<TPixel> image, Configuration configuration, MemoryAllocator memoryAllocator) |
|
|
|
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; |
|
|
|
byte[] alphaData = new byte[width * height]; |
|
|
|
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(); |
|
|
|
@ -116,7 +122,7 @@ namespace SixLabors.ImageSharp.Formats.Webp |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return alphaData; |
|
|
|
return alphaDataBuffer; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|