Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

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

Material Type: Lab; Professor: Summers; Class: Data Structures; Subject: Computer Science; University: Columbus State University; Term: Unknown 2007;

Typology: Lab Reports

Pre 2010

Uploaded on 08/04/2009

koofers-user-vx6-1
koofers-user-vx6-1 🇺🇸

4

(1)

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

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