private static void BilinearInterpolation <TPixelType>(Image <TPixelType> image, PixelInterpolationOperator <TPixelType> pixelOperator, int width, int height) { var originalImage = image.Copy(); image.Initialize(width, height); var rw = (double)originalImage.Width / image.Width; var rh = (double)originalImage.Height / image.Height; image.ForEach((x, y) => { var xrw = x * rw; var yrh = y * rh; var m = originalImage.ClampWidth((int)xrw); var n = originalImage.ClampWidth((int)yrh); var x0 = originalImage.ClampWidth(m); var y0 = originalImage.ClampHeight(n); var x1 = originalImage.ClampWidth(x0 + 1); var y1 = originalImage.ClampHeight(y0 + 1); var a = xrw % m; if (double.IsNaN(a)) { a = 0; } var b = yrh % n; if (double.IsNaN(b)) { b = 0; } var p00 = originalImage.Get(x0, y0); var p01 = originalImage.Get(x0, y1); var p10 = originalImage.Get(x1, y0); var p11 = originalImage.Get(x1, y1); var newPixel = pixelOperator(p00, p01, p10, p11, a, b); image.Set(x, y, newPixel); }); }
private static Image <TPixelType> Resize <TPixelType>(this Image <TPixelType> image, PixelInterpolationOperator <TPixelType> pixelOperator, int width, int height, ResizeMethod method) { switch (method) { case ResizeMethod.BilinearInterpolation: BilinearInterpolation(image, pixelOperator, width, height); break; case ResizeMethod.NearestNeighbour: image.Resize(width, height); break; } return(image); }