public static byte[] filterGray(byte[] grayimg, int width, int height, int[] kernel, int x, int y) {
	   byte[] result = new byte[width * height];
	   int score = 0;
	   int span = x / 2;
	   int idx = 0;
	   int idx2 = 0;
	   int count = 0;
	   for (int row = 0; row < height; row++) {
		   for (int col = 0; col < width; col++) {
			   score = 0;
			   count = 0;
			   for (int m = 0; m < y; m++) {
				   for (int n = 0; n < x; n++) {
					   idx = (row - span + m) * width + col - span + n;
					   if (row - span + m >= 0 && row - span + m < height
							   && col - span + n >= 0 && col - span + n < width) 
					   {
						   idx2 = m * x + n;
						   score += (0xFF & grayimg[idx]) * kernel[idx2];
						   count += (0xFF & kernel[idx2]);
					   }
				   }
			   }
			   result[row * width + col] = (byte)(score / count);
		   }
	   }
	   return result;
   }