CSCI 220
HOMEWORK ASSIGNMENT #4
Assigned Date: Monday, October 6, 2003
Due Date: Friday, October 17, 2003
Due Time: Noon

 

Purpose:

This assignment focuses on arrays and iteration.

Source filename to be submitted:  EnhancePGM.java

 

Documentation:

See instructions in the first homework assignment.

 

Introduction:

The NASA Viking Orbiter has sent back to Earth thousands of images of the Mars surface.  You have been hired by NASA to enhance these images to make the Mars planetary features more distinguishable.

Assignment: 

Write a program to read in and modify (enhance) images from the planet Mars. The images will be stored in individual files using the Portable GrayMap (PGM) format. 

A PGM file has the following format:

         P2

    #comment

    width height

    maxGrayValue

    width*height gray values (between 0 and maxGrayValue) in order by rows,
    separated by whitespace, no lines longer than 70 characters. 
    0 is black and maxGrayValue is white.

Here is an example:

         P2

    #CofC

    17 7

    15

    0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0

    0  3  3  3  0  0  0  0  0  0  0  0  0 15 15 15  0

    0  3  0  0  0  5  5  5  0  9  9  9  0 15  0  0  0

    0  3  0  0  0  5  0  5  0  9  0  0  0 15  0  0  0

    0  3  0  0  0  5  0  5  0  9  9  0  0 15  0  0  0

    0  3  3  3  0  5  5  5  0  9  0  0  0 15 15 15  0

    0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0

Note that each row is not required to fit on one line, or end in a newline.  Newlines may occur between any values.  The end of each row should be determined using the width value.

Your program should:

a)     Read the grayscale values from an arbitrary PGM file into an one-dimensional array of integers (using input redirection).  Make sure you use the StdIn.class provided on the class webpage (it allows you to read more than one integer off of a line as necessary).

b)    Find the maximum and minimum values actually occurring in the array (i.e., 0 and maxGrayValue may not actually occur in the array).

c)     Transform the array using the following formula:  shade = (shade - min) * (256 / (max - min)) , for each grayscale value (shade) in the array.

d)    Output the updated array values to a PGM file (using output redirection).  Be sure to output the appropriate header information before outputing the array.  Note that some of the information in the header may have changed as a result of the processing.  Also, output 15 values in each row.

Notes:

1)    You should use standard input and output for your input and output operations.  Use redirection from the MSDOS prompt to make your executable program read from and output to files as follows:

           % java EnhancePGM  <input_image.pgm  >output_image.pgm

where input_image.pgm is the name of the input file and output_image.pgm is the name of the output file (you may use whatever file names you desire).

2)    A set of images have been provided to you for testing. Use them to test your program.

Bonus Part:

Instead of steps a, b and c above, your program should:

a)     Read the grayscale values from an arbitrary PGM file into an two-dimensional array of integers (using input redirection).

b)    Make a histogram of the grayscale values that occur in the array.  For each grayscale value (from 0 to maxGrayValue) record how many times that value occurs in the array.  This histogram will be used in step c below. You should not output it.

c)     Multiply each value in the two-dimensional array by a scale factor, s_i, where i is the specific grayscale value.  The value of s_i for each greyscale value i is:

s_i = (1/n) * (n_0 + n_1 + n_2 + . . . + n_i)

where n is the total number of pixels (values in the array), and n_i is the histogram entry for gray level i (see step b).

Reference:

This assignment has been adapted from an assignment presented by Harriet Fell and Viera Proulx at SIGCSE '97.