Advertisement
Guest User

poopoo

a guest
Dec 31st, 2019
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.42 KB | None | 0 0
  1. import java.awt.*;
  2. import java.util.*;
  3.  
  4. public class Steganography
  5. {
  6.   public static void main(String args[])
  7.   {
  8. //    Picture beach = new Picture ("beach.jpg");
  9. //    beach.explore();
  10. //    Picture copy = testClearLow(beach);
  11. //    copy.explore();
  12. //    
  13. //    Picture beach2 = new Picture ("beach.jpg");
  14. //    beach2.explore();
  15. //    Picture copy2 = testSetLow(beach2, Color.PINK);
  16. //    copy2.explore();
  17. //    
  18. //    Picture copy3 = revealPicture(copy2);
  19. //    copy3.explore();
  20.    
  21.     Picture swan = new Picture ("swan.jpg");
  22.     Picture gorge = new Picture ("gorge.jpg");
  23.    
  24.     if(canHide(swan,gorge))
  25.     {
  26.      swan.explore();
  27.      Picture combined1 = hidePicture(swan,gorge);
  28.      combined1.explore();
  29.      Picture revealed = revealPicture(combined1);
  30.      revealed.explore();
  31.     }
  32.    
  33.   }
  34.  
  35.  public static void clearLow(Pixel p)
  36.  {
  37.    Color oldColor = p.getColor();
  38.    
  39.    p.setColor(new Color(4*(oldColor.getRed()/4),
  40.                         4*(oldColor.getGreen()/4),
  41.                         4*(oldColor.getBlue()/4)));
  42.    
  43.  }
  44.  
  45.  public static String tenToTwo(int num)
  46.  {
  47.   int quot = num;
  48.   int remain;
  49.  
  50.   String s = "";
  51.  
  52.   while(quot > 0)
  53.   {
  54.    remain = quot % 2;
  55.    quot /= 2;
  56.    
  57.    s = remain + "" + s;
  58.   }
  59.  
  60.   return s;
  61.  }
  62.  
  63.  public static Picture testClearLow(Picture pic)
  64.  {
  65.    Picture p = new Picture(pic);
  66.    Pixel [][] pixels = p.getPixels2D();
  67.    for(int r= 0; r< pixels.length; r++)
  68.    {
  69.      for(int c =0; c<pixels[0].length;c++)
  70.      {
  71.       clearLow(pixels[r][c]);
  72.      }
  73.    }
  74.    
  75.    return p;
  76.  }
  77.  
  78.  public static Picture testSetLow(Picture pic, Color col)
  79.  {
  80.    Picture p = new Picture(pic);
  81.    Pixel [][] pixels = p.getPixels2D();
  82.    
  83.    for(int r= 0; r< pixels.length; r++)
  84.    {
  85.      for(int c =0; c<pixels[0].length;c++)
  86.      {
  87.       setLow(pixels[r][c], col);
  88.      }
  89.    }
  90.    
  91.    return p;
  92.  }
  93.  
  94.  public static void setLow(Pixel p, Color c)
  95.  {
  96.    int pRed = (p.getRed()/16)*16;
  97.    int pGreen = (p.getGreen()/16)*16;
  98.    int pBlue = (p.getBlue()/16)*16;
  99.    
  100.    int cRed = c.getRed()/16;
  101.    int cGreen = c.getGreen()/16;
  102.    int cBlue = c.getBlue()/16;
  103.    
  104.    p.setColor(new Color((pRed+cRed),(pGreen+cGreen),(pBlue+cBlue)));
  105.  }
  106.  
  107.  /**
  108. * Sets the highest two bits of each pixel’s colors
  109. * to the lowest two bits of each pixel’s colors
  110. */
  111.  public static Picture revealPicture(Picture hidden)
  112.  {
  113.    Picture copy = new Picture(hidden);
  114.    Pixel[][] pixels = copy.getPixels2D();
  115.    Pixel[][] source = hidden.getPixels2D();
  116.    for (int r = 0; r < pixels.length; r++)
  117.    {
  118.      for (int c = 0; c < pixels[0].length; c++)
  119.      {
  120.        Color col = source[r][c].getColor();
  121.        Pixel p = pixels[r][c];
  122.        p.setRed(col.getRed() % 16 * 16);
  123.        p.setGreen(col.getGreen() % 16 * 16);
  124.        p.setBlue(col.getBlue() % 16 * 16);
  125.      }
  126.    }
  127.    return copy;
  128.    
  129.  }
  130.  
  131.  /**
  132. * Determines whether secret can be hidden in source, which is
  133. * true if source and secret are the same dimensions.
  134. * @param source is not null
  135. * @param secret is not null
  136. * @return true if secret can be hidden in source, false otherwise.
  137. */
  138. public static boolean canHide(Picture source, Picture secret)
  139. {
  140.   int sWidth = source.getWidth();
  141.   int sHeight = source.getHeight();
  142.  
  143.   int secWidth = secret.getWidth();
  144.   int secHeight = secret.getHeight();
  145.  
  146.   if(sWidth == secWidth && sHeight == secHeight)
  147.   {
  148.    return true;
  149.   }
  150.   else
  151.     return false;
  152. }
  153.  
  154. /**
  155. * Creates a new Picture with data from secret hidden in data from source
  156. * @param source is not null
  157. * @param secret is not null
  158. * @return combined Picture with secret hidden in source
  159. * precondition: source is same width and height as secret
  160. */
  161. public static Picture hidePicture(Picture source, Picture secret)
  162. {
  163.   Picture hidden = new Picture(source);
  164.   Pixel[][] hPixels = hidden.getPixels2D();
  165.   Pixel[][] sPixels = secret.getPixels2D();
  166.  
  167.   // Since the images are the same size
  168.   // either can be used for loop conditions
  169.   for(int r= 0; r< hPixels.length; r++)
  170.   {
  171.    for(int c = 0; c< hPixels[0].length; c++)
  172.    {
  173.     Pixel s = sPixels[r][c];
  174.     setLow(hPixels[r][c], s.getColor());
  175.    }
  176.   }
  177.   return hidden;
  178. }
  179.  
  180. //public static boolean isSame(Picture p1, Picture p2)
  181. //{
  182. //  if(p1.getHeight() != p2.getHeight() || p1.getWidth() != p2.getWidth())
  183. //    return false;
  184. //  for(int r =0; r<pixels1.length;r++)
  185. //  {
  186. //   for(int c = 0; c< pixels1[r].length;c++)
  187. //   {
  188. //     Pixel p1;
  189. //     Pixel p2;
  190. //     if(p1.getRed() != p2.getRed() ||
  191. //        p1.getGreen() != p2.getGreen() ||
  192. //        p1.getBlue() != p2.getBlue())
  193. //     {
  194. //       return false;
  195. //     }
  196. //    
  197. //   }
  198. //  }
  199. //  return true;
  200. //}
  201.  
  202. public static ArrayList<Point> findDifferences(Picture pic1, Picture pic2)
  203. {
  204.   ArrayList<Point> pointList = new ArrayList<Point>();
  205.  
  206.   Pixel[][] pixels1 = pic1.getPixels2D();
  207.   Pixel[][] pixels2 = pic2.getPixels2D();
  208.  
  209.   Pixel p1;
  210.   Pixel p2;
  211.  
  212.   if(pic1.getWidth() != pic2.getWidth() || pic1.getHeight() != pic2.getHeight())
  213.   {
  214.    return pointList;
  215.   }
  216.    
  217.   for(int r =0; r<pixels1.length;r++)
  218.   {
  219.    for(int c = 0; c< pixels1[r].length;c++)
  220.    {
  221.      p1 = pixels1[r][c];
  222.      p2 = pixels2[r][c];
  223.      
  224.      if(p1.getRed() != p2.getRed() ||
  225.         p1.getGreen() != p2.getGreen() ||
  226.         p1.getBlue() != p2.getBlue())
  227.      {
  228.       pointList.add(new Point(r,c));
  229.      }
  230.    }
  231.   }
  232.   return pointList;
  233. }
  234.  
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement