Lab 6: Image Manipulation
Download and unzip lab6.zip, which contains all
the files you will need for this assignment. Open the
file Lab6.java in DrJava. All the code you write for this
assignment should be added to this file.
Be sure to test your code after completing each exercise.
onlyRed
Copy a medium-sized image into your lab6 folder. (You can steal one
from the web.) In DrJava's interactions pane, load that image into a
new grid window, like this:
Grid.window("myimage.jpg");
You should see your image appear in a new window.
The onlyRed method has already been implemented for you.
Call it like this:
Lab6.onlyRed();
You should now see only the red parts of your image.
The onlyRed method modifies the image so as to keep only the
red values, setting the green and blue values to 0. Look at the code
for onlyRed and make sure you understand it.
negate
Complete the negate method, which should show the "negative"
of the current image. This means that every red, green, and blue
value should be replaced by 255 minus that value. For example, if the
color of a pixel in the original image is (25, 150, 210), then that
pixel's color should be xchanged to (230, 105, 45). So, if our image
originally looks like this:
and we call the negate method, then the window should now
contain the following image:

brightness
We can estimate how bright a particular color appears by averaging its
red, green, and blue components. Complete the brightness
method, which should return the average of the given 3 color
components.
grayScale
Complete the grayScale method, which should modify
the current image so that it now appears in shades of gray. In
order for a color to appear as a shade of gray, it must have equal
parts red, green, and blue. For example, the color (116, 116, 116)
will appear as a somewhat medium gray, and (222, 222, 222) will appear
as a very bright gray.
We'll need to replace each color in the original image with an
appropriate shade of gray. We'll call the brightness method
to determine how bright the original color was. This will tell us
what shade of gray to use. For example, if the original color is (20,
0, 100), then its average brightness is 40, so we'll replace that
color with (40, 40, 40), which will appear as a very dark gray. So,
if the current image is
and we call the grayScale, then the window should now contain
the following image:

reduceColors
Implement the method reduceColors, which takes in a value
called span, and reduces the number of colors in the picture.
This is done by examining each pixel's red, green, and blue
components, and dividing and then multiplying each of those values
by span. For example, if span is 63, and we're
examining a color with a red component value of 150, then we'll
compute 150 / 63 * 63, which will give us a new red component value of
126. (Make sure you understand why.) In fact, with a span
of 63, every color component value from 126 to 188 will be converted
into a component value of 126. (Do you see why?) Therefore, many
different colors will be converted into the same color, and we'll
therefore reduce the number of colors in the picture. And the larger
the span value, the fewer colors we'll have left in the picture.
Be sure to test your code using different span values. If
the original picture looked like this
and we use a span of 63, the resulting image will look like
this:

tint
Complete the tint method, which should multiply the color
components of every pixel in the current image by the corresponding
coefficients, making sure not to allow any color component to exceed
255. For example, if the color of a pixel in the original image is
(25, 150, 210), and we pass the coefficients 0.5, 1, and 2 to
tint, then the resulting color should be (12, 150, 255).
(Make sure you understand why.) So, if the original image looks like
this
and we call Lab6.tint(1.25, 0.75, 1) (to increase the red
parts, decrease the green parts, and leave the blue parts unchanged),
then the window should now contain the following image:

Challenge: flip
Write a method called flip that flips the image horizontally.
So, for example, if the window refers to the following image
and we call flip, then the window should now contain the
following image: