Skip to main content

Logical operation AND with two image

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
No replies
argb
Offline
Joined: 2014-04-10

Hi, I create a method to do a logical operation between two images; I want to see a third image only with the common part, unfortunately I see blank image. Can you help me to understand this matter?

public BufferedImage calAnd(BufferedImage image1, BufferedImage image2, BufferedImage image3, int height, int width) {

        int argb1, argb2;
        int a1, r1, g1, b1;
        int a2, r2, g2, b2;
        int aAnd, rAnd, gAnd, bAnd;
        int and;
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                argb1 = image1.getRGB(x, y);
                argb2 = image2.getRGB(x, y);

                a1 = (argb1 >> 24) & 0xFF;
                r1 = (argb1 >> 16) & 0xFF;
                g1 = (argb1 >> 8) & 0xFF;
                b1 = (argb1) & 0xFF;

                a2 = (argb2 >> 24) & 0xFF;
                r2 = (argb2 >> 16) & 0xFF;
                g2 = (argb2 >> 8) & 0xFF;
                b2 = (argb2) & 0xFF;

                aAnd = a1 & a2;
                rAnd = r1 & r2;
                gAnd = g1 & g2;
                bAnd = b1 & b2;
              
                and = (aAnd << 24) & (rAnd << 16) & (gAnd << 8) & aAnd;
                image3.setRGB(x, y, and);
            }
        }
        return image3;
    }
    }

Thank you a lot for your attention