CS 101 - Intro to Computing, Spring 2008

Lab 13

Images Using a GrayScale

A black and white picture uses various shades of gray. These shades of grey all have the same amount of red, green and blue color. The higher the color value, the lighter the shade of gray is. The lower the color value, the darker the shade of gray is. For example, look at the following squares for various shades of gray. The amount of red, green and blue for the color is displayed as text in each square. Note that the amount of red, green and blue is the same in each square. Also note that the larger the amount of color, the lighter the color of gray.


r = 0
g = 0
b = 0

r = 63
g = 63
b = 63

r = 127
g = 127
b = 127

r = 191
g = 191
b = 191

r = 255
g = 255
b = 255

Changing the color in a pixel

The color in a pixel can be changed by using the setRed(), setGreen() and setBlue() functions in the Pixel class as part of the Java bookClasses.

Determining the amount of gray

One method to determine which grayscale color to use for a pixel when creating a black and white picture from a color picture is to average the amount of red, green and blue color the corresponding pixel in the colored picture. This method works fairly well and is fairly easy to understand and use. This way using the intensity of each color to determine the grayscale value.

Lab Assignment 13

Due: Tuesday 4/22/2007 by 11:59 pm

Write three Java programs that will do the following:

  1. Program 1: Make Black And White

    1. The program is to be named: lab13a
    2. Print out your name and your net-id
    3. Allow the user to select a picture from a file stored on the local machine
    4. Change the image to black and white (i.e. grayscale) using the average intensity method. You may use which ever method you wish. Note, the code is given in the book. This modification will need to be done for every pixel in the selected image. Refer to the java examples page for the examples from Lecture on 4/10/08. In particular the example Lect410c.java, you will see code that almost does what you need to do. Your loop should be patterned after the loop in the code.
    5. Display the modified image
    6. Save the modfied image to a file on the local machine

  2. Program 2: Make Black And Red

    1. The function is to be named: lab13b
    2. Print out your name and your net-id
    3. Allow the user to select a picture from a file stored on the local machine
    4. Change the image to a "red scale" image. This is done in a similar manner as the creation of a grayscale image, except the values for green and blue are kept at zero. Only the red value for each pixel will range from 0 to 255. Where the grayscale image is black, the red scale image will also be black. Where the grayscale image is white, the red scale image will be red. Where the grayscale image is gray, this image will range from black to red (the middle values will be a shade of maroon).
    5. Display the modified image
    6. Save the modfied image to a file on the local machine

  3. program 3: Make Red And White

    1. The function is to be named: lab13c
    2. Print out your name and your net-id
    3. Allow the user to select a picture from a file stored on the local machine
    4. Change the image to a version of a red scale image. This is done in a similar manner as the creation of a grayscale image, but with a twist. Where the grayscale image is black, this image will be red. Where the grayscale image is white, this image will also be white. Where the grayscale image is gray, this image will range from red to white (the middle values will be rather "pinky"). Look at the table below to determine which color value need to change and which one need to remain constant.
    5. Display the modified image
    6. Save the modfied image to a file on the local machine

    Some of the resulting colors will appear as:
    makeBlackAndWhite()

    r = 0
    g = 0
    b = 0

    r = 63
    g = 63
    b = 63

    r = 127
    g = 127
    b = 127

    r = 191
    g = 191
    b = 191

    r = 255
    g = 255
    b = 255
    makeBlackAndRed()

    r = 0
    g = 0
    b = 0

    r = 63
    g = 0
    b = 0

    r = 127
    g = 0
    b = 0

    r = 191
    g = 0
    b = 0

    r = 255
    g = 0
    b = 0
    makeRedAndWhite()

    r = 255
    g = 0
    b = 0

    r = 255
    g = 63
    b = 63

    r = 255
    g = 127
    b = 127

    r = 255
    g = 191
    b = 191

    r = 255
    g = 255
    b = 255

    Be sure to change comment with the name of the author to contain the following:

  4. You are also to submit the Java file electronically by using the UNIX turnin command.

    To use the UNIX turnin command to electronically hand-in your html file using the project name of lab13. To submit the files in lab13a.java, lab13b.java and lab13c.java for lab13, the turnin command is entered as:

         turnin -c cs101 -p lab13 lab13a.java lab13b.java lab13c.java
    
    To verify what you submitted using the turnin command type:
         turnin -c cs101 -p lab13 -v
    

There is a Finger Exercise based on this lab.

Grading Criteria