Browse Source

Add VP8 bitreader to image info, remove image data size (not used except inside the bitreader)

pull/1147/head
Brian Popow 7 years ago
parent
commit
948728830a
  1. 23
      src/ImageSharp/Formats/WebP/WebPDecoderCore.cs
  2. 12
      src/ImageSharp/Formats/WebP/WebPImageInfo.cs
  3. 14
      src/ImageSharp/Formats/WebP/WebPLossyDecoder.cs

23
src/ImageSharp/Formats/WebP/WebPDecoderCore.cs

@ -89,13 +89,13 @@ namespace SixLabors.ImageSharp.Formats.WebP
Buffer2D<TPixel> pixels = image.GetRootFramePixelBuffer(); Buffer2D<TPixel> pixels = image.GetRootFramePixelBuffer();
if (imageInfo.IsLossLess) if (imageInfo.IsLossLess)
{ {
var losslessDecoder = new WebPLosslessDecoder(imageInfo.Vp9LBitReader, this.memoryAllocator); var losslessDecoder = new WebPLosslessDecoder(imageInfo.Vp8LBitReader, this.memoryAllocator);
losslessDecoder.Decode(pixels, image.Width, image.Height); losslessDecoder.Decode(pixels, image.Width, image.Height);
} }
else else
{ {
var lossyDecoder = new WebPLossyDecoder(this.configuration, this.currentStream); var lossyDecoder = new WebPLossyDecoder(imageInfo.Vp8BitReader, this.memoryAllocator);
lossyDecoder.Decode(pixels, image.Width, image.Height, imageInfo.ImageDataSize, imageInfo.Vp8Profile); lossyDecoder.Decode(pixels, image.Width, image.Height, imageInfo.Vp8Profile);
} }
// There can be optional chunks after the image data, like EXIF and XMP. // There can be optional chunks after the image data, like EXIF and XMP.
@ -335,15 +335,17 @@ namespace SixLabors.ImageSharp.Formats.WebP
WebPThrowHelper.ThrowImageFormatException("width or height can not be zero"); WebPThrowHelper.ThrowImageFormatException("width or height can not be zero");
} }
var bitReader = new Vp8BitReader(this.currentStream, dataSize - 10, this.memoryAllocator);
return new WebPImageInfo() return new WebPImageInfo()
{ {
Width = width, Width = width,
Height = height, Height = height,
BitsPerPixel = features?.Alpha is true ? WebPBitsPerPixel.Pixel32 : WebPBitsPerPixel.Pixel24, BitsPerPixel = features?.Alpha is true ? WebPBitsPerPixel.Pixel32 : WebPBitsPerPixel.Pixel24,
IsLossLess = false, IsLossLess = false,
ImageDataSize = dataSize - 10, // 10 bytes are read here in the header already.
Features = features, Features = features,
Vp8Profile = (sbyte)version Vp8Profile = (sbyte)version,
Vp8BitReader = bitReader
}; };
} }
@ -377,12 +379,16 @@ namespace SixLabors.ImageSharp.Formats.WebP
} }
// The alphaIsUsed flag should be set to 0 when all alpha values are 255 in the picture, and 1 otherwise. // The alphaIsUsed flag should be set to 0 when all alpha values are 255 in the picture, and 1 otherwise.
// TODO: this flag value is not used yet
bool alphaIsUsed = bitReader.ReadBit(); bool alphaIsUsed = bitReader.ReadBit();
// The next 3 bits are the version. The version_number is a 3 bit code that must be set to 0. // The next 3 bits are the version. The version number is a 3 bit code that must be set to 0.
// Any other value should be treated as an error. // Any other value should be treated as an error.
// TODO: should we throw here when version number is != 0?
uint version = bitReader.ReadValue(WebPConstants.Vp8LVersionBits); uint version = bitReader.ReadValue(WebPConstants.Vp8LVersionBits);
if (version != 0)
{
WebPThrowHelper.ThrowNotSupportedException($"Unexpected version number {version} found in VP8L header");
}
return new WebPImageInfo() return new WebPImageInfo()
{ {
@ -390,9 +396,8 @@ namespace SixLabors.ImageSharp.Formats.WebP
Height = (int)height, Height = (int)height,
BitsPerPixel = WebPBitsPerPixel.Pixel32, BitsPerPixel = WebPBitsPerPixel.Pixel32,
IsLossLess = true, IsLossLess = true,
ImageDataSize = imageDataSize,
Features = features, Features = features,
Vp9LBitReader = bitReader Vp8LBitReader = bitReader
}; };
} }

12
src/ImageSharp/Formats/WebP/WebPImageInfo.cs

@ -31,18 +31,18 @@ namespace SixLabors.ImageSharp.Formats.WebP
public WebPFeatures Features { get; set; } public WebPFeatures Features { get; set; }
/// <summary> /// <summary>
/// Gets or sets the bytes of the image payload. /// Gets or sets the VP8 profile / version. Valid values are between 0 and 3. Default value will be the invalid value -1.
/// </summary> /// </summary>
public uint ImageDataSize { get; set; } public int Vp8Profile { get; set; } = -1;
/// <summary> /// <summary>
/// Gets or sets the VP8 profile / version. Valid values are between 0 and 3. Default value will be the invalid value -1. /// Gets or sets the Vp8L bitreader. Will be null, if its not lossless image.
/// </summary> /// </summary>
public int Vp8Profile { get; set; } = -1; public Vp8LBitReader Vp8LBitReader { get; set; } = null;
/// <summary> /// <summary>
/// Gets or sets Vp8L bitreader. Will be null if its not lossless image. /// Gets or sets the VP8 bitreader. Will be null, if its not a lossy image.
/// </summary> /// </summary>
public Vp8LBitReader Vp9LBitReader { get; set; } = null; public Vp8BitReader Vp8BitReader { get; set; } = null;
} }
} }

14
src/ImageSharp/Formats/WebP/WebPLossyDecoder.cs

@ -11,20 +11,17 @@ namespace SixLabors.ImageSharp.Formats.WebP
{ {
internal sealed class WebPLossyDecoder : WebPDecoderBase internal sealed class WebPLossyDecoder : WebPDecoderBase
{ {
private readonly Configuration configuration; private readonly Vp8BitReader bitReader;
private readonly Stream currentStream;
private readonly MemoryAllocator memoryAllocator; private readonly MemoryAllocator memoryAllocator;
public WebPLossyDecoder(Configuration configuration, Stream currentStream) public WebPLossyDecoder(Vp8BitReader bitReader, MemoryAllocator memoryAllocator)
{ {
this.configuration = configuration; this.memoryAllocator = memoryAllocator;
this.currentStream = currentStream; this.bitReader = bitReader;
this.memoryAllocator = configuration.MemoryAllocator;
} }
public void Decode<TPixel>(Buffer2D<TPixel> pixels, int width, int height, uint imageDataSize, int vp8Version) public void Decode<TPixel>(Buffer2D<TPixel> pixels, int width, int height, int vp8Version)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
// we need buffers for Y U and V in size of the image // we need buffers for Y U and V in size of the image
@ -35,7 +32,6 @@ namespace SixLabors.ImageSharp.Formats.WebP
// those prediction values are the base, the values from DCT processing are added to that // those prediction values are the base, the values from DCT processing are added to that
// TODO residue signal from DCT: 4x4 blocks of DCT transforms, 16Y, 4U, 4V // TODO residue signal from DCT: 4x4 blocks of DCT transforms, 16Y, 4U, 4V
var bitReader = new Vp8BitReader(this.currentStream, imageDataSize, this.memoryAllocator);
Vp8Profile vp8Profile = this.DecodeProfile(vp8Version); Vp8Profile vp8Profile = this.DecodeProfile(vp8Version);
} }

Loading…
Cancel
Save