|
|
|
@ -23,23 +23,26 @@ namespace ImageSharp.Formats |
|
|
|
/// <param name="bytesPerScanline">The number of bytes per scanline</param>
|
|
|
|
/// <param name="bytesPerPixel">The bytes per pixel.</param>
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
|
|
public static void Decode(ref byte scanline, ref byte previousScanline, int bytesPerScanline, int bytesPerPixel) |
|
|
|
public static void Decode(BufferSpan<byte> scanline, BufferSpan<byte> previousScanline, int bytesPerScanline, int bytesPerPixel) |
|
|
|
{ |
|
|
|
ref byte scanPointer = ref scanline.DangerousGetPinnableReference(); |
|
|
|
ref byte prevPointer = ref previousScanline.DangerousGetPinnableReference(); |
|
|
|
|
|
|
|
// Paeth(x) + PaethPredictor(Raw(x-bpp), Prior(x), Prior(x-bpp))
|
|
|
|
for (int x = 1; x < bytesPerScanline; x++) |
|
|
|
{ |
|
|
|
if (x - bytesPerPixel < 1) |
|
|
|
{ |
|
|
|
ref byte scan = ref Unsafe.Add(ref scanline, x); |
|
|
|
ref byte above = ref Unsafe.Add(ref previousScanline, x); |
|
|
|
ref byte scan = ref Unsafe.Add(ref scanPointer, x); |
|
|
|
byte above = Unsafe.Add(ref prevPointer, x); |
|
|
|
scan = (byte)((scan + PaethPredicator(0, above, 0)) % 256); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
ref byte scan = ref Unsafe.Add(ref scanline, x); |
|
|
|
ref byte left = ref Unsafe.Add(ref scanline, x - bytesPerPixel); |
|
|
|
ref byte above = ref Unsafe.Add(ref previousScanline, x); |
|
|
|
ref byte upperLeft = ref Unsafe.Add(ref previousScanline, x - bytesPerPixel); |
|
|
|
ref byte scan = ref Unsafe.Add(ref scanPointer, x); |
|
|
|
byte left = Unsafe.Add(ref scanPointer, x - bytesPerPixel); |
|
|
|
byte above = Unsafe.Add(ref prevPointer, x); |
|
|
|
byte upperLeft = Unsafe.Add(ref prevPointer, x - bytesPerPixel); |
|
|
|
scan = (byte)((scan + PaethPredicator(left, above, upperLeft)) % 256); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -54,27 +57,31 @@ namespace ImageSharp.Formats |
|
|
|
/// <param name="bytesPerScanline">The number of bytes per scanline</param>
|
|
|
|
/// <param name="bytesPerPixel">The bytes per pixel.</param>
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
|
|
public static void Encode(ref byte scanline, ref byte previousScanline, ref byte result, int bytesPerScanline, int bytesPerPixel) |
|
|
|
public static void Encode(BufferSpan<byte> scanline, BufferSpan<byte> previousScanline, BufferSpan<byte> result, int bytesPerScanline, int bytesPerPixel) |
|
|
|
{ |
|
|
|
ref byte scanPointer = ref scanline.DangerousGetPinnableReference(); |
|
|
|
ref byte prevPointer = ref previousScanline.DangerousGetPinnableReference(); |
|
|
|
ref byte resultPointer = ref result.DangerousGetPinnableReference(); |
|
|
|
|
|
|
|
// Paeth(x) = Raw(x) - PaethPredictor(Raw(x-bpp), Prior(x), Prior(x - bpp))
|
|
|
|
result = 4; |
|
|
|
resultPointer = 4; |
|
|
|
|
|
|
|
for (int x = 0; x < bytesPerScanline; x++) |
|
|
|
{ |
|
|
|
if (x - bytesPerPixel < 0) |
|
|
|
{ |
|
|
|
ref byte scan = ref Unsafe.Add(ref scanline, x); |
|
|
|
ref byte above = ref Unsafe.Add(ref previousScanline, x); |
|
|
|
ref byte res = ref Unsafe.Add(ref result, x + 1); |
|
|
|
byte scan = Unsafe.Add(ref scanPointer, x); |
|
|
|
byte above = Unsafe.Add(ref prevPointer, x); |
|
|
|
ref byte res = ref Unsafe.Add(ref resultPointer, x + 1); |
|
|
|
res = (byte)((scan - PaethPredicator(0, above, 0)) % 256); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
ref byte scan = ref Unsafe.Add(ref scanline, x); |
|
|
|
ref byte left = ref Unsafe.Add(ref scanline, x - bytesPerPixel); |
|
|
|
ref byte above = ref Unsafe.Add(ref previousScanline, x); |
|
|
|
ref byte upperLeft = ref Unsafe.Add(ref previousScanline, x - bytesPerPixel); |
|
|
|
ref byte res = ref Unsafe.Add(ref result, x + 1); |
|
|
|
byte scan = Unsafe.Add(ref scanPointer, x); |
|
|
|
byte left = Unsafe.Add(ref scanPointer, x - bytesPerPixel); |
|
|
|
byte above = Unsafe.Add(ref prevPointer, x); |
|
|
|
byte upperLeft = Unsafe.Add(ref prevPointer, x - bytesPerPixel); |
|
|
|
ref byte res = ref Unsafe.Add(ref resultPointer, x + 1); |
|
|
|
res = (byte)((scan - PaethPredicator(left, above, upperLeft)) % 256); |
|
|
|
} |
|
|
|
} |
|
|
|
|