Computer Science
15-111 (Sections A & B), Spring 2007
Homework #4.1
Due: Fri
02-Feb-2007 at 8:00pm (online submission).
There is no
written portion to this assignment, just the following code which should be
place in hw4.1.zip.
1. Image Editor
Note that our PixelImages.java file has been modified to
load a pixel array from a jpeg file (see the sample drawing which displays this
sampleImage.jpg, and the comment at the head
of the file, for more details).
Starting from this version of PixelImages.java, modify it by adding the
following methods:
public static int[][] rotateImage(int[][]
pixels, int degrees)
This method rotates the
image counterclockwise by the given
amount, where “degrees” must be a multiple of 90 (if not, you should round it
to the nearest multiple of 90, whether positive or negative). Note that rotating a non-square image will
change its dimensions (why?), which is not possible to do with an int[][] parameter
(again, why?), so this method does not modify the pixels array! Instead, it returns a new pixel array. Thus, it is
used as such:
// rotated 90 degrees counterclockwise
int[][] newPixels = rotateImage(pixels,90);
public static int[][] cropImage(int[][]
pixels,
int left,
int top,
int
width, int height)
This method does not
modify the pixels array itself, but returns a new pixel array of dimensions
(width x height) containing just those pixels in the given rectangle of the
source image.
public static int[][] zoomInImage(int[][]
pixels, int zoom)
This method does not
modify the pixels array itself, but returns a new pixel array of dimensions
(zoom * width, zoom * height), where width and height refer to the dimensions
of the source array, and where “zoom” is a positive integer (you do not have to
handle the negative or zero case). To
enlarge an image, each source pixel will be mapped to a zoom-by-zoom block of
pixels in the target. So, if zoom==3,
each source pixel would map to a 3x3 block of the same pixel in the target. Note that this may look a little “pixelated”,
and we could improve this by smoothing the transitions between pixel blocks,
but that is not necessary for this assignment.
public static int[][]
zoomOutImage(int[][] pixels, int zoom)
This method (which
essentially makes thumbnails, as we did in class!) does not modify the pixels
array itself, but returns a new pixel array of dimensions (width / zoom, height
/ zoom), where width and height refer to the dimensions of the source array,
and where “zoom” is a positive integer (you do not have to handle the negative
or zero case). Shrinking an image is the
opposite of enlarging it. Here, each
zoom-by-zoom block of source pixels is mapped to a single target pixel. To do this, take the average value of the alpha, red, green, and blue over the entire
source block, and use these values for the target pixel. Note that you must average by individual ARGB
component, and not by the entire “int” value!
public static void testHw4()
This method should read
in the sampleImage.jpg into a pixels array, then display 12 images as follows:
a) Display the unmodified jpeg;
b) Rotate the jpeg by 90, 180, and 270
degrees, displaying each result;
c) Crop the image to only display the
middle portion (those pixels not in the topmost or bottommost quarters when
divided vertically, nor in the leftmost or rightmost quarters when divided
horizontally), and display this result;
d) Zoom in by 2 times, 3 times, and 4
times, and display each result;
e) Zoom out by 2 times, 3 times, and 4
times, and display each result;
f) Redisplay the original jpeg,
demonstrating that it was unmodified throughout this method.