public static Int32[] BaseFilter(Int32[] pixels, ImageSize size, Func<Int32, Int32, Int32> func)
 {
     Func<Int32, Int32, bool> isValid = (y, x) => ((x > 0 && y > 0) && (x < size.X && y < size.Y));
     if ((size.X * size.Y) != pixels.LongLength)
         throw new ApplicationException();
     Int32[] outPixels = new Int32[pixels.Length];
     for (Int32 i = 0; i < size.Y; ++i)
     {
         for (Int32 j = 0; j < size.X; ++j)
         {
             Int32 min = pixels[i * size.X + j];
             for (Int32 i1 = -1; i1 <= 1; ++i1)
             {
                 for (Int32 j1 = -1; j1 <= 1; ++j1)
                 {
                     if (isValid(i + i1, j + j1))
                     {
                         min = func(pixels[(i + i1) * size.X + (j + j1)], min);
                     }
                 }
             }
             outPixels[i * size.X + j] = min;
         }
     }
     return outPixels;
 }
 public static Int32[] MinFilter(Int32[] pixels, ImageSize size)
 {
     return BaseFilter(pixels, size, Math.Min);
 }
 public static Int32[] MaxMinFilter(Int32[] pixels, ImageSize size)
 {
     isMin = !isMin;
     return (isMin ? MinFilter(pixels, size) : MaxFilter(pixels, size));
 }
 public Image(Int32[] pixels, ImageSize size)
 {
     Size = size;
     SetPixels(pixels);
 }