Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
10 changes: 6 additions & 4 deletions src/ImageSharp/Formats/Webp/BitReader/Vp8BitReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,11 @@ public int GetSigned(int v)
[MethodImpl(InliningOptions.ShortMethod)]
public bool ReadBool() => this.ReadValue(1) is 1;

[MethodImpl(InliningOptions.ShortMethod)]
public uint ReadValue(int nBits)
{
Guard.MustBeGreaterThan(nBits, 0, nameof(nBits));
Guard.MustBeLessThanOrEqualTo(nBits, 32, nameof(nBits));
DebugGuard.MustBeGreaterThan(nBits, 0, nameof(nBits));
DebugGuard.MustBeLessThanOrEqualTo(nBits, 32, nameof(nBits));

uint v = 0;
while (nBits-- > 0)
Expand All @@ -156,10 +157,11 @@ public uint ReadValue(int nBits)
return v;
}

[MethodImpl(InliningOptions.ShortMethod)]
public int ReadSignedValue(int nBits)
{
Guard.MustBeGreaterThan(nBits, 0, nameof(nBits));
Guard.MustBeLessThanOrEqualTo(nBits, 32, nameof(nBits));
DebugGuard.MustBeGreaterThan(nBits, 0, nameof(nBits));
DebugGuard.MustBeLessThanOrEqualTo(nBits, 32, nameof(nBits));

int value = (int)this.ReadValue(nBits);
return this.ReadValue(1) != 0 ? -value : value;
Expand Down
24 changes: 8 additions & 16 deletions src/ImageSharp/Formats/Webp/BitReader/Vp8LBitReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ internal class Vp8LBitReader : BitReaderBase
/// </summary>
private const int Wbits = 32;

private readonly uint[] bitMask =
private static readonly uint[] BitMask =
{
0,
0x000001, 0x000003, 0x000007, 0x00000f,
Expand Down Expand Up @@ -125,19 +125,19 @@ public Vp8LBitReader(Stream inputStream, uint imageDataSize, MemoryAllocator mem
/// </summary>
/// <param name="nBits">The number of bits to read (should not exceed 16).</param>
/// <returns>A ushort value.</returns>
[MethodImpl(InliningOptions.ShortMethod)]
public uint ReadValue(int nBits)
{
Guard.MustBeGreaterThan(nBits, 0, nameof(nBits));
DebugGuard.MustBeGreaterThan(nBits, 0, nameof(nBits));

if (!this.Eos && nBits <= Vp8LMaxNumBitRead)
{
ulong val = this.PrefetchBits() & this.bitMask[nBits];
ulong val = this.PrefetchBits() & BitMask[nBits];
this.bitPos += nBits;
this.ShiftBytes();
return (uint)val;
}

this.SetEndOfStream();
return 0;
}

Expand Down Expand Up @@ -169,6 +169,7 @@ public bool ReadBit()
/// <summary>
/// Advances the read buffer by 4 bytes to make room for reading next 32 bits.
/// </summary>
[MethodImpl(InliningOptions.ShortMethod)]
public void FillBitWindow()
{
if (this.bitPos >= Wbits)
Expand All @@ -181,14 +182,16 @@ public void FillBitWindow()
/// Returns true if there was an attempt at reading bit past the end of the buffer.
/// </summary>
/// <returns>True, if end of buffer was reached.</returns>
public bool IsEndOfStream() => this.Eos || ((this.pos == this.len) && (this.bitPos > Lbits));
[MethodImpl(InliningOptions.ShortMethod)]
public bool IsEndOfStream() => this.Eos || (this.pos == this.len && this.bitPos > Lbits);

[MethodImpl(InliningOptions.ShortMethod)]
private void DoFillBitWindow() => this.ShiftBytes();

/// <summary>
/// If not at EOS, reload up to Vp8LLbits byte-by-byte.
/// </summary>
[MethodImpl(InliningOptions.ShortMethod)]
private void ShiftBytes()
{
System.Span<byte> dataSpan = this.Data.Memory.Span;
Expand All @@ -199,17 +202,6 @@ private void ShiftBytes()
++this.pos;
this.bitPos -= 8;
}

if (this.IsEndOfStream())
{
this.SetEndOfStream();
}
}

private void SetEndOfStream()
{
this.Eos = true;
this.bitPos = 0; // To avoid undefined behaviour with shifts.
}
}
}
48 changes: 24 additions & 24 deletions src/ImageSharp/Formats/Webp/Lossy/LossyUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -934,11 +934,11 @@ private static void DoFilter2(Span<byte> p, int offset, int step)
int p0 = p[offset - step];
int q0 = p[offset];
int q1 = p[offset + step];
int a = (3 * (q0 - p0)) + WebpLookupTables.Sclip1[p1 - q1 + 1020];
int a1 = WebpLookupTables.Sclip2[((a + 4) >> 3) + 112];
int a2 = WebpLookupTables.Sclip2[((a + 3) >> 3) + 112];
p[offset - step] = WebpLookupTables.Clip1[p0 + a2 + 255];
p[offset] = WebpLookupTables.Clip1[q0 - a1 + 255];
int a = (3 * (q0 - p0)) + WebpLookupTables.Sclip1(p1 - q1);
int a1 = WebpLookupTables.Sclip2((a + 4) >> 3);
int a2 = WebpLookupTables.Sclip2((a + 3) >> 3);
p[offset - step] = WebpLookupTables.Clip1(p0 + a2);
p[offset] = WebpLookupTables.Clip1(q0 - a1);
}

private static void DoFilter4(Span<byte> p, int offset, int step)
Expand All @@ -950,13 +950,13 @@ private static void DoFilter4(Span<byte> p, int offset, int step)
int q0 = p[offset];
int q1 = p[offset + step];
int a = 3 * (q0 - p0);
int a1 = WebpLookupTables.Sclip2[((a + 4) >> 3) + 112];
int a2 = WebpLookupTables.Sclip2[((a + 3) >> 3) + 112];
int a1 = WebpLookupTables.Sclip2((a + 4) >> 3);
int a2 = WebpLookupTables.Sclip2((a + 3) >> 3);
int a3 = (a1 + 1) >> 1;
p[offsetMinus2Step] = WebpLookupTables.Clip1[p1 + a3 + 255];
p[offset - step] = WebpLookupTables.Clip1[p0 + a2 + 255];
p[offset] = WebpLookupTables.Clip1[q0 - a1 + 255];
p[offset + step] = WebpLookupTables.Clip1[q1 - a3 + 255];
p[offsetMinus2Step] = WebpLookupTables.Clip1(p1 + a3);
p[offset - step] = WebpLookupTables.Clip1(p0 + a2);
p[offset] = WebpLookupTables.Clip1(q0 - a1);
p[offset + step] = WebpLookupTables.Clip1(q1 - a3);
}

private static void DoFilter6(Span<byte> p, int offset, int step)
Expand All @@ -971,18 +971,18 @@ private static void DoFilter6(Span<byte> p, int offset, int step)
int q0 = p[offset];
int q1 = p[offset + step];
int q2 = p[offset + step2];
int a = WebpLookupTables.Sclip1[(3 * (q0 - p0)) + WebpLookupTables.Sclip1[p1 - q1 + 1020] + 1020];
int a = WebpLookupTables.Sclip1((3 * (q0 - p0)) + WebpLookupTables.Sclip1(p1 - q1));

// a is in [-128,127], a1 in [-27,27], a2 in [-18,18] and a3 in [-9,9]
int a1 = ((27 * a) + 63) >> 7; // eq. to ((3 * a + 7) * 9) >> 7
int a2 = ((18 * a) + 63) >> 7; // eq. to ((2 * a + 7) * 9) >> 7
int a3 = ((9 * a) + 63) >> 7; // eq. to ((1 * a + 7) * 9) >> 7
p[offset - step3] = WebpLookupTables.Clip1[p2 + a3 + 255];
p[offset - step2] = WebpLookupTables.Clip1[p1 + a2 + 255];
p[offsetMinusStep] = WebpLookupTables.Clip1[p0 + a1 + 255];
p[offset] = WebpLookupTables.Clip1[q0 - a1 + 255];
p[offset + step] = WebpLookupTables.Clip1[q1 - a2 + 255];
p[offset + step2] = WebpLookupTables.Clip1[q2 - a3 + 255];
p[offset - step3] = WebpLookupTables.Clip1(p2 + a3);
p[offset - step2] = WebpLookupTables.Clip1(p1 + a2);
p[offsetMinusStep] = WebpLookupTables.Clip1(p0 + a1);
p[offset] = WebpLookupTables.Clip1(q0 - a1);
p[offset + step] = WebpLookupTables.Clip1(q1 - a2);
p[offset + step2] = WebpLookupTables.Clip1(q2 - a3);
}

[MethodImpl(InliningOptions.ShortMethod)]
Expand All @@ -992,7 +992,7 @@ private static bool NeedsFilter(Span<byte> p, int offset, int step, int t)
int p0 = p[offset - step];
int q0 = p[offset];
int q1 = p[offset + step];
return (4 * WebpLookupTables.Abs0[p0 - q0 + 255]) + WebpLookupTables.Abs0[p1 - q1 + 255] <= t;
return (4 * WebpLookupTables.Abs0(p0 - q0)) + WebpLookupTables.Abs0(p1 - q1) <= t;
}

private static bool NeedsFilter2(Span<byte> p, int offset, int step, int t, int it)
Expand All @@ -1007,14 +1007,14 @@ private static bool NeedsFilter2(Span<byte> p, int offset, int step, int t, int
int q1 = p[offset + step];
int q2 = p[offset + step2];
int q3 = p[offset + step3];
if ((4 * WebpLookupTables.Abs0[p0 - q0 + 255]) + WebpLookupTables.Abs0[p1 - q1 + 255] > t)
if ((4 * WebpLookupTables.Abs0(p0 - q0)) + WebpLookupTables.Abs0(p1 - q1) > t)
{
return false;
}

return WebpLookupTables.Abs0[p3 - p2 + 255] <= it && WebpLookupTables.Abs0[p2 - p1 + 255] <= it &&
WebpLookupTables.Abs0[p1 - p0 + 255] <= it && WebpLookupTables.Abs0[q3 - q2 + 255] <= it &&
WebpLookupTables.Abs0[q2 - q1 + 255] <= it && WebpLookupTables.Abs0[q1 - q0 + 255] <= it;
return WebpLookupTables.Abs0(p3 - p2) <= it && WebpLookupTables.Abs0(p2 - p1) <= it &&
WebpLookupTables.Abs0(p1 - p0) <= it && WebpLookupTables.Abs0(q3 - q2) <= it &&
WebpLookupTables.Abs0(q2 - q1) <= it && WebpLookupTables.Abs0(q1 - q0) <= it;
}

[MethodImpl(InliningOptions.ShortMethod)]
Expand All @@ -1024,7 +1024,7 @@ private static bool Hev(Span<byte> p, int offset, int step, int thresh)
int p0 = p[offset - step];
int q0 = p[offset];
int q1 = p[offset + step];
return WebpLookupTables.Abs0[p1 - p0 + 255] > thresh || WebpLookupTables.Abs0[q1 - q0 + 255] > thresh;
return WebpLookupTables.Abs0(p1 - p0) > thresh || WebpLookupTables.Abs0(q1 - q0) > thresh;
}

[MethodImpl(InliningOptions.ShortMethod)]
Expand Down
Loading