|
Problem Set 9 Due on Tuesday, December 4 at the start of class |
split method in String classparseInt method in Integer classThe purpose of this problem set is to give you experience with arrays, I/O, and writing your own classes from scratch.
All code for this assignment is available in the
ps09_programs folder in the cs111d download
directory on the cs server.
Pic.java file.
ps09_programs folder. In particular,
the Pic subfolder should
contain your final version of Pic.java.
As discussed in Lecture #20, images can be represented in a computer as a 2-dimensional (2D) array of pixels. Pixels can be encoded in different ways, e.g., as Colors or as integers. For instance, two images are shown below along with representations of the two images as 2D arrays of pixels (one 16x16 pixels and one 8x8 pixels), where each pixel is encoded as an integer (0 encodes the Color Black, -1 encodes the Color White, and -65536 encodes the Color Red).
![]() |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
|
There are many different formats for storing images in files on a computer. Some popular formats
include JPEG, GIF, PNG, and BMP. For this task, we will be storing images in text files, though
you can easily convert back and forth between formats using operations found in the
PicOps class below. The text files we will use contain
2D integer array representations of an image. The first line of the text file must contain
two integers, separated by a single space " ", representing the height
(i.e., number of rows of pixels) in the image and the width (i.e., number of columns of pixels)
in the image, respectively. Following the first line, there are height further lines
in the text file, each containing width integers separated by a single space,
" ". Here are three examples of text files containing image representations:
hi.txt, checkerBoard.txt,
and MonaLisa.txt.
In this problem, you will write code to read in a file containing a 2D array of integers representing the pixels of an image. You will then perform various manipulations of the image, such as mirroring the image, cropping the image, shrinking the image, and rotating the image. For example, the figures below represent an image as well as mirrored, cropped, shrunk, and rotated versions of the image.
![]() |
![]() |
![]() |
![]() |
![]() |
| Original image | Mirrored image | Cropped image | Shrunk image | Rotated image |
One of the main aims of this problem is to give you practice writing a class from scratch.
Your task is to create from scratch your own Pic class to represent an image
and support various image manipulation operations. To do this, you will need to think
carefully about appropriate declarations in the Pic class, such as
instance variables, class variables, constructors, instance methods, and class methods.
Your program will be evaluated, not only on the basis of correctness, but also on
appropriateness of class declarations.
At a minimum, your Pic class should have the following 1 constructor,
9 instance methods, and 3 class method.
public Pic (String textFileName) throws IOExceptionPic object. The constructor should read in an image from the text file
specified by the String argument. The text file is assumed to contain a 2D integer
array representation of an image. Since the constructor, or some helper method
that it may invoke, reads in a file, the method may throw an IOException.
public int getHeight ()Examples:
getHeight() invoked on the HI image above returns 16.
getHeight() invoked on the checkerBoard image above returns 8.
getHeight() invoked on the MonaLisa image above returns 520.
getHeight() invoked on the Mt_Baker image below returns 333.
public int getWidth ()Examples:
getWidth() invoked on the HI image above returns 16.
getWidth() invoked on the checkerBoard image above returns 8.
getWidth() invoked on the MonaLisa image above returns 334.
getWidth() invoked on the Mt_Baker image below returns 500.
public int getPixel (int row, int col)row is in the range
[0, getHeight()-1] and col
is in the range [0, getWidth()-1].
Examples:
getPixel(1, 4) invoked on the HI image above returns -1.
getPixel(1, 4) invoked on the checkerBoard image above returns -65536.
getPixel(1, 4) invoked on the MonaLisa image above returns -11248061.
getPixel(1, 4) invoked on the Mt_Baker image below returns -9120807.
public void setPixel (int row, int col, int value)row is in the range
[0, getHeight()-1] and col
is in the range [0, getWidth()-1].
public int[][] getPixArray ()
public void mirror ()Example:
![]() |
![]() |
| Mt_Baker | mirror() |
public void crop (int row1, int col1, int row2, int col2)Example:
![]() |
![]() |
| Mt_Baker | crop(25, 150, 300, 350) |
public void shrink ()Example:
![]() |
![]() |
| Mt_Baker | shrink() |
public void rotate90 ()Example:
![]() |
![]() |
| Mt_Baker | rotate90() |
public static int[][] readInTextFile (String textFileName)
throws IOExceptionNotes:
split method in the String class
may be useful when reading in lines of a file.
parseInt method in the Integer
class may be useful for converting a String to an integer.
import java.io.*; at the top of your file.
readInTextFile() method, and you may not use the
readInTextFile() method provided in the PicOps
class. However, until you implement your readInTextFile()
method, to enable testing of other methods, it may be helpful to employ
the readInTextFile() method from the PicOps
class. In particular, the following line in your readInTextFile()
method will allow it to work correctly until you are able to implement
the method yourself:return PicOps.readInTextFile(textFileName);
public static void outputTextFile (String textFileName, int[][] pix)
throws IOExceptionNotes:
import java.io.*; at the top of your file.
outputTextFile() method, and you may not use the
outputTextFile() method provided in the PicOps
class. However, until you implement your outputTextFile()
method, to enable testing of other methods, it may be helpful to employ
the outputTextFile() method from the PicOps
class. In particular, the following line in your outputTextFile()
method will allow it to work correctly until you are able to implement
the method yourself (assuming that you have implemented the
getPixArray() method):PicOps.outputTextFile(textFileName, getPixArray());
public static void main (String[] args)
throws IOExceptionjava Pic display MonaLisa.txt
in the Interactions pane in Dr. Java, the main method
will be invoked, and any command line arguments (i.e., display
and MonaLisa.txt in the example above) will be referred to
by the String array parameter args in the main
method. The main method should process the command line arguments,
referred to by args, and perform the appropriate image operations.
The main method should process 13 different sets of command
line arguments:
display and the second
is the name of a file containing an image representation, then the program
should display the image in a new frame.
toTextFile and the second
is the name of an image file (e.g., JPEG, GIF, PNG, or BMP), then the program
should output the image to a text file containing a 2D integer array representation
of the image.
textFileToJPEG and the
second is the name of a text file containing an image representation, then the
program should output the image to a JPEG file.
getHeight and the second
is the name of a text file containing an image representation, then the program
should output the height (i.e., number of rows of pixels) of the image.
getWidth and the second
is the name of a text file containing an image representation, then the program
should output the width (i.e., number of columns of pixels) of the image.
getPixel, the second
is a String representing an integer row, the third is a String representing
an integer column, and the fourth is the name of a text file containing an
image representation, then the program should output the integer value of the
specified pixel in the image.
setPixel, the second
is a String representing an integer row, the third is a String representing
an integer column, the fourth is a String representing an integer value of
a pixel, and the fifth is the name of a text file containing an
image representation, then the program should display an image in a new frame,
where the displayed image corresponds to that in the text file, except that
the value of the specified pixel in the image has been set to the specified value.
getPixArray and the second
is the name of a text file containing an image representation, then the program
should create a new Pic object and display in a new frame the 2D integer array
returned by invoking getPixelArray on the new Pic object.
mirror, the second
is the name of a text file containing an image representation, and the third is
the name of a text file to which an image representation will be output, then
the program should display a mirrored version of the image represented by the
text file in the second command line argument and output the mirrored image
to the text file named by the third command line argument.
crop, the second
is a String representing an integer row of the upper left corner of a
rectangle, the third is a String representing an integer column of the upper
left corner of a rectangle, the fourth is a String representing an integer
row of the lower right corner of a rectangle, the fifth is a String
representing an integer column of the lower right corner of a rectangle, the
sixth is the name of a text file containing an image representation, and the
seventh is the name of a text file to which an image representation will be
output, then the program should display a cropped version of the image represented
by the text file in the sixth command line argument and output the cropped image
to the text file named by the seventh command line argument.
shrink, the second
is the name of a text file containing an image representation, and the third is
the name of a text file to which an image representation will be output, then
the program should display a shrunken version of the image represented by the
text file in the second command line argument and output the shrunken image
to the text file named by the third command line argument.
rotate90, the second
is the name of a text file containing an image representation, and the third is
the name of a text file to which an image representation will be output, then
the program should display a rotated version of the image represented by the
text file in the second command line argument and output the rotated image
to the text file named by the third command line argument.
readInTextFile and the second
is the name of a text file containing an image representation, then the program
should create a new Pic object corresponding to the image contained in the text
file and then display the image in a new frame.
For example, the figure below illustrates example executions of the Pic
program.

The final line
(java Pic display MonaLisa_rotate180_crop.jpg) in the examples
above would result in the following image being displayed.

Since the main method, or some helper method that it may invoke,
reads or writes to a file, the method may throw an IOException.
To aid you in designing and testing your Pic class, we
have provided you with a number of helpful methods in the PicOps class.
The methods in the PicOps class are all class methods and may be invoked,
e.g., by typing PicOps.display("MonaLisa.jpg"), either in the Dr. Java
Interactions pane or in your Pic class. The following 8 methods
are available in the PicOps class for you to use.
public static void display (String name, int[][] pix)public static void display (int[][] pix)public static void display (String imageFileName).txt) containing a 2D integer array
representation of an image.public static int[][] readInTextFile (String textFileName)
throws IOExceptionpublic static void outputTextFile (String textFileName, int[][] pix)
throws IOExceptionpublic static void toTextFile (String imageFileName)public static void textFileToJPEG (String textFileName)public static String getFolder ()Pic folder
in the ps09_programs folder (where all of your image files
should be stored). When working with the image
file "MonaLisa.jpg", for example, some computers, notably Macintosh
machines, search for the image file in the folder where Dr. Java resides
and not in the folder where your Java source files and class files
(e.g., Pic.java, PicOps.class) reside. Hence, if your image file is
located in the Pic folder in the ps09_programs
folder, it is useful to direct your program to the appropriate folder
when reading/writing your image file, e.g.,
PicOps.getFolder() + "MonaLisa.jpg"As an example of how these PicOps methods may be useful to you
when designing and testing your Pic class, consider the following
example strategy:
PicOps.toTextFile(PicOps.getFolder() + "buggle.jpg")Pic object representing the imagePic p1 = new Pic(PicOps.getFolder() + "buggle.txt")PicOps.display(p1.getPixArray())p1.mirror()PicOps.display(p1.getPixArray())outputTextFile (PicOps.getFolder() + "buggle_mirror.txt", p1.getPixArray())PicOps.textFileToJPEG(PicOps.getFolder() + "buggle_mirror.txt")To experiment with a working version of a solution to this problem,
run the application Pic_Solutions (provided in the Pic
folder in the ps09_programs folder) from the Dr. Java
Interactions pane with appropriate command line arguments, e.g.,
java Pic_Solutions display MonaLisa.txt. Your solution should behave similar to this
sample solution application.