# Extension : Extension 2.1: Image Processor With Iteration

Authors

## Manipulating an Image Raster

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. 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));`