Advertisement
Guest User

XOR Images

a guest
Jan 6th, 2014
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.63 KB | None | 0 0
  1. package com.ignatieff.xormask;
  2.  
  3. import java.awt.image.BufferedImage;
  4. import java.io.File;
  5. import java.io.IOException;
  6.  
  7. import javax.imageio.ImageIO;
  8.  
  9. public class Encoder {
  10.    
  11.     /**
  12.      * This function loads an image from the system, given a path.
  13.      * @param path The path to load
  14.      * @return A BufferedImage representing the file on the system.
  15.      * @throws IOException May throw IOException if the path is empty or the file is protected.
  16.      */
  17.     public static BufferedImage loadImage(String path) throws IOException{
  18.         return ImageIO.read(new File(path));
  19.     }
  20.    
  21.     /**
  22.      * XOR two images together, given their paths on the file system.
  23.      * @param path_of_image Path of the first image.
  24.      * @param path_of_key Path of the second image.
  25.      * @return The XOR'ed representation of the two images.
  26.      */
  27.     public static BufferedImage XORImages(String path_of_image, String path_of_key){
  28.         try {
  29.             return XORImages(clipImages(
  30.                     loadImage(path_of_image),
  31.                     loadImage(path_of_key)));
  32.         } catch (IOException e) {
  33.             System.out.println("[ERROR]: " + e.getMessage());
  34.             return null;
  35.         }
  36.     }
  37.    
  38.     /**
  39.      * XOR two images together, and saves the result.
  40.      * @param path_of_image Path of the first image.
  41.      * @param path_of_key Path of the second image.
  42.      * @param path_of_result Path of the result.
  43.      */
  44.     public static void convertImage(String path_of_image, String path_of_key, String path_of_result){
  45.         BufferedImage result = XORImages(path_of_image, path_of_key);
  46.         try {
  47.             saveImage(result, path_of_result);
  48.         } catch (IOException e) {
  49.             System.out.println("[ERROR]: " + e.getMessage());
  50.         }
  51.     }
  52.    
  53.     /**
  54.      * Saves a BufferedImage on the system.
  55.      * @param image The image to save.
  56.      * @param path The path of the image.
  57.      * @throws IOException If the path is protected, the program may throw an IOException.
  58.      */
  59.     public static void saveImage(BufferedImage image, String path) throws IOException{
  60.         ImageIO.write(image, "png", new File(path));
  61.     }
  62.  
  63.     /**
  64.      * XOR an array of BufferedImages.
  65.      * @param images The array of BufferedImages to XOR together.
  66.      * @return The XOR'ed representation of the array of images.
  67.      */
  68.     public static BufferedImage XORImages(BufferedImage[] images){
  69.         int width = images[0].getWidth();
  70.         int height = images[0].getHeight();
  71.        
  72.         for(int i=1; i<images.length; i++){
  73.             if((images[i].getWidth() != width) || (images[i].getHeight() != height)){
  74.                 System.out.println("[ERROR]: Images are not of same size. Use 'clipImages()' to alleviate this issue.");
  75.                 return null;
  76.             }
  77.         }
  78.        
  79.         BufferedImage returnImage = new BufferedImage(images[0].getWidth(), images[0].getHeight(), BufferedImage.TYPE_INT_RGB);
  80.        
  81.         for(int x=0; x<width; x++){
  82.             for(int y=0; y<height; y++){
  83.                 int rgb = images[0].getRGB(x, y);
  84.                 for(int i=1; i<images.length; i++){
  85.                     rgb ^= images[i].getRGB(x, y);
  86.                 }
  87.                
  88.                 returnImage.setRGB(x, y, rgb);
  89.             }
  90.         }
  91.        
  92.         return returnImage;
  93.     }
  94.    
  95.     /**
  96.      * If the images aren't the same size, XOR won't work, so this function is used to ensure the images are the same size.
  97.      * The resulting image size is ( min{img1.w, img2.w}, min{img1.h, img2.h} ).
  98.      * @param img1 The first image.
  99.      * @param img2 The second image.
  100.      * @return
  101.      */
  102.     public static BufferedImage[] clipImages(BufferedImage img1, BufferedImage img2){
  103.         BufferedImage[] images = new BufferedImage[2];
  104.        
  105.         int height = Math.min(img1.getHeight(), img2.getHeight());
  106.         int width = Math.min(img1.getWidth(), img2.getWidth());
  107.        
  108.         System.out.println(height+" x " + width);
  109.        
  110.         images[0] = img1.getSubimage(0, 0, width, height);
  111.         images[1] = img2.getSubimage(0, 0, width, height);
  112.        
  113.         return images;
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement