CS 101 - Intro to Computing, Spring 2012

Lab 8

Posterization

Posterization is a technique that reduces the number of colors used in a picture. During lecture we did examples of posterizing a picture to 2 colors, to 4 colors and to 8 colors.

The code to posterize is often a modification of the black & white (grayScale) code. Once the "grayAmount" has been determined for a pixel, we use that value in if statement(s) to determine which of the posterized colors that pixel will be set. The following shows that code for posterizing into 2 colors: black and white.

  // determine the grayAmount from the color at the pixel
  int grayAmount = (int) (red * 0.299 + green * 0.587 + blue * 0.114);

  // determine the final color value of the pixel
  if ( grayAmount < 128 )
  {
     // set the pixel to black
     red = 0;
     green = 0;
     blue = 0;
  }
  else
  {
     // set the pixel to white
     red = 255;
     green = 255;
     blue = 255;
  }

To posterize to more colors we just need to use nested if statements (often called "else-if" clauses). The following posterizes to 4 colors (black, blue, green and white). In this one the ranges for the grayAmount are:

  // determine the grayAmount from the color at the pixel
  int grayAmount = (int) (red * 0.299 + green * 0.587 + blue * 0.114);

  // determine the final color value of the pixel
  if ( grayAmount < 64 )
  {
     // set the pixel to black
     red = 0;
     green = 0;
     blue = 0;
  }
  else if ( grayAmount < 128 )
  {
     // set the pixel to blue
     red = 0;
     green = 0;
     blue = 255;
  }
  else if ( grayAmount < 192 )
  {
     // set the pixel to green
     red = 0;
     green = 255;
     blue = 0;
  }
  else
  {
     // set the pixel to white
     red = 255;
     green = 255;
     blue = 255;
  }

Lab Assignment

Due: Wednesday 3/7/2012 by 11:59 pm

For this lab assignment, you are to posterize a picture into 6 colors. We will use the "grayAmount" that was used when creating a black and white (grayscale) pricture to determine which of the six color values will be used.

Since our range for the "grayAmount" can vary from 0 to 255, we need to divide that range into 6 parts to distribute the six colors evenly throughout the picture.

We will use the following shades of blue for our 6 colors. The names come from the web page at: http://www.tayloredmktg.com/rgb/.

grayAmount
Range
0 - 42 43 - 84 85 - 127 128 - 170 171 - 212 213 - 255
Color
Value

Midnight Blue
r = 25
g = 25
b = 112

Dark Slate Blue
r = 72
g = 61
b = 139

Royal Blue
r = 65
g = 105
b = 225

Deep Sky Blue
r = 0
g = 191
b = 255

Pale Torquoise
r = 175
g = 238
b = 238

Light Cyan
r = 224
g = 255
b = 255

For this lab assignment, you are to write a java program that will complete the following:

  1. Prompt the user for an picture and open that picture.

  2. Call a method that will posterize the picture as described above. The picture opened in step 1 is to sent as a parameter to this method.

  3. Display the posterized picture.

  4. You must write your programs using good programming style which includes:

Submission of the Lab

The lab must be submitted electronically to the Assignment Link for Lab 8 inside of Blackboard. You will only need to submit the java source code file (the ".java" file). Please only submit source code file (the .java file, not the .class).