Change Contents of the Bubble
View this PageEdit this PageUploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

Midterm Exam 1 Review-Spring 2007: Posterize

Back to Midterm Exam 1 Review-Spring 2007

Questions? Answers? Comments?

  /**
   * Method to posterize (reduce the number of colors) in the picture
   * The number of reds, greens, and blues will be 4
   */
  public void newPosterize()
  {
    Pixel pixel = null;
    int redValue = 0;
    int greenValue = 0;
    int blueValue = 0;
    
    // loop through the pixels
    for (int x = 0; x < this.getWidth(); x++) {
      for (int y = 0; y < this.getHeight(); y++) {
        
        // get the current pixel and colors
        pixel = this.getPixel(x,y);
        redValue = pixel.getRed();
        greenValue = pixel.getGreen();
        blueValue = pixel.getBlue();
        
        // check for red range and change color
        if (redValue <= 128)
          redValue = 5;
        else 
          redValue = 255;
        
        // check for green range
        if (greenValue < 10)
          greenValue = 200;
        else if (greenValue < 200)
          greenValue = greenValue;
        else
          greenValue = 10;
        
        // check for blue range
        if (blueValue <=250)
          blueValue = 200;
        else
          blueValue = 201;
        
        // set the colors
        pixel.setRed(redValue);
        pixel.setGreen(greenValue);
        pixel.setBlue(blueValue);
      }
    }
  }
-Ashley W.

Sorry, I forgot to change the initial description that the number of reds, blues, and greens will be 4. However, the code should be correct...-Ashley

Prof. Guzdial, what specifically do we have to know about sound. I know there was mention of it in recitation but I do not see anything in the review. REMOVEDk lists, tracing code, writing code, ect.?
Why do the initial number or reds, blues, and green need to be 4? Is the code above correct? -Neal
  public void posterize()
  {
    Pixel pixel = null;
    int redValue = 0;
    int greenValue = 0;
    int blueValue = 0;
    
    // loop through the pixels
    for (int x = 0; x < this.getWidth(); x++) {
      for (int y = 0; y < this.getHeight(); y++) {
        
        // get the current pixel and colors
        pixel = this.getPixel(x,y);
        redValue = pixel.getRed();
        greenValue = pixel.getGreen();
        blueValue = pixel.getBlue();
        
        // check for red range and change color
        if (redValue > 128)
          redValue = 255;
        else 
	  redValue = 5;
        
        // check for green range
        if (greenValue < 10)
          greenValue = 200;
        else if (greenValue > 200)
          greenValue = 10;
        else
          greenValue = greenValue;
        
        // check for blue range
        if (blueValue <= 250)
          blueValue = 200;
        else if (blueValue > 251)
          blueValue = 201;
        
        // set the colors
        pixel.setRed(redValue);
        pixel.setGreen(greenValue);
        pixel.setBlue(blueValue);
      }
    }
  }

Darshini



Link to this Page