# 8.7.2. Free Response - Gray Image B¶

The following is part b of a free response question from 2012. It was question 4 on the exam. You can see all the free response questions from past exams at https://apstudents.collegeboard.org/courses/ap-computer-science-a/free-response-questions-by-year.

Question 4. A grayscale image is represented by a 2-dimensional rectangular array of pixels (picture elements). A pixel is an integer value that represents a shade of gray. In this question, pixel values can be in the range from 0 through 255, inclusive. A black pixel is represented by 0, and a white pixel is represented by 255. The declaration of the GrayImage class is shown below.

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

/** The 2-dimensional representation of this image.
*  Guaranteed not to be null.
*  All values in the array are within the range
*  [BLACK, WHITE], inclusive.
*/
private int[][] pixelValues;

/** Processes this image in row-major order and
*  decreases the value of each pixel at position (row, col)
*  by the value of the pixel at position (row + 2, col + 2)
*  if it exists.
* Resulting values that would be less than BLACK are replaced
*   by BLACK.
* Pixels for which there is no pixel at
*   position (row + 2, col + 2) are unchanged.
*/
public void processImage()
{ /* to be implemented in part (b) */ }
}


Part b. Write the method processImage that modifies the image by changing the values in the instance variable pixelValues according to the following description. The pixels in the image are processed one at a time in row-major order. Row-major order processes the first row in the array from left to right and then processes the second row from left to right, continuing until all rows are processed from left to right. The first index of pixelValues represents the row number, and the second index represents the column number.

The pixel value at position (row, col) is decreased by the value at position (row + 2, col + 2) if such a position exists. If the result of the subtraction is less than the value BLACK, the pixel is assigned the value of BLACK. The values of the pixels for which there is no pixel at position (row + 2, col + 2) remain unchanged. You may assume that all the original values in the array are within the range [BLACK, WHITE], inclusive.

The following diagram shows the contents of the instance variable pixelValues before and after a call to processImage. The values shown in boldface represent the pixels that could be modified in a grayscale image with 4 rows and 5 columns.

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

/** The 2-dimensional representation of this image.
*  Guaranteed not to be null.
*  All values in the array are within the range
*  [BLACK, WHITE], inclusive.
*/
private int[][] pixelValues;

/** Processes this image in row-major order and
*  decreases the value of each pixel at position (row, col)
*  by the value of the pixel at position (row + 2, col + 2)
*  if it exists.
* Resulting values that would be less than BLACK are replaced
*   by BLACK.
* Pixels for which there is no pixel at
*   position (row + 2, col + 2) are unchanged.
*/
public void processImage()
{ /* to be implemented in part (b) */ }
}


## 8.7.2.1. How to solve this problem¶

Once again, this problem starts with looping through the array of pixels, using a nested for loop for the 2D array. As we loop we will need to subtract pixel values from one another.

When comparing our pixel values to values deeper in the array, we need to be careful to correctly set the terminating conditions on the for loops.

## 8.7.2.2. Algorithm¶

There are several ways to solve this problem, we will focus on two possible solutions below. It is not required for you to come up with both solutions but it would be good practice. If you wish to solve these exercises, click on the buttons to reveal them.

9-11-4: Explain in plain English what your code will have to do to answer this question. Use the variable names given above.

This set of questions will focus on a solution that starts indexing the array at zero, and stops 2 spaces before the end.

Next we will have questions focused on a solution that starts at the beginning of the loop and iterated through the entire thing, but checks for out of bounds errors as it subtracts.

## 8.7.2.3. Try and Solve It¶

FRQ Gray Image B: write the code for the method processImage. Please use row and col for your loop variables.

## 8.7.2.4. Video - One way to code the solution¶

The following video is also on YouTube at https://youtu.be/8j34xQkjsJI. It walks through coding a solution.