Colorado State University

Recitation R21 - 2D Arrays, Start P7
Fall 2113

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 R21 object that instantiates and calls methods in Picture.java, and is in turn called from a graphical user interface (GUI) class called UserInterface.java. Both 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 R21, without a main method.
  2. At the top of the class, declare the following instance (non-static) variables:
    1. An object of type Picture, 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
  3. Name your variables whatever you want

Phase 3

  1. Download and import Picture.java
  2. Download and import UserInterface.java
  3. Do NOT modify either of the given files, just import them into your R21/src/ folder
  4. Create a constructor for the R21 class as shown below that instantiates an object of type Picture into the associated class instance variable.
        public R21(){
            //Instantiate Picture
        }
        

Phase 4

Write the following public (non-static) methods, which will be described below in more detail:
    public void readImage(String inFile) {} 

    public void writeImage(String outFile) { } 

    public int[][] imageData() { } 

    public void negateImage() { } 

    public void increaseBrightness() { } 

    public void decreaseBrightness() { } 

readImage & writeImage

The readImage method should call the readPGM method on the Picture 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 Picture.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 copy of the image array.

negateImage

Calling negateImage inverts each pixel by subtracting the pixel value from MAXVAL which is set in the Picture class. We suggest writing only these four methods first, then testing them before implementing the remaining transformations. We have provided a test file called Cam.pgm that you can download to the R21 project directory (not the src or bin).

increaseBrightness & decreaseBrightness

Calling increaseBrightness adds 32 to each pixel value, then clamps values greater than MAXVAL to MAXVAL. Calling decreaseBrightness subtracts 32 from each pixel value, then clamps values less than 0 to 0. Your TA will explain what is meant by clamping.

Phase 5 - Testing

We should be able test your code with an image file that has a different size and contents than the provided test file, so do not hardcode anything.
Show your R21.java program to the TA for grading and submit to RamCT to get credit for this lab.

© 2113 CS160 Colorado State University. All Rights Reserved.