Browse Source

Fix issue with resizing the bitwriter buffer

pull/1552/head
Brian Popow 5 years ago
parent
commit
3b603be32e
  1. 9
      src/ImageSharp/Formats/WebP/BitWriter/BitWriterBase.cs
  2. 20
      src/ImageSharp/Formats/WebP/BitWriter/Vp8LBitWriter.cs
  3. 1
      tests/ImageSharp.Tests/Formats/WebP/WebpEncoderTests.cs
  4. 1
      tests/ImageSharp.Tests/TestImages.cs

9
src/ImageSharp/Formats/WebP/BitWriter/BitWriterBase.cs

@ -61,13 +61,8 @@ namespace SixLabors.ImageSharp.Formats.Webp.BitWriter
/// <param name="height">The height of the image.</param> /// <param name="height">The height of the image.</param>
public abstract void WriteEncodedImageToStream(Stream stream, ExifProfile exifProfile, uint width, uint height); public abstract void WriteEncodedImageToStream(Stream stream, ExifProfile exifProfile, uint width, uint height);
protected bool ResizeBuffer(int maxBytes, int sizeRequired) protected void ResizeBuffer(int maxBytes, int sizeRequired)
{ {
if (maxBytes > 0 && sizeRequired < maxBytes)
{
return true;
}
int newSize = (3 * maxBytes) >> 1; int newSize = (3 * maxBytes) >> 1;
if (newSize < sizeRequired) if (newSize < sizeRequired)
{ {
@ -77,8 +72,6 @@ namespace SixLabors.ImageSharp.Formats.Webp.BitWriter
// Make new size multiple of 1k. // Make new size multiple of 1k.
newSize = ((newSize >> 10) + 1) << 10; newSize = ((newSize >> 10) + 1) << 10;
Array.Resize(ref this.buffer, newSize); Array.Resize(ref this.buffer, newSize);
return false;
} }
/// <summary> /// <summary>

20
src/ImageSharp/Formats/WebP/BitWriter/Vp8LBitWriter.cs

@ -43,14 +43,14 @@ namespace SixLabors.ImageSharp.Formats.Webp.BitWriter
/// </summary> /// </summary>
private int cur; private int cur;
private int end;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Vp8LBitWriter"/> class. /// Initializes a new instance of the <see cref="Vp8LBitWriter"/> class.
/// </summary> /// </summary>
/// <param name="expectedSize">The expected size in bytes.</param> /// <param name="expectedSize">The expected size in bytes.</param>
public Vp8LBitWriter(int expectedSize) public Vp8LBitWriter(int expectedSize)
: base(expectedSize) => this.end = this.Buffer.Length; : base(expectedSize)
{
}
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Vp8LBitWriter"/> class. /// Initializes a new instance of the <see cref="Vp8LBitWriter"/> class.
@ -184,9 +184,9 @@ namespace SixLabors.ImageSharp.Formats.Webp.BitWriter
private void PutBitsFlushBits() private void PutBitsFlushBits()
{ {
// If needed, make some room by flushing some bits out. // If needed, make some room by flushing some bits out.
if (this.cur + WriterBytes > this.end) if (this.cur + WriterBytes > this.Buffer.Length)
{ {
int extraSize = this.end - this.cur + MinExtraSize; int extraSize = this.Buffer.Length - this.cur + MinExtraSize;
this.BitWriterResize(extraSize); this.BitWriterResize(extraSize);
} }
@ -204,15 +204,9 @@ namespace SixLabors.ImageSharp.Formats.Webp.BitWriter
/// <param name="extraSize">The extra size in bytes needed.</param> /// <param name="extraSize">The extra size in bytes needed.</param>
public override void BitWriterResize(int extraSize) public override void BitWriterResize(int extraSize)
{ {
int maxBytes = this.end + this.Buffer.Length; int maxBytes = this.Buffer.Length + this.Buffer.Length;
int sizeRequired = this.cur + extraSize; int sizeRequired = this.cur + extraSize;
this.ResizeBuffer(maxBytes, sizeRequired);
if (this.ResizeBuffer(maxBytes, sizeRequired))
{
return;
}
this.end = this.Buffer.Length;
} }
} }
} }

1
tests/ImageSharp.Tests/Formats/WebP/WebpEncoderTests.cs

@ -64,6 +64,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 4, 100)] [WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 4, 100)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 5, 100)] [WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 5, 100)]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 6, 100)] [WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 6, 100)]
[WithFile(TestImages.Png.BikeSmall, PixelTypes.Rgba32, 6, 100)]
public void Encode_Lossless_WithDifferentMethodAndQuality_Works<TPixel>(TestImageProvider<TPixel> provider, int method, int quality) public void Encode_Lossless_WithDifferentMethodAndQuality_Works<TPixel>(TestImageProvider<TPixel> provider, int method, int quality)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {

1
tests/ImageSharp.Tests/TestImages.cs

@ -44,6 +44,7 @@ namespace SixLabors.ImageSharp.Tests
public const string CalliphoraPartial = "Png/CalliphoraPartial.png"; public const string CalliphoraPartial = "Png/CalliphoraPartial.png";
public const string CalliphoraPartialGrayscale = "Png/CalliphoraPartialGrayscale.png"; public const string CalliphoraPartialGrayscale = "Png/CalliphoraPartialGrayscale.png";
public const string Bike = "Png/Bike.png"; public const string Bike = "Png/Bike.png";
public const string BikeSmall = "Png/bike-small.png";
public const string BikeGrayscale = "Png/BikeGrayscale.png"; public const string BikeGrayscale = "Png/BikeGrayscale.png";
public const string SnakeGame = "Png/SnakeGame.png"; public const string SnakeGame = "Png/SnakeGame.png";
public const string Icon = "Png/icon.png"; public const string Icon = "Png/icon.png";

Loading…
Cancel
Save