LAB 17 – Conditionally Modifying Pixels (part 1 – Replacing Colors)

Topics

n      To replace one color with another in a picture

n      To conditionally execute a statement or block of statements using an if statement.

 

6.1 Conditional Pixel Changes

Type in the following code in the Program window (changing the comments):

 

import java.awt.Color;

/* Program that transforms pictures using loops and conditionals

 *

* Picture and Pixel classes are defined in bookClasses developed

 * at Georgia Tech by Mark Guzdial / Barbara Ericson

 *

 * @author Wayne Summers

 * @date   Sept. 23, 2007

 */

 

public class TransformPictures

{

  public static void main(String[] args)

  {

    String fileName1, fileName2;

    System.out.println(“Find the source file”);

    fileName1 = FileChooser.pickAFile();  // note that this pops up a window to select a jpg file

    Picture sourcePicture;

    sourcePicture = new Picture(fileName1);

 

// calls to methods that manipulate images go here

 

   sourcePicture.show(); 

  }

}

 

6.1.2 Replacing Colors

i) Type in the following code in the Program window below the main method and before the last }:

 

/**

  * Method that turns brown in a source picture into red

  * @param sourcePic - picture Object to be modified

*/

  public static void turnBrownIntoRed (Picture sourcePic)

  {

     Color brown = new Color(42, 25, 15);

     Pixel[] pixels = sourcePic.getPixels();

     Pixel pix= null;

     int len = pixels.length;

   

    // loop through the array of pixels in the picture

    for (int index = 0; index < len; index++) 

    {

          // get the current pixel

          pix = pixels[index];

          // if the color is near brown then double the amount of red

            if (pix.colorDistance(brown) < 50.0)

           {

              pix.setColor(new Color( (int) (pix.getRed() * 2.0), pix.getGreen(), pix.getBlue()) );

           } //end if

    } //end loop

  }

 

ii) Type in the following code in the Program window in the main method and before the pic.show();

 

turnBrownIntoRed (sourcePicture);

 

iii) Test your program with different pictures that have brown (e.g. Barbara.jpg).

 

6.1.3 Reducing Redeye

iv) Type in the following code in the Program window below the main method and before the last }:

 

/**

  * Method that removes the redeye from a source picture

  * @param sourcePic - picture Object to be modified

  * @param startX – starting x-coordinate of region to be checked

  * @param startY – starting y-coordinate of region to be checked

  * @param endX – ending x-coordinate of region to be checked

  * @param endY – ending y-coordinate of region to be checked

  * @param oldColor – color to be replaced

  * @param newColor – new color to be replaced with

  * @param distance – distance allowed from oldColor before color needs to be changed

 

*/

  public static void replaceColor (Picture sourcePic, int startX, int startY, int endX,

                                                            int endY, Color oldColor, Color newColor, double distance)

  {

    Pixel pixelObj = null;

   

    // loop through the pixels in the rectangle defined by the // startX, startY, and endX and endY

    for (int x = startX; x < endX; x++) 

    {

        for (int y = startY; y < endY; y++) 

        {

            // get the current pixel

            pixelObj = sourcePic.getPixel(x,y);

            // if the color is near red then change it

            if (pixelObj.colorDistance(oldColor) < distance)

           {

              pixelObj.setColor(newColor);

           } //end if

        } //end inner loop

    } //end outer loop

  }

 

v) Type in the following code in the Program window in the main method and before the pic.show();

 

replaceColor (sourcePicture, 109, 91, 202, 107, Color.red, Color.black, 167) and test with the picture jenny-red.jpg

 

vi) Test your program with other values as arguments sent to replaceColor [use either the explore method or the PixelViewer using Squeak in the MediaTools folder].

 

 

 

QUESTIONS:

 

Submit the .java file (with replaceColor where you set the arguments in the main method to values that will allow you to turn the green pixels in the bottom half of caterpillar.jpg from green to cyan) through the DropBox in WebCT.