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>
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;
if (newSize < sizeRequired)
{
@ -77,8 +72,6 @@ namespace SixLabors.ImageSharp.Formats.Webp.BitWriter
// Make new size multiple of 1k.
newSize = ((newSize >> 10) + 1) << 10;
Array.Resize(ref this.buffer, newSize);
return false;
}
/// <summary>

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

@ -43,14 +43,14 @@ namespace SixLabors.ImageSharp.Formats.Webp.BitWriter
/// </summary>
private int cur;
private int end;
/// <summary>
/// Initializes a new instance of the <see cref="Vp8LBitWriter"/> class.
/// </summary>
/// <param name="expectedSize">The expected size in bytes.</param>
public Vp8LBitWriter(int expectedSize)
: base(expectedSize) => this.end = this.Buffer.Length;
: base(expectedSize)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Vp8LBitWriter"/> class.
@ -184,9 +184,9 @@ namespace SixLabors.ImageSharp.Formats.Webp.BitWriter
private void PutBitsFlushBits()
{
// 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);
}
@ -204,15 +204,9 @@ namespace SixLabors.ImageSharp.Formats.Webp.BitWriter
/// <param name="extraSize">The extra size in bytes needed.</param>
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;
if (this.ResizeBuffer(maxBytes, sizeRequired))
{
return;
}
this.end = this.Buffer.Length;
this.ResizeBuffer(maxBytes, sizeRequired);
}
}
}

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, 5, 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)
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 CalliphoraPartialGrayscale = "Png/CalliphoraPartialGrayscale.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 SnakeGame = "Png/SnakeGame.png";
public const string Icon = "Png/icon.png";

Loading…
Cancel
Save