Skip to content

Commit d021222

Browse files
authored
Merge pull request #1800 from SixLabors/bp/avoiddictionarys
Webp: Use byte arrays instead of Dictionary's for lookups
2 parents 527e0fb + 414e4a8 commit d021222

File tree

2 files changed

+234
-57
lines changed

2 files changed

+234
-57
lines changed

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

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

944944
private static void DoFilter4(Span<byte> p, int offset, int step)
@@ -950,13 +950,13 @@ private static void DoFilter4(Span<byte> p, int offset, int step)
950950
int q0 = p[offset];
951951
int q1 = p[offset + step];
952952
int a = 3 * (q0 - p0);
953-
int a1 = WebpLookupTables.Sclip2[(a + 4) >> 3];
954-
int a2 = WebpLookupTables.Sclip2[(a + 3) >> 3];
953+
int a1 = WebpLookupTables.Sclip2[((a + 4) >> 3) + 112];
954+
int a2 = WebpLookupTables.Sclip2[((a + 3) >> 3) + 112];
955955
int a3 = (a1 + 1) >> 1;
956-
p[offsetMinus2Step] = WebpLookupTables.Clip1[p1 + a3];
957-
p[offset - step] = WebpLookupTables.Clip1[p0 + a2];
958-
p[offset] = WebpLookupTables.Clip1[q0 - a1];
959-
p[offset + step] = WebpLookupTables.Clip1[q1 - a3];
956+
p[offsetMinus2Step] = WebpLookupTables.Clip1[p1 + a3 + 255];
957+
p[offset - step] = WebpLookupTables.Clip1[p0 + a2 + 255];
958+
p[offset] = WebpLookupTables.Clip1[q0 - a1 + 255];
959+
p[offset + step] = WebpLookupTables.Clip1[q1 - a3 + 255];
960960
}
961961

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

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

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

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

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

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

10301030
[MethodImpl(InliningOptions.ShortMethod)]

0 commit comments

Comments
 (0)