|
|
@ -30,15 +30,16 @@ namespace SixLabors.ImageSharp.Formats.Png.Filters |
|
|
ref byte prevBaseRef = ref MemoryMarshal.GetReference(previousScanline); |
|
|
ref byte prevBaseRef = ref MemoryMarshal.GetReference(previousScanline); |
|
|
|
|
|
|
|
|
// Paeth(x) + PaethPredictor(Raw(x-bpp), Prior(x), Prior(x-bpp))
|
|
|
// Paeth(x) + PaethPredictor(Raw(x-bpp), Prior(x), Prior(x-bpp))
|
|
|
int offset = bytesPerPixel + 1; |
|
|
int offset = bytesPerPixel + 1; // Add one bcause x starts at one.
|
|
|
for (int x = 1; x < offset; x++) |
|
|
int x = 1; |
|
|
|
|
|
for (; x < offset; x++) |
|
|
{ |
|
|
{ |
|
|
ref byte scan = ref Unsafe.Add(ref scanBaseRef, x); |
|
|
ref byte scan = ref Unsafe.Add(ref scanBaseRef, x); |
|
|
byte above = Unsafe.Add(ref prevBaseRef, x); |
|
|
byte above = Unsafe.Add(ref prevBaseRef, x); |
|
|
scan = (byte)(scan + above); |
|
|
scan = (byte)(scan + above); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
for (int x = offset; x < scanline.Length; x++) |
|
|
for (; x < scanline.Length; x++) |
|
|
{ |
|
|
{ |
|
|
ref byte scan = ref Unsafe.Add(ref scanBaseRef, x); |
|
|
ref byte scan = ref Unsafe.Add(ref scanBaseRef, x); |
|
|
byte left = Unsafe.Add(ref scanBaseRef, x - bytesPerPixel); |
|
|
byte left = Unsafe.Add(ref scanBaseRef, x - bytesPerPixel); |
|
|
@ -70,39 +71,17 @@ namespace SixLabors.ImageSharp.Formats.Png.Filters |
|
|
// Paeth(x) = Raw(x) - PaethPredictor(Raw(x-bpp), Prior(x), Prior(x - bpp))
|
|
|
// Paeth(x) = Raw(x) - PaethPredictor(Raw(x-bpp), Prior(x), Prior(x - bpp))
|
|
|
resultBaseRef = 4; |
|
|
resultBaseRef = 4; |
|
|
|
|
|
|
|
|
#if OLD_AND_SLOW
|
|
|
|
|
|
for (int x = 0; x < scanline.Length; x++) |
|
|
|
|
|
{ |
|
|
|
|
|
if (x - bytesPerPixel < 0) |
|
|
|
|
|
{ |
|
|
|
|
|
byte scan = Unsafe.Add(ref scanBaseRef, x); |
|
|
|
|
|
byte above = Unsafe.Add(ref prevBaseRef, x); |
|
|
|
|
|
ref byte res = ref Unsafe.Add(ref resultBaseRef, x + 1); |
|
|
|
|
|
res = (byte)((scan - PaethPredicator(0, above, 0)) % 256); |
|
|
|
|
|
sum += res < 128 ? res : 256 - res; |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
byte scan = Unsafe.Add(ref scanBaseRef, x); |
|
|
|
|
|
byte left = Unsafe.Add(ref scanBaseRef, x - bytesPerPixel); |
|
|
|
|
|
byte above = Unsafe.Add(ref prevBaseRef, x); |
|
|
|
|
|
byte upperLeft = Unsafe.Add(ref prevBaseRef, x - bytesPerPixel); |
|
|
|
|
|
ref byte res = ref Unsafe.Add(ref resultBaseRef, x + 1); |
|
|
|
|
|
res = (byte)((scan - PaethPredicator(left, above, upperLeft)) % 256); |
|
|
|
|
|
sum += res < 128 ? res : 256 - res; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
#else
|
|
|
|
|
|
int x = 0; |
|
|
int x = 0; |
|
|
for (; x < bytesPerPixel; ++x) { |
|
|
for (; x < bytesPerPixel; /* Note: ++x happens in the body to avoid one add operation */) { |
|
|
byte scan = Unsafe.Add(ref scanBaseRef, x); |
|
|
byte scan = Unsafe.Add(ref scanBaseRef, x); |
|
|
byte above = Unsafe.Add(ref prevBaseRef, x); |
|
|
byte above = Unsafe.Add(ref prevBaseRef, x); |
|
|
ref byte res = ref Unsafe.Add(ref resultBaseRef, x + 1); |
|
|
++x; |
|
|
|
|
|
ref byte res = ref Unsafe.Add(ref resultBaseRef, x); |
|
|
res = (byte)(scan - PaethPredictor(0, above, 0)); |
|
|
res = (byte)(scan - PaethPredictor(0, above, 0)); |
|
|
sum += ImageMaths.FastAbs(unchecked((sbyte)res)); |
|
|
sum += ImageMaths.FastAbs(unchecked((sbyte)res)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
for (int xLeft = x - bytesPerPixel; x < scanline.Length; ++xLeft) { |
|
|
for (int xLeft = x - bytesPerPixel; x < scanline.Length; ++xLeft /* Note: ++x happens in the body to avoid one add operation */) { |
|
|
byte scan = Unsafe.Add(ref scanBaseRef, x); |
|
|
byte scan = Unsafe.Add(ref scanBaseRef, x); |
|
|
byte left = Unsafe.Add(ref scanBaseRef, xLeft); |
|
|
byte left = Unsafe.Add(ref scanBaseRef, xLeft); |
|
|
byte above = Unsafe.Add(ref prevBaseRef, x); |
|
|
byte above = Unsafe.Add(ref prevBaseRef, x); |
|
|
@ -112,7 +91,6 @@ namespace SixLabors.ImageSharp.Formats.Png.Filters |
|
|
res = (byte)(scan - PaethPredictor(left, above, upperLeft)); |
|
|
res = (byte)(scan - PaethPredictor(left, above, upperLeft)); |
|
|
sum += ImageMaths.FastAbs(unchecked((sbyte)res)); |
|
|
sum += ImageMaths.FastAbs(unchecked((sbyte)res)); |
|
|
} |
|
|
} |
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
sum -= 4; |
|
|
sum -= 4; |
|
|
} |
|
|
} |
|
|
|