Question

I want to XOR two images pixel by pixel. I am using the following code.

 import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.*;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.util.Scanner;
import java.security.*;
import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

public class sefi 
{
    public static void main(String[] args) throws Exception 
    {   
        encdec ed = new encdec();

        String plainimagename = args[0];
        String keyfilename = args[1];
        String choice = args[2];

        BufferedImage bikey = ImageIO.read(new File(keyfilename));
        BufferedImage biplain = ImageIO.read(new File(plainimagename));

        BufferedImage resizedImage = new BufferedImage(biplain.getWidth(), biplain.getHeight(), BufferedImage.TYPE_INT_RGB);
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(bikey, 0, 0, biplain.getWidth(), biplain.getHeight(), null);
    g.dispose();
        ImageIO.write(resizedImage, "jpg", new File("resizeimage.jpg"));

        if(choice.equals("enc"))
        {
            ed.encrypt(resizedImage,biplain);   
        }
        else if(choice.equals("dec"))
        {
            ed.decrypt(resizedImage,biplain);
        }
    }     
}

class encdec
{
    public void encrypt(BufferedImage bikey, BufferedImage biplain) throws Exception
    {
        BufferedImage xoredimage = xor(bikey, biplain);
        File xored = new File("xored.jpg");
        ImageIO.write(xoredimage, "JPEG", xored);
    }

    public void decrypt(BufferedImage bikey, BufferedImage biplain) throws Exception
    {
        BufferedImage xoredimage = xor(bikey, biplain);
        File xored = new File("newplain.jpg");
        ImageIO.write(xoredimage, "JPEG", xored);
    }
    private BufferedImage xor(BufferedImage image1, BufferedImage image2) throws Exception
    {
        BufferedImage outputimage = new BufferedImage(image1.getWidth(), image1.getHeight(), BufferedImage.TYPE_INT_RGB);
        for (int y = 0; y < image1.getHeight(); y++) 
        {
            for (int x = 0; x < image1.getWidth(); x++) 
            {
                outputimage.setRGB(x,y,((image1.getRGB(x, y))^(image2.getRGB(x, y))));
                System.out.println("one:" + image1.getRGB(x, y) + "\ttwo:" + image2.getRGB(x, y) + "\txor:" + ((image1.getRGB(x, y))^(image2.getRGB(x, y))));
                System.out.println("one:" + Integer.toBinaryString(image1.getRGB(x, y)) + "\ttwo:" + Integer.toBinaryString(image2.getRGB(x, y)) + "\txor:" + Integer.toBinaryString((image1.getRGB(x, y)^image2.getRGB(x, y))));
            }
        }

        return outputimage;
    }
}

First time I run this code where image1 is a 4-pixel colored image and image2 is a 4-pixel white image, I get the output as: input: #java sefi white.jpg key.jpg enc

one:-201053 two:-1  xor:201052
one:11111111111111001110111010100011    two:11111111111111111111111111111111    xor:110001000101011100
one:-265579 two:-1  xor:265578
one:11111111111110111111001010010101    two:11111111111111111111111111111111    xor:1000000110101101010
one:-664247 two:-1  xor:664246
one:11111111111101011101110101001001    two:11111111111111111111111111111111    xor:10100010001010110110
one:-925624 two:-1  xor:925623
one:11111111111100011110000001001000    two:11111111111111111111111111111111    xor:11100001111110110111

Next time I run with image1 as the xored image file and image 2 as the 4-pixel colored file, which should give me the original white image as output. But I get this as output instead: Input:#java sefi xored.jpg key.jpg dec

one:-1  two:-16773753   xor:16773752
one:11111111111111111111111111111111    two:11111111000000000000110110000111    xor:111111111111001001111000
one:-1  two:-16773753   xor:16773752
one:11111111111111111111111111111111    two:11111111000000000000110110000111    xor:111111111111001001111000
one:-1  two:-15786601   xor:15786600
one:11111111111111111111111111111111    two:11111111000011110001110110010111    xor:111100001110001001101000
one:-1  two:-15786601   xor:15786600
one:11111111111111111111111111111111    two:11111111000011110001110110010111    xor:111100001110001001101000

If you look at the output we can see that the colors of the xored image from first time has changed. I am not able to understand why I am getting different color value for the same image file.

Was it helpful?

Solution 2

Found the Answer. It is because I am using JPEG images. JPEG compresses raw image, but when it decompress it does not guarantee to produce the exact same colors as it was before compression. Thus the different color values before and after. When I used bmp images, I got the same colors before and after.

OTHER TIPS

There's something wrong with your image selection. If you were selecting the RGB for the generated image then the first pixel would be 110001000101011100 and not 11111111000000000000110110000111.

So my advice is for you to check if you are using the correct images on the second step.

Your code looks ok, although you'd have to paste the whole code for me to have a better idea.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top