Browse Source

Avoid bounds checks in IsFlat

pull/1852/head
Brian Popow 5 years ago
parent
commit
38fd3a8458
  1. 6
      src/ImageSharp/Formats/Webp/Lossy/QuantEnc.cs

6
src/ImageSharp/Formats/Webp/Lossy/QuantEnc.cs

@ -744,19 +744,21 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy
private static bool IsFlat(Span<short> levels, int numBlocks, int thresh) private static bool IsFlat(Span<short> levels, int numBlocks, int thresh)
{ {
int score = 0; int score = 0;
ref short levelsRef = ref MemoryMarshal.GetReference(levels);
int offset = 0;
while (numBlocks-- > 0) while (numBlocks-- > 0)
{ {
for (int i = 1; i < 16; i++) for (int i = 1; i < 16; i++)
{ {
// omit DC, we're only interested in AC // omit DC, we're only interested in AC
score += levels[i] != 0 ? 1 : 0; score += Unsafe.Add(ref levelsRef, offset) != 0 ? 1 : 0;
if (score > thresh) if (score > thresh)
{ {
return false; return false;
} }
} }
levels = levels.Slice(16); offset += 16;
} }
return true; return true;

Loading…
Cancel
Save