Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have been given the assignment to: Steganography is a technique of encoding a text message into a picture. Create a Steganographer class. Write a

I have been given the assignment to:

Steganography is a technique of encoding a text message into a picture. Create a Steganographer class. Write a method for this class called "encode" that accepts a text filename, a Picture filename, and a target filename as arguments. The "encode" function should blend the text from the text file in a regulated manner to create a target Picture file. Write a function called "decode" that accepts an encoded Picture filename as an argument, decodes the Picture data, and prints the text to the user. Write a main method that demonstrates the encoding and decoding of a secret message.

Hint: The easy way to approach this is to cast your char values as ints (encoding), and then subsequently cast your int values as chars (decoding). By spreading your encoding across your images file, youll make it less likely that your message can be seen as a visual anomaly.

Question: I have created the Steganographer.java file (below). I was trying to create an encode function in the Steganographer.java file. Currently I get a compiler error. The error is cannot find symbol. symbol: method encode(), location: variable modifiedPicture of type Picture. I was starting the assignment by seeing if I could change the red value in the picture by a certain value from within the Steganographer class file using an encode function. I don't fully understand how to get the different classes to talk to each other. Below are the files you may need.

File Steganographer.java

import java.io.*;

public class Steganographer extends SimplePicture { // Write a function called "decode" that accepts an encoded Picture filename as an // argument, decodes the Picture data, and prints the text to the user.

public void encode() { Pixel[] pixelArray = this.getPixels(); Pixel pixel = null; int value = 0; int index = 0; //loop through all the pixels while (index < pixelArray.length) { //get the current pixel pixel = pixelArray[index]; //get the value value = pixel.getRed(); System.out.println("Red Value: " + value); //change the value of red by 1 value = ((int) (value)) + 1; System.out.println("Red Value Changed: " + value);

//set the red value to the changed red value pixel.setRed(value); //increment the index index++; } }

// Write a main method that demonstrates the encoding and decoding of a secret message. public static void main(String[] args) { // try { // /*File object, file reader object, BufferedReader object is created which reads the file secretmessage.txt*/ // File f = new File("C:/intro-prog-java/mediasources/secretmessage.txt"); // FileReader fr = new FileReader(f); // BufferedReader br = new BufferedReader(fr); // // //contents of file read are made array of characters // String m = br.readLine(); // char[] fc = m.toCharArray(); // // /*for loop is used to loop the characters of the File*/ // // for(int i=0; i<= fc.length-1; i++) // { // fc[i] = fc[i]; // // int value; // value = (int) fc[i]; // // System.out.println("counter: " + i + " " + value); // } // // /* File writer and print writer object are formed and used to write the manipulated text in the file*/ // // FileWriter fw = new FileWriter(f); // PrintWriter pw = new PrintWriter(fw); // pw.println(fc); // fw.close(); // fr.close(); // } // catch(IOException e){} //

//Assignment 6 // Test to encode String fName = "C:/intro-prog-java/mediasources/Donald.png"; //Original Picture Picture originalPicture = new Picture(fName); //Modified Picture Picture modifiedPicture = new Picture(fName); //Increase Red modifiedPicture.encode(); originalPicture.explore(); modifiedPicture.explore(); } } //end of Steganographer class

File SimplePicture.java

import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import javax.swing.ImageIcon; import java.awt.*; import java.io.*; import java.awt.geom.*;

/** * A class that represents a simple picture. A simple picture may have * an associated file name and a title. A simple picture has pixels, * width, and height. A simple picture uses a BufferedImage to * hold the pixels. You can show a simple picture in a * PictureFrame (a JFrame). * * Copyright Georgia Institute of Technology 2004 * @author Barb Ericson ericson@cc.gatech.edu */ public class SimplePicture implements DigitalPicture { /////////////////////// Fields ///////////////////////// /** * the file name associated with the simple picture */ private String fileName; /** * the title of the simple picture */ private String title; /** * buffered image to hold pixels for the simple picture */ private BufferedImage bufferedImage; /** * frame used to display the simple picture */ private PictureFrame pictureFrame; /** * extension for this file (jpg or bmp) */ private String extension; /////////////////////// Constructors ///////////////////////// /** * A Constructor that takes no arguments. All fields will be null. * A no-argument constructor must be given in order for a class to * be able to be subclassed. By default all subclasses will implicitly * call this in their parent's no argument constructor unless a * different call to super() is explicitly made as the first line * of code in a constructor. */ public SimplePicture() {this(200,100);} /** * A Constructor that takes a file name and uses the file to create * a picture * @param fileName the file name to use in creating the picture */ public SimplePicture(String fileName) { // load the picture into the buffered image load(fileName); } /** * A constructor that takes the width and height desired for a picture and * creates a buffered image of that size. This constructor doesn't * show the picture. * @param width the desired width * @param height the desired height */ public SimplePicture(int width, int height) { bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); title = "New Picture"; extension = "jpg"; setAllPixelsToAColor(Color.white); } /** * A Constructor that takes a picture to copy information from * @param copyPicture the picture to copy from */ public SimplePicture(SimplePicture copyPicture) { this.fileName = new String(copyPicture.fileName); this.title = new String(copyPicture.title); this.extension = copyPicture.extension; if (copyPicture.bufferedImage != null) { this.bufferedImage = new BufferedImage(copyPicture.getWidth(), copyPicture.getHeight(), BufferedImage.TYPE_INT_RGB); this.copyPicture(copyPicture); } } ////////////////////////// Methods ////////////////////////////////// /** * Method to get the extension for this picture * @return the extendsion (jpg or bmp) */ public String getExtension() { return extension; }

/** * Method that will copy all of the passed source picture into * the current picture object * @param sourcePicture the picture object to copy */ public void copyPicture(SimplePicture sourcePicture) { Pixel sourcePixel = null; Pixel targetPixel = null; // loop through the columns for (int sourceX = 0, targetX = 0; sourceX < sourcePicture.getWidth() && targetX < this.getWidth(); sourceX++, targetX++) { // loop through the rows for (int sourceY = 0, targetY = 0; sourceY < sourcePicture.getHeight() && targetY < this.getHeight(); sourceY++, targetY++) { sourcePixel = sourcePicture.getPixel(sourceX,sourceY); targetPixel = this.getPixel(targetX,targetY); targetPixel.setColor(sourcePixel.getColor()); } } } /** * Method to set the color in the picture to the passed color * @param color the color to set to */ public void setAllPixelsToAColor(Color color) { // loop through all x for (int x = 0; x < this.getWidth(); x++) { // loop through all y for (int y = 0; y < this.getHeight(); y++) { getPixel(x,y).setColor(color); } } } /** * Method to get the buffered image * @return the buffered image */ public BufferedImage getBufferedImage() { return bufferedImage; } /** * Method to get a graphics object for this picture to use to draw on * @return a graphics object to use for drawing */ public Graphics getGraphics() { return bufferedImage.getGraphics(); } /** * Method to get a Graphics2D object for this picture which can * be used to do 2D drawing on the picture */ public Graphics2D createGraphics() { return bufferedImage.createGraphics(); } /** * Method to get the file name associated with the picture * @return the file name associated with the picture */ public String getFileName() { return fileName; } /** * Method to get the title of the picture * @return the title of the picture */ public String getTitle() { return title; } /** * Method to set the title for the picture * @param title the title to use for the picture */ public void setTitle(String title) { this.title = title; pictureFrame.setTitle(title); } /** * Method to get the width of the picture in pixels * @return the width of the picture in pixels */ public int getWidth() { return bufferedImage.getWidth(); } /** * Method to get the height of the picture in pixels * @return the height of the picture in pixels */ public int getHeight() { return bufferedImage.getHeight(); } /** * Method to get the picture frame for the picture * @return the picture frame associated with this picture * (it may be null) */ public PictureFrame getPictureFrame() { return pictureFrame; } /** * Method to set the picture frame for this picture * @param pictureFrame the picture frame to use */ public void setPictureFrame(PictureFrame pictureFrame) { // set this picture objects' picture frame to the passed one this.pictureFrame = pictureFrame; } /** * Method to get an image from the picture * @return the buffered image since it is an image */ public Image getImage() { return bufferedImage; } /** * Method to return the pixel value as an int for the given x and y location * @param x the x coordinate of the pixel * @param y the y coordinate of the pixel * @return the pixel value as an integer (alpha, red, green, blue) */ public int getBasicPixel(int x, int y) { return bufferedImage.getRGB(x,y); } /** * Method to set the value of a pixel in the picture from an int * @param x the x coordinate of the pixel * @param y the y coordinate of the pixel * @param rgb the new rgb value of the pixel (alpha, red, green, blue) */ public void setBasicPixel(int x, int y, int rgb) { bufferedImage.setRGB(x,y,rgb); } /** * Method to get a pixel object for the given x and y location * @param x the x location of the pixel in the picture * @param y the y location of the pixel in the picture * @return a Pixel object for this location */ public Pixel getPixel(int x, int y) { // create the pixel object for this picture and the given x and y location Pixel pixel = new Pixel(this,x,y); return pixel; } /** * Method to get a one-dimensional array of Pixels for this simple picture * @return a one-dimensional array of Pixel objects starting with y=0 * to y=height-1 and x=0 to x=width-1. */ public Pixel[] getPixels() { int width = getWidth(); int height = getHeight(); Pixel[] pixelArray = new Pixel[width * height]; // loop through height rows from top to bottom for (int row = 0; row < height; row++) for (int col = 0; col < width; col++) pixelArray[row * width + col] = new Pixel(this,col,row); return pixelArray; }

/** * Method to load the buffered image with the passed image * @param image the image to use */ public void load(Image image) { // get a graphics context to use to draw on the buffered image Graphics2D graphics2d = bufferedImage.createGraphics(); // draw the image on the buffered image starting at 0,0 graphics2d.drawImage(image,0,0,null); // show the new image show(); } /** * Method to show the picture in a picture frame */ public void show() { // if there is a current picture frame then use it if (pictureFrame != null) pictureFrame.updateImageAndShowIt(); // else create a new picture frame with this picture else pictureFrame = new PictureFrame(this); } /** * Method to hide the picture */ public void hide() { if (pictureFrame != null) pictureFrame.setVisible(false); } /** * Method to make this picture visible or not * @param flag true if you want it visible else false */ public void setVisible(boolean flag) { if (flag) this.show(); else this.hide(); }

/** * Method to open a picture explorer on a copy of this simple picture */ public void explore() { // create a copy of the current picture and explore it new PictureExplorer(new SimplePicture(this)); } /** * Method to force the picture to redraw itself. This is very * useful after you have changed the pixels in a picture. */ public void repaint() { // if there is a picture frame tell it to repaint if (pictureFrame != null) pictureFrame.repaint(); // else create a new picture frame else pictureFrame = new PictureFrame(this); } /** * Method to load the picture from the passed file name * @param fileName the file name to use to load the picture from * @return true if success else false */ public boolean load(String fileName) { boolean result = true; // the default is an okay result // set the current piture's file name this.fileName = fileName; // set the extension int posDot = fileName.indexOf('.'); if (posDot >= 0) this.extension = fileName.substring(posDot + 1); // if the current title is null use the file name if (title == null) title = fileName; // try to get the buffered image from the file try { bufferedImage = ImageIO.read(new File(this.fileName)); /* if anything goes wrong tell the user on the console, set the * image to a black image with a message on it, and return false */ } catch (Exception ex) { System.out.println("Couldn't load the file " + this.fileName); bufferedImage = new BufferedImage(600,200, BufferedImage.TYPE_INT_RGB); addMessage("Couldn't load " + fileName,5,100); result = false; } return result; } /** * Method to draw a message as a string on the buffered image * @param message the message to draw on the buffered image * @param xPos the leftmost point of the string in x * @param yPos the bottom of the string in y */ public void addMessage(String message, int xPos, int yPos) { // get a graphics context to use to draw on the buffered image Graphics2D graphics2d = bufferedImage.createGraphics(); // set the color to white graphics2d.setPaint(Color.white); // set the font to Helvetica bold style and size 16 graphics2d.setFont(new Font("Helvetica",Font.BOLD,16)); // draw the message graphics2d.drawString(message,xPos,yPos); } /** * Method to create a new picture of the passed height. * The aspect ratio of the width and height will stay * the same. * @param height the desired height * @return the resulting picture */ public Picture getPictureWithHeight(int height) { // set up the scale tranform double yFactor = (double) height / this.getHeight(); AffineTransform scaleTransform = new AffineTransform(); scaleTransform.scale(yFactor,yFactor); // create a new picture object that is the right size Picture result = new Picture((int) (getWidth() * yFactor), (int) (getHeight() * yFactor)); // get the graphics 2d object to draw on the result Graphics graphics = result.getGraphics(); Graphics2D g2 = (Graphics2D) graphics; // draw the current image onto the result image scaled g2.drawImage(this.getImage(),scaleTransform,null); return result; } /** * Method to load a picture from a file name and show it in a picture frame * @param fileName the file name to load the picture from * @return true if success else false */ public boolean loadPictureAndShowIt(String fileName) { boolean result = true; // the default is that it worked // try to load the picture into the buffered image from the file name result = load(fileName); // show the picture in a picture frame show(); return result; } /** * Method to write the contents of the picture to a file with * the passed name * @param fileName the name of the file to write the picture to */ public void write(String fileName) { String extension = this.extension; // the default is current try { // create the file object File file = new File(fileName); // get the extension int posDot = fileName.indexOf('.'); if (posDot >= 0) extension = fileName.substring(posDot + 1); // write the contents of the buffered image to the file as jpeg ImageIO.write(bufferedImage, extension, file); } catch (Exception ex) { System.out.println("Couldn't write out the picture to the file " + fileName); } }

/** * Method to set the media path by setting the directory to use * @param directory the directory to use for the media path */ public static void setMediaPath(String directory) { FileChooser.setMediaPath(directory); } /** * Method to get the directory for the media * @param fileName the base file name to use * @return the full path name by appending * the file name to the media directory */ public static String getMediaPath(String fileName) { return FileChooser.getMediaPath(fileName); } /** * Method to get the coordinates of the enclosing rectangle after this * transformation is applied to the current picture * @return the enclosing rectangle */ public Rectangle2D getTransformEnclosingRect(AffineTransform trans) { int width = getWidth(); int height = getHeight(); double maxX = width - 1; double maxY = height - 1; double minX, minY; Point2D.Double p1 = new Point2D.Double(0,0); Point2D.Double p2 = new Point2D.Double(maxX,0); Point2D.Double p3 = new Point2D.Double(maxX,maxY); Point2D.Double p4 = new Point2D.Double(0,maxY); Point2D.Double result = new Point2D.Double(0,0); Rectangle2D.Double rect = null; // get the new points and min x and y and max x and y trans.deltaTransform(p1,result); minX = result.getX(); maxX = result.getX(); minY = result.getY(); maxY = result.getY(); trans.deltaTransform(p2,result); minX = Math.min(minX,result.getX()); maxX = Math.max(maxX,result.getX()); minY = Math.min(minY,result.getY()); maxY = Math.max(maxY,result.getY()); trans.deltaTransform(p3,result); minX = Math.min(minX,result.getX()); maxX = Math.max(maxX,result.getX()); minY = Math.min(minY,result.getY()); maxY = Math.max(maxY,result.getY()); trans.deltaTransform(p4,result); minX = Math.min(minX,result.getX()); maxX = Math.max(maxX,result.getX()); minY = Math.min(minY,result.getY()); maxY = Math.max(maxY,result.getY()); // create the bounding rectangle to return rect = new Rectangle2D.Double(minX,minY,maxX - minX + 1, maxY - minY + 1); return rect; } /** * Method to return a string with information about this picture * @return a string with information about the picture */ public String toString() { String output = "Simple Picture, filename " + fileName + " height " + getHeight() + " width " + getWidth(); return output; }

} // end of SimplePicture class

FIle Picture.java

import java.awt.*; import java.awt.font.*; import java.awt.geom.*; import java.text.*;

/** * A class that represents a picture. This class inherits from * SimplePicture and allows the student to add functionality to * the Picture class. * * Copyright Georgia Institute of Technology 2004-2005 * @author Barbara Ericson ericson@cc.gatech.edu */ public class Picture extends SimplePicture { ///////////////////// constructors ////////////////////////////////// /** * Constructor that takes no arguments */ public Picture () { /* not needed but use it to show students the implicit call to super() * child constructors always call a parent constructor */ super(); } /** * Constructor that takes a file name and creates the picture * @param fileName the name of the file to create the picture from */ public Picture(String fileName) { // let the parent class handle this fileName super(fileName); } /** * Constructor that takes the width and height * @param width the width of the desired picture * @param height the height of the desired picture */ public Picture(int width, int height) { // let the parent class handle this width and height super(width,height); } /** * Constructor that takes a picture and creates a * copy of that picture */ public Picture(Picture copyPicture) { // let the parent class do the copy super(copyPicture); } ////////////////////// methods /////////////////////////////////////// /** * Method to return a string with information about this picture. * @return a string with information about the picture such as fileName, * height and width. */ public String toString() { String output = "Picture, filename " + getFileName() + " height " + getHeight() + " width " + getWidth(); return output; } //decreaseRed Method public void decreaseRedForEach () { Pixel[] pixelArray = this.getPixels(); int value = 0; //loop through all the pixels in the array for (Pixel pixelObj : pixelArray) { //get the red value value = pixelObj.getRed(); //decrease the red value by 50% (1/2) value = (int) (value * 0.5); //set the red value of the current pixel to the new value pixelObj.setRed(value); } } //method to decrease Red by half in the current picture using a while loop public void decreaseRed1() { Pixel[] pixelArray = this.getPixels(); Pixel pixel = null; int value = 0; int index = 0; //loop through all the pixels in the array while (index < pixelArray.length) { //get the current pixel pixel = pixelArray[index]; //get the red value value = pixel.getRed(); //decrease the red value by 50% (1/2) value = (int) (value * 0.5); //set the red value of the current pixel to the new value pixel.setRed(value); //increment the index index = index + 1; } } public void drawBox(Color color, int topLeftX, int topLeftY, int width, int height) { //get the graphics context for drawing Graphics g = this.getGraphics(); //set the current color g.setColor(color); //draw the filled rectangle g.fillRect(topLeftX, topLeftY, width,height); }

/** * Method to increase the amount of red by 30% **/ public void increaseRed() { Pixel[] pixelArray = this.getPixels(); Pixel pixel = null; int value = 0; int index = 0; //loop through all the pixels while (index < pixelArray.length) { //get the current pixel pixel = pixelArray[index]; //get the value value = pixel.getRed(); //change the value to 1.3 times what it was value = (int) (value * 1.3); //set the red value to 1.3 time what it was pixel.setRed(value); //increment the index index++; } } /** * Method to increase the amount of red by an increase factor **/ public void increaseRed(double increaseFactor) { Pixel[] pixelArray = this.getPixels(); Pixel pixel = null; int value = 0; int index = 0; //loop through all the pixels while (index < pixelArray.length) { //get the current pixel pixel = pixelArray[index]; //get the value value = pixel.getRed(); //change the value to increase factor times what it was value = (int) (value * increaseFactor);

//set the red value to increase factor times what it was pixel.setRed(value); //increment the index index++; } } /** * Method to increase the amount of green by 30% **/ public void increaseGreen() { Pixel[] pixelArray = this.getPixels(); Pixel pixel = null; int value = 0; int index = 0; //loop through all the pixels while (index < pixelArray.length) { //get the current pixel pixel = pixelArray[index]; //get the value value = pixel.getGreen(); //change the value to 1.3 times what it was value = (int) (value * 1.3); //set the red value to 1.3 times what it was pixel.setGreen(value); //increment the index index++; } } /** * Method to increase the amount of green by an increase factor **/ public void increaseGreen(double increaseFactor) { Pixel[] pixelArray = this.getPixels(); Pixel pixel = null; int value = 0; int index = 0; //loop through all the pixels while (index < pixelArray.length) { //get the current pixel pixel = pixelArray[index]; //get the value value = pixel.getGreen(); //change the value to increase factor times what it was value = (int) (value * increaseFactor); //set the red value to increase factor times what it was pixel.setGreen(value); //increment the index index++; } } /** * Method to increase the amount of blue by 30% **/ public void increaseBlue() { Pixel[] pixelArray = this.getPixels(); Pixel pixel = null; int value = 0; int index = 0; //loop through all the pixels while (index < pixelArray.length) { //get the current pixel pixel = pixelArray[index]; //get the value value = pixel.getBlue(); //change the value to 1.3 times what it was value = (int) (value * 1.3); //set the red value to 1.3 times what it was pixel.setBlue(value); //increment the index index++; } } /** * Method to increase the amount of blue by an increase factor **/ public void increaseBlue(double increaseFactor) { Pixel[] pixelArray = this.getPixels(); Pixel pixel = null; int value = 0; int index = 0; //loop through all the pixels while (index < pixelArray.length) { //get the current pixel pixel = pixelArray[index]; //get the value value = pixel.getBlue(); //change the value to increase factor times what it was value = (int) (value * increaseFactor); //set the red value to increase factor times what it was pixel.setBlue(value); //increment the index index++; } }

/** * Method to create a new picture that is scaled up by the * passed number of times * @return the new scaled up picture */ public Picture scaleUp(int numTimes) { Picture targetPicture = new Picture(this.getWidth() * numTimes, this.getHeight() * numTimes); Pixel sourcePixel = null; Pixel targetPixel = null; int targetX = 0; int targetY = 0; // loop through the source picture columns for (int sourceX = 0; sourceX < this.getWidth(); sourceX++) { //loop through the source picture rows for(int sourceY=0; sourceY < this.getHeight(); sourceY++) { //get the source pixel sourcePixel = this.getPixel(sourceX, sourceY); //loop copying to the target y for (int indexY = 0; indexY < numTimes; indexY++) { //loop copying to the target x for (int indexX = 0; indexX < numTimes; indexX++) { targetX = sourceX * numTimes + indexX; targetY = sourceY * numTimes + indexY; targetPixel = targetPicture.getPixel(targetX, targetY); targetPixel.setColor(sourcePixel.getColor()); } } } } return targetPicture; } /** * Method to blur the pixels * @param numPixels the number of pixels to average in all * directions so if the numPixels is 2 then we will average * all pixels in the triangle defined by 2 before the * current pixel to 2 after the current pixel * */ public void blur(int startX, int endX, int startY, int endY) { Pixel pixel = null; Pixel samplePixel = null; int redValue = 0; int greenValue = 0; int blueValue = 0; int count = 0; int numPixels = 2; //loop through the pixels for (int x=startX; x < endX; x++){ for (int y=startY; y < endY; y++) { //get the current pixel pixel = this.getPixel(x,y); //reset the count and red, green and blue values count = 0; redValue = greenValue = blueValue = 0; /* loop through pixel numPixels before x to * numPixels after x * */ for (int xSample = x - numPixels; xSample <= x + numPixels; xSample++){ for (int ySample = y - numPixels; ySample <= y + numPixels; ySample++) { /* check that we are in the range of acceptable * pixels */ if (xSample >= 0 && xSample < this.getWidth() && ySample >= 0 && ySample < this.getHeight()) { samplePixel = this.getPixel(xSample, ySample); // System.out.println("samplePixel: " + samplePixel); redValue = redValue + samplePixel.getRed(); // System.out.println("redValue: " + redValue); greenValue = greenValue + samplePixel.getGreen(); //System.out.println("greenValue: " + greenValue); blueValue = blueValue + samplePixel.getBlue(); //System.out.println("blueValue: " + blueValue); count = count + 1; //System.out.println("count: " + count); } } } // use average color of surrounding pixels Color newColor = new Color(redValue / count, greenValue / count, blueValue / count); pixel.setColor(newColor); } } } /** * Method that will copy all of the passed source picture into * the current picture object starting with the left corner * given by xStart, yStart. * @param sourcePicture the picture object to copy * @param xStart the x position to start the copy into on the * target * @param yStart the y position to start the copy into on the * target */ public void copyPictureTo(Picture sourcePicture, int xStart, int yStart) { Pixel sourcePixel = null; Pixel targetPixel = null; // loop through the columns for (int sourceX = 0, targetX = xStart; sourceX < sourcePicture.getWidth(); sourceX++, targetX++) { //loop through the rows for (int sourceY = 0, targetY = yStart; sourceY < sourcePicture.getHeight(); sourceY++, targetY++) { sourcePixel = sourcePicture.getPixel(sourceX,sourceY); targetPixel = this.getPixel(targetX,targetY); targetPixel.setColor(sourcePixel.getColor()); } } } /** * Method to do chromakey using a blue background * @param newBg the new background image to use to replace * the blue from the current picture */ public void chromakey(Picture newBg) { Pixel[] pixelArray = this.getPixels(); Pixel currPixel = null; Pixel newPixel = null; //loop through the pixels for (int i = 0; i { //get the current pixel currPixel = pixelArray[i]; /* if the color at the current pixel is mostly blue * (blue value is greater than green and red * combined), then use new background color */ if (currPixel.getRed() + currPixel.getGreen() < currPixel.getBlue()) { newPixel = newBg.getPixel(currPixel.getX(), currPixel.getY()); currPixel.setColor(newPixel.getColor()); } } } /** * Method to do chromakey using a blue background * starting at specified coordinates to a background picture */ public void chromakeyBlue(Picture sourcePicture, int xStart, int yStart) { Pixel sourcePixel = null; Pixel targetPixel = null; // loop through the rows for (int sourceX = 1, targetX = xStart; sourceX < sourcePicture.getWidth(); sourceX++, targetX++) { //loop through the columns for (int sourceY = 1, targetY = yStart; sourceY < sourcePicture.getHeight(); sourceY++, targetY++) { sourcePixel = sourcePicture.getPixel(sourceX,sourceY); targetPixel = this.getPixel(targetX,targetY); /* if the color at the current pixel is mostly blue * (blue value is greater than green and red * combined), then use the new background color */ if (sourcePixel.getRed() + sourcePixel.getGreen() > sourcePixel.getBlue()) { targetPixel.setColor(sourcePixel.getColor()); } else { targetPixel.setColor(targetPixel.getColor()); } } } }

public static void main(String[] args) { //Assignment 3 // String fName = "C:/intro-prog-java/mediasources/mickey2.jpg"; // // /** RED **/ // //Test to increase red by 30% // // //Original Picture // Picture originalPictureRed = new Picture(fName); // // //Modified Picture // Picture modifiedPictureRed = new Picture(fName); // // //Increase Red by 30% // modifiedPictureRed.increaseRed(); // // originalPictureRed.explore(); // modifiedPictureRed.explore(); // // // //Test to increase red by factor // // //Original Picture // Picture originalPictureRed1 = new Picture(fName); // // //Modified Picture // Picture modifiedPictureRed1 = new Picture(fName); // // //Increase Red by factor 10% // modifiedPictureRed1.increaseRed(1.1); // // originalPictureRed1.explore(); // modifiedPictureRed1.explore(); // // // /** GREEN **/ // //Test to increase green by 30% // // //Original Picture // Picture originalPictureGreen = new Picture(fName); // // //Modified Picture // Picture modifiedPictureGreen = new Picture(fName); // // //Increase green by 30% // modifiedPictureGreen.increaseGreen(); // // originalPictureGreen.explore(); // modifiedPictureGreen.explore(); // // // //Test to increase green by factor // // //Original Picture // Picture originalPictureGreen1 = new Picture(fName); // // //Modified Picture // Picture modifiedPictureGreen1 = new Picture(fName); // // //Increase green by factor 1.5 // modifiedPictureGreen1.increaseGreen(1.5); // // originalPictureGreen1.explore(); // modifiedPictureGreen1.explore(); // // // /** BLUE **/ // //Test to increase blue by 30% // // //Original Picture // Picture originalPictureBlue = new Picture(fName); // // //Modified Picture // Picture modifiedPictureBlue = new Picture(fName); // // //Increase blue by 30% // modifiedPictureBlue.increaseBlue(); // // originalPictureBlue.explore(); // modifiedPictureBlue.explore(); // // // //Test to increase blue by factor 1.8 // // //Original Picture // Picture originalPictureBlue1 = new Picture(fName); // // //Modified Picture // Picture modifiedPictureBlue1 = new Picture(fName); // // //Increase blue by factor 1.8 // modifiedPictureBlue1.increaseBlue(1.8); // // originalPictureBlue1.explore(); // modifiedPictureBlue1.explore(); // // // /** BLURR **/ // // //Original Picture // Picture originalPictureBlur = new Picture(fName); // // //Modified Picture // Picture modifiedPictureBlur = new Picture(fName); // // //Rectangle to blur // modifiedPictureBlur.blur(6,7,5,6); // // originalPictureBlur.explore(); // modifiedPictureBlur.explore(); // // // /** Method of Choice copyPictureTo Method**/ // //Pictures to use in collage // String fName1 = "C:/intro-prog-java/mediasources/Donald.jpg"; // String fName2 = "C:/intro-prog-java/mediasources/goofy2.jpg"; // String fName3 = "C:/intro-prog-java/mediasources/Minnie5.jpg"; // String fName4 = "C:/intro-prog-java/mediasources/pluto5.jpg"; // String fName5 = "C:/intro-prog-java/mediasources/mickey4.jpg"; // String fName6 = "C:/intro-prog-java/mediasources/7inx95in.jpg"; // // Picture targetPicture = new Picture(fName6); // Picture mickeyPicture = new Picture(fName5); // Picture minniePicture = new Picture(fName3); // Picture goofyPicture = new Picture(fName2); // Picture donaldPicture = new Picture(fName1); // Picture plutoPicture = new Picture(fName4); // // //making the collage // targetPicture.copyPictureTo(plutoPicture,0,0); // targetPicture.show(); // targetPicture.copyPictureTo(goofyPicture,250,0); // targetPicture.show(); // targetPicture.copyPictureTo(mickeyPicture,0,375); // targetPicture.show(); // targetPicture.copyPictureTo(minniePicture,150,250); // targetPicture.show(); // targetPicture.copyPictureTo(donaldPicture,300,425); // targetPicture.show(); //Assignment 4 FileChooser.setMediaPath("C:/intro-prog-java/mediasources/"); String fName1 = "C:/intro-prog-java/mediasources/monster.jpg"; String fName2 = "C:/intro-prog-java/mediasources/mark-at-beach.jpg"; Picture sourcePicture = new Picture(fName2); Picture monsterPicture = new Picture(fName1); sourcePicture.chromakeyBlue(monsterPicture,270,60); sourcePicture.show(); sourcePicture.write("mark-with-monster-on-head.jpg"); } } // end of class Picture, put all new methods before this

File Digital Picture:

import java.awt.Image; import java.awt.image.BufferedImage;

/** * Interface to describe a digital picture. A digital picture can have a * associated file name. It can have a title. It has pixels * associated with it and you can get and set the pixels. You * can get an Image from a picture or a BufferedImage. You can load * it from a file name or image. You can show a picture. You can * create a new image for it. * * Copyright Georgia Institute of Technology 2004 * @author Barb Ericson ericson@cc.gatech.edu */ public interface DigitalPicture { public String getFileName(); // get the file name that the picture came from public String getTitle(); // get the title of the picture public void setTitle(String title); // set the title of the picture public int getWidth(); // get the width of the picture in pixels public int getHeight(); // get the height of the picture in pixels public Image getImage(); // get the image from the picture public BufferedImage getBufferedImage(); // get the buffered image public int getBasicPixel(int x, int y); // get the pixel information as an int public void setBasicPixel(int x, int y, int rgb); // set the pixel information public Pixel getPixel(int x, int y); // get the pixel information as an object public void load(Image image); // load the image into the picture public boolean load(String fileName); // load the picture from a file public void show(); // show the picture

File Pixel.java

import java.awt.Color;

/** * Class that references a pixel in a picture. A pixel has an x and y * location in a picture. A pixel knows how to get and set the red, * green, blue, and alpha values in the picture. A pixel also knows * how to get and set the color using a Color object. * * Copyright Georgia Institute of Technology 2004 * @author Barb Ericson ericson@cc.gatech.edu */ public class Pixel { ////////////////////////// fields /////////////////////////////////// /** the digital picture this pixel belongs to */ private DigitalPicture picture; /** the x location of this pixel in the picture (0,0) is top left */ private int x; /** the y location of this pixel in the picture (0,0) is top left */ private int y; ////////////////////// constructors ///////////////////////////////// /** * A constructor that take the x and y location for the pixel and * the picture the pixel is coming from * @param picture the picture that the pixel is in * @param x the x location of the pixel in the picture * @param y the y location of the pixel in the picture */ public Pixel(DigitalPicture picture, int x, int y) { // set the picture this.picture = picture; // set the x location this.x = x; // set the y location this.y = y; } ///////////////////////// methods ////////////////////////////// /** * Method to get the x location of this pixel. * @return the x location of the pixel in the picture */ public int getX() { return x; } /** * Method to get the y location of this pixel. * @return the y location of the pixel in the picture */ public int getY() { return y; } /** * Method to get the amount of alpha (transparency) at this pixel. * It will be from 0-255. * @return the amount of alpha (transparency) */ public int getAlpha() { /* get the value at the location from the picture as a 32 bit int * with alpha, red, green, blue each taking 8 bits from left to right */ int value = picture.getBasicPixel(x,y);

// get the alpha value (starts at 25 so shift right 24) // then and it with all 1's for the first 8 bits to keep // end up with from 0 to 255 int alpha = (value >> 24) & 0xff; return alpha; } /** * Method to get the amount of red at this pixel. It will be * from 0-255 with 0 being no red and 255 being as much red as * you can have. * @return the amount of red from 0 for none to 255 for max */ public int getRed() { /* get the value at the location from the picture as a 32 bit int * with alpha, red, green, blue each taking 8 bits from left to right */ int value = picture.getBasicPixel(x,y);

// get the red value (starts at 17 so shift right 16) // then and it with all 1's for the first 8 bits to keep // end up with from 0 to 255 int red = (value >> 16) & 0xff; return red; } /** * Method to get the red value from a pixel represented as an int * @param value the color value as an int * @return the amount of red */ public static int getRed(int value) { int red = (value >> 16) & 0xff; return red; } /** * Method to get the amount of green at this pixel. It will be * from 0-255 with 0 being no green and 255 being as much green as * you can have. * @return the amount of green from 0 for none to 255 for max */ public int getGreen() { /* get the value at the location from the picture as a 32 bit int * with alpha, red, green, blue each taking 8 bits from left to right */ int value = picture.getBasicPixel(x,y);

// get the green value (starts at 9 so shift right 8) int green = (value >> 8) & 0xff; return green; } /** * Method to get the green value from a pixel represented as an int * @param value the color value as an int * @return the amount of green */ public static int getGreen(int value) { int green = (value >> 8) & 0xff; return green; } /** * Method to get the amount of blue at this pixel. It will be * from 0-255 with 0 being no blue and 255 being as much blue as * you can have. * @return the amount of blue from 0 for none to 255 for max */ public int getBlue() { /* get the value at the location from the picture as a 32 bit int * with alpha, red, green, blue each taking 8 bits from left to right */ int value = picture.getBasicPixel(x,y);

// get the blue value (starts at 0 so no shift required) int blue = value & 0xff; return blue; } /** * Method to get the blue value from a pixel represented as an int * @param value the color value as an int * @return the amount of blue */ public static int getBlue(int value) { int blue = value & 0xff; return blue; } /** * Method to get a color object that represents the color at this pixel. * @return a color object that represents the pixel color */ public Color getColor() { /* get the value at the location from the picture as a 32 bit int * with alpha, red, green, blue each taking 8 bits from left to right */ int value = picture.getBasicPixel(x,y);

// get the red value (starts at 17 so shift right 16) // then and it with all 1's for the first 8 bits to keep // end up with from 0 to 255 int red = (value >> 16) & 0xff; // get the green value (starts at 9 so shift right 8) int green = (value >> 8) & 0xff; // get the blue value (starts at 0 so no shift required) int blue = value & 0xff; return new Color(red,green,blue); } /** * Method to set the pixel color to the passed in color object. * @param newColor the new color to use */ public void setColor(Color newColor) { // set the red, green, and blue values int red = newColor.getRed(); int green = newColor.getGreen(); int blue = newColor.getBlue(); // update the associated picture updatePicture(this.getAlpha(),red,green,blue); } /** * Method to update the picture based on the passed color * values for this pixel * @param alpha the alpha (transparency) at this pixel * @param red the red value for the color at this pixel * @param green the green value for the color at this pixel * @param blue the blue value for the color at this pixel */ public void updatePicture(int alpha, int red, int green, int blue) { // create a 32 bit int with alpha, red, green blue from left to right int value = (alpha << 24) + (red << 16) + (green << 8) + blue; // update the picture with the int value picture.setBasicPixel(x,y,value); } /** * Method to correct a color value to be within 0 and 255 * @param the value to use * @return a value within 0 and 255 */ private static int correctValue(int value) { if (value < 0) value = 0; if (value > 255) value = 255; return value; } /** * Method to set the red to a new red value * @param value the new value to use */ public void setRed(int value) { // set the red value to the corrected value int red = correctValue(value); // update the pixel value in the picture updatePicture(getAlpha(), red, getGreen(), getBlue()); } /** * Method to set the green to a new green value * @param value the value to use */ public void setGreen(int value) { // set the green value to the corrected value int green = correctValue(value); // update the pixel value in the picture updatePicture(getAlpha(), getRed(), green, getBlue()); } /** * Method to set the blue to a new blue value * @param value the new value to use */ public void setBlue(int value) { // set the blue value to the corrected value int blue = correctValue(value); // update the pixel value in the picture updatePicture(getAlpha(), getRed(), getGreen(), blue); } /** * Method to set the alpha (transparency) to a new alpha value * @param value the new value to use */ public void setAlpha(int value) { // make sure that the alpha is from 0 to 255 int alpha = correctValue(value); // update the associated picture updatePicture(alpha, getRed(), getGreen(), getBlue()); } /** * Method to get the distance between this pixel's color and the passed color * @param testColor the color to compare to * @return the distance between this pixel's color and the passed color */ public double colorDistance(Color testColor) { double redDistance = this.getRed() - testColor.getRed(); double greenDistance = this.getGreen() - testColor.getGreen(); double blueDistance = this.getBlue() - testColor.getBlue(); double distance = Math.sqrt(redDistance * redDistance + greenDistance * greenDistance + blueDistance * blueDistance); return distance; } /** * Method to compute the color distances between two color objects * @param color1 a color object * @param color2 a color object * @return the distance between the two colors */ public static double colorDistance(Color color1,Color color2) { double redDistance = color1.getRed() - color2.getRed(); double greenDistance = color1.getGreen() - color2.getGreen(); double blueDistance = color1.getBlue() - color2.getBlue(); double distance = Math.sqrt(redDistance * redDistance + greenDistance * greenDistance + blueDistance * blueDistance); return distance; } /** * Method to get the average of the colors of this pixel * @return the average of the red, green, and blue values */ public double getAverage() { double average = (getRed() + getGreen() + getBlue()) / 3.0; return average; } /** * Method to return a string with information about this pixel * @return a string with information about this pixel */ public String toString() { return "Pixel red=" + getRed() + " green=" + getGreen() + " blue=" + getBlue(); }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

What is meant by formal organisation ?

Answered: 1 week ago

Question

What is meant by staff authority ?

Answered: 1 week ago

Question

Discuss the various types of policies ?

Answered: 1 week ago

Question

Briefly explain the various types of leadership ?

Answered: 1 week ago

Question

Explain the need for and importance of co-ordination?

Answered: 1 week ago