Browse Source

Use Numerics.Log2

pull/1552/head
Brian Popow 5 years ago
parent
commit
94c49d7c0f
  1. 2
      src/ImageSharp/Formats/Webp/BitReader/Vp8BitReader.cs
  2. 4
      src/ImageSharp/Formats/Webp/Lossless/LosslessUtils.cs
  3. 4
      src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs
  4. 17
      src/ImageSharp/Formats/Webp/WebpCommonUtils.cs

2
src/ImageSharp/Formats/Webp/BitReader/Vp8BitReader.cs

@ -111,7 +111,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.BitReader
range = split + 1;
}
int shift = 7 ^ WebpCommonUtils.BitsLog2Floor(range);
int shift = 7 ^ Numerics.Log2(range);
range <<= shift;
this.bits -= shift;

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

@ -847,7 +847,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless
/// </summary>
private static int PrefixEncodeBitsNoLut(int distance, ref int extraBits)
{
int highestBit = WebpCommonUtils.BitsLog2Floor((uint)--distance);
int highestBit = Numerics.Log2((uint)--distance);
int secondHighestBit = (distance >> (highestBit - 1)) & 1;
extraBits = highestBit - 1;
int code = (2 * highestBit) + secondHighestBit;
@ -856,7 +856,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless
private static int PrefixEncodeNoLut(int distance, ref int extraBits, ref int extraBitsValue)
{
int highestBit = WebpCommonUtils.BitsLog2Floor((uint)--distance);
int highestBit = Numerics.Log2((uint)--distance);
int secondHighestBit = (distance >> (highestBit - 1)) & 1;
extraBits = highestBit - 1;
extraBitsValue = distance & ((1 << extraBits) - 1);

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

@ -330,7 +330,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless
// If using a color cache, do not have it bigger than the number of colors.
if (useCache && this.PaletteSize < 1 << WebpConstants.MaxColorCacheBits)
{
this.CacheBits = WebpCommonUtils.BitsLog2Floor((uint)this.PaletteSize) + 1;
this.CacheBits = Numerics.Log2((uint)this.PaletteSize) + 1;
}
}
@ -893,7 +893,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless
}
else
{
int nBits = WebpCommonUtils.BitsLog2Floor((uint)trimmedLength - 2);
int nBits = Numerics.Log2((uint)trimmedLength - 2);
int nBitPairs = (nBits / 2) + 1;
this.bitWriter.PutBits((uint)nBitPairs - 1, 3);
this.bitWriter.PutBits((uint)trimmedLength - 2, nBitPairs * 2);

17
src/ImageSharp/Formats/Webp/WebpCommonUtils.cs

@ -2,7 +2,6 @@
// Licensed under the Apache License, Version 2.0.
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using SixLabors.ImageSharp.PixelFormats;
#if SUPPORTS_RUNTIME_INTRINSICS
@ -17,22 +16,6 @@ namespace SixLabors.ImageSharp.Formats.Webp
/// </summary>
internal static class WebpCommonUtils
{
/// <summary>
/// Returns 31 ^ clz(n) = log2(n).Returns 31 ^ clz(n) = log2(n).
/// </summary>
[MethodImpl(InliningOptions.ShortMethod)]
public static int BitsLog2Floor(uint n)
{
int logValue = 0;
while (n >= 256)
{
logValue += 8;
n >>= 8;
}
return logValue + Unsafe.Add(ref MemoryMarshal.GetReference(WebpLookupTables.LogTable8Bit), (int)n);
}
/// <summary>
/// Checks if the pixel row is not opaque.
/// </summary>

Loading…
Cancel
Save