Browse Source

Merge branch 'master' into af/UniformUnmanagedMemoryPoolMemoryAllocator-02

pull/1730/head
Anton Firszov 4 years ago
committed by GitHub
parent
commit
5b8be243c1
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      src/ImageSharp/Formats/Jpeg/Components/Encoder/HuffmanScanEncoder.cs
  2. 4
      src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs
  3. 18
      tests/ImageSharp.Tests/Formats/WebP/WebpEncoderTests.cs

11
src/ImageSharp/Formats/Jpeg/Components/Encoder/HuffmanScanEncoder.cs

@ -650,6 +650,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder
}
this.target.Write(this.streamWriteBuffer, 0, writeIdx);
this.emitWriteIndex = this.emitBuffer.Length;
}
/// <summary>
@ -660,11 +661,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder
/// This must be called only if <see cref="IsStreamFlushNeeded"/> is true
/// only during the macroblocks encoding routine.
/// </remarks>
private void FlushToStream()
{
private void FlushToStream() =>
this.FlushToStream(this.emitWriteIndex * 4);
this.emitWriteIndex = this.emitBuffer.Length;
}
/// <summary>
/// Flushes final cached bits to the stream padding 1's to
@ -681,10 +679,11 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder
// And writing only valuable count of bytes count we want to write to the output stream
int valuableBytesCount = (int)Numerics.DivideCeil((uint)this.bitCount, 8);
uint packedBytes = this.accumulatedBits | (uint.MaxValue >> this.bitCount);
this.emitBuffer[--this.emitWriteIndex] = packedBytes;
this.emitBuffer[this.emitWriteIndex - 1] = packedBytes;
// Flush cached bytes to the output stream with padding bits
this.FlushToStream((this.emitWriteIndex * 4) - 4 + valuableBytesCount);
int lastByteIndex = (this.emitWriteIndex * 4) - valuableBytesCount;
this.FlushToStream(lastByteIndex);
}
}
}

4
src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs

@ -192,7 +192,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless
public bool UseCrossColorTransform { get; set; }
/// <summary>
/// Gets or sets a value indicating whether to use the substract green transform.
/// Gets or sets a value indicating whether to use the subtract green transform.
/// </summary>
public bool UseSubtractGreenTransform { get; set; }
@ -1050,7 +1050,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless
return EntropyIx.Palette;
}
using IMemoryOwner<uint> histoBuffer = this.memoryAllocator.Allocate<uint>((int)HistoIx.HistoTotal * 256);
using IMemoryOwner<uint> histoBuffer = this.memoryAllocator.Allocate<uint>((int)HistoIx.HistoTotal * 256, AllocationOptions.Clean);
Span<uint> histo = histoBuffer.Memory.Span;
uint pixPrev = bgra[0]; // Skip the first pixel.
ReadOnlySpan<uint> prevRow = null;

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

@ -101,6 +101,24 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp
image.VerifyEncoder(provider, "webp", testOutputDetails, encoder);
}
[Theory]
[WithFile(Lossy.NoFilter06, PixelTypes.Rgba32, 15114)]
public void Encode_Lossless_WithBestQuality_HasExpectedSize<TPixel>(TestImageProvider<TPixel> provider, int expectedBytes)
where TPixel : unmanaged, IPixel<TPixel>
{
var encoder = new WebpEncoder()
{
FileFormat = WebpFileFormatType.Lossless,
Method = WebpEncodingMethod.BestQuality
};
using Image<TPixel> image = provider.GetImage();
using var memoryStream = new MemoryStream();
image.Save(memoryStream, encoder);
Assert.Equal(memoryStream.Length, expectedBytes);
}
[Theory]
[WithFile(RgbTestPattern100x100, PixelTypes.Rgba32, 85)]
[WithFile(RgbTestPattern100x100, PixelTypes.Rgba32, 60)]

Loading…
Cancel
Save