Skip to content

Minor PNG Encoding Optimization for DeflaterEngine and WuQuantizer #2023

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions src/ImageSharp/Compression/Zlib/DeflaterEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -483,18 +483,21 @@ private bool FindLongestMatch(int curMatch)
int niceLength = Math.Min(this.niceLength, this.lookahead);

int matchStrt = this.matchStart;
this.matchLen = Math.Max(this.matchLen, DeflaterConstants.MIN_MATCH - 1);
int matchLength = this.matchLen;
matchLength = Math.Max(matchLength, DeflaterConstants.MIN_MATCH - 1);
this.matchLen = matchLength;

if (scan + matchLength > scanMax)
if (scan > scanMax - matchLength)
{
return false;
}

int scanEndPosition = scan + matchLength;

byte* pinnedWindow = this.pinnedWindowPointer;
int scanStart = this.strstart;
byte scanEnd1 = pinnedWindow[scan + matchLength - 1];
byte scanEnd = pinnedWindow[scan + matchLength];
byte scanEnd1 = pinnedWindow[scanEndPosition - 1];
byte scanEnd = pinnedWindow[scanEndPosition];

// Do not waste too much time if we already have a good match:
if (matchLength >= this.goodLength)
Expand All @@ -508,8 +511,9 @@ private bool FindLongestMatch(int curMatch)
match = curMatch;
scan = scanStart;

if (pinnedWindow[match + matchLength] != scanEnd
|| pinnedWindow[match + matchLength - 1] != scanEnd1
int matchEndPosition = match + matchLength;
if (pinnedWindow[matchEndPosition] != scanEnd
|| pinnedWindow[matchEndPosition - 1] != scanEnd1
|| pinnedWindow[match] != pinnedWindow[scan]
|| pinnedWindow[++match] != pinnedWindow[++scan])
{
Expand Down Expand Up @@ -685,6 +689,7 @@ private bool DeflateFast(bool flush, bool finish)
return false;
}

const int windowLen = (2 * DeflaterConstants.WSIZE) - DeflaterConstants.MIN_LOOKAHEAD;
while (this.lookahead >= DeflaterConstants.MIN_LOOKAHEAD || flush)
{
if (this.lookahead == 0)
Expand All @@ -695,7 +700,7 @@ private bool DeflateFast(bool flush, bool finish)
return false;
}

if (this.strstart > (2 * DeflaterConstants.WSIZE) - DeflaterConstants.MIN_LOOKAHEAD)
if (this.strstart > windowLen)
{
// slide window, as FindLongestMatch needs this.
// This should only happen when flushing and the window
Expand Down Expand Up @@ -766,6 +771,7 @@ private bool DeflateSlow(bool flush, bool finish)
return false;
}

const int windowLen = (2 * DeflaterConstants.WSIZE) - DeflaterConstants.MIN_LOOKAHEAD;
while (this.lookahead >= DeflaterConstants.MIN_LOOKAHEAD || flush)
{
if (this.lookahead == 0)
Expand All @@ -783,7 +789,7 @@ private bool DeflateSlow(bool flush, bool finish)
return false;
}

if (this.strstart >= (2 * DeflaterConstants.WSIZE) - DeflaterConstants.MIN_LOOKAHEAD)
if (this.strstart >= windowLen)
{
// slide window, as FindLongestMatch needs this.
// This should only happen when flushing and the window
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,10 @@ public readonly byte GetQuantizedColor(TPixel color, out TPixel match)
Rgba32 rgba = default;
color.ToRgba32(ref rgba);

int r = rgba.R >> (8 - IndexBits);
int g = rgba.G >> (8 - IndexBits);
int b = rgba.B >> (8 - IndexBits);
const int shift = 8 - IndexBits;
int r = rgba.R >> shift;
int g = rgba.G >> shift;
int b = rgba.B >> shift;
int a = rgba.A >> (8 - IndexAlphaBits);

ReadOnlySpan<byte> tagSpan = this.tagsOwner.GetSpan();
Expand Down Expand Up @@ -413,6 +414,9 @@ private void Get3DMoments(MemoryAllocator allocator)
Span<Moment> momentSpan = this.momentsOwner.GetSpan();
Span<Moment> volumeSpan = volume.GetSpan();
Span<Moment> areaSpan = area.GetSpan();
const int indexBits2 = IndexBits * 2;
const int indexAndAlphaBits = IndexBits + IndexAlphaBits;
const int indexBitsAndAlphaBits1 = IndexBits + IndexAlphaBits + 1;
int baseIndex = GetPaletteIndex(1, 0, 0, 0);

for (int r = 1; r < IndexCount; r++)
Expand All @@ -421,9 +425,9 @@ private void Get3DMoments(MemoryAllocator allocator)
// 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)) +
int ind1R = (r << (indexBits2 + IndexAlphaBits)) +
(r << indexBitsAndAlphaBits1) +
(r << indexBits2) +
(r << (IndexBits + 1)) +
r;

Expand All @@ -432,7 +436,7 @@ private void Get3DMoments(MemoryAllocator allocator)
for (int g = 1; g < IndexCount; g++)
{
int ind1G = ind1R +
(g << (IndexBits + IndexAlphaBits)) +
(g << indexAndAlphaBits) +
(g << IndexBits) +
g;
int r_g = r + g;
Expand All @@ -446,7 +450,7 @@ private void Get3DMoments(MemoryAllocator allocator)
b;

Moment line = default;

int bIndexAlphaOffset = b * IndexAlphaCount;
for (int a = 1; a < IndexAlphaCount; a++)
{
int ind1 = ind1B + a;
Expand All @@ -455,7 +459,7 @@ private void Get3DMoments(MemoryAllocator allocator)

areaSpan[a] += line;

int inv = (b * IndexAlphaCount) + a;
int inv = bIndexAlphaOffset + a;
volumeSpan[inv] += areaSpan[a];

int ind2 = ind1 - baseIndex;
Expand Down