Docsity

Conditionally Modifying Pixels of Data Structures - Lab 19 | CPSC 2108, Lab Reports of Data Structures and Algorithms

Toggle sidebar
bg1
LAB 19 – Conditionally Modifying Pixels (part 3 – Highlighting Extremes and Blurring)
Topics
To replace a range of colors with one color in a picture: posterizing.
To average nearby pixels: blur
To combine Boolean expressions with and and or.
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 TransformPictures3
{
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.4 Highlighting Extremes
i) Type in the following code in the Program window below the main method and before the last }:
/**
* Method to replace the pixel colors of the picture that have a color distance less than
* the passed amount of white or black with the passed replacement color
* @param pict - picture Object to be modified
* @param amount - distance from white or black
* @param replacementColor - the new color to use
*/
public static void highlightLightAndDark(Picture sourcePic, double amount, Color replacementColor)
{
Pixel pix = null;
int width = sourcePic.getWidth();
int height = sourcePic.getHeight();
// loop through the pixels
pf3

Partial preview of the text

Download Conditionally Modifying Pixels of Data Structures - Lab 19 | CPSC 2108 and more Lab Reports Data Structures and Algorithms in PDF only on Docsity!

LAB 19 – Conditionally Modifying Pixels (part 3 – Highlighting Extremes and Blurring)

Topics  To replace a range of colors with one color in a picture: posterizing.  To average nearby pixels: blur  To combine Boolean expressions with and and or.

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.4 Highlighting Extremes i) Type in the following code in the Program window below the main method and before the last }: /*
  • Method to replace the pixel colors of the picture that have a color distance less than
  • the passed amount of white or black with the passed replacement color
  • @param pict - picture Object to be modified
  • @param amount - distance from white or black
  • @param replacementColor - the new color to use */ public static void highlightLightAndDark(Picture sourcePic, double amount, Color replacementColor) { Pixel pix = null; int width = sourcePic.getWidth(); int height = sourcePic.getHeight(); // loop through the pixels

for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { // get the current pixel and color values pix = sourcePic.getPixel(x,y); // if the distance from white or black is less than passed amount // use the replacement color if (pix.colorDistance(Color.white) < amount || pix.colorDistance(Color.black) < amount) pix.setColor(replacementColor); } } } ii) Type in the following code in the Program window in the main method and before the pic.show(); highlightLightAndDark(sourcePicture, 50.0, Color.yellow); iii) Test your program with different pictures, values for the distance and colors (e.g. butterfly1.jpg, 50, yellow).

6. 5 Combining Pixels: Blurring iv) Type in the following code in the Program window below the main method and before the last }: /** * Method to blur the pixels of the picture * @param pict - picture Object to be modified * @param numPixels - number of pixels to average in all directions */ public static void blur(Picture sourcePic, int numPixels) { Pixel pix = null; Pixel samplePix = null; int width = sourcePic.getWidth(); int height = sourcePic.getHeight(); int redValue = 0; int greenValue = 0; int blueValue = 0; int count = 0; // loop through the pixels for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { // get the current pixel and color values pix = sourcePic.getPixel(x,y); // reset the count and red, green, and blue values count = 0; redValue = greenValue = blueValue = 0; // loop through pixel numPixels before x and after x and sum values of colors