Browse Source

Fix BitsLog2Floor

pull/1147/head
Brian Popow 7 years ago
parent
commit
1e80ba9c97
  1. 17
      src/ImageSharp/Formats/WebP/Vp8BitReader.cs
  2. 21
      src/ImageSharp/Formats/WebP/WebPConstants.cs
  3. 2
      src/ImageSharp/Formats/WebP/WebPLossyDecoder.cs

17
src/ImageSharp/Formats/WebP/Vp8BitReader.cs

@ -199,22 +199,17 @@ namespace SixLabors.ImageSharp.Formats.WebP
return x; return x;
} }
// Returns 31 ^ clz(n) = log2(n).Returns 31 ^ clz(n) = log2(n).
private int BitsLog2Floor(uint n) private int BitsLog2Floor(uint n)
{ {
// Search the mask data from most significant bit (MSB) to least significant bit (LSB) for a set bit (1). int logValue = 0;
// https://docs.microsoft.com/en-us/cpp/intrinsics/bitscanreverse-bitscanreverse64?view=vs-2019 while (n >= 256)
uint mask = 1;
for (int y = 0; y < 32; y++)
{ {
if ((mask & n) == mask) logValue += 8;
{ n >>= 8;
return y;
}
mask <<= 1;
} }
return 0; return logValue + WebPConstants.LogTable8bit[n];
} }
} }
} }

21
src/ImageSharp/Formats/WebP/WebPConstants.cs

@ -143,6 +143,27 @@ namespace SixLabors.ImageSharp.Formats.WebP
0, 1, 2, 3, 6, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7, 0 0, 1, 2, 3, 6, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7, 0
}; };
// 31 ^ clz(i)
public static readonly byte[] LogTable8bit =
{
0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
};
// Paragraph 14.1 // Paragraph 14.1
public static readonly int[] DcTable = public static readonly int[] DcTable =
{ {

2
src/ImageSharp/Formats/WebP/WebPLossyDecoder.cs

@ -121,7 +121,7 @@ namespace SixLabors.ImageSharp.Formats.WebP
block.Skip = (byte)this.bitReader.GetBit(dec.SkipProbability); block.Skip = (byte)this.bitReader.GetBit(dec.SkipProbability);
} }
block.IsI4x4 = this.bitReader.GetBit(145) != 0; block.IsI4x4 = this.bitReader.GetBit(145) is 0;
if (!block.IsI4x4) if (!block.IsI4x4)
{ {
// Hardcoded 16x16 intra-mode decision tree. // Hardcoded 16x16 intra-mode decision tree.

Loading…
Cancel
Save