Skip to content

Commit 911aafa

Browse files
authored
Merge branch 'master' into bp/reduceallocations
2 parents cf79e44 + 5a7c1f4 commit 911aafa

File tree

4 files changed

+267
-253
lines changed

4 files changed

+267
-253
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,11 @@ public int GetSigned(int v)
142142
[MethodImpl(InliningOptions.ShortMethod)]
143143
public bool ReadBool() => this.ReadValue(1) is 1;
144144

145+
[MethodImpl(InliningOptions.ShortMethod)]
145146
public uint ReadValue(int nBits)
146147
{
147-
Guard.MustBeGreaterThan(nBits, 0, nameof(nBits));
148-
Guard.MustBeLessThanOrEqualTo(nBits, 32, nameof(nBits));
148+
DebugGuard.MustBeGreaterThan(nBits, 0, nameof(nBits));
149+
DebugGuard.MustBeLessThanOrEqualTo(nBits, 32, nameof(nBits));
149150

150151
uint v = 0;
151152
while (nBits-- > 0)
@@ -156,10 +157,11 @@ public uint ReadValue(int nBits)
156157
return v;
157158
}
158159

160+
[MethodImpl(InliningOptions.ShortMethod)]
159161
public int ReadSignedValue(int nBits)
160162
{
161-
Guard.MustBeGreaterThan(nBits, 0, nameof(nBits));
162-
Guard.MustBeLessThanOrEqualTo(nBits, 32, nameof(nBits));
163+
DebugGuard.MustBeGreaterThan(nBits, 0, nameof(nBits));
164+
DebugGuard.MustBeLessThanOrEqualTo(nBits, 32, nameof(nBits));
163165

164166
int value = (int)this.ReadValue(nBits);
165167
return this.ReadValue(1) != 0 ? -value : value;

src/ImageSharp/Formats/Webp/BitReader/Vp8LBitReader.cs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ internal class Vp8LBitReader : BitReaderBase
2828
/// </summary>
2929
private const int Wbits = 32;
3030

31-
private readonly uint[] bitMask =
31+
private static readonly uint[] BitMask =
3232
{
3333
0,
3434
0x000001, 0x000003, 0x000007, 0x00000f,
@@ -125,19 +125,19 @@ public Vp8LBitReader(Stream inputStream, uint imageDataSize, MemoryAllocator mem
125125
/// </summary>
126126
/// <param name="nBits">The number of bits to read (should not exceed 16).</param>
127127
/// <returns>A ushort value.</returns>
128+
[MethodImpl(InliningOptions.ShortMethod)]
128129
public uint ReadValue(int nBits)
129130
{
130-
Guard.MustBeGreaterThan(nBits, 0, nameof(nBits));
131+
DebugGuard.MustBeGreaterThan(nBits, 0, nameof(nBits));
131132

132133
if (!this.Eos && nBits <= Vp8LMaxNumBitRead)
133134
{
134-
ulong val = this.PrefetchBits() & this.bitMask[nBits];
135+
ulong val = this.PrefetchBits() & BitMask[nBits];
135136
this.bitPos += nBits;
136137
this.ShiftBytes();
137138
return (uint)val;
138139
}
139140

140-
this.SetEndOfStream();
141141
return 0;
142142
}
143143

@@ -169,6 +169,7 @@ public bool ReadBit()
169169
/// <summary>
170170
/// Advances the read buffer by 4 bytes to make room for reading next 32 bits.
171171
/// </summary>
172+
[MethodImpl(InliningOptions.ShortMethod)]
172173
public void FillBitWindow()
173174
{
174175
if (this.bitPos >= Wbits)
@@ -181,14 +182,16 @@ public void FillBitWindow()
181182
/// Returns true if there was an attempt at reading bit past the end of the buffer.
182183
/// </summary>
183184
/// <returns>True, if end of buffer was reached.</returns>
184-
public bool IsEndOfStream() => this.Eos || ((this.pos == this.len) && (this.bitPos > Lbits));
185+
[MethodImpl(InliningOptions.ShortMethod)]
186+
public bool IsEndOfStream() => this.Eos || (this.pos == this.len && this.bitPos > Lbits);
185187

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

189191
/// <summary>
190192
/// If not at EOS, reload up to Vp8LLbits byte-by-byte.
191193
/// </summary>
194+
[MethodImpl(InliningOptions.ShortMethod)]
192195
private void ShiftBytes()
193196
{
194197
System.Span<byte> dataSpan = this.Data.Memory.Span;
@@ -199,17 +202,6 @@ private void ShiftBytes()
199202
++this.pos;
200203
this.bitPos -= 8;
201204
}
202-
203-
if (this.IsEndOfStream())
204-
{
205-
this.SetEndOfStream();
206-
}
207-
}
208-
209-
private void SetEndOfStream()
210-
{
211-
this.Eos = true;
212-
this.bitPos = 0; // To avoid undefined behaviour with shifts.
213205
}
214206
}
215207
}

src/ImageSharp/Formats/Webp/Lossy/LossyUtils.cs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -933,11 +933,11 @@ private static void DoFilter2(Span<byte> p, int offset, int step)
933933
int p0 = p[offset - step];
934934
int q0 = p[offset];
935935
int q1 = p[offset + step];
936-
int a = (3 * (q0 - p0)) + WebpLookupTables.Sclip1[p1 - q1 + 1020];
937-
int a1 = WebpLookupTables.Sclip2[((a + 4) >> 3) + 112];
938-
int a2 = WebpLookupTables.Sclip2[((a + 3) >> 3) + 112];
939-
p[offset - step] = WebpLookupTables.Clip1[p0 + a2 + 255];
940-
p[offset] = WebpLookupTables.Clip1[q0 - a1 + 255];
936+
int a = (3 * (q0 - p0)) + WebpLookupTables.Sclip1(p1 - q1);
937+
int a1 = WebpLookupTables.Sclip2((a + 4) >> 3);
938+
int a2 = WebpLookupTables.Sclip2((a + 3) >> 3);
939+
p[offset - step] = WebpLookupTables.Clip1(p0 + a2);
940+
p[offset] = WebpLookupTables.Clip1(q0 - a1);
941941
}
942942

943943
private static void DoFilter4(Span<byte> p, int offset, int step)
@@ -949,13 +949,13 @@ private static void DoFilter4(Span<byte> p, int offset, int step)
949949
int q0 = p[offset];
950950
int q1 = p[offset + step];
951951
int a = 3 * (q0 - p0);
952-
int a1 = WebpLookupTables.Sclip2[((a + 4) >> 3) + 112];
953-
int a2 = WebpLookupTables.Sclip2[((a + 3) >> 3) + 112];
952+
int a1 = WebpLookupTables.Sclip2((a + 4) >> 3);
953+
int a2 = WebpLookupTables.Sclip2((a + 3) >> 3);
954954
int a3 = (a1 + 1) >> 1;
955-
p[offsetMinus2Step] = WebpLookupTables.Clip1[p1 + a3 + 255];
956-
p[offset - step] = WebpLookupTables.Clip1[p0 + a2 + 255];
957-
p[offset] = WebpLookupTables.Clip1[q0 - a1 + 255];
958-
p[offset + step] = WebpLookupTables.Clip1[q1 - a3 + 255];
955+
p[offsetMinus2Step] = WebpLookupTables.Clip1(p1 + a3);
956+
p[offset - step] = WebpLookupTables.Clip1(p0 + a2);
957+
p[offset] = WebpLookupTables.Clip1(q0 - a1);
958+
p[offset + step] = WebpLookupTables.Clip1(q1 - a3);
959959
}
960960

961961
private static void DoFilter6(Span<byte> p, int offset, int step)
@@ -970,18 +970,18 @@ private static void DoFilter6(Span<byte> p, int offset, int step)
970970
int q0 = p[offset];
971971
int q1 = p[offset + step];
972972
int q2 = p[offset + step2];
973-
int a = WebpLookupTables.Sclip1[(3 * (q0 - p0)) + WebpLookupTables.Sclip1[p1 - q1 + 1020] + 1020];
973+
int a = WebpLookupTables.Sclip1((3 * (q0 - p0)) + WebpLookupTables.Sclip1(p1 - q1));
974974

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

987987
[MethodImpl(InliningOptions.ShortMethod)]
@@ -991,7 +991,7 @@ private static bool NeedsFilter(Span<byte> p, int offset, int step, int t)
991991
int p0 = p[offset - step];
992992
int q0 = p[offset];
993993
int q1 = p[offset + step];
994-
return (4 * WebpLookupTables.Abs0[p0 - q0 + 255]) + WebpLookupTables.Abs0[p1 - q1 + 255] <= t;
994+
return (4 * WebpLookupTables.Abs0(p0 - q0)) + WebpLookupTables.Abs0(p1 - q1) <= t;
995995
}
996996

997997
private static bool NeedsFilter2(Span<byte> p, int offset, int step, int t, int it)
@@ -1006,14 +1006,14 @@ private static bool NeedsFilter2(Span<byte> p, int offset, int step, int t, int
10061006
int q1 = p[offset + step];
10071007
int q2 = p[offset + step2];
10081008
int q3 = p[offset + step3];
1009-
if ((4 * WebpLookupTables.Abs0[p0 - q0 + 255]) + WebpLookupTables.Abs0[p1 - q1 + 255] > t)
1009+
if ((4 * WebpLookupTables.Abs0(p0 - q0)) + WebpLookupTables.Abs0(p1 - q1) > t)
10101010
{
10111011
return false;
10121012
}
10131013

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

10191019
[MethodImpl(InliningOptions.ShortMethod)]
@@ -1023,7 +1023,7 @@ private static bool Hev(Span<byte> p, int offset, int step, int thresh)
10231023
int p0 = p[offset - step];
10241024
int q0 = p[offset];
10251025
int q1 = p[offset + step];
1026-
return WebpLookupTables.Abs0[p1 - p0 + 255] > thresh || WebpLookupTables.Abs0[q1 - q0 + 255] > thresh;
1026+
return WebpLookupTables.Abs0(p1 - p0) > thresh || WebpLookupTables.Abs0(q1 - q0) > thresh;
10271027
}
10281028

10291029
[MethodImpl(InliningOptions.ShortMethod)]

0 commit comments

Comments
 (0)