Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.util.*;
- public class Steganography
- {
- public static void main(String args[])
- {
- // Picture beach = new Picture ("beach.jpg");
- // beach.explore();
- // Picture copy = testClearLow(beach);
- // copy.explore();
- //
- // Picture beach2 = new Picture ("beach.jpg");
- // beach2.explore();
- // Picture copy2 = testSetLow(beach2, Color.PINK);
- // copy2.explore();
- //
- // Picture copy3 = revealPicture(copy2);
- // copy3.explore();
- Picture swan = new Picture ("swan.jpg");
- Picture gorge = new Picture ("gorge.jpg");
- if(canHide(swan,gorge))
- {
- swan.explore();
- Picture combined1 = hidePicture(swan,gorge);
- combined1.explore();
- Picture revealed = revealPicture(combined1);
- revealed.explore();
- }
- }
- public static void clearLow(Pixel p)
- {
- Color oldColor = p.getColor();
- p.setColor(new Color(4*(oldColor.getRed()/4),
- 4*(oldColor.getGreen()/4),
- 4*(oldColor.getBlue()/4)));
- }
- public static String tenToTwo(int num)
- {
- int quot = num;
- int remain;
- String s = "";
- while(quot > 0)
- {
- remain = quot % 2;
- quot /= 2;
- s = remain + "" + s;
- }
- return s;
- }
- public static Picture testClearLow(Picture pic)
- {
- Picture p = new Picture(pic);
- Pixel [][] pixels = p.getPixels2D();
- for(int r= 0; r< pixels.length; r++)
- {
- for(int c =0; c<pixels[0].length;c++)
- {
- clearLow(pixels[r][c]);
- }
- }
- return p;
- }
- public static Picture testSetLow(Picture pic, Color col)
- {
- Picture p = new Picture(pic);
- Pixel [][] pixels = p.getPixels2D();
- for(int r= 0; r< pixels.length; r++)
- {
- for(int c =0; c<pixels[0].length;c++)
- {
- setLow(pixels[r][c], col);
- }
- }
- return p;
- }
- public static void setLow(Pixel p, Color c)
- {
- int pRed = (p.getRed()/16)*16;
- int pGreen = (p.getGreen()/16)*16;
- int pBlue = (p.getBlue()/16)*16;
- int cRed = c.getRed()/16;
- int cGreen = c.getGreen()/16;
- int cBlue = c.getBlue()/16;
- p.setColor(new Color((pRed+cRed),(pGreen+cGreen),(pBlue+cBlue)));
- }
- /**
- * Sets the highest two bits of each pixel’s colors
- * to the lowest two bits of each pixel’s colors
- */
- 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() % 16 * 16);
- p.setGreen(col.getGreen() % 16 * 16);
- p.setBlue(col.getBlue() % 16 * 16);
- }
- }
- return copy;
- }
- /**
- * Determines whether secret can be hidden in source, which is
- * true if source and secret are the same dimensions.
- * @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)
- {
- int sWidth = source.getWidth();
- int sHeight = source.getHeight();
- int secWidth = secret.getWidth();
- int secHeight = secret.getHeight();
- if(sWidth == secWidth && sHeight == secHeight)
- {
- return true;
- }
- else
- return false;
- }
- /**
- * 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)
- {
- Picture hidden = new Picture(source);
- Pixel[][] hPixels = hidden.getPixels2D();
- Pixel[][] sPixels = secret.getPixels2D();
- // Since the images are the same size
- // either can be used for loop conditions
- for(int r= 0; r< hPixels.length; r++)
- {
- for(int c = 0; c< hPixels[0].length; c++)
- {
- Pixel s = sPixels[r][c];
- setLow(hPixels[r][c], s.getColor());
- }
- }
- return hidden;
- }
- //public static boolean isSame(Picture p1, Picture p2)
- //{
- // if(p1.getHeight() != p2.getHeight() || p1.getWidth() != p2.getWidth())
- // return false;
- // for(int r =0; r<pixels1.length;r++)
- // {
- // for(int c = 0; c< pixels1[r].length;c++)
- // {
- // Pixel p1;
- // Pixel p2;
- // if(p1.getRed() != p2.getRed() ||
- // p1.getGreen() != p2.getGreen() ||
- // p1.getBlue() != p2.getBlue())
- // {
- // return false;
- // }
- //
- // }
- // }
- // return true;
- //}
- public static ArrayList<Point> findDifferences(Picture pic1, Picture pic2)
- {
- ArrayList<Point> pointList = new ArrayList<Point>();
- Pixel[][] pixels1 = pic1.getPixels2D();
- Pixel[][] pixels2 = pic2.getPixels2D();
- Pixel p1;
- Pixel p2;
- if(pic1.getWidth() != pic2.getWidth() || pic1.getHeight() != pic2.getHeight())
- {
- return pointList;
- }
- for(int r =0; r<pixels1.length;r++)
- {
- for(int c = 0; c< pixels1[r].length;c++)
- {
- p1 = pixels1[r][c];
- p2 = pixels2[r][c];
- if(p1.getRed() != p2.getRed() ||
- p1.getGreen() != p2.getGreen() ||
- p1.getBlue() != p2.getBlue())
- {
- pointList.add(new Point(r,c));
- }
- }
- }
- return pointList;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement