public int[][] getGrayLevels()
    {
        if (image == null) return new int[0][0];
        int[][] grayLevels = new int[getHeight()][getWidth()];
      
        for (int i = 0; i < grayLevels.length; i++)
            for (int j = 0; j < grayLevels[i].length; j++)
            {
                int rgb = image.getRGB(j, i);
                // Use NTSC/PAL algorithm to convert RGB to gray level
                grayLevels[i][j] = (int)(0.2989 * ((rgb >> 16) & 0xFF) + 0.5866 * ((rgb >> 8) & 0xFF) + 0.1144 * (rgb & 0xFF));	       
            }
        return grayLevels;
    }