Question
This code will store messages consisting of uppercase letters and spaces in a picture. To achieve this, the 26 letters will be represented by numbers
This code will store messages consisting of uppercase letters and spaces in a picture. To achieve this, the 26 letters will be represented by numbers (1-26 where A = 1, etc.), a space will be represented using the value 27, and a 0 will represent the end of the string. In this way, messages will be able to be coded using 28 distinct integer values.
By encoding a message in this way, five bits will be needed to hold each letter or space. Recall that five bits can hold the decimal values ranging from 0 to 31 so there are even a few extra bits in case punctuation is desired. The desire is for the text to be truly hidden in the picture, so only the rightmost two bits in each color value of each pixel are used to store the coded message. Since there are three color components per pixel, each coded character will be split into three pairs of two bits where the leftmost bit is always 0. Again, more characters such as punctuation can be addedlater to the messages being stored. This six-bit scheme is convenient for storing in three colors, but also gives room to grow later if necessary.
Here's an example of the encoding of a message. Suppose "HELLO WORLD" was the message. Using the 28-integer code, it would be: 8, 5, 12, 12, 15, 27, 23, 15, 18, 12, 4, 0.
Edit and complete the following code
import java.awt.Color; import java.util.ArrayList; public class Steganography { /** * Clear the lower (rightmost) two bits in a pixel. * @param p pixel to be changed */ public static void clearLow(Pixel p) { p.setRed((p.getRed() / 4) * 4); p.setGreen((p.getGreen() / 4) * 4); p.setBlue((p.getBlue() / 4) * 4); } /** * Method to test clearLow on all pixels in a Picture * @param pic Picture to be changed * @return modified picture */ public static Picture testClearLow(Picture p) { Picture newPic = new Picture(p); Pixel[][] pixels = newPic.getPixels2D(); for (Pixel[] row : pixels) { for (Pixel col : row) { clearLow(col); } } return newPic; } /** * Set the lower 2 bits in a pixel to the highest 2 bits in c * @param p pixel to be changed * @param c color to be used for modification */ public static void setLow(Pixel p, Color c) { clearLow(p); int red = c.getRed() / 64; int green = c.getGreen() / 64; int blue = c.getBlue() / 64; p.setRed(p.getRed() + red); p.setGreen(p.getGreen() + green); p.setBlue(p.getBlue() + blue); } /** * Method to test setLow on all pixels in a Picture * @param pic picture to be changed * @param col color to be used in modification * @return modified picture */ public static Picture testSetLow(Picture p, Color c) { Picture newPic = new Picture(p); Pixel[][] pixels = newPic.getPixels2D(); for (Pixel[] row : pixels) { for (Pixel col : row) { setLow(col, c); } } return newPic; } /** * Sets the highest two bits of each pixel's colors * to the lowest two bits of each pixel's colors * @param hidden picture with hidden image * @return revealed picture */ public static Picture revealPicture(Picture hidden) { Picture copy = new Picture(hidden); Pixel[][] pixels = copy.getPixels2D(); Pixel[][] source = hidden.getPixels2D(); for (int r = 0; r < pixels.length; r++) { for (int c = 0; c < pixels[0].length; c++) { Color col = source[r][c].getColor(); Pixel p = pixels[r][c]; p.setRed(col.getRed() % 4 * 64); p.setGreen(col.getGreen() % 4 * 64); p.setBlue(col.getBlue() % 4 * 64); } } return copy; } /** * Determines whether secret can be hidden in source, which is * true if secret is same size or smaller than source * @param source is not null * @param secret is not null * @return true if secret can be hidden in source, false otherwise. */ public static Boolean canHide(Picture source, Picture secret) { //modify this code to hide text int sourceHeight = source.getHeight(); int sourceWidth = source.getWidth(); int secretHeight = secret.getHeight(); int secretWidth = secret.getWidth(); return (sourceHeight >= secretHeight && sourceWidth >= secretWidth); } /** * Creates a new Picture with data from secret hidden in data from source * @param source is not null * @param secret is not null * @return combined Picture with secret hidden in source * precondition: source is same width and height as secret */ public static Picture hidePicture(Picture source, Picture secret) { //copy your code from the Hiding and Revealing exercise. } /** * Hides secret in picture, starting at a given point in picture * @param source is not null * @param secret is not null * @param startRow row in source where hidden pic will start * @param startColumn column in source where hidden pic will start * @return combined Picture with secret hidden in source * precondition: source is same or greater width and height as secret */ public static Picture hidePicture(Picture source, Picture secret, int startRow, int startColumn) { Picture sourcePic = new Picture(source); Picture secretPic = new Picture(secret); Pixel[][] sourcePixels = sourcePic.getPixels2D(); Pixel[][] secretPixels = secretPic.getPixels2D(); Boolean hide = canHide(source, secret); if (hide) { for (int r = startRow, r2 = 0; r < sourcePixels.length && r2 < secretPixels.length; r++, r2++) { for (int c = startColumn, c2 = 0; c < sourcePixels[0].length - 1 && c2 < secretPixels[0].length; c++, c2++) { setLow(sourcePixels[r][c], secretPixels[r2][c2].getColor()); } } } return sourcePic; } /** * Checks to see if pic1 and pic2 match * @param pic1 first picture * @param pic2 second picture * @return true if pics match; false otherwise */ public static boolean isSame(Picture pic1, Picture pic2) { } /** * Returns arraylist of points where pictures differ * @param pic1 first picture * @param pic2 second picture * @return list of points where pic1 and pic2 differ or * returns empty list if they are not the same size */ public static ArrayList findDifferences(Picture pic1, Picture pic2) { } /** * Draws red rectangle around area containing points * @param pic source picture * @param points list of points * @return pic with rectangle drawn * pre-condition all of points are on the Picture pic * pre-condition - points contains at least two points */ public static Picture showDifferentArea (Picture pic, ArrayList points) { } //Add methods from the description here: /** * Hide a string (must be only capital letters and spacees) in a * picture. * The string always starts in the upper left corner. * @param source picture to hide string in * @param s string to hide * @return pic with hidden string */ public static Picture hideText(Picture source, String s) { } /** * Returns a string hidden in the picture * @param source picture with hidden string * @return revealed string */ public static String revealText(Picture source) { } public static void main(String[] args) { Picture beach = new Picture("./labs/steganography/beach.jpg"); beach.explore(); Picture hiddenText = hideText(beach,"THIS IS A TEST"); hiddenText.explore(); String reveal = revealText(hiddenText); if(reveal.equals("THIS IS A TEST")) { System.out.println("It worked!"); } else { System.out.println("Something went wrong!"); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started