private static unsafe double derivativeSNR(UnmanagedImage a, UnmanagedImage b)
{
int width = a.Width;
int height = a.Height;
int pixelSize = System.Drawing.Image.GetPixelFormatSize(a.PixelFormat) / 8;
int stride = a.Stride;
int offset = stride - a.Width * pixelSize;
byte* ptrA = (byte*)a.ImageData.ToPointer();
byte* ptrB = (byte*)b.ImageData.ToPointer();
double sum1 = 0;
double sum2 = 0;
for (int y = 0; y < height - 1; y++)
{
for (int x = 0; x < width - 1; x++, ptrA++, ptrB++)
{
// original image
int gradO = System.Math.Abs(*ptrA - ptrA[+stride]) + System.Math.Abs(*ptrA - ptrA[+1]);
sum1 += gradO * gradO;
// reconstructed image
int gradR = System.Math.Abs(*ptrB - ptrB[+stride]) + System.Math.Abs(*ptrB - ptrB[+1]);
sum2 += gradR * gradR;
}
ptrA += offset + 1;
ptrB += offset + 1;
}
if (sum2 == 0)
return 0;
return 10 * System.Math.Log10(sum1 / sum2);
}
}