Colorado State University

Recitation R16 - 2D Arrays, Start P10
Spring 2015

CS160: Foundations in Programming


The purpose of this lab is to:

Phase 1

The purpose of the assignment is to write a Java class that can be called by a user interface to manipulate images in the Portable GreyMap (PGM) format. To do this you need to write an R16 object that implements an interface called RecitationInterface, and instantiates and calls methods in PictureLibrary.java, and is in turn called from a graphical user interface (GUI) class called RecitationProgram.java. All of these are supplied below.

Write the following program from scratch in the following phases:

Phase 2

  1. Create a new project and class called R16, without a main method.
  2. Download and import PictureLibrary.java
  3. Download and import RecitationProgram.java
  4. Download and import RecitationInterface.java
  5. Make the class implement the RecitationInterface interface, your TA will explain this.
  6. Do NOT modify any of the given files, just import them into your R16 src folder

Phase 3

  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 R16 class as shown below that instantiates an object of type PictureLibrary into the associated class instance variable.
        public R16(){
            //Instantiate PictureLibrary object
        }
        

Phase 4

Implement the methods declared in the interface, but put the code in R16.java!

readImage and writeImage

The readImage method should call the readPGM method on 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 PictureLibrary 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) {
	System.out.println(e.getMessage());
}

imageData

Implement imageData by simply returning a reference to the image array.

negateImage

Calling negateImage inverts each pixel by subtracting the pixel value from MAXVAL which is set in the PictureLibrary class. We suggest writing only these four methods first, then testing them before implementing the remaining transformations.

increaseContrast

Calling increaseContrast subtracts 16 from pixels with 0 <= value <= 127 and adds 16 to pixels with 128 <= value <= MAXVAL. Do not allow pixel values to overflow MAXVAL or become negative, i.e. clamp the pixels to 0 when subtracting and MAXVAL when adding. Your TA will explain what is meant by clamping.

decreaseContrast

Calling decreaseContrast adds 16 to pixels with 0 <= value <= 127 and subtracts 16 from pixels with 128 <= value <= MAXVAL. No clamping is necessary.

Phase 5 - Testing

We have provided a test file called Cam.pgm that you can download to the R16 project directory (not the src or bin). Test all of your methods by using the associated menu items in the user interface.
Submit your R16.java program to RamCT drop box to get credit for this lab. This program should make a good starting point for the P10 programming assignment.

© 2015 CS160 Colorado State University. All Rights Reserved.