Skip to content

Commit

Permalink
Merge branch 'master' into bp/itransformsse
Browse files Browse the repository at this point in the history
  • Loading branch information
brianpopow authored Nov 14, 2021
2 parents 5074ee6 + 3a10e93 commit 16bb94f
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 21 deletions.
4 changes: 3 additions & 1 deletion src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,10 @@ private void WriteJfifApplicationHeader(ImageMetadata meta)
/// <param name="componentCount">The number of components to write.</param>
private void WriteDefineHuffmanTables(int componentCount)
{
// This uses a C#'s compiler optimization that refers to the static data segment of the assembly,
// and doesn't incur any allocation at all.
// Table identifiers.
ReadOnlySpan<byte> headers = stackalloc byte[]
ReadOnlySpan<byte> headers = new byte[]
{
0x00,
0x10,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ private void WriteCode(Stream stream, int code)

private void ResetTables()
{
this.children.GetSpan().Fill(0);
this.siblings.GetSpan().Fill(0);
this.children.GetSpan().Clear();
this.siblings.GetSpan().Clear();
this.bitsPerCode = MinBits;
this.maxCode = MaxValue(this.bitsPerCode);
this.nextValidCode = EoiCode + 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected override void Decompress(BufferedReadStream stream, int byteCount, int
uint bitsWritten = 0;
for (int y = 0; y < height; y++)
{
scanLine.Fill(0);
scanLine.Clear();
Decode2DScanline(bitReader, this.isWhiteZero, referenceScanLine, scanLine);

bitsWritten = this.WriteScanLine(buffer, scanLine, bitsWritten);
Expand Down Expand Up @@ -116,7 +116,15 @@ private static void Decode2DScanline(T6BitReader bitReader, bool whiteIsZero, Cc
{
// If a TIFF reader encounters EOFB before the expected number of lines has been extracted,
// it is appropriate to assume that the missing rows consist entirely of white pixels.
scanline.Fill(whiteIsZero ? (byte)0 : (byte)255);
if (whiteIsZero)
{
scanline.Clear();
}
else
{
scanline.Fill((byte)255);
}

break;
}

Expand Down
2 changes: 1 addition & 1 deletion src/ImageSharp/Formats/Webp/Lossless/CostModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private static void ConvertPopulationCountTableToBitEstimates(int numSymbols, ui

if (nonzeros <= 1)
{
output.AsSpan(0, numSymbols).Fill(0);
output.AsSpan(0, numSymbols).Clear();
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/ImageSharp/Formats/Webp/Lossless/HistogramEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ private static void OptimizeHistogramSymbols(ushort[] clusterMappings, int numCl

// Create a mapping from a cluster id to its minimal version.
int clusterMax = 0;
clusterMappingsTmp.AsSpan().Fill(0);
clusterMappingsTmp.AsSpan().Clear();

// Re-map the ids.
for (int i = 0; i < symbols.Length; i++)
Expand Down
2 changes: 1 addition & 1 deletion src/ImageSharp/Formats/Webp/Lossless/HuffmanUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ internal static class HuffmanUtils
public static void CreateHuffmanTree(uint[] histogram, int treeDepthLimit, bool[] bufRle, HuffmanTree[] huffTree, HuffmanTreeCode huffCode)
{
int numSymbols = huffCode.NumSymbols;
bufRle.AsSpan().Fill(false);
bufRle.AsSpan().Clear();
OptimizeHuffmanForRle(numSymbols, bufRle, histogram);
GenerateOptimalTree(huffTree, histogram, numSymbols, treeDepthLimit, huffCode.CodeLengths);

Expand Down
10 changes: 5 additions & 5 deletions src/ImageSharp/Formats/Webp/Lossless/Vp8LHistogram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ private void AddLiteral(Vp8LHistogram b, Vp8LHistogram output, int literalSize)
}
else
{
output.Literal.AsSpan(0, literalSize).Fill(0);
output.Literal.AsSpan(0, literalSize).Clear();
}
}

Expand All @@ -343,7 +343,7 @@ private void AddRed(Vp8LHistogram b, Vp8LHistogram output, int size)
}
else
{
output.Red.AsSpan(0, size).Fill(0);
output.Red.AsSpan(0, size).Clear();
}
}

Expand All @@ -366,7 +366,7 @@ private void AddBlue(Vp8LHistogram b, Vp8LHistogram output, int size)
}
else
{
output.Blue.AsSpan(0, size).Fill(0);
output.Blue.AsSpan(0, size).Clear();
}
}

Expand All @@ -389,7 +389,7 @@ private void AddAlpha(Vp8LHistogram b, Vp8LHistogram output, int size)
}
else
{
output.Alpha.AsSpan(0, size).Fill(0);
output.Alpha.AsSpan(0, size).Clear();
}
}

Expand All @@ -412,7 +412,7 @@ private void AddDistance(Vp8LHistogram b, Vp8LHistogram output, int size)
}
else
{
output.Distance.AsSpan(0, size).Fill(0);
output.Distance.AsSpan(0, size).Clear();
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/ImageSharp/Formats/Webp/Lossy/Vp8EncIterator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -911,22 +911,22 @@ private void InitLeft()

this.LeftNz[8] = 0;

this.LeftDerr.AsSpan().Fill(0);
this.LeftDerr.AsSpan().Clear();
}

private void InitTop()
{
int topSize = this.mbw * 16;
this.YTop.AsSpan(0, topSize).Fill(127);
this.UvTop.AsSpan().Fill(127);
this.Nz.AsSpan().Fill(0);
this.Nz.AsSpan().Clear();

int predsW = (4 * this.mbw) + 1;
int predsH = (4 * this.mbh) + 1;
int predsSize = predsW * predsH;
this.Preds.AsSpan(predsSize + this.predsWidth, this.mbw).Fill(0);
this.Preds.AsSpan(predsSize + this.predsWidth, this.mbw).Clear();

this.TopDerr.AsSpan().Fill(0);
this.TopDerr.AsSpan().Clear();
}

private int Bit(uint nz, int n) => (nz & (1 << n)) != 0 ? 1 : 0;
Expand Down
2 changes: 1 addition & 1 deletion src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ private void ResetBoundaryPredictions()
int predsW = (4 * this.Mbw) + 1;
int predsH = (4 * this.Mbh) + 1;
int predsSize = predsW * predsH;
this.Preds.AsSpan(predsSize + this.PredsWidth - 4, 4).Fill(0);
this.Preds.AsSpan(predsSize + this.PredsWidth - 4, 4).Clear();

this.Nz[0] = 0; // constant
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,19 +417,40 @@ private void Get3DMoments(MemoryAllocator allocator)

for (int r = 1; r < IndexCount; r++)
{
// Currently, RyuJIT hoists the invariants of multi-level nested loop only to the
// immediate outer loop. See https://github.com/dotnet/runtime/issues/61420
// To ensure the calculation doesn't happen repeatedly, hoist some of the calculations
// in the form of ind1* manually.
int ind1R = (r << ((IndexBits * 2) + IndexAlphaBits)) +
(r << (IndexBits + IndexAlphaBits + 1)) +
(r << (IndexBits * 2)) +
(r << (IndexBits + 1)) +
r;

volumeSpan.Clear();

for (int g = 1; g < IndexCount; g++)
{
int ind1G = ind1R +
(g << (IndexBits + IndexAlphaBits)) +
(g << IndexBits) +
g;
int r_g = r + g;

areaSpan.Clear();

for (int b = 1; b < IndexCount; b++)
{
int ind1B = ind1G +
((r_g + b) << IndexAlphaBits) +
b;

Moment line = default;

for (int a = 1; a < IndexAlphaCount; a++)
{
int ind1 = GetPaletteIndex(r, g, b, a);
int ind1 = ind1B + a;

line += momentSpan[ind1];

areaSpan[a] += line;
Expand Down Expand Up @@ -628,13 +649,35 @@ private void Mark(ref Box cube, byte label)

for (int r = cube.RMin + 1; r <= cube.RMax; r++)
{
// Currently, RyuJIT hoists the invariants of multi-level nested loop only to the
// immediate outer loop. See https://github.com/dotnet/runtime/issues/61420
// To ensure the calculation doesn't happen repeatedly, hoist some of the calculations
// in the form of ind1* manually.
int ind1R = (r << ((IndexBits * 2) + IndexAlphaBits)) +
(r << (IndexBits + IndexAlphaBits + 1)) +
(r << (IndexBits * 2)) +
(r << (IndexBits + 1)) +
r;

for (int g = cube.GMin + 1; g <= cube.GMax; g++)
{
int ind1G = ind1R +
(g << (IndexBits + IndexAlphaBits)) +
(g << IndexBits) +
g;
int r_g = r + g;

for (int b = cube.BMin + 1; b <= cube.BMax; b++)
{
int ind1B = ind1G +
((r_g + b) << IndexAlphaBits) +
b;

for (int a = cube.AMin + 1; a <= cube.AMax; a++)
{
tagSpan[GetPaletteIndex(r, g, b, a)] = label;
int index = ind1B + a;

tagSpan[index] = label;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ImageSharp.Benchmarks/Codecs/EncodeIndexedPng.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void PngCoreWu()
public void PngCoreWuNoDither()
{
using var memoryStream = new MemoryStream();
var options = new PngEncoder { Quantizer = new WuQuantizer(new QuantizerOptions { Dither = null }) };
var options = new PngEncoder { Quantizer = new WuQuantizer(new QuantizerOptions { Dither = null }), ColorType = PngColorType.Palette };
this.bmpCore.SaveAsPng(memoryStream, options);
}
}
Expand Down

0 comments on commit 16bb94f

Please sign in to comment.