Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.ignatieff.xormask;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import javax.imageio.ImageIO;
- public class Encoder {
- /**
- * This function loads an image from the system, given a path.
- * @param path The path to load
- * @return A BufferedImage representing the file on the system.
- * @throws IOException May throw IOException if the path is empty or the file is protected.
- */
- public static BufferedImage loadImage(String path) throws IOException{
- return ImageIO.read(new File(path));
- }
- /**
- * XOR two images together, given their paths on the file system.
- * @param path_of_image Path of the first image.
- * @param path_of_key Path of the second image.
- * @return The XOR'ed representation of the two images.
- */
- public static BufferedImage XORImages(String path_of_image, String path_of_key){
- try {
- return XORImages(clipImages(
- loadImage(path_of_image),
- loadImage(path_of_key)));
- } catch (IOException e) {
- System.out.println("[ERROR]: " + e.getMessage());
- return null;
- }
- }
- /**
- * XOR two images together, and saves the result.
- * @param path_of_image Path of the first image.
- * @param path_of_key Path of the second image.
- * @param path_of_result Path of the result.
- */
- public static void convertImage(String path_of_image, String path_of_key, String path_of_result){
- BufferedImage result = XORImages(path_of_image, path_of_key);
- try {
- saveImage(result, path_of_result);
- } catch (IOException e) {
- System.out.println("[ERROR]: " + e.getMessage());
- }
- }
- /**
- * Saves a BufferedImage on the system.
- * @param image The image to save.
- * @param path The path of the image.
- * @throws IOException If the path is protected, the program may throw an IOException.
- */
- public static void saveImage(BufferedImage image, String path) throws IOException{
- ImageIO.write(image, "png", new File(path));
- }
- /**
- * XOR an array of BufferedImages.
- * @param images The array of BufferedImages to XOR together.
- * @return The XOR'ed representation of the array of images.
- */
- public static BufferedImage XORImages(BufferedImage[] images){
- int width = images[0].getWidth();
- int height = images[0].getHeight();
- for(int i=1; i<images.length; i++){
- if((images[i].getWidth() != width) || (images[i].getHeight() != height)){
- System.out.println("[ERROR]: Images are not of same size. Use 'clipImages()' to alleviate this issue.");
- return null;
- }
- }
- BufferedImage returnImage = new BufferedImage(images[0].getWidth(), images[0].getHeight(), BufferedImage.TYPE_INT_RGB);
- for(int x=0; x<width; x++){
- for(int y=0; y<height; y++){
- int rgb = images[0].getRGB(x, y);
- for(int i=1; i<images.length; i++){
- rgb ^= images[i].getRGB(x, y);
- }
- returnImage.setRGB(x, y, rgb);
- }
- }
- return returnImage;
- }
- /**
- * 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.
- * The resulting image size is ( min{img1.w, img2.w}, min{img1.h, img2.h} ).
- * @param img1 The first image.
- * @param img2 The second image.
- * @return
- */
- public static BufferedImage[] clipImages(BufferedImage img1, BufferedImage img2){
- BufferedImage[] images = new BufferedImage[2];
- int height = Math.min(img1.getHeight(), img2.getHeight());
- int width = Math.min(img1.getWidth(), img2.getWidth());
- System.out.println(height+" x " + width);
- images[0] = img1.getSubimage(0, 0, width, height);
- images[1] = img2.getSubimage(0, 0, width, height);
- return images;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement