## CSE 131 Module 2: Choice & Iteration

### Manipulating an Image Raster Using Iteration

Directions: In the imagetransforms package, modify the provided Transforms Java program to implement the methods as described below. Your methods will use iteration (either while loops or for loops) to operate on the pixels of a picture.

#### Notes

• To run this extension, right (control) click on Main and choose Run as... then Java Application.

This extension uses the same base code as the filter extension.

• The Picture class is a Sedgewick class whose API is documented on page 720 of our text. The coordinates are expressed using the standard in computer science graphics: the coordinate (x,y) denotes the pixel that is
• x pixels to the right of the leftmost edge of the picture
• y pixels down from the top edge of the picture
Thus, (0,0) is the top left corner of the picture.
• Pixel addressing follows the common Java convention: if there are w horizontal pixels, then 0 is the first (leftmost) and w-1 is the last (rightmost).
• Almost all of the methods are written in terms of two Picture parameters:
• source: the picture to be used as input to your transformation
• target: the picture area to be used as output from your transformation
There is just one exception: the gradient method produces its output without needing a source. Its only parameter is target.

• Each Picture has a width and a height, and these can be obtained for a Picture p as follows:
```  int width  = p.width();
int height = p.height();
```
• To find the Color of a pixel at location x,y, you use
```  Color c = source.get(x,y);
```
• To set the Color of a pixel at location x,y to the color c, you use
```  target.set(x,y,c);
```

#### Instructions

Each of the methods described below is found in the Transforms class.

1. The provided method flipHoriz(Picture source, Picture target) flips the image horizontally.

Look at the code given to you for this example carefully. It is broken into simple steps and the comments help explain why the pixel indexing works.

2. Complete the method flipVert(Picture source, Picture target) that flips the image vertically.
3. Complete the method flipHorizLeftHalf(Picture source, Picture target) that flips the left half of the image horizontally.
The left half of the target image should be same as the source, but the right half of the target image should be the mirror of the left half of the source.

4. Complete the method flipVertBotHalf(Picture source, Picture target) that flips the bottom half of the image vertically.
5. Complete the method gradient(Picture target) that takes a single Picture as a parameter. Your code should create a color gradient by computing the following for each pixel:
• The amount of red in each pixel increases gradually from 0 at the left edge of the image, to 255 at the right edge of the image.
• The amount of green in each pixel increases gradually from 0 at the top edge of the image, to 255 at the bottom edge of the image.
• The amount of blue in every pixel should be 128.
Thus, each pixel will have a different color depending on its position. For example, the pixel at the top left will have red=0, green=0, and blue=128. The pixel about 1/4 of the way down on the right edge will have red=255, green=64, and blue=128.
Develop an expression for the amount of red and green in each pixel, given the x and y position of that pixel and the width and height of the image:
```   int amountRed   = .....
int amountGreen = ....
```
Then set the pixel at (x,y) to a color based on those computations:
```    target.set(x, y, new Color(amountRed, amountGreen, 128));
```

Note There are other methods in the Transform class, but they are the subject of other extensions. Once you have completed the above work, demo and you are done with this extension.
When you done with this extension, you must be cleared by the TA to receive credit.
• Fill in the form below with the relevant information
• Have a TA check your work
• The TA should check your work and then fill in his or her name
• Click OK while the TA watches

If you worked in a team using a group– repository:
• Your work must be committed in that repository for this demo.
• Each team member must complete this form and have the TA authenticate.
For example, if there are two of you, then the form must be submitted twice, so that each person's last name and 6 digit ID is captured with the repository name.
• The repository name must be included on each of the submissions.

group– Enter the name of your group repository here
 Last name 1