1

I have tried to change the red channel value of an image and then reverse it to the original value. I simply add 100 to the red channel value and after that subtract to 100 to get the original image. But it does not work

Below is my code:

    static void encrypt() throws IOException {
        BufferedImage oimage = ImageIO.read(new File("inputjpg.jpg"));
        for (int x = 0; x < oimage.getWidth(); x++) {
            for (int y = 0; y < oimage.getHeight(); y++) {
                int orgb = oimage.getRGB(x, y);
                int nr = red(orgb) + 100;
                if (nr < 255) {
                    oimage.setRGB(x, y, new Color(nr, green(orgb), blue(orgb)).getRGB());
                }
            }
        }
        writeImage(oimage, "encrypt.jpg");
    }

    static void decrypt() throws IOException {
        BufferedImage oimage = ImageIO.read(new File("encrypt.jpg"));
        for (int x = 0; x < oimage.getWidth(); x++) {
            for (int y = 0; y < oimage.getHeight(); y++) {
                int orgb = oimage.getRGB(x, y);
                if (red(orgb) >= 100) {
                    int nr = red(orgb) - 100;
                    oimage.setRGB(x, y, new Color(nr, green(orgb), blue(orgb)).getRGB());
                }
            }
        }
        writeImage(oimage, "origin.jpg");
    }

    static void writeImage(BufferedImage img, String filePath) {
        String format = filePath.substring(filePath.indexOf('.') + 1);
        // Get Picture Format
        System.out.println(format);
        try {
            ImageIO.write(img, format, new File(filePath));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    static int maskAlpha(int rgb, int alpha) {
        // strip alpha from original color
        int color = (0x00ffffff & rgb);
        return color + ((alpha) << 24);
    }

    static int alpha(int rgb) {
        return (0xff & (rgb >> 24));
    }

    static int red(int rgb) {
        return (0xff & rgb);
    }

    static int green(int rgb) {
        return (0xff & (rgb >> 8));
    }

    static int blue(int rgb) {
        return (0xff & (rgb >> 16));
    }

And this is images I used

inputjpg.jpg

enter image description here

encrypt.jpg

enter image description here

origin.jpg

enter image description here

Could you help me to solve it? Thank you!

Robotic Vn
  • 437
  • 2
  • 7
  • 20
  • 3
    That's because the modification you are doing is not reversible. RGB values are between 0 and 255. Suppose that the original R value is 180. Then you add 100: 180 + 100 = 280. But because this is > 255, you're not saving the modified value. When you transform it back, it's impossible to know whether the original pixel value was already > 155 (= 255 - 100). In other words: it's not possible to get the original image back with this algorithm, because you're throwing away information. – Jesper Aug 15 '22 at 09:27
  • @Jesper Ok, I know where I went wrong. Thanks for your help! – Robotic Vn Aug 16 '22 at 00:26
  • I change my method to use nr = 255 - red(orgb) for all pixels and for both encrypt and decrypt. But I also can not get the origin image. Could you give me a suggestion about this case? – Robotic Vn Aug 16 '22 at 00:39

0 Answers0