public class GrayImage {
    public static final int BLACK = 0;
    public static final int WHITE = 255;

    private int[][] pixelValues;

    public GrayImage(int[][] pixelValues) {
        this.pixelValues = pixelValues;
    }

    public int countWhitePixels() {
        int whitePixelCount = 0;
        for (int[] row : this.pixelValues) { // iterating through rows
            for (int pv : row) {
                if (pv == this.WHITE) { // if pixel is white, increment the count
                    whitePixelCount++;
                }
            }
        }
        return whitePixelCount;
    }

    public void processImage() {
        // Loop through each pixel, excluding the last 2 rows and columns
        for (int row = 0; row < this.pixelValues.length - 2; row++) {
            for (int col = 0; col < this.pixelValues[0].length - 2; col++) {
                // Subtract pixel value from the corresponding pixel 2 rows and 2 columns away
                this.pixelValues[row][col] -= this.pixelValues[row + 2][col + 2];
                // If the result is less than black, set it to black
                if (this.pixelValues[row][col] < BLACK) {
                    this.pixelValues[row][col] = BLACK;
                }
            }
        }
    }

    public static void main(String[] args) {
        int[][] pixels = {
            {255, 0, 235, 0, 218},
            {17, 255, 186, 0, 255},
            {255, 0, 87, 255, 0},
            {0, 75, 255, 128, 255},
            {255, 38, 0, 0, 206}
        };
        
        GrayImage image = new GrayImage(pixels);

        System.out.println("White pixels before processing: " + image.countWhitePixels());

        image.processImage();

        System.out.println("White pixels after processing: " + image.countWhitePixels());
    }
}
GrayImage.main(null)
White pixels before processing: 8
White pixels after processing: 6

GrayImage Class:

  • This class represents a grayscale image with methods to count white pixels and process the image according to the specified rules. countWhitePixels Method:

  • This method iterates through each pixel in the pixelValues array. It checks if the pixel value is equal to WHITE (255). If a pixel is white, it increments the whitePixelCount variable. Finally, it returns the total count of white pixels in the image. processImage Method:

  • This method processes the image as described in the problem. It iterates through each pixel in the pixelValues array, excluding the last 2 rows and columns. For each pixel at position (row, col), it subtracts the value of the pixel at position (row + 2, col + 2) if such a position exists.

  • If the result of the subtraction is less than BLACK (0), it sets the pixel value to BLACK. This modifies the image according to the specified rules. main Method:

  • In the main method, a sample 2D array pixels is created to represent a grayscale image.

An instance of the GrayImage class is created using this array. The initial count of white pixels is printed using the countWhitePixels method. The processImage method is then called to modify the image. Finally, the count of white pixels after processing is printed again.