CS 160, Spring 2015
Programming Assignment P10
Unscrambling Images

Programming due Wednesday, Apr. 22 at 6:00pm; late deadline Apr. 22 at 11:59pm.

Using 2D Arrays to Write an Image Editor

This assignment has the goal of teaching you how to:
  1. Implement a set of methods that allow a GUI object to use your class.
  2. Instantiate and call a supplied class to read and write images.
  3. Declare and use 2D arrays to store images.
  4. Manipulate the data in 2D arrays to unscramble images.


The purpose of the assignment is to write a Java class that can be called by a user interface program to unscramble images in the Portable GreyMap (PGM) format. To do this you need to write a P10 object that inherits from an interface and implements all methods in that interface. It also instantiates and calls methods in PictureLibrary.java, and is in turn called from a graphical user interface (GUI) class called ImageProgram.java. Both of these are supplied below.


Part One

  1. Create a new project and class called P10, without a main method.
    Note: You may want to leverage code from the R16 recitation.
  2. Download and import PictureLibrary.java
  3. Download and import ImageProgram.java
  4. Download and import ImageInterface.java
  5. Make the class implement the ImageInterface interface, as shown in the lab and discussed in class.
  6. Do NOT modify any of the given files, just import them into your P10 src folder

Part Two

  1. At the top of the class, declare the following instance (non-static) variables:
    1. An object of type PictureLibrary, set to null
    2. An integer to store the image width, set to 0
    3. An integer to store the image height, set to 0
    4. A 2-dimensional array of integers to store the image data, not allocated
  2. Name your variables whatever you want
  3. Create a constructor for the P10 class as shown below that instantiates an object of type PictureLibrary into the associated class instance variable.
        public P10(){
            //Instantiate PictureLibrary object

Part Three

Implement the methods in the ImageInterface interface. All methods need to be created before your code will compile. The readImage method should call the readPGM method in the PictureLibrary object, passing the input file name, then it should call the getHeight, getWidth, and getData methods to fill in the class instance data defined above. The writeImage method should call the setData method in the Picture object with the image data, then call the writePGM method passing the output file name. The parameters and return types of the methods in PictureLibrary.java are not documented here, so you must look at the file to find them. The calls to readImage and writeImage should be wrapped in a try catch block as follows:
try {
	// Calls to readPGM or writePGM and associated code here
} catch (Exception e) {
Implement imageData by simply returning a copy of the image array. The remaining methods manipulate the image data in some way or another to restore an image that has been scrambled.

Part Four

Make sure that all methods exist in your P10.java, even though some of the transformations may not do anything. You should now be able to run the main program in ImageProgram.java to read and write PGM files. If you are having trouble integrating with the provided files, check that your method names and parameters match. We have provided a test file called Cam.pgm that you can download to the P10 project directory (not into the /src or /bin subdirectories).

Part Five

Implement the remaining methods as follows: NOTE: The maximum value of a pixel (PictureLibrary.MAXVAL) is 255, so only 8 bits are valid for each pixel. These are numbered bits 0-7, where bit 0 is equal to 1 and bit 7 is equal to 128. There are no negative values allowed.


The decode method can be tested with Decode.pgm. The swap method can be tested with Swap.pgm. The mirror method can be tested with Mirror.pgm. The exchange method can be tested with Exchange.pgm. In all cases, the restored image should be identical to Cam.pgm. After unscrambling an image you can write it to the disk, and compare it to Cam.pgm using the Linux diff command, thus all students can verify that their code is perfect before submission! NOTE: We may test your code with an image file that has a different size and contents than the provided test file, so do not hardcode anything.

Grading Criteria

Note: Do not modify the provided files in any way, or your program will not compile in our test system! Please follow the usual rules for submitting Java programs.


Submit your modified source file named P10.java to the the Checkin tab on the course web site.

© 2014 CS160 Colorado State University. All Rights Reserved.